diff options
author | Aram Drevekenin <aram@poor.dev> | 2023-01-27 16:19:28 +0100 |
---|---|---|
committer | Aram Drevekenin <aram@poor.dev> | 2023-01-27 16:19:28 +0100 |
commit | 3866c905009eedfe61ae427d093b2d5bfa9472d8 (patch) | |
tree | 76d195ece00dc8a3dbc3f34d967f343c93c7199b /zellij-server | |
parent | 393b0bcc9d5ac7b799026adb5d57553f41fff702 (diff) |
fix(swap-layouts): include base layout
Diffstat (limited to 'zellij-server')
5 files changed, 86 insertions, 153 deletions
diff --git a/zellij-server/src/tab/mod.rs b/zellij-server/src/tab/mod.rs index cdb12c725..83f45b52b 100644 --- a/zellij-server/src/tab/mod.rs +++ b/zellij-server/src/tab/mod.rs @@ -37,7 +37,7 @@ use std::cell::RefCell; use std::rc::Rc; use std::time::Instant; use std::{ - collections::{HashMap, HashSet}, + collections::{HashMap, HashSet, BTreeMap}, str, }; use zellij_utils::{ @@ -134,10 +134,8 @@ pub(crate) struct Tab { struct SwapLayouts { swap_tiled_layouts: Vec<SwapTiledLayout>, swap_floating_layouts: Vec<SwapFloatingLayout>, - swap_layouts: Vec<(TiledPaneLayout, Vec<FloatingPaneLayout>)>, current_floating_layout_position: usize, current_tiled_layout_position: usize, - current_layout_position: Option<usize>, is_floating_damaged: bool, is_tiled_damaged: bool, display_area: Rc<RefCell<Size>>, // includes all panes (including eg. the status bar and tab bar in the default layout) @@ -155,9 +153,20 @@ impl SwapLayouts { ..Default::default() } } - pub fn set_current_layout(&mut self, layout: (TiledPaneLayout, Vec<FloatingPaneLayout>)) { - self.swap_layouts.push(layout); - self.current_layout_position = Some(self.swap_layouts.len() - 1); + pub fn set_base_layout(&mut self, layout: (TiledPaneLayout, Vec<FloatingPaneLayout>)) { + let mut base_swap_tiled_layout = BTreeMap::new(); + let mut base_swap_floating_layout = BTreeMap::new(); + let tiled_panes_count = layout.0.pane_count(); + let floating_panes_count = layout.1.len(); + // we set MaxPanes to the current panes in the layout, because the base layout is not + // intended to be progressive - i.e. to have additional panes added to it + // we still want to keep it around in case we'd like to swap layouts without adding panes + base_swap_tiled_layout.insert(LayoutConstraint::MaxPanes(tiled_panes_count), layout.0); + base_swap_floating_layout.insert(LayoutConstraint::MaxPanes(floating_panes_count), layout.1); + self.swap_tiled_layouts.insert(0, (base_swap_tiled_layout, Some("BASE".into()))); + self.swap_floating_layouts.insert(0, (base_swap_floating_layout, Some("BASE".into()))); + self.current_tiled_layout_position = 0; + self.current_floating_layout_position = 0; } pub fn set_is_floating_damaged(&mut self) { self.is_floating_damaged = true; @@ -187,70 +196,6 @@ impl SwapLayouts { None => (None, self.is_floating_damaged) } } - pub fn swap(&mut self, tiled_panes: &TiledPanes, floating_panes: &FloatingPanes) -> Option<(TiledPaneLayout, Vec<FloatingPaneLayout>)> { - let current_tiled_panes_count = tiled_panes.visible_panes_count(); - let current_floating_panes_count = floating_panes.visible_panes_count(); - let layout_fits_panes = |(tiled_panes_layout, floating_panes_layout): &(TiledPaneLayout, Vec<FloatingPaneLayout>)| { - tiled_panes_layout.pane_count() >= current_tiled_panes_count && floating_panes_layout.len() >= current_floating_panes_count - }; - if let Some(current_layout) = self.current_layout_position - .and_then(|position| self.swap_layouts.get(position)) { - let current_layout = current_layout.clone(); - if (self.is_tiled_damaged || self.is_floating_damaged) && layout_fits_panes(¤t_layout) { - self.reset_positions_and_damage(); - return Some(current_layout); - } - }; - // self.reset_positions_and_damage(); - match self.current_layout_position { - Some(current_position) => { - // look for candidate after current - for (i, layout_candidate) in self.swap_layouts.iter().enumerate().skip(current_position + 1) { - if layout_fits_panes(&layout_candidate) { - self.current_layout_position = Some(i); - return Some(layout_candidate.clone()); - } - } - - // look for candidate before current position - for (i, layout_candidate) in self.swap_layouts.iter().enumerate().take(current_position) { - if layout_fits_panes(&layout_candidate) { - self.current_layout_position = Some(i); - return Some(layout_candidate.clone()); - } - } - - // take the next or first layout even if it doesn't fit, the extra panes will be added on top of it - if let Some(layout_candidate) = self.swap_layouts.get(current_position + 1) { - self.current_layout_position = Some(current_position + 1); - return Some(layout_candidate.clone()); - } else if let Some(layout_candidate) = self.swap_layouts.iter().next() { - self.current_layout_position = Some(0); - return Some(layout_candidate.clone()); - } - - }, - None => { - // tiled panes are set to current layout if it exists, look for a candidate in the swap - // layouts - for (i, layout_candidate) in self.swap_layouts.iter().enumerate() { - if layout_fits_panes(&layout_candidate) { - self.current_layout_position = Some(i); - return Some(layout_candidate.clone()); - } - } - - } - }; - // take the first layout even if it doesn't fit, the extra panes will be added on top of it - if let Some(layout_candidate) = self.swap_layouts.get(0) { - let layout_candidate = layout_candidate.clone(); - self.current_layout_position = Some(0); - self.reset_positions_and_damage(); - return Some(layout_candidate); - } - None - } pub fn swap_floating_panes(&mut self, floating_panes: &FloatingPanes, search_backwards: bool) -> Option<Vec<FloatingPaneLayout>> { if self.swap_floating_layouts.is_empty() { return None; @@ -368,12 +313,6 @@ impl SwapLayouts { } None } - fn reset_positions_and_damage(&mut self) { - self.current_floating_layout_position = 0; - self.current_tiled_layout_position = 0; - self.is_floating_damaged = false; - self.is_tiled_damaged = false; - } } #[derive(Clone, Debug, Default, Serialize, Deserialize)] @@ -791,7 +730,7 @@ impl Tab { new_plugin_ids: HashMap<RunPluginLocation, Vec<u32>>, client_id: ClientId, ) -> Result<()> { - self.swap_layouts.set_current_layout((layout.clone(), floating_panes_layout.clone())); + self.swap_layouts.set_base_layout((layout.clone(), floating_panes_layout.clone())); let layout_has_floating_panes = LayoutApplier::new( &self.viewport, &self.senders, @@ -915,44 +854,6 @@ impl Tab { self.relayout_tiled_panes(client_id, search_backwards) } } - pub fn relayout(&mut self, client_id: Option<ClientId>) -> Result<()> { - if let Some(layout_candidate) = self.swap_layouts.swap(&self.tiled_panes, &self.floating_panes) { - if self.tiled_panes.fullscreen_is_active() { - self.tiled_panes.unset_fullscreen(); - } - let layout_has_floating_panes = LayoutApplier::new( - &self.viewport, - &self.senders, - &self.sixel_image_store, - &self.link_handler, - &self.terminal_emulator_colors, - &self.terminal_emulator_color_codes, - &self.character_cell_size, - &self.style, - &self.display_area, - &mut self.tiled_panes, - &mut self.floating_panes, - self.draw_pane_frames, - &mut self.focus_pane_id, - &self.os_api, - ) - .apply_layout_to_existing_panes( - &layout_candidate.0, - &layout_candidate.1, - client_id, - )?; - if layout_has_floating_panes { - if !self.floating_panes.panes_are_visible() { - self.toggle_floating_panes(client_id, None)?; - } - } - self.tiled_panes.reapply_pane_frames(); - } - self.is_pending = false; - self.apply_buffered_instructions()?; - // self.set_force_render(); - Ok(()) - } pub fn apply_buffered_instructions(&mut self) -> Result<()> { let buffered_instructions: Vec<BufferedTabInstruction> = self.pending_instructions.drain(..).collect(); diff --git a/zellij-server/src/tab/unit/snapshots/zellij_server__tab__tab_integration_tests__swapping_layouts_after_resize_snaps_to_current_layout.snap b/zellij-server/src/tab/unit/snapshots/zellij_server__tab__tab_integration_tests__swapping_layouts_after_resize_snaps_to_current_layout.snap index a144dac19..571c72b12 100644 --- a/zellij-server/src/tab/unit/snapshots/zellij_server__tab__tab_integration_tests__swapping_layouts_after_resize_snaps_to_current_layout.snap +++ b/zellij-server/src/tab/unit/snapshots/zellij_server__tab__tab_integration_tests__swapping_layouts_after_resize_snaps_to_current_layout.snap @@ -3,24 +3,24 @@ source: zellij-server/src/tab/./unit/tab_integration_tests.rs assertion_line: 3095 expression: snapshot --- -00 (C): ┌ Pane #2 ──────────────────────────────────────────────────┐┌ Pane #1 ─────────────────────────────────────────────────┐ -01 (C): │ ││ │ -02 (C): │ ││ │ -03 (C): │ ││ │ -04 (C): │ ││ │ -05 (C): │ ││ │ -06 (C): │ ││ │ -07 (C): │ ││ │ -08 (C): │ ││ │ -09 (C): │ ││ │ -10 (C): │ ││ │ -11 (C): │ ││ │ -12 (C): │ ││ │ -13 (C): │ ││ │ -14 (C): │ ││ │ -15 (C): │ ││ │ -16 (C): │ ││ │ -17 (C): │ ││ │ -18 (C): │ ││ │ -19 (C): └───────────────────────────────────────────────────────────┘└──────────────────────────────────────────────────────────┘ +00 (C): ┌ Pane #1 ─────────────────────────────────────────────────┐┌ Pane #2 ──────────────────────────────────────────────────┐ +01 (C): │ ││ │ +02 (C): │ ││ │ +03 (C): │ ││ │ +04 (C): │ ││ │ +05 (C): │ ││ │ +06 (C): │ ││ │ +07 (C): │ ││ │ +08 (C): │ ││ │ +09 (C): │ ││ │ +10 (C): │ ││ │ +11 (C): │ ││ │ +12 (C): │ ││ │ +13 (C): │ ││ │ +14 (C): │ ││ │ +15 (C): │ ││ │ +16 (C): │ ││ │ +17 (C): │ ││ │ +18 (C): │ ││ │ +19 (C): └──────────────────────────────────────────────────────────┘└───────────────────────────────────────────────────────────┘ diff --git a/zellij-server/src/tab/unit/tab_integration_tests.rs b/zellij-server/src/tab/unit/tab_integration_tests.rs index 032940ea0..1b002bcc8 100644 --- a/zellij-server/src/tab/unit/tab_integration_tests.rs +++ b/zellij-server/src/tab/unit/tab_integration_tests.rs @@ -3082,9 +3082,9 @@ fn swapping_layouts_after_resize_snaps_to_current_layout() { tab.new_pane(new_pane_id_1, None, None, Some(client_id)) .unwrap(); - tab.relayout(Some(client_id)).unwrap(); + tab.next_swap_layout(Some(client_id)).unwrap(); tab.resize(client_id, ResizeStrategy::new(Resize::Increase, None)).unwrap(); - tab.relayout(Some(client_id)).unwrap(); + tab.next_swap_layout(Some(client_id)).unwrap(); tab.render(&mut output, None).unwrap(); let snapshot = take_snapshot( output.serialize().unwrap().get(&client_id).unwrap(), diff --git a/zellij-server/src/unit/snapshots/zellij_server__screen__screen_tests__send_cli_rename_tab.snap b/zellij-server/src/unit/snapshots/zellij_server__screen__screen_tests__send_cli_rename_tab.snap index 5da586fde..957af3ccc 100644 --- a/zellij-server/src/unit/snapshots/zellij_server__screen__screen_tests__send_cli_rename_tab.snap +++ b/zellij-server/src/unit/snapshots/zellij_server__screen__screen_tests__send_cli_rename_tab.snap @@ -170,7 +170,9 @@ expression: "format!(\"{:#?}\", * received_plugin_instructions.lock().unwrap())" is_sync_panes_active: false, are_floating_panes_visible: false, other_focused_clients: [], - active_swap_layout_name: None, + active_swap_layout_name: Some( + "BASE", + ), is_swap_layout_dirty: false, }, ], @@ -325,7 +327,9 @@ expression: "format!(\"{:#?}\", * received_plugin_instructions.lock().unwrap())" is_sync_panes_active: false, are_floating_panes_visible: false, other_focused_clients: [], - active_swap_layout_name: None, + active_swap_layout_name: Some( + "BASE", + ), is_swap_layout_dirty: false, }, TabInfo { @@ -337,7 +341,9 @@ expression: "format!(\"{:#?}\", * received_plugin_instructions.lock().unwrap())" is_sync_panes_active: false, are_floating_panes_visible: false, other_focused_clients: [], - active_swap_layout_name: None, + active_swap_layout_name: Some( + "BASE", + ), is_swap_layout_dirty: false, }, ], @@ -363,7 +369,9 @@ expression: "format!(\"{:#?}\", * received_plugin_instructions.lock().unwrap())" is_sync_panes_active: false, are_floating_panes_visible: false, other_focused_clients: [], - active_swap_layout_name: None, + active_swap_layout_name: Some( + "BASE", + ), is_swap_layout_dirty: false, }, TabInfo { @@ -375,7 +383,9 @@ expression: "format!(\"{:#?}\", * received_plugin_instructions.lock().unwrap())" is_sync_panes_active: false, are_floating_panes_visible: false, other_focused_clients: [], - active_swap_layout_name: None, + active_swap_layout_name: Some( + "BASE", + ), is_swap_layout_dirty: false, }, ], @@ -401,7 +411,9 @@ expression: "format!(\"{:#?}\", * received_plugin_instructions.lock().unwrap())" is_sync_panes_active: false, are_floating_panes_visible: false, other_focused_clients: [], - active_swap_layout_name: None, + active_swap_layout_name: Some( + "BASE", + ), is_swap_layout_dirty: false, }, TabInfo { @@ -413,7 +425,9 @@ expression: "format!(\"{:#?}\", * received_plugin_instructions.lock().unwrap())" is_sync_panes_active: false, are_floating_panes_visible: false, other_focused_clients: [], - active_swap_layout_name: None, + active_swap_layout_name: Some( + "BASE", + ), is_swap_layout_dirty: false, }, ], diff --git a/zellij-server/src/unit/snapshots/zellij_server__screen__screen_tests__send_cli_undo_rename_tab.snap b/zellij-server/src/unit/snapshots/zellij_server__screen__screen_tests__send_cli_undo_rename_tab.snap index 0b07a016e..a229be90b 100644 --- a/zellij-server/src/unit/snapshots/zellij_server__screen__screen_tests__send_cli_undo_rename_tab.snap +++ b/zellij-server/src/unit/snapshots/zellij_server__screen__screen_tests__send_cli_undo_rename_tab.snap @@ -170,7 +170,9 @@ expression: "format!(\"{:#?}\", * received_plugin_instructions.lock().unwrap())" is_sync_panes_active: false, are_floating_panes_visible: false, other_focused_clients: [], - active_swap_layout_name: None, + active_swap_layout_name: Some( + "BASE", + ), is_swap_layout_dirty: false, }, ], @@ -325,7 +327,9 @@ expression: "format!(\"{:#?}\", * received_plugin_instructions.lock().unwrap())" is_sync_panes_active: false, are_floating_panes_visible: false, other_focused_clients: [], - active_swap_layout_name: None, + active_swap_layout_name: Some( + "BASE", + ), is_swap_layout_dirty: false, }, TabInfo { @@ -337,7 +341,9 @@ expression: "format!(\"{:#?}\", * received_plugin_instructions.lock().unwrap())" is_sync_panes_active: false, are_floating_panes_visible: false, other_focused_clients: [], - active_swap_layout_name: None, + active_swap_layout_name: Some( + "BASE", + ), is_swap_layout_dirty: false, }, ], @@ -363,7 +369,9 @@ expression: "format!(\"{:#?}\", * received_plugin_instructions.lock().unwrap())" is_sync_panes_active: false, are_floating_panes_visible: false, other_focused_clients: [], - active_swap_layout_name: None, + active_swap_layout_name: Some( + "BASE", + ), is_swap_layout_dirty: false, }, TabInfo { @@ -375,7 +383,9 @@ expression: "format!(\"{:#?}\", * received_plugin_instructions.lock().unwrap())" is_sync_panes_active: false, are_floating_panes_visible: false, other_focused_clients: [], - active_swap_layout_name: None, + active_swap_layout_name: Some( + "BASE", + ), is_swap_layout_dirty: false, }, ], @@ -401,7 +411,9 @@ expression: "format!(\"{:#?}\", * received_plugin_instructions.lock().unwrap())" is_sync_panes_active: false, are_floating_panes_visible: false, other_focused_clients: [], - active_swap_layout_name: None, + active_swap_layout_name: Some( + "BASE", + ), is_swap_layout_dirty: false, }, TabInfo { @@ -413,7 +425,9 @@ expression: "format!(\"{:#?}\", * received_plugin_instructions.lock().unwrap())" is_sync_panes_active: false, are_floating_panes_visible: false, other_focused_clients: [], - active_swap_layout_name: None, + active_swap_layout_name: Some( + "BASE", + ), is_swap_layout_dirty: false, }, ], @@ -450,7 +464,9 @@ expression: "format!(\"{:#?}\", * received_plugin_instructions.lock().unwrap())" is_sync_panes_active: false, are_floating_panes_visible: false, other_focused_clients: [], - active_swap_layout_name: None, + active_swap_layout_name: Some( + "BASE", + ), is_swap_layout_dirty: false, }, TabInfo { @@ -462,7 +478,9 @@ expression: "format!(\"{:#?}\", * received_plugin_instructions.lock().unwrap())" is_sync_panes_active: false, are_floating_panes_visible: false, other_focused_clients: [], - active_swap_layout_name: None, + active_swap_layout_name: Some( + "BASE", + ), is_swap_layout_dirty: false, }, ], |