Skip to content

Commit

Permalink
WindowLayoutStorage -> WindowLayoutRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
ulyssa committed Aug 10, 2024
1 parent 35386c8 commit 200ad03
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 10 deletions.
15 changes: 14 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/modalkit-ratatui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ serde = { version = "^1.0", features = ["derive"] }

[dev-dependencies]
rand = { workspace = true }
serde_json = "1.0.122"
4 changes: 2 additions & 2 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, WindowLayoutState, WindowLayoutStorage},
windows::{WindowActions, WindowLayout, WindowLayoutRoot, WindowLayoutState},
TerminalCursor,
Window,
WindowOps,
Expand Down Expand Up @@ -246,7 +246,7 @@ fn bold<'a>(s: String) -> Span<'a> {
#[serde(bound(serialize = "I::WindowId: Serialize"))]
pub struct TabbedLayoutDescription<I: ApplicationInfo> {
/// The description of the window layout for each tab.
pub tabs: Vec<WindowLayoutStorage<I>>,
pub tabs: Vec<WindowLayoutRoot<I>>,
/// The index of the last focused tab
pub focused: usize,
}
Expand Down
17 changes: 11 additions & 6 deletions crates/modalkit-ratatui/src/windows/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1106,14 +1106,14 @@ where
#[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> {
#[serde(rename_all = "lowercase")]
pub struct WindowLayoutRoot<I: ApplicationInfo> {
layout: WindowLayoutDescription<I>,
focused: usize,
zoomed: bool,
}

impl<I> WindowLayoutStorage<I>
impl<I> WindowLayoutRoot<I>
where
I: ApplicationInfo,
{
Expand Down Expand Up @@ -1310,13 +1310,13 @@ where
}

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

let Some(root) = &self.root else {
return WindowLayoutStorage {
return WindowLayoutRoot {
layout: WindowLayoutDescription::Split { children, length: None },
focused,
zoomed,
Expand All @@ -1327,7 +1327,7 @@ where
children.push(w.into());
}

return WindowLayoutStorage {
return WindowLayoutRoot {
layout: WindowLayoutDescription::Split { children, length: None },
focused,
zoomed,
Expand Down Expand Up @@ -2904,5 +2904,10 @@ mod tests {
.to_layout::<TestWindow>(tree.info.area.into(), &mut store)
.unwrap();
assert_eq!(tree.as_description(), desc1);

// Test against an example JSON serialization to test naming.
let serialized = serde_json::to_string_pretty(&desc1).unwrap();
let exp = include_str!("../../tests/window-layout.json");
assert_eq!(serialized, exp.trim_end());
}
}
2 changes: 1 addition & 1 deletion crates/modalkit-ratatui/src/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ mod tree;
pub use self::layout::{
WindowLayout,
WindowLayoutDescription,
WindowLayoutRoot,
WindowLayoutState,
WindowLayoutStorage,
};

struct AxisTreeNode<W, X: AxisT, Y: AxisT> {
Expand Down
91 changes: 91 additions & 0 deletions crates/modalkit-ratatui/tests/window-layout.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{
"layout": {
"type": "split",
"children": [
{
"type": "split",
"children": [
{
"type": "split",
"children": [
{
"type": "split",
"children": [
{
"type": "window",
"window": 0,
"length": 20
},
{
"type": "window",
"window": 1,
"length": 20
}
],
"length": 20
},
{
"type": "split",
"children": [
{
"type": "window",
"window": 2,
"length": 20
},
{
"type": "window",
"window": 3,
"length": 20
}
],
"length": 20
},
{
"type": "split",
"children": [
{
"type": "window",
"window": 4,
"length": 20
},
{
"type": "window",
"window": 5,
"length": 20
}
],
"length": 20
}
],
"length": 40
},
{
"type": "split",
"children": [
{
"type": "window",
"window": 6,
"length": 20
},
{
"type": "window",
"window": 7,
"length": 20
},
{
"type": "window",
"window": 8,
"length": 20
}
],
"length": 20
}
],
"length": 60
}
],
"length": null
},
"focused": 3,
"zoomed": false
}

0 comments on commit 200ad03

Please sign in to comment.