diff options
author | Ryan Dwyer <ryandwyer1@gmail.com> | 2018-07-11 21:30:43 +1000 |
---|---|---|
committer | Ryan Dwyer <ryandwyer1@gmail.com> | 2018-07-14 10:00:39 +1000 |
commit | 558ca9fc284738d15f8f73acb7fbd347ee30aeff (patch) | |
tree | ef96fd1ebb23008042d93f72cf07d48d9e446269 /sway/commands/resize.c | |
parent | Merge pull request #2269 from minus7/swaybarbg-crash-on-dpms-resume (diff) | |
download | sway-558ca9fc284738d15f8f73acb7fbd347ee30aeff.tar.gz sway-558ca9fc284738d15f8f73acb7fbd347ee30aeff.tar.zst sway-558ca9fc284738d15f8f73acb7fbd347ee30aeff.zip |
Implement resize command for floating views
Implements the following for floating views:
* resize set <width> <height>
* resize <grow|shrink> <width|height|up|down|left|right> <amount>
Diffstat (limited to 'sway/commands/resize.c')
-rw-r--r-- | sway/commands/resize.c | 137 |
1 files changed, 129 insertions, 8 deletions
diff --git a/sway/commands/resize.c b/sway/commands/resize.c index 5efbd8b0..afb369c2 100644 --- a/sway/commands/resize.c +++ b/sway/commands/resize.c | |||
@@ -7,6 +7,7 @@ | |||
7 | #include <wlr/util/log.h> | 7 | #include <wlr/util/log.h> |
8 | #include "sway/commands.h" | 8 | #include "sway/commands.h" |
9 | #include "sway/tree/arrange.h" | 9 | #include "sway/tree/arrange.h" |
10 | #include "sway/tree/view.h" | ||
10 | #include "log.h" | 11 | #include "log.h" |
11 | 12 | ||
12 | static const int MIN_SANE_W = 100, MIN_SANE_H = 60; | 13 | static const int MIN_SANE_W = 100, MIN_SANE_H = 60; |
@@ -21,6 +22,10 @@ enum resize_unit { | |||
21 | enum resize_axis { | 22 | enum resize_axis { |
22 | RESIZE_AXIS_HORIZONTAL, | 23 | RESIZE_AXIS_HORIZONTAL, |
23 | RESIZE_AXIS_VERTICAL, | 24 | RESIZE_AXIS_VERTICAL, |
25 | RESIZE_AXIS_UP, | ||
26 | RESIZE_AXIS_DOWN, | ||
27 | RESIZE_AXIS_LEFT, | ||
28 | RESIZE_AXIS_RIGHT, | ||
24 | RESIZE_AXIS_INVALID, | 29 | RESIZE_AXIS_INVALID, |
25 | }; | 30 | }; |
26 | 31 | ||
@@ -44,6 +49,18 @@ static enum resize_axis parse_resize_axis(const char *axis) { | |||
44 | if (strcasecmp(axis, "height") == 0 || strcasecmp(axis, "vertical") == 0) { | 49 | if (strcasecmp(axis, "height") == 0 || strcasecmp(axis, "vertical") == 0) { |
45 | return RESIZE_AXIS_VERTICAL; | 50 | return RESIZE_AXIS_VERTICAL; |
46 | } | 51 | } |
52 | if (strcasecmp(axis, "up") == 0) { | ||
53 | return RESIZE_AXIS_UP; | ||
54 | } | ||
55 | if (strcasecmp(axis, "down") == 0) { | ||
56 | return RESIZE_AXIS_DOWN; | ||
57 | } | ||
58 | if (strcasecmp(axis, "left") == 0) { | ||
59 | return RESIZE_AXIS_LEFT; | ||
60 | } | ||
61 | if (strcasecmp(axis, "right") == 0) { | ||
62 | return RESIZE_AXIS_RIGHT; | ||
63 | } | ||
47 | return RESIZE_AXIS_INVALID; | 64 | return RESIZE_AXIS_INVALID; |
48 | } | 65 | } |
49 | 66 | ||
@@ -185,13 +202,62 @@ static void resize_tiled(int amount, enum resize_axis axis) { | |||
185 | arrange_and_commit(parent->parent); | 202 | arrange_and_commit(parent->parent); |
186 | } | 203 | } |
187 | 204 | ||
205 | static void resize_floating(int amount, enum resize_axis axis) { | ||
206 | struct sway_container *con = config->handler_context.current_container; | ||
207 | int grow_x = 0, grow_y = 0; | ||
208 | int grow_width = 0, grow_height = 0; | ||
209 | switch (axis) { | ||
210 | case RESIZE_AXIS_HORIZONTAL: | ||
211 | grow_x = -amount / 2; | ||
212 | grow_width = amount; | ||
213 | break; | ||
214 | case RESIZE_AXIS_VERTICAL: | ||
215 | grow_y = -amount / 2; | ||
216 | grow_height = amount; | ||
217 | break; | ||
218 | case RESIZE_AXIS_UP: | ||
219 | grow_y = -amount; | ||
220 | grow_height = amount; | ||
221 | break; | ||
222 | case RESIZE_AXIS_LEFT: | ||
223 | grow_x = -amount; | ||
224 | grow_width = amount; | ||
225 | break; | ||
226 | case RESIZE_AXIS_DOWN: | ||
227 | grow_height = amount; | ||
228 | break; | ||
229 | case RESIZE_AXIS_RIGHT: | ||
230 | grow_width = amount; | ||
231 | break; | ||
232 | case RESIZE_AXIS_INVALID: | ||
233 | sway_assert(false, "Didn't expect RESIZE_AXIS_INVALID"); | ||
234 | return; | ||
235 | } | ||
236 | con->x += grow_x; | ||
237 | con->y += grow_y; | ||
238 | con->width += grow_width; | ||
239 | con->height += grow_height; | ||
240 | |||
241 | if (con->type == C_VIEW) { | ||
242 | struct sway_view *view = con->sway_view; | ||
243 | view->x += grow_x; | ||
244 | view->y += grow_y; | ||
245 | view->width += grow_width; | ||
246 | view->height += grow_height; | ||
247 | } | ||
248 | |||
249 | arrange_and_commit(con); | ||
250 | } | ||
251 | |||
188 | static void resize(int amount, enum resize_axis axis, enum resize_unit unit) { | 252 | static void resize(int amount, enum resize_axis axis, enum resize_unit unit) { |
189 | struct sway_container *current = config->handler_context.current_container; | 253 | struct sway_container *current = config->handler_context.current_container; |
254 | if (container_is_floating(current)) { | ||
255 | return resize_floating(amount, axis); | ||
256 | } | ||
257 | |||
190 | if (unit == RESIZE_UNIT_DEFAULT) { | 258 | if (unit == RESIZE_UNIT_DEFAULT) { |
191 | // Default for tiling; TODO floating should be px | ||
192 | unit = RESIZE_UNIT_PPT; | 259 | unit = RESIZE_UNIT_PPT; |
193 | } | 260 | } |
194 | |||
195 | if (unit == RESIZE_UNIT_PPT) { | 261 | if (unit == RESIZE_UNIT_PPT) { |
196 | float pct = amount / 100.0f; | 262 | float pct = amount / 100.0f; |
197 | switch (axis) { | 263 | switch (axis) { |
@@ -210,6 +276,65 @@ static void resize(int amount, enum resize_axis axis, enum resize_unit unit) { | |||
210 | return resize_tiled(amount, axis); | 276 | return resize_tiled(amount, axis); |
211 | } | 277 | } |
212 | 278 | ||
279 | // resize set <width> [px|ppt] <height> [px|ppt] | ||
280 | static struct cmd_results *cmd_resize_set(int argc, char **argv) { | ||
281 | struct cmd_results *error; | ||
282 | if ((error = checkarg(argc, "resize", EXPECTED_AT_LEAST, 2))) { | ||
283 | return error; | ||
284 | } | ||
285 | struct sway_container *con = config->handler_context.current_container; | ||
286 | if (!container_is_floating(con)) { | ||
287 | return cmd_results_new(CMD_INVALID, "resize", | ||
288 | "resize set is not currently supported for tiled containers"); | ||
289 | } | ||
290 | const char *usage = "Expected 'resize set <width> <height>'"; | ||
291 | |||
292 | char *err; | ||
293 | size_t width = (int)strtol(*argv, &err, 10); | ||
294 | if (*err) { | ||
295 | // e.g. `resize set width 500px` | ||
296 | enum resize_unit unit = parse_resize_unit(err); | ||
297 | if (unit == RESIZE_UNIT_INVALID) { | ||
298 | return cmd_results_new(CMD_INVALID, "resize", usage); | ||
299 | } | ||
300 | } | ||
301 | --argc; ++argv; | ||
302 | if (parse_resize_unit(argv[0]) != RESIZE_UNIT_INVALID) { | ||
303 | --argc; ++argv; | ||
304 | } | ||
305 | |||
306 | if (!argc) { | ||
307 | return cmd_results_new(CMD_INVALID, "resize", usage); | ||
308 | } | ||
309 | size_t height = (int)strtol(*argv, &err, 10); | ||
310 | if (*err) { | ||
311 | // e.g. `resize set height 500px` | ||
312 | enum resize_unit unit = parse_resize_unit(err); | ||
313 | if (unit == RESIZE_UNIT_INVALID) { | ||
314 | return cmd_results_new(CMD_INVALID, "resize", usage); | ||
315 | } | ||
316 | } | ||
317 | |||
318 | int grow_width = width - con->width; | ||
319 | int grow_height = height - con->height; | ||
320 | con->x -= grow_width / 2; | ||
321 | con->y -= grow_height / 2; | ||
322 | con->width = width; | ||
323 | con->height = height; | ||
324 | |||
325 | if (con->type == C_VIEW) { | ||
326 | struct sway_view *view = con->sway_view; | ||
327 | view->x -= grow_width / 2; | ||
328 | view->y -= grow_height / 2; | ||
329 | view->width += grow_width; | ||
330 | view->height += grow_height; | ||
331 | } | ||
332 | |||
333 | arrange_and_commit(con); | ||
334 | |||
335 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
336 | } | ||
337 | |||
213 | struct cmd_results *cmd_resize(int argc, char **argv) { | 338 | struct cmd_results *cmd_resize(int argc, char **argv) { |
214 | struct sway_container *current = config->handler_context.current_container; | 339 | struct sway_container *current = config->handler_context.current_container; |
215 | if (!current) { | 340 | if (!current) { |
@@ -226,15 +351,11 @@ struct cmd_results *cmd_resize(int argc, char **argv) { | |||
226 | } | 351 | } |
227 | 352 | ||
228 | if (strcasecmp(argv[0], "set") == 0) { | 353 | if (strcasecmp(argv[0], "set") == 0) { |
229 | // TODO | 354 | return cmd_resize_set(argc - 1, &argv[1]); |
230 | //return cmd_resize_set(argc - 1, &argv[1]); | ||
231 | return cmd_results_new(CMD_INVALID, "resize", "resize set unimplemented"); | ||
232 | } | 355 | } |
233 | 356 | ||
234 | // TODO: resize grow|shrink left|right|up|down | ||
235 | |||
236 | const char *usage = "Expected 'resize <shrink|grow> " | 357 | const char *usage = "Expected 'resize <shrink|grow> " |
237 | "<width|height> [<amount>] [px|ppt]'"; | 358 | "<width|height|up|down|left|right> [<amount>] [px|ppt]'"; |
238 | 359 | ||
239 | int multiplier = 0; | 360 | int multiplier = 0; |
240 | if (strcasecmp(*argv, "grow") == 0) { | 361 | if (strcasecmp(*argv, "grow") == 0) { |