From 0b521abe880474b86ae86e385ad67dfcdb52f785 Mon Sep 17 00:00:00 2001 From: Erik Reider <35975961+ErikReider@users.noreply.github.com> Date: Mon, 22 Jan 2024 18:13:05 +0100 Subject: [PATCH] Added workspace_gesture_wrap_around config option --- include/sway/commands.h | 1 + include/sway/config.h | 1 + sway/commands.c | 1 + sway/commands/workspace_gesture.c | 12 +++++++++ sway/config.c | 1 + sway/desktop/output.c | 44 ++++++++++++++++--------------- sway/desktop/render.c | 6 +++-- sway/sway.5.scd | 5 ++++ 8 files changed, 48 insertions(+), 23 deletions(-) diff --git a/include/sway/commands.h b/include/sway/commands.h index baa893ce..e7f90a3b 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h @@ -231,6 +231,7 @@ sway_cmd cmd_workspace; sway_cmd cmd_workspace_layout; sway_cmd cmd_ws_auto_back_and_forth; sway_cmd cmd_ws_gesture_spring_size; +sway_cmd cmd_ws_gesture_wrap_around; sway_cmd cmd_xwayland; sway_cmd bar_cmd_bindcode; diff --git a/include/sway/config.h b/include/sway/config.h index bc329df6..a82f2230 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -508,6 +508,7 @@ struct sway_config { bool scratchpad_minimize; int workspace_gesture_spring_size; + bool workspace_gesture_wrap_around; list_t *layer_criteria; diff --git a/sway/commands.c b/sway/commands.c index a968d4f0..02f338e6 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -123,6 +123,7 @@ static const struct cmd_handler handlers[] = { { "workspace", cmd_workspace }, { "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth }, { "workspace_gesture_spring_size", cmd_ws_gesture_spring_size }, + { "workspace_gesture_wrap_around", cmd_ws_gesture_wrap_around }, }; /* Config-time only commands. Keep alphabetized */ diff --git a/sway/commands/workspace_gesture.c b/sway/commands/workspace_gesture.c index ec0bfcd8..01b39545 100644 --- a/sway/commands/workspace_gesture.c +++ b/sway/commands/workspace_gesture.c @@ -1,6 +1,7 @@ #define _POSIX_C_SOURCE 200809L #include "sway/commands.h" #include "sway/config.h" +#include "util.h" struct cmd_results *cmd_ws_gesture_spring_size(int argc, char **argv) { struct cmd_results *error = NULL; @@ -18,3 +19,14 @@ struct cmd_results *cmd_ws_gesture_spring_size(int argc, char **argv) { return cmd_results_new(CMD_SUCCESS, NULL); } + +struct cmd_results *cmd_ws_gesture_wrap_around(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "workspace_gesture_wrap_around", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + config->workspace_gesture_wrap_around = parse_boolean(argv[0], true); + + return cmd_results_new(CMD_SUCCESS, NULL); +} diff --git a/sway/config.c b/sway/config.c index 1f9f0f70..d24a3b1e 100644 --- a/sway/config.c +++ b/sway/config.c @@ -369,6 +369,7 @@ static void config_defaults(struct sway_config *config) { config->scratchpad_minimize = false; config->workspace_gesture_spring_size = 50; + config->workspace_gesture_wrap_around = false; if (!(config->layer_criteria = create_list())) goto cleanup; diff --git a/sway/desktop/output.c b/sway/desktop/output.c index 4ed91f96..11ecd594 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -33,6 +33,9 @@ #include "sway/tree/workspace.h" #include "util.h" +#define PREV_WS_LIMIT -1.0f +#define NEXT_WS_LIMIT 1.0f + struct sway_output *output_by_name_or_id(const char *name_or_id) { for (int i = 0; i < root->outputs->length; ++i) { struct sway_output *output = root->outputs->items[i]; @@ -1130,26 +1133,25 @@ void update_workspace_scroll_percent(struct sway_seat *seat, int gesture_percent return; } - // TODO: Make the threshold configurable?? - const float THRESHOLD = MAX(0.35 - 0.1, 0); + float min = PREV_WS_LIMIT, max = NEXT_WS_LIMIT; + if (!config->workspace_gesture_wrap_around) { + // TODO: Make the threshold configurable?? + const float THRESHOLD = MAX(0.35 - 0.1, 0); - // TODO: Make configurable? - // Visualized to the user that this is the last / first workspace by - // allowing a small swipe, a "Spring effect" - float spring_limit = (float) config->workspace_gesture_spring_size / - output->width * output->wlr_output->scale; - // Make sure that the limit is always smaller than the threshold - spring_limit = MIN(THRESHOLD, spring_limit); - // Limit the percent depending on if the workspace is the first/last or in - // the middle somewhere. - float min = -1.0f, max = 1.0f; - if (visible_index + 1 >= output->workspaces->length) { - // NOTE: Can be adjusted in the future to wrap around workspaces - max = spring_limit; - } - if (visible_index == 0) { - // NOTE: Can be adjusted in the future to wrap around workspaces - min = -spring_limit; + // Visualized to the user that this is the last / first workspace by + // allowing a small swipe, a "Spring effect" + float spring_limit = (float) config->workspace_gesture_spring_size / + output->width * output->wlr_output->scale; + // Make sure that the limit is always smaller than the threshold + spring_limit = MIN(THRESHOLD, spring_limit); + // Limit the percent depending on if the workspace is the first/last or in + // the middle somewhere. + if (visible_index + 1 >= output->workspaces->length) { + max = spring_limit; + } + if (visible_index == 0) { + min = -spring_limit; + } } output->workspace_scroll.percent = MIN(max, MAX(min, percent)); output->workspace_scroll.direction = direction; @@ -1170,9 +1172,9 @@ void snap_workspace_scroll_percent(struct sway_seat *seat) { int dir = 0; if (output->workspace_scroll.percent < 0) { - dir = -1; + dir = PREV_WS_LIMIT; } else if (output->workspace_scroll.percent > 0) { - dir = 1; + dir = NEXT_WS_LIMIT; } else { // Skip setting workspace if the percentage is zero goto reset_state; diff --git a/sway/desktop/render.c b/sway/desktop/render.c index 84912a0f..e964ceba 100644 --- a/sway/desktop/render.c +++ b/sway/desktop/render.c @@ -2058,9 +2058,11 @@ void output_render(struct sway_output *output, struct timespec *when, // Get the sibling workspaces struct sway_workspace *other_ws = NULL; if (output->workspace_scroll.percent < 0) { - other_ws = workspace_output_prev_wrap(workspace, false); + other_ws = workspace_output_prev_wrap(workspace, + config->workspace_gesture_wrap_around); } else { - other_ws = workspace_output_next_wrap(workspace, false); + other_ws = workspace_output_next_wrap(workspace, + config->workspace_gesture_wrap_around); } struct sway_container *fullscreen_con = root->fullscreen_global; diff --git a/sway/sway.5.scd b/sway/sway.5.scd index 7f448fec..0e4b55f8 100644 --- a/sway/sway.5.scd +++ b/sway/sway.5.scd @@ -592,6 +592,11 @@ runtime. Adjusts the workspace gestures spring size. Can use values between 0 (disabled) and 100 while 50 is the default value. +*workspace_gesture_wrap_around* + Sets whether or not the workspace gesture should wrap around when trying to + swipe past the first/last workspace. Disables the + _workspace_gesture_spring_size_ config option. Disabled by default. + *client.background* This command is ignored and is only present for i3 compatibility.