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-
31 use smithay::backend::input::ButtonState;
42 use smithay::desktop::Window;
53 use smithay::input::pointer::{
@@ -17,7 +15,6 @@ pub struct MoveGrab {
1715 start_data: PointerGrabStartData<State>,
1816 last_location: Point<f64, Logical>,
1917 window: Window,
20- is_moving: bool,
2118 }
2219
2320 impl MoveGrab {
@@ -26,7 +23,6 @@ impl MoveGrab {
2623 last_location: start_data.location,
2724 start_data,
2825 window,
29- is_moving: false,
3026 }
3127 }
3228
@@ -64,14 +60,6 @@ impl PointerGrab<State> for MoveGrab {
6460 pos_within_output,
6561 );
6662 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- }
7563 // FIXME: only redraw the previous and the new output.
7664 data.niri.queue_redraw_all();
7765 return;
@@ -104,25 +92,6 @@ impl PointerGrab<State> for MoveGrab {
10492 ) {
10593 handle.button(data, event);
10694
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-
12695 // When moving with the left button, right toggles floating, and vice versa.
12796 let toggle_floating_button = if self.start_data.button == 0x110 {
12897 0x111
src/layout/mod.rsmodified+110 -12
@@ -1087,6 +1087,12 @@ impl<W: LayoutElement> Layout<W> {
10871087 else {
10881088 unreachable!()
10891089 };
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+
10901096 return Some(RemovedTile {
10911097 tile: move_.tile,
10921098 width: move_.width,
@@ -2372,6 +2378,8 @@ impl<W: LayoutElement> Layout<W> {
23722378 assert!(primary_idx < monitors.len());
23732379 assert!(active_monitor_idx < monitors.len());
23742380
2381+ let mut saw_view_offset_gesture = false;
2382+
23752383 for (idx, monitor) in monitors.iter().enumerate() {
23762384 assert!(
23772385 !monitor.workspaces.is_empty(),
@@ -2508,6 +2516,28 @@ impl<W: LayoutElement> Layout<W> {
25082516 }
25092517
25102518 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;
25112541 }
25122542 }
25132543 }
@@ -2517,6 +2547,23 @@ impl<W: LayoutElement> Layout<W> {
25172547
25182548 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
25192549 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+ }
25202567 }
25212568
25222569 match &mut self.monitor_set {
@@ -2535,11 +2582,15 @@ impl<W: LayoutElement> Layout<W> {
25352582
25362583 pub fn are_animations_ongoing(&self, output: Option<&Output>) -> bool {
25372584 if let Some(InteractiveMoveState::Moving(move_)) = &self.interactive_move {
2538- #[allow(clippy::collapsible_if)]
25392585 if output.map_or(true, |output| *output == move_.output) {
25402586 if move_.tile.are_animations_ongoing() {
25412587 return true;
25422588 }
2589+
2590+ // Keep advancing animations if we might need to scroll the view.
2591+ if !move_.is_floating {
2592+ return true;
2593+ }
25432594 }
25442595 }
25452596
@@ -2919,6 +2970,7 @@ impl<W: LayoutElement> Layout<W> {
29192970
29202971 win.request_size_once(size, true);
29212972 }
2973+
29222974 return;
29232975 }
29242976 }
@@ -3516,6 +3568,7 @@ impl<W: LayoutElement> Layout<W> {
35163568 return false;
35173569 }
35183570
3571+ let is_floating = ws.is_floating(&window_id);
35193572 let (tile, tile_offset, _visible) = ws
35203573 .tiles_with_render_positions()
35213574 .find(|(tile, _, _)| tile.window().id() == &window_id)
@@ -3537,6 +3590,13 @@ impl<W: LayoutElement> Layout<W> {
35373590 pointer_ratio_within_window,
35383591 });
35393592
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+
35403600 true
35413601 }
35423602
@@ -3744,14 +3804,25 @@ impl<W: LayoutElement> Layout<W> {
37443804 unreachable!()
37453805 };
37463806
3747- let tile = self
3748- .workspaces_mut()
3749- .flat_map(|ws| ws.tiles_mut())
3750- .find(|tile| *tile.window().id() == window_id)
3751- .unwrap();
3752- let offset = tile.interactive_move_offset;
3753- tile.interactive_move_offset = Point::from((0., 0.));
3754- tile.animate_move_from(offset);
3807+ for ws in self.workspaces_mut() {
3808+ if let Some(tile) = ws.tiles_mut().find(|tile| *tile.window().id() == window_id)
3809+ {
3810+ let offset = tile.interactive_move_offset;
3811+ tile.interactive_move_offset = Point::from((0., 0.));
3812+ tile.animate_move_from(offset);
3813+ }
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+ }
37553826
37563827 return;
37573828 }
@@ -3766,6 +3837,13 @@ impl<W: LayoutElement> Layout<W> {
37663837 unreachable!()
37673838 };
37683839
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+
37693847 match &mut self.monitor_set {
37703848 MonitorSet::Normal {
37713849 monitors,
@@ -4271,6 +4349,7 @@ impl<W: LayoutElement> Layout<W> {
42714349
42724350 self.is_active = is_active;
42734351
4352+ let mut ongoing_scrolling_dnd = None;
42744353 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
42754354 let win = move_.tile.window_mut();
42764355
@@ -4284,6 +4363,16 @@ impl<W: LayoutElement> Layout<W> {
42844363
42854364 win.send_pending_configure();
42864365 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));
42874376 }
42884377
42894378 match &mut self.monitor_set {
@@ -4299,9 +4388,18 @@ impl<W: LayoutElement> Layout<W> {
42994388 for (ws_idx, ws) in mon.workspaces.iter_mut().enumerate() {
43004389 ws.refresh(is_active);
43014390
4302- // Cancel the view offset gesture after workspace switches, moves, etc.
4303- if ws_idx != mon.active_workspace_idx {
4304- ws.view_offset_gesture_end(false, None);
4391+ if let Some(is_scrolling) = ongoing_scrolling_dnd {
4392+ // Lock or unlock the view for scrolling interactive move.
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+ }
43054403 }
43064404 }
43074405 }
src/layout/scrolling.rsmodified+102 -3
@@ -123,7 +123,7 @@ struct ColumnData {
123123 }
124124
125125 #[derive(Debug)]
126-enum ViewOffset {
126+pub(super) enum ViewOffset {
127127 /// The view offset is static.
128128 Static(f64),
129129 /// The view offset is animating.
@@ -133,7 +133,7 @@ enum ViewOffset {
133133 }
134134
135135 #[derive(Debug)]
136-struct ViewGesture {
136+pub(super) struct ViewGesture {
137137 current_view_offset: f64,
138138 tracker: SwipeTracker,
139139 delta_from_tracker: f64,
@@ -141,6 +141,10 @@ struct ViewGesture {
141141 stationary_view_offset: f64,
142142 /// Whether the gesture is controlled by the touchpad.
143143 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>,
144148 }
145149
146150 #[derive(Debug)]
@@ -324,6 +328,17 @@ impl<W: LayoutElement> ScrollingSpace<W> {
324328 }
325329 }
326330
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+
327342 for col in &mut self.columns {
328343 col.advance_animations();
329344 }
@@ -2711,10 +2726,34 @@ impl<W: LayoutElement> ScrollingSpace<W> {
27112726 delta_from_tracker: self.view_offset.current(),
27122727 stationary_view_offset: self.view_offset.stationary(),
27132728 is_touchpad,
2729+ dnd_last_event_time: None,
27142730 };
27152731 self.view_offset = ViewOffset::Gesture(gesture);
27162732 }
27172733
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+
27182757 pub fn view_offset_gesture_update(
27192758 &mut self,
27202759 delta_x: f64,
@@ -2725,7 +2764,7 @@ impl<W: LayoutElement> ScrollingSpace<W> {
27252764 return None;
27262765 };
27272766
2728- if gesture.is_touchpad != is_touchpad {
2767+ if gesture.is_touchpad != is_touchpad || gesture.dnd_last_event_time.is_some() {
27292768 return None;
27302769 }
27312770
@@ -2743,6 +2782,61 @@ impl<W: LayoutElement> ScrollingSpace<W> {
27432782 Some(true)
27442783 }
27452784
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+
27462840 pub fn view_offset_gesture_end(&mut self, _cancelled: bool, is_touchpad: Option<bool>) -> bool {
27472841 let ViewOffset::Gesture(gesture) = &mut self.view_offset else {
27482842 return false;
@@ -3228,6 +3322,11 @@ impl<W: LayoutElement> ScrollingSpace<W> {
32283322 self.active_column_idx
32293323 }
32303324
3325+ #[cfg(test)]
3326+ pub(super) fn view_offset(&self) -> &ViewOffset {
3327+ &self.view_offset
3328+ }
3329+
32313330 #[cfg(test)]
32323331 pub fn verify_invariants(&self, working_area: Rectangle<f64, Logical>) {
32333332 assert!(self.view_size.w > 0.);
src/layout/workspace.rsmodified+28 -0
@@ -1609,6 +1609,34 @@ impl<W: LayoutElement> Workspace<W> {
16091609 .view_offset_gesture_end(cancelled, is_touchpad)
16101610 }
16111611
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+
16121640 pub fn interactive_resize_begin(&mut self, window: W::Id, edges: ResizeEdge) -> bool {
16131641 if self.floating.has_window(&window) {
16141642 self.floating.interactive_resize_begin(window, edges)