From 53dc83fb68d2da079f2595994af41cc6b9acbeea Mon Sep 17 00:00:00 2001 From: Tudor Brindus Date: Thu, 4 Jun 2020 14:28:43 -0400 Subject: [PATCH] tree/container: introduce `container_toplevel_ancestor` helper This allows us to not have to explicitly write the same while loop everywhere. --- include/sway/tree/container.h | 7 +++++++ sway/input/seatop_default.c | 10 ++-------- sway/tree/container.c | 19 ++++++++++--------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h index 8d2d8827..fd028131 100644 --- a/include/sway/tree/container.h +++ b/include/sway/tree/container.h @@ -272,6 +272,13 @@ void container_set_fullscreen(struct sway_container *con, */ void container_fullscreen_disable(struct sway_container *con); +/** + * Walk up the container tree branch starting at the given container, and return + * its earliest ancestor. + */ +struct sway_container *container_toplevel_ancestor( + struct sway_container *container); + /** * Return true if the container is floating, or a child of a floating split * container. diff --git a/sway/input/seatop_default.c b/sway/input/seatop_default.c index 2bce2f77..b8265441 100644 --- a/sway/input/seatop_default.c +++ b/sway/input/seatop_default.c @@ -338,10 +338,7 @@ static void handle_button(struct sway_seat *seat, uint32_t time_msec, if (button == btn_move && (mod_pressed || on_titlebar)) { seat_set_focus_container(seat, seat_get_focus_inactive_view(seat, &cont->node)); - while (cont->parent) { - cont = cont->parent; - } - seatop_begin_move_floating(seat, cont); + seatop_begin_move_floating(seat, container_toplevel_ancestor(cont)); return; } } @@ -359,10 +356,7 @@ static void handle_button(struct sway_seat *seat, uint32_t time_msec, uint32_t btn_resize = config->floating_mod_inverse ? BTN_LEFT : BTN_RIGHT; if (mod_pressed && button == btn_resize) { - struct sway_container *floater = cont; - while (floater->parent) { - floater = floater->parent; - } + struct sway_container *floater = container_toplevel_ancestor(cont); edge = 0; edge |= cursor->cursor->x > floater->x + floater->width / 2 ? WLR_EDGE_RIGHT : WLR_EDGE_LEFT; diff --git a/sway/tree/container.c b/sway/tree/container.c index 3e99aa75..2fbd0d38 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -1108,11 +1108,17 @@ void container_set_fullscreen(struct sway_container *con, } } -bool container_is_floating_or_child(struct sway_container *container) { +struct sway_container *container_toplevel_ancestor( + struct sway_container *container) { while (container->parent) { container = container->parent; } - return container_is_floating(container); + + return container; +} + +bool container_is_floating_or_child(struct sway_container *container) { + return container_is_floating(container_toplevel_ancestor(container)); } bool container_is_fullscreen_or_child(struct sway_container *container) { @@ -1537,10 +1543,7 @@ void container_update_marks_textures(struct sway_container *con) { void container_raise_floating(struct sway_container *con) { // Bring container to front by putting it at the end of the floating list. - struct sway_container *floater = con; - while (floater->parent) { - floater = floater->parent; - } + struct sway_container *floater = container_toplevel_ancestor(con); if (container_is_floating(floater) && floater->workspace) { list_move_to_end(floater->workspace->floating, floater); node_set_dirty(&floater->workspace->node); @@ -1552,8 +1555,6 @@ bool container_is_scratchpad_hidden(struct sway_container *con) { } bool container_is_scratchpad_hidden_or_child(struct sway_container *con) { - while (con->parent) { - con = con->parent; - } + con = container_toplevel_ancestor(con); return con->scratchpad && !con->workspace; }