From 51d230ad4ca1bc4202ffb1b7e9157944542e3596 Mon Sep 17 00:00:00 2001 From: Orestis Floros Date: Fri, 24 Aug 2018 03:06:52 +0300 Subject: [PATCH] Make resize set px work with tiling containers --- docs/userguide | 5 +- src/commands.c | 78 +++++++++++++---------------- testcases/t/541-resize-set-tiling.t | 33 ++++++++++++ 3 files changed, 70 insertions(+), 46 deletions(-) diff --git a/docs/userguide b/docs/userguide index 1c85feb6..9c601e88 100644 --- a/docs/userguide +++ b/docs/userguide @@ -2337,9 +2337,8 @@ optional ppt argument means "percentage points", and if specified it indicates that a *tiling container* should be grown or shrunk by that many points, instead of by the +px+ value. -Notes about +resize set+: a value of 0 for or means "do -not resize in this direction", and resizing a tiling container by +px+ is not -implemented. +Note about +resize set+: a value of 0 for or means "do not +resize in this direction". It is recommended to define bindings for resizing in a dedicated binding mode. See <> and the example in the i3 diff --git a/src/commands.c b/src/commands.c index 9686934f..1b05d672 100644 --- a/src/commands.c +++ b/src/commands.c @@ -629,6 +629,35 @@ void cmd_resize(I3_CMD, const char *way, const char *direction, long resize_px, ysuccess(true); } +static bool resize_set_tiling(I3_CMD, Con *target, orientation_t resize_orientation, bool is_ppt, long target_size) { + direction_t search_direction; + char *mode; + if (resize_orientation == HORIZ) { + search_direction = D_LEFT; + mode = "width"; + } else { + search_direction = D_DOWN; + mode = "height"; + } + + /* Get the appropriate current container (skip stacked/tabbed cons) */ + Con *dummy; + resize_find_tiling_participants(&target, &dummy, search_direction, true); + + /* Calculate new size for the target container */ + int ppt = 0; + int px = 0; + if (is_ppt) { + ppt = target_size - target->percent * 100; + } else { + px = target_size - (resize_orientation == HORIZ ? target->rect.width : target->rect.height); + } + + /* Perform resizing and report failure if not possible */ + return cmd_resize_tiling_width_height(current_match, cmd_output, + target, mode, px, ppt); +} + /* * Implementation of 'resize set [px | ppt] [px | ppt]'. * @@ -665,50 +694,13 @@ void cmd_resize_set(I3_CMD, long cwidth, const char *mode_width, long cheight, c continue; } - if (cwidth > 0 && mode_width && strcmp(mode_width, "ppt") == 0) { - /* get the appropriate current container (skip stacked/tabbed cons) */ - Con *target = current->con; - Con *dummy; - resize_find_tiling_participants(&target, &dummy, D_LEFT, true); - - /* Calculate new size for the target container */ - double current_percent = target->percent; - long adjustment; - - if (current_percent > cwidth) { - adjustment = (int)(current_percent * 100) - cwidth; - } else { - adjustment = cwidth - (int)(current_percent * 100); - } - - /* perform resizing and report failure if not possible */ - if (!cmd_resize_tiling_width_height(current_match, cmd_output, - target, "width", 0, adjustment)) { - success = false; - } + if (cwidth > 0 && mode_width) { + success &= resize_set_tiling(current_match, cmd_output, current->con, + HORIZ, strcmp(mode_width, "ppt") == 0, cwidth); } - - if (cheight > 0 && mode_height && strcmp(mode_height, "ppt") == 0) { - /* get the appropriate current container (skip stacked/tabbed cons) */ - Con *target = current->con; - Con *dummy; - resize_find_tiling_participants(&target, &dummy, D_DOWN, true); - - /* Calculate new size for the target container */ - double current_percent = target->percent; - long adjustment; - - if (current_percent > cheight) { - adjustment = (int)(current_percent * 100) - cheight; - } else { - adjustment = cheight - (int)(current_percent * 100); - } - - /* perform resizing and report failure if not possible */ - if (!cmd_resize_tiling_width_height(current_match, cmd_output, - target, "height", 0, adjustment)) { - success = false; - } + if (cheight > 0 && mode_height) { + success &= resize_set_tiling(current_match, cmd_output, current->con, + VERT, strcmp(mode_height, "ppt") == 0, cheight); } } } diff --git a/testcases/t/541-resize-set-tiling.t b/testcases/t/541-resize-set-tiling.t index 659fcc67..956622a2 100644 --- a/testcases/t/541-resize-set-tiling.t +++ b/testcases/t/541-resize-set-tiling.t @@ -47,6 +47,13 @@ cmd 'resize set width 80 ppt'; cmp_float($nodes->[0]->{percent}, 0.20, 'left window got 20%'); cmp_float($nodes->[1]->{percent}, 0.80, 'right window got 80%'); +# Same but with px. +cmd 'resize set width 200 px'; + +($nodes, $focus) = get_ws_content($tmp); + +cmp_float($nodes->[1]->{rect}->{width}, 200, 'right window got 200 px'); + ############################################################ # resize vertically ############################################################ @@ -77,6 +84,13 @@ cmd 'resize set height 80 ppt'; cmp_float($nodes->[0]->{percent}, 0.20, 'top window got 20%'); cmp_float($nodes->[1]->{percent}, 0.80, 'bottom window got 80%'); +# Same but with px. +cmd 'resize set height 200 px'; + +($nodes, $focus) = get_ws_content($tmp); + +cmp_float($nodes->[1]->{rect}->{height}, 200, 'bottom window got 200 px'); + ############################################################ # resize horizontally and vertically ############################################################ @@ -102,6 +116,13 @@ cmp_float($nodes->[1]->{percent}, 0.75, 'right container got 75%'); cmp_float($nodes->[1]->{nodes}->[0]->{percent}, 0.25, 'top-right window got 25%'); cmp_float($nodes->[1]->{nodes}->[1]->{percent}, 0.75, 'bottom-right window got 75%'); +# Same but with px. +cmd 'resize set 155 px 135 px'; + +($nodes, $focus) = get_ws_content($tmp); + +cmp_float($nodes->[1]->{nodes}->[1]->{rect}->{width}, 155, 'bottom-right window got 155 px width'); +cmp_float($nodes->[1]->{nodes}->[1]->{rect}->{height}, 135, 'bottom-right window got 135 px height'); ############################################################ # resize from inside a tabbed container @@ -130,6 +151,12 @@ cmd 'resize set 75 ppt 0 ppt'; cmp_float($nodes->[0]->{percent}, 0.25, 'left container got 25%'); cmp_float($nodes->[1]->{percent}, 0.75, 'right container got 75%'); +# Same but with px. +cmd 'resize set 155 px'; + +($nodes, $focus) = get_ws_content($tmp); + +cmp_float($nodes->[1]->{rect}->{width}, 155, 'right container got 155 px'); ############################################################ # resize from inside a stacked container @@ -158,5 +185,11 @@ cmd 'resize set 75 ppt 0 ppt'; cmp_float($nodes->[0]->{percent}, 0.25, 'left container got 25%'); cmp_float($nodes->[1]->{percent}, 0.75, 'right container got 75%'); +# Same but with px. +cmd 'resize set 130 px'; + +($nodes, $focus) = get_ws_content($tmp); + +cmp_float($nodes->[1]->{rect}->{width}, 130, 'right container got 130 px'); done_testing; -- 2.39.5