forge
Explore
Log in

Don't you dare go Hollow

Add move-workspace-to-index and move-workspace-to-monitor actions (#1007)

Browse files at this commit

852da5714affd067de731599136ed619dc3bba40UnverifiedKirottu and Ivan MolodetskikhParent: 4f79303
* Added move-workspace-to-index and move-workspace-to-monitor IPC actions

* Added redraws to the workspace handling actions, fixed tests that panicked, fixed other mentioned problems.

* Fixed workspace focusing and handling numbered workspaces with `move-workspace-to-index`

* Fixed more inconsistencies with move-workspace-to-monitor

* Added back `self.workspace_switch = None`

* Reordered some workspace cleanup logic

* Fix formatting

* Add missing blank lines

* Fix moving workspace to same monitor and wrong current index updating

* Move function up and add fixme comment

---------
niri-config/src/lib.rsmodified+34 -0
@@ -1358,6 +1358,18 @@ pub enum Action {
1358 MoveColumnToWorkspace(#[knuffel(argument)] WorkspaceReference),1358 MoveColumnToWorkspace(#[knuffel(argument)] WorkspaceReference),
1359 MoveWorkspaceDown,1359 MoveWorkspaceDown,
1360 MoveWorkspaceUp,1360 MoveWorkspaceUp,
1361+ MoveWorkspaceToIndex(#[knuffel(argument)] usize),
1362+ #[knuffel(skip)]
1363+ MoveWorkspaceToIndexByRef {
1364+ new_idx: usize,
1365+ reference: WorkspaceReference,
1366+ },
1367+ #[knuffel(skip)]
1368+ MoveWorkspaceToMonitorByRef {
1369+ output_name: String,
1370+ reference: WorkspaceReference,
1371+ },
1372+ MoveWorkspaceToMonitor(#[knuffel(argument)] String),
1361 SetWorkspaceName(#[knuffel(argument)] String),1373 SetWorkspaceName(#[knuffel(argument)] String),
1362 #[knuffel(skip)]1374 #[knuffel(skip)]
1363 SetWorkspaceNameByRef {1375 SetWorkspaceNameByRef {
@@ -1612,6 +1624,28 @@ impl From<niri_ipc::Action> for Action {
1612 niri_ipc::Action::MoveWorkspaceToMonitorPrevious {} => {1624 niri_ipc::Action::MoveWorkspaceToMonitorPrevious {} => {
1613 Self::MoveWorkspaceToMonitorPrevious1625 Self::MoveWorkspaceToMonitorPrevious
1614 }1626 }
1627+ niri_ipc::Action::MoveWorkspaceToIndex {
1628+ index,
1629+ reference: Some(reference),
1630+ } => Self::MoveWorkspaceToIndexByRef {
1631+ new_idx: index,
1632+ reference: WorkspaceReference::from(reference),
1633+ },
1634+ niri_ipc::Action::MoveWorkspaceToIndex {
1635+ index,
1636+ reference: None,
1637+ } => Self::MoveWorkspaceToIndex(index),
1638+ niri_ipc::Action::MoveWorkspaceToMonitor {
1639+ output,
1640+ reference: Some(reference),
1641+ } => Self::MoveWorkspaceToMonitorByRef {
1642+ output_name: output,
1643+ reference: WorkspaceReference::from(reference),
1644+ },
1645+ niri_ipc::Action::MoveWorkspaceToMonitor {
1646+ output,
1647+ reference: None,
1648+ } => Self::MoveWorkspaceToMonitor(output),
1615 niri_ipc::Action::MoveWorkspaceToMonitorNext {} => Self::MoveWorkspaceToMonitorNext,1649 niri_ipc::Action::MoveWorkspaceToMonitorNext {} => Self::MoveWorkspaceToMonitorNext,
1616 niri_ipc::Action::ToggleDebugTint {} => Self::ToggleDebugTint,1650 niri_ipc::Action::ToggleDebugTint {} => Self::ToggleDebugTint,
1617 niri_ipc::Action::DebugToggleOpaqueRegions {} => Self::DebugToggleOpaqueRegions,1651 niri_ipc::Action::DebugToggleOpaqueRegions {} => Self::DebugToggleOpaqueRegions,
niri-ipc/src/lib.rsmodified+32 -0
@@ -365,6 +365,22 @@ pub enum Action {
365 MoveWorkspaceDown {},365 MoveWorkspaceDown {},
366 /// Move the focused workspace up.366 /// Move the focused workspace up.
367 MoveWorkspaceUp {},367 MoveWorkspaceUp {},
368+ /// Move a workspace to a specific index on its monitor.
369+ #[cfg_attr(
370+ feature = "clap",
371+ clap(about = "Move the focused workspace to a specific index on its monitor")
372+ )]
373+ MoveWorkspaceToIndex {
374+ /// New index for the workspace.
375+ #[cfg_attr(feature = "clap", arg())]
376+ index: usize,
377+
378+ /// Reference (index or name) of the workspace to move.
379+ ///
380+ /// If `None`, uses the focused workspace.
381+ #[cfg_attr(feature = "clap", arg(long))]
382+ reference: Option<WorkspaceReferenceArg>,
383+ },
368 /// Set the name of a workspace.384 /// Set the name of a workspace.
369 #[cfg_attr(385 #[cfg_attr(
370 feature = "clap",386 feature = "clap",
@@ -519,6 +535,22 @@ pub enum Action {
519 MoveWorkspaceToMonitorPrevious {},535 MoveWorkspaceToMonitorPrevious {},
520 /// Move the focused workspace to the next monitor.536 /// Move the focused workspace to the next monitor.
521 MoveWorkspaceToMonitorNext {},537 MoveWorkspaceToMonitorNext {},
538+ /// Move a workspace to a specific monitor.
539+ #[cfg_attr(
540+ feature = "clap",
541+ clap(about = "Move the focused workspace to a specific monitor")
542+ )]
543+ MoveWorkspaceToMonitor {
544+ /// The target output name.
545+ #[cfg_attr(feature = "clap", arg())]
546+ output: String,
547+
548+ // Reference (index or name) of the workspace to move.
549+ ///
550+ /// If `None`, uses the focused workspace.
551+ #[cfg_attr(feature = "clap", arg(long))]
552+ reference: Option<WorkspaceReferenceArg>,
553+ },
522 /// Toggle a debug tint on windows.554 /// Toggle a debug tint on windows.
523 ToggleDebugTint {},555 ToggleDebugTint {},
524 /// Toggle visualization of render element opaque regions.556 /// Toggle visualization of render element opaque regions.
src/input/mod.rsmodified+43 -0
@@ -1163,6 +1163,18 @@ impl State {
1163 // FIXME: granular1163 // FIXME: granular
1164 self.niri.queue_redraw_all();1164 self.niri.queue_redraw_all();
1165 }1165 }
1166+ Action::MoveWorkspaceToIndex(new_idx) => {
1167+ self.niri.layout.move_workspace_to_idx(None, new_idx);
1168+ // FIXME: granular
1169+ self.niri.queue_redraw_all();
1170+ }
1171+ Action::MoveWorkspaceToIndexByRef { new_idx, reference } => {
1172+ if let Some(res) = self.niri.find_output_and_workspace_index(reference) {
1173+ self.niri.layout.move_workspace_to_idx(Some(res), new_idx);
1174+ // FIXME: granular
1175+ self.niri.queue_redraw_all();
1176+ }
1177+ }
1166 Action::SetWorkspaceName(name) => {1178 Action::SetWorkspaceName(name) => {
1167 self.niri.layout.set_workspace_name(name, None);1179 self.niri.layout.set_workspace_name(name, None);
1168 }1180 }
@@ -1497,6 +1509,37 @@ impl State {
1497 }1509 }
1498 }1510 }
1499 }1511 }
1512+ Action::MoveWorkspaceToMonitor(new_output) => {
1513+ if let Some(new_output) = self.niri.output_by_name_match(&new_output).cloned() {
1514+ if self.niri.layout.move_workspace_to_output(&new_output)
1515+ && !self.maybe_warp_cursor_to_focus_centered()
1516+ {
1517+ self.move_cursor_to_output(&new_output);
1518+ }
1519+ }
1520+ }
1521+ Action::MoveWorkspaceToMonitorByRef {
1522+ output_name,
1523+ reference,
1524+ } => {
1525+ if let Some((output, old_idx)) =
1526+ self.niri.find_output_and_workspace_index(reference)
1527+ {
1528+ if let Some(new_output) = self.niri.output_by_name_match(&output_name).cloned()
1529+ {
1530+ if self.niri.layout.move_workspace_to_output_by_id(
1531+ old_idx,
1532+ output,
1533+ new_output.clone(),
1534+ ) {
1535+ // Cursor warp already calls `queue_redraw_all`
1536+ if !self.maybe_warp_cursor_to_focus_centered() {
1537+ self.move_cursor_to_output(&new_output);
1538+ }
1539+ }
1540+ }
1541+ }
1542+ }
1500 Action::ToggleWindowFloating => {1543 Action::ToggleWindowFloating => {
1501 self.niri.layout.toggle_window_floating(None);1544 self.niri.layout.toggle_window_floating(None);
1502 // FIXME: granular1545 // FIXME: granular
src/layout/mod.rsmodified+245 -2
@@ -3032,17 +3032,23 @@ impl<W: LayoutElement> Layout<W> {
3032 }3032 }
3033 }3033 }
3034 3034
3035- pub fn move_workspace_to_output(&mut self, output: &Output) {3035+ pub fn move_workspace_to_output(&mut self, output: &Output) -> bool {
3036 let MonitorSet::Normal {3036 let MonitorSet::Normal {
3037 monitors,3037 monitors,
3038 active_monitor_idx,3038 active_monitor_idx,
3039 ..3039 ..
3040 } = &mut self.monitor_set3040 } = &mut self.monitor_set
3041 else {3041 else {
3042- return;3042+ return false;
3043 };3043 };
3044 3044
3045 let current = &mut monitors[*active_monitor_idx];3045 let current = &mut monitors[*active_monitor_idx];
3046+
3047+ // Do not do anything if the output is already correct
3048+ if &current.output == output {
3049+ return false;
3050+ }
3051+
3046 if current.active_workspace_idx == current.workspaces.len() - 1 {3052 if current.active_workspace_idx == current.workspaces.len() - 1 {
3047 // Insert a new empty workspace.3053 // Insert a new empty workspace.
3048 current.add_workspace_bottom();3054 current.add_workspace_bottom();
@@ -3080,6 +3086,96 @@ impl<W: LayoutElement> Layout<W> {
3080 target.clean_up_workspaces();3086 target.clean_up_workspaces();
3081 3087
3082 *active_monitor_idx = target_idx;3088 *active_monitor_idx = target_idx;
3089+
3090+ true
3091+ }
3092+
3093+ // FIXME: accept workspace by id and deduplicate logic with move_workspace_to_output()
3094+ pub fn move_workspace_to_output_by_id(
3095+ &mut self,
3096+ old_idx: usize,
3097+ old_output: Option<Output>,
3098+ new_output: Output,
3099+ ) -> bool {
3100+ let MonitorSet::Normal {
3101+ monitors,
3102+ active_monitor_idx,
3103+ ..
3104+ } = &mut self.monitor_set
3105+ else {
3106+ return false;
3107+ };
3108+
3109+ let current_idx = if let Some(old_output) = old_output {
3110+ monitors
3111+ .iter()
3112+ .position(|mon| mon.output == old_output)
3113+ .unwrap()
3114+ } else {
3115+ *active_monitor_idx
3116+ };
3117+ let target_idx = monitors
3118+ .iter()
3119+ .position(|mon| mon.output == new_output)
3120+ .unwrap();
3121+
3122+ if current_idx == target_idx {
3123+ return false;
3124+ }
3125+
3126+ let current = &mut monitors[current_idx];
3127+ let current_active_ws_idx = current.active_workspace_idx;
3128+
3129+ if old_idx == current.workspaces.len() - 1 {
3130+ // Insert a new empty workspace.
3131+ current.add_workspace_bottom();
3132+ }
3133+
3134+ let mut ws = current.workspaces.remove(old_idx);
3135+
3136+ if current.options.empty_workspace_above_first && old_idx == 0 {
3137+ current.add_workspace_top();
3138+ }
3139+
3140+ if old_idx < current.active_workspace_idx {
3141+ current.active_workspace_idx -= 1;
3142+ }
3143+ current.workspace_switch = None;
3144+ current.clean_up_workspaces();
3145+
3146+ ws.set_output(Some(new_output.clone()));
3147+ ws.original_output = OutputId::new(&new_output);
3148+
3149+ let target = &mut monitors[target_idx];
3150+
3151+ target.previous_workspace_id = Some(target.workspaces[target.active_workspace_idx].id());
3152+
3153+ if target.options.empty_workspace_above_first && target.workspaces.len() == 1 {
3154+ // Insert a new empty workspace on top to prepare for insertion of new workspce.
3155+ target.add_workspace_top();
3156+ }
3157+ // Insert the workspace after the currently active one. Unless the currently active one is
3158+ // the last empty workspace, then insert before.
3159+ let target_ws_idx = min(target.active_workspace_idx + 1, target.workspaces.len() - 1);
3160+ target.workspaces.insert(target_ws_idx, ws);
3161+
3162+ // Only switch active monitor if the workspace moved was the currently focused one on the
3163+ // current monitor
3164+ let res = if current_idx == *active_monitor_idx && old_idx == current_active_ws_idx {
3165+ *active_monitor_idx = target_idx;
3166+ target.active_workspace_idx = target_ws_idx;
3167+ true
3168+ } else {
3169+ if target_ws_idx <= target.active_workspace_idx {
3170+ target.active_workspace_idx += 1;
3171+ }
3172+ false
3173+ };
3174+
3175+ target.workspace_switch = None;
3176+ target.clean_up_workspaces();
3177+
3178+ res
3083 }3179 }
3084 3180
3085 pub fn set_fullscreen(&mut self, window: &W::Id, is_fullscreen: bool) {3181 pub fn set_fullscreen(&mut self, window: &W::Id, is_fullscreen: bool) {
@@ -3792,6 +3888,37 @@ impl<W: LayoutElement> Layout<W> {
3792 monitor.move_workspace_up();3888 monitor.move_workspace_up();
3793 }3889 }
3794 3890
3891+ pub fn move_workspace_to_idx(
3892+ &mut self,
3893+ reference: Option<(Option<Output>, usize)>,
3894+ new_idx: usize,
3895+ ) {
3896+ let (monitor, old_idx) = if let Some((output, old_idx)) = reference {
3897+ let monitor = if let Some(output) = output {
3898+ let Some(monitor) = self.monitor_for_output_mut(&output) else {
3899+ return;
3900+ };
3901+ monitor
3902+ } else {
3903+ // In case a numbered workspace reference is used, assume the active monitor
3904+ let Some(monitor) = self.active_monitor() else {
3905+ return;
3906+ };
3907+ monitor
3908+ };
3909+
3910+ (monitor, old_idx)
3911+ } else {
3912+ let Some(monitor) = self.active_monitor() else {
3913+ return;
3914+ };
3915+ let index = monitor.active_workspace_idx;
3916+ (monitor, index)
3917+ };
3918+
3919+ monitor.move_workspace_to_idx(old_idx, new_idx);
3920+ }
3921+
3795 pub fn set_workspace_name(&mut self, name: String, reference: Option<WorkspaceReference>) {3922 pub fn set_workspace_name(&mut self, name: String, reference: Option<WorkspaceReference>) {
3796 // ignore the request if the name is already used by another workspace3923 // ignore the request if the name is already used by another workspace
3797 if self.find_workspace_by_name(&name).is_some() {3924 if self.find_workspace_by_name(&name).is_some() {
@@ -4607,6 +4734,18 @@ mod tests {
4607 MoveColumnToWorkspace(#[proptest(strategy = "0..=4usize")] usize),4734 MoveColumnToWorkspace(#[proptest(strategy = "0..=4usize")] usize),
4608 MoveWorkspaceDown,4735 MoveWorkspaceDown,
4609 MoveWorkspaceUp,4736 MoveWorkspaceUp,
4737+ MoveWorkspaceToIndex {
4738+ #[proptest(strategy = "proptest::option::of(1..=5usize)")]
4739+ ws_name: Option<usize>,
4740+ #[proptest(strategy = "0..=4usize")]
4741+ target_idx: usize,
4742+ },
4743+ MoveWorkspaceToMonitor {
4744+ #[proptest(strategy = "proptest::option::of(1..=5usize)")]
4745+ ws_name: Option<usize>,
4746+ #[proptest(strategy = "0..=5usize")]
4747+ output_id: usize,
4748+ },
4610 SetWorkspaceName {4749 SetWorkspaceName {
4611 #[proptest(strategy = "1..=5usize")]4750 #[proptest(strategy = "1..=5usize")]
4612 new_ws_name: usize,4751 new_ws_name: usize,
@@ -5179,6 +5318,78 @@ mod tests {
5179 }5318 }
5180 Op::MoveWorkspaceDown => layout.move_workspace_down(),5319 Op::MoveWorkspaceDown => layout.move_workspace_down(),
5181 Op::MoveWorkspaceUp => layout.move_workspace_up(),5320 Op::MoveWorkspaceUp => layout.move_workspace_up(),
5321+ Op::MoveWorkspaceToIndex {
5322+ ws_name: Some(ws_name),
5323+ target_idx,
5324+ } => {
5325+ let MonitorSet::Normal { monitors, .. } = &mut layout.monitor_set else {
5326+ return;
5327+ };
5328+
5329+ let Some((old_idx, old_output)) = monitors.iter().find_map(|monitor| {
5330+ monitor
5331+ .workspaces
5332+ .iter()
5333+ .enumerate()
5334+ .find_map(|(i, ws)| {
5335+ if ws.name == Some(format!("ws{ws_name}")) {
5336+ Some(i)
5337+ } else {
5338+ None
5339+ }
5340+ })
5341+ .map(|i| (i, monitor.output.clone()))
5342+ }) else {
5343+ return;
5344+ };
5345+
5346+ layout.move_workspace_to_idx(Some((Some(old_output), old_idx)), target_idx)
5347+ }
5348+ Op::MoveWorkspaceToIndex {
5349+ ws_name: None,
5350+ target_idx,
5351+ } => layout.move_workspace_to_idx(None, target_idx),
5352+ Op::MoveWorkspaceToMonitor {
5353+ ws_name: None,
5354+ output_id: id,
5355+ } => {
5356+ let name = format!("output{id}");
5357+ let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
5358+ return;
5359+ };
5360+ layout.move_workspace_to_output(&output);
5361+ }
5362+ Op::MoveWorkspaceToMonitor {
5363+ ws_name: Some(ws_name),
5364+ output_id: id,
5365+ } => {
5366+ let name = format!("output{id}");
5367+ let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
5368+ return;
5369+ };
5370+ let MonitorSet::Normal { monitors, .. } = &mut layout.monitor_set else {
5371+ return;
5372+ };
5373+
5374+ let Some((old_idx, old_output)) = monitors.iter().find_map(|monitor| {
5375+ monitor
5376+ .workspaces
5377+ .iter()
5378+ .enumerate()
5379+ .find_map(|(i, ws)| {
5380+ if ws.name == Some(format!("ws{ws_name}")) {
5381+ Some(i)
5382+ } else {
5383+ None
5384+ }
5385+ })
5386+ .map(|i| (i, monitor.output.clone()))
5387+ }) else {
5388+ return;
5389+ };
5390+
5391+ layout.move_workspace_to_output_by_id(old_idx, Some(old_output), output);
5392+ }
5182 Op::SwitchPresetColumnWidth => layout.toggle_width(),5393 Op::SwitchPresetColumnWidth => layout.toggle_width(),
5183 Op::SwitchPresetWindowWidth { id } => {5394 Op::SwitchPresetWindowWidth { id } => {
5184 let id = id.filter(|id| layout.has_window(id));5395 let id = id.filter(|id| layout.has_window(id));
@@ -6986,6 +7197,38 @@ mod tests {
6986 check_ops(&ops);7197 check_ops(&ops);
6987 }7198 }
6988 7199
7200+ #[test]
7201+ fn move_workspace_to_same_monitor_doesnt_reorder() {
7202+ let ops = [
7203+ Op::AddOutput(0),
7204+ Op::SetWorkspaceName {
7205+ new_ws_name: 0,
7206+ ws_name: None,
7207+ },
7208+ Op::AddWindow {
7209+ params: TestWindowParams::new(0),
7210+ },
7211+ Op::FocusWorkspaceDown,
7212+ Op::AddWindow {
7213+ params: TestWindowParams::new(1),
7214+ },
7215+ Op::AddWindow {
7216+ params: TestWindowParams::new(2),
7217+ },
7218+ Op::MoveWorkspaceToMonitor {
7219+ ws_name: Some(0),
7220+ output_id: 0,
7221+ },
7222+ ];
7223+
7224+ let layout = check_ops(&ops);
7225+ let counts: Vec<_> = layout
7226+ .workspaces()
7227+ .map(|(_, _, ws)| ws.windows().count())
7228+ .collect();
7229+ assert_eq!(counts, &[1, 2, 0]);
7230+ }
7231+
6989 fn parent_id_causes_loop(layout: &Layout<TestWindow>, id: usize, mut parent_id: usize) -> bool {7232 fn parent_id_causes_loop(layout: &Layout<TestWindow>, id: usize, mut parent_id: usize) -> bool {
6990 if parent_id == id {7233 if parent_id == id {
6991 return true;7234 return true;
src/layout/monitor.rsmodified+47 -0
@@ -858,6 +858,53 @@ impl<W: LayoutElement> Monitor<W> {
858 self.clean_up_workspaces();858 self.clean_up_workspaces();
859 }859 }
860 860
861+ pub fn move_workspace_to_idx(&mut self, old_idx: usize, new_idx: usize) {
862+ let mut new_idx = new_idx.clamp(0, self.workspaces.len() - 1);
863+ if old_idx == new_idx {
864+ return;
865+ }
866+
867+ let ws = self.workspaces.remove(old_idx);
868+ self.workspaces.insert(new_idx, ws);
869+
870+ if new_idx > old_idx {
871+ if new_idx == self.workspaces.len() - 1 {
872+ // Insert a new empty workspace.
873+ self.add_workspace_bottom();
874+ }
875+
876+ if self.options.empty_workspace_above_first && old_idx == 0 {
877+ self.add_workspace_top();
878+ new_idx += 1;
879+ }
880+ } else {
881+ if old_idx == self.workspaces.len() - 1 {
882+ // Insert a new empty workspace.
883+ self.add_workspace_bottom();
884+ }
885+
886+ if self.options.empty_workspace_above_first && new_idx == 0 {
887+ self.add_workspace_top();
888+ new_idx += 1;
889+ }
890+ }
891+
892+ // Only refocus the workspace if it was already focused
893+ if self.active_workspace_idx == old_idx {
894+ self.active_workspace_idx = new_idx;
895+ // If the workspace order was switched so that the current workspace moved down the
896+ // workspace stack, focus correctly
897+ } else if new_idx <= self.active_workspace_idx && old_idx > self.active_workspace_idx {
898+ self.active_workspace_idx += 1;
899+ } else if new_idx >= self.active_workspace_idx && old_idx < self.active_workspace_idx {
900+ self.active_workspace_idx = self.active_workspace_idx.saturating_sub(1);
901+ }
902+
903+ self.workspace_switch = None;
904+
905+ self.clean_up_workspaces();
906+ }
907+
861 /// Returns the geometry of the active tile relative to and clamped to the output.908 /// Returns the geometry of the active tile relative to and clamped to the output.
862 ///909 ///
863 /// During animations, assumes the final view position.910 /// During animations, assumes the final view position.