Skip to content

Commit 0704248

Browse files
XyeneRedSoxFan
authored andcommitted
tree/container: introduce container_is_sticky[_or_child] functions
To query whether a container is sticky, checking `con->is_sticky` is insufficient. `container_is_floating_or_child` must also return true; this led to a lot of repetition. This commit introduces `container_is_sticky[_or_child]` functions, and switches all stickiness checks to use them. (Including ones where the container is already known to be floating, for consistency.)
1 parent a56098a commit 0704248

File tree

7 files changed

+25
-14
lines changed

7 files changed

+25
-14
lines changed

include/sway/tree/container.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ struct sway_container {
7878
enum sway_container_layout layout;
7979
enum sway_container_layout prev_split_layout;
8080

81+
// Whether stickiness has been enabled on this container. Use
82+
// `container_is_sticky_[or_child]` rather than accessing this field
83+
// directly; it'll also check that the container is floating.
8184
bool is_sticky;
8285

8386
// For C_ROOT, this has no meaning
@@ -367,4 +370,8 @@ bool container_is_scratchpad_hidden(struct sway_container *con);
367370

368371
bool container_is_scratchpad_hidden_or_child(struct sway_container *con);
369372

373+
bool container_is_sticky(struct sway_container *con);
374+
375+
bool container_is_sticky_or_child(struct sway_container *con);
376+
370377
#endif

sway/commands/move.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ static struct cmd_results *cmd_move_container(bool no_auto_back_and_forth,
482482
// We have to create the workspace, but if the container is
483483
// sticky and the workspace is going to be created on the same
484484
// output, we'll bail out first.
485-
if (container->is_sticky && container_is_floating_or_child(container)) {
485+
if (container_is_sticky_or_child(container)) {
486486
struct sway_output *new_output =
487487
workspace_get_initial_output(ws_name);
488488
if (old_output == new_output) {
@@ -521,8 +521,8 @@ static struct cmd_results *cmd_move_container(bool no_auto_back_and_forth,
521521
return cmd_move_to_scratchpad();
522522
}
523523

524-
if (container->is_sticky && container_is_floating_or_child(container) &&
525-
old_output && node_has_ancestor(destination, &old_output->node)) {
524+
if (container_is_sticky_or_child(container) && old_output &&
525+
node_has_ancestor(destination, &old_output->node)) {
526526
return cmd_results_new(CMD_FAILURE, "Can't move sticky "
527527
"container to another workspace on the same output");
528528
}

sway/commands/sticky.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct cmd_results *cmd_sticky(int argc, char **argv) {
2525

2626
container->is_sticky = parse_boolean(argv[0], container->is_sticky);
2727

28-
if (container->is_sticky && container_is_floating_or_child(container) &&
28+
if (container_is_sticky_or_child(container) &&
2929
!container_is_scratchpad_hidden(container)) {
3030
// move container to active workspace
3131
struct sway_workspace *active_workspace =

sway/input/seat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,7 @@ void seat_set_focus(struct sway_seat *seat, struct sway_node *node) {
11511151
for (int i = 0; i < new_output_last_ws->floating->length; ++i) {
11521152
struct sway_container *floater =
11531153
new_output_last_ws->floating->items[i];
1154-
if (floater->is_sticky) {
1154+
if (container_is_sticky(floater)) {
11551155
container_detach(floater);
11561156
workspace_add_floating(new_workspace, floater);
11571157
--i;

sway/tree/container.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,3 +1609,11 @@ bool container_is_scratchpad_hidden_or_child(struct sway_container *con) {
16091609
con = container_toplevel_ancestor(con);
16101610
return con->scratchpad && !con->workspace;
16111611
}
1612+
1613+
bool container_is_sticky(struct sway_container *con) {
1614+
return con->is_sticky && container_is_floating(con);
1615+
}
1616+
1617+
bool container_is_sticky_or_child(struct sway_container *con) {
1618+
return container_is_sticky(container_toplevel_ancestor(con));
1619+
}

sway/tree/view.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,13 +1244,9 @@ bool view_is_visible(struct sway_view *view) {
12441244
return false;
12451245
}
12461246
}
1247-
// Determine if view is nested inside a floating container which is sticky
1248-
struct sway_container *floater = view->container;
1249-
while (floater->parent) {
1250-
floater = floater->parent;
1251-
}
1252-
bool is_sticky = container_is_floating(floater) && floater->is_sticky;
1253-
if (!is_sticky && workspace && !workspace_is_visible(workspace)) {
1247+
1248+
if (!container_is_sticky_or_child(view->container) && workspace &&
1249+
!workspace_is_visible(workspace)) {
12541250
return false;
12551251
}
12561252
// Check view isn't in a tabbed or stacked container on an inactive tab

sway/tree/workspace.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ bool workspace_is_empty(struct sway_workspace *ws) {
498498
// Sticky views are not considered to be part of this workspace
499499
for (int i = 0; i < ws->floating->length; ++i) {
500500
struct sway_container *floater = ws->floating->items[i];
501-
if (!floater->is_sticky) {
501+
if (!container_is_sticky(floater)) {
502502
return false;
503503
}
504504
}
@@ -819,7 +819,7 @@ size_t workspace_num_tiling_views(struct sway_workspace *ws) {
819819
}
820820

821821
static void count_sticky_containers(struct sway_container *con, void *data) {
822-
if (container_is_floating(con) && con->is_sticky) {
822+
if (container_is_sticky(con)) {
823823
size_t *count = data;
824824
*count += 1;
825825
}

0 commit comments

Comments
 (0)