forge
Explore
Log in

Don't you dare go Hollow

Move workspace gesture into monitor & fix missing workspace cleanup

Browse files at this commit

28977d1d3fd7883a7f757a1ba4e636bdacf9d0beIvan MolodetskikhParent: ba10bab
src/layout/mod.rsmodified+35 -48
@@ -49,10 +49,9 @@ use smithay::utils::{Logical, Point, Rectangle, Scale, Size, Transform};
49 use smithay::wayland::compositor::{send_surface_state, with_states};49 use smithay::wayland::compositor::{send_surface_state, with_states};
50 use smithay::wayland::shell::xdg::SurfaceCachedState;50 use smithay::wayland::shell::xdg::SurfaceCachedState;
51 51
52+use self::monitor::Monitor;
52 pub use self::monitor::MonitorRenderElement;53 pub use self::monitor::MonitorRenderElement;
53-use self::monitor::{Monitor, WorkspaceSwitch, WorkspaceSwitchGesture};
54 use self::workspace::{compute_working_area, Column, ColumnWidth, OutputId, Workspace};54 use self::workspace::{compute_working_area, Column, ColumnWidth, OutputId, Workspace};
55-use crate::animation::Animation;
56 use crate::niri::WindowOffscreenId;55 use crate::niri::WindowOffscreenId;
57 use crate::niri_render_elements;56 use crate::niri_render_elements;
58 use crate::render_helpers::renderer::NiriRenderer;57 use crate::render_helpers::renderer::NiriRenderer;
@@ -1222,6 +1221,8 @@ impl<W: LayoutElement> Layout<W> {
1222 1221
1223 #[cfg(test)]1222 #[cfg(test)]
1224 fn verify_invariants(&self) {1223 fn verify_invariants(&self) {
1224+ use crate::layout::monitor::WorkspaceSwitch;
1225+
1225 let (monitors, &primary_idx, &active_monitor_idx) = match &self.monitor_set {1226 let (monitors, &primary_idx, &active_monitor_idx) = match &self.monitor_set {
1226 MonitorSet::Normal {1227 MonitorSet::Normal {
1227 monitors,1228 monitors,
@@ -1594,24 +1595,11 @@ impl<W: LayoutElement> Layout<W> {
1594 for monitor in monitors {1595 for monitor in monitors {
1595 // Cancel the gesture on other outputs.1596 // Cancel the gesture on other outputs.
1596 if &monitor.output != output {1597 if &monitor.output != output {
1597- if let Some(WorkspaceSwitch::Gesture(_)) = monitor.workspace_switch {1598+ monitor.workspace_switch_gesture_end(true);
1598- monitor.workspace_switch = None;
1599- }
1600 continue;1599 continue;
1601 }1600 }
1602 1601
1603- let center_idx = monitor.active_workspace_idx;1602+ monitor.workspace_switch_gesture_begin();
1604- let current_idx = monitor
1605- .workspace_switch
1606- .as_ref()
1607- .map(|s| s.current_idx())
1608- .unwrap_or(center_idx as f64);
1609-
1610- let gesture = WorkspaceSwitchGesture {
1611- center_idx,
1612- current_idx,
1613- };
1614- monitor.workspace_switch = Some(WorkspaceSwitch::Gesture(gesture));
1615 }1603 }
1616 }1604 }
1617 1605
@@ -1622,20 +1610,12 @@ impl<W: LayoutElement> Layout<W> {
1622 };1610 };
1623 1611
1624 for monitor in monitors {1612 for monitor in monitors {
1625- if let Some(WorkspaceSwitch::Gesture(gesture)) = &mut monitor.workspace_switch {1613+ if let Some(refresh) = monitor.workspace_switch_gesture_update(delta_y) {
1626- // Normalize like GNOME Shell's workspace switching.1614+ if refresh {
1627- let delta_y = delta_y / 400.;1615+ return Some(Some(monitor.output.clone()));
1628-1616+ } else {
1629- let min = gesture.center_idx.saturating_sub(1) as f64;
1630- let max = (gesture.center_idx + 1).min(monitor.workspaces.len() - 1) as f64;
1631- let new_idx = (gesture.current_idx + delta_y).clamp(min, max);
1632-
1633- if gesture.current_idx == new_idx {
1634 return Some(None);1617 return Some(None);
1635 }1618 }
1636-
1637- gesture.current_idx = new_idx;
1638- return Some(Some(monitor.output.clone()));
1639 }1619 }
1640 }1620 }
1641 1621
@@ -1649,25 +1629,7 @@ impl<W: LayoutElement> Layout<W> {
1649 };1629 };
1650 1630
1651 for monitor in monitors {1631 for monitor in monitors {
1652- if let Some(WorkspaceSwitch::Gesture(gesture)) = &mut monitor.workspace_switch {1632+ if monitor.workspace_switch_gesture_end(cancelled) {
1653- if cancelled {
1654- monitor.workspace_switch = None;
1655- return Some(monitor.output.clone());
1656- }
1657-
1658- // FIXME: keep track of gesture velocity and use it to compute the final point and
1659- // to animate to it.
1660- let current_idx = gesture.current_idx;
1661- let idx = current_idx.round() as usize;
1662-
1663- monitor.active_workspace_idx = idx;
1664- monitor.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new(
1665- current_idx,
1666- idx as f64,
1667- self.options.animations.workspace_switch,
1668- niri_config::Animation::default_workspace_switch(),
1669- )));
1670-
1671 return Some(monitor.output.clone());1633 return Some(monitor.output.clone());
1672 }1634 }
1673 }1635 }
@@ -2072,6 +2034,17 @@ mod tests {
2072 delta: f64,2034 delta: f64,
2073 },2035 },
2074 ViewOffsetGestureEnd,2036 ViewOffsetGestureEnd,
2037+ WorkspaceSwitchGestureBegin {
2038+ #[proptest(strategy = "1..=5usize")]
2039+ output_idx: usize,
2040+ },
2041+ WorkspaceSwitchGestureUpdate {
2042+ #[proptest(strategy = "-400f64..400f64")]
2043+ delta: f64,
2044+ },
2045+ WorkspaceSwitchGestureEnd {
2046+ cancelled: bool,
2047+ },
2075 }2048 }
2076 2049
2077 impl Op {2050 impl Op {
@@ -2322,6 +2295,20 @@ mod tests {
2322 // We don't handle cancels in this gesture.2295 // We don't handle cancels in this gesture.
2323 layout.view_offset_gesture_end(false);2296 layout.view_offset_gesture_end(false);
2324 }2297 }
2298+ Op::WorkspaceSwitchGestureBegin { output_idx: id } => {
2299+ let name = format!("output{id}");
2300+ let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
2301+ return;
2302+ };
2303+
2304+ layout.workspace_switch_gesture_begin(&output);
2305+ }
2306+ Op::WorkspaceSwitchGestureUpdate { delta } => {
2307+ layout.workspace_switch_gesture_update(delta);
2308+ }
2309+ Op::WorkspaceSwitchGestureEnd { cancelled } => {
2310+ layout.workspace_switch_gesture_end(cancelled);
2311+ }
2325 }2312 }
2326 }2313 }
2327 }2314 }
src/layout/monitor.rsmodified+62 -0
@@ -691,4 +691,66 @@ impl<W: LayoutElement> Monitor<W> {
691 }691 }
692 }692 }
693 }693 }
694+
695+ pub fn workspace_switch_gesture_begin(&mut self) {
696+ let center_idx = self.active_workspace_idx;
697+ let current_idx = self
698+ .workspace_switch
699+ .as_ref()
700+ .map(|s| s.current_idx())
701+ .unwrap_or(center_idx as f64);
702+
703+ let gesture = WorkspaceSwitchGesture {
704+ center_idx,
705+ current_idx,
706+ };
707+ self.workspace_switch = Some(WorkspaceSwitch::Gesture(gesture));
708+ }
709+
710+ pub fn workspace_switch_gesture_update(&mut self, delta_y: f64) -> Option<bool> {
711+ let Some(WorkspaceSwitch::Gesture(gesture)) = &mut self.workspace_switch else {
712+ return None;
713+ };
714+
715+ // Normalize like GNOME Shell's workspace switching.
716+ let delta_y = delta_y / 400.;
717+
718+ let min = gesture.center_idx.saturating_sub(1) as f64;
719+ let max = (gesture.center_idx + 1).min(self.workspaces.len() - 1) as f64;
720+ let new_idx = (gesture.current_idx + delta_y).clamp(min, max);
721+
722+ if gesture.current_idx == new_idx {
723+ return Some(false);
724+ }
725+
726+ gesture.current_idx = new_idx;
727+ Some(true)
728+ }
729+
730+ pub fn workspace_switch_gesture_end(&mut self, cancelled: bool) -> bool {
731+ let Some(WorkspaceSwitch::Gesture(gesture)) = &mut self.workspace_switch else {
732+ return false;
733+ };
734+
735+ if cancelled {
736+ self.workspace_switch = None;
737+ self.clean_up_workspaces();
738+ return true;
739+ }
740+
741+ // FIXME: keep track of gesture velocity and use it to compute the final point and to
742+ // animate to it.
743+ let current_idx = gesture.current_idx;
744+ let idx = current_idx.round() as usize;
745+
746+ self.active_workspace_idx = idx;
747+ self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new(
748+ current_idx,
749+ idx as f64,
750+ self.options.animations.workspace_switch,
751+ niri_config::Animation::default_workspace_switch(),
752+ )));
753+
754+ true
755+ }
694 }756 }