Skip to content

Commit bb6cd68

Browse files
committed
feat: Gesture configuration in shortcuts config
This commit adds a Gesture abstraction, which can define gestures by the number of fingers used, and the direction the gesture is performed in. Oftentimes, we want the gesture to be either absolute (i.e. ignoring context such as workspace direction), or relative (if workspaces are horizontal, forward would be right, backward would be left. if workspaces are vertical, forward would be up, backward would be down). The abstraction is modeled after the Binding type, and the resulting Gestures type is similar to the Shortcuts type. This is the first step in implementing configurable touchpad gestures (pop-os/cosmic-comp#396) Signed-off-by: Ryan Brue <[email protected]>
1 parent 9d9ad8e commit bb6cd68

File tree

2 files changed

+263
-0
lines changed

2 files changed

+263
-0
lines changed

config/src/shortcuts/gesture.rs

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
// SPDX-License-Identifier: MPL-2.0
2+
use std::str::FromStr;
3+
4+
use serde::{Deserialize, Serialize};
5+
6+
/// Description of a gesture that can be handled by the compositor
7+
#[serde_with::serde_as]
8+
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, Hash)]
9+
#[serde(deny_unknown_fields)]
10+
pub struct Gesture {
11+
/// How many fingers are held down
12+
pub fingers: i32,
13+
pub direction: Direction,
14+
// A custom description for a custom binding
15+
#[serde(default, skip_serializing_if = "Option::is_none")]
16+
pub description: Option<String>,
17+
}
18+
19+
/// Describes a direction, either absolute or relative
20+
#[serde_with::serde_as]
21+
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, Hash)]
22+
#[serde(deny_unknown_fields)]
23+
pub enum Direction {
24+
Relative(RelativeDirection),
25+
Absolute(AbsoluteDirection),
26+
}
27+
28+
impl ToString for Direction {
29+
fn to_string(&self) -> String {
30+
match self {
31+
Direction::Absolute(abs) => match abs {
32+
AbsoluteDirection::AbsoluteUp => "AbsoluteUp".to_string(),
33+
AbsoluteDirection::AbsoluteDown => "AbsoluteDown".to_string(),
34+
AbsoluteDirection::AbsoluteLeft => "AbsoluteLeft".to_string(),
35+
AbsoluteDirection::AbsoluteRight => "AbsoluteRight".to_string(),
36+
},
37+
Direction::Relative(rel) => match rel {
38+
RelativeDirection::RelativeForward => "RelativeForward".to_string(),
39+
RelativeDirection::RelativeBackward => "RelativeBackward".to_string(),
40+
RelativeDirection::RelativeLeft => "RelativeLeft".to_string(),
41+
RelativeDirection::RelativeRight => "RelativeRight".to_string(),
42+
},
43+
}
44+
}
45+
}
46+
47+
impl FromStr for Direction {
48+
type Err = String;
49+
50+
fn from_str(s: &str) -> Result<Self, Self::Err> {
51+
match s {
52+
// Absolute directions
53+
"AbsoluteUp" => Ok(Direction::Absolute(AbsoluteDirection::AbsoluteUp)),
54+
"AbsoluteDown" => Ok(Direction::Absolute(AbsoluteDirection::AbsoluteDown)),
55+
"AbsoluteLeft" => Ok(Direction::Absolute(AbsoluteDirection::AbsoluteLeft)),
56+
"AbsoluteRight" => Ok(Direction::Absolute(AbsoluteDirection::AbsoluteRight)),
57+
// Relative directions
58+
"RelativeForward" => Ok(Direction::Relative(RelativeDirection::RelativeForward)),
59+
"RelativeBackward" => Ok(Direction::Relative(RelativeDirection::RelativeBackward)),
60+
"RelativeLeft" => Ok(Direction::Relative(RelativeDirection::RelativeLeft)),
61+
"RelativeRight" => Ok(Direction::Relative(RelativeDirection::RelativeRight)),
62+
_ => Err(format!("Invalid direction string"))
63+
}
64+
}
65+
}
66+
67+
/// Describes a relative direction (typically relative to the workspace direction)
68+
#[serde_with::serde_as]
69+
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, Hash)]
70+
#[serde(deny_unknown_fields)]
71+
pub enum RelativeDirection {
72+
RelativeForward,
73+
RelativeBackward,
74+
RelativeLeft,
75+
RelativeRight,
76+
}
77+
78+
/// Describes an absolute direction (i.e. not relative to workspace direction)
79+
#[serde_with::serde_as]
80+
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize, Hash)]
81+
#[serde(deny_unknown_fields)]
82+
pub enum AbsoluteDirection {
83+
AbsoluteUp,
84+
AbsoluteDown,
85+
AbsoluteLeft,
86+
AbsoluteRight,
87+
}
88+
89+
impl Gesture {
90+
/// Creates a new gesture from a number of fingers and a direction
91+
pub fn new(fingers: impl Into<i32>, direction: impl Into<Direction>) -> Gesture {
92+
Gesture {
93+
fingers: fingers.into(),
94+
direction: direction.into(),
95+
description: None,
96+
}
97+
}
98+
99+
/// Returns true if the direction is absolute
100+
pub fn is_absolute(&self) -> bool {
101+
matches!(self.direction, Direction::Absolute(_))
102+
}
103+
104+
/// Append the binding to an existing string
105+
pub fn to_string_in_place(&self, string: &mut String) {
106+
string.push_str(&format!(
107+
"{} Finger {}",
108+
self.fingers,
109+
self.direction.to_string()
110+
));
111+
}
112+
}
113+
114+
impl ToString for Gesture {
115+
fn to_string(&self) -> String {
116+
let mut string = String::new();
117+
self.to_string_in_place(&mut string);
118+
string
119+
}
120+
}
121+
122+
123+
impl FromStr for Gesture {
124+
type Err = String;
125+
126+
fn from_str(value: &str) -> Result<Self, Self::Err> {
127+
let mut value_iter = value.split("+");
128+
let n = match value_iter.next() {
129+
Some(val) => val,
130+
None => {
131+
return Err(format!("no value for the number of fingers"));
132+
},
133+
};
134+
let fingers = match i32::from_str(n) {
135+
Ok(a) => a,
136+
Err(_) => {
137+
return Err(format!("could not parse number of fingers"));
138+
},
139+
};
140+
141+
let n2 = match value_iter.next() {
142+
Some(val) => val,
143+
None => {
144+
return Err(format!("could not parse direction"));
145+
},
146+
};
147+
148+
let direction = match Direction::from_str(n2) {
149+
Ok(dir) => dir,
150+
Err(e) => {
151+
return Err(e);
152+
},
153+
};
154+
155+
if let Some(n3) = value_iter.next() {
156+
return Err(format!("Extra data {} not expected", n3));
157+
}
158+
159+
return Ok(Self {
160+
fingers,
161+
direction,
162+
description: None,
163+
});
164+
}
165+
}
166+
167+
168+
#[cfg(test)]
169+
mod tests {
170+
use crate::shortcuts::gesture::{AbsoluteDirection, Direction, RelativeDirection};
171+
172+
use super::Gesture;
173+
use std::str::FromStr;
174+
175+
#[test]
176+
fn binding_from_str() {
177+
assert_eq!(
178+
Gesture::from_str("3+RelativeLeft"),
179+
Ok(Gesture::new(
180+
3,
181+
Direction::Relative(RelativeDirection::RelativeLeft)
182+
))
183+
);
184+
185+
assert_eq!(
186+
Gesture::from_str("5+AbsoluteUp"),
187+
Ok(Gesture::new(
188+
5,
189+
Direction::Absolute(AbsoluteDirection::AbsoluteUp)
190+
))
191+
);
192+
193+
assert_ne!(
194+
Gesture::from_str("4+AbsoluteLeft+More+Info"),
195+
Ok(Gesture::new(
196+
4,
197+
Direction::Absolute(AbsoluteDirection::AbsoluteLeft)
198+
))
199+
);
200+
}
201+
}

config/src/shortcuts/mod.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ pub mod modifier;
88
pub use modifier::{Modifier, Modifiers, ModifiersDef};
99

1010
mod binding;
11+
mod gesture;
1112
pub use binding::Binding;
13+
pub use gesture::Gesture;
1214

1315
pub mod sym;
1416

@@ -80,6 +82,8 @@ pub fn system_actions(context: &cosmic_config::Config) -> SystemActions {
8082
pub struct Config {
8183
pub defaults: Shortcuts,
8284
pub custom: Shortcuts,
85+
pub default_gestures: Gestures,
86+
pub custom_gestures: Gestures,
8387
pub system_actions: SystemActions,
8488
}
8589

@@ -97,6 +101,18 @@ impl Config {
97101
.shortcut_for_action(action)
98102
.or_else(|| self.defaults.shortcut_for_action(action))
99103
}
104+
105+
pub fn gestures(&self) -> impl Iterator<Item = (&Gesture, &Action)> {
106+
self.custom_gestures
107+
.iter()
108+
.chain(self.default_gestures.iter())
109+
}
110+
111+
pub fn gesture_for_action(&self, action: &Action) -> Option<String> {
112+
self.custom_gestures
113+
.gesture_for_action(action)
114+
.or_else(|| self.default_gestures.gesture_for_action(action))
115+
}
100116
}
101117

102118
/// A map of defined key [Binding]s and their triggerable [Action]s
@@ -172,3 +188,49 @@ pub enum State {
172188
Pressed,
173189
Released,
174190
}
191+
192+
/// A map of defined [Gesture]s and their triggerable [Action]s
193+
#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)]
194+
#[serde(transparent)]
195+
pub struct Gestures(pub HashMap<Gesture, Action>);
196+
197+
impl Gestures {
198+
pub fn insert_default_gesture(
199+
&mut self,
200+
fingers: i32,
201+
direction: gesture::Direction,
202+
action: Action,
203+
) {
204+
if !self.0.values().any(|a| a == &action) {
205+
let pattern = Gesture {
206+
description: None,
207+
fingers,
208+
direction,
209+
};
210+
if !self.0.contains_key(&pattern) {
211+
self.0.insert(pattern, action.clone());
212+
}
213+
}
214+
}
215+
216+
pub fn iter(&self) -> impl Iterator<Item = (&Gesture, &Action)> {
217+
self.0.iter()
218+
}
219+
220+
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&Gesture, &mut Action)> {
221+
self.0.iter_mut()
222+
}
223+
224+
pub fn gesture_for_action(&self, action: &Action) -> Option<String> {
225+
self.gestures(action)
226+
.next() // take the first one
227+
.map(|gesture| gesture.to_string())
228+
}
229+
230+
pub fn gestures<'a>(&'a self, action: &'a Action) -> impl Iterator<Item = &'a Gesture> {
231+
self.0
232+
.iter()
233+
.filter(move |(_, a)| *a == action)
234+
.map(|(b, _)| b)
235+
}
236+
}

0 commit comments

Comments
 (0)