forge
Explore
Log in

Don't you dare go Hollow

Add actions to move the active workspace to another monitor

Browse files at this commit

e51268a39eeffd56d016a8d25dc98a40ff045a9cAndreas StührkParent: 0a715ce
niri-config/src/lib.rsmodified+4 -0
@@ -490,6 +490,10 @@ pub enum Action {
490 SetColumnWidth(#[knuffel(argument, str)] SizeChange),490 SetColumnWidth(#[knuffel(argument, str)] SizeChange),
491 SwitchLayout(#[knuffel(argument)] LayoutAction),491 SwitchLayout(#[knuffel(argument)] LayoutAction),
492 ShowHotkeyOverlay,492 ShowHotkeyOverlay,
493+ MoveWorkspaceToMonitorLeft,
494+ MoveWorkspaceToMonitorRight,
495+ MoveWorkspaceToMonitorDown,
496+ MoveWorkspaceToMonitorUp,
493 }497 }
494 498
495 #[derive(Debug, Clone, Copy, PartialEq)]499 #[derive(Debug, Clone, Copy, PartialEq)]
resources/default-config.kdlmodified+4 -0
@@ -275,6 +275,10 @@ binds {
275 // Mod+Shift+Ctrl+Left { move-window-to-monitor-left; }275 // Mod+Shift+Ctrl+Left { move-window-to-monitor-left; }
276 // ...276 // ...
277 277
278+ // And you can also move a whole workspace to another monitor:
279+ // Mod+Shift+Ctrl+Left { move-workspace-to-monitor-left; }
280+ // ...
281+
278 Mod+Page_Down { focus-workspace-down; }282 Mod+Page_Down { focus-workspace-down; }
279 Mod+Page_Up { focus-workspace-up; }283 Mod+Page_Up { focus-workspace-up; }
280 Mod+U { focus-workspace-down; }284 Mod+U { focus-workspace-down; }
src/input.rsmodified+24 -0
@@ -609,6 +609,30 @@ impl State {
609 self.niri.queue_redraw_all();609 self.niri.queue_redraw_all();
610 }610 }
611 }611 }
612+ Action::MoveWorkspaceToMonitorLeft => {
613+ if let Some(output) = self.niri.output_left() {
614+ self.niri.layout.move_workspace_to_output(&output);
615+ self.move_cursor_to_output(&output);
616+ }
617+ }
618+ Action::MoveWorkspaceToMonitorRight => {
619+ if let Some(output) = self.niri.output_right() {
620+ self.niri.layout.move_workspace_to_output(&output);
621+ self.move_cursor_to_output(&output);
622+ }
623+ }
624+ Action::MoveWorkspaceToMonitorDown => {
625+ if let Some(output) = self.niri.output_down() {
626+ self.niri.layout.move_workspace_to_output(&output);
627+ self.move_cursor_to_output(&output);
628+ }
629+ }
630+ Action::MoveWorkspaceToMonitorUp => {
631+ if let Some(output) = self.niri.output_up() {
632+ self.niri.layout.move_workspace_to_output(&output);
633+ self.move_cursor_to_output(&output);
634+ }
635+ }
612 }636 }
613 }637 }
614 638
src/layout/mod.rsmodified+82 -0
@@ -1335,6 +1335,42 @@ impl<W: LayoutElement> Layout<W> {
1335 }1335 }
1336 }1336 }
1337 1337
1338+ pub fn move_workspace_to_output(&mut self, output: &Output) {
1339+ let MonitorSet::Normal {
1340+ monitors,
1341+ active_monitor_idx,
1342+ ..
1343+ } = &mut self.monitor_set
1344+ else {
1345+ return;
1346+ };
1347+
1348+ let current = &mut monitors[*active_monitor_idx];
1349+ if current.active_workspace_idx == current.workspaces.len() - 1 {
1350+ // Insert a new empty workspace.
1351+ let ws = Workspace::new(current.output.clone(), current.options.clone());
1352+ current.workspaces.push(ws);
1353+ }
1354+ let mut ws = current.workspaces.remove(current.active_workspace_idx);
1355+ current.active_workspace_idx = current.active_workspace_idx.saturating_sub(1);
1356+ current.workspace_switch = None;
1357+ current.clean_up_workspaces();
1358+
1359+ ws.set_output(Some(output.clone()));
1360+ ws.original_output = OutputId::new(output);
1361+
1362+ let target_idx = monitors
1363+ .iter()
1364+ .position(|mon| &mon.output == output)
1365+ .unwrap();
1366+ let target = &mut monitors[target_idx];
1367+ target.workspaces.insert(target.active_workspace_idx, ws);
1368+ target.workspace_switch = None;
1369+ target.clean_up_workspaces();
1370+
1371+ *active_monitor_idx = target_idx;
1372+ }
1373+
1338 pub fn set_fullscreen(&mut self, window: &W, is_fullscreen: bool) {1374 pub fn set_fullscreen(&mut self, window: &W, is_fullscreen: bool) {
1339 match &mut self.monitor_set {1375 match &mut self.monitor_set {
1340 MonitorSet::Normal { monitors, .. } => {1376 MonitorSet::Normal { monitors, .. } => {
@@ -1737,6 +1773,7 @@ mod tests {
1737 SetColumnWidth(#[proptest(strategy = "arbitrary_size_change()")] SizeChange),1773 SetColumnWidth(#[proptest(strategy = "arbitrary_size_change()")] SizeChange),
1738 SetWindowHeight(#[proptest(strategy = "arbitrary_size_change()")] SizeChange),1774 SetWindowHeight(#[proptest(strategy = "arbitrary_size_change()")] SizeChange),
1739 Communicate(#[proptest(strategy = "1..=5usize")] usize),1775 Communicate(#[proptest(strategy = "1..=5usize")] usize),
1776+ MoveWorkspaceToOutput(#[proptest(strategy = "1..=5u8")] u8),
1740 }1777 }
1741 1778
1742 impl Op {1779 impl Op {
@@ -1910,6 +1947,14 @@ mod tests {
1910 layout.update_window(&win);1947 layout.update_window(&win);
1911 }1948 }
1912 }1949 }
1950+ Op::MoveWorkspaceToOutput(id) => {
1951+ let name = format!("output{id}");
1952+ let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
1953+ return;
1954+ };
1955+
1956+ layout.move_workspace_to_output(&output);
1957+ }
1913 }1958 }
1914 }1959 }
1915 }1960 }
@@ -1996,6 +2041,7 @@ mod tests {
1996 Op::MoveWindowDownOrToWorkspaceDown,2041 Op::MoveWindowDownOrToWorkspaceDown,
1997 Op::MoveWindowUp,2042 Op::MoveWindowUp,
1998 Op::MoveWindowUpOrToWorkspaceUp,2043 Op::MoveWindowUpOrToWorkspaceUp,
2044+ Op::MoveWorkspaceToOutput(1),
1999 ];2045 ];
2000 2046
2001 for third in every_op {2047 for third in every_op {
@@ -2410,6 +2456,42 @@ mod tests {
2410 check_ops(&ops);2456 check_ops(&ops);
2411 }2457 }
2412 2458
2459+ #[test]
2460+ fn move_workspace_to_output() {
2461+ let ops = [
2462+ Op::AddOutput(1),
2463+ Op::AddOutput(2),
2464+ Op::FocusOutput(1),
2465+ Op::AddWindow {
2466+ id: 0,
2467+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2468+ min_max_size: Default::default(),
2469+ },
2470+ Op::MoveWorkspaceToOutput(2),
2471+ ];
2472+
2473+ let mut layout = Layout::default();
2474+ for op in ops {
2475+ op.apply(&mut layout);
2476+ }
2477+
2478+ let MonitorSet::Normal {
2479+ monitors,
2480+ active_monitor_idx,
2481+ ..
2482+ } = layout.monitor_set
2483+ else {
2484+ unreachable!()
2485+ };
2486+
2487+ assert_eq!(active_monitor_idx, 1);
2488+ assert_eq!(monitors[0].workspaces.len(), 1);
2489+ assert!(!monitors[0].workspaces[0].has_windows());
2490+ assert_eq!(monitors[1].active_workspace_idx, 0);
2491+ assert_eq!(monitors[1].workspaces.len(), 2);
2492+ assert!(monitors[1].workspaces[0].has_windows());
2493+ }
2494+
2413 fn arbitrary_spacing() -> impl Strategy<Value = u16> {2495 fn arbitrary_spacing() -> impl Strategy<Value = u16> {
2414 // Give equal weight to:2496 // Give equal weight to:
2415 // - 0: the element is disabled2497 // - 0: the element is disabled