From 97f7d47413967e2b6f405c4fa303850b7c56f57a Mon Sep 17 00:00:00 2001 From: wil Date: Sat, 10 Dec 2016 16:44:43 +0100 Subject: Added Awesome/Monad type "auto" layouts --- include/sway/layout.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'include/sway/layout.h') diff --git a/include/sway/layout.h b/include/sway/layout.h index b982365c..38096947 100644 --- a/include/sway/layout.h +++ b/include/sway/layout.h @@ -75,4 +75,7 @@ void swayc_log(log_importance_t verbosity, swayc_t *cont, const char* format, .. */ enum swayc_layouts default_layout(swayc_t *output); +inline bool is_auto_layout(enum swayc_layouts layout) { + return (layout >= L_AUTO_LEFT) && (layout <= L_AUTO_BOTTOM); +} #endif -- cgit v1.2.3 From a0aa8d9780c6c8b0138800e3b2c2c0053174a2c5 Mon Sep 17 00:00:00 2001 From: wil Date: Thu, 29 Dec 2016 20:26:35 +0100 Subject: cleanup in auto layouts - added L_AUTO_FIRST/LAST instead of using explicit layouts. - when switching between auto layout that don't share the same major axis, invert the width/height of their child views to preserve their relative proportions. --- include/sway/container.h | 3 +++ include/sway/layout.h | 2 +- sway/commands/layout.c | 48 ++++++++++++++++++++++++++++++---------- sway/commands/workspace_layout.c | 10 ++++++++- sway/container.c | 23 ++++++++++++++++--- sway/sway.5.txt | 2 +- 6 files changed, 70 insertions(+), 18 deletions(-) (limited to 'include/sway/layout.h') diff --git a/include/sway/container.h b/include/sway/container.h index 1d0fb265..f0574b1b 100644 --- a/include/sway/container.h +++ b/include/sway/container.h @@ -44,6 +44,9 @@ enum swayc_layouts { L_AUTO_TOP, L_AUTO_BOTTOM, + L_AUTO_FIRST = L_AUTO_LEFT, + L_AUTO_LAST = L_AUTO_BOTTOM, + // Keep last L_LAYOUTS, }; diff --git a/include/sway/layout.h b/include/sway/layout.h index 38096947..a771a72e 100644 --- a/include/sway/layout.h +++ b/include/sway/layout.h @@ -76,6 +76,6 @@ void swayc_log(log_importance_t verbosity, swayc_t *cont, const char* format, .. enum swayc_layouts default_layout(swayc_t *output); inline bool is_auto_layout(enum swayc_layouts layout) { - return (layout >= L_AUTO_LEFT) && (layout <= L_AUTO_BOTTOM); + return (layout >= L_AUTO_FIRST) && (layout <= L_AUTO_LAST); } #endif diff --git a/sway/commands/layout.c b/sway/commands/layout.c index 9e468c21..5426186e 100644 --- a/sway/commands/layout.c +++ b/sway/commands/layout.c @@ -56,24 +56,12 @@ struct cmd_results *cmd_layout(int argc, char **argv) { swayc_change_layout(parent, L_HORIZ); } } else if (strcasecmp(argv[0], "auto_left") == 0) { - if (parent->type != C_CONTAINER && !swayc_is_empty_workspace(parent)){ - parent = new_container(parent, L_AUTO_LEFT); - } swayc_change_layout(parent, L_AUTO_LEFT); } else if (strcasecmp(argv[0], "auto_right") == 0) { - if (parent->type != C_CONTAINER && !swayc_is_empty_workspace(parent)){ - parent = new_container(parent, L_AUTO_RIGHT); - } swayc_change_layout(parent, L_AUTO_RIGHT); } else if (strcasecmp(argv[0], "auto_top") == 0) { - if (parent->type != C_CONTAINER && !swayc_is_empty_workspace(parent)){ - parent = new_container(parent, L_AUTO_TOP); - } swayc_change_layout(parent, L_AUTO_TOP); } else if (strcasecmp(argv[0], "auto_bot") == 0) { - if (parent->type != C_CONTAINER && !swayc_is_empty_workspace(parent)){ - parent = new_container(parent, L_AUTO_BOTTOM); - } swayc_change_layout(parent, L_AUTO_BOTTOM); } else if (strcasecmp(argv[0], "incnmaster") == 0) { if ((error = checkarg(argc, "layout incnmaster", @@ -105,6 +93,42 @@ struct cmd_results *cmd_layout(int argc, char **argv) { ((int)container->parent->nb_slave_groups + inc >= 1)) { container->parent->nb_slave_groups += inc; } + } else if (strcasecmp(argv[0], "auto") == 0) { + if ((error = checkarg(argc, "auto", EXPECTED_EQUAL_TO, 2))) { + return error; + } + swayc_t *container = get_focused_view(swayc_active_workspace()); + swayc_t *parent = container->parent; + enum swayc_layouts layout; + if (strcasecmp(argv[1], "next") == 0) { + if (is_auto_layout(parent->layout) && parent->layout < L_AUTO_LAST) { + layout = parent->layout + 1; + } else { + layout = L_AUTO_FIRST; + } + } else if (strcasecmp(argv[1], "prev") == 0) { + if (is_auto_layout(parent->layout) && parent->layout > L_AUTO_FIRST) { + layout = parent->layout - 1; + } else { + layout = L_AUTO_FIRST; + } + } else { + return cmd_results_new(CMD_FAILURE, "layout auto", + "Must be one of ."); + } + swayc_change_layout(parent, layout); + } else if (strcasecmp(argv[0], "promote") == 0) { + // swap first child in auto layout with currently focused child + swayc_t *container = get_focused_view(swayc_active_workspace()); + swayc_t *parent = container->parent; + if (is_auto_layout(parent->layout)) { + int focused_idx = index_child(container); + swayc_t *first = parent->children->items[0]; + if (focused_idx > 0) { + list_swap(parent->children, 0, focused_idx); + swap_geometry(first, container); + } + } } } diff --git a/sway/commands/workspace_layout.c b/sway/commands/workspace_layout.c index b7b4b033..3e0a12ce 100644 --- a/sway/commands/workspace_layout.c +++ b/sway/commands/workspace_layout.c @@ -13,8 +13,16 @@ struct cmd_results *cmd_workspace_layout(int argc, char **argv) { config->default_layout = L_STACKED; } else if (strcasecmp(argv[0], "tabbed") == 0) { config->default_layout = L_TABBED; + } else if (strcasecmp(argv[0], "auto_left") == 0) { + config->default_layout = L_AUTO_LEFT; + } else if (strcasecmp(argv[0], "auto_right") == 0) { + config->default_layout = L_AUTO_RIGHT; + } else if (strcasecmp(argv[0], "auto_top") == 0) { + config->default_layout = L_AUTO_TOP; + } else if (strcasecmp(argv[0], "auto_bottom") == 0) { + config->default_layout = L_AUTO_BOTTOM; } else { - return cmd_results_new(CMD_INVALID, "workspace_layout", "Expected 'workspace_layout '"); + return cmd_results_new(CMD_INVALID, "workspace_layout", "Expected 'workspace_layout '"); } return cmd_results_new(CMD_SUCCESS, NULL, NULL); } diff --git a/sway/container.c b/sway/container.c index d034115f..3bdc8b6c 100644 --- a/sway/container.c +++ b/sway/container.c @@ -962,11 +962,28 @@ swayc_t *swayc_tabbed_stacked_parent(swayc_t *con) { } swayc_t *swayc_change_layout(swayc_t *container, enum swayc_layouts layout) { + // if layout change modifies the auto layout's major axis, swap width and height + // to preserve current ratios. + if (is_auto_layout(layout) && is_auto_layout(container->layout)) { + enum swayc_layouts prev_major = (container->layout == L_AUTO_LEFT || + container->layout == L_AUTO_RIGHT) + ? L_HORIZ : L_VERT; + enum swayc_layouts new_major = (layout == L_AUTO_LEFT || layout == L_AUTO_RIGHT) + ? L_HORIZ : L_VERT; + if (new_major != prev_major) { + for (int i = 0; i < container->children->length; ++i) { + swayc_t *child = container->children->items[i]; + double h = child->height; + child->height = child->width; + child->width = h; + } + } + } if (container->type == C_WORKSPACE) { container->workspace_layout = layout; - if (layout == L_HORIZ || layout == L_VERT) { - container->layout = layout; - } + if (layout == L_HORIZ || layout == L_VERT || is_auto_layout(layout)) { + container->layout = layout; + } } else { container->layout = layout; } diff --git a/sway/sway.5.txt b/sway/sway.5.txt index 8f8302f0..c0c4bfb2 100644 --- a/sway/sway.5.txt +++ b/sway/sway.5.txt @@ -371,7 +371,7 @@ The default colors are: switch to workspace 2, then invoke the "workspace 2" command again, you will be returned to workspace 1. Defaults to _no_. -**workspace_layout** :: +**workspace_layout** :: Specifies the start layout for new workspaces. **include** :: -- cgit v1.2.3 From c01b89839874239bee7948fad004b19a19917432 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sun, 1 Jan 2017 12:36:47 -0500 Subject: Fix inline is_auto_layout --- include/sway/layout.h | 5 ++--- sway/layout.c | 4 ++++ 2 files changed, 6 insertions(+), 3 deletions(-) (limited to 'include/sway/layout.h') diff --git a/include/sway/layout.h b/include/sway/layout.h index a771a72e..d7fe748d 100644 --- a/include/sway/layout.h +++ b/include/sway/layout.h @@ -75,7 +75,6 @@ void swayc_log(log_importance_t verbosity, swayc_t *cont, const char* format, .. */ enum swayc_layouts default_layout(swayc_t *output); -inline bool is_auto_layout(enum swayc_layouts layout) { - return (layout >= L_AUTO_FIRST) && (layout <= L_AUTO_LAST); -} +bool is_auto_layout(enum swayc_layouts layout); + #endif diff --git a/sway/layout.c b/sway/layout.c index 2de6da45..2195863e 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -1487,3 +1487,7 @@ enum swayc_layouts default_layout(swayc_t *output) { return L_VERT; } } + +bool is_auto_layout(enum swayc_layouts layout) { + return (layout >= L_AUTO_FIRST) && (layout <= L_AUTO_LAST); +} -- cgit v1.2.3 From bd415029ba72425c97647b55fce19213e7909cbc Mon Sep 17 00:00:00 2001 From: wil Date: Sat, 7 Jan 2017 17:41:15 +0100 Subject: Moved auto_* layout functions from resize.c to layout.c --- include/sway/layout.h | 4 +++ sway/commands/resize.c | 98 ++------------------------------------------------ sway/layout.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 96 deletions(-) (limited to 'include/sway/layout.h') diff --git a/include/sway/layout.h b/include/sway/layout.h index d7fe748d..c51fece9 100644 --- a/include/sway/layout.h +++ b/include/sway/layout.h @@ -76,5 +76,9 @@ void swayc_log(log_importance_t verbosity, swayc_t *cont, const char* format, .. enum swayc_layouts default_layout(swayc_t *output); bool is_auto_layout(enum swayc_layouts layout); +int auto_group_start_index(swayc_t *container, int index); +int auto_group_end_index(swayc_t *container, int index); +size_t auto_group_count(swayc_t *container); +size_t auto_group_index(swayc_t *container, int index); #endif diff --git a/sway/commands/resize.c b/sway/commands/resize.c index 1c052286..c391945f 100644 --- a/sway/commands/resize.c +++ b/sway/commands/resize.c @@ -62,100 +62,6 @@ static bool resize_floating(int amount, bool use_width) { return false; } -/** - * Return the number of children in the slave groups. This corresponds to the children - * that are not members of the master group. - */ -static inline size_t auto_slave_count(swayc_t *container) { - return container->children->length - container->nb_master; - -} - -/** - * given the index of a container's child, return the index of the first child of the group - * which index is a member of. - */ -static int auto_group_start_index(swayc_t *container, int index) { - if (index < 0 || ! is_auto_layout(container->layout) - || (size_t) index < container->nb_master) { - return 0; - } else { - size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups; - size_t remainder = auto_slave_count(container) % container->nb_slave_groups; - int start_idx; - int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; - if (index < idx2) { - start_idx = ((index - container->nb_master) / grp_sz) * grp_sz + container->nb_master; - } else { - start_idx = idx2 + ((index - idx2) / (grp_sz + 1)) * (grp_sz + 1); - } - return MIN(start_idx, container->children->length); - } -} - -/** - * given the index of a container's child, return the index of the first child of the group - * that follows the one which index is a member of. - * This makes the function usable to walk through the groups in a container. - */ -static int auto_group_end_index(swayc_t *container, int index) { - if (index < 0 || ! is_auto_layout(container->layout)) { - return container->children->length; - } else { - int nxt_idx; - if ((size_t)index < container->nb_master) { - nxt_idx = container->nb_master; - } else { - size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups; - size_t remainder = auto_slave_count(container) % container->nb_slave_groups; - int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; - if (index < idx2) { - nxt_idx = ((index - container->nb_master) / grp_sz + 1) * grp_sz + container->nb_master; - } else { - nxt_idx = idx2 + ((index - idx2) / (grp_sz + 1) + 1) * (grp_sz + 1); - } - } - return MIN(nxt_idx, container->children->length); - } -} - -/** - * Return the combined number of master and slave groups in the container. - */ -static inline size_t auto_group_count(swayc_t *container) { - return MIN(container->nb_slave_groups, auto_slave_count(container)) + (container->nb_master ? 1 : 0); -} - -/** - * return the index of the Group containing th child of . - * The index is the order of the group along the container's major axis (starting at 0). - */ -static size_t auto_group_index(swayc_t *container, int index) { - if (index < 0) { - return 0; - } - bool master_first = (container->layout == L_AUTO_LEFT || container->layout == L_AUTO_TOP); - int nb_slaves = auto_slave_count(container); - if ((size_t) index < container->nb_master) { - if (master_first || nb_slaves <= 0) { - return 0; - } else { - return MIN(container->nb_slave_groups, nb_slaves); - } - } else { - size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups; - size_t remainder = auto_slave_count(container) % container->nb_slave_groups; - size_t grp_idx; - int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; - if (index < idx2) { - grp_idx = (index - container->nb_master) / grp_sz; - } else { - grp_idx = (container->nb_slave_groups - remainder) + (index - idx2) / (grp_sz + 1) ; - } - return grp_idx + (master_first ? 1 : 0); - } -} - static bool resize_tiled(int amount, bool use_width) { swayc_t *container = get_focused_view(swayc_active_workspace()); swayc_t *parent = container->parent; @@ -229,8 +135,8 @@ static bool resize_tiled(int amount, bool use_width) { } sway_log(L_DEBUG, "Check container %p: width %g vs %d, height %g vs %d", sibling, sibling->width + pixels, min_sane_w, sibling->height + pixels, min_sane_h); if (use_width ? - sibling->width + pixels < min_sane_w : - sibling->height + pixels < min_sane_h) { + sibling->width + pixels < min_sane_w : + sibling->height + pixels < min_sane_h) { valid = false; sway_log(L_DEBUG, "Container size no longer sane"); break; diff --git a/sway/layout.c b/sway/layout.c index 377dad47..5f8da9e6 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -1503,3 +1503,96 @@ enum swayc_layouts default_layout(swayc_t *output) { bool is_auto_layout(enum swayc_layouts layout) { return (layout >= L_AUTO_FIRST) && (layout <= L_AUTO_LAST); } + +/** + * Return the number of children in the slave groups. This corresponds to the children + * that are not members of the master group. + */ +static inline size_t auto_slave_count(swayc_t *container) { + return container->children->length - container->nb_master; +} + +/** + * given the index of a container's child, return the index of the first child of the group + * which index is a member of. + */ +int auto_group_start_index(swayc_t *container, int index) { + if (index < 0 || ! is_auto_layout(container->layout) + || (size_t) index < container->nb_master) { + return 0; + } else { + size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups; + size_t remainder = auto_slave_count(container) % container->nb_slave_groups; + int start_idx; + int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; + if (index < idx2) { + start_idx = ((index - container->nb_master) / grp_sz) * grp_sz + container->nb_master; + } else { + start_idx = idx2 + ((index - idx2) / (grp_sz + 1)) * (grp_sz + 1); + } + return MIN(start_idx, container->children->length); + } +} + +/** + * given the index of a container's child, return the index of the first child of the group + * that follows the one which index is a member of. + * This makes the function usable to walk through the groups in a container. + */ +int auto_group_end_index(swayc_t *container, int index) { + if (index < 0 || ! is_auto_layout(container->layout)) { + return container->children->length; + } else { + int nxt_idx; + if ((size_t)index < container->nb_master) { + nxt_idx = container->nb_master; + } else { + size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups; + size_t remainder = auto_slave_count(container) % container->nb_slave_groups; + int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; + if (index < idx2) { + nxt_idx = ((index - container->nb_master) / grp_sz + 1) * grp_sz + container->nb_master; + } else { + nxt_idx = idx2 + ((index - idx2) / (grp_sz + 1) + 1) * (grp_sz + 1); + } + } + return MIN(nxt_idx, container->children->length); + } +} + +/** + * Return the combined number of master and slave groups in the container. + */ +size_t auto_group_count(swayc_t *container) { + return MIN(container->nb_slave_groups, auto_slave_count(container)) + (container->nb_master ? 1 : 0); +} + +/** + * return the index of the Group containing th child of . + * The index is the order of the group along the container's major axis (starting at 0). + */ +size_t auto_group_index(swayc_t *container, int index) { + if (index < 0) { + return 0; + } + bool master_first = (container->layout == L_AUTO_LEFT || container->layout == L_AUTO_TOP); + int nb_slaves = auto_slave_count(container); + if ((size_t) index < container->nb_master) { + if (master_first || nb_slaves <= 0) { + return 0; + } else { + return MIN(container->nb_slave_groups, nb_slaves); + } + } else { + size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups; + size_t remainder = auto_slave_count(container) % container->nb_slave_groups; + size_t grp_idx; + int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; + if (index < idx2) { + grp_idx = (index - container->nb_master) / grp_sz; + } else { + grp_idx = (container->nb_slave_groups - remainder) + (index - idx2) / (grp_sz + 1) ; + } + return grp_idx + (master_first ? 1 : 0); + } +} -- cgit v1.2.3 From 1f47c58d63130b8de59cb81422a4339bc0273273 Mon Sep 17 00:00:00 2001 From: wil Date: Sat, 7 Jan 2017 20:26:46 +0100 Subject: simplification of apply_auto_layout Achieved by introducing auto_group_bounds function that produces the start/end indexes of a group inside an auto layot container. --- include/sway/layout.h | 9 +-- sway/layout.c | 158 ++++++++++++++++++++++++++++---------------------- 2 files changed, 95 insertions(+), 72 deletions(-) (limited to 'include/sway/layout.h') diff --git a/include/sway/layout.h b/include/sway/layout.h index c51fece9..fbedcdb3 100644 --- a/include/sway/layout.h +++ b/include/sway/layout.h @@ -76,9 +76,10 @@ void swayc_log(log_importance_t verbosity, swayc_t *cont, const char* format, .. enum swayc_layouts default_layout(swayc_t *output); bool is_auto_layout(enum swayc_layouts layout); -int auto_group_start_index(swayc_t *container, int index); -int auto_group_end_index(swayc_t *container, int index); -size_t auto_group_count(swayc_t *container); -size_t auto_group_index(swayc_t *container, int index); +int auto_group_start_index(const swayc_t *container, int index); +int auto_group_end_index(const swayc_t *container, int index); +size_t auto_group_count(const swayc_t *container); +size_t auto_group_index(const swayc_t *container, int index); +bool auto_group_bounds(const swayc_t *container, size_t group_index, int *start, int *end); #endif diff --git a/sway/layout.c b/sway/layout.c index 78673cf9..6a2af2a6 100644 --- a/sway/layout.c +++ b/sway/layout.c @@ -1164,9 +1164,7 @@ void apply_auto_layout(swayc_t *container, const double x, const double y, // a single slave group (containing slave 1 and 2). The master // group and slave group are layed out using L_VERT. - size_t nb_slaves = container->children->length - container->nb_master; - size_t nb_groups = (container->nb_master > 0 ? 1 : 0) + - MIN(container->nb_slave_groups, nb_slaves); + size_t nb_groups = auto_group_count(container); // the target dimension of the container along the "major" axis, each // group in the container will be layed out using "group_layout" along @@ -1216,74 +1214,53 @@ void apply_auto_layout(swayc_t *container, const double x, const double y, * layout. */ double old_group_dim[nb_groups]; double old_dim = 0; - size_t group = 0; - for (int i = 0; i < container->children->length;) { - swayc_t *child = container->children->items[i]; - double *dim = group_layout == L_HORIZ ? &child->height : &child->width; - if (*dim <= 0) { - // New child with uninitialized dimension - *dim = dim_maj; - if (nb_groups > 1) { - // child gets a dimension proportional to existing groups, - // it will be later scaled based on to the available size - // in the major axis. - *dim /= (nb_groups - 1); + for (size_t group = 0; group < nb_groups; ++group) { + int idx; + if (auto_group_bounds(container, group, &idx, NULL)) { + swayc_t *child = container->children->items[idx]; + double *dim = group_layout == L_HORIZ ? &child->height : &child->width; + if (*dim <= 0) { + // New child with uninitialized dimension + *dim = dim_maj; + if (nb_groups > 1) { + // child gets a dimension proportional to existing groups, + // it will be later scaled based on to the available size + // in the major axis. + *dim /= (nb_groups - 1); + } } + old_dim += *dim; + old_group_dim[group] = *dim; } - if (i == 0 && container->nb_master > 0) { - i += container->nb_master; - } else { - i += (nb_slaves - i + container->nb_master) / (nb_groups - group); - } - old_dim += *dim; - old_group_dim[group++] = *dim; } - double scale = dim_maj / old_dim; /* Apply layout to each group */ pos = pos_maj; - // first child in the current group - int start; - - // index immediately after the last child in the current group - int end = 0; - - for (group = 0; group < nb_groups; ++group) { - // column to include next by increasing position. - size_t layout_group = master_first ? group : (group + 1) % nb_groups; - - // adjusted size of the group - group_dim = old_group_dim[layout_group] * scale; - if (container->nb_master > 0 && layout_group == 0) { - start = 0; - end = MIN(container->nb_master, container->children->length); - } else { - if (group == 0) { - start = container->nb_master; - } else { - start = end; + for (size_t group = 0; group < nb_groups; ++group) { + int start, end; // index of first (inclusive) and last (exclusive) child in the group + if (auto_group_bounds(container, group, &start, &end)) { + // adjusted size of the group + group_dim = old_group_dim[group] * scale; + if (group == nb_groups - 1) { + group_dim = pos_maj + dim_maj - pos; // remaining width + } + sway_log(L_DEBUG, "Arranging container %p column %zu, children [%d,%d[ (%fx%f+%f,%f)", + container, group, start, end, *group_w, *group_h, *group_x, *group_y); + switch (group_layout) { + default: + case L_VERT: + apply_vert_layout(container, *group_x, *group_y, *group_w, *group_h, start, end); + break; + case L_HORIZ: + apply_horiz_layout(container, *group_x, *group_y, *group_w, *group_h, start, end); + break; } - end = start + (nb_slaves - start + container->nb_master) / (nb_groups - layout_group); - } - if (group == nb_groups - 1) { - group_dim = pos_maj + dim_maj - pos; // remaining width - } - sway_log(L_DEBUG, "Arranging container %p column %zu, children [%d,%d[ (%fx%f+%f,%f)", - container, group, start, end, *group_w, *group_h, *group_x, *group_y); - switch (group_layout) { - default: - case L_VERT: - apply_vert_layout(container, *group_x, *group_y, *group_w, *group_h, start, end); - break; - case L_HORIZ: - apply_horiz_layout(container, *group_x, *group_y, *group_w, *group_h, start, end); - break; - } - /* update position for next group */ - pos += group_dim; + /* update position for next group */ + pos += group_dim; + } } } @@ -1508,7 +1485,7 @@ bool is_auto_layout(enum swayc_layouts layout) { /** * Return the number of master elements in a container */ -static inline size_t auto_master_count(swayc_t *container) { +static inline size_t auto_master_count(const swayc_t *container) { return MIN(container->nb_master, container->children->length); } @@ -1516,21 +1493,21 @@ static inline size_t auto_master_count(swayc_t *container) { * Return the number of children in the slave groups. This corresponds to the children * that are not members of the master group. */ -static inline size_t auto_slave_count(swayc_t *container) { +static inline size_t auto_slave_count(const swayc_t *container) { return container->children->length - auto_master_count(container); } /** * Return the number of slave groups in the container. */ -size_t auto_slave_group_count(swayc_t *container) { +size_t auto_slave_group_count(const swayc_t *container) { return MIN(container->nb_slave_groups, auto_slave_count(container)); } /** * Return the combined number of master and slave groups in the container. */ -size_t auto_group_count(swayc_t *container) { +size_t auto_group_count(const swayc_t *container) { return auto_slave_group_count(container) + (container->nb_master ? 1 : 0); } @@ -1538,7 +1515,7 @@ size_t auto_group_count(swayc_t *container) { * given the index of a container's child, return the index of the first child of the group * which index is a member of. */ -int auto_group_start_index(swayc_t *container, int index) { +int auto_group_start_index(const swayc_t *container, int index) { if (index < 0 || ! is_auto_layout(container->layout) || (size_t) index < container->nb_master) { return 0; @@ -1563,7 +1540,7 @@ int auto_group_start_index(swayc_t *container, int index) { * that follows the one which index is a member of. * This makes the function usable to walk through the groups in a container. */ -int auto_group_end_index(swayc_t *container, int index) { +int auto_group_end_index(const swayc_t *container, int index) { if (index < 0 || ! is_auto_layout(container->layout)) { return container->children->length; } else { @@ -1590,7 +1567,7 @@ int auto_group_end_index(swayc_t *container, int index) { * return the index of the Group containing th child of . * The index is the order of the group along the container's major axis (starting at 0). */ -size_t auto_group_index(swayc_t *container, int index) { +size_t auto_group_index(const swayc_t *container, int index) { if (index < 0) { return 0; } @@ -1616,3 +1593,48 @@ size_t auto_group_index(swayc_t *container, int index) { return grp_idx + (master_first ? 1 : 0); } } + +/** + * Return the first index (inclusive) and last index (exclusive) of the elements of a group in + * an auto layout. + * If the bounds of the given group can be calculated, they are returned in the start/end + * parameters (int pointers) and the return value will be true. + * The indexes are passed by reference and can be NULL. + */ +bool auto_group_bounds(const swayc_t *container, size_t group_index, int *start, int *end) { + size_t nb_grp = auto_group_count(container); + if (group_index >= nb_grp) { + return false; + } + bool master_first = (container->layout == L_AUTO_LEFT || container->layout == L_AUTO_TOP); + size_t nb_master = auto_master_count(container); + size_t nb_slave_grp = auto_slave_group_count(container); + int g_start, g_end; + if (nb_master && (master_first ? group_index == 0 : group_index == nb_grp - 1)) { + g_start = 0; + g_end = nb_master; + } else { + size_t nb_slaves = auto_slave_count(container); + size_t grp_sz = nb_slaves / nb_slave_grp; + size_t remainder = nb_slaves % nb_slave_grp; + size_t g0 = master_first && container->nb_master ? 1 : 0; + size_t g1 = g0 + nb_slave_grp - remainder; + if (group_index < g1) { + g_start = container->nb_master + (group_index - g0) * grp_sz; + g_end = g_start + grp_sz; + } else { + size_t g2 = group_index - g1; + g_start = container->nb_master + + (nb_slave_grp - remainder) * grp_sz + + g2 * (grp_sz + 1); + g_end = g_start + grp_sz + 1; + } + } + if (start) { + *start = g_start; + } + if (end) { + *end = g_end; + } + return true; +} -- cgit v1.2.3