diff options
author | Pedro CĂ´rte-Real <pedro@pedrocr.net> | 2019-06-28 22:21:20 +0100 |
---|---|---|
committer | Brian Ashworth <bosrsf04@gmail.com> | 2019-07-14 11:13:55 -0400 |
commit | e3a3917d3afb66fc8ba3eebb7aed603d3b7ce844 (patch) | |
tree | 6af979d7b1c1aeed9d88f168a9a0d1e1f7e663c0 /sway/tree | |
parent | 1312b5bb9faf7ab50c3024fc4f05b58481bf318c (diff) | |
download | sway-e3a3917d3afb66fc8ba3eebb7aed603d3b7ce844.tar.xz |
Layout tiled using a width/height fraction
Instead of using container->width/height as both the input and output
of the layout calculation have container->width_fraction/height_fraction
as the share of the parent this container occupies and calculate the
layout based on that. That way the container arrangement can always be
recalculated even if width/height have been altered by things like
fullscreen.
To do this several parts are reworked:
- The vertical and horizontal arrangement code is ajusted to work with
fractions instead of directly with width/height
- The resize code is then changed to manipulate the fractions when
working on tiled containers.
- Finally the places that manipulated width/height are adjusted to
match. The adjusted parts are container split, swap, and the input
seat code.
It's possible that some parts of the code are now adjusting width and
height only for those to be immediately recalculated. That's harmless
and since non-tiled containers are still sized with width/height
directly it may avoid breaking other corner cases.
Fixes #3547
Fixes #4297
Diffstat (limited to 'sway/tree')
-rw-r--r-- | sway/tree/arrange.c | 68 | ||||
-rw-r--r-- | sway/tree/container.c | 8 |
2 files changed, 50 insertions, 26 deletions
diff --git a/sway/tree/arrange.c b/sway/tree/arrange.c index fc5d49ed..dd0a72cd 100644 --- a/sway/tree/arrange.c +++ b/sway/tree/arrange.c @@ -18,39 +18,49 @@ static void apply_horiz_layout(list_t *children, struct wlr_box *parent) { return; } - // Count the number of new windows we are resizing + // Count the number of new windows we are resizing, and how much space + // is currently occupied int new_children = 0; + double current_width_fraction = 0; for (int i = 0; i < children->length; ++i) { struct sway_container *child = children->items[i]; - if (child->width <= 0) { + current_width_fraction += child->width_fraction; + if (child->width_fraction <= 0) { new_children += 1; } } - // Calculate total width of children - double total_width = 0; + // Calculate each height fraction + double total_width_fraction = 0; for (int i = 0; i < children->length; ++i) { struct sway_container *child = children->items[i]; - if (child->width <= 0) { - if (children->length > new_children) { - child->width = parent->width / (children->length - new_children); + if (child->width_fraction <= 0) { + if (current_width_fraction <= 0) { + child->width_fraction = 1.0; + } else if (children->length > new_children) { + child->width_fraction = current_width_fraction / + (children->length - new_children); } else { - child->width = parent->width; + child->width_fraction = current_width_fraction; } } - container_remove_gaps(child); - total_width += child->width; + total_width_fraction += child->width_fraction; + } + // Normalize width fractions so the sum is 1.0 + for (int i = 0; i < children->length; ++i) { + struct sway_container *child = children->items[i]; + child->width_fraction /= total_width_fraction; } - double scale = parent->width / total_width; // Resize windows sway_log(SWAY_DEBUG, "Arranging %p horizontally", parent); double child_x = parent->x; for (int i = 0; i < children->length; ++i) { struct sway_container *child = children->items[i]; + container_remove_gaps(child); child->x = child_x; child->y = parent->y; - child->width = floor(child->width * scale); + child->width = floor(child->width_fraction * parent->width); child->height = parent->height; child_x += child->width; @@ -67,40 +77,50 @@ static void apply_vert_layout(list_t *children, struct wlr_box *parent) { return; } - // Count the number of new windows we are resizing + // Count the number of new windows we are resizing, and how much space + // is currently occupied int new_children = 0; + double current_height_fraction = 0; for (int i = 0; i < children->length; ++i) { struct sway_container *child = children->items[i]; - if (child->height <= 0) { + current_height_fraction += child->height_fraction; + if (child->height_fraction <= 0) { new_children += 1; } } - // Calculate total height of children - double total_height = 0; + // Calculate each height fraction + double total_height_fraction = 0; for (int i = 0; i < children->length; ++i) { struct sway_container *child = children->items[i]; - if (child->height <= 0) { - if (children->length > new_children) { - child->height = parent->height / (children->length - new_children); + if (child->height_fraction <= 0) { + if (current_height_fraction <= 0) { + child->height_fraction = 1.0; + } else if (children->length > new_children) { + child->height_fraction = current_height_fraction / + (children->length - new_children); } else { - child->height = parent->height; + child->height_fraction = current_height_fraction; } } - container_remove_gaps(child); - total_height += child->height; + total_height_fraction += child->height_fraction; + } + // Normalize height fractions so the sum is 1.0 + for (int i = 0; i < children->length; ++i) { + struct sway_container *child = children->items[i]; + child->height_fraction /= total_height_fraction; } - double scale = parent->height / total_height; // Resize sway_log(SWAY_DEBUG, "Arranging %p vertically", parent); double child_y = parent->y; for (int i = 0; i < children->length; ++i) { struct sway_container *child = children->items[i]; + container_remove_gaps(child); child->x = parent->x; child->y = child_y; child->width = parent->width; - child->height = floor(child->height * scale); + child->height = floor(child->height_fraction * parent->height); child_y += child->height; // Make last child use remaining height of parent diff --git a/sway/tree/container.c b/sway/tree/container.c index 9046ae27..068dbb88 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -789,6 +789,8 @@ void container_set_floating(struct sway_container *container, bool enable) { container->border = container->saved_border; } } + container->width_fraction = 0; + container->height_fraction = 0; } container_end_mouse_operation(container); @@ -1022,9 +1024,9 @@ void container_fullscreen_disable(struct sway_container *con) { if (container_is_floating(con)) { con->x = con->saved_x; con->y = con->saved_y; + con->width = con->saved_width; + con->height = con->saved_height; } - con->width = con->saved_width; - con->height = con->saved_height; if (con->fullscreen_mode == FULLSCREEN_WORKSPACE) { if (con->workspace) { @@ -1415,6 +1417,8 @@ struct sway_container *container_split(struct sway_container *child, struct sway_container *cont = container_create(NULL); cont->width = child->width; cont->height = child->height; + cont->width_fraction = child->width_fraction; + cont->height_fraction = child->height_fraction; cont->x = child->x; cont->y = child->y; cont->current_gaps = child->current_gaps; |