diff options
author | Ryan Dwyer <ryandwyer1@gmail.com> | 2018-04-28 11:26:14 +1000 |
---|---|---|
committer | Ryan Dwyer <ryandwyer1@gmail.com> | 2018-04-28 11:26:14 +1000 |
commit | 167c2e1aa99aa8011e169b0fb61c47953cc9a5f6 (patch) | |
tree | f312cac40cd542964f62d8b7ed53d44bb13dea37 /sway/tree | |
parent | 85ec7002068277fe82dcc139ebb401b515ca5d80 (diff) |
Refactor arrange_windows()
Replaces arrange_windows() with arrange_root(), arrange_output(),
arrange_workspace() and arrange_children_of().
Also makes fullscreen views save and restore their dimensions, which
allows it to preserve any custom resize and is also a requirement for
floating views once they are implemented.
Diffstat (limited to 'sway/tree')
-rw-r--r-- | sway/tree/arrange.c | 239 | ||||
-rw-r--r-- | sway/tree/container.c | 4 | ||||
-rw-r--r-- | sway/tree/layout.c | 279 | ||||
-rw-r--r-- | sway/tree/view.c | 30 | ||||
-rw-r--r-- | sway/tree/workspace.c | 3 |
5 files changed, 288 insertions, 267 deletions
diff --git a/sway/tree/arrange.c b/sway/tree/arrange.c new file mode 100644 index 00000000..dd87ce7c --- /dev/null +++ b/sway/tree/arrange.c @@ -0,0 +1,239 @@ +#define _POSIX_C_SOURCE 200809L +#include <ctype.h> +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> +#include <wlr/types/wlr_output.h> +#include <wlr/types/wlr_output_layout.h> +#include "sway/debug.h" +#include "sway/tree/arrange.h" +#include "sway/tree/container.h" +#include "sway/tree/layout.h" +#include "sway/output.h" +#include "sway/tree/workspace.h" +#include "sway/tree/view.h" +#include "list.h" +#include "log.h" + +struct sway_container root_container; + +void arrange_windows(struct sway_container *container) { + switch (container->type) { + case C_ROOT: + arrange_root(); + break; + case C_OUTPUT: + arrange_output(container); + break; + case C_WORKSPACE: + arrange_workspace(container); + break; + case C_CONTAINER: + arrange_children_of(container); + break; + case C_VIEW: + // ignore + break; + case C_TYPES: + sway_assert( + false, "Called arrange_windows() with container type C_TYPES"); + break; + } +} + +void arrange_root() { + if (config->reloading) { + return; + } + struct wlr_output_layout *output_layout = + root_container.sway_root->output_layout; + const struct wlr_box *layout_box = + wlr_output_layout_get_box(output_layout, NULL); + root_container.x = layout_box->x; + root_container.y = layout_box->y; + root_container.width = layout_box->width; + root_container.height = layout_box->height; + for (int i = 0; i < root_container.children->length; ++i) { + struct sway_container *output = root_container.children->items[i]; + arrange_output(output); + } +} + +void arrange_output(struct sway_container *output) { + if (config->reloading) { + return; + } + if (!sway_assert(output->type == C_OUTPUT, + "called arrange_output() on non-output container")) { + return; + } + const struct wlr_box *output_box = wlr_output_layout_get_box( + root_container.sway_root->output_layout, + output->sway_output->wlr_output); + output->x = output_box->x; + output->y = output_box->y; + output->width = output_box->width; + output->height = output_box->height; + wlr_log(L_DEBUG, "Arranging output '%s' at %f,%f", + output->name, output->x, output->y); + for (int i = 0; i < output->children->length; ++i) { + struct sway_container *workspace = output->children->items[i]; + arrange_workspace(workspace); + } + container_damage_whole(output); +} + +void arrange_workspace(struct sway_container *workspace) { + if (config->reloading) { + return; + } + if (!sway_assert(workspace->type == C_WORKSPACE, + "called arrange_workspace() on non-workspace container")) { + return; + } + struct sway_container *output = workspace->parent; + struct wlr_box *area = &output->sway_output->usable_area; + wlr_log(L_DEBUG, "Usable area for ws: %dx%d@%d,%d", + area->width, area->height, area->x, area->y); + workspace->width = area->width; + workspace->height = area->height; + workspace->x = area->x; + workspace->y = area->y; + wlr_log(L_DEBUG, "Arranging workspace '%s' at %f, %f", + workspace->name, workspace->x, workspace->y); + arrange_children_of(workspace); + container_damage_whole(workspace); +} + +static void apply_horiz_layout(struct sway_container *parent) { + size_t num_children = parent->children->length; + if (!num_children) { + return; + } + // Calculate total width of children + double total_width = 0; + for (size_t i = 0; i < num_children; ++i) { + struct sway_container *child = parent->children->items[i]; + if (child->width <= 0) { + if (num_children > 1) { + child->width = parent->width / (num_children - 1); + } else { + child->width = parent->width; + } + } + total_width += child->width; + } + double scale = parent->width / total_width; + + // Resize windows + wlr_log(L_DEBUG, "Arranging %p horizontally", parent); + double child_x = parent->x; + struct sway_container *child; + for (size_t i = 0; i < num_children; ++i) { + child = parent->children->items[i]; + wlr_log(L_DEBUG, + "Calculating arrangement for %p:%d (will scale %f by %f)", + child, child->type, child->width, scale); + child->x = child_x; + child->y = parent->y; + child->width = floor(child->width * scale); + child->height = parent->height; + child_x += child->width; + } + // Make last child use remaining width of parent + child->width = parent->x + parent->width - child->x; +} + +static void apply_vert_layout(struct sway_container *parent) { + size_t num_children = parent->children->length; + if (!num_children) { + return; + } + // Calculate total height of children + double total_height = 0; + for (size_t i = 0; i < num_children; ++i) { + struct sway_container *child = parent->children->items[i]; + if (child->height <= 0) { + if (num_children > 1) { + child->height = parent->height / (num_children - 1); + } else { + child->height = parent->height; + } + } + total_height += child->height; + } + double scale = parent->height / total_height; + + // Resize + wlr_log(L_DEBUG, "Arranging %p vertically", parent); + double child_y = parent->y; + struct sway_container *child; + for (size_t i = 0; i < num_children; ++i) { + child = parent->children->items[i]; + wlr_log(L_DEBUG, + "Calculating arrangement for %p:%d (will scale %f by %f)", + child, child->type, child->height, scale); + child->x = parent->x; + child->y = child_y; + child->width = parent->width; + child->height = floor(child->height * scale); + child_y += child->height; + } + // Make last child use remaining height of parent + child->height = parent->y + parent->height - child->y; +} + +void arrange_children_of(struct sway_container *parent) { + if (config->reloading) { + return; + } + if (!sway_assert(parent->type == C_WORKSPACE || parent->type == C_CONTAINER, + "container is a %s", container_type_to_str(parent->type))) { + return; + } + + struct sway_container *workspace = parent; + if (workspace->type != C_WORKSPACE) { + workspace = container_parent(workspace, C_WORKSPACE); + } + if (workspace->sway_workspace->fullscreen) { + // Just arrange the fullscreen view and jump out + struct sway_container *view = + workspace->sway_workspace->fullscreen->swayc; + view_configure(view->sway_view, 0, 0, + workspace->parent->width, workspace->parent->height); + view->width = workspace->parent->width; + view->height = workspace->parent->height; + return; + } + + wlr_log(L_DEBUG, "Arranging layout for %p %s %fx%f+%f,%f", parent, + parent->name, parent->width, parent->height, parent->x, parent->y); + + // Calculate x, y, width and height of children + switch (parent->layout) { + case L_HORIZ: + apply_horiz_layout(parent); + break; + case L_VERT: + apply_vert_layout(parent); + break; + default: + wlr_log(L_DEBUG, "TODO: arrange layout type %d", parent->layout); + apply_horiz_layout(parent); + break; + } + + // Apply x, y, width and height to children and recurse if needed + for (int i = 0; i < parent->children->length; ++i) { + struct sway_container *child = parent->children->items[i]; + if (child->type == C_VIEW) { + view_configure(child->sway_view, child->x, child->y, + child->width, child->height); + } else { + arrange_children_of(child); + } + } + container_damage_whole(parent); + update_debug_tree(); +} diff --git a/sway/tree/container.c b/sway/tree/container.c index 09a6b7ce..03d7e49c 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -13,6 +13,7 @@ #include "sway/ipc-server.h" #include "sway/output.h" #include "sway/server.h" +#include "sway/tree/arrange.h" #include "sway/tree/layout.h" #include "sway/tree/view.h" #include "sway/tree/workspace.h" @@ -143,8 +144,7 @@ static struct sway_container *container_output_destroy( container_add_child(root_container.children->items[p], child); } container_sort_workspaces(root_container.children->items[p]); - arrange_windows(root_container.children->items[p], - -1, -1); + arrange_output(root_container.children->items[p]); } } diff --git a/sway/tree/layout.c b/sway/tree/layout.c index a64cc9a9..ec1c6fe5 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -7,6 +7,7 @@ #include <wlr/types/wlr_output.h> #include <wlr/types/wlr_output_layout.h> #include "sway/debug.h" +#include "sway/tree/arrange.h" #include "sway/tree/container.h" #include "sway/tree/layout.h" #include "sway/output.h" @@ -21,16 +22,7 @@ struct sway_container root_container; static void output_layout_handle_change(struct wl_listener *listener, void *data) { - struct wlr_output_layout *output_layout = - root_container.sway_root->output_layout; - const struct wlr_box *layout_box = - wlr_output_layout_get_box(output_layout, NULL); - root_container.x = layout_box->x; - root_container.y = layout_box->y; - root_container.width = layout_box->width; - root_container.height = layout_box->height; - - arrange_windows(&root_container, layout_box->width, layout_box->height); + arrange_root(); } void layout_init(void) { @@ -91,13 +83,15 @@ static void container_handle_fullscreen_reparent(struct sway_container *viewcon, // Mark the new workspace as fullscreen if (new_workspace->sway_workspace->fullscreen) { - view_set_fullscreen(new_workspace->sway_workspace->fullscreen, false); + view_set_fullscreen_raw( + new_workspace->sway_workspace->fullscreen, false); } new_workspace->sway_workspace->fullscreen = view; // Resize view to new output dimensions - struct sway_output *output = new_workspace->parent->sway_output; - view_configure(view, 0, 0, - output->wlr_output->width, output->wlr_output->height); + struct sway_container *output = new_workspace->parent; + view_configure(view, 0, 0, output->width, output->height); + view->swayc->width = output->width; + view->swayc->height = output->height; } void container_insert_child(struct sway_container *parent, @@ -166,6 +160,7 @@ void container_move_to(struct sway_container *container, } struct sway_container *old_parent = container_remove_child(container); container->width = container->height = 0; + container->saved_width = container->saved_height = 0; struct sway_container *new_parent; if (destination->type == C_VIEW) { new_parent = container_add_sibling(destination, container); @@ -188,9 +183,9 @@ void container_move_to(struct sway_container *container, seat_set_focus(seat, new_parent); } if (old_parent) { - arrange_windows(old_parent, -1, -1); + arrange_children_of(old_parent); } - arrange_windows(new_parent, -1, -1); + arrange_children_of(new_parent); // If view was moved to a fullscreen workspace, refocus the fullscreen view struct sway_container *new_workspace = container; if (new_workspace->type != C_WORKSPACE) { @@ -299,7 +294,7 @@ static void workspace_rejigger(struct sway_container *ws, container_reap_empty_recursive(original_parent); wl_signal_emit(&child->events.reparent, original_parent); container_create_notify(new_parent); - arrange_windows(ws, -1, -1); + arrange_workspace(ws); } void container_move(struct sway_container *container, @@ -378,7 +373,7 @@ void container_move(struct sway_container *container, move_dir == MOVE_LEFT || move_dir == MOVE_RIGHT ? L_HORIZ : L_VERT; container_insert_child(current, container, offs < 0 ? 0 : 1); - arrange_windows(current, -1, -1); + arrange_workspace(current); } return; } else { @@ -402,8 +397,8 @@ void container_move(struct sway_container *container, container_insert_child(current->parent, container, index + (offs < 0 ? 0 : 1)); container->width = container->height = 0; - arrange_windows(current->parent, -1, -1); - arrange_windows(old_parent, -1, -1); + arrange_children_of(current->parent); + arrange_children_of(old_parent); return; } } else { @@ -433,14 +428,14 @@ void container_move(struct sway_container *container, wlr_log(L_DEBUG, "Swapping siblings"); sibling->parent->children->items[index + offs] = container; sibling->parent->children->items[index] = sibling; - arrange_windows(sibling->parent, -1, -1); + arrange_children_of(sibling->parent); } else { wlr_log(L_DEBUG, "Promoting to sibling of cousin"); container_insert_child(sibling->parent, container, index_child(sibling) + (offs > 0 ? 0 : 1)); container->width = container->height = 0; - arrange_windows(sibling->parent, -1, -1); - arrange_windows(old_parent, -1, -1); + arrange_children_of(sibling->parent); + arrange_children_of(old_parent); } sibling = NULL; break; @@ -454,8 +449,8 @@ void container_move(struct sway_container *container, "(move dir: %d)", limit, move_dir); container_insert_child(sibling, container, limit); container->width = container->height = 0; - arrange_windows(sibling, -1, -1); - arrange_windows(old_parent, -1, -1); + arrange_children_of(sibling); + arrange_children_of(old_parent); sibling = NULL; } else { wlr_log(L_DEBUG, "Reparenting container (perpendicular)"); @@ -478,8 +473,8 @@ void container_move(struct sway_container *container, container_add_child(sibling, container); } container->width = container->height = 0; - arrange_windows(sibling, -1, -1); - arrange_windows(old_parent, -1, -1); + arrange_children_of(sibling); + arrange_children_of(old_parent); sibling = NULL; } break; @@ -553,234 +548,6 @@ void container_sort_workspaces(struct sway_container *output) { list_stable_sort(output->children, sort_workspace_cmp_qsort); } -static void apply_horiz_layout(struct sway_container *container, const double x, - const double y, const double width, - const double height, const int start, - const int end); - -static void apply_vert_layout(struct sway_container *container, const double x, - const double y, const double width, - const double height, const int start, - const int end); - -void arrange_windows(struct sway_container *container, - double width, double height) { - if (config->reloading) { - return; - } - int i; - if (width == -1 || height == -1) { - width = container->width; - height = container->height; - } - // pixels are indivisible. if we don't round the pixels, then the view - // calculations will be off (e.g. 50.5 + 50.5 = 101, but in reality it's - // 50 + 50 = 100). doing it here cascades properly to all width/height/x/y. - width = floor(width); - height = floor(height); - - wlr_log(L_DEBUG, "Arranging layout for %p %s %fx%f+%f,%f", container, - container->name, container->width, container->height, container->x, - container->y); - - double x = 0, y = 0; - switch (container->type) { - case C_ROOT: - for (i = 0; i < container->children->length; ++i) { - struct sway_container *output = container->children->items[i]; - const struct wlr_box *output_box = wlr_output_layout_get_box( - container->sway_root->output_layout, - output->sway_output->wlr_output); - output->x = output_box->x; - output->y = output_box->y; - output->width = output_box->width; - output->height = output_box->height; - wlr_log(L_DEBUG, "Arranging output '%s' at %f,%f", - output->name, output->x, output->y); - arrange_windows(output, output_box->width, output_box->height); - } - return; - case C_OUTPUT: - // arrange all workspaces: - for (i = 0; i < container->children->length; ++i) { - struct sway_container *child = container->children->items[i]; - arrange_windows(child, -1, -1); - } - return; - case C_WORKSPACE: - { - struct sway_container *output = - container_parent(container, C_OUTPUT); - struct wlr_box *area = &output->sway_output->usable_area; - wlr_log(L_DEBUG, "Usable area for ws: %dx%d@%d,%d", - area->width, area->height, area->x, area->y); - container->width = width = area->width; - container->height = height = area->height; - container->x = x = area->x; - container->y = y = area->y; - wlr_log(L_DEBUG, "Arranging workspace '%s' at %f, %f", - container->name, container->x, container->y); - if (container->sway_workspace->fullscreen) { - view_configure(container->sway_workspace->fullscreen, 0, 0, - output->width, output->height); - return; - } - } - // children are properly handled below - break; - case C_VIEW: - { - container->width = width; - container->height = height; - view_configure(container->sway_view, container->x, container->y, - container->width, container->height); - wlr_log(L_DEBUG, "Set view to %.f x %.f @ %.f, %.f", - container->width, container->height, - container->x, container->y); - } - return; - default: - container->width = width; - container->height = height; - x = container->x; - y = container->y; - break; - } - - switch (container->layout) { - case L_HORIZ: - apply_horiz_layout(container, x, y, width, height, 0, - container->children->length); - break; - case L_VERT: - apply_vert_layout(container, x, y, width, height, 0, - container->children->length); - break; - default: - wlr_log(L_DEBUG, "TODO: arrange layout type %d", container->layout); - apply_horiz_layout(container, x, y, width, height, 0, - container->children->length); - break; - } - container_damage_whole(container); - // TODO: Make this less shitty - update_debug_tree(); -} - -static void apply_horiz_layout(struct sway_container *container, - const double x, const double y, - const double width, const double height, - const int start, const int end) { - double scale = 0; - // Calculate total width - for (int i = start; i < end; ++i) { - double *old_width = - &((struct sway_container *)container->children->items[i])->width; - if (*old_width <= 0) { - if (end - start > 1) { - *old_width = width / (end - start - 1); - } else { - *old_width = width; - } - } - scale += *old_width; - } - scale = width / scale; - - // Resize windows - double child_x = x; - if (scale > 0.1) { - wlr_log(L_DEBUG, "Arranging %p horizontally", container); - for (int i = start; i < end; ++i) { - struct sway_container *child = container->children->items[i]; - wlr_log(L_DEBUG, - "Calculating arrangement for %p:%d (will scale %f by %f)", - child, child->type, width, scale); - - if (child->type == C_VIEW) { - view_configure(child->sway_view, child_x, y, child->width, - child->height); - } else { - child->x = child_x; - child->y = y; - } - - if (i == end - 1) { - double remaining_width = x + width - child_x; - arrange_windows(child, remaining_width, height); - } else { - arrange_windows(child, child->width * scale, height); - } - child_x += child->width; - } - - // update focused view border last because it may - // depend on the title bar geometry of its siblings. - /* TODO WLR - if (focused && container->children->length > 1) { - update_container_border(focused); - } - */ - } -} - -void apply_vert_layout(struct sway_container *container, - const double x, const double y, - const double width, const double height, const int start, - const int end) { - int i; - double scale = 0; - // Calculate total height - for (i = start; i < end; ++i) { - double *old_height = - &((struct sway_container *)container->children->items[i])->height; - if (*old_height <= 0) { - if (end - start > 1) { - *old_height = height / (end - start - 1); - } else { - *old_height = height; - } - } - scale += *old_height; - } - scale = height / scale; - - // Resize - double child_y = y; - if (scale > 0.1) { - wlr_log(L_DEBUG, "Arranging %p vertically", container); - for (i = start; i < end; ++i) { - struct sway_container *child = container->children->items[i]; - wlr_log(L_DEBUG, - "Calculating arrangement for %p:%d (will scale %f by %f)", - child, child->type, height, scale); - if (child->type == C_VIEW) { - view_configure(child->sway_view, x, child_y, child->width, - child->height); - } else { - child->x = x; - child->y = child_y; - } - - if (i == end - 1) { - double remaining_height = y + height - child_y; - arrange_windows(child, width, remaining_height); - } else { - arrange_windows(child, width, child->height * scale); - } - child_y += child->height; - } - - // update focused view border last because it may - // depend on the title bar geometry of its siblings. - /* TODO WLR - if (focused && container->children->length > 1) { - update_container_border(focused); - } - */ - } -} - /** * Get swayc in the direction of newly entered output. */ @@ -1026,7 +793,7 @@ struct sway_container *container_split(struct sway_container *child, // Special case: this just behaves like splitt child->prev_layout = child->layout; child->layout = layout; - arrange_windows(child, -1, -1); + arrange_children_of(child); return child; } diff --git a/sway/tree/view.c b/sway/tree/view.c index 3eeb1d97..e0aa6c0c 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c @@ -7,6 +7,7 @@ #include "sway/ipc-server.h" #include "sway/output.h" #include "sway/input/seat.h" +#include "sway/tree/arrange.h" #include "sway/tree/container.h" #include "sway/tree/layout.h" #include "sway/tree/view.h" @@ -78,14 +79,14 @@ void view_set_activated(struct sway_view *view, bool activated) { } } -void view_set_fullscreen(struct sway_view *view, bool fullscreen) { +// Set fullscreen, but without IPC events or arranging windows. +void view_set_fullscreen_raw(struct sway_view *view, bool fullscreen) { if (view->is_fullscreen == fullscreen) { return; } - struct sway_container *workspace = container_parent(view->swayc, C_WORKSPACE); - struct sway_container *container = container_parent(workspace, C_OUTPUT); - struct sway_output *output = container->sway_output; + struct sway_container *workspace = + container_parent(view->swayc, C_WORKSPACE); if (view->impl->set_fullscreen) { view->impl->set_fullscreen(view, fullscreen); @@ -98,6 +99,8 @@ void view_set_fullscreen(struct sway_view *view, bool fullscreen) { view_set_fullscreen(workspace->sway_workspace->fullscreen, false); } workspace->sway_workspace->fullscreen = view; + view->swayc->saved_width = view->swayc->width; + view->swayc->saved_height = view->swayc->height; struct sway_seat *seat; struct sway_container *focus, *focus_ws; @@ -114,11 +117,22 @@ void view_set_fullscreen(struct sway_view *view, bool fullscreen) { } } else { workspace->sway_workspace->fullscreen = NULL; + view->swayc->width = view->swayc->saved_width; + view->swayc->height = view->swayc->saved_height; } +} - arrange_windows(workspace, -1, -1); - output_damage_whole(output); +void view_set_fullscreen(struct sway_view *view, bool fullscreen) { + if (view->is_fullscreen == fullscreen) { + return; + } + view_set_fullscreen_raw(view, fullscreen); + + struct sway_container *workspace = + container_parent(view->swayc, C_WORKSPACE); + arrange_workspace(workspace); + output_damage_whole(workspace->parent->sway_output); ipc_event_window(view->swayc, "fullscreen_mode"); } @@ -257,7 +271,7 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface) { wl_signal_add(&view->swayc->events.reparent, &view->container_reparent); view->container_reparent.notify = view_handle_container_reparent; - arrange_windows(cont->parent, -1, -1); + arrange_children_of(cont->parent); input_manager_set_focus(input_manager, cont); view_damage(view, true); @@ -288,7 +302,7 @@ void view_unmap(struct sway_view *view) { view->swayc = NULL; view->surface = NULL; - arrange_windows(parent, -1, -1); + arrange_children_of(parent); } void view_update_position(struct sway_view *view, double ox, double oy) { diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index 66e1f7b9..f34baa9e 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -9,6 +9,7 @@ #include "sway/input/input-manager.h" #include "sway/input/seat.h" #include "sway/ipc-server.h" +#include "sway/tree/arrange.h" #include "sway/tree/container.h" #include "sway/tree/workspace.h" #include "log.h" @@ -392,7 +393,7 @@ bool workspace_switch(struct sway_container *workspace) { } seat_set_focus(seat, next); struct sway_container *output = container_parent(workspace, C_OUTPUT); - arrange_windows(output, -1, -1); + arrange_output(output); return true; } |