From 01af34391267e91461a4ab7a1234dd58f45d2c93 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 30 Mar 2018 10:31:21 -0400 Subject: Destroy empty workspaces when moving away --- include/sway/tree/container.h | 8 +++++++ include/sway/tree/layout.h | 3 +++ sway/input/seat.c | 4 ++++ sway/tree/container.c | 55 ++++++++++++++++++++++++++++++++++++++++--- sway/tree/layout.c | 37 +++++++++++++++++++++++++++++ 5 files changed, 104 insertions(+), 3 deletions(-) diff --git a/include/sway/tree/container.h b/include/sway/tree/container.h index 3bb497db..24e8468e 100644 --- a/include/sway/tree/container.h +++ b/include/sway/tree/container.h @@ -99,8 +99,13 @@ struct sway_container *container_view_create( struct sway_container *container_output_destroy(struct sway_container *output); +struct sway_container *container_workspace_destroy( + struct sway_container *workspace); + struct sway_container *container_view_destroy(struct sway_container *view); +void container_destroy(struct sway_container *cont); + struct sway_container *container_set_layout(struct sway_container *container, enum sway_container_layout layout); @@ -140,4 +145,7 @@ void container_for_each_descendant_bfs(struct sway_container *container, void container_for_each_descendant_dfs(struct sway_container *container, void (*f)(struct sway_container *container, void *data), void *data); +bool container_has_anscestor(struct sway_container *descendant, + struct sway_container *anscestor); + #endif diff --git a/include/sway/tree/layout.h b/include/sway/tree/layout.h index ad52bdb0..8239366b 100644 --- a/include/sway/tree/layout.h +++ b/include/sway/tree/layout.h @@ -39,6 +39,9 @@ struct sway_container *container_add_sibling(struct sway_container *parent, struct sway_container *container_remove_child(struct sway_container *child); +void container_move_to(struct sway_container* container, + struct sway_container* destination); + enum sway_container_layout container_get_default_layout(struct sway_container *output); void container_sort_workspaces(struct sway_container *output); diff --git a/sway/input/seat.c b/sway/input/seat.c index 7cf0dd08..ae536264 100644 --- a/sway/input/seat.c +++ b/sway/input/seat.c @@ -8,6 +8,7 @@ #include "sway/input/keyboard.h" #include "sway/ipc-server.h" #include "sway/output.h" +#include "sway/tree/container.h" #include "sway/tree/view.h" #include "log.h" @@ -331,6 +332,9 @@ void sway_seat_set_focus(struct sway_seat *seat, struct sway_container *containe if (last_ws) { wlr_log(L_DEBUG, "sending workspace event"); ipc_event_workspace(last_ws, container, "focus"); + if (last_ws->children->length == 0) { + container_workspace_destroy(last_ws); + } } } diff --git a/sway/tree/container.c b/sway/tree/container.c index 2eac812e..ed39a154 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -58,7 +58,7 @@ static struct sway_container *container_create(enum sway_container_type type) { return c; } -static void container_destroy(struct sway_container *cont) { +void container_destroy(struct sway_container *cont) { if (cont == NULL) { return; } @@ -203,8 +203,7 @@ struct sway_container *container_view_create(struct sway_container *sibling, } struct sway_container *container_output_destroy(struct sway_container *output) { - if (!sway_assert(output, - "null output passed to container_output_destroy")) { + if (!sway_assert(output, "cannot destroy null output")) { return NULL; } @@ -236,6 +235,45 @@ struct sway_container *container_output_destroy(struct sway_container *output) { return &root_container; } +struct sway_container *container_workspace_destroy( + struct sway_container *workspace) { + if (!sway_assert(workspace, "cannot destroy null workspace")) { + return NULL; + } + + // Do not destroy this if it's the last workspace on this output + struct sway_container *output = container_parent(workspace, C_OUTPUT); + if (output && output->children->length == 1) { + return NULL; + } + + struct sway_container *parent = workspace->parent; + if (workspace->children->length == 0) { + // destroy the WS if there are no children (TODO check for floating) + wlr_log(L_DEBUG, "destroying workspace '%s'", workspace->name); + ipc_event_workspace(workspace, NULL, "empty"); + } else { + // Move children to a different workspace on this output + struct sway_container *new_workspace = NULL; + // TODO move floating + for (int i = 0; i < output->children->length; i++) { + if (output->children->items[i] != workspace) { + new_workspace = output->children->items[i]; + break; + } + } + + wlr_log(L_DEBUG, "moving children to different workspace '%s' -> '%s'", + workspace->name, new_workspace->name); + for (int i = 0; i < workspace->children->length; i++) { + container_move_to(workspace->children->items[i], new_workspace); + } + } + + container_destroy(workspace); + return parent; +} + struct sway_container *container_view_destroy(struct sway_container *view) { if (!view) { return NULL; @@ -438,3 +476,14 @@ void container_for_each_descendant_bfs(struct sway_container *con, list_cat(queue, current->children); } } + +bool container_has_anscestor(struct sway_container *descendant, + struct sway_container *anscestor) { + while (descendant->type != C_ROOT) { + descendant = descendant->parent; + if (descendant == anscestor) { + return true; + } + } + return false; +} diff --git a/sway/tree/layout.c b/sway/tree/layout.c index dc0ee5b4..97007888 100644 --- a/sway/tree/layout.c +++ b/sway/tree/layout.c @@ -11,6 +11,7 @@ #include "sway/output.h" #include "sway/tree/view.h" #include "sway/input/seat.h" +#include "sway/ipc-server.h" #include "list.h" #include "log.h" @@ -121,6 +122,42 @@ struct sway_container *container_remove_child(struct sway_container *child) { return parent; } +struct sway_container *container_reap_empty(struct sway_container *container) { + if (!sway_assert(container, "reaping null container")) { + return NULL; + } + while (container->children->length == 0 && container->type == C_CONTAINER) { + wlr_log(L_DEBUG, "Container: Destroying container '%p'", container); + struct sway_container *parent = container->parent; + container_destroy(container); + container = parent; + } + return container; +} + +void container_move_to(struct sway_container* container, + struct sway_container* destination) { + if (container == destination + || container_has_anscestor(container, destination)) { + return; + } + struct sway_container *old_parent = container_remove_child(container); + container->width = container->height = 0; + struct sway_container *new_parent = + container_add_sibling(destination, container); + if (destination->type == C_WORKSPACE) { + // If the workspace only has one child after adding one, it + // means that the workspace was just initialized. + // TODO: Consider floating views in this test + if (destination->children->length == 1) { + ipc_event_workspace(NULL, destination, "init"); + } + } + old_parent = container_reap_empty(old_parent); + arrange_windows(old_parent, -1, -1); + arrange_windows(new_parent, -1, -1); +} + enum sway_container_layout container_get_default_layout( struct sway_container *output) { /* TODO WLR -- cgit v1.2.3-54-g00ecf