Skip to content

Commit

Permalink
Updated TabLayoutDescription to additionally store: window zoom & foc…
Browse files Browse the repository at this point in the history
…us, and tab focus.
  • Loading branch information
Andrew-Collins committed May 4, 2024
1 parent f1760d6 commit 8dfa990
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 8 deletions.
17 changes: 14 additions & 3 deletions crates/modalkit-ratatui/src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use ratatui::{
use super::{
cmdbar::{CommandBar, CommandBarState},
util::{rect_down, rect_zero_height},
windows::{WindowActions, WindowLayout, WindowLayoutDescription, WindowLayoutState},
windows::{WindowActions, WindowLayout, WindowLayoutState, WindowLayoutStorage},
TerminalCursor,
Window,
WindowOps,
Expand Down Expand Up @@ -246,7 +246,9 @@ fn bold<'a>(s: String) -> Span<'a> {
#[serde(bound(serialize = "I::WindowId: Serialize"))]
pub struct TabLayoutDescription<I: ApplicationInfo> {
/// The description of the window layout for each tab.
pub tabs: Vec<WindowLayoutDescription<I>>,
pub tabs: Vec<WindowLayoutStorage<I>>,
/// The index of the last focused tab
pub focused: usize,
}

impl<I: ApplicationInfo> TabLayoutDescription<I> {
Expand All @@ -256,11 +258,19 @@ impl<I: ApplicationInfo> TabLayoutDescription<I> {
area: Option<Rect>,
store: &mut Store<I>,
) -> UIResult<FocusList<WindowLayoutState<W, I>>, I> {
let focused = self.focused;
self.tabs
.into_iter()
.map(|desc| desc.to_layout(area, store))
.collect::<UIResult<Vec<_>, I>>()
.map(FocusList::new)
.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
})
}
}

Expand Down Expand Up @@ -317,6 +327,7 @@ where
pub fn as_description(&self) -> TabLayoutDescription<I> {
TabLayoutDescription {
tabs: self.tabs.iter().map(WindowLayoutState::as_description).collect(),
focused: self.tabs.pos(),
}
}

Expand Down
45 changes: 41 additions & 4 deletions crates/modalkit-ratatui/src/windows/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,33 @@ where
}
}

/// Data structure holding layout description and state
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(bound(deserialize = "I::WindowId: Deserialize<'de>"))]
#[serde(bound(serialize = "I::WindowId: Serialize"))]
#[serde(rename_all = "lowercase", tag = "type")]
pub struct WindowLayoutStorage<I: ApplicationInfo> {
layout: WindowLayoutDescription<I>,
focused: usize,
zoomed: bool,
}

impl<I> WindowLayoutStorage<I>
where
I: ApplicationInfo,
{
/// Restore a layout from a description of windows and splits.
pub fn to_layout<W: Window<I>>(
self,
area: Option<Rect>,
store: &mut Store<I>,
) -> UIResult<WindowLayoutState<W, I>, I> {
let mut layout = self.layout.to_layout(area, store)?;
layout.focused = 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 @@ -1282,18 +1309,28 @@ where
}

/// Convert this layout to a serializable summary of its windows and splits.
pub fn as_description(&self) -> WindowLayoutDescription<I> {
pub fn as_description(&self) -> WindowLayoutStorage<I> {
let mut children = vec![];
let focused = self.focused;
let zoomed = self.zoom;

let Some(root) = &self.root else {
return WindowLayoutDescription::Split { children, length: None };
return WindowLayoutStorage {
layout: WindowLayoutDescription::Split { children, length: None },
focused,
zoomed,
};
};

for w in root.iter() {
children.push(w.into());
}

return WindowLayoutDescription::Split { children, length: None };
return WindowLayoutStorage {
layout: WindowLayoutDescription::Split { children, length: None },
focused,
zoomed,
};
}

/// Create a new instance containing a single [Window] displaying some content.
Expand Down Expand Up @@ -2855,7 +2892,7 @@ mod tests {
}],
length: None,
};
assert_eq!(desc1, exp);
assert_eq!(desc1.layout, exp);

// Turn back into a layout, and then generate a new description to show it's the same.
let tree = desc1
Expand Down
7 changes: 6 additions & 1 deletion crates/modalkit-ratatui/src/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ mod size;
mod slot;
mod tree;

pub use self::layout::{WindowLayout, WindowLayoutDescription, WindowLayoutState};
pub use self::layout::{
WindowLayout,
WindowLayoutDescription,
WindowLayoutState,
WindowLayoutStorage,
};

struct AxisTreeNode<W, X: AxisT, Y: AxisT> {
value: Value<W, X, Y>,
Expand Down

0 comments on commit 8dfa990

Please sign in to comment.