diff options
author | 2019-01-14 11:19:18 +0100 | |
---|---|---|
committer | 2019-01-14 11:19:18 +0100 | |
commit | 64ef9366733d6d332a24897f72eba90ba2adca1e (patch) | |
tree | 69c687149ddca5e87ffc109b1797112a98817a0d /sway/commands/resize.c | |
parent | Merge pull request #3419 from ianyfan/swaynag (diff) | |
parent | Use uint32_t where resize axis might contain a bitfield (diff) | |
download | sway-64ef9366733d6d332a24897f72eba90ba2adca1e.tar.gz sway-64ef9366733d6d332a24897f72eba90ba2adca1e.tar.zst sway-64ef9366733d6d332a24897f72eba90ba2adca1e.zip |
Merge pull request #3418 from RyanDwyer/remove-resize-axis
Replace resize_axis with wlr_edges
Diffstat (limited to 'sway/commands/resize.c')
-rw-r--r-- | sway/commands/resize.c | 161 |
1 files changed, 47 insertions, 114 deletions
diff --git a/sway/commands/resize.c b/sway/commands/resize.c index cf5dea02..0e849afc 100644 --- a/sway/commands/resize.c +++ b/sway/commands/resize.c | |||
@@ -13,6 +13,9 @@ | |||
13 | #include "sway/tree/workspace.h" | 13 | #include "sway/tree/workspace.h" |
14 | #include "log.h" | 14 | #include "log.h" |
15 | 15 | ||
16 | #define AXIS_HORIZONTAL (WLR_EDGE_LEFT | WLR_EDGE_RIGHT) | ||
17 | #define AXIS_VERTICAL (WLR_EDGE_TOP | WLR_EDGE_BOTTOM) | ||
18 | |||
16 | static const int MIN_SANE_W = 100, MIN_SANE_H = 60; | 19 | static const int MIN_SANE_W = 100, MIN_SANE_H = 60; |
17 | 20 | ||
18 | enum resize_unit { | 21 | enum resize_unit { |
@@ -22,16 +25,6 @@ enum resize_unit { | |||
22 | RESIZE_UNIT_INVALID, | 25 | RESIZE_UNIT_INVALID, |
23 | }; | 26 | }; |
24 | 27 | ||
25 | enum resize_axis { | ||
26 | RESIZE_AXIS_HORIZONTAL, | ||
27 | RESIZE_AXIS_VERTICAL, | ||
28 | RESIZE_AXIS_UP, | ||
29 | RESIZE_AXIS_DOWN, | ||
30 | RESIZE_AXIS_LEFT, | ||
31 | RESIZE_AXIS_RIGHT, | ||
32 | RESIZE_AXIS_INVALID, | ||
33 | }; | ||
34 | |||
35 | struct resize_amount { | 28 | struct resize_amount { |
36 | int amount; | 29 | int amount; |
37 | enum resize_unit unit; | 30 | enum resize_unit unit; |
@@ -111,61 +104,48 @@ static void calculate_constraints(int *min_width, int *max_width, | |||
111 | } | 104 | } |
112 | } | 105 | } |
113 | 106 | ||
114 | static enum resize_axis parse_resize_axis(const char *axis) { | 107 | static uint32_t parse_resize_axis(const char *axis) { |
115 | if (strcasecmp(axis, "width") == 0 || strcasecmp(axis, "horizontal") == 0) { | 108 | if (strcasecmp(axis, "width") == 0 || strcasecmp(axis, "horizontal") == 0) { |
116 | return RESIZE_AXIS_HORIZONTAL; | 109 | return AXIS_HORIZONTAL; |
117 | } | 110 | } |
118 | if (strcasecmp(axis, "height") == 0 || strcasecmp(axis, "vertical") == 0) { | 111 | if (strcasecmp(axis, "height") == 0 || strcasecmp(axis, "vertical") == 0) { |
119 | return RESIZE_AXIS_VERTICAL; | 112 | return AXIS_VERTICAL; |
120 | } | 113 | } |
121 | if (strcasecmp(axis, "up") == 0) { | 114 | if (strcasecmp(axis, "up") == 0) { |
122 | return RESIZE_AXIS_UP; | 115 | return WLR_EDGE_TOP; |
123 | } | 116 | } |
124 | if (strcasecmp(axis, "down") == 0) { | 117 | if (strcasecmp(axis, "down") == 0) { |
125 | return RESIZE_AXIS_DOWN; | 118 | return WLR_EDGE_BOTTOM; |
126 | } | 119 | } |
127 | if (strcasecmp(axis, "left") == 0) { | 120 | if (strcasecmp(axis, "left") == 0) { |
128 | return RESIZE_AXIS_LEFT; | 121 | return WLR_EDGE_LEFT; |
129 | } | 122 | } |
130 | if (strcasecmp(axis, "right") == 0) { | 123 | if (strcasecmp(axis, "right") == 0) { |
131 | return RESIZE_AXIS_RIGHT; | 124 | return WLR_EDGE_RIGHT; |
132 | } | 125 | } |
133 | return RESIZE_AXIS_INVALID; | 126 | return WLR_EDGE_NONE; |
134 | } | 127 | } |
135 | 128 | ||
136 | static enum resize_axis normalize_axis(enum resize_axis axis) { | 129 | static bool is_horizontal(uint32_t axis) { |
137 | switch (axis) { | 130 | return axis & (WLR_EDGE_LEFT | WLR_EDGE_RIGHT); |
138 | case RESIZE_AXIS_HORIZONTAL: | ||
139 | case RESIZE_AXIS_LEFT: | ||
140 | case RESIZE_AXIS_RIGHT: | ||
141 | return RESIZE_AXIS_HORIZONTAL; | ||
142 | case RESIZE_AXIS_VERTICAL: | ||
143 | case RESIZE_AXIS_UP: | ||
144 | case RESIZE_AXIS_DOWN: | ||
145 | return RESIZE_AXIS_VERTICAL; | ||
146 | case RESIZE_AXIS_INVALID: | ||
147 | sway_assert(false, "Never reached"); | ||
148 | } | ||
149 | sway_assert(false, "Never reached"); | ||
150 | return RESIZE_AXIS_INVALID; | ||
151 | } | 131 | } |
152 | 132 | ||
153 | static int parallel_coord(struct sway_container *c, enum resize_axis a) { | 133 | static int parallel_coord(struct sway_container *c, uint32_t axis) { |
154 | return normalize_axis(a) == RESIZE_AXIS_HORIZONTAL ? c->x : c->y; | 134 | return is_horizontal(axis) ? c->x : c->y; |
155 | } | 135 | } |
156 | 136 | ||
157 | static int parallel_size(struct sway_container *c, enum resize_axis a) { | 137 | static int parallel_size(struct sway_container *c, uint32_t axis) { |
158 | return normalize_axis(a) == RESIZE_AXIS_HORIZONTAL ? c->width : c->height; | 138 | return is_horizontal(axis) ? c->width : c->height; |
159 | } | 139 | } |
160 | 140 | ||
161 | static void container_recursive_resize(struct sway_container *container, | 141 | static void container_recursive_resize(struct sway_container *container, |
162 | double amount, enum wlr_edges edge) { | 142 | double amount, enum wlr_edges edge) { |
163 | bool layout_match = true; | 143 | bool layout_match = true; |
164 | wlr_log(WLR_DEBUG, "Resizing %p with amount: %f", container, amount); | 144 | wlr_log(WLR_DEBUG, "Resizing %p with amount: %f", container, amount); |
165 | if (edge == WLR_EDGE_LEFT || edge == WLR_EDGE_RIGHT) { | 145 | if (edge & (WLR_EDGE_LEFT | WLR_EDGE_RIGHT)) { |
166 | container->width += amount; | 146 | container->width += amount; |
167 | layout_match = container->layout == L_HORIZ; | 147 | layout_match = container->layout == L_HORIZ; |
168 | } else if (edge == WLR_EDGE_TOP || edge == WLR_EDGE_BOTTOM) { | 148 | } else if (edge & (WLR_EDGE_TOP | WLR_EDGE_BOTTOM)) { |
169 | container->height += amount; | 149 | container->height += amount; |
170 | layout_match = container->layout == L_VERT; | 150 | layout_match = container->layout == L_VERT; |
171 | } | 151 | } |
@@ -180,14 +160,14 @@ static void container_recursive_resize(struct sway_container *container, | |||
180 | } | 160 | } |
181 | 161 | ||
182 | static void resize_tiled(struct sway_container *parent, int amount, | 162 | static void resize_tiled(struct sway_container *parent, int amount, |
183 | enum resize_axis axis) { | 163 | uint32_t axis) { |
184 | struct sway_container *focused = parent; | 164 | struct sway_container *focused = parent; |
185 | if (!parent) { | 165 | if (!parent) { |
186 | return; | 166 | return; |
187 | } | 167 | } |
188 | 168 | ||
189 | enum sway_container_layout parallel_layout = | 169 | enum sway_container_layout parallel_layout = |
190 | normalize_axis(axis) == RESIZE_AXIS_HORIZONTAL ? L_HORIZ : L_VERT; | 170 | is_horizontal(axis) ? L_HORIZ : L_VERT; |
191 | int minor_weight = 0; | 171 | int minor_weight = 0; |
192 | int major_weight = 0; | 172 | int major_weight = 0; |
193 | while (parent) { | 173 | while (parent) { |
@@ -219,16 +199,15 @@ static void resize_tiled(struct sway_container *parent, int amount, | |||
219 | return; | 199 | return; |
220 | } | 200 | } |
221 | 201 | ||
222 | // Implement up/down/left/right direction by zeroing one of the weights, | 202 | // Implement up/down/left/right direction by zeroing one of the weights |
223 | // then setting the axis to be horizontal or vertical | 203 | if (axis == WLR_EDGE_TOP || axis == WLR_EDGE_LEFT) { |
224 | if (axis == RESIZE_AXIS_UP || axis == RESIZE_AXIS_LEFT) { | ||
225 | major_weight = 0; | 204 | major_weight = 0; |
226 | } else if (axis == RESIZE_AXIS_RIGHT || axis == RESIZE_AXIS_DOWN) { | 205 | } else if (axis == WLR_EDGE_RIGHT || axis == WLR_EDGE_BOTTOM) { |
227 | minor_weight = 0; | 206 | minor_weight = 0; |
228 | } | 207 | } |
229 | axis = normalize_axis(axis); | ||
230 | 208 | ||
231 | int min_sane = axis == RESIZE_AXIS_HORIZONTAL ? MIN_SANE_W : MIN_SANE_H; | 209 | bool horizontal = is_horizontal(axis); |
210 | int min_sane = horizontal ? MIN_SANE_W : MIN_SANE_H; | ||
232 | 211 | ||
233 | //TODO: Ensure rounding is done in such a way that there are NO pixel leaks | 212 | //TODO: Ensure rounding is done in such a way that there are NO pixel leaks |
234 | // ^ ????? | 213 | // ^ ????? |
@@ -268,10 +247,8 @@ static void resize_tiled(struct sway_container *parent, int amount, | |||
268 | } | 247 | } |
269 | } | 248 | } |
270 | 249 | ||
271 | enum wlr_edges minor_edge = axis == RESIZE_AXIS_HORIZONTAL ? | 250 | enum wlr_edges minor_edge = horizontal ? WLR_EDGE_LEFT : WLR_EDGE_TOP; |
272 | WLR_EDGE_LEFT : WLR_EDGE_TOP; | 251 | enum wlr_edges major_edge = horizontal ? WLR_EDGE_RIGHT : WLR_EDGE_BOTTOM; |
273 | enum wlr_edges major_edge = axis == RESIZE_AXIS_HORIZONTAL ? | ||
274 | WLR_EDGE_RIGHT : WLR_EDGE_BOTTOM; | ||
275 | 252 | ||
276 | for (int i = 0; i < siblings->length; i++) { | 253 | for (int i = 0; i < siblings->length; i++) { |
277 | struct sway_container *sibling = siblings->items[i]; | 254 | struct sway_container *sibling = siblings->items[i]; |
@@ -321,47 +298,23 @@ static void resize_tiled(struct sway_container *parent, int amount, | |||
321 | 298 | ||
322 | void container_resize_tiled(struct sway_container *parent, | 299 | void container_resize_tiled(struct sway_container *parent, |
323 | enum wlr_edges edge, int amount) { | 300 | enum wlr_edges edge, int amount) { |
324 | enum resize_axis axis = RESIZE_AXIS_INVALID; | 301 | resize_tiled(parent, amount, edge); |
325 | switch (edge) { | ||
326 | case WLR_EDGE_TOP: | ||
327 | axis = RESIZE_AXIS_UP; | ||
328 | break; | ||
329 | case WLR_EDGE_RIGHT: | ||
330 | axis = RESIZE_AXIS_RIGHT; | ||
331 | break; | ||
332 | case WLR_EDGE_BOTTOM: | ||
333 | axis = RESIZE_AXIS_DOWN; | ||
334 | break; | ||
335 | case WLR_EDGE_LEFT: | ||
336 | axis = RESIZE_AXIS_LEFT; | ||
337 | break; | ||
338 | case WLR_EDGE_NONE: | ||
339 | break; | ||
340 | } | ||
341 | resize_tiled(parent, amount, axis); | ||
342 | } | 302 | } |
343 | 303 | ||
344 | /** | 304 | /** |
345 | * Implement `resize <grow|shrink>` for a floating container. | 305 | * Implement `resize <grow|shrink>` for a floating container. |
346 | */ | 306 | */ |
347 | static struct cmd_results *resize_adjust_floating(enum resize_axis axis, | 307 | static struct cmd_results *resize_adjust_floating(uint32_t axis, |
348 | struct resize_amount *amount) { | 308 | struct resize_amount *amount) { |
349 | struct sway_container *con = config->handler_context.container; | 309 | struct sway_container *con = config->handler_context.container; |
350 | int grow_width = 0, grow_height = 0; | 310 | int grow_width = 0, grow_height = 0; |
351 | switch (axis) { | 311 | |
352 | case RESIZE_AXIS_HORIZONTAL: | 312 | if (is_horizontal(axis)) { |
353 | case RESIZE_AXIS_LEFT: | ||
354 | case RESIZE_AXIS_RIGHT: | ||
355 | grow_width = amount->amount; | 313 | grow_width = amount->amount; |
356 | break; | 314 | } else { |
357 | case RESIZE_AXIS_VERTICAL: | ||
358 | case RESIZE_AXIS_UP: | ||
359 | case RESIZE_AXIS_DOWN: | ||
360 | grow_height = amount->amount; | 315 | grow_height = amount->amount; |
361 | break; | ||
362 | case RESIZE_AXIS_INVALID: | ||
363 | return cmd_results_new(CMD_INVALID, "resize", "Invalid axis/direction"); | ||
364 | } | 316 | } |
317 | |||
365 | // Make sure we're not adjusting beyond floating min/max size | 318 | // Make sure we're not adjusting beyond floating min/max size |
366 | int min_width, max_width, min_height, max_height; | 319 | int min_width, max_width, min_height, max_height; |
367 | calculate_constraints(&min_width, &max_width, &min_height, &max_height); | 320 | calculate_constraints(&min_width, &max_width, &min_height, &max_height); |
@@ -376,24 +329,15 @@ static struct cmd_results *resize_adjust_floating(enum resize_axis axis, | |||
376 | grow_height = max_height - con->height; | 329 | grow_height = max_height - con->height; |
377 | } | 330 | } |
378 | int grow_x = 0, grow_y = 0; | 331 | int grow_x = 0, grow_y = 0; |
379 | switch (axis) { | 332 | |
380 | case RESIZE_AXIS_HORIZONTAL: | 333 | if (axis == AXIS_HORIZONTAL) { |
381 | grow_x = -grow_width / 2; | 334 | grow_x = -grow_width / 2; |
382 | break; | 335 | } else if (axis == AXIS_VERTICAL) { |
383 | case RESIZE_AXIS_VERTICAL: | ||
384 | grow_y = -grow_height / 2; | 336 | grow_y = -grow_height / 2; |
385 | break; | 337 | } else if (axis == WLR_EDGE_TOP) { |
386 | case RESIZE_AXIS_UP: | ||
387 | grow_y = -grow_height; | 338 | grow_y = -grow_height; |
388 | break; | 339 | } else if (axis == WLR_EDGE_LEFT) { |
389 | case RESIZE_AXIS_LEFT: | ||
390 | grow_x = -grow_width; | 340 | grow_x = -grow_width; |
391 | break; | ||
392 | case RESIZE_AXIS_DOWN: | ||
393 | case RESIZE_AXIS_RIGHT: | ||
394 | break; | ||
395 | case RESIZE_AXIS_INVALID: | ||
396 | return cmd_results_new(CMD_INVALID, "resize", "Invalid axis/direction"); | ||
397 | } | 341 | } |
398 | if (grow_x == 0 && grow_y == 0) { | 342 | if (grow_x == 0 && grow_y == 0) { |
399 | return cmd_results_new(CMD_INVALID, "resize", | 343 | return cmd_results_new(CMD_INVALID, "resize", |
@@ -417,7 +361,7 @@ static struct cmd_results *resize_adjust_floating(enum resize_axis axis, | |||
417 | /** | 361 | /** |
418 | * Implement `resize <grow|shrink>` for a tiled container. | 362 | * Implement `resize <grow|shrink>` for a tiled container. |
419 | */ | 363 | */ |
420 | static struct cmd_results *resize_adjust_tiled(enum resize_axis axis, | 364 | static struct cmd_results *resize_adjust_tiled(uint32_t axis, |
421 | struct resize_amount *amount) { | 365 | struct resize_amount *amount) { |
422 | struct sway_container *current = config->handler_context.container; | 366 | struct sway_container *current = config->handler_context.container; |
423 | 367 | ||
@@ -426,20 +370,11 @@ static struct cmd_results *resize_adjust_tiled(enum resize_axis axis, | |||
426 | } | 370 | } |
427 | if (amount->unit == RESIZE_UNIT_PPT) { | 371 | if (amount->unit == RESIZE_UNIT_PPT) { |
428 | float pct = amount->amount / 100.0f; | 372 | float pct = amount->amount / 100.0f; |
429 | switch (axis) { | 373 | |
430 | case RESIZE_AXIS_LEFT: | 374 | if (is_horizontal(axis)) { |
431 | case RESIZE_AXIS_RIGHT: | ||
432 | case RESIZE_AXIS_HORIZONTAL: | ||
433 | amount->amount = (float)current->width * pct; | 375 | amount->amount = (float)current->width * pct; |
434 | break; | 376 | } else { |
435 | case RESIZE_AXIS_UP: | ||
436 | case RESIZE_AXIS_DOWN: | ||
437 | case RESIZE_AXIS_VERTICAL: | ||
438 | amount->amount = (float)current->height * pct; | 377 | amount->amount = (float)current->height * pct; |
439 | break; | ||
440 | case RESIZE_AXIS_INVALID: | ||
441 | return cmd_results_new(CMD_INVALID, "resize", | ||
442 | "Invalid resize axis/direction"); | ||
443 | } | 378 | } |
444 | } | 379 | } |
445 | 380 | ||
@@ -474,8 +409,7 @@ static struct cmd_results *resize_set_tiled(struct sway_container *con, | |||
474 | width->unit = RESIZE_UNIT_PX; | 409 | width->unit = RESIZE_UNIT_PX; |
475 | } | 410 | } |
476 | if (width->unit == RESIZE_UNIT_PX) { | 411 | if (width->unit == RESIZE_UNIT_PX) { |
477 | resize_tiled(con, width->amount - con->width, | 412 | resize_tiled(con, width->amount - con->width, AXIS_HORIZONTAL); |
478 | RESIZE_AXIS_HORIZONTAL); | ||
479 | } | 413 | } |
480 | } | 414 | } |
481 | 415 | ||
@@ -495,8 +429,7 @@ static struct cmd_results *resize_set_tiled(struct sway_container *con, | |||
495 | height->unit = RESIZE_UNIT_PX; | 429 | height->unit = RESIZE_UNIT_PX; |
496 | } | 430 | } |
497 | if (height->unit == RESIZE_UNIT_PX) { | 431 | if (height->unit == RESIZE_UNIT_PX) { |
498 | resize_tiled(con, height->amount - con->height, | 432 | resize_tiled(con, height->amount - con->height, AXIS_VERTICAL); |
499 | RESIZE_AXIS_VERTICAL); | ||
500 | } | 433 | } |
501 | } | 434 | } |
502 | 435 | ||
@@ -631,8 +564,8 @@ static struct cmd_results *cmd_resize_adjust(int argc, char **argv, | |||
631 | int multiplier) { | 564 | int multiplier) { |
632 | const char *usage = "Expected 'resize grow|shrink <direction> " | 565 | const char *usage = "Expected 'resize grow|shrink <direction> " |
633 | "[<amount> px|ppt [or <amount> px|ppt]]'"; | 566 | "[<amount> px|ppt [or <amount> px|ppt]]'"; |
634 | enum resize_axis axis = parse_resize_axis(*argv); | 567 | uint32_t axis = parse_resize_axis(*argv); |
635 | if (axis == RESIZE_AXIS_INVALID) { | 568 | if (axis == WLR_EDGE_NONE) { |
636 | return cmd_results_new(CMD_INVALID, "resize", usage); | 569 | return cmd_results_new(CMD_INVALID, "resize", usage); |
637 | } | 570 | } |
638 | --argc; ++argv; | 571 | --argc; ++argv; |