refactor code for removing children from a con

Let’s see how this callback stuff will work out. If it doesn’t work out well,
we will remove it.
This commit is contained in:
Michael Stapelberg 2011-02-14 18:08:36 +01:00
parent a5e075c154
commit 28dd226259
4 changed files with 31 additions and 22 deletions

View File

@ -358,6 +358,9 @@ struct Con {
TAILQ_ENTRY(Con) focused; TAILQ_ENTRY(Con) focused;
TAILQ_ENTRY(Con) all_cons; TAILQ_ENTRY(Con) all_cons;
TAILQ_ENTRY(Con) floating_windows; TAILQ_ENTRY(Con) floating_windows;
/** callbacks */
void(*on_remove_child)(Con *);
}; };
#endif #endif

View File

@ -34,6 +34,8 @@
} \ } \
while (0) while (0)
#define CALL(obj, member, ...) obj->member(obj, ## __VA_ARGS__)
int min(int a, int b); int min(int a, int b);
int max(int a, int b); int max(int a, int b);
bool rect_contains(Rect rect, uint32_t x, uint32_t y); bool rect_contains(Rect rect, uint32_t x, uint32_t y);

View File

@ -24,6 +24,8 @@ char *colors[] = {
"#aa00aa" "#aa00aa"
}; };
static void con_on_remove_child(Con *con);
/* /*
* Create a new container (and attach it to the given parent, if not NULL). * Create a new container (and attach it to the given parent, if not NULL).
* This function initializes the data structures and creates the appropriate * This function initializes the data structures and creates the appropriate
@ -32,6 +34,7 @@ char *colors[] = {
*/ */
Con *con_new(Con *parent) { Con *con_new(Con *parent) {
Con *new = scalloc(sizeof(Con)); Con *new = scalloc(sizeof(Con));
new->on_remove_child = con_on_remove_child;
TAILQ_INSERT_TAIL(&all_cons, new, all_cons); TAILQ_INSERT_TAIL(&all_cons, new, all_cons);
new->type = CT_CON; new->type = CT_CON;
new->border_style = config.default_border; new->border_style = config.default_border;
@ -522,14 +525,7 @@ void con_move_to_workspace(Con *con, Con *workspace) {
/* 8: keep focus on the current workspace */ /* 8: keep focus on the current workspace */
con_focus(focus_next); con_focus(focus_next);
/* 9: check if the parent container is empty now and close it */ CALL(parent, on_remove_child);
if (parent->type != CT_WORKSPACE &&
TAILQ_EMPTY(&(parent->nodes_head))) {
DLOG("Closing empty parent container\n");
/* TODO: check if this container would swallow any other client and
* dont close it automatically. */
tree_close(parent, false, false);
}
} }
/* /*
@ -744,3 +740,17 @@ void con_set_layout(Con *con, int layout) {
con->layout = layout; con->layout = layout;
} }
static void con_on_remove_child(Con *con) {
/* Nothing to do for workspaces */
if (con->type == CT_WORKSPACE)
return;
/* TODO: check if this container would swallow any other client and
* dont close it automatically. */
DLOG("on_remove_child\n");
if (con_num_children(con) == 0) {
DLOG("Container empty, closing\n");
tree_close(con, false, false);
}
}

View File

@ -190,14 +190,8 @@ void tree_close(Con *con, bool kill_window, bool dont_kill_parent) {
} }
/* check if the parent container is empty now and close it */ /* check if the parent container is empty now and close it */
if (!dont_kill_parent && if (!dont_kill_parent)
parent->type != CT_WORKSPACE && CALL(parent, on_remove_child);
TAILQ_EMPTY(&(parent->nodes_head))) {
DLOG("Closing empty parent container\n");
/* TODO: check if this container would swallow any other client and
* dont close it automatically. */
tree_close(parent, false, false);
}
} }
/* /*
@ -504,6 +498,8 @@ void tree_move(char way, orientation_t orientation) {
fix_percent = true; fix_percent = true;
} }
CALL(con->parent, on_remove_child);
TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, con, nodes); TAILQ_INSERT_AFTER(&(next->parent->nodes_head), next, con, nodes);
TAILQ_INSERT_HEAD(&(next->parent->focus_head), con, focused); TAILQ_INSERT_HEAD(&(next->parent->focus_head), con, focused);
/* TODO: dont influence focus handling? */ /* TODO: dont influence focus handling? */
@ -567,13 +563,11 @@ void tree_move(char way, orientation_t orientation) {
* container(s) would still point to the old container(s)). */ * container(s) would still point to the old container(s)). */
con_focus(con); con_focus(con);
if (con_num_children(old_parent) == 0) { /* fix the percentages in the container we moved from */
DLOG("Old container empty after moving. Let's close it\n"); if (level_changed)
tree_close(old_parent, false, false);
} else if (level_changed) {
/* fix the percentages in the container we moved from */
con_fix_percent(old_parent); con_fix_percent(old_parent);
}
CALL(old_parent, on_remove_child);
tree_flatten(croot); tree_flatten(croot);
} }