Skip to content

Commit 0eccd82

Browse files
committed
feat: touchpad gesture configuration
1 parent 895ea6a commit 0eccd82

File tree

5 files changed

+128
-44
lines changed

5 files changed

+128
-44
lines changed

cosmic-comp-config/src/input.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,43 @@ pub struct ScrollConfig {
4646
pub scroll_button: Option<u32>,
4747
pub scroll_factor: Option<f64>,
4848
}
49+
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
50+
pub struct TouchpadGestureConfig {
51+
pub three_finger: Option<GestureConfig>,
52+
pub four_finger: Option<GestureConfig>,
53+
pub five_finger: Option<GestureConfig>,
54+
}
55+
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
56+
pub enum GestureConfig {
57+
WorkspaceDependent(RelativeGestureConfig),
58+
Directional(AbsoluteGestureConfig),
59+
}
60+
#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)]
61+
pub struct RelativeGestureConfig {
62+
pub action_forward: Option<GestureCommand>,
63+
pub action_backward: Option<GestureCommand>,
64+
pub action_side_1: Option<GestureCommand>,
65+
pub action_side_2: Option<GestureCommand>,
66+
}
67+
#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)]
68+
pub struct AbsoluteGestureConfig {
69+
pub action_up: Option<GestureCommand>,
70+
pub action_down: Option<GestureCommand>,
71+
pub action_left: Option<GestureCommand>,
72+
pub action_right: Option<GestureCommand>,
73+
}
74+
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
75+
pub enum GestureCommand {
76+
WorkspaceForward,
77+
WorkspaceBackward,
78+
WorkspaceOverviewEnable,
79+
WorkspaceOverviewDisable,
80+
WindowUp,
81+
WindowDown,
82+
WindowLeft,
83+
WindowRight,
84+
Custom(String),
85+
}
4986

5087
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
5188
pub enum DeviceState {

cosmic-comp-config/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub struct CosmicCompConfig {
1313
pub workspaces: workspace::WorkspaceConfig,
1414
pub input_default: input::InputConfig,
1515
pub input_touchpad: input::InputConfig,
16+
pub input_touchpad_gestures: input::TouchpadGestureConfig,
1617
pub input_devices: HashMap<String, input::InputConfig>,
1718
pub xkb_config: XkbConfig,
1819
/// Autotiling enabled
@@ -31,6 +32,7 @@ impl Default for CosmicCompConfig {
3132
workspaces: Default::default(),
3233
input_default: Default::default(),
3334
input_touchpad: Default::default(),
35+
input_touchpad_gestures: Default::default(),
3436
input_devices: Default::default(),
3537
xkb_config: Default::default(),
3638
autotile: Default::default(),

src/config/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub use key_bindings::{Action, KeyModifier, KeyModifiers, KeyPattern};
3030
mod types;
3131
pub use self::types::*;
3232
use cosmic_comp_config::{
33-
input::InputConfig,
33+
input::{InputConfig, TouchpadGestureConfig},
3434
workspace::{WorkspaceConfig, WorkspaceLayout},
3535
CosmicCompConfig, TileBehavior, XkbConfig,
3636
};
@@ -553,6 +553,11 @@ fn config_changed(config: cosmic_config::Config, keys: Vec<String>, state: &mut
553553
state.common.config.cosmic_conf.input_touchpad = value;
554554
update_input(state);
555555
}
556+
"input_touchpad_gestures" => {
557+
let value = get_config::<TouchpadGestureConfig>(&config, "input_touchpad_gestures");
558+
state.common.config.cosmic_conf.input_touchpad_gestures = value;
559+
update_input(state);
560+
}
556561
"input_devices" => {
557562
let value = get_config::<HashMap<String, InputConfig>>(&config, "input_devices");
558563
state.common.config.cosmic_conf.input_devices = value;

src/input/gestures/mod.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use cosmic_comp_config::input::GestureCommand;
12
use smithay::utils::{Logical, Point};
23
use std::{collections::VecDeque, time::Duration};
34
use tracing::trace;
@@ -13,17 +14,11 @@ pub struct SwipeEvent {
1314
timestamp: Duration,
1415
}
1516

16-
#[derive(Debug, Clone, Copy)]
17-
pub enum SwipeAction {
18-
NextWorkspace,
19-
PrevWorkspace,
20-
}
21-
2217
#[derive(Debug, Clone)]
2318
pub struct GestureState {
2419
pub fingers: u32,
2520
pub direction: Option<Direction>,
26-
pub action: Option<SwipeAction>,
21+
pub action: Option<GestureCommand>,
2722
pub delta: f64,
2823
// Delta tracking inspired by Niri (GPL-3.0) https://github.com/YaLTeR/niri/tree/v0.1.3
2924
pub history: VecDeque<SwipeEvent>,

src/input/mod.rs

Lines changed: 81 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::{
44
backend::render::cursor::CursorState,
55
config::{xkb_config_to_wl, Action, Config, KeyModifiers, KeyPattern},
6-
input::gestures::{GestureState, SwipeAction},
6+
input::gestures::GestureState,
77
shell::{
88
focus::{
99
target::{KeyboardFocusTarget, PointerFocusTarget},
@@ -25,7 +25,11 @@ use crate::{
2525
},
2626
};
2727
use calloop::{timer::Timer, RegistrationToken};
28-
use cosmic_comp_config::{workspace::WorkspaceLayout, TileBehavior};
28+
use cosmic_comp_config::{
29+
input::{GestureCommand, GestureConfig},
30+
workspace::WorkspaceLayout,
31+
TileBehavior,
32+
};
2933
use cosmic_config::ConfigSet;
3034
use smithay::{
3135
backend::input::{
@@ -1140,52 +1144,92 @@ impl State {
11401144
}
11411145
InputEvent::GestureSwipeUpdate { event, .. } => {
11421146
if let Some(seat) = self.common.seat_with_device(&event.device()).cloned() {
1143-
let mut activate_action: Option<SwipeAction> = None;
1147+
let mut activate_action: Option<GestureCommand> = None;
11441148
if let Some(ref mut gesture_state) = self.common.gesture_state {
11451149
let first_update = gesture_state.update(
11461150
event.delta(),
11471151
Duration::from_millis(event.time_msec() as u64),
11481152
);
11491153
// Decide on action if first update
11501154
if first_update {
1151-
activate_action = match gesture_state.fingers {
1152-
3 => None, // TODO: 3 finger gestures
1153-
4 => {
1154-
if self.common.config.cosmic_conf.workspaces.workspace_layout
1155-
== WorkspaceLayout::Horizontal
1156-
{
1157-
match gesture_state.direction {
1158-
Some(Direction::Left) => {
1159-
Some(SwipeAction::NextWorkspace)
1160-
}
1161-
Some(Direction::Right) => {
1162-
Some(SwipeAction::PrevWorkspace)
1163-
}
1164-
_ => None, // TODO: Other actions
1155+
let target_config = match gesture_state.fingers {
1156+
3 => self
1157+
.common
1158+
.config
1159+
.cosmic_conf
1160+
.input_touchpad_gestures
1161+
.three_finger
1162+
.clone(),
1163+
4 => self
1164+
.common
1165+
.config
1166+
.cosmic_conf
1167+
.input_touchpad_gestures
1168+
.four_finger
1169+
.clone(),
1170+
5 => self
1171+
.common
1172+
.config
1173+
.cosmic_conf
1174+
.input_touchpad_gestures
1175+
.five_finger
1176+
.clone(),
1177+
_ => None,
1178+
};
1179+
1180+
activate_action = match target_config {
1181+
Some(GestureConfig::WorkspaceDependent(config)) => {
1182+
match (
1183+
self.common.config.cosmic_conf.workspaces.workspace_layout,
1184+
gesture_state.direction,
1185+
) {
1186+
(WorkspaceLayout::Vertical, Some(Direction::Down)) => {
1187+
config.action_forward.clone()
11651188
}
1166-
} else {
1167-
match gesture_state.direction {
1168-
Some(Direction::Up) => Some(SwipeAction::NextWorkspace),
1169-
Some(Direction::Down) => {
1170-
Some(SwipeAction::PrevWorkspace)
1171-
}
1172-
_ => None, // TODO: Other actions
1189+
(WorkspaceLayout::Vertical, Some(Direction::Up)) => {
1190+
config.action_backward.clone()
1191+
}
1192+
(WorkspaceLayout::Vertical, Some(Direction::Right)) => {
1193+
config.action_side_1.clone()
1194+
}
1195+
(WorkspaceLayout::Vertical, Some(Direction::Left)) => {
1196+
config.action_side_2.clone()
1197+
}
1198+
(WorkspaceLayout::Horizontal, Some(Direction::Down)) => {
1199+
config.action_side_1.clone()
1200+
}
1201+
(WorkspaceLayout::Horizontal, Some(Direction::Up)) => {
1202+
config.action_side_2.clone()
1203+
}
1204+
(WorkspaceLayout::Horizontal, Some(Direction::Right)) => {
1205+
config.action_forward.clone()
1206+
}
1207+
(WorkspaceLayout::Horizontal, Some(Direction::Left)) => {
1208+
config.action_backward.clone()
11731209
}
1210+
_ => None,
11741211
}
11751212
}
1176-
_ => None,
1213+
Some(GestureConfig::Directional(config)) => {
1214+
match gesture_state.direction {
1215+
Some(Direction::Up) => config.action_up.clone(),
1216+
Some(Direction::Down) => config.action_down.clone(),
1217+
Some(Direction::Right) => config.action_right.clone(),
1218+
Some(Direction::Left) => config.action_left.clone(),
1219+
None => None,
1220+
}
1221+
}
1222+
None => None,
11771223
};
1178-
1179-
gesture_state.action = activate_action;
1224+
gesture_state.action = activate_action.clone();
11801225
}
11811226

11821227
match gesture_state.action {
1183-
Some(SwipeAction::NextWorkspace) | Some(SwipeAction::PrevWorkspace) => {
1184-
self.common.shell.update_workspace_delta(
1185-
&seat.active_output(),
1186-
gesture_state.delta,
1187-
)
1188-
}
1228+
Some(GestureCommand::WorkspaceForward)
1229+
| Some(GestureCommand::WorkspaceBackward) => self
1230+
.common
1231+
.shell
1232+
.update_workspace_delta(&seat.active_output(), gesture_state.delta),
11891233
_ => {}
11901234
}
11911235
} else {
@@ -1199,10 +1243,10 @@ impl State {
11991243
);
12001244
}
12011245
match activate_action {
1202-
Some(SwipeAction::NextWorkspace) => {
1246+
Some(GestureCommand::WorkspaceForward) => {
12031247
let _ = self.to_next_workspace(&seat, true);
12041248
}
1205-
Some(SwipeAction::PrevWorkspace) => {
1249+
Some(GestureCommand::WorkspaceBackward) => {
12061250
let _ = self.to_previous_workspace(&seat, true);
12071251
}
12081252
_ => {}
@@ -1213,7 +1257,8 @@ impl State {
12131257
if let Some(seat) = self.common.seat_with_device(&event.device()).cloned() {
12141258
if let Some(ref gesture_state) = self.common.gesture_state {
12151259
match gesture_state.action {
1216-
Some(SwipeAction::NextWorkspace) | Some(SwipeAction::PrevWorkspace) => {
1260+
Some(GestureCommand::WorkspaceForward)
1261+
| Some(GestureCommand::WorkspaceBackward) => {
12171262
let velocity = gesture_state.velocity();
12181263
let norm_velocity =
12191264
if self.common.config.cosmic_conf.workspaces.workspace_layout

0 commit comments

Comments
 (0)