diff options
author | Brian Ashworth <bosrsf04@gmail.com> | 2019-01-14 21:45:14 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-14 21:45:14 -0500 |
commit | bbd312c95a35db9604cfac1c7b179c3d716ff094 (patch) | |
tree | a46789c96e74e56ef9bf84d83e26c1937bdfe57e /sway/commands/resize.c | |
parent | Disarm key repeat on reload (diff) | |
parent | Fix invalid pointers when using resize grow width on first/last siblings (diff) | |
download | sway-bbd312c95a35db9604cfac1c7b179c3d716ff094.tar.gz sway-bbd312c95a35db9604cfac1c7b179c3d716ff094.tar.zst sway-bbd312c95a35db9604cfac1c7b179c3d716ff094.zip |
Merge pull request #3422 from RyanDwyer/resize-immediate-siblings
Resize only current and immediate siblings rather than all siblings
Diffstat (limited to 'sway/commands/resize.c')
-rw-r--r-- | sway/commands/resize.c | 246 |
1 files changed, 94 insertions, 152 deletions
diff --git a/sway/commands/resize.c b/sway/commands/resize.c index fad1ecb1..6cdeb90c 100644 --- a/sway/commands/resize.c +++ b/sway/commands/resize.c | |||
@@ -130,177 +130,117 @@ static bool is_horizontal(uint32_t axis) { | |||
130 | return axis & (WLR_EDGE_LEFT | WLR_EDGE_RIGHT); | 130 | return axis & (WLR_EDGE_LEFT | WLR_EDGE_RIGHT); |
131 | } | 131 | } |
132 | 132 | ||
133 | static int parallel_coord(struct sway_container *c, uint32_t axis) { | 133 | struct sway_container *container_find_resize_parent(struct sway_container *con, |
134 | return is_horizontal(axis) ? c->x : c->y; | 134 | uint32_t axis) { |
135 | } | 135 | enum sway_container_layout parallel_layout = |
136 | 136 | is_horizontal(axis) ? L_HORIZ : L_VERT; | |
137 | static int parallel_size(struct sway_container *c, uint32_t axis) { | 137 | bool allow_first = axis != WLR_EDGE_TOP && axis != WLR_EDGE_LEFT; |
138 | return is_horizontal(axis) ? c->width : c->height; | 138 | bool allow_last = axis != WLR_EDGE_RIGHT && axis != WLR_EDGE_BOTTOM; |
139 | } | 139 | |
140 | 140 | while (con) { | |
141 | static void container_recursive_resize(struct sway_container *container, | 141 | list_t *siblings = container_get_siblings(con); |
142 | double amount, enum wlr_edges edge) { | 142 | int index = container_sibling_index(con); |
143 | bool layout_match = true; | 143 | if (container_parent_layout(con) == parallel_layout && |
144 | wlr_log(WLR_DEBUG, "Resizing %p with amount: %f", container, amount); | 144 | siblings->length > 1 && (allow_first || index > 0) && |
145 | if (edge & (WLR_EDGE_LEFT | WLR_EDGE_RIGHT)) { | 145 | (allow_last || index < siblings->length - 1)) { |
146 | container->width += amount; | 146 | return con; |
147 | layout_match = container->layout == L_HORIZ; | ||
148 | } else if (edge & (WLR_EDGE_TOP | WLR_EDGE_BOTTOM)) { | ||
149 | container->height += amount; | ||
150 | layout_match = container->layout == L_VERT; | ||
151 | } | ||
152 | if (container->children) { | ||
153 | for (int i = 0; i < container->children->length; i++) { | ||
154 | struct sway_container *child = container->children->items[i]; | ||
155 | double amt = layout_match ? | ||
156 | amount / container->children->length : amount; | ||
157 | container_recursive_resize(child, amt, edge); | ||
158 | } | 147 | } |
148 | con = con->parent; | ||
159 | } | 149 | } |
150 | |||
151 | return NULL; | ||
160 | } | 152 | } |
161 | 153 | ||
162 | static void resize_tiled(struct sway_container *parent, int amount, | 154 | void container_resize_tiled(struct sway_container *con, |
163 | uint32_t axis) { | 155 | uint32_t axis, int amount) { |
164 | struct sway_container *focused = parent; | 156 | if (!con) { |
165 | if (!parent) { | ||
166 | return; | 157 | return; |
167 | } | 158 | } |
168 | 159 | ||
169 | enum sway_container_layout parallel_layout = | 160 | con = container_find_resize_parent(con, axis); |
170 | is_horizontal(axis) ? L_HORIZ : L_VERT; | 161 | if (!con) { |
171 | int minor_weight = 0; | ||
172 | int major_weight = 0; | ||
173 | while (parent) { | ||
174 | list_t *siblings = container_get_siblings(parent); | ||
175 | if (container_parent_layout(parent) == parallel_layout) { | ||
176 | for (int i = 0; i < siblings->length; i++) { | ||
177 | struct sway_container *sibling = siblings->items[i]; | ||
178 | |||
179 | int sibling_pos = parallel_coord(sibling, axis); | ||
180 | int focused_pos = parallel_coord(focused, axis); | ||
181 | int parent_pos = parallel_coord(parent, axis); | ||
182 | |||
183 | if (sibling_pos != focused_pos) { | ||
184 | if (sibling_pos < parent_pos) { | ||
185 | minor_weight++; | ||
186 | } else if (sibling_pos > parent_pos) { | ||
187 | major_weight++; | ||
188 | } | ||
189 | } | ||
190 | } | ||
191 | if (major_weight || minor_weight) { | ||
192 | break; | ||
193 | } | ||
194 | } | ||
195 | parent = parent->parent; | ||
196 | } | ||
197 | if (!parent) { | ||
198 | // Can't resize in this direction | 162 | // Can't resize in this direction |
199 | return; | 163 | return; |
200 | } | 164 | } |
201 | 165 | ||
202 | // Implement up/down/left/right direction by zeroing one of the weights | 166 | // For HORIZONTAL or VERTICAL, we are growing in two directions so select |
203 | if (axis == WLR_EDGE_TOP || axis == WLR_EDGE_LEFT) { | 167 | // both adjacent siblings. For RIGHT or DOWN, just select the next sibling. |
204 | major_weight = 0; | 168 | // For LEFT or UP, convert it to a RIGHT or DOWN resize and reassign con to |
205 | } else if (axis == WLR_EDGE_RIGHT || axis == WLR_EDGE_BOTTOM) { | 169 | // the previous sibling. |
206 | minor_weight = 0; | 170 | struct sway_container *prev = NULL; |
207 | } | 171 | struct sway_container *next = NULL; |
208 | 172 | list_t *siblings = container_get_siblings(con); | |
209 | bool horizontal = is_horizontal(axis); | 173 | int index = container_sibling_index(con); |
210 | int min_sane = horizontal ? MIN_SANE_W : MIN_SANE_H; | 174 | |
211 | 175 | if (axis == AXIS_HORIZONTAL || axis == AXIS_VERTICAL) { | |
212 | //TODO: Ensure rounding is done in such a way that there are NO pixel leaks | 176 | if (index == 0) { |
213 | // ^ ????? | 177 | next = siblings->items[1]; |
214 | list_t *siblings = container_get_siblings(parent); | 178 | } else if (index == siblings->length - 1) { |
215 | 179 | // Convert edge to top/left | |
216 | for (int i = 0; i < siblings->length; i++) { | 180 | next = con; |
217 | struct sway_container *sibling = siblings->items[i]; | 181 | con = siblings->items[index - 1]; |
218 | 182 | amount = -amount; | |
219 | int sibling_pos = parallel_coord(sibling, axis); | ||
220 | int focused_pos = parallel_coord(focused, axis); | ||
221 | int parent_pos = parallel_coord(parent, axis); | ||
222 | |||
223 | int sibling_size = parallel_size(sibling, axis); | ||
224 | int parent_size = parallel_size(parent, axis); | ||
225 | |||
226 | if (sibling_pos != focused_pos) { | ||
227 | if (sibling_pos < parent_pos && minor_weight) { | ||
228 | double pixels = -amount / minor_weight; | ||
229 | if (major_weight && (sibling_size + pixels / 2) < min_sane) { | ||
230 | return; // Too small | ||
231 | } else if (!major_weight && sibling_size + pixels < min_sane) { | ||
232 | return; // Too small | ||
233 | } | ||
234 | } else if (sibling_pos > parent_pos && major_weight) { | ||
235 | double pixels = -amount / major_weight; | ||
236 | if (minor_weight && (sibling_size + pixels / 2) < min_sane) { | ||
237 | return; // Too small | ||
238 | } else if (!minor_weight && sibling_size + pixels < min_sane) { | ||
239 | return; // Too small | ||
240 | } | ||
241 | } | ||
242 | } else { | 183 | } else { |
243 | double pixels = amount; | 184 | prev = siblings->items[index - 1]; |
244 | if (parent_size + pixels < min_sane) { | 185 | next = siblings->items[index + 1]; |
245 | return; // Too small | 186 | } |
246 | } | 187 | } else if (axis == WLR_EDGE_TOP || axis == WLR_EDGE_LEFT) { |
188 | if (!sway_assert(index > 0, "Didn't expect first child")) { | ||
189 | return; | ||
247 | } | 190 | } |
191 | next = con; | ||
192 | con = siblings->items[index - 1]; | ||
193 | amount = -amount; | ||
194 | } else { | ||
195 | if (!sway_assert(index < siblings->length - 1, | ||
196 | "Didn't expect last child")) { | ||
197 | return; | ||
198 | } | ||
199 | next = siblings->items[index + 1]; | ||
248 | } | 200 | } |
249 | 201 | ||
250 | enum wlr_edges minor_edge = horizontal ? WLR_EDGE_LEFT : WLR_EDGE_TOP; | 202 | // Apply new dimensions |
251 | enum wlr_edges major_edge = horizontal ? WLR_EDGE_RIGHT : WLR_EDGE_BOTTOM; | 203 | int sibling_amount = prev ? amount / 2 : amount; |
252 | 204 | ||
253 | for (int i = 0; i < siblings->length; i++) { | 205 | if (is_horizontal(axis)) { |
254 | struct sway_container *sibling = siblings->items[i]; | 206 | if (con->width + amount < MIN_SANE_W) { |
255 | 207 | return; | |
256 | int sibling_pos = parallel_coord(sibling, axis); | 208 | } |
257 | int focused_pos = parallel_coord(focused, axis); | 209 | if (next->width - sibling_amount < MIN_SANE_W) { |
258 | int parent_pos = parallel_coord(parent, axis); | 210 | return; |
259 | 211 | } | |
260 | if (sibling_pos != focused_pos) { | 212 | if (prev && prev->width - sibling_amount < MIN_SANE_W) { |
261 | if (sibling_pos < parent_pos && minor_weight) { | 213 | return; |
262 | double pixels = -1 * amount; | 214 | } |
263 | pixels /= minor_weight; | 215 | con->width += amount; |
264 | if (major_weight) { | 216 | next->width -= sibling_amount; |
265 | container_recursive_resize(sibling, pixels / 2, major_edge); | 217 | if (prev) { |
266 | } else { | 218 | prev->width -= sibling_amount; |
267 | container_recursive_resize(sibling, pixels, major_edge); | 219 | } |
268 | } | 220 | } else { |
269 | } else if (sibling_pos > parent_pos && major_weight) { | 221 | if (con->height + amount < MIN_SANE_H) { |
270 | double pixels = -1 * amount; | 222 | return; |
271 | pixels /= major_weight; | 223 | } |
272 | if (minor_weight) { | 224 | if (next->height - sibling_amount < MIN_SANE_H) { |
273 | container_recursive_resize(sibling, pixels / 2, minor_edge); | 225 | return; |
274 | } else { | 226 | } |
275 | container_recursive_resize(sibling, pixels, minor_edge); | 227 | if (prev && prev->height - sibling_amount < MIN_SANE_H) { |
276 | } | 228 | return; |
277 | } | 229 | } |
278 | } else { | 230 | con->height += amount; |
279 | if (major_weight != 0 && minor_weight != 0) { | 231 | next->height -= sibling_amount; |
280 | double pixels = amount; | 232 | if (prev) { |
281 | pixels /= 2; | 233 | prev->height -= sibling_amount; |
282 | container_recursive_resize(parent, pixels, minor_edge); | ||
283 | container_recursive_resize(parent, pixels, major_edge); | ||
284 | } else if (major_weight) { | ||
285 | container_recursive_resize(parent, amount, major_edge); | ||
286 | } else if (minor_weight) { | ||
287 | container_recursive_resize(parent, amount, minor_edge); | ||
288 | } | ||
289 | } | 234 | } |
290 | } | 235 | } |
291 | 236 | ||
292 | if (parent->parent) { | 237 | if (con->parent) { |
293 | arrange_container(parent->parent); | 238 | arrange_container(con->parent); |
294 | } else { | 239 | } else { |
295 | arrange_workspace(parent->workspace); | 240 | arrange_workspace(con->workspace); |
296 | } | 241 | } |
297 | } | 242 | } |
298 | 243 | ||
299 | void container_resize_tiled(struct sway_container *parent, | ||
300 | enum wlr_edges edge, int amount) { | ||
301 | resize_tiled(parent, amount, edge); | ||
302 | } | ||
303 | |||
304 | /** | 244 | /** |
305 | * Implement `resize <grow|shrink>` for a floating container. | 245 | * Implement `resize <grow|shrink>` for a floating container. |
306 | */ | 246 | */ |
@@ -379,7 +319,7 @@ static struct cmd_results *resize_adjust_tiled(uint32_t axis, | |||
379 | 319 | ||
380 | double old_width = current->width; | 320 | double old_width = current->width; |
381 | double old_height = current->height; | 321 | double old_height = current->height; |
382 | resize_tiled(current, amount->amount, axis); | 322 | container_resize_tiled(current, axis, amount->amount); |
383 | if (current->width == old_width && current->height == old_height) { | 323 | if (current->width == old_width && current->height == old_height) { |
384 | return cmd_results_new(CMD_INVALID, "Cannot resize any further"); | 324 | return cmd_results_new(CMD_INVALID, "Cannot resize any further"); |
385 | } | 325 | } |
@@ -407,7 +347,8 @@ static struct cmd_results *resize_set_tiled(struct sway_container *con, | |||
407 | width->unit = RESIZE_UNIT_PX; | 347 | width->unit = RESIZE_UNIT_PX; |
408 | } | 348 | } |
409 | if (width->unit == RESIZE_UNIT_PX) { | 349 | if (width->unit == RESIZE_UNIT_PX) { |
410 | resize_tiled(con, width->amount - con->width, AXIS_HORIZONTAL); | 350 | container_resize_tiled(con, AXIS_HORIZONTAL, |
351 | width->amount - con->width); | ||
411 | } | 352 | } |
412 | } | 353 | } |
413 | 354 | ||
@@ -427,7 +368,8 @@ static struct cmd_results *resize_set_tiled(struct sway_container *con, | |||
427 | height->unit = RESIZE_UNIT_PX; | 368 | height->unit = RESIZE_UNIT_PX; |
428 | } | 369 | } |
429 | if (height->unit == RESIZE_UNIT_PX) { | 370 | if (height->unit == RESIZE_UNIT_PX) { |
430 | resize_tiled(con, height->amount - con->height, AXIS_VERTICAL); | 371 | container_resize_tiled(con, AXIS_VERTICAL, |
372 | height->amount - con->height); | ||
431 | } | 373 | } |
432 | } | 374 | } |
433 | 375 | ||