Skip to content

Commit

Permalink
TabLayoutDescription -> TabbedLayoutDescription
Browse files Browse the repository at this point in the history
  • Loading branch information
ulyssa committed Aug 10, 2024
1 parent 8dfa990 commit 35386c8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
28 changes: 14 additions & 14 deletions crates/modalkit-ratatui/src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<I: ApplicationInfo> {
pub struct TabbedLayoutDescription<I: ApplicationInfo> {
/// The description of the window layout for each tab.
pub tabs: Vec<WindowLayoutStorage<I>>,
/// The index of the last focused tab
pub focused: usize,
}

impl<I: ApplicationInfo> TabLayoutDescription<I> {
impl<I: ApplicationInfo> TabbedLayoutDescription<I> {
/// Create a new collection of tabs from this description.
pub fn to_layout<W: Window<I>>(
self,
area: Option<Rect>,
store: &mut Store<I>,
) -> UIResult<FocusList<WindowLayoutState<W, I>>, I> {
let focused = self.focused;
self.tabs
let mut tabs = self
.tabs
.into_iter()
.map(|desc| desc.to_layout(area, store))
.collect::<UIResult<Vec<_>, 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)
}
}

Expand Down Expand Up @@ -324,8 +324,8 @@ where
}

/// Get a description of the open tabs and their window layouts.
pub fn as_description(&self) -> TabLayoutDescription<I> {
TabLayoutDescription {
pub fn as_description(&self) -> TabbedLayoutDescription<I> {
TabbedLayoutDescription {
tabs: self.tabs.iter().map(WindowLayoutState::as_description).collect(),
focused: self.tabs.pos(),
}
Expand Down
6 changes: 5 additions & 1 deletion crates/modalkit-ratatui/src/windows/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1124,11 +1124,12 @@ where
store: &mut Store<I>,
) -> UIResult<WindowLayoutState<W, I>, 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>"))]
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 35386c8

Please sign in to comment.