From 2ab7341fa50865d6f0c26da59f945321ef839c5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 12 May 2020 10:09:30 +0200 Subject: [PATCH] Implement `State::with_content` in `pane_grid` --- native/src/widget/pane_grid.rs | 2 + native/src/widget/pane_grid/content.rs | 12 +++++ native/src/widget/pane_grid/state.rs | 61 +++++++++++++++++++------- wgpu/src/widget/pane_grid.rs | 4 +- 4 files changed, 62 insertions(+), 17 deletions(-) create mode 100644 native/src/widget/pane_grid/content.rs diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index 4c0eeed2..076ae76f 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -9,6 +9,7 @@ //! [`pane_grid` example]: https://github.com/hecrj/iced/tree/0.1/examples/pane_grid //! [`PaneGrid`]: struct.PaneGrid.html mod axis; +mod content; mod direction; mod node; mod pane; @@ -16,6 +17,7 @@ mod split; mod state; pub use axis::Axis; +pub use content::Content; pub use direction::Direction; pub use node::Node; pub use pane::Pane; diff --git a/native/src/widget/pane_grid/content.rs b/native/src/widget/pane_grid/content.rs new file mode 100644 index 00000000..6b0bd99a --- /dev/null +++ b/native/src/widget/pane_grid/content.rs @@ -0,0 +1,12 @@ +use crate::pane_grid::Axis; + +#[derive(Debug, Clone)] +pub enum Content { + Split { + axis: Axis, + ratio: f32, + a: Box>, + b: Box>, + }, + Pane(T), +} diff --git a/native/src/widget/pane_grid/state.rs b/native/src/widget/pane_grid/state.rs index 4cda818c..41f3cffd 100644 --- a/native/src/widget/pane_grid/state.rs +++ b/native/src/widget/pane_grid/state.rs @@ -1,6 +1,6 @@ use crate::{ keyboard, - pane_grid::{node::Node, Axis, Direction, Pane, Split}, + pane_grid::{Axis, Content, Direction, Node, Pane, Split}, Hasher, Point, Rectangle, Size, }; @@ -53,23 +53,24 @@ impl State { /// [`State`]: struct.State.html /// [`Pane`]: struct.Pane.html pub fn new(first_pane_state: T) -> (Self, Pane) { - let first_pane = Pane(0); + (Self::with_content(Content::Pane(first_pane_state)), Pane(0)) + } + pub fn with_content(content: impl Into>) -> Self { let mut panes = HashMap::new(); - let _ = panes.insert(first_pane, first_pane_state); - ( - State { - panes, - internal: Internal { - layout: Node::Pane(first_pane), - last_id: 0, - action: Action::Idle { focus: None }, - }, - modifiers: keyboard::ModifiersState::default(), + let (layout, last_id) = + Self::distribute_content(&mut panes, content.into(), 0); + + State { + panes, + internal: Internal { + layout, + last_id, + action: Action::Idle { focus: None }, }, - first_pane, - ) + modifiers: keyboard::ModifiersState::default(), + } } /// Returns the total amount of panes in the [`State`]. @@ -110,7 +111,7 @@ impl State { self.panes.iter_mut() } - /// Returns the layout tree stored in the [`State`]. + /// Returns the layout of the [`State`]. /// /// [`State`]: struct.State.html pub fn layout(&self) -> &Node { @@ -266,6 +267,36 @@ impl State { None } } + + fn distribute_content( + panes: &mut HashMap, + content: Content, + next_id: usize, + ) -> (Node, usize) { + match content { + Content::Split { axis, ratio, a, b } => { + let (a, next_id) = Self::distribute_content(panes, *a, next_id); + let (b, next_id) = Self::distribute_content(panes, *b, next_id); + + ( + Node::Split { + id: Split(next_id), + axis, + ratio, + a: Box::new(a), + b: Box::new(b), + }, + next_id + 1, + ) + } + Content::Pane(state) => { + let id = Pane(next_id); + let _ = panes.insert(id, state); + + (Node::Pane(id), next_id + 1) + } + } + } } #[derive(Debug, Clone)] diff --git a/wgpu/src/widget/pane_grid.rs b/wgpu/src/widget/pane_grid.rs index 54438f5c..6f437df7 100644 --- a/wgpu/src/widget/pane_grid.rs +++ b/wgpu/src/widget/pane_grid.rs @@ -11,8 +11,8 @@ use crate::Renderer; pub use iced_native::pane_grid::{ - Axis, Direction, DragEvent, Focus, KeyPressEvent, Node, Pane, ResizeEvent, - Split, State, + Axis, Content, Direction, DragEvent, Focus, KeyPressEvent, Node, Pane, + ResizeEvent, Split, State, }; /// A collection of panes distributed using either vertical or horizontal splits