forge
Explore
Log in

Don't you dare go Hollow

Implement scrolling the view during DnD

Browse files at this commit

ca1500ae90f33344fe776898ae11137bf7d1ecc1Ivan MolodetskikhParent: d7f3ca0
DnD is external to the layout, so we just inform it when one is ongoing.
src/handlers/mod.rsmodified+3 -0
@@ -344,6 +344,9 @@ impl ClientDndGrabHandler for State {
344344 fn dropped(&mut self, target: Option<WlSurface>, validated: bool, _seat: Seat<Self>) {
345345 trace!("client dropped, target: {target:?}, validated: {validated}");
346346
347+ // End DnD before activating a specific window below so that it takes precedence.
348+ self.niri.layout.dnd_end();
349+
347350 // Activate the target output, since that's how Firefox drag-tab-into-new-window works for
348351 // example. On successful drop, additionally activate the target window.
349352 let mut activate_output = true;
src/input/mod.rsmodified+37 -0
@@ -32,6 +32,7 @@ use smithay::output::Output;
3232 use smithay::utils::{Logical, Point, Rectangle, Transform, SERIAL_COUNTER};
3333 use smithay::wayland::keyboard_shortcuts_inhibit::KeyboardShortcutsInhibitor;
3434 use smithay::wayland::pointer_constraints::{with_pointer_constraint, PointerConstraint};
35+use smithay::wayland::selection::data_device::DnDGrab;
3536 use smithay::wayland::tablet_manager::{TabletDescriptor, TabletSeatTrait};
3637 use touch_move_grab::TouchMoveGrab;
3738
@@ -1888,6 +1889,18 @@ impl State {
18881889 // Activate a new confinement if necessary.
18891890 self.niri.maybe_activate_pointer_constraint();
18901891
1892+ // Inform the layout of an ongoing DnD operation.
1893+ let mut is_dnd_grab = false;
1894+ pointer.with_grab(|_, grab| {
1895+ is_dnd_grab = grab.as_any().downcast_ref::<DnDGrab<Self>>().is_some();
1896+ });
1897+ if is_dnd_grab {
1898+ if let Some((output, pos_within_output)) = self.niri.output_under(new_pos) {
1899+ let output = output.clone();
1900+ self.niri.layout.dnd_update(output, pos_within_output);
1901+ }
1902+ }
1903+
18911904 // Redraw to update the cursor position.
18921905 // FIXME: redraw only outputs overlapping the cursor.
18931906 self.niri.queue_redraw_all();
@@ -1950,6 +1963,18 @@ impl State {
19501963 // We moved the regular pointer, so show it now.
19511964 self.niri.tablet_cursor_location = None;
19521965
1966+ // Inform the layout of an ongoing DnD operation.
1967+ let mut is_dnd_grab = false;
1968+ pointer.with_grab(|_, grab| {
1969+ is_dnd_grab = grab.as_any().downcast_ref::<DnDGrab<Self>>().is_some();
1970+ });
1971+ if is_dnd_grab {
1972+ if let Some((output, pos_within_output)) = self.niri.output_under(pos) {
1973+ let output = output.clone();
1974+ self.niri.layout.dnd_update(output, pos_within_output);
1975+ }
1976+ }
1977+
19531978 // Redraw to update the cursor position.
19541979 // FIXME: redraw only outputs overlapping the cursor.
19551980 self.niri.queue_redraw_all();
@@ -2912,6 +2937,18 @@ impl State {
29122937 time: evt.time_msec(),
29132938 },
29142939 );
2940+
2941+ // Inform the layout of an ongoing DnD operation.
2942+ let mut is_dnd_grab = false;
2943+ handle.with_grab(|_, grab| {
2944+ is_dnd_grab = grab.as_any().downcast_ref::<DnDGrab<Self>>().is_some();
2945+ });
2946+ if is_dnd_grab {
2947+ if let Some((output, pos_within_output)) = self.niri.output_under(touch_location) {
2948+ let output = output.clone();
2949+ self.niri.layout.dnd_update(output, pos_within_output);
2950+ }
2951+ }
29152952 }
29162953 fn on_touch_frame<I: InputBackend>(&mut self, _evt: I::TouchFrameEvent) {
29172954 let Some(handle) = self.niri.seat.get_touch() else {
src/layout/mod.rsmodified+78 -22
@@ -278,6 +278,8 @@ pub struct Layout<W: LayoutElement> {
278278 last_active_workspace_id: HashMap<String, WorkspaceId>,
279279 /// Ongoing interactive move.
280280 interactive_move: Option<InteractiveMoveState<W>>,
281+ /// Ongoing drag-and-drop operation.
282+ dnd: Option<DndData>,
281283 /// Clock for driving animations.
282284 clock: Clock,
283285 /// Time that we last updated render elements for.
@@ -400,6 +402,14 @@ struct InteractiveMoveData<W: LayoutElement> {
400402 pub(self) pointer_ratio_within_window: (f64, f64),
401403 }
402404
405+#[derive(Debug)]
406+pub struct DndData {
407+ /// Output where the pointer is currently located.
408+ output: Output,
409+ /// Current pointer position within output.
410+ pointer_pos_within_output: Point<f64, Logical>,
411+}
412+
403413 #[derive(Debug, Clone, Copy)]
404414 pub struct InteractiveResizeData {
405415 pub(self) edges: ResizeEdge,
@@ -600,6 +610,7 @@ impl<W: LayoutElement> Layout<W> {
600610 is_active: true,
601611 last_active_workspace_id: HashMap::new(),
602612 interactive_move: None,
613+ dnd: None,
603614 clock,
604615 update_render_elements_time: Duration::ZERO,
605616 options: Rc::new(options),
@@ -622,6 +633,7 @@ impl<W: LayoutElement> Layout<W> {
622633 is_active: true,
623634 last_active_workspace_id: HashMap::new(),
624635 interactive_move: None,
636+ dnd: None,
625637 clock,
626638 update_render_elements_time: Duration::ZERO,
627639 options: opts,
@@ -2518,7 +2530,7 @@ impl<W: LayoutElement> Layout<W> {
25182530 workspace.verify_invariants(move_win_id.as_ref());
25192531
25202532 let has_view_offset_gesture = workspace.scrolling().view_offset().is_gesture();
2521- if self.interactive_move.is_some() {
2533+ if self.dnd.is_some() || self.interactive_move.is_some() {
25222534 // We'd like to check that all workspaces have the gesture here, furthermore we
25232535 // want to check that they have the gesture only if the interactive move
25242536 // targets the scrolling layout. However, we cannot do that because we start
@@ -2545,23 +2557,30 @@ impl<W: LayoutElement> Layout<W> {
25452557 pub fn advance_animations(&mut self) {
25462558 let _span = tracy_client::span!("Layout::advance_animations");
25472559
2560+ let mut dnd_scroll = None;
2561+ if let Some(dnd) = &self.dnd {
2562+ dnd_scroll = Some((dnd.output.clone(), dnd.pointer_pos_within_output));
2563+ }
2564+
25482565 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
25492566 move_.tile.advance_animations();
25502567
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- }
2568+ if !move_.is_floating && dnd_scroll.is_none() {
2569+ dnd_scroll = Some((move_.output.clone(), move_.pointer_pos_within_output));
2570+ }
2571+ }
2572+
2573+ // Scroll the view if needed.
2574+ if let Some((output, pos_within_output)) = dnd_scroll {
2575+ if let Some(mon) = self.monitor_for_output_mut(&output) {
2576+ if let Some((ws, offset)) = mon.workspace_under(pos_within_output) {
2577+ let ws_id = ws.id();
2578+ let ws = mon
2579+ .workspaces
2580+ .iter_mut()
2581+ .find(|ws| ws.id() == ws_id)
2582+ .unwrap();
2583+ ws.dnd_scroll_gesture_scroll(pos_within_output - offset);
25652584 }
25662585 }
25672586 }
@@ -2581,6 +2600,13 @@ impl<W: LayoutElement> Layout<W> {
25812600 }
25822601
25832602 pub fn are_animations_ongoing(&self, output: Option<&Output>) -> bool {
2603+ // Keep advancing animations if we might need to scroll the view.
2604+ if let Some(dnd) = &self.dnd {
2605+ if output.map_or(true, |output| *output == dnd.output) {
2606+ return true;
2607+ }
2608+ }
2609+
25842610 if let Some(InteractiveMoveState::Moving(move_)) = &self.interactive_move {
25852611 if output.map_or(true, |output| *output == move_.output) {
25862612 if move_.tile.are_animations_ongoing() {
@@ -3991,6 +4017,33 @@ impl<W: LayoutElement> Layout<W> {
39914017 move_.output == *output
39924018 }
39934019
4020+ pub fn dnd_update(&mut self, output: Output, pointer_pos_within_output: Point<f64, Logical>) {
4021+ let begin_gesture = self.dnd.is_none();
4022+
4023+ self.dnd = Some(DndData {
4024+ output,
4025+ pointer_pos_within_output,
4026+ });
4027+
4028+ if begin_gesture {
4029+ for ws in self.workspaces_mut() {
4030+ ws.dnd_scroll_gesture_begin();
4031+ }
4032+ }
4033+ }
4034+
4035+ pub fn dnd_end(&mut self) {
4036+ if self.dnd.is_none() {
4037+ return;
4038+ }
4039+
4040+ self.dnd = None;
4041+
4042+ for ws in self.workspaces_mut() {
4043+ ws.view_offset_gesture_end(false, None);
4044+ }
4045+ }
4046+
39944047 pub fn interactive_resize_begin(&mut self, window: W::Id, edges: ResizeEdge) -> bool {
39954048 match &mut self.monitor_set {
39964049 MonitorSet::Normal { monitors, .. } => {
@@ -4349,7 +4402,8 @@ impl<W: LayoutElement> Layout<W> {
43494402
43504403 self.is_active = is_active;
43514404
4352- let mut ongoing_scrolling_dnd = None;
4405+ let mut ongoing_scrolling_dnd = self.dnd.is_some().then_some(true);
4406+
43534407 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
43544408 let win = move_.tile.window_mut();
43554409
@@ -4364,15 +4418,17 @@ impl<W: LayoutElement> Layout<W> {
43644418 win.send_pending_configure();
43654419 win.refresh();
43664420
4367- ongoing_scrolling_dnd = Some(!move_.is_floating);
4421+ ongoing_scrolling_dnd.get_or_insert(!move_.is_floating);
43684422 } else if let Some(InteractiveMoveState::Starting { window_id, .. }) =
43694423 &self.interactive_move
43704424 {
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));
4425+ ongoing_scrolling_dnd.get_or_insert_with(|| {
4426+ let (_, _, ws) = self
4427+ .workspaces()
4428+ .find(|(_, _, ws)| ws.has_window(window_id))
4429+ .unwrap();
4430+ !ws.is_floating(window_id)
4431+ });
43764432 }
43774433
43784434 match &mut self.monitor_set {
src/layout/tests.rsmodified+19 -0
@@ -589,6 +589,15 @@ enum Op {
589589 #[proptest(strategy = "1..=5usize")]
590590 window: usize,
591591 },
592+ DndUpdate {
593+ #[proptest(strategy = "1..=5usize")]
594+ output_idx: usize,
595+ #[proptest(strategy = "-20000f64..20000f64")]
596+ px: f64,
597+ #[proptest(strategy = "-20000f64..20000f64")]
598+ py: f64,
599+ },
600+ DndEnd,
592601 InteractiveResizeBegin {
593602 #[proptest(strategy = "1..=5usize")]
594603 window: usize,
@@ -1353,6 +1362,16 @@ impl Op {
13531362 Op::InteractiveMoveEnd { window } => {
13541363 layout.interactive_move_end(&window);
13551364 }
1365+ Op::DndUpdate { output_idx, px, py } => {
1366+ let name = format!("output{output_idx}");
1367+ let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
1368+ return;
1369+ };
1370+ layout.dnd_update(output, Point::from((px, py)));
1371+ }
1372+ Op::DndEnd => {
1373+ layout.dnd_end();
1374+ }
13561375 Op::InteractiveResizeBegin { window, edges } => {
13571376 layout.interactive_resize_begin(window, edges);
13581377 }