forge
Explore
Log in

Don't you dare go Hollow

Implement scrolling the view during interactive move

Browse files at this commit

d7f3ca00c70b264032025c262b3c0b10e67c04beIvan MolodetskikhParent: fd8140e
src/input/move_grab.rsmodified+0 -31
@@ -1,5 +1,3 @@
1-use std::time::Duration;
2-
3 use smithay::backend::input::ButtonState;1 use smithay::backend::input::ButtonState;
4 use smithay::desktop::Window;2 use smithay::desktop::Window;
5 use smithay::input::pointer::{3 use smithay::input::pointer::{
@@ -17,7 +15,6 @@ pub struct MoveGrab {
17 start_data: PointerGrabStartData<State>,15 start_data: PointerGrabStartData<State>,
18 last_location: Point<f64, Logical>,16 last_location: Point<f64, Logical>,
19 window: Window,17 window: Window,
20- is_moving: bool,
21 }18 }
22 19
23 impl MoveGrab {20 impl MoveGrab {
@@ -26,7 +23,6 @@ impl MoveGrab {
26 last_location: start_data.location,23 last_location: start_data.location,
27 start_data,24 start_data,
28 window,25 window,
29- is_moving: false,
30 }26 }
31 }27 }
32 28
@@ -64,14 +60,6 @@ impl PointerGrab<State> for MoveGrab {
64 pos_within_output,60 pos_within_output,
65 );61 );
66 if ongoing {62 if ongoing {
67- let timestamp = Duration::from_millis(u64::from(event.time));
68- if self.is_moving {
69- data.niri.layout.view_offset_gesture_update(
70- -event_delta.x,
71- timestamp,
72- false,
73- );
74- }
75 // FIXME: only redraw the previous and the new output.63 // FIXME: only redraw the previous and the new output.
76 data.niri.queue_redraw_all();64 data.niri.queue_redraw_all();
77 return;65 return;
@@ -104,25 +92,6 @@ impl PointerGrab<State> for MoveGrab {
104 ) {92 ) {
105 handle.button(data, event);93 handle.button(data, event);
106 94
107- // MouseButton::Middle
108- if event.button == 0x112 {
109- if event.state == ButtonState::Pressed {
110- let output = data
111- .niri
112- .output_under(handle.current_location())
113- .map(|(output, _)| output)
114- .cloned();
115- // FIXME: workspace switch gesture.
116- if let Some(output) = output {
117- self.is_moving = true;
118- data.niri.layout.view_offset_gesture_begin(&output, false);
119- }
120- } else if event.state == ButtonState::Released {
121- self.is_moving = false;
122- data.niri.layout.view_offset_gesture_end(false, None);
123- }
124- }
125-
126 // When moving with the left button, right toggles floating, and vice versa.95 // When moving with the left button, right toggles floating, and vice versa.
127 let toggle_floating_button = if self.start_data.button == 0x110 {96 let toggle_floating_button = if self.start_data.button == 0x110 {
128 0x11197 0x111
src/layout/mod.rsmodified+110 -12
@@ -1087,6 +1087,12 @@ impl<W: LayoutElement> Layout<W> {
1087 else {1087 else {
1088 unreachable!()1088 unreachable!()
1089 };1089 };
1090+
1091+ // Unlock the view on the workspaces.
1092+ for ws in self.workspaces_mut() {
1093+ ws.view_offset_gesture_end(false, None);
1094+ }
1095+
1090 return Some(RemovedTile {1096 return Some(RemovedTile {
1091 tile: move_.tile,1097 tile: move_.tile,
1092 width: move_.width,1098 width: move_.width,
@@ -2372,6 +2378,8 @@ impl<W: LayoutElement> Layout<W> {
2372 assert!(primary_idx < monitors.len());2378 assert!(primary_idx < monitors.len());
2373 assert!(active_monitor_idx < monitors.len());2379 assert!(active_monitor_idx < monitors.len());
2374 2380
2381+ let mut saw_view_offset_gesture = false;
2382+
2375 for (idx, monitor) in monitors.iter().enumerate() {2383 for (idx, monitor) in monitors.iter().enumerate() {
2376 assert!(2384 assert!(
2377 !monitor.workspaces.is_empty(),2385 !monitor.workspaces.is_empty(),
@@ -2508,6 +2516,28 @@ impl<W: LayoutElement> Layout<W> {
2508 }2516 }
2509 2517
2510 workspace.verify_invariants(move_win_id.as_ref());2518 workspace.verify_invariants(move_win_id.as_ref());
2519+
2520+ let has_view_offset_gesture = workspace.scrolling().view_offset().is_gesture();
2521+ if self.interactive_move.is_some() {
2522+ // We'd like to check that all workspaces have the gesture here, furthermore we
2523+ // want to check that they have the gesture only if the interactive move
2524+ // targets the scrolling layout. However, we cannot do that because we start
2525+ // and stop the gesture lazily. Otherwise the gesture code would pollute a lot
2526+ // of places like adding new workspaces, implicitly moving windows between
2527+ // floating and tiling on fullscreen, etc.
2528+ //
2529+ // assert!(
2530+ // has_view_offset_gesture,
2531+ // "during an interactive move in the scrolling layout, \
2532+ // all workspaces should be in a view offset gesture"
2533+ // );
2534+ } else if saw_view_offset_gesture {
2535+ assert!(
2536+ !has_view_offset_gesture,
2537+ "only one workspace can have an ongoing view offset gesture"
2538+ );
2539+ }
2540+ saw_view_offset_gesture = has_view_offset_gesture;
2511 }2541 }
2512 }2542 }
2513 }2543 }
@@ -2517,6 +2547,23 @@ impl<W: LayoutElement> Layout<W> {
2517 2547
2518 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {2548 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
2519 move_.tile.advance_animations();2549 move_.tile.advance_animations();
2550+
2551+ // Scroll the view if needed.
2552+ if !move_.is_floating {
2553+ let output = move_.output.clone();
2554+ let pos_within_output = move_.pointer_pos_within_output;
2555+ if let Some(mon) = self.monitor_for_output_mut(&output) {
2556+ if let Some((ws, offset)) = mon.workspace_under(pos_within_output) {
2557+ let ws_id = ws.id();
2558+ let ws = mon
2559+ .workspaces
2560+ .iter_mut()
2561+ .find(|ws| ws.id() == ws_id)
2562+ .unwrap();
2563+ ws.dnd_scroll_gesture_scroll(pos_within_output - offset);
2564+ }
2565+ }
2566+ }
2520 }2567 }
2521 2568
2522 match &mut self.monitor_set {2569 match &mut self.monitor_set {
@@ -2535,11 +2582,15 @@ impl<W: LayoutElement> Layout<W> {
2535 2582
2536 pub fn are_animations_ongoing(&self, output: Option<&Output>) -> bool {2583 pub fn are_animations_ongoing(&self, output: Option<&Output>) -> bool {
2537 if let Some(InteractiveMoveState::Moving(move_)) = &self.interactive_move {2584 if let Some(InteractiveMoveState::Moving(move_)) = &self.interactive_move {
2538- #[allow(clippy::collapsible_if)]
2539 if output.map_or(true, |output| *output == move_.output) {2585 if output.map_or(true, |output| *output == move_.output) {
2540 if move_.tile.are_animations_ongoing() {2586 if move_.tile.are_animations_ongoing() {
2541 return true;2587 return true;
2542 }2588 }
2589+
2590+ // Keep advancing animations if we might need to scroll the view.
2591+ if !move_.is_floating {
2592+ return true;
2593+ }
2543 }2594 }
2544 }2595 }
2545 2596
@@ -2919,6 +2970,7 @@ impl<W: LayoutElement> Layout<W> {
2919 2970
2920 win.request_size_once(size, true);2971 win.request_size_once(size, true);
2921 }2972 }
2973+
2922 return;2974 return;
2923 }2975 }
2924 }2976 }
@@ -3516,6 +3568,7 @@ impl<W: LayoutElement> Layout<W> {
3516 return false;3568 return false;
3517 }3569 }
3518 3570
3571+ let is_floating = ws.is_floating(&window_id);
3519 let (tile, tile_offset, _visible) = ws3572 let (tile, tile_offset, _visible) = ws
3520 .tiles_with_render_positions()3573 .tiles_with_render_positions()
3521 .find(|(tile, _, _)| tile.window().id() == &window_id)3574 .find(|(tile, _, _)| tile.window().id() == &window_id)
@@ -3537,6 +3590,13 @@ impl<W: LayoutElement> Layout<W> {
3537 pointer_ratio_within_window,3590 pointer_ratio_within_window,
3538 });3591 });
3539 3592
3593+ // Lock the view for scrolling interactive move.
3594+ if !is_floating {
3595+ for ws in self.workspaces_mut() {
3596+ ws.dnd_scroll_gesture_begin();
3597+ }
3598+ }
3599+
3540 true3600 true
3541 }3601 }
3542 3602
@@ -3744,14 +3804,25 @@ impl<W: LayoutElement> Layout<W> {
3744 unreachable!()3804 unreachable!()
3745 };3805 };
3746 3806
3747- let tile = self3807+ for ws in self.workspaces_mut() {
3748- .workspaces_mut()3808+ if let Some(tile) = ws.tiles_mut().find(|tile| *tile.window().id() == window_id)
3749- .flat_map(|ws| ws.tiles_mut())3809+ {
3750- .find(|tile| *tile.window().id() == window_id)3810+ let offset = tile.interactive_move_offset;
3751- .unwrap();3811+ tile.interactive_move_offset = Point::from((0., 0.));
3752- let offset = tile.interactive_move_offset;3812+ tile.animate_move_from(offset);
3753- tile.interactive_move_offset = Point::from((0., 0.));3813+ }
3754- tile.animate_move_from(offset);3814+
3815+ // Unlock the view on the workspaces, but if the moved window was active,
3816+ // preserve that.
3817+ let moved_tile_was_active =
3818+ ws.active_window().is_some_and(|win| *win.id() == window_id);
3819+
3820+ ws.view_offset_gesture_end(false, None);
3821+
3822+ if moved_tile_was_active {
3823+ ws.activate_window(&window_id);
3824+ }
3825+ }
3755 3826
3756 return;3827 return;
3757 }3828 }
@@ -3766,6 +3837,13 @@ impl<W: LayoutElement> Layout<W> {
3766 unreachable!()3837 unreachable!()
3767 };3838 };
3768 3839
3840+ // Unlock the view on the workspaces.
3841+ if !move_.is_floating {
3842+ for ws in self.workspaces_mut() {
3843+ ws.view_offset_gesture_end(false, None);
3844+ }
3845+ }
3846+
3769 match &mut self.monitor_set {3847 match &mut self.monitor_set {
3770 MonitorSet::Normal {3848 MonitorSet::Normal {
3771 monitors,3849 monitors,
@@ -4271,6 +4349,7 @@ impl<W: LayoutElement> Layout<W> {
4271 4349
4272 self.is_active = is_active;4350 self.is_active = is_active;
4273 4351
4352+ let mut ongoing_scrolling_dnd = None;
4274 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {4353 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
4275 let win = move_.tile.window_mut();4354 let win = move_.tile.window_mut();
4276 4355
@@ -4284,6 +4363,16 @@ impl<W: LayoutElement> Layout<W> {
4284 4363
4285 win.send_pending_configure();4364 win.send_pending_configure();
4286 win.refresh();4365 win.refresh();
4366+
4367+ ongoing_scrolling_dnd = Some(!move_.is_floating);
4368+ } else if let Some(InteractiveMoveState::Starting { window_id, .. }) =
4369+ &self.interactive_move
4370+ {
4371+ let (_, _, ws) = self
4372+ .workspaces()
4373+ .find(|(_, _, ws)| ws.has_window(window_id))
4374+ .unwrap();
4375+ ongoing_scrolling_dnd = Some(!ws.is_floating(window_id));
4287 }4376 }
4288 4377
4289 match &mut self.monitor_set {4378 match &mut self.monitor_set {
@@ -4299,9 +4388,18 @@ impl<W: LayoutElement> Layout<W> {
4299 for (ws_idx, ws) in mon.workspaces.iter_mut().enumerate() {4388 for (ws_idx, ws) in mon.workspaces.iter_mut().enumerate() {
4300 ws.refresh(is_active);4389 ws.refresh(is_active);
4301 4390
4302- // Cancel the view offset gesture after workspace switches, moves, etc.4391+ if let Some(is_scrolling) = ongoing_scrolling_dnd {
4303- if ws_idx != mon.active_workspace_idx {4392+ // Lock or unlock the view for scrolling interactive move.
4304- ws.view_offset_gesture_end(false, None);4393+ if is_scrolling {
4394+ ws.dnd_scroll_gesture_begin();
4395+ } else {
4396+ ws.view_offset_gesture_end(false, None);
4397+ }
4398+ } else {
4399+ // Cancel the view offset gesture after workspace switches, moves, etc.
4400+ if ws_idx != mon.active_workspace_idx {
4401+ ws.view_offset_gesture_end(false, None);
4402+ }
4305 }4403 }
4306 }4404 }
4307 }4405 }
src/layout/scrolling.rsmodified+102 -3
@@ -123,7 +123,7 @@ struct ColumnData {
123 }123 }
124 124
125 #[derive(Debug)]125 #[derive(Debug)]
126-enum ViewOffset {126+pub(super) enum ViewOffset {
127 /// The view offset is static.127 /// The view offset is static.
128 Static(f64),128 Static(f64),
129 /// The view offset is animating.129 /// The view offset is animating.
@@ -133,7 +133,7 @@ enum ViewOffset {
133 }133 }
134 134
135 #[derive(Debug)]135 #[derive(Debug)]
136-struct ViewGesture {136+pub(super) struct ViewGesture {
137 current_view_offset: f64,137 current_view_offset: f64,
138 tracker: SwipeTracker,138 tracker: SwipeTracker,
139 delta_from_tracker: f64,139 delta_from_tracker: f64,
@@ -141,6 +141,10 @@ struct ViewGesture {
141 stationary_view_offset: f64,141 stationary_view_offset: f64,
142 /// Whether the gesture is controlled by the touchpad.142 /// Whether the gesture is controlled by the touchpad.
143 is_touchpad: bool,143 is_touchpad: bool,
144+
145+ // If this gesture is for drag-and-drop scrolling, this is the last event's unadjusted
146+ // timestamp.
147+ dnd_last_event_time: Option<Duration>,
144 }148 }
145 149
146 #[derive(Debug)]150 #[derive(Debug)]
@@ -324,6 +328,17 @@ impl<W: LayoutElement> ScrollingSpace<W> {
324 }328 }
325 }329 }
326 330
331+ if let ViewOffset::Gesture(gesture) = &mut self.view_offset {
332+ // Make sure the last event time doesn't go too much out of date (for
333+ // workspaces not under cursor), causing sudden jumps.
334+ //
335+ // This happens after any dnd_scroll_gesture_scroll() calls (in
336+ // Layout::advance_animations()), so it doesn't mess up the time delta there.
337+ if let Some(last_time) = &mut gesture.dnd_last_event_time {
338+ *last_time = self.clock.now_unadjusted();
339+ }
340+ }
341+
327 for col in &mut self.columns {342 for col in &mut self.columns {
328 col.advance_animations();343 col.advance_animations();
329 }344 }
@@ -2711,10 +2726,34 @@ impl<W: LayoutElement> ScrollingSpace<W> {
2711 delta_from_tracker: self.view_offset.current(),2726 delta_from_tracker: self.view_offset.current(),
2712 stationary_view_offset: self.view_offset.stationary(),2727 stationary_view_offset: self.view_offset.stationary(),
2713 is_touchpad,2728 is_touchpad,
2729+ dnd_last_event_time: None,
2714 };2730 };
2715 self.view_offset = ViewOffset::Gesture(gesture);2731 self.view_offset = ViewOffset::Gesture(gesture);
2716 }2732 }
2717 2733
2734+ pub fn dnd_scroll_gesture_begin(&mut self) {
2735+ if let ViewOffset::Gesture(ViewGesture {
2736+ dnd_last_event_time: Some(_),
2737+ ..
2738+ }) = &self.view_offset
2739+ {
2740+ // Already active.
2741+ return;
2742+ }
2743+
2744+ let gesture = ViewGesture {
2745+ current_view_offset: self.view_offset.current(),
2746+ tracker: SwipeTracker::new(),
2747+ delta_from_tracker: self.view_offset.current(),
2748+ stationary_view_offset: self.view_offset.stationary(),
2749+ is_touchpad: false,
2750+ dnd_last_event_time: Some(self.clock.now_unadjusted()),
2751+ };
2752+ self.view_offset = ViewOffset::Gesture(gesture);
2753+
2754+ self.interactive_resize = None;
2755+ }
2756+
2718 pub fn view_offset_gesture_update(2757 pub fn view_offset_gesture_update(
2719 &mut self,2758 &mut self,
2720 delta_x: f64,2759 delta_x: f64,
@@ -2725,7 +2764,7 @@ impl<W: LayoutElement> ScrollingSpace<W> {
2725 return None;2764 return None;
2726 };2765 };
2727 2766
2728- if gesture.is_touchpad != is_touchpad {2767+ if gesture.is_touchpad != is_touchpad || gesture.dnd_last_event_time.is_some() {
2729 return None;2768 return None;
2730 }2769 }
2731 2770
@@ -2743,6 +2782,61 @@ impl<W: LayoutElement> ScrollingSpace<W> {
2743 Some(true)2782 Some(true)
2744 }2783 }
2745 2784
2785+ pub fn dnd_scroll_gesture_scroll(&mut self, delta: f64) {
2786+ let ViewOffset::Gesture(gesture) = &mut self.view_offset else {
2787+ return;
2788+ };
2789+
2790+ let Some(last_time) = gesture.dnd_last_event_time else {
2791+ // Not a DnD scroll.
2792+ return;
2793+ };
2794+
2795+ let now = self.clock.now_unadjusted();
2796+ gesture.dnd_last_event_time = Some(now);
2797+ let time_delta = now.saturating_sub(last_time).as_secs_f64();
2798+
2799+ let delta = delta * time_delta * 50.;
2800+
2801+ gesture.tracker.push(delta, now);
2802+
2803+ let view_offset = gesture.tracker.pos() + gesture.delta_from_tracker;
2804+
2805+ // Clamp it so that it doesn't go too much out of bounds.
2806+ let (leftmost, rightmost) = if self.columns.is_empty() {
2807+ (0., 0.)
2808+ } else {
2809+ let gaps = self.options.gaps;
2810+
2811+ let mut leftmost = -self.working_area.size.w;
2812+
2813+ let last_col_idx = self.columns.len() - 1;
2814+ let last_col_x = self
2815+ .columns
2816+ .iter()
2817+ .take(last_col_idx)
2818+ .fold(0., |col_x, col| col_x + col.width() + gaps);
2819+ let last_col_width = self.data[last_col_idx].width;
2820+ let mut rightmost = last_col_x + last_col_width - self.working_area.loc.x;
2821+
2822+ let active_col_x = self
2823+ .columns
2824+ .iter()
2825+ .take(self.active_column_idx)
2826+ .fold(0., |col_x, col| col_x + col.width() + gaps);
2827+ leftmost -= active_col_x;
2828+ rightmost -= active_col_x;
2829+
2830+ (leftmost, rightmost)
2831+ };
2832+ let min_offset = f64::min(leftmost, rightmost);
2833+ let max_offset = f64::max(leftmost, rightmost);
2834+ let clamped_offset = view_offset.clamp(min_offset, max_offset);
2835+
2836+ gesture.delta_from_tracker += clamped_offset - view_offset;
2837+ gesture.current_view_offset = clamped_offset;
2838+ }
2839+
2746 pub fn view_offset_gesture_end(&mut self, _cancelled: bool, is_touchpad: Option<bool>) -> bool {2840 pub fn view_offset_gesture_end(&mut self, _cancelled: bool, is_touchpad: Option<bool>) -> bool {
2747 let ViewOffset::Gesture(gesture) = &mut self.view_offset else {2841 let ViewOffset::Gesture(gesture) = &mut self.view_offset else {
2748 return false;2842 return false;
@@ -3228,6 +3322,11 @@ impl<W: LayoutElement> ScrollingSpace<W> {
3228 self.active_column_idx3322 self.active_column_idx
3229 }3323 }
3230 3324
3325+ #[cfg(test)]
3326+ pub(super) fn view_offset(&self) -> &ViewOffset {
3327+ &self.view_offset
3328+ }
3329+
3231 #[cfg(test)]3330 #[cfg(test)]
3232 pub fn verify_invariants(&self, working_area: Rectangle<f64, Logical>) {3331 pub fn verify_invariants(&self, working_area: Rectangle<f64, Logical>) {
3233 assert!(self.view_size.w > 0.);3332 assert!(self.view_size.w > 0.);
src/layout/workspace.rsmodified+28 -0
@@ -1609,6 +1609,34 @@ impl<W: LayoutElement> Workspace<W> {
1609 .view_offset_gesture_end(cancelled, is_touchpad)1609 .view_offset_gesture_end(cancelled, is_touchpad)
1610 }1610 }
1611 1611
1612+ pub fn dnd_scroll_gesture_begin(&mut self) {
1613+ self.scrolling.dnd_scroll_gesture_begin();
1614+ }
1615+
1616+ pub fn dnd_scroll_gesture_scroll(&mut self, pos: Point<f64, Logical>) {
1617+ // Taken from GTK 4.
1618+ const SCROLL_EDGE_SIZE: f64 = 30.;
1619+
1620+ // This working area intentionally does not include extra struts from Options.
1621+ let x = pos.x - self.working_area.loc.x;
1622+ let width = self.working_area.size.w;
1623+ let x = x.clamp(0., width);
1624+
1625+ let delta = if x < SCROLL_EDGE_SIZE {
1626+ -(SCROLL_EDGE_SIZE - x)
1627+ } else if width - x < SCROLL_EDGE_SIZE {
1628+ SCROLL_EDGE_SIZE - (width - x)
1629+ } else {
1630+ 0.
1631+ };
1632+
1633+ self.scrolling.dnd_scroll_gesture_scroll(delta);
1634+ }
1635+
1636+ pub fn dnd_scroll_gesture_update_time(&mut self) {
1637+ self.scrolling.dnd_scroll_gesture_scroll(0.);
1638+ }
1639+
1612 pub fn interactive_resize_begin(&mut self, window: W::Id, edges: ResizeEdge) -> bool {1640 pub fn interactive_resize_begin(&mut self, window: W::Id, edges: ResizeEdge) -> bool {
1613 if self.floating.has_window(&window) {1641 if self.floating.has_window(&window) {
1614 self.floating.interactive_resize_begin(window, edges)1642 self.floating.interactive_resize_begin(window, edges)