forge
Explore
Log in

Don't you dare go Hollow

Implement vertical middle mouse gesture

Browse files at this commit

db89d4d3dd1440e18193d4ce98d207531c7720e0Ivan MolodetskikhParent: 226273f
src/input/mod.rsmodified+8 -8
@@ -29,7 +29,7 @@ use smithay::wayland::pointer_constraints::{with_pointer_constraint, PointerCons
2929 use smithay::wayland::tablet_manager::{TabletDescriptor, TabletSeatTrait};
3030
3131 use self::resize_grab::ResizeGrab;
32-use self::view_offset_grab::ViewOffsetGrab;
32+use self::spatial_movement_grab::SpatialMovementGrab;
3333 use crate::niri::State;
3434 use crate::ui::screenshot_ui::ScreenshotUi;
3535 use crate::utils::spawning::spawn;
@@ -37,8 +37,8 @@ use crate::utils::{center, get_monotonic_time, ResizeEdge};
3737
3838 pub mod resize_grab;
3939 pub mod scroll_tracker;
40+pub mod spatial_movement_grab;
4041 pub mod swipe_tracker;
41-pub mod view_offset_grab;
4242
4343 pub const DOUBLE_CLICK_TIME: Duration = Duration::from_millis(400);
4444
@@ -1252,15 +1252,13 @@ impl State {
12521252 };
12531253 if mod_down {
12541254 if let Some(output) = self.niri.output_under_cursor() {
1255- self.niri.layout.view_offset_gesture_begin(&output, false);
1256-
12571255 let location = pointer.current_location();
12581256 let start_data = PointerGrabStartData {
12591257 focus: None,
12601258 button: event.button_code(),
12611259 location,
12621260 };
1263- let grab = ViewOffsetGrab::new(start_data);
1261+ let grab = SpatialMovementGrab::new(start_data, output);
12641262 pointer.set_grab(self, grab, serial, Focus::Clear);
12651263 self.niri.pointer_grab_ongoing = true;
12661264 self.niri
@@ -1699,7 +1697,9 @@ impl State {
16991697 if cx.abs() > cy.abs() {
17001698 self.niri.layout.view_offset_gesture_begin(&output, true);
17011699 } else {
1702- self.niri.layout.workspace_switch_gesture_begin(&output);
1700+ self.niri
1701+ .layout
1702+ .workspace_switch_gesture_begin(&output, true);
17031703 }
17041704 }
17051705 }
@@ -1711,7 +1711,7 @@ impl State {
17111711 let res = self
17121712 .niri
17131713 .layout
1714- .workspace_switch_gesture_update(delta_y, timestamp);
1714+ .workspace_switch_gesture_update(delta_y, timestamp, true);
17151715 if let Some(output) = res {
17161716 if let Some(output) = output {
17171717 self.niri.queue_redraw(&output);
@@ -1757,7 +1757,7 @@ impl State {
17571757 let res = self
17581758 .niri
17591759 .layout
1760- .workspace_switch_gesture_end(event.cancelled());
1760+ .workspace_switch_gesture_end(event.cancelled(), Some(true));
17611761 if let Some(output) = res {
17621762 self.niri.queue_redraw(&output);
17631763 handled = true;
src/input/view_offset_grab.rs → src/input/spatial_movement_grab.rsrenamed+53 -12
@@ -7,28 +7,45 @@ use smithay::input::pointer::{
77 MotionEvent, PointerGrab, PointerInnerHandle, RelativeMotionEvent,
88 };
99 use smithay::input::SeatHandler;
10+use smithay::output::Output;
1011 use smithay::utils::{Logical, Point};
1112
1213 use crate::niri::State;
1314
14-pub struct ViewOffsetGrab {
15+pub struct SpatialMovementGrab {
1516 start_data: PointerGrabStartData<State>,
1617 last_location: Point<f64, Logical>,
18+ output: Output,
19+ gesture: GestureState,
1720 }
1821
19-impl ViewOffsetGrab {
20- pub fn new(start_data: PointerGrabStartData<State>) -> Self {
22+#[derive(Debug, Clone, Copy)]
23+enum GestureState {
24+ Recognizing,
25+ ViewOffset,
26+ WorkspaceSwitch,
27+}
28+
29+impl SpatialMovementGrab {
30+ pub fn new(start_data: PointerGrabStartData<State>, output: Output) -> Self {
2131 Self {
2232 last_location: start_data.location,
2333 start_data,
34+ output,
35+ gesture: GestureState::Recognizing,
2436 }
2537 }
2638
2739 fn on_ungrab(&mut self, state: &mut State) {
28- let res = state
29- .niri
30- .layout
31- .view_offset_gesture_end(false, Some(false));
40+ let layout = &mut state.niri.layout;
41+ let res = match self.gesture {
42+ GestureState::Recognizing => None,
43+ GestureState::ViewOffset => layout.view_offset_gesture_end(false, Some(false)),
44+ GestureState::WorkspaceSwitch => {
45+ layout.workspace_switch_gesture_end(false, Some(false))
46+ }
47+ };
48+
3249 if let Some(output) = res {
3350 state.niri.queue_redraw(&output);
3451 }
@@ -41,7 +58,7 @@ impl ViewOffsetGrab {
4158 }
4259 }
4360
44-impl PointerGrab<State> for ViewOffsetGrab {
61+impl PointerGrab<State> for SpatialMovementGrab {
4562 fn motion(
4663 &mut self,
4764 data: &mut State,
@@ -56,10 +73,34 @@ impl PointerGrab<State> for ViewOffsetGrab {
5673 let delta = event.location - self.last_location;
5774 self.last_location = event.location;
5875
59- let res = data
60- .niri
61- .layout
62- .view_offset_gesture_update(-delta.x, timestamp, false);
76+ let layout = &mut data.niri.layout;
77+ let res = match self.gesture {
78+ GestureState::Recognizing => {
79+ let c = event.location - self.start_data.location;
80+
81+ // Check if the gesture moved far enough to decide. Threshold copied from GTK 4.
82+ if c.x * c.x + c.y * c.y >= 8. * 8. {
83+ if c.x.abs() > c.y.abs() {
84+ self.gesture = GestureState::ViewOffset;
85+ layout.view_offset_gesture_begin(&self.output, false);
86+ layout.view_offset_gesture_update(-c.x, timestamp, false)
87+ } else {
88+ self.gesture = GestureState::WorkspaceSwitch;
89+ layout.workspace_switch_gesture_begin(&self.output, false);
90+ layout.workspace_switch_gesture_update(-c.y, timestamp, false)
91+ }
92+ } else {
93+ Some(None)
94+ }
95+ }
96+ GestureState::ViewOffset => {
97+ layout.view_offset_gesture_update(-delta.x, timestamp, false)
98+ }
99+ GestureState::WorkspaceSwitch => {
100+ layout.workspace_switch_gesture_update(-delta.y, timestamp, false)
101+ }
102+ };
103+
63104 if let Some(output) = res {
64105 if let Some(output) = output {
65106 data.niri.queue_redraw(&output);
src/layout/mod.rsmodified+32 -12
@@ -2002,7 +2002,7 @@ impl<W: LayoutElement> Layout<W> {
20022002 }
20032003 }
20042004
2005- pub fn workspace_switch_gesture_begin(&mut self, output: &Output) {
2005+ pub fn workspace_switch_gesture_begin(&mut self, output: &Output, is_touchpad: bool) {
20062006 let monitors = match &mut self.monitor_set {
20072007 MonitorSet::Normal { monitors, .. } => monitors,
20082008 MonitorSet::NoOutputs { .. } => unreachable!(),
@@ -2011,11 +2011,11 @@ impl<W: LayoutElement> Layout<W> {
20112011 for monitor in monitors {
20122012 // Cancel the gesture on other outputs.
20132013 if &monitor.output != output {
2014- monitor.workspace_switch_gesture_end(true);
2014+ monitor.workspace_switch_gesture_end(true, None);
20152015 continue;
20162016 }
20172017
2018- monitor.workspace_switch_gesture_begin();
2018+ monitor.workspace_switch_gesture_begin(is_touchpad);
20192019 }
20202020 }
20212021
@@ -2023,6 +2023,7 @@ impl<W: LayoutElement> Layout<W> {
20232023 &mut self,
20242024 delta_y: f64,
20252025 timestamp: Duration,
2026+ is_touchpad: bool,
20262027 ) -> Option<Option<Output>> {
20272028 let monitors = match &mut self.monitor_set {
20282029 MonitorSet::Normal { monitors, .. } => monitors,
@@ -2030,7 +2031,9 @@ impl<W: LayoutElement> Layout<W> {
20302031 };
20312032
20322033 for monitor in monitors {
2033- if let Some(refresh) = monitor.workspace_switch_gesture_update(delta_y, timestamp) {
2034+ if let Some(refresh) =
2035+ monitor.workspace_switch_gesture_update(delta_y, timestamp, is_touchpad)
2036+ {
20342037 if refresh {
20352038 return Some(Some(monitor.output.clone()));
20362039 } else {
@@ -2042,14 +2045,18 @@ impl<W: LayoutElement> Layout<W> {
20422045 None
20432046 }
20442047
2045- pub fn workspace_switch_gesture_end(&mut self, cancelled: bool) -> Option<Output> {
2048+ pub fn workspace_switch_gesture_end(
2049+ &mut self,
2050+ cancelled: bool,
2051+ is_touchpad: Option<bool>,
2052+ ) -> Option<Output> {
20462053 let monitors = match &mut self.monitor_set {
20472054 MonitorSet::Normal { monitors, .. } => monitors,
20482055 MonitorSet::NoOutputs { .. } => return None,
20492056 };
20502057
20512058 for monitor in monitors {
2052- if monitor.workspace_switch_gesture_end(cancelled) {
2059+ if monitor.workspace_switch_gesture_end(cancelled, is_touchpad) {
20532060 return Some(monitor.output.clone());
20542061 }
20552062 }
@@ -2750,14 +2757,17 @@ mod tests {
27502757 WorkspaceSwitchGestureBegin {
27512758 #[proptest(strategy = "1..=5usize")]
27522759 output_idx: usize,
2760+ is_touchpad: bool,
27532761 },
27542762 WorkspaceSwitchGestureUpdate {
27552763 #[proptest(strategy = "-400f64..400f64")]
27562764 delta: f64,
27572765 timestamp: Duration,
2766+ is_touchpad: bool,
27582767 },
27592768 WorkspaceSwitchGestureEnd {
27602769 cancelled: bool,
2770+ is_touchpad: Option<bool>,
27612771 },
27622772 InteractiveResizeBegin {
27632773 #[proptest(strategy = "1..=5usize")]
@@ -3135,19 +3145,29 @@ mod tests {
31353145 // We don't handle cancels in this gesture.
31363146 layout.view_offset_gesture_end(false, is_touchpad);
31373147 }
3138- Op::WorkspaceSwitchGestureBegin { output_idx: id } => {
3148+ Op::WorkspaceSwitchGestureBegin {
3149+ output_idx: id,
3150+ is_touchpad,
3151+ } => {
31393152 let name = format!("output{id}");
31403153 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
31413154 return;
31423155 };
31433156
3144- layout.workspace_switch_gesture_begin(&output);
3157+ layout.workspace_switch_gesture_begin(&output, is_touchpad);
31453158 }
3146- Op::WorkspaceSwitchGestureUpdate { delta, timestamp } => {
3147- layout.workspace_switch_gesture_update(delta, timestamp);
3159+ Op::WorkspaceSwitchGestureUpdate {
3160+ delta,
3161+ timestamp,
3162+ is_touchpad,
3163+ } => {
3164+ layout.workspace_switch_gesture_update(delta, timestamp, is_touchpad);
31483165 }
3149- Op::WorkspaceSwitchGestureEnd { cancelled } => {
3150- layout.workspace_switch_gesture_end(cancelled);
3166+ Op::WorkspaceSwitchGestureEnd {
3167+ cancelled,
3168+ is_touchpad,
3169+ } => {
3170+ layout.workspace_switch_gesture_end(cancelled, is_touchpad);
31513171 }
31523172 Op::InteractiveResizeBegin { window, edges } => {
31533173 layout.interactive_resize_begin(window, edges);
src/layout/monitor.rsmodified+35 -8
@@ -54,10 +54,12 @@ pub enum WorkspaceSwitch {
5454 #[derive(Debug)]
5555 pub struct WorkspaceSwitchGesture {
5656 /// Index of the workspace where the gesture was started.
57- pub center_idx: usize,
57+ center_idx: usize,
5858 /// Current, fractional workspace index.
5959 pub current_idx: f64,
60- pub tracker: SwipeTracker,
60+ tracker: SwipeTracker,
61+ /// Whether the gesture is controlled by the touchpad.
62+ is_touchpad: bool,
6163 }
6264
6365 pub type MonitorRenderElement<R> =
@@ -973,7 +975,7 @@ impl<W: LayoutElement> Monitor<W> {
973975 }
974976 }
975977
976- pub fn workspace_switch_gesture_begin(&mut self) {
978+ pub fn workspace_switch_gesture_begin(&mut self, is_touchpad: bool) {
977979 let center_idx = self.active_workspace_idx;
978980 let current_idx = self
979981 .workspace_switch
@@ -985,6 +987,7 @@ impl<W: LayoutElement> Monitor<W> {
985987 center_idx,
986988 current_idx,
987989 tracker: SwipeTracker::new(),
990+ is_touchpad,
988991 };
989992 self.workspace_switch = Some(WorkspaceSwitch::Gesture(gesture));
990993 }
@@ -993,14 +996,24 @@ impl<W: LayoutElement> Monitor<W> {
993996 &mut self,
994997 delta_y: f64,
995998 timestamp: Duration,
999+ is_touchpad: bool,
9961000 ) -> Option<bool> {
9971001 let Some(WorkspaceSwitch::Gesture(gesture)) = &mut self.workspace_switch else {
9981002 return None;
9991003 };
10001004
1005+ if gesture.is_touchpad != is_touchpad {
1006+ return None;
1007+ }
1008+
10011009 gesture.tracker.push(delta_y, timestamp);
10021010
1003- let pos = gesture.tracker.pos() / WORKSPACE_GESTURE_MOVEMENT;
1011+ let total_height = if gesture.is_touchpad {
1012+ WORKSPACE_GESTURE_MOVEMENT
1013+ } else {
1014+ self.workspaces[0].view_size().h
1015+ };
1016+ let pos = gesture.tracker.pos() / total_height;
10041017
10051018 let min = gesture.center_idx.saturating_sub(1) as f64;
10061019 let max = (gesture.center_idx + 1).min(self.workspaces.len() - 1) as f64;
@@ -1015,20 +1028,34 @@ impl<W: LayoutElement> Monitor<W> {
10151028 Some(true)
10161029 }
10171030
1018- pub fn workspace_switch_gesture_end(&mut self, cancelled: bool) -> bool {
1031+ pub fn workspace_switch_gesture_end(
1032+ &mut self,
1033+ cancelled: bool,
1034+ is_touchpad: Option<bool>,
1035+ ) -> bool {
10191036 let Some(WorkspaceSwitch::Gesture(gesture)) = &mut self.workspace_switch else {
10201037 return false;
10211038 };
10221039
1040+ if is_touchpad.map_or(false, |x| gesture.is_touchpad != x) {
1041+ return false;
1042+ }
1043+
10231044 if cancelled {
10241045 self.workspace_switch = None;
10251046 self.clean_up_workspaces();
10261047 return true;
10271048 }
10281049
1029- let mut velocity = gesture.tracker.velocity() / WORKSPACE_GESTURE_MOVEMENT;
1030- let current_pos = gesture.tracker.pos() / WORKSPACE_GESTURE_MOVEMENT;
1031- let pos = gesture.tracker.projected_end_pos() / WORKSPACE_GESTURE_MOVEMENT;
1050+ let total_height = if gesture.is_touchpad {
1051+ WORKSPACE_GESTURE_MOVEMENT
1052+ } else {
1053+ self.workspaces[0].view_size().h
1054+ };
1055+
1056+ let mut velocity = gesture.tracker.velocity() / total_height;
1057+ let current_pos = gesture.tracker.pos() / total_height;
1058+ let pos = gesture.tracker.projected_end_pos() / total_height;
10321059
10331060 let min = gesture.center_idx.saturating_sub(1) as f64;
10341061 let max = (gesture.center_idx + 1).min(self.workspaces.len() - 1) as f64;