fix(layouts): recover from resurrection crash and pick up swap layouts properly (#3249)

* fix(layouts): recover from issues in the constraint system

* fix(keybinds): pick up swap layouts for new tab keybinding
This commit is contained in:
Aram Drevekenin 2024-04-10 15:26:54 +02:00 committed by GitHub
parent 462239b535
commit 41dbe65e26
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 246 additions and 237 deletions

View file

@ -124,11 +124,19 @@ impl<'a> LayoutApplier<'a> {
refocus_pane: bool, refocus_pane: bool,
client_id: Option<ClientId>, client_id: Option<ClientId>,
) -> Result<()> { ) -> Result<()> {
let err_context = || format!("failed to apply tiled panes layout");
let free_space = self.total_space_for_tiled_panes(); let free_space = self.total_space_for_tiled_panes();
let tiled_panes_count = self.tiled_panes.visible_panes_count(); let tiled_panes_count = self.tiled_panes.visible_panes_count();
match layout.position_panes_in_space(&free_space, Some(tiled_panes_count)) { let positions_in_layout =
Ok(positions_in_layout) => { match layout.position_panes_in_space(&free_space, Some(tiled_panes_count), false) {
Ok(positions_in_layout) => positions_in_layout,
// in the error branch, we try to recover by positioning the panes in the space but
// ignoring the percentage sizes (passing true as the third argument), this is a hack
// around some issues with the constraint system that should be addressed in a systemic
// manner
Err(_e) => layout
.position_panes_in_space(&free_space, Some(tiled_panes_count), true)
.map_err(|e| anyhow!(e))?,
};
let currently_focused_pane_id = let currently_focused_pane_id =
client_id.and_then(|client_id| self.tiled_panes.focused_pane_id(client_id)); client_id.and_then(|client_id| self.tiled_panes.focused_pane_id(client_id));
let mut existing_tab_state = let mut existing_tab_state =
@ -149,8 +157,7 @@ impl<'a> LayoutApplier<'a> {
Some(position_and_size), Some(position_and_size),
); );
pane_focuser.set_pane_id_in_focused_location(layout.focus, &pane); pane_focuser.set_pane_id_in_focused_location(layout.focus, &pane);
pane_focuser pane_focuser.set_expanded_stacked_pane(layout.is_expanded_in_stack, &pane);
.set_expanded_stacked_pane(layout.is_expanded_in_stack, &pane);
resize_pty!(pane, self.os_api, self.senders, self.character_cell_size)?; resize_pty!(pane, self.os_api, self.senders, self.character_cell_size)?;
self.tiled_panes self.tiled_panes
.add_pane_with_existing_geom(pane.pid(), pane); .add_pane_with_existing_geom(pane.pid(), pane);
@ -168,11 +175,7 @@ impl<'a> LayoutApplier<'a> {
layout.focus.unwrap_or(false), layout.focus.unwrap_or(false),
true, true,
) { ) {
self.apply_layout_properties_to_pane( self.apply_layout_properties_to_pane(&mut pane, &layout, Some(position_and_size));
&mut pane,
&layout,
Some(position_and_size),
);
pane_focuser.set_pane_id_in_focused_location(layout.focus, &pane); pane_focuser.set_pane_id_in_focused_location(layout.focus, &pane);
pane_focuser.set_expanded_stacked_pane(layout.is_expanded_in_stack, &pane); pane_focuser.set_expanded_stacked_pane(layout.is_expanded_in_stack, &pane);
resize_pty!(pane, self.os_api, self.senders, self.character_cell_size)?; resize_pty!(pane, self.os_api, self.senders, self.character_cell_size)?;
@ -193,13 +196,6 @@ impl<'a> LayoutApplier<'a> {
self.tiled_panes, self.tiled_panes,
self.draw_pane_frames, self.draw_pane_frames,
); );
},
Err(e) => {
Err::<(), _>(anyError::msg(e))
.with_context(err_context)
.non_fatal(); // TODO: propagate this to the user
},
};
Ok(()) Ok(())
} }
fn apply_tiled_panes_layout( fn apply_tiled_panes_layout(
@ -211,8 +207,17 @@ impl<'a> LayoutApplier<'a> {
) -> Result<()> { ) -> Result<()> {
let err_context = || format!("failed to apply tiled panes layout"); let err_context = || format!("failed to apply tiled panes layout");
let free_space = self.total_space_for_tiled_panes(); let free_space = self.total_space_for_tiled_panes();
match layout.position_panes_in_space(&free_space, None) { let mut positions_in_layout = match layout.position_panes_in_space(&free_space, None, false)
Ok(mut positions_in_layout) => { {
Ok(positions_in_layout) => positions_in_layout,
// in the error branch, we try to recover by positioning the panes in the space but
// ignoring the percentage sizes (passing true as the third argument), this is a hack
// around some issues with the constraint system that should be addressed in a systemic
// manner
Err(_e) => layout
.position_panes_in_space(&free_space, None, true)
.map_err(|e| anyhow!(e))?,
};
let mut run_instructions_to_ignore = layout.run_instructions_to_ignore.clone(); let mut run_instructions_to_ignore = layout.run_instructions_to_ignore.clone();
let mut new_terminal_ids = new_terminal_ids.iter(); let mut new_terminal_ids = new_terminal_ids.iter();
@ -337,10 +342,8 @@ impl<'a> LayoutApplier<'a> {
if let Some(held_command) = hold_for_command { if let Some(held_command) = hold_for_command {
new_pane.hold(None, true, held_command.clone()); new_pane.hold(None, true, held_command.clone());
} }
self.tiled_panes.add_pane_with_existing_geom( self.tiled_panes
PaneId::Terminal(*pid), .add_pane_with_existing_geom(PaneId::Terminal(*pid), Box::new(new_pane));
Box::new(new_pane),
);
set_focus_pane_id(layout, PaneId::Terminal(*pid)); set_focus_pane_id(layout, PaneId::Terminal(*pid));
} }
} }
@ -352,18 +355,6 @@ impl<'a> LayoutApplier<'a> {
} }
self.adjust_viewport().with_context(err_context)?; self.adjust_viewport().with_context(err_context)?;
self.set_focused_tiled_pane(focus_pane_id, client_id); self.set_focused_tiled_pane(focus_pane_id, client_id);
},
Err(e) => {
for (unused_pid, _) in new_terminal_ids {
self.senders
.send_to_pty(PtyInstruction::ClosePane(PaneId::Terminal(unused_pid)))
.with_context(err_context)?;
}
Err::<(), _>(anyError::msg(e))
.with_context(err_context)
.non_fatal(); // TODO: propagate this to the user
},
};
Ok(()) Ok(())
} }
fn apply_floating_panes_layout( fn apply_floating_panes_layout(

View file

@ -249,7 +249,7 @@ impl SwapLayouts {
let pane_count = tiled_panes.visible_panes_count(); let pane_count = tiled_panes.visible_panes_count();
let display_area = PaneGeom::from(&*display_area); let display_area = PaneGeom::from(&*display_area);
if layout if layout
.position_panes_in_space(&display_area, Some(pane_count)) .position_panes_in_space(&display_area, Some(pane_count), false)
.is_ok() .is_ok()
{ {
return Some(layout.clone()); return Some(layout.clone());
@ -279,7 +279,7 @@ impl SwapLayouts {
let pane_count = tiled_panes.visible_panes_count(); let pane_count = tiled_panes.visible_panes_count();
let display_area = PaneGeom::from(&*display_area); let display_area = PaneGeom::from(&*display_area);
if layout if layout
.position_panes_in_space(&display_area, Some(pane_count)) .position_panes_in_space(&display_area, Some(pane_count), false)
.is_ok() .is_ok()
{ {
return Some(layout.clone()); return Some(layout.clone());

View file

@ -849,6 +849,7 @@ impl TiledPaneLayout {
&self, &self,
space: &PaneGeom, space: &PaneGeom,
max_panes: Option<usize>, max_panes: Option<usize>,
ignore_percent_split_sizes: bool,
) -> Result<Vec<(TiledPaneLayout, PaneGeom)>, &'static str> { ) -> Result<Vec<(TiledPaneLayout, PaneGeom)>, &'static str> {
let layouts = match max_panes { let layouts = match max_panes {
Some(max_panes) => { Some(max_panes) => {
@ -874,9 +875,9 @@ impl TiledPaneLayout {
layout_to_split.focus_deepest_pane(); layout_to_split.focus_deepest_pane();
} }
split_space(space, &layout_to_split, space)? split_space(space, &layout_to_split, space, ignore_percent_split_sizes)?
}, },
None => split_space(space, self, space)?, None => split_space(space, self, space, ignore_percent_split_sizes)?,
}; };
for (_pane_layout, pane_geom) in layouts.iter() { for (_pane_layout, pane_geom) in layouts.iter() {
if !pane_geom.is_at_least_minimum_size() { if !pane_geom.is_at_least_minimum_size() {
@ -1426,6 +1427,7 @@ fn split_space(
space_to_split: &PaneGeom, space_to_split: &PaneGeom,
layout: &TiledPaneLayout, layout: &TiledPaneLayout,
total_space_to_split: &PaneGeom, total_space_to_split: &PaneGeom,
ignore_percent_split_sizes: bool,
) -> Result<Vec<(TiledPaneLayout, PaneGeom)>, &'static str> { ) -> Result<Vec<(TiledPaneLayout, PaneGeom)>, &'static str> {
let sizes: Vec<Option<SplitSize>> = if layout.children_are_stacked { let sizes: Vec<Option<SplitSize>> = if layout.children_are_stacked {
let index_of_expanded_pane = layout.children.iter().position(|p| p.is_expanded_in_stack); let index_of_expanded_pane = layout.children.iter().position(|p| p.is_expanded_in_stack);
@ -1440,6 +1442,15 @@ fn split_space(
*last_size = None; *last_size = None;
} }
sizes sizes
} else if ignore_percent_split_sizes {
layout
.children
.iter()
.map(|part| match part.split_size {
Some(SplitSize::Percent(_)) => None,
split_size => split_size,
})
.collect()
} else { } else {
layout.children.iter().map(|part| part.split_size).collect() layout.children.iter().map(|part| part.split_size).collect()
}; };
@ -1539,8 +1550,12 @@ fn split_space(
for (i, part) in layout.children.iter().enumerate() { for (i, part) in layout.children.iter().enumerate() {
let part_position_and_size = split_geom.get(i).unwrap(); let part_position_and_size = split_geom.get(i).unwrap();
if !part.children.is_empty() { if !part.children.is_empty() {
let mut part_positions = let mut part_positions = split_space(
split_space(part_position_and_size, part, total_space_to_split)?; part_position_and_size,
part,
total_space_to_split,
ignore_percent_split_sizes,
)?;
pane_positions.append(&mut part_positions); pane_positions.append(&mut part_positions);
} else { } else {
let part = part.clone(); let part = part.clone();

View file

@ -860,6 +860,9 @@ impl TryFrom<(&KdlNode, &Options)> for Action {
) )
})?; })?;
let swap_tiled_layouts = Some(layout.swap_tiled_layouts.clone());
let swap_floating_layouts = Some(layout.swap_floating_layouts.clone());
let mut tabs = layout.tabs(); let mut tabs = layout.tabs();
if tabs.len() > 1 { if tabs.len() > 1 {
return Err(ConfigError::new_kdl_error( return Err(ConfigError::new_kdl_error(
@ -874,8 +877,8 @@ impl TryFrom<(&KdlNode, &Options)> for Action {
Ok(Action::NewTab( Ok(Action::NewTab(
Some(layout), Some(layout),
floating_panes_layout, floating_panes_layout,
None, swap_tiled_layouts,
None, swap_floating_layouts,
name, name,
)) ))
} else { } else {
@ -884,8 +887,8 @@ impl TryFrom<(&KdlNode, &Options)> for Action {
Ok(Action::NewTab( Ok(Action::NewTab(
Some(layout), Some(layout),
floating_panes_layout, floating_panes_layout,
None, swap_tiled_layouts,
None, swap_floating_layouts,
name, name,
)) ))
} }

View file

@ -122,7 +122,7 @@ impl Dimension {
self.inner += by; self.inner += by;
} }
pub fn decrease_inner(&mut self, by: usize) { pub fn decrease_inner(&mut self, by: usize) {
self.inner -= by; self.inner = self.inner.saturating_sub(by);
} }
pub fn is_fixed(&self) -> bool { pub fn is_fixed(&self) -> bool {