forge
Explore
Log in

Don't you dare go Hollow

layout: Extract Monitor::insert_workspace()

Browse files at this commit

ea3bbea6bdbdbdd02a62e99ff51cd48d655ae84fIvan MolodetskikhParent: 3b4cf1e
src/layout/mod.rsmodified+10 -55
@@ -31,7 +31,6 @@
3131 //! workspace just like any other. Then they come back, reconnect the second monitor, and now we
3232 //! don't want an unassuming workspace to end up on it.
3333
34-use std::cmp::min;
3534 use std::collections::HashMap;
3635 use std::mem;
3736 use std::rc::Rc;
@@ -2902,24 +2901,13 @@ impl<W: LayoutElement> Layout<W> {
29022901 .unwrap_or(*active_monitor_idx);
29032902 let mon = &mut monitors[mon_idx];
29042903
2905- let mut insert_idx = 0;
2906- if mon.options.layout.empty_workspace_above_first {
2907- // need to insert new empty workspace on top
2908- mon.add_workspace_top();
2909- insert_idx += 1;
2910- }
2911-
29122904 let ws = Workspace::new_with_config(
29132905 mon.output.clone(),
29142906 Some(ws_config.clone()),
29152907 clock,
29162908 options,
29172909 );
2918- mon.workspaces.insert(insert_idx, ws);
2919- mon.active_workspace_idx += 1;
2920-
2921- mon.workspace_switch = None;
2922- mon.clean_up_workspaces();
2910+ mon.insert_workspace(ws, 0, false);
29232911 }
29242912 MonitorSet::NoOutputs { workspaces } => {
29252913 let ws =
@@ -3415,7 +3403,6 @@ impl<W: LayoutElement> Layout<W> {
34153403 }
34163404
34173405 let mut ws = current.remove_workspace_by_idx(current.active_workspace_idx);
3418- ws.set_output(Some(output.clone()));
34193406 ws.original_output = OutputId::new(output);
34203407
34213408 let target_idx = monitors
@@ -3424,19 +3411,7 @@ impl<W: LayoutElement> Layout<W> {
34243411 .unwrap();
34253412 let target = &mut monitors[target_idx];
34263413
3427- target.previous_workspace_id = Some(target.workspaces[target.active_workspace_idx].id());
3428-
3429- if target.options.layout.empty_workspace_above_first && target.workspaces.len() == 1 {
3430- // Insert a new empty workspace on top to prepare for insertion of new workspace.
3431- target.add_workspace_top();
3432- }
3433- // Insert the workspace after the currently active one. Unless the currently active one is
3434- // the last empty workspace, then insert before.
3435- let target_ws_idx = min(target.active_workspace_idx + 1, target.workspaces.len() - 1);
3436- target.workspaces.insert(target_ws_idx, ws);
3437- target.active_workspace_idx = target_ws_idx;
3438- target.workspace_switch = None;
3439- target.clean_up_workspaces();
3414+ target.insert_workspace(ws, target.active_workspace_idx + 1, true);
34403415
34413416 *active_monitor_idx = target_idx;
34423417
@@ -3486,42 +3461,22 @@ impl<W: LayoutElement> Layout<W> {
34863461 return false;
34873462 }
34883463
3489- let current_active_ws_idx = current.active_workspace_idx;
3464+ // Only switch active monitor if the workspace to be moved is the currently focused one on
3465+ // the current monitor.
3466+ let activate =
3467+ current_idx == *active_monitor_idx && old_idx == current.active_workspace_idx;
34903468
34913469 let mut ws = current.remove_workspace_by_idx(old_idx);
3492- ws.set_output(Some(new_output.clone()));
34933470 ws.original_output = OutputId::new(&new_output);
34943471
34953472 let target = &mut monitors[target_idx];
3473+ target.insert_workspace(ws, target.active_workspace_idx + 1, activate);
34963474
3497- target.previous_workspace_id = Some(target.workspaces[target.active_workspace_idx].id());
3498-
3499- if target.options.layout.empty_workspace_above_first && target.workspaces.len() == 1 {
3500- // Insert a new empty workspace on top to prepare for insertion of new workspace.
3501- target.add_workspace_top();
3502- }
3503- // Insert the workspace after the currently active one. Unless the currently active one is
3504- // the last empty workspace, then insert before.
3505- let target_ws_idx = min(target.active_workspace_idx + 1, target.workspaces.len() - 1);
3506- target.workspaces.insert(target_ws_idx, ws);
3507-
3508- // Only switch active monitor if the workspace moved was the currently focused one on the
3509- // current monitor
3510- let res = if current_idx == *active_monitor_idx && old_idx == current_active_ws_idx {
3475+ if activate {
35113476 *active_monitor_idx = target_idx;
3512- target.active_workspace_idx = target_ws_idx;
3513- true
3514- } else {
3515- if target_ws_idx <= target.active_workspace_idx {
3516- target.active_workspace_idx += 1;
3517- }
3518- false
3519- };
3520-
3521- target.workspace_switch = None;
3522- target.clean_up_workspaces();
3477+ }
35233478
3524- res
3479+ activate
35253480 }
35263481
35273482 pub fn set_fullscreen(&mut self, id: &W::Id, is_fullscreen: bool) {
src/layout/monitor.rsmodified+28 -0
@@ -629,6 +629,34 @@ impl<W: LayoutElement> Monitor<W> {
629629 ws
630630 }
631631
632+ pub fn insert_workspace(&mut self, mut ws: Workspace<W>, mut idx: usize, activate: bool) {
633+ ws.set_output(Some(self.output.clone()));
634+
635+ // Don't insert past the last empty workspace.
636+ if idx == self.workspaces.len() {
637+ idx -= 1;
638+ }
639+ if idx == 0 && self.options.layout.empty_workspace_above_first {
640+ // Insert a new empty workspace on top to prepare for insertion of new workspace.
641+ self.add_workspace_top();
642+ idx += 1;
643+ }
644+
645+ self.workspaces.insert(idx, ws);
646+
647+ if idx <= self.active_workspace_idx {
648+ self.active_workspace_idx += 1;
649+ }
650+
651+ if activate {
652+ self.workspace_switch = None;
653+ self.activate_workspace(idx);
654+ }
655+
656+ self.workspace_switch = None;
657+ self.clean_up_workspaces();
658+ }
659+
632660 pub fn move_down_or_to_workspace_down(&mut self) {
633661 if !self.active_workspace().move_down() {
634662 self.move_to_workspace_down(true);