aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands/resize.c
diff options
context:
space:
mode:
authorLibravatar Ryan Dwyer <ryandwyer1@gmail.com>2018-07-14 10:00:22 +1000
committerLibravatar Ryan Dwyer <ryandwyer1@gmail.com>2018-07-14 10:00:39 +1000
commit0584ecec0ac40c0fbeb13375379fe0e5936541f3 (patch)
tree74ca43b11fd43471ffb9f3d51ca1c465e604bc4b /sway/commands/resize.c
parentImplement resize grow|shrink <direction> <amount> or <amount> (diff)
downloadsway-0584ecec0ac40c0fbeb13375379fe0e5936541f3.tar.gz
sway-0584ecec0ac40c0fbeb13375379fe0e5936541f3.tar.zst
sway-0584ecec0ac40c0fbeb13375379fe0e5936541f3.zip
Force min/max size when resizing floating containers
Diffstat (limited to 'sway/commands/resize.c')
-rw-r--r--sway/commands/resize.c84
1 files changed, 74 insertions, 10 deletions
diff --git a/sway/commands/resize.c b/sway/commands/resize.c
index 3f243cf7..2cf811d8 100644
--- a/sway/commands/resize.c
+++ b/sway/commands/resize.c
@@ -1,4 +1,5 @@
1#include <errno.h> 1#include <errno.h>
2#include <limits.h>
2#include <math.h> 3#include <math.h>
3#include <stdbool.h> 4#include <stdbool.h>
4#include <stdlib.h> 5#include <stdlib.h>
@@ -71,6 +72,45 @@ static int parse_resize_amount(int argc, char **argv,
71 return 2; 72 return 2;
72} 73}
73 74
75static void calculate_constraints(int *min_width, int *max_width,
76 int *min_height, int *max_height) {
77 struct sway_container *con = config->handler_context.current_container;
78
79 if (config->floating_minimum_width == -1) { // no minimum
80 *min_width = 0;
81 } else if (config->floating_minimum_width == 0) { // automatic
82 *min_width = 75;
83 } else {
84 *min_width = config->floating_minimum_width;
85 }
86
87 if (config->floating_minimum_height == -1) { // no minimum
88 *min_height = 0;
89 } else if (config->floating_minimum_height == 0) { // automatic
90 *min_height = 50;
91 } else {
92 *min_height = config->floating_minimum_height;
93 }
94
95 if (config->floating_maximum_width == -1) { // no maximum
96 *max_width = INT_MAX;
97 } else if (config->floating_maximum_width == 0) { // automatic
98 struct sway_container *ws = container_parent(con, C_WORKSPACE);
99 *max_width = ws->width;
100 } else {
101 *max_width = config->floating_maximum_width;
102 }
103
104 if (config->floating_maximum_height == -1) { // no maximum
105 *max_height = INT_MAX;
106 } else if (config->floating_maximum_height == 0) { // automatic
107 struct sway_container *ws = container_parent(con, C_WORKSPACE);
108 *max_height = ws->height;
109 } else {
110 *max_height = config->floating_maximum_height;
111 }
112}
113
74static enum resize_axis parse_resize_axis(const char *axis) { 114static enum resize_axis parse_resize_axis(const char *axis) {
75 if (strcasecmp(axis, "width") == 0 || strcasecmp(axis, "horizontal") == 0) { 115 if (strcasecmp(axis, "width") == 0 || strcasecmp(axis, "horizontal") == 0) {
76 return RESIZE_AXIS_HORIZONTAL; 116 return RESIZE_AXIS_HORIZONTAL;
@@ -237,30 +277,50 @@ static void resize_tiled(int amount, enum resize_axis axis) {
237static struct cmd_results *resize_adjust_floating(enum resize_axis axis, 277static struct cmd_results *resize_adjust_floating(enum resize_axis axis,
238 struct resize_amount *amount) { 278 struct resize_amount *amount) {
239 struct sway_container *con = config->handler_context.current_container; 279 struct sway_container *con = config->handler_context.current_container;
240 int grow_x = 0, grow_y = 0;
241 int grow_width = 0, grow_height = 0; 280 int grow_width = 0, grow_height = 0;
242 switch (axis) { 281 switch (axis) {
243 case RESIZE_AXIS_HORIZONTAL: 282 case RESIZE_AXIS_HORIZONTAL:
244 grow_x = -amount->amount / 2; 283 case RESIZE_AXIS_LEFT:
284 case RESIZE_AXIS_RIGHT:
245 grow_width = amount->amount; 285 grow_width = amount->amount;
246 break; 286 break;
247 case RESIZE_AXIS_VERTICAL: 287 case RESIZE_AXIS_VERTICAL:
248 grow_y = -amount->amount / 2; 288 case RESIZE_AXIS_UP:
289 case RESIZE_AXIS_DOWN:
249 grow_height = amount->amount; 290 grow_height = amount->amount;
250 break; 291 break;
292 case RESIZE_AXIS_INVALID:
293 return cmd_results_new(CMD_INVALID, "resize", "Invalid axis/direction");
294 }
295 // Make sure we're not adjusting beyond floating min/max size
296 int min_width, max_width, min_height, max_height;
297 calculate_constraints(&min_width, &max_width, &min_height, &max_height);
298 if (con->width + grow_width < min_width) {
299 grow_width = min_width - con->width;
300 } else if (con->width + grow_width > max_width) {
301 grow_width = max_width - con->width;
302 }
303 if (con->height + grow_height < min_height) {
304 grow_height = min_height - con->height;
305 } else if (con->height + grow_height > max_height) {
306 grow_height = max_height - con->height;
307 }
308 int grow_x = 0, grow_y = 0;
309 switch (axis) {
310 case RESIZE_AXIS_HORIZONTAL:
311 grow_x = -grow_width / 2;
312 break;
313 case RESIZE_AXIS_VERTICAL:
314 grow_y = -grow_height / 2;
315 break;
251 case RESIZE_AXIS_UP: 316 case RESIZE_AXIS_UP:
252 grow_y = -amount->amount; 317 grow_y = -grow_height;
253 grow_height = amount->amount;
254 break; 318 break;
255 case RESIZE_AXIS_LEFT: 319 case RESIZE_AXIS_LEFT:
256 grow_x = -amount->amount; 320 grow_x = -grow_width;
257 grow_width = amount->amount;
258 break; 321 break;
259 case RESIZE_AXIS_DOWN: 322 case RESIZE_AXIS_DOWN:
260 grow_height = amount->amount;
261 break;
262 case RESIZE_AXIS_RIGHT: 323 case RESIZE_AXIS_RIGHT:
263 grow_width = amount->amount;
264 break; 324 break;
265 case RESIZE_AXIS_INVALID: 325 case RESIZE_AXIS_INVALID:
266 return cmd_results_new(CMD_INVALID, "resize", "Invalid axis/direction"); 326 return cmd_results_new(CMD_INVALID, "resize", "Invalid axis/direction");
@@ -331,6 +391,10 @@ static struct cmd_results *resize_set_tiled(struct sway_container *con,
331 */ 391 */
332static struct cmd_results *resize_set_floating(struct sway_container *con, 392static struct cmd_results *resize_set_floating(struct sway_container *con,
333 struct resize_amount *width, struct resize_amount *height) { 393 struct resize_amount *width, struct resize_amount *height) {
394 int min_width, max_width, min_height, max_height;
395 calculate_constraints(&min_width, &max_width, &min_height, &max_height);
396 width->amount = fmax(min_width, fmin(width->amount, max_width));
397 height->amount = fmax(min_height, fmin(height->amount, max_height));
334 int grow_width = width->amount - con->width; 398 int grow_width = width->amount - con->width;
335 int grow_height = height->amount - con->height; 399 int grow_height = height->amount - con->height;
336 con->x -= grow_width / 2; 400 con->x -= grow_width / 2;