Refactor TouchOverviewGrab to process on frame event
Browse files at this commita434ea522cea42831cf559186d70050f9a004d37Parent: 88ea7fa1 changed file +101 -79
- src/input/touch_overview_grab.rs +101 -79
src/input/touch_overview_grab.rsmodified+101 -79
| @@ -26,6 +26,10 @@ pub struct TouchOverviewGrab { | |||
| 26 | workspace_matched_narrow: bool, | 26 | workspace_matched_narrow: bool, |
| 27 | window: Option<Window>, | 27 | window: Option<Window>, |
| 28 | gesture: GestureState, | 28 | gesture: GestureState, |
| 29 | + | ||
| 30 | + // Accumulated and applied in frame(). | ||
| 31 | + new_location: Point<f64, Logical>, | ||
| 32 | + event_timestamp: Option<Duration>, | ||
| 29 | } | 33 | } |
| 30 | 34 | ||
| 31 | #[derive(Debug, Clone, Copy)] | 35 | #[derive(Debug, Clone, Copy)] |
| @@ -46,8 +50,10 @@ impl TouchOverviewGrab { | |||
| 46 | workspace_matched_narrow: bool, | 50 | workspace_matched_narrow: bool, |
| 47 | window: Option<Window>, | 51 | window: Option<Window>, |
| 48 | ) -> Self { | 52 | ) -> Self { |
| 53 | + let location = start_data.location; | ||
| 54 | + | ||
| 49 | Self { | 55 | Self { |
| 50 | - last_location: start_data.location, | 56 | + last_location: location, |
| 51 | start_timestamp, | 57 | start_timestamp, |
| 52 | start_data, | 58 | start_data, |
| 53 | output, | 59 | output, |
| @@ -56,9 +62,96 @@ impl TouchOverviewGrab { | |||
| 56 | workspace_matched_narrow, | 62 | workspace_matched_narrow, |
| 57 | window, | 63 | window, |
| 58 | gesture: GestureState::Recognizing, | 64 | gesture: GestureState::Recognizing, |
| 65 | + new_location: location, | ||
| 66 | + event_timestamp: None, | ||
| 59 | } | 67 | } |
| 60 | } | 68 | } |
| 61 | 69 | ||
| 70 | + fn on_frame(&mut self, data: &mut State) -> bool { | ||
| 71 | + let Some(timestamp) = self.event_timestamp.take() else { | ||
| 72 | + return true; | ||
| 73 | + }; | ||
| 74 | + | ||
| 75 | + let layout = &mut data.niri.layout; | ||
| 76 | + | ||
| 77 | + // Check if we should become interactive move. | ||
| 78 | + if matches!(self.gesture, GestureState::Recognizing) { | ||
| 79 | + if let Some(window) = self.window.as_ref().filter(|win| win.alive()) { | ||
| 80 | + let passed = timestamp.saturating_sub(self.start_timestamp); | ||
| 81 | + if INTERACTIVE_MOVE_THRESHOLD <= passed | ||
| 82 | + && layout.interactive_move_begin( | ||
| 83 | + window.clone(), | ||
| 84 | + &self.output, | ||
| 85 | + self.start_pos_within_output, | ||
| 86 | + ) | ||
| 87 | + { | ||
| 88 | + self.gesture = GestureState::InteractiveMove; | ||
| 89 | + } | ||
| 90 | + } | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + // Check if we should become a spatial scroll. | ||
| 94 | + if matches!(self.gesture, GestureState::Recognizing) { | ||
| 95 | + let c = self.new_location - self.start_data.location; | ||
| 96 | + | ||
| 97 | + // Check if the gesture moved far enough to decide. Threshold copied from libadwaita. | ||
| 98 | + if c.x * c.x + c.y * c.y >= 16. * 16. { | ||
| 99 | + if let Some(ws_id) = self.workspace_id.filter(|_| c.x.abs() > c.y.abs()) { | ||
| 100 | + if let Some((ws_idx, ws)) = layout.find_workspace_by_id(ws_id) { | ||
| 101 | + if ws.current_output() == Some(&self.output) { | ||
| 102 | + layout.view_offset_gesture_begin(&self.output, Some(ws_idx), false); | ||
| 103 | + self.gesture = GestureState::ViewOffset; | ||
| 104 | + } | ||
| 105 | + } | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + if matches!(self.gesture, GestureState::Recognizing) { | ||
| 109 | + layout.workspace_switch_gesture_begin(&self.output, false); | ||
| 110 | + self.gesture = GestureState::WorkspaceSwitch; | ||
| 111 | + } | ||
| 112 | + } | ||
| 113 | + } | ||
| 114 | + | ||
| 115 | + // Do nothing if still recognizing. | ||
| 116 | + if matches!(self.gesture, GestureState::Recognizing) { | ||
| 117 | + return true; | ||
| 118 | + } | ||
| 119 | + | ||
| 120 | + let delta = self.new_location - self.last_location; | ||
| 121 | + self.last_location = self.new_location; | ||
| 122 | + | ||
| 123 | + let ongoing = match self.gesture { | ||
| 124 | + GestureState::Recognizing => unreachable!(), | ||
| 125 | + GestureState::ViewOffset => layout | ||
| 126 | + .view_offset_gesture_update(-delta.x, timestamp, false) | ||
| 127 | + .is_some(), | ||
| 128 | + GestureState::WorkspaceSwitch => layout | ||
| 129 | + .workspace_switch_gesture_update(-delta.y, timestamp, false) | ||
| 130 | + .is_some(), | ||
| 131 | + GestureState::InteractiveMove => { | ||
| 132 | + let window = self.window.as_ref().unwrap(); | ||
| 133 | + if let Some((output, pos_within_output)) = data.niri.output_under(self.new_location) | ||
| 134 | + { | ||
| 135 | + let output = output.clone(); | ||
| 136 | + data.niri.layout.interactive_move_update( | ||
| 137 | + window, | ||
| 138 | + delta, | ||
| 139 | + output, | ||
| 140 | + pos_within_output, | ||
| 141 | + ) | ||
| 142 | + } else { | ||
| 143 | + false | ||
| 144 | + } | ||
| 145 | + } | ||
| 146 | + }; | ||
| 147 | + | ||
| 148 | + if ongoing { | ||
| 149 | + data.niri.queue_redraw_all(); | ||
| 150 | + } | ||
| 151 | + | ||
| 152 | + ongoing | ||
| 153 | + } | ||
| 154 | + | ||
| 62 | fn on_ungrab(&mut self, state: &mut State) { | 155 | fn on_ungrab(&mut self, state: &mut State) { |
| 63 | let layout = &mut state.niri.layout; | 156 | let layout = &mut state.niri.layout; |
| 64 | match self.gesture { | 157 | match self.gesture { |
| @@ -158,88 +251,17 @@ impl TouchGrab<State> for TouchOverviewGrab { | |||
| 158 | return; | 251 | return; |
| 159 | } | 252 | } |
| 160 | 253 | ||
| 161 | - let timestamp = Duration::from_millis(u64::from(event.time)); | 254 | + self.new_location = event.location; |
| 162 | - let layout = &mut data.niri.layout; | 255 | + self.event_timestamp = Some(Duration::from_millis(u64::from(event.time))); |
| 163 | - | ||
| 164 | - // Check if we should become interactive move. | ||
| 165 | - if matches!(self.gesture, GestureState::Recognizing) { | ||
| 166 | - if let Some(window) = self.window.as_ref().filter(|win| win.alive()) { | ||
| 167 | - let passed = timestamp.saturating_sub(self.start_timestamp); | ||
| 168 | - if INTERACTIVE_MOVE_THRESHOLD <= passed | ||
| 169 | - && layout.interactive_move_begin( | ||
| 170 | - window.clone(), | ||
| 171 | - &self.output, | ||
| 172 | - self.start_pos_within_output, | ||
| 173 | - ) | ||
| 174 | - { | ||
| 175 | - self.gesture = GestureState::InteractiveMove; | ||
| 176 | - } | ||
| 177 | - } | ||
| 178 | - } | ||
| 179 | - | ||
| 180 | - // Check if we should become a spatial scroll. | ||
| 181 | - if matches!(self.gesture, GestureState::Recognizing) { | ||
| 182 | - let c = event.location - self.start_data.location; | ||
| 183 | - | ||
| 184 | - // Check if the gesture moved far enough to decide. Threshold copied from libadwaita. | ||
| 185 | - if c.x * c.x + c.y * c.y >= 16. * 16. { | ||
| 186 | - if let Some(ws_id) = self.workspace_id.filter(|_| c.x.abs() > c.y.abs()) { | ||
| 187 | - if let Some((ws_idx, ws)) = layout.find_workspace_by_id(ws_id) { | ||
| 188 | - if ws.current_output() == Some(&self.output) { | ||
| 189 | - layout.view_offset_gesture_begin(&self.output, Some(ws_idx), false); | ||
| 190 | - self.gesture = GestureState::ViewOffset; | ||
| 191 | - } | ||
| 192 | - } | ||
| 193 | - } | ||
| 194 | - | ||
| 195 | - if matches!(self.gesture, GestureState::Recognizing) { | ||
| 196 | - layout.workspace_switch_gesture_begin(&self.output, false); | ||
| 197 | - self.gesture = GestureState::WorkspaceSwitch; | ||
| 198 | - } | ||
| 199 | - } | ||
| 200 | - } | ||
| 201 | - | ||
| 202 | - // Do nothing if still recognizing. | ||
| 203 | - if matches!(self.gesture, GestureState::Recognizing) { | ||
| 204 | - return; | ||
| 205 | - } | ||
| 206 | - | ||
| 207 | - let delta = event.location - self.last_location; | ||
| 208 | - self.last_location = event.location; | ||
| 209 | - | ||
| 210 | - let ongoing = match self.gesture { | ||
| 211 | - GestureState::Recognizing => unreachable!(), | ||
| 212 | - GestureState::ViewOffset => layout | ||
| 213 | - .view_offset_gesture_update(-delta.x, timestamp, false) | ||
| 214 | - .is_some(), | ||
| 215 | - GestureState::WorkspaceSwitch => layout | ||
| 216 | - .workspace_switch_gesture_update(-delta.y, timestamp, false) | ||
| 217 | - .is_some(), | ||
| 218 | - GestureState::InteractiveMove => { | ||
| 219 | - let window = self.window.as_ref().unwrap(); | ||
| 220 | - if let Some((output, pos_within_output)) = data.niri.output_under(event.location) { | ||
| 221 | - let output = output.clone(); | ||
| 222 | - data.niri.layout.interactive_move_update( | ||
| 223 | - window, | ||
| 224 | - delta, | ||
| 225 | - output, | ||
| 226 | - pos_within_output, | ||
| 227 | - ) | ||
| 228 | - } else { | ||
| 229 | - false | ||
| 230 | - } | ||
| 231 | - } | ||
| 232 | - }; | ||
| 233 | - | ||
| 234 | - if ongoing { | ||
| 235 | - data.niri.queue_redraw_all(); | ||
| 236 | - } else { | ||
| 237 | - handle.unset_grab(self, data); | ||
| 238 | - } | ||
| 239 | } | 256 | } |
| 240 | 257 | ||
| 241 | fn frame(&mut self, data: &mut State, handle: &mut TouchInnerHandle<'_, State>) { | 258 | fn frame(&mut self, data: &mut State, handle: &mut TouchInnerHandle<'_, State>) { |
| 242 | handle.frame(data); | 259 | handle.frame(data); |
| 260 | + | ||
| 261 | + if !self.on_frame(data) { | ||
| 262 | + // The gesture is no longer ongoing. | ||
| 263 | + handle.unset_grab(self, data); | ||
| 264 | + } | ||
| 243 | } | 265 | } |
| 244 | 266 | ||
| 245 | fn cancel(&mut self, data: &mut State, handle: &mut TouchInnerHandle<'_, State>) { | 267 | fn cancel(&mut self, data: &mut State, handle: &mut TouchInnerHandle<'_, State>) { |