From 2992b72d61933568476e2bf4baf573e714f9ed40 Mon Sep 17 00:00:00 2001 From: Tony Crisci Date: Mon, 2 Apr 2018 22:37:21 -0400 Subject: change reap container approach --- sway/tree/container.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 4 deletions(-) (limited to 'sway/tree/container.c') diff --git a/sway/tree/container.c b/sway/tree/container.c index 8688edd6..686a52c7 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -1,4 +1,5 @@ #define _POSIX_C_SOURCE 200809L +#include #include #include #include @@ -109,12 +110,55 @@ static struct sway_container *_container_destroy(struct sway_container *cont) { return parent; } -struct sway_container *container_destroy(struct sway_container *cont) { - struct sway_container *parent = _container_destroy(cont); - parent = container_reap_empty(parent); +static void reap_empty_func(struct sway_container *con, void *data) { + switch (con->type) { + case C_TYPES: + case C_ROOT: + case C_OUTPUT: + // dont reap these + break; + case C_WORKSPACE: + if (!workspace_is_visible(con) && con->children->length == 0) { + container_workspace_destroy(con); + } + break; + case C_CONTAINER: + if (con->children->length == 0) { + _container_destroy(con); + } else if (con->children->length == 1) { + struct sway_container *only_child = con->children->items[0]; + if (only_child->type == C_CONTAINER) { + container_remove_child(only_child); + container_replace_child(con, only_child); + _container_destroy(con); + } + } + case C_VIEW: + break; + } +} + +struct sway_container *container_reap_empty(struct sway_container *container) { + struct sway_container *parent = container->parent; + + container_for_each_descendant_dfs(container, reap_empty_func, NULL); + return parent; } +void container_destroy(struct sway_container *cont) { + if (cont == NULL) { + return; + } + + if (cont->children != NULL && cont->children->length) { + assert(false && "dont destroy containers with children"); + } + + _container_destroy(cont); + container_reap_empty(&root_container); +} + static void container_close_func(struct sway_container *container, void *data) { if (container->type == C_VIEW) { view_close(container->sway_view); @@ -126,6 +170,8 @@ struct sway_container *container_close(struct sway_container *con) { return NULL; } + struct sway_container *parent = con->parent; + switch (con->type) { case C_TYPES: wlr_log(L_ERROR, "tried to close an invalid container"); @@ -148,7 +194,7 @@ struct sway_container *container_close(struct sway_container *con) { } - return con->parent; + return parent; } struct sway_container *container_output_create( -- cgit v1.2.3-54-g00ecf