layout: Extract Monitor::insert_workspace()
Browse files at this commitea3bbea6bdbdbdd02a62e99ff51cd48d655ae84fParent: 3b4cf1e2 changed files +38 -55
- src/layout/mod.rs +10 -55
- src/layout/monitor.rs +28 -0
src/layout/mod.rsmodified+10 -55
| @@ -31,7 +31,6 @@ | ||
| 31 | 31 | //! workspace just like any other. Then they come back, reconnect the second monitor, and now we |
| 32 | 32 | //! don't want an unassuming workspace to end up on it. |
| 33 | 33 | |
| 34 | -use std::cmp::min; | |
| 35 | 34 | use std::collections::HashMap; |
| 36 | 35 | use std::mem; |
| 37 | 36 | use std::rc::Rc; |
| @@ -2902,24 +2901,13 @@ impl<W: LayoutElement> Layout<W> { | ||
| 2902 | 2901 | .unwrap_or(*active_monitor_idx); |
| 2903 | 2902 | let mon = &mut monitors[mon_idx]; |
| 2904 | 2903 | |
| 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 | - | |
| 2912 | 2904 | let ws = Workspace::new_with_config( |
| 2913 | 2905 | mon.output.clone(), |
| 2914 | 2906 | Some(ws_config.clone()), |
| 2915 | 2907 | clock, |
| 2916 | 2908 | options, |
| 2917 | 2909 | ); |
| 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); | |
| 2923 | 2911 | } |
| 2924 | 2912 | MonitorSet::NoOutputs { workspaces } => { |
| 2925 | 2913 | let ws = |
| @@ -3415,7 +3403,6 @@ impl<W: LayoutElement> Layout<W> { | ||
| 3415 | 3403 | } |
| 3416 | 3404 | |
| 3417 | 3405 | let mut ws = current.remove_workspace_by_idx(current.active_workspace_idx); |
| 3418 | - ws.set_output(Some(output.clone())); | |
| 3419 | 3406 | ws.original_output = OutputId::new(output); |
| 3420 | 3407 | |
| 3421 | 3408 | let target_idx = monitors |
| @@ -3424,19 +3411,7 @@ impl<W: LayoutElement> Layout<W> { | ||
| 3424 | 3411 | .unwrap(); |
| 3425 | 3412 | let target = &mut monitors[target_idx]; |
| 3426 | 3413 | |
| 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); | |
| 3440 | 3415 | |
| 3441 | 3416 | *active_monitor_idx = target_idx; |
| 3442 | 3417 | |
| @@ -3486,42 +3461,22 @@ impl<W: LayoutElement> Layout<W> { | ||
| 3486 | 3461 | return false; |
| 3487 | 3462 | } |
| 3488 | 3463 | |
| 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; | |
| 3490 | 3468 | |
| 3491 | 3469 | let mut ws = current.remove_workspace_by_idx(old_idx); |
| 3492 | - ws.set_output(Some(new_output.clone())); | |
| 3493 | 3470 | ws.original_output = OutputId::new(&new_output); |
| 3494 | 3471 | |
| 3495 | 3472 | let target = &mut monitors[target_idx]; |
| 3473 | + target.insert_workspace(ws, target.active_workspace_idx + 1, activate); | |
| 3496 | 3474 | |
| 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 { | |
| 3511 | 3476 | *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 | + } | |
| 3523 | 3478 | |
| 3524 | - res | |
| 3479 | + activate | |
| 3525 | 3480 | } |
| 3526 | 3481 | |
| 3527 | 3482 | 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> { | ||
| 629 | 629 | ws |
| 630 | 630 | } |
| 631 | 631 | |
| 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 | + | |
| 632 | 660 | pub fn move_down_or_to_workspace_down(&mut self) { |
| 633 | 661 | if !self.active_workspace().move_down() { |
| 634 | 662 | self.move_to_workspace_down(true); |