diff options
author | 2020-05-12 10:09:30 +0200 | |
---|---|---|
committer | 2020-05-22 21:56:50 +0200 | |
commit | 2ab7341fa50865d6f0c26da59f945321ef839c5f (patch) | |
tree | 10dfdfee1f3ff0c02a3137f3f7f2a15f9c50fa28 /native | |
parent | 32b9c1fdbd217c664aae40f5852013e682d480df (diff) | |
download | iced-2ab7341fa50865d6f0c26da59f945321ef839c5f.tar.gz iced-2ab7341fa50865d6f0c26da59f945321ef839c5f.tar.bz2 iced-2ab7341fa50865d6f0c26da59f945321ef839c5f.zip |
Implement `State::with_content` in `pane_grid`
Diffstat (limited to 'native')
-rw-r--r-- | native/src/widget/pane_grid.rs | 2 | ||||
-rw-r--r-- | native/src/widget/pane_grid/content.rs | 12 | ||||
-rw-r--r-- | native/src/widget/pane_grid/state.rs | 63 |
3 files changed, 61 insertions, 16 deletions
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<T> { + Split { + axis: Axis, + ratio: f32, + a: Box<Content<T>>, + b: Box<Content<T>>, + }, + 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<T> State<T> { /// [`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<Content<T>>) -> 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<T> State<T> { 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<T> State<T> { None } } + + fn distribute_content( + panes: &mut HashMap<Pane, T>, + content: Content<T>, + 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)] |