Handle resize in auto layouts

master
wil 8 years ago
parent 97f7d47413
commit 5425d0489f

@ -62,225 +62,216 @@ static bool resize_floating(int amount, bool use_width) {
return false; return false;
} }
static bool resize_tiled(int amount, bool use_width) { /**
swayc_t *parent = get_focused_view(swayc_active_workspace()); * returns the index of the container's child that is first in a group.
swayc_t *focused = parent; * This index is > to the <after> argument.
swayc_t *sibling; * This makes the function usable to walk through the groups in a container.
if (!parent) { */
return true; static int next_group_index(swayc_t *container, int after) {
} if (after < 0) {
// Find the closest parent container which has siblings of the proper layout. return 0;
// Then apply the resize to all of them. } else if (is_auto_layout(container->layout)) {
int i; if ((uint_fast32_t) after < container->nb_master) {
if (use_width) { return container->nb_master;
int lnumber = 0; } else {
int rnumber = 0; uint_fast32_t grp_idx = 0;
while (parent->parent) { for (int i = container->nb_master; i < container->children->length; ) {
if (parent->parent->layout == L_HORIZ && parent->parent->children) { uint_fast32_t grp_sz = (container->children->length - i) /
for (i = 0; i < parent->parent->children->length; i++) { (container->nb_slave_groups - grp_idx);
sibling = parent->parent->children->items[i]; if (after - i < (int) grp_sz) {
if (sibling->x != focused->x) { return i + grp_sz;
if (sibling->x < parent->x) {
lnumber++;
} else if (sibling->x > parent->x) {
rnumber++;
}
}
}
if (rnumber || lnumber) {
break;
} }
i += grp_sz;
} }
parent = parent->parent; return container->children->length;
}
} else {
// return after + 1;
return container->children->length;
}
}
/**
* Return the number of children in the slave groups. This corresponds to the children
* that are not members of the master group.
*/
static inline uint_fast32_t slave_count(swayc_t *container) {
return container->children->length - container->nb_master;
}
/**
* given the index of a container's child, return the index of the first child of the group
* which index is a member of.
*/
static int group_start_index(swayc_t *container, int index) {
if (index < 0 || ! is_auto_layout(container->layout) || (uint_fast32_t) index < container->nb_master) {
return 0;
} else {
uint_fast32_t grp_sz = slave_count(container) / container->nb_slave_groups;
uint_fast32_t remainder = slave_count(container) % container->nb_slave_groups;
if ((index - container->nb_master) / grp_sz < container->nb_slave_groups - remainder) {
return ((index - container->nb_master) / grp_sz) * grp_sz + container->nb_master;
} else {
int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master;
return idx2 + ((idx2 - index) / (grp_sz + 1)) * (grp_sz + 1);
} }
if (parent == &root_container) { }
return true; }
/**
* given the index of a container's child, return the index of the first child of the group
* that follows the one which index is a member of.
*/
static int group_end_index(swayc_t *container, int index) {
if (index < 0 || ! is_auto_layout(container->layout)) {
return container->children->length;
} else {
uint_fast32_t grp_sz = slave_count(container) / container->nb_slave_groups;
uint_fast32_t remainder = slave_count(container) % container->nb_slave_groups;
if ((index - container->nb_master) / grp_sz < container->nb_slave_groups - remainder) {
return ((index - container->nb_master) / grp_sz + 1) * grp_sz + container->nb_master;
} else {
int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master;
return idx2 + ((idx2 - index) / (grp_sz + 1) + 1) * (grp_sz + 1);
} }
sway_log(L_DEBUG, "Found the proper parent: %p. It has %d l conts, and %d r conts", parent->parent, lnumber, rnumber); }
//TODO: Ensure rounding is done in such a way that there are NO pixel leaks }
bool valid = true;
for (i = 0; i < parent->parent->children->length; i++) { /**
sibling = parent->parent->children->items[i]; * Return the combined number of master and slave groups in the container.
if (sibling->x != focused->x) { */
if (sibling->x < parent->x) { static inline uint_fast32_t group_count(swayc_t *container) {
double pixels = -1 * amount; return MIN(container->nb_slave_groups, slave_count(container)) + (container->nb_master ? 1 : 0);
pixels /= lnumber; }
if (rnumber) {
if ((sibling->width + pixels/2) < min_sane_w) { /**
valid = false; * return the index of the Group containing <index>th child of <container>.
break; * The index is the order of the group along the container's major axis (starting at 0).
} */
} else { static uint_fast32_t group_index(swayc_t *container, int index) {
if ((sibling->width + pixels) < min_sane_w) { bool master_first = (container->layout == L_AUTO_LEFT || container->layout == L_AUTO_TOP);
valid = false; int nb_slaves = slave_count(container);
break; if (index < (int) container->nb_master) {
} if (master_first || nb_slaves <= 0) {
} return 0;
} else if (sibling->x > parent->x) { } else {
double pixels = -1 * amount; return MIN(container->nb_slave_groups, nb_slaves);
pixels /= rnumber; }
if (lnumber) { } else {
if ((sibling->width + pixels/2) < min_sane_w) { uint_fast32_t grp_idx = 0;
valid = false; for (int i = container->nb_master; i < container->children->length; ) {
break; uint_fast32_t grp_sz = (container->children->length - i) /
} (container->nb_slave_groups - grp_idx);
} else { if (index - i < (int) grp_sz) {
if ((sibling->width + pixels) < min_sane_w) { break;
valid = false;
break;
}
}
}
} else {
double pixels = amount;
if (parent->width + pixels < min_sane_w) {
valid = false;
break;
}
} }
} }
if (valid) { return grp_idx + (master_first ? 1 : 0);
for (i = 0; i < parent->parent->children->length; i++) { }
sibling = parent->parent->children->items[i]; }
if (sibling->x != focused->x) {
if (sibling->x < parent->x) { static bool resize_tiled(int amount, bool use_width) {
double pixels = -1 * amount; swayc_t *container = get_focused_view(swayc_active_workspace());
pixels /= lnumber; swayc_t *parent = container->parent;
if (rnumber) { int idx_focused = 0;
recursive_resize(sibling, pixels/2, WLC_RESIZE_EDGE_RIGHT); bool use_major = false;
} else { uint_fast32_t nb_before = 0;
recursive_resize(sibling, pixels, WLC_RESIZE_EDGE_RIGHT); uint_fast32_t nb_after = 0;
}
} else if (sibling->x > parent->x) { // 1. Identify a container ancestor that will allow the focused child to grow in the requested
double pixels = -1 * amount; // direction.
pixels /= rnumber; while (container->parent) {
if (lnumber) { parent = container->parent;
recursive_resize(sibling, pixels/2, WLC_RESIZE_EDGE_LEFT); if ((parent->children && parent->children->length > 1) &&
} else { (is_auto_layout(parent->layout) || (use_width ? parent->layout == L_HORIZ :
recursive_resize(sibling, pixels, WLC_RESIZE_EDGE_LEFT); parent->layout == L_VERT))) {
} // check if container has siblings that can provide/absorb the space needed for
} // the resize operation.
} else { use_major = use_width
if (rnumber != 0 && lnumber != 0) { ? parent->layout == L_AUTO_LEFT || parent->layout == L_AUTO_RIGHT
double pixels = amount; : parent->layout == L_AUTO_TOP || parent->layout == L_AUTO_BOTTOM;
pixels /= 2; // Note: use_major will be false for L_HORIZ and L_VERT
recursive_resize(parent, pixels, WLC_RESIZE_EDGE_LEFT);
recursive_resize(parent, pixels, WLC_RESIZE_EDGE_RIGHT); idx_focused = index_child(container);
} else if (rnumber) { if (idx_focused < 0) {
recursive_resize(parent, amount, WLC_RESIZE_EDGE_RIGHT); sway_log(L_ERROR, "Something weird is happening, child container not "
} else if (lnumber) { "present in its parent's children list.");
recursive_resize(parent, amount, WLC_RESIZE_EDGE_LEFT); continue;
} }
} if (use_major) {
nb_before = group_index(parent, idx_focused);
nb_after = group_count(parent) - nb_before - 1;
} else {
nb_before = idx_focused - group_start_index(parent, idx_focused);
nb_after = next_group_index(parent, idx_focused) - idx_focused - 1;
}
if (nb_before || nb_after) {
break;
} }
// Recursive resize does not handle positions, let arrange_windows
// take care of that.
arrange_windows(swayc_active_workspace(), -1, -1);
} }
container = parent; /* continue up the tree to the next ancestor */
}
if (parent == &root_container) {
return true; return true;
} else { }
int tnumber = 0; sway_log(L_DEBUG, "Found the proper parent: %p. It has %" PRIuFAST32 " before conts, and %"
int bnumber = 0; PRIuFAST32 " after conts", parent, nb_before, nb_after);
while (parent->parent) { // 2. Ensure that the resize operation will not make one of the resized containers drop
if (parent->parent->layout == L_VERT) { // below the "sane" size threshold.
for (i = 0; i < parent->parent->children->length; i++) { bool valid = true;
sibling = parent->parent->children->items[i]; swayc_t *focused = parent->children->items[idx_focused];
if (sibling->y != focused->y) { int start = use_major ? 0 : group_start_index(parent, idx_focused);
if (sibling->y < parent->y) { int end = use_major ? parent->children->length : group_end_index(parent, idx_focused);
bnumber++; for (int i = start; i < end; ) {
} else if (sibling->y > parent->y) { swayc_t *sibling = parent->children->items[i];
tnumber++; double pixels = amount;
} bool is_before = use_width ? sibling->x < focused->x : sibling->y < focused->y;
} bool is_after = use_width ? sibling->x > focused->x : sibling->y > focused->y;
} if (is_before || is_after) {
if (bnumber || tnumber) { pixels = -pixels;
break; pixels /= is_before ? nb_before : nb_after;
} if (nb_after != 0 && nb_before != 0) {
pixels /= 2;
} }
parent = parent->parent;
} }
if (parent->parent == NULL || parent->parent->children == NULL) { if (use_width ?
return true; sibling->width + pixels < min_sane_w :
sibling->height + pixels < min_sane_h) {
valid = false;
break;
} }
sway_log(L_DEBUG, "Found the proper parent: %p. It has %d b conts, and %d t conts", parent->parent, bnumber, tnumber); i = use_major ? next_group_index(parent, i) : (i + 1);
//TODO: Ensure rounding is done in such a way that there are NO pixel leaks }
bool valid = true; // 3. Apply the size change
for (i = 0; i < parent->parent->children->length; i++) { if (valid) {
sibling = parent->parent->children->items[i]; for (int i = 0; i < parent->children->length; ++i) {
if (sibling->y != focused->y) { swayc_t *sibling = parent->children->items[i];
if (sibling->y < parent->y) { double pixels = amount;
double pixels = -1 * amount; bool is_before = use_width ? sibling->x < focused->x : sibling->y < focused->y;
pixels /= bnumber; bool is_after = use_width ? sibling->x > focused->x : sibling->y > focused->y;
if (tnumber) { if (is_before || is_after) {
if ((sibling->height + pixels/2) < min_sane_h) { pixels = -pixels;
valid = false; pixels /= is_before ? nb_before : nb_after;
break; if (nb_after != 0 && nb_before != 0) {
} pixels /= 2;
} else {
if ((sibling->height + pixels) < min_sane_h) {
valid = false;
break;
}
}
} else if (sibling->y > parent->y) {
double pixels = -1 * amount;
pixels /= tnumber;
if (bnumber) {
if ((sibling->height + pixels/2) < min_sane_h) {
valid = false;
break;
}
} else {
if ((sibling->height + pixels) < min_sane_h) {
valid = false;
break;
}
}
} }
sway_log(L_DEBUG, "%p: %s", sibling, is_before ? "before" : "after");
recursive_resize(sibling, pixels,
use_width ?
(is_before ? WLC_RESIZE_EDGE_RIGHT : WLC_RESIZE_EDGE_LEFT) :
(is_before ? WLC_RESIZE_EDGE_BOTTOM : WLC_RESIZE_EDGE_TOP));
} else { } else {
double pixels = amount; sway_log(L_DEBUG, "%p: same pos", sibling);
if (parent->height + pixels < min_sane_h) { recursive_resize(sibling, pixels,
valid = false; use_width ? WLC_RESIZE_EDGE_LEFT : WLC_RESIZE_EDGE_TOP);
break; recursive_resize(sibling, pixels,
} use_width ? WLC_RESIZE_EDGE_RIGHT : WLC_RESIZE_EDGE_BOTTOM);
}
}
if (valid) {
for (i = 0; i < parent->parent->children->length; i++) {
sibling = parent->parent->children->items[i];
if (sibling->y != focused->y) {
if (sibling->y < parent->y) {
double pixels = -1 * amount;
pixels /= bnumber;
if (tnumber) {
recursive_resize(sibling, pixels/2, WLC_RESIZE_EDGE_BOTTOM);
} else {
recursive_resize(sibling, pixels, WLC_RESIZE_EDGE_BOTTOM);
}
} else if (sibling->x > parent->x) {
double pixels = -1 * amount;
pixels /= tnumber;
if (bnumber) {
recursive_resize(sibling, pixels/2, WLC_RESIZE_EDGE_TOP);
} else {
recursive_resize(sibling, pixels, WLC_RESIZE_EDGE_TOP);
}
}
} else {
if (bnumber != 0 && tnumber != 0) {
double pixels = amount/2;
recursive_resize(parent, pixels, WLC_RESIZE_EDGE_TOP);
recursive_resize(parent, pixels, WLC_RESIZE_EDGE_BOTTOM);
} else if (tnumber) {
recursive_resize(parent, amount, WLC_RESIZE_EDGE_TOP);
} else if (bnumber) {
recursive_resize(parent, amount, WLC_RESIZE_EDGE_BOTTOM);
}
}
} }
arrange_windows(swayc_active_workspace(), -1, -1);
} }
return true; // Recursive resize does not handle positions, let arrange_windows
// take care of that.
arrange_windows(swayc_active_workspace(), -1, -1);
} }
return true; return true;
} }

Loading…
Cancel
Save