From 1113e5653aff8d769150bc9412b90934f05ebb2b Mon Sep 17 00:00:00 2001 From: Erik Reider <35975961+ErikReider@users.noreply.github.com> Date: Mon, 22 Jan 2024 18:25:00 +0100 Subject: [PATCH] Added workspace_gesture_threshold config option --- include/sway/commands.h | 1 + include/sway/config.h | 1 + sway/commands.c | 1 + sway/commands/workspace_gesture.c | 17 +++++++++++++++++ sway/config.c | 1 + sway/desktop/output.c | 13 +++++-------- sway/sway.5.scd | 5 +++++ 7 files changed, 31 insertions(+), 8 deletions(-) diff --git a/include/sway/commands.h b/include/sway/commands.h index e7f90a3b..c94e1ebc 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_threshold; sway_cmd cmd_ws_gesture_wrap_around; sway_cmd cmd_xwayland; diff --git a/include/sway/config.h b/include/sway/config.h index a82f2230..ed33d400 100644 --- a/include/sway/config.h +++ b/include/sway/config.h @@ -509,6 +509,7 @@ struct sway_config { int workspace_gesture_spring_size; bool workspace_gesture_wrap_around; + float workspace_gesture_threshold; list_t *layer_criteria; diff --git a/sway/commands.c b/sway/commands.c index 02f338e6..bab1b95d 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_threshold", cmd_ws_gesture_threshold }, { "workspace_gesture_wrap_around", cmd_ws_gesture_wrap_around }, }; diff --git a/sway/commands/workspace_gesture.c b/sway/commands/workspace_gesture.c index 01b39545..6c007066 100644 --- a/sway/commands/workspace_gesture.c +++ b/sway/commands/workspace_gesture.c @@ -30,3 +30,20 @@ struct cmd_results *cmd_ws_gesture_wrap_around(int argc, char **argv) { return cmd_results_new(CMD_SUCCESS, NULL); } + +struct cmd_results *cmd_ws_gesture_threshold(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "workspace_gesture_threshold", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + char *err; + float val = strtof(argv[0], &err); + if (*err || val < 0.1f || val > 0.9f) { + return cmd_results_new(CMD_INVALID, "workspace_gesture_threshold float invalid. " + "Should be between 0.1 and 0.9"); + } + config->workspace_gesture_threshold = val; + + return cmd_results_new(CMD_SUCCESS, NULL); +} diff --git a/sway/config.c b/sway/config.c index d24a3b1e..87219032 100644 --- a/sway/config.c +++ b/sway/config.c @@ -370,6 +370,7 @@ static void config_defaults(struct sway_config *config) { config->workspace_gesture_spring_size = 50; config->workspace_gesture_wrap_around = false; + config->workspace_gesture_threshold = 0.35; if (!(config->layer_criteria = create_list())) goto cleanup; diff --git a/sway/desktop/output.c b/sway/desktop/output.c index 11ecd594..7b64487d 100644 --- a/sway/desktop/output.c +++ b/sway/desktop/output.c @@ -1135,15 +1135,14 @@ void update_workspace_scroll_percent(struct sway_seat *seat, int gesture_percent 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); - // 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); + // Make sure that the limit is always smaller than the threshold to + // avoid accidental workspace switches + float small_threshold = MAX(config->workspace_gesture_threshold - 0.1, 0); + spring_limit = MIN(small_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) { @@ -1164,9 +1163,7 @@ void snap_workspace_scroll_percent(struct sway_seat *seat) { struct sway_workspace *focused_ws = seat_get_focused_workspace(seat); struct sway_output *output = focused_ws->output; - // TODO: Make the threshold configurable?? - const float THRESHOLD = 0.35; - if (fabs(output->workspace_scroll.percent) <= THRESHOLD) { + if (fabs(output->workspace_scroll.percent) <= config->workspace_gesture_threshold) { goto reset_state; } diff --git a/sway/sway.5.scd b/sway/sway.5.scd index 0e4b55f8..dee8fc7d 100644 --- a/sway/sway.5.scd +++ b/sway/sway.5.scd @@ -597,6 +597,11 @@ runtime. swipe past the first/last workspace. Disables the _workspace_gesture_spring_size_ config option. Disabled by default. +*workspace_gesture_spring_size* + Adjusts the swipe threshold of switching workspaces. A lower value makes it + easier to switch workspaces. Accepts values between 0.1 (small swipes) and + 0.9 (large swipes). The default value is set to 0.35. + *client.background* This command is ignored and is only present for i3 compatibility.