Implement State::with_content in pane_grid

This commit is contained in:
Héctor Ramón Jiménez 2020-05-12 10:09:30 +02:00
parent 32b9c1fdbd
commit 2ab7341fa5
4 changed files with 62 additions and 17 deletions

View File

@ -9,6 +9,7 @@
//! [`pane_grid` example]: https://github.com/hecrj/iced/tree/0.1/examples/pane_grid //! [`pane_grid` example]: https://github.com/hecrj/iced/tree/0.1/examples/pane_grid
//! [`PaneGrid`]: struct.PaneGrid.html //! [`PaneGrid`]: struct.PaneGrid.html
mod axis; mod axis;
mod content;
mod direction; mod direction;
mod node; mod node;
mod pane; mod pane;
@ -16,6 +17,7 @@ mod split;
mod state; mod state;
pub use axis::Axis; pub use axis::Axis;
pub use content::Content;
pub use direction::Direction; pub use direction::Direction;
pub use node::Node; pub use node::Node;
pub use pane::Pane; pub use pane::Pane;

View File

@ -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),
}

View File

@ -1,6 +1,6 @@
use crate::{ use crate::{
keyboard, keyboard,
pane_grid::{node::Node, Axis, Direction, Pane, Split}, pane_grid::{Axis, Content, Direction, Node, Pane, Split},
Hasher, Point, Rectangle, Size, Hasher, Point, Rectangle, Size,
}; };
@ -53,23 +53,24 @@ impl<T> State<T> {
/// [`State`]: struct.State.html /// [`State`]: struct.State.html
/// [`Pane`]: struct.Pane.html /// [`Pane`]: struct.Pane.html
pub fn new(first_pane_state: T) -> (Self, Pane) { 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 mut panes = HashMap::new();
let _ = panes.insert(first_pane, first_pane_state);
( let (layout, last_id) =
State { Self::distribute_content(&mut panes, content.into(), 0);
panes,
internal: Internal { State {
layout: Node::Pane(first_pane), panes,
last_id: 0, internal: Internal {
action: Action::Idle { focus: None }, layout,
}, last_id,
modifiers: keyboard::ModifiersState::default(), action: Action::Idle { focus: None },
}, },
first_pane, modifiers: keyboard::ModifiersState::default(),
) }
} }
/// Returns the total amount of panes in the [`State`]. /// Returns the total amount of panes in the [`State`].
@ -110,7 +111,7 @@ impl<T> State<T> {
self.panes.iter_mut() self.panes.iter_mut()
} }
/// Returns the layout tree stored in the [`State`]. /// Returns the layout of the [`State`].
/// ///
/// [`State`]: struct.State.html /// [`State`]: struct.State.html
pub fn layout(&self) -> &Node { pub fn layout(&self) -> &Node {
@ -266,6 +267,36 @@ impl<T> State<T> {
None 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)] #[derive(Debug, Clone)]

View File

@ -11,8 +11,8 @@
use crate::Renderer; use crate::Renderer;
pub use iced_native::pane_grid::{ pub use iced_native::pane_grid::{
Axis, Direction, DragEvent, Focus, KeyPressEvent, Node, Pane, ResizeEvent, Axis, Content, Direction, DragEvent, Focus, KeyPressEvent, Node, Pane,
Split, State, ResizeEvent, Split, State,
}; };
/// A collection of panes distributed using either vertical or horizontal splits /// A collection of panes distributed using either vertical or horizontal splits