forge
Explore
Log in

Don't you dare go Hollow

Add barebones three-finger-swipe workspace switch

Browse files at this commit

e70e660df62d46879f01fc76c55fa09adce9c994Ivan MolodetskikhParent: 0411fd8
Notable omission is velocity tracking.
src/input.rsmodified+37 -0
@@ -631,6 +631,19 @@ impl State {
631 }631 }
632 }632 }
633 InputEvent::GestureSwipeBegin { event } => {633 InputEvent::GestureSwipeBegin { event } => {
634+ if event.fingers() == 3 {
635+ if let Some(output) = self.niri.output_under_cursor() {
636+ self.niri.layout.workspace_switch_gesture_begin(&output);
637+
638+ // FIXME: granular. This one is awkward because this can cancel a gesture on
639+ // multiple other outputs in theory.
640+ self.niri.queue_redraw_all();
641+ }
642+
643+ // We handled this event.
644+ return;
645+ }
646+
634 let serial = SERIAL_COUNTER.next_serial();647 let serial = SERIAL_COUNTER.next_serial();
635 let pointer = self.niri.seat.get_pointer().unwrap();648 let pointer = self.niri.seat.get_pointer().unwrap();
636 pointer.gesture_swipe_begin(649 pointer.gesture_swipe_begin(
@@ -643,6 +656,19 @@ impl State {
643 );656 );
644 }657 }
645 InputEvent::GestureSwipeUpdate { event } => {658 InputEvent::GestureSwipeUpdate { event } => {
659+ let res = self
660+ .niri
661+ .layout
662+ .workspace_switch_gesture_update(event.delta_y());
663+ if let Some(output) = res {
664+ if let Some(output) = output {
665+ self.niri.queue_redraw(output);
666+ }
667+
668+ // We handled this event.
669+ return;
670+ }
671+
646 let pointer = self.niri.seat.get_pointer().unwrap();672 let pointer = self.niri.seat.get_pointer().unwrap();
647 pointer.gesture_swipe_update(673 pointer.gesture_swipe_update(
648 self,674 self,
@@ -653,6 +679,17 @@ impl State {
653 );679 );
654 }680 }
655 InputEvent::GestureSwipeEnd { event } => {681 InputEvent::GestureSwipeEnd { event } => {
682+ let res = self
683+ .niri
684+ .layout
685+ .workspace_switch_gesture_end(event.cancelled());
686+ if let Some(output) = res {
687+ self.niri.queue_redraw(output);
688+
689+ // We handled this event.
690+ return;
691+ }
692+
656 let serial = SERIAL_COUNTER.next_serial();693 let serial = SERIAL_COUNTER.next_serial();
657 let pointer = self.niri.seat.get_pointer().unwrap();694 let pointer = self.niri.seat.get_pointer().unwrap();
658 pointer.gesture_swipe_end(695 pointer.gesture_swipe_end(
src/layout.rsmodified+138 -16
@@ -110,12 +110,26 @@ pub struct Monitor<W: LayoutElement> {
110 workspaces: Vec<Workspace<W>>,110 workspaces: Vec<Workspace<W>>,
111 /// Index of the currently active workspace.111 /// Index of the currently active workspace.
112 active_workspace_idx: usize,112 active_workspace_idx: usize,
113- /// Animation for workspace switching.113+ /// In-progress switch between workspaces.
114- workspace_idx_anim: Option<Animation>,114+ workspace_switch: Option<WorkspaceSwitch>,
115 /// Configurable properties of the layout.115 /// Configurable properties of the layout.
116 options: Rc<Options>,116 options: Rc<Options>,
117 }117 }
118 118
119+#[derive(Debug)]
120+enum WorkspaceSwitch {
121+ Animation(Animation),
122+ Gesture(WorkspaceSwitchGesture),
123+}
124+
125+#[derive(Debug)]
126+struct WorkspaceSwitchGesture {
127+ /// Index of the workspace where the gesture was started.
128+ center_idx: usize,
129+ /// Current, fractional workspace index.
130+ current_idx: f64,
131+}
132+
119 #[derive(Debug)]133 #[derive(Debug)]
120 pub struct Workspace<W: LayoutElement> {134 pub struct Workspace<W: LayoutElement> {
121 /// The original output of this workspace.135 /// The original output of this workspace.
@@ -361,6 +375,23 @@ impl FocusRing {
361 }375 }
362 }376 }
363 377
378+impl WorkspaceSwitch {
379+ fn current_idx(&self) -> f64 {
380+ match self {
381+ WorkspaceSwitch::Animation(anim) => anim.value(),
382+ WorkspaceSwitch::Gesture(gesture) => gesture.current_idx,
383+ }
384+ }
385+
386+ /// Returns `true` if the workspace switch is [`Animation`].
387+ ///
388+ /// [`Animation`]: WorkspaceSwitch::Animation
389+ #[must_use]
390+ fn is_animation(&self) -> bool {
391+ matches!(self, Self::Animation(..))
392+ }
393+}
394+
364 impl ColumnWidth {395 impl ColumnWidth {
365 fn resolve(self, options: &Options, view_width: i32) -> i32 {396 fn resolve(self, options: &Options, view_width: i32) -> i32 {
366 match self {397 match self {
@@ -1138,6 +1169,95 @@ impl<W: LayoutElement> Layout<W> {
1138 }1169 }
1139 }1170 }
1140 }1171 }
1172+
1173+ pub fn workspace_switch_gesture_begin(&mut self, output: &Output) {
1174+ let monitors = match &mut self.monitor_set {
1175+ MonitorSet::Normal { monitors, .. } => monitors,
1176+ MonitorSet::NoOutputs { .. } => unreachable!(),
1177+ };
1178+
1179+ for monitor in monitors {
1180+ // Cancel the gesture on other outputs.
1181+ if &monitor.output != output {
1182+ if let Some(WorkspaceSwitch::Gesture(_)) = monitor.workspace_switch {
1183+ monitor.workspace_switch = None;
1184+ }
1185+ continue;
1186+ }
1187+
1188+ let center_idx = monitor.active_workspace_idx;
1189+ let current_idx = monitor
1190+ .workspace_switch
1191+ .as_ref()
1192+ .map(|s| s.current_idx())
1193+ .unwrap_or(center_idx as f64);
1194+
1195+ let gesture = WorkspaceSwitchGesture {
1196+ center_idx,
1197+ current_idx,
1198+ };
1199+ monitor.workspace_switch = Some(WorkspaceSwitch::Gesture(gesture));
1200+ }
1201+ }
1202+
1203+ pub fn workspace_switch_gesture_update(&mut self, delta_y: f64) -> Option<Option<Output>> {
1204+ let monitors = match &mut self.monitor_set {
1205+ MonitorSet::Normal { monitors, .. } => monitors,
1206+ MonitorSet::NoOutputs { .. } => return None,
1207+ };
1208+
1209+ for monitor in monitors {
1210+ if let Some(WorkspaceSwitch::Gesture(gesture)) = &mut monitor.workspace_switch {
1211+ // Normalize like GNOME Shell's workspace switching.
1212+ let delta_y = -delta_y / 400.;
1213+
1214+ let min = gesture.center_idx.saturating_sub(1) as f64;
1215+ let max = (gesture.center_idx + 1).min(monitor.workspaces.len() - 1) as f64;
1216+ let new_idx = (gesture.current_idx + delta_y).clamp(min, max);
1217+
1218+ if gesture.current_idx == new_idx {
1219+ return Some(None);
1220+ }
1221+
1222+ gesture.current_idx = new_idx;
1223+ return Some(Some(monitor.output.clone()));
1224+ }
1225+ }
1226+
1227+ None
1228+ }
1229+
1230+ pub fn workspace_switch_gesture_end(&mut self, cancelled: bool) -> Option<Output> {
1231+ let monitors = match &mut self.monitor_set {
1232+ MonitorSet::Normal { monitors, .. } => monitors,
1233+ MonitorSet::NoOutputs { .. } => return None,
1234+ };
1235+
1236+ for monitor in monitors {
1237+ if let Some(WorkspaceSwitch::Gesture(gesture)) = &mut monitor.workspace_switch {
1238+ if cancelled {
1239+ monitor.workspace_switch = None;
1240+ return Some(monitor.output.clone());
1241+ }
1242+
1243+ // FIXME: keep track of gesture velocity and use it to compute the final point and
1244+ // to animate to it.
1245+ let current_idx = gesture.current_idx;
1246+ let idx = current_idx.round() as usize;
1247+
1248+ monitor.active_workspace_idx = idx;
1249+ monitor.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new(
1250+ current_idx,
1251+ idx as f64,
1252+ Duration::from_millis(250),
1253+ )));
1254+
1255+ return Some(monitor.output.clone());
1256+ }
1257+ }
1258+
1259+ None
1260+ }
1141 }1261 }
1142 1262
1143 impl Layout<Window> {1263 impl Layout<Window> {
@@ -1173,7 +1293,7 @@ impl<W: LayoutElement> Monitor<W> {
1173 output,1293 output,
1174 workspaces,1294 workspaces,
1175 active_workspace_idx: 0,1295 active_workspace_idx: 0,
1176- workspace_idx_anim: None,1296+ workspace_switch: None,
1177 options,1297 options,
1178 }1298 }
1179 }1299 }
@@ -1188,18 +1308,18 @@ impl<W: LayoutElement> Monitor<W> {
1188 }1308 }
1189 1309
1190 let current_idx = self1310 let current_idx = self
1191- .workspace_idx_anim1311+ .workspace_switch
1192 .as_ref()1312 .as_ref()
1193- .map(|anim| anim.value())1313+ .map(|s| s.current_idx())
1194 .unwrap_or(self.active_workspace_idx as f64);1314 .unwrap_or(self.active_workspace_idx as f64);
1195 1315
1196 self.active_workspace_idx = idx;1316 self.active_workspace_idx = idx;
1197 1317
1198- self.workspace_idx_anim = Some(Animation::new(1318+ self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new(
1199 current_idx,1319 current_idx,
1200 idx as f64,1320 idx as f64,
1201 Duration::from_millis(250),1321 Duration::from_millis(250),
1202- ));1322+ )));
1203 }1323 }
1204 1324
1205 pub fn add_window(&mut self, workspace_idx: usize, window: W, activate: bool) {1325 pub fn add_window(&mut self, workspace_idx: usize, window: W, activate: bool) {
@@ -1222,7 +1342,7 @@ impl<W: LayoutElement> Monitor<W> {
1222 }1342 }
1223 1343
1224 fn clean_up_workspaces(&mut self) {1344 fn clean_up_workspaces(&mut self) {
1225- assert!(self.workspace_idx_anim.is_none());1345+ assert!(self.workspace_switch.is_none());
1226 1346
1227 for idx in (0..self.workspaces.len() - 1).rev() {1347 for idx in (0..self.workspaces.len() - 1).rev() {
1228 if self.active_workspace_idx == idx {1348 if self.active_workspace_idx == idx {
@@ -1330,7 +1450,7 @@ impl<W: LayoutElement> Monitor<W> {
1330 self.add_window(new_idx, window, true);1450 self.add_window(new_idx, window, true);
1331 1451
1332 // Don't animate this action.1452 // Don't animate this action.
1333- self.workspace_idx_anim = None;1453+ self.workspace_switch = None;
1334 }1454 }
1335 1455
1336 pub fn switch_workspace_up(&mut self) {1456 pub fn switch_workspace_up(&mut self) {
@@ -1350,7 +1470,7 @@ impl<W: LayoutElement> Monitor<W> {
1350 self.workspaces.len() - 1,1470 self.workspaces.len() - 1,
1351 ));1471 ));
1352 // Don't animate this action.1472 // Don't animate this action.
1353- self.workspace_idx_anim = None;1473+ self.workspace_switch = None;
1354 }1474 }
1355 1475
1356 pub fn consume_into_column(&mut self) {1476 pub fn consume_into_column(&mut self) {
@@ -1372,10 +1492,10 @@ impl<W: LayoutElement> Monitor<W> {
1372 }1492 }
1373 1493
1374 pub fn advance_animations(&mut self, current_time: Duration, is_active: bool) {1494 pub fn advance_animations(&mut self, current_time: Duration, is_active: bool) {
1375- if let Some(anim) = &mut self.workspace_idx_anim {1495+ if let Some(WorkspaceSwitch::Animation(anim)) = &mut self.workspace_switch {
1376 anim.set_current_time(current_time);1496 anim.set_current_time(current_time);
1377 if anim.is_done() {1497 if anim.is_done() {
1378- self.workspace_idx_anim = None;1498+ self.workspace_switch = None;
1379 self.clean_up_workspaces();1499 self.clean_up_workspaces();
1380 }1500 }
1381 }1501 }
@@ -1386,7 +1506,9 @@ impl<W: LayoutElement> Monitor<W> {
1386 }1506 }
1387 1507
1388 pub fn are_animations_ongoing(&self) -> bool {1508 pub fn are_animations_ongoing(&self) -> bool {
1389- self.workspace_idx_anim.is_some()1509+ self.workspace_switch
1510+ .as_ref()
1511+ .is_some_and(|s| s.is_animation())
1390 || self.workspaces.iter().any(|ws| ws.are_animations_ongoing())1512 || self.workspaces.iter().any(|ws| ws.are_animations_ongoing())
1391 }1513 }
1392 1514
@@ -1421,9 +1543,9 @@ impl Monitor<Window> {
1421 let output_mode = self.output.current_mode().unwrap();1543 let output_mode = self.output.current_mode().unwrap();
1422 let output_size = output_transform.transform_size(output_mode.size);1544 let output_size = output_transform.transform_size(output_mode.size);
1423 1545
1424- match &self.workspace_idx_anim {1546+ match &self.workspace_switch {
1425- Some(anim) => {1547+ Some(switch) => {
1426- let render_idx = anim.value();1548+ let render_idx = switch.current_idx();
1427 let below_idx = render_idx.floor() as usize;1549 let below_idx = render_idx.floor() as usize;
1428 let above_idx = render_idx.ceil() as usize;1550 let above_idx = render_idx.ceil() as usize;
1429 1551