Implement vertical middle mouse gesture
Browse files at this commitdb89d4d3dd1440e18193d4ce98d207531c7720e0Parent: 226273f4 changed files +128 -40
- src/input/mod.rs +8 -8
- src/input/view_offset_grab.rs → src/input/spatial_movement_grab.rs +53 -12
- src/layout/mod.rs +32 -12
- src/layout/monitor.rs +35 -8
src/input/mod.rsmodified+8 -8
| @@ -29,7 +29,7 @@ use smithay::wayland::pointer_constraints::{with_pointer_constraint, PointerCons | ||
| 29 | 29 | use smithay::wayland::tablet_manager::{TabletDescriptor, TabletSeatTrait}; |
| 30 | 30 | |
| 31 | 31 | use self::resize_grab::ResizeGrab; |
| 32 | -use self::view_offset_grab::ViewOffsetGrab; | |
| 32 | +use self::spatial_movement_grab::SpatialMovementGrab; | |
| 33 | 33 | use crate::niri::State; |
| 34 | 34 | use crate::ui::screenshot_ui::ScreenshotUi; |
| 35 | 35 | use crate::utils::spawning::spawn; |
| @@ -37,8 +37,8 @@ use crate::utils::{center, get_monotonic_time, ResizeEdge}; | ||
| 37 | 37 | |
| 38 | 38 | pub mod resize_grab; |
| 39 | 39 | pub mod scroll_tracker; |
| 40 | +pub mod spatial_movement_grab; | |
| 40 | 41 | pub mod swipe_tracker; |
| 41 | -pub mod view_offset_grab; | |
| 42 | 42 | |
| 43 | 43 | pub const DOUBLE_CLICK_TIME: Duration = Duration::from_millis(400); |
| 44 | 44 | |
| @@ -1252,15 +1252,13 @@ impl State { | ||
| 1252 | 1252 | }; |
| 1253 | 1253 | if mod_down { |
| 1254 | 1254 | if let Some(output) = self.niri.output_under_cursor() { |
| 1255 | - self.niri.layout.view_offset_gesture_begin(&output, false); | |
| 1256 | - | |
| 1257 | 1255 | let location = pointer.current_location(); |
| 1258 | 1256 | let start_data = PointerGrabStartData { |
| 1259 | 1257 | focus: None, |
| 1260 | 1258 | button: event.button_code(), |
| 1261 | 1259 | location, |
| 1262 | 1260 | }; |
| 1263 | - let grab = ViewOffsetGrab::new(start_data); | |
| 1261 | + let grab = SpatialMovementGrab::new(start_data, output); | |
| 1264 | 1262 | pointer.set_grab(self, grab, serial, Focus::Clear); |
| 1265 | 1263 | self.niri.pointer_grab_ongoing = true; |
| 1266 | 1264 | self.niri |
| @@ -1699,7 +1697,9 @@ impl State { | ||
| 1699 | 1697 | if cx.abs() > cy.abs() { |
| 1700 | 1698 | self.niri.layout.view_offset_gesture_begin(&output, true); |
| 1701 | 1699 | } else { |
| 1702 | - self.niri.layout.workspace_switch_gesture_begin(&output); | |
| 1700 | + self.niri | |
| 1701 | + .layout | |
| 1702 | + .workspace_switch_gesture_begin(&output, true); | |
| 1703 | 1703 | } |
| 1704 | 1704 | } |
| 1705 | 1705 | } |
| @@ -1711,7 +1711,7 @@ impl State { | ||
| 1711 | 1711 | let res = self |
| 1712 | 1712 | .niri |
| 1713 | 1713 | .layout |
| 1714 | - .workspace_switch_gesture_update(delta_y, timestamp); | |
| 1714 | + .workspace_switch_gesture_update(delta_y, timestamp, true); | |
| 1715 | 1715 | if let Some(output) = res { |
| 1716 | 1716 | if let Some(output) = output { |
| 1717 | 1717 | self.niri.queue_redraw(&output); |
| @@ -1757,7 +1757,7 @@ impl State { | ||
| 1757 | 1757 | let res = self |
| 1758 | 1758 | .niri |
| 1759 | 1759 | .layout |
| 1760 | - .workspace_switch_gesture_end(event.cancelled()); | |
| 1760 | + .workspace_switch_gesture_end(event.cancelled(), Some(true)); | |
| 1761 | 1761 | if let Some(output) = res { |
| 1762 | 1762 | self.niri.queue_redraw(&output); |
| 1763 | 1763 | handled = true; |
src/input/view_offset_grab.rs → src/input/spatial_movement_grab.rsrenamed+53 -12
| @@ -7,28 +7,45 @@ use smithay::input::pointer::{ | ||
| 7 | 7 | MotionEvent, PointerGrab, PointerInnerHandle, RelativeMotionEvent, |
| 8 | 8 | }; |
| 9 | 9 | use smithay::input::SeatHandler; |
| 10 | +use smithay::output::Output; | |
| 10 | 11 | use smithay::utils::{Logical, Point}; |
| 11 | 12 | |
| 12 | 13 | use crate::niri::State; |
| 13 | 14 | |
| 14 | -pub struct ViewOffsetGrab { | |
| 15 | +pub struct SpatialMovementGrab { | |
| 15 | 16 | start_data: PointerGrabStartData<State>, |
| 16 | 17 | last_location: Point<f64, Logical>, |
| 18 | + output: Output, | |
| 19 | + gesture: GestureState, | |
| 17 | 20 | } |
| 18 | 21 | |
| 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 { | |
| 21 | 31 | Self { |
| 22 | 32 | last_location: start_data.location, |
| 23 | 33 | start_data, |
| 34 | + output, | |
| 35 | + gesture: GestureState::Recognizing, | |
| 24 | 36 | } |
| 25 | 37 | } |
| 26 | 38 | |
| 27 | 39 | 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 | + | |
| 32 | 49 | if let Some(output) = res { |
| 33 | 50 | state.niri.queue_redraw(&output); |
| 34 | 51 | } |
| @@ -41,7 +58,7 @@ impl ViewOffsetGrab { | ||
| 41 | 58 | } |
| 42 | 59 | } |
| 43 | 60 | |
| 44 | -impl PointerGrab<State> for ViewOffsetGrab { | |
| 61 | +impl PointerGrab<State> for SpatialMovementGrab { | |
| 45 | 62 | fn motion( |
| 46 | 63 | &mut self, |
| 47 | 64 | data: &mut State, |
| @@ -56,10 +73,34 @@ impl PointerGrab<State> for ViewOffsetGrab { | ||
| 56 | 73 | let delta = event.location - self.last_location; |
| 57 | 74 | self.last_location = event.location; |
| 58 | 75 | |
| 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 | + | |
| 63 | 104 | if let Some(output) = res { |
| 64 | 105 | if let Some(output) = output { |
| 65 | 106 | data.niri.queue_redraw(&output); |
src/layout/mod.rsmodified+32 -12
| @@ -2002,7 +2002,7 @@ impl<W: LayoutElement> Layout<W> { | ||
| 2002 | 2002 | } |
| 2003 | 2003 | } |
| 2004 | 2004 | |
| 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) { | |
| 2006 | 2006 | let monitors = match &mut self.monitor_set { |
| 2007 | 2007 | MonitorSet::Normal { monitors, .. } => monitors, |
| 2008 | 2008 | MonitorSet::NoOutputs { .. } => unreachable!(), |
| @@ -2011,11 +2011,11 @@ impl<W: LayoutElement> Layout<W> { | ||
| 2011 | 2011 | for monitor in monitors { |
| 2012 | 2012 | // Cancel the gesture on other outputs. |
| 2013 | 2013 | if &monitor.output != output { |
| 2014 | - monitor.workspace_switch_gesture_end(true); | |
| 2014 | + monitor.workspace_switch_gesture_end(true, None); | |
| 2015 | 2015 | continue; |
| 2016 | 2016 | } |
| 2017 | 2017 | |
| 2018 | - monitor.workspace_switch_gesture_begin(); | |
| 2018 | + monitor.workspace_switch_gesture_begin(is_touchpad); | |
| 2019 | 2019 | } |
| 2020 | 2020 | } |
| 2021 | 2021 | |
| @@ -2023,6 +2023,7 @@ impl<W: LayoutElement> Layout<W> { | ||
| 2023 | 2023 | &mut self, |
| 2024 | 2024 | delta_y: f64, |
| 2025 | 2025 | timestamp: Duration, |
| 2026 | + is_touchpad: bool, | |
| 2026 | 2027 | ) -> Option<Option<Output>> { |
| 2027 | 2028 | let monitors = match &mut self.monitor_set { |
| 2028 | 2029 | MonitorSet::Normal { monitors, .. } => monitors, |
| @@ -2030,7 +2031,9 @@ impl<W: LayoutElement> Layout<W> { | ||
| 2030 | 2031 | }; |
| 2031 | 2032 | |
| 2032 | 2033 | 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 | + { | |
| 2034 | 2037 | if refresh { |
| 2035 | 2038 | return Some(Some(monitor.output.clone())); |
| 2036 | 2039 | } else { |
| @@ -2042,14 +2045,18 @@ impl<W: LayoutElement> Layout<W> { | ||
| 2042 | 2045 | None |
| 2043 | 2046 | } |
| 2044 | 2047 | |
| 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> { | |
| 2046 | 2053 | let monitors = match &mut self.monitor_set { |
| 2047 | 2054 | MonitorSet::Normal { monitors, .. } => monitors, |
| 2048 | 2055 | MonitorSet::NoOutputs { .. } => return None, |
| 2049 | 2056 | }; |
| 2050 | 2057 | |
| 2051 | 2058 | for monitor in monitors { |
| 2052 | - if monitor.workspace_switch_gesture_end(cancelled) { | |
| 2059 | + if monitor.workspace_switch_gesture_end(cancelled, is_touchpad) { | |
| 2053 | 2060 | return Some(monitor.output.clone()); |
| 2054 | 2061 | } |
| 2055 | 2062 | } |
| @@ -2750,14 +2757,17 @@ mod tests { | ||
| 2750 | 2757 | WorkspaceSwitchGestureBegin { |
| 2751 | 2758 | #[proptest(strategy = "1..=5usize")] |
| 2752 | 2759 | output_idx: usize, |
| 2760 | + is_touchpad: bool, | |
| 2753 | 2761 | }, |
| 2754 | 2762 | WorkspaceSwitchGestureUpdate { |
| 2755 | 2763 | #[proptest(strategy = "-400f64..400f64")] |
| 2756 | 2764 | delta: f64, |
| 2757 | 2765 | timestamp: Duration, |
| 2766 | + is_touchpad: bool, | |
| 2758 | 2767 | }, |
| 2759 | 2768 | WorkspaceSwitchGestureEnd { |
| 2760 | 2769 | cancelled: bool, |
| 2770 | + is_touchpad: Option<bool>, | |
| 2761 | 2771 | }, |
| 2762 | 2772 | InteractiveResizeBegin { |
| 2763 | 2773 | #[proptest(strategy = "1..=5usize")] |
| @@ -3135,19 +3145,29 @@ mod tests { | ||
| 3135 | 3145 | // We don't handle cancels in this gesture. |
| 3136 | 3146 | layout.view_offset_gesture_end(false, is_touchpad); |
| 3137 | 3147 | } |
| 3138 | - Op::WorkspaceSwitchGestureBegin { output_idx: id } => { | |
| 3148 | + Op::WorkspaceSwitchGestureBegin { | |
| 3149 | + output_idx: id, | |
| 3150 | + is_touchpad, | |
| 3151 | + } => { | |
| 3139 | 3152 | let name = format!("output{id}"); |
| 3140 | 3153 | let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else { |
| 3141 | 3154 | return; |
| 3142 | 3155 | }; |
| 3143 | 3156 | |
| 3144 | - layout.workspace_switch_gesture_begin(&output); | |
| 3157 | + layout.workspace_switch_gesture_begin(&output, is_touchpad); | |
| 3145 | 3158 | } |
| 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); | |
| 3148 | 3165 | } |
| 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); | |
| 3151 | 3171 | } |
| 3152 | 3172 | Op::InteractiveResizeBegin { window, edges } => { |
| 3153 | 3173 | layout.interactive_resize_begin(window, edges); |
src/layout/monitor.rsmodified+35 -8
| @@ -54,10 +54,12 @@ pub enum WorkspaceSwitch { | ||
| 54 | 54 | #[derive(Debug)] |
| 55 | 55 | pub struct WorkspaceSwitchGesture { |
| 56 | 56 | /// Index of the workspace where the gesture was started. |
| 57 | - pub center_idx: usize, | |
| 57 | + center_idx: usize, | |
| 58 | 58 | /// Current, fractional workspace index. |
| 59 | 59 | pub current_idx: f64, |
| 60 | - pub tracker: SwipeTracker, | |
| 60 | + tracker: SwipeTracker, | |
| 61 | + /// Whether the gesture is controlled by the touchpad. | |
| 62 | + is_touchpad: bool, | |
| 61 | 63 | } |
| 62 | 64 | |
| 63 | 65 | pub type MonitorRenderElement<R> = |
| @@ -973,7 +975,7 @@ impl<W: LayoutElement> Monitor<W> { | ||
| 973 | 975 | } |
| 974 | 976 | } |
| 975 | 977 | |
| 976 | - pub fn workspace_switch_gesture_begin(&mut self) { | |
| 978 | + pub fn workspace_switch_gesture_begin(&mut self, is_touchpad: bool) { | |
| 977 | 979 | let center_idx = self.active_workspace_idx; |
| 978 | 980 | let current_idx = self |
| 979 | 981 | .workspace_switch |
| @@ -985,6 +987,7 @@ impl<W: LayoutElement> Monitor<W> { | ||
| 985 | 987 | center_idx, |
| 986 | 988 | current_idx, |
| 987 | 989 | tracker: SwipeTracker::new(), |
| 990 | + is_touchpad, | |
| 988 | 991 | }; |
| 989 | 992 | self.workspace_switch = Some(WorkspaceSwitch::Gesture(gesture)); |
| 990 | 993 | } |
| @@ -993,14 +996,24 @@ impl<W: LayoutElement> Monitor<W> { | ||
| 993 | 996 | &mut self, |
| 994 | 997 | delta_y: f64, |
| 995 | 998 | timestamp: Duration, |
| 999 | + is_touchpad: bool, | |
| 996 | 1000 | ) -> Option<bool> { |
| 997 | 1001 | let Some(WorkspaceSwitch::Gesture(gesture)) = &mut self.workspace_switch else { |
| 998 | 1002 | return None; |
| 999 | 1003 | }; |
| 1000 | 1004 | |
| 1005 | + if gesture.is_touchpad != is_touchpad { | |
| 1006 | + return None; | |
| 1007 | + } | |
| 1008 | + | |
| 1001 | 1009 | gesture.tracker.push(delta_y, timestamp); |
| 1002 | 1010 | |
| 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; | |
| 1004 | 1017 | |
| 1005 | 1018 | let min = gesture.center_idx.saturating_sub(1) as f64; |
| 1006 | 1019 | let max = (gesture.center_idx + 1).min(self.workspaces.len() - 1) as f64; |
| @@ -1015,20 +1028,34 @@ impl<W: LayoutElement> Monitor<W> { | ||
| 1015 | 1028 | Some(true) |
| 1016 | 1029 | } |
| 1017 | 1030 | |
| 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 { | |
| 1019 | 1036 | let Some(WorkspaceSwitch::Gesture(gesture)) = &mut self.workspace_switch else { |
| 1020 | 1037 | return false; |
| 1021 | 1038 | }; |
| 1022 | 1039 | |
| 1040 | + if is_touchpad.map_or(false, |x| gesture.is_touchpad != x) { | |
| 1041 | + return false; | |
| 1042 | + } | |
| 1043 | + | |
| 1023 | 1044 | if cancelled { |
| 1024 | 1045 | self.workspace_switch = None; |
| 1025 | 1046 | self.clean_up_workspaces(); |
| 1026 | 1047 | return true; |
| 1027 | 1048 | } |
| 1028 | 1049 | |
| 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; | |
| 1032 | 1059 | |
| 1033 | 1060 | let min = gesture.center_idx.saturating_sub(1) as f64; |
| 1034 | 1061 | let max = (gesture.center_idx + 1).min(self.workspaces.len() - 1) as f64; |