From 35386c834c1e314973f6187d746510ce8cb00604 Mon Sep 17 00:00:00 2001 From: Ulyssa Date: Fri, 9 Aug 2024 20:01:18 -0700 Subject: [PATCH] TabLayoutDescription -> TabbedLayoutDescription --- crates/modalkit-ratatui/src/screen.rs | 28 +++++++++---------- crates/modalkit-ratatui/src/windows/layout.rs | 6 +++- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/crates/modalkit-ratatui/src/screen.rs b/crates/modalkit-ratatui/src/screen.rs index 3052f51..8137982 100644 --- a/crates/modalkit-ratatui/src/screen.rs +++ b/crates/modalkit-ratatui/src/screen.rs @@ -244,33 +244,33 @@ fn bold<'a>(s: String) -> Span<'a> { #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(bound(deserialize = "I::WindowId: Deserialize<'de>"))] #[serde(bound(serialize = "I::WindowId: Serialize"))] -pub struct TabLayoutDescription { +pub struct TabbedLayoutDescription { /// The description of the window layout for each tab. pub tabs: Vec>, /// The index of the last focused tab pub focused: usize, } -impl TabLayoutDescription { +impl TabbedLayoutDescription { /// Create a new collection of tabs from this description. pub fn to_layout>( self, area: Option, store: &mut Store, ) -> UIResult>, I> { - let focused = self.focused; - self.tabs + let mut tabs = self + .tabs .into_iter() .map(|desc| desc.to_layout(area, store)) .collect::, I>>() - .map(|layout| { - let mut focus = FocusList::new(layout); - let ctx = EditContext::default(); - // Count starts at 1 - let change = FocusChange::Offset(Count::Exact(focused + 1), true); - focus.focus(&change, &ctx); - focus - }) + .map(FocusList::new)?; + + // Count starts at 1 + let change = FocusChange::Offset(Count::Exact(self.focused + 1), true); + let ctx = EditContext::default(); + tabs.focus(&change, &ctx); + + Ok(tabs) } } @@ -324,8 +324,8 @@ where } /// Get a description of the open tabs and their window layouts. - pub fn as_description(&self) -> TabLayoutDescription { - TabLayoutDescription { + pub fn as_description(&self) -> TabbedLayoutDescription { + TabbedLayoutDescription { tabs: self.tabs.iter().map(WindowLayoutState::as_description).collect(), focused: self.tabs.pos(), } diff --git a/crates/modalkit-ratatui/src/windows/layout.rs b/crates/modalkit-ratatui/src/windows/layout.rs index d19a223..f130f33 100644 --- a/crates/modalkit-ratatui/src/windows/layout.rs +++ b/crates/modalkit-ratatui/src/windows/layout.rs @@ -1124,11 +1124,12 @@ where store: &mut Store, ) -> UIResult, I> { let mut layout = self.layout.to_layout(area, store)?; - layout.focused = self.focused; + layout._focus(self.focused); layout.zoom = self.zoomed; Ok(layout) } } + /// A description of a window layout. #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] #[serde(bound(deserialize = "I::WindowId: Deserialize<'de>"))] @@ -2845,6 +2846,7 @@ mod tests { let (mut tree, mut store, _) = three_by_three(); let mut buffer = Buffer::empty(Rect::new(0, 0, 60, 60)); let area = Rect::new(0, 0, 60, 60); + tree._focus(3); // Draw so that everything gets an initial area. WindowLayout::new(&mut store).render(area, &mut buffer, &mut tree); @@ -2893,6 +2895,8 @@ mod tests { length: None, }; assert_eq!(desc1.layout, exp); + assert_eq!(desc1.focused, 3); + assert_eq!(desc1.zoomed, false); // Turn back into a layout, and then generate a new description to show it's the same. let tree = desc1