forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB99bf2dfIvan Molodetskikh1 year ago2106 commits

touch_overview_grab.rs274 lines, 8.9 KiBHistoryRawDownload
1use std::time::Duration;
2
3use smithay::desktop::Window;
4use smithay::input::touch::{
5 DownEvent, GrabStartData as TouchGrabStartData, MotionEvent, OrientationEvent, ShapeEvent,
6 TouchGrab, TouchInnerHandle, UpEvent,
7};
8use smithay::input::SeatHandler;
9use smithay::output::Output;
10use smithay::utils::{IsAlive, Logical, Point, Serial};
11
12use crate::layout::workspace::{Workspace, WorkspaceId};
13use crate::niri::State;
14use crate::window::Mapped;
15
16// When the touch is stationary for this much time, it becomes an interactive move.
17const INTERACTIVE_MOVE_THRESHOLD: Duration = Duration::from_millis(500);
18
19pub struct TouchOverviewGrab {
20 start_data: TouchGrabStartData<State>,
21 start_timestamp: Duration,
22 last_location: Point<f64, Logical>,
23 output: Output,
24 start_pos_within_output: Point<f64, Logical>,
25 workspace_id: Option<WorkspaceId>,
26 workspace_matched_narrow: bool,
27 window: Option<Window>,
28 gesture: GestureState,
29}
30
31#[derive(Debug, Clone, Copy)]
32enum GestureState {
33 Recognizing,
34 ViewOffset,
35 WorkspaceSwitch,
36 InteractiveMove,
37}
38
39impl TouchOverviewGrab {
40 pub fn new(
41 start_data: TouchGrabStartData<State>,
42 start_timestamp: Duration,
43 output: Output,
44 start_pos_within_output: Point<f64, Logical>,
45 workspace_id: Option<WorkspaceId>,
46 workspace_matched_narrow: bool,
47 window: Option<Window>,
48 ) -> Self {
49 Self {
50 last_location: start_data.location,
51 start_timestamp,
52 start_data,
53 output,
54 start_pos_within_output,
55 workspace_id,
56 workspace_matched_narrow,
57 window,
58 gesture: GestureState::Recognizing,
59 }
60 }
61
62 fn on_ungrab(&mut self, state: &mut State) {
63 let layout = &mut state.niri.layout;
64 match self.gesture {
65 GestureState::Recognizing => {
66 // Tap to activate.
67 layout.focus_output(&self.output);
68
69 // Activate the workspace if necessary.
70 if self.window.is_some() || self.workspace_matched_narrow {
71 // When activating a window, we want to activate the window's current
72 // workspace. Otherwise, find the workspace that we tapped on.
73 let ws_matches = |ws: &Workspace<Mapped>| {
74 if let Some(window) = &self.window {
75 ws.has_window(window)
76 } else if let Some(ws_id) = self.workspace_id {
77 ws.id() == ws_id
78 } else {
79 false
80 }
81 };
82
83 let ws_idx = if let Some((Some(mon), ws_idx, _)) =
84 layout.workspaces().find(|(_, _, ws)| ws_matches(ws))
85 {
86 // The workspace could've moved to a different output in the meantime.
87 (*mon.output() == self.output).then_some(ws_idx)
88 } else {
89 None
90 };
91
92 if let Some(ws_idx) = ws_idx {
93 layout.toggle_overview_to_workspace(ws_idx);
94 }
95 }
96
97 if let Some(window) = self.window.as_ref() {
98 layout.activate_window(window);
99 }
100 }
101 GestureState::ViewOffset => {
102 layout.view_offset_gesture_end(Some(false));
103 }
104 GestureState::WorkspaceSwitch => {
105 layout.workspace_switch_gesture_end(Some(false));
106 }
107 GestureState::InteractiveMove => {
108 layout.interactive_move_end(self.window.as_ref().unwrap());
109 }
110 };
111
112 state.niri.queue_redraw_all();
113 }
114}
115
116impl TouchGrab<State> for TouchOverviewGrab {
117 fn down(
118 &mut self,
119 data: &mut State,
120 handle: &mut TouchInnerHandle<'_, State>,
121 _focus: Option<(<State as SeatHandler>::TouchFocus, Point<f64, Logical>)>,
122 event: &DownEvent,
123 seq: Serial,
124 ) {
125 handle.down(data, None, event, seq);
126 }
127
128 fn up(
129 &mut self,
130 data: &mut State,
131 handle: &mut TouchInnerHandle<'_, State>,
132 event: &UpEvent,
133 seq: Serial,
134 ) {
135 handle.up(data, event, seq);
136
137 if event.slot != self.start_data.slot {
138 return;
139 }
140
141 handle.unset_grab(self, data);
142 }
143
144 fn motion(
145 &mut self,
146 data: &mut State,
147 handle: &mut TouchInnerHandle<'_, State>,
148 _focus: Option<(<State as SeatHandler>::TouchFocus, Point<f64, Logical>)>,
149 event: &MotionEvent,
150 seq: Serial,
151 ) {
152 handle.motion(data, None, event, seq);
153
154 if event.slot != self.start_data.slot {
155 return;
156 }
157
158 let timestamp = Duration::from_millis(u64::from(event.time));
159 let layout = &mut data.niri.layout;
160
161 // Check if we should become interactive move.
162 if matches!(self.gesture, GestureState::Recognizing) {
163 if let Some(window) = self.window.as_ref().filter(|win| win.alive()) {
164 let passed = timestamp.saturating_sub(self.start_timestamp);
165 if INTERACTIVE_MOVE_THRESHOLD <= passed
166 && layout.interactive_move_begin(
167 window.clone(),
168 &self.output,
169 self.start_pos_within_output,
170 )
171 {
172 self.gesture = GestureState::InteractiveMove;
173 }
174 }
175 }
176
177 // Check if we should become a spatial scroll.
178 if matches!(self.gesture, GestureState::Recognizing) {
179 let c = event.location - self.start_data.location;
180
181 // Check if the gesture moved far enough to decide. Threshold copied from libadwaita.
182 if c.x * c.x + c.y * c.y >= 16. * 16. {
183 if let Some(ws_id) = self.workspace_id.filter(|_| c.x.abs() > c.y.abs()) {
184 if let Some((ws_idx, ws)) = layout.find_workspace_by_id(ws_id) {
185 if ws.current_output() == Some(&self.output) {
186 layout.view_offset_gesture_begin(&self.output, Some(ws_idx), false);
187 self.gesture = GestureState::ViewOffset;
188 }
189 }
190 }
191
192 if matches!(self.gesture, GestureState::Recognizing) {
193 layout.workspace_switch_gesture_begin(&self.output, false);
194 self.gesture = GestureState::WorkspaceSwitch;
195 }
196 }
197 }
198
199 // Do nothing if still recognizing.
200 if matches!(self.gesture, GestureState::Recognizing) {
201 return;
202 }
203
204 let delta = event.location - self.last_location;
205 self.last_location = event.location;
206
207 let ongoing = match self.gesture {
208 GestureState::Recognizing => unreachable!(),
209 GestureState::ViewOffset => layout
210 .view_offset_gesture_update(-delta.x, timestamp, false)
211 .is_some(),
212 GestureState::WorkspaceSwitch => layout
213 .workspace_switch_gesture_update(-delta.y, timestamp, false)
214 .is_some(),
215 GestureState::InteractiveMove => {
216 let window = self.window.as_ref().unwrap();
217 if let Some((output, pos_within_output)) = data.niri.output_under(event.location) {
218 let output = output.clone();
219 data.niri.layout.interactive_move_update(
220 window,
221 delta,
222 output,
223 pos_within_output,
224 )
225 } else {
226 false
227 }
228 }
229 };
230
231 if ongoing {
232 data.niri.queue_redraw_all();
233 } else {
234 handle.unset_grab(self, data);
235 }
236 }
237
238 fn frame(&mut self, data: &mut State, handle: &mut TouchInnerHandle<'_, State>, seq: Serial) {
239 handle.frame(data, seq);
240 }
241
242 fn cancel(&mut self, data: &mut State, handle: &mut TouchInnerHandle<'_, State>, seq: Serial) {
243 handle.cancel(data, seq);
244 handle.unset_grab(self, data);
245 }
246
247 fn shape(
248 &mut self,
249 data: &mut State,
250 handle: &mut TouchInnerHandle<'_, State>,
251 event: &ShapeEvent,
252 seq: Serial,
253 ) {
254 handle.shape(data, event, seq);
255 }
256
257 fn orientation(
258 &mut self,
259 data: &mut State,
260 handle: &mut TouchInnerHandle<'_, State>,
261 event: &OrientationEvent,
262 seq: Serial,
263 ) {
264 handle.orientation(data, event, seq);
265 }
266
267 fn start_data(&self) -> &TouchGrabStartData<State> {
268 &self.start_data
269 }
270
271 fn unset(&mut self, data: &mut State) {
272 self.on_ungrab(data);
273 }
274}