From cfa403fc58f3df9cdef2bc03d0a8bbdd4333ed00 Mon Sep 17 00:00:00 2001 From: Thayne McCombs Date: Tue, 21 Jan 2020 01:32:22 -0700 Subject: [PATCH] Keep windows in bounds on move to position mouse If the mouse/cursor/pointer is near the edge of an output when a "move position to pointer" command is run, then the floating container will be constrained to fit inside the bounds of the output as much as possible. This behavior matches what i3 does in this scenario. I also think it is a better user experience. Relates to #4906 The logic for the bounds check follows the implementation in i3: https://github.com/i3/i3/blob/733077822302d8b77eacb606a26fd002a42f534f/src/floating.c#L536 --- sway/commands/move.c | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/sway/commands/move.c b/sway/commands/move.c index 8111a07c..23d392f9 100644 --- a/sway/commands/move.c +++ b/sway/commands/move.c @@ -1,5 +1,6 @@ #define _POSIX_C_SOURCE 200809L #include +#include #include #include #include @@ -753,6 +754,39 @@ static struct cmd_results *cmd_move_in_direction( return cmd_results_new(CMD_SUCCESS, NULL); } +static struct cmd_results *cmd_move_to_position_pointer( + struct sway_container *container) { + struct sway_seat *seat = config->handler_context.seat; + if (!seat->cursor) { + return cmd_results_new(CMD_FAILURE, "No cursor device"); + } + struct wlr_cursor *cursor = seat->cursor->cursor; + /* Determine where to put the window. */ + double lx = cursor->x - container->width / 2; + double ly = cursor->y - container->height / 2; + + /* Correct target coordinates to be in bounds (on screen). */ + for (int i = 0; i < root->outputs->length; ++i) { + struct wlr_box box; + output_get_box(root->outputs->items[i], &box); + if (wlr_box_contains_point(&box, cursor->x, cursor->y)) { + lx = fmax(lx, box.x); + ly = fmax(ly, box.y); + if (lx + container->width > box.x + box.width) { + lx = box.x + box.width - container->width; + } + if (ly + container->height > box.y + box.height) { + ly = box.y + box.height - container->height; + } + break; + } + } + + /* Actually move the container. */ + container_floating_move_to(container, lx, ly); + return cmd_results_new(CMD_SUCCESS, NULL); +} + static const char expected_position_syntax[] = "Expected 'move [absolute] position [px] [px]' or " "'move [absolute] position center' or " @@ -790,14 +824,7 @@ static struct cmd_results *cmd_move_to_position(int argc, char **argv) { if (absolute) { return cmd_results_new(CMD_INVALID, expected_position_syntax); } - struct sway_seat *seat = config->handler_context.seat; - if (!seat->cursor) { - return cmd_results_new(CMD_FAILURE, "No cursor device"); - } - double lx = seat->cursor->cursor->x - container->width / 2; - double ly = seat->cursor->cursor->y - container->height / 2; - container_floating_move_to(container, lx, ly); - return cmd_results_new(CMD_SUCCESS, NULL); + return cmd_move_to_position_pointer(container); } else if (strcmp(argv[0], "center") == 0) { double lx, ly; if (absolute) {