Merge pull request #1658 from swaywm/delete-empty-ws

Destroy empty workspaces when moving away
This commit is contained in:
Drew DeVault 2018-03-30 11:39:00 -04:00 committed by GitHub
commit 2426ca0241
Failed to generate hash of commit
5 changed files with 104 additions and 3 deletions

View file

@ -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

View file

@ -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);

View file

@ -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);
}
}
}

View file

@ -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;
}

View file

@ -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