diff options
author | wil <william.barsse@gmail.com> | 2017-01-07 17:41:15 +0100 |
---|---|---|
committer | wil <william.barsse@gmail.com> | 2017-01-07 17:41:15 +0100 |
commit | bd415029ba72425c97647b55fce19213e7909cbc (patch) | |
tree | c10ce0c530bc0900bd2b9744afcede424a763077 | |
parent | Merge branch 'master' of https://github.com/willakat/sway (diff) | |
download | sway-bd415029ba72425c97647b55fce19213e7909cbc.tar.gz sway-bd415029ba72425c97647b55fce19213e7909cbc.tar.zst sway-bd415029ba72425c97647b55fce19213e7909cbc.zip |
Moved auto_* layout functions from resize.c to layout.c
-rw-r--r-- | include/sway/layout.h | 4 | ||||
-rw-r--r-- | sway/commands/resize.c | 98 | ||||
-rw-r--r-- | sway/layout.c | 93 |
3 files changed, 99 insertions, 96 deletions
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, .. | |||
76 | enum swayc_layouts default_layout(swayc_t *output); | 76 | enum swayc_layouts default_layout(swayc_t *output); |
77 | 77 | ||
78 | bool is_auto_layout(enum swayc_layouts layout); | 78 | bool is_auto_layout(enum swayc_layouts layout); |
79 | int auto_group_start_index(swayc_t *container, int index); | ||
80 | int auto_group_end_index(swayc_t *container, int index); | ||
81 | size_t auto_group_count(swayc_t *container); | ||
82 | size_t auto_group_index(swayc_t *container, int index); | ||
79 | 83 | ||
80 | #endif | 84 | #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) { | |||
62 | return false; | 62 | return false; |
63 | } | 63 | } |
64 | 64 | ||
65 | /** | ||
66 | * Return the number of children in the slave groups. This corresponds to the children | ||
67 | * that are not members of the master group. | ||
68 | */ | ||
69 | static inline size_t auto_slave_count(swayc_t *container) { | ||
70 | return container->children->length - container->nb_master; | ||
71 | |||
72 | } | ||
73 | |||
74 | /** | ||
75 | * given the index of a container's child, return the index of the first child of the group | ||
76 | * which index is a member of. | ||
77 | */ | ||
78 | static int auto_group_start_index(swayc_t *container, int index) { | ||
79 | if (index < 0 || ! is_auto_layout(container->layout) | ||
80 | || (size_t) index < container->nb_master) { | ||
81 | return 0; | ||
82 | } else { | ||
83 | size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups; | ||
84 | size_t remainder = auto_slave_count(container) % container->nb_slave_groups; | ||
85 | int start_idx; | ||
86 | int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; | ||
87 | if (index < idx2) { | ||
88 | start_idx = ((index - container->nb_master) / grp_sz) * grp_sz + container->nb_master; | ||
89 | } else { | ||
90 | start_idx = idx2 + ((index - idx2) / (grp_sz + 1)) * (grp_sz + 1); | ||
91 | } | ||
92 | return MIN(start_idx, container->children->length); | ||
93 | } | ||
94 | } | ||
95 | |||
96 | /** | ||
97 | * given the index of a container's child, return the index of the first child of the group | ||
98 | * that follows the one which index is a member of. | ||
99 | * This makes the function usable to walk through the groups in a container. | ||
100 | */ | ||
101 | static int auto_group_end_index(swayc_t *container, int index) { | ||
102 | if (index < 0 || ! is_auto_layout(container->layout)) { | ||
103 | return container->children->length; | ||
104 | } else { | ||
105 | int nxt_idx; | ||
106 | if ((size_t)index < container->nb_master) { | ||
107 | nxt_idx = container->nb_master; | ||
108 | } else { | ||
109 | size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups; | ||
110 | size_t remainder = auto_slave_count(container) % container->nb_slave_groups; | ||
111 | int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; | ||
112 | if (index < idx2) { | ||
113 | nxt_idx = ((index - container->nb_master) / grp_sz + 1) * grp_sz + container->nb_master; | ||
114 | } else { | ||
115 | nxt_idx = idx2 + ((index - idx2) / (grp_sz + 1) + 1) * (grp_sz + 1); | ||
116 | } | ||
117 | } | ||
118 | return MIN(nxt_idx, container->children->length); | ||
119 | } | ||
120 | } | ||
121 | |||
122 | /** | ||
123 | * Return the combined number of master and slave groups in the container. | ||
124 | */ | ||
125 | static inline size_t auto_group_count(swayc_t *container) { | ||
126 | return MIN(container->nb_slave_groups, auto_slave_count(container)) + (container->nb_master ? 1 : 0); | ||
127 | } | ||
128 | |||
129 | /** | ||
130 | * return the index of the Group containing <index>th child of <container>. | ||
131 | * The index is the order of the group along the container's major axis (starting at 0). | ||
132 | */ | ||
133 | static size_t auto_group_index(swayc_t *container, int index) { | ||
134 | if (index < 0) { | ||
135 | return 0; | ||
136 | } | ||
137 | bool master_first = (container->layout == L_AUTO_LEFT || container->layout == L_AUTO_TOP); | ||
138 | int nb_slaves = auto_slave_count(container); | ||
139 | if ((size_t) index < container->nb_master) { | ||
140 | if (master_first || nb_slaves <= 0) { | ||
141 | return 0; | ||
142 | } else { | ||
143 | return MIN(container->nb_slave_groups, nb_slaves); | ||
144 | } | ||
145 | } else { | ||
146 | size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups; | ||
147 | size_t remainder = auto_slave_count(container) % container->nb_slave_groups; | ||
148 | size_t grp_idx; | ||
149 | int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; | ||
150 | if (index < idx2) { | ||
151 | grp_idx = (index - container->nb_master) / grp_sz; | ||
152 | } else { | ||
153 | grp_idx = (container->nb_slave_groups - remainder) + (index - idx2) / (grp_sz + 1) ; | ||
154 | } | ||
155 | return grp_idx + (master_first ? 1 : 0); | ||
156 | } | ||
157 | } | ||
158 | |||
159 | static bool resize_tiled(int amount, bool use_width) { | 65 | static bool resize_tiled(int amount, bool use_width) { |
160 | swayc_t *container = get_focused_view(swayc_active_workspace()); | 66 | swayc_t *container = get_focused_view(swayc_active_workspace()); |
161 | swayc_t *parent = container->parent; | 67 | swayc_t *parent = container->parent; |
@@ -229,8 +135,8 @@ static bool resize_tiled(int amount, bool use_width) { | |||
229 | } | 135 | } |
230 | 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); | 136 | 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); |
231 | if (use_width ? | 137 | if (use_width ? |
232 | sibling->width + pixels < min_sane_w : | 138 | sibling->width + pixels < min_sane_w : |
233 | sibling->height + pixels < min_sane_h) { | 139 | sibling->height + pixels < min_sane_h) { |
234 | valid = false; | 140 | valid = false; |
235 | sway_log(L_DEBUG, "Container size no longer sane"); | 141 | sway_log(L_DEBUG, "Container size no longer sane"); |
236 | break; | 142 | 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) { | |||
1503 | bool is_auto_layout(enum swayc_layouts layout) { | 1503 | bool is_auto_layout(enum swayc_layouts layout) { |
1504 | return (layout >= L_AUTO_FIRST) && (layout <= L_AUTO_LAST); | 1504 | return (layout >= L_AUTO_FIRST) && (layout <= L_AUTO_LAST); |
1505 | } | 1505 | } |
1506 | |||
1507 | /** | ||
1508 | * Return the number of children in the slave groups. This corresponds to the children | ||
1509 | * that are not members of the master group. | ||
1510 | */ | ||
1511 | static inline size_t auto_slave_count(swayc_t *container) { | ||
1512 | return container->children->length - container->nb_master; | ||
1513 | } | ||
1514 | |||
1515 | /** | ||
1516 | * given the index of a container's child, return the index of the first child of the group | ||
1517 | * which index is a member of. | ||
1518 | */ | ||
1519 | int auto_group_start_index(swayc_t *container, int index) { | ||
1520 | if (index < 0 || ! is_auto_layout(container->layout) | ||
1521 | || (size_t) index < container->nb_master) { | ||
1522 | return 0; | ||
1523 | } else { | ||
1524 | size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups; | ||
1525 | size_t remainder = auto_slave_count(container) % container->nb_slave_groups; | ||
1526 | int start_idx; | ||
1527 | int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; | ||
1528 | if (index < idx2) { | ||
1529 | start_idx = ((index - container->nb_master) / grp_sz) * grp_sz + container->nb_master; | ||
1530 | } else { | ||
1531 | start_idx = idx2 + ((index - idx2) / (grp_sz + 1)) * (grp_sz + 1); | ||
1532 | } | ||
1533 | return MIN(start_idx, container->children->length); | ||
1534 | } | ||
1535 | } | ||
1536 | |||
1537 | /** | ||
1538 | * given the index of a container's child, return the index of the first child of the group | ||
1539 | * that follows the one which index is a member of. | ||
1540 | * This makes the function usable to walk through the groups in a container. | ||
1541 | */ | ||
1542 | int auto_group_end_index(swayc_t *container, int index) { | ||
1543 | if (index < 0 || ! is_auto_layout(container->layout)) { | ||
1544 | return container->children->length; | ||
1545 | } else { | ||
1546 | int nxt_idx; | ||
1547 | if ((size_t)index < container->nb_master) { | ||
1548 | nxt_idx = container->nb_master; | ||
1549 | } else { | ||
1550 | size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups; | ||
1551 | size_t remainder = auto_slave_count(container) % container->nb_slave_groups; | ||
1552 | int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; | ||
1553 | if (index < idx2) { | ||
1554 | nxt_idx = ((index - container->nb_master) / grp_sz + 1) * grp_sz + container->nb_master; | ||
1555 | } else { | ||
1556 | nxt_idx = idx2 + ((index - idx2) / (grp_sz + 1) + 1) * (grp_sz + 1); | ||
1557 | } | ||
1558 | } | ||
1559 | return MIN(nxt_idx, container->children->length); | ||
1560 | } | ||
1561 | } | ||
1562 | |||
1563 | /** | ||
1564 | * Return the combined number of master and slave groups in the container. | ||
1565 | */ | ||
1566 | size_t auto_group_count(swayc_t *container) { | ||
1567 | return MIN(container->nb_slave_groups, auto_slave_count(container)) + (container->nb_master ? 1 : 0); | ||
1568 | } | ||
1569 | |||
1570 | /** | ||
1571 | * return the index of the Group containing <index>th child of <container>. | ||
1572 | * The index is the order of the group along the container's major axis (starting at 0). | ||
1573 | */ | ||
1574 | size_t auto_group_index(swayc_t *container, int index) { | ||
1575 | if (index < 0) { | ||
1576 | return 0; | ||
1577 | } | ||
1578 | bool master_first = (container->layout == L_AUTO_LEFT || container->layout == L_AUTO_TOP); | ||
1579 | int nb_slaves = auto_slave_count(container); | ||
1580 | if ((size_t) index < container->nb_master) { | ||
1581 | if (master_first || nb_slaves <= 0) { | ||
1582 | return 0; | ||
1583 | } else { | ||
1584 | return MIN(container->nb_slave_groups, nb_slaves); | ||
1585 | } | ||
1586 | } else { | ||
1587 | size_t grp_sz = auto_slave_count(container) / container->nb_slave_groups; | ||
1588 | size_t remainder = auto_slave_count(container) % container->nb_slave_groups; | ||
1589 | size_t grp_idx; | ||
1590 | int idx2 = (container->nb_slave_groups - remainder) * grp_sz + container->nb_master; | ||
1591 | if (index < idx2) { | ||
1592 | grp_idx = (index - container->nb_master) / grp_sz; | ||
1593 | } else { | ||
1594 | grp_idx = (container->nb_slave_groups - remainder) + (index - idx2) / (grp_sz + 1) ; | ||
1595 | } | ||
1596 | return grp_idx + (master_first ? 1 : 0); | ||
1597 | } | ||
1598 | } | ||