diff options
Diffstat (limited to '')
| -rw-r--r-- | native/src/widget/pane_grid.rs | 1 | ||||
| -rw-r--r-- | native/src/widget/pane_grid/node.rs | 137 | ||||
| -rw-r--r-- | native/src/widget/pane_grid/state.rs | 7 | 
3 files changed, 87 insertions, 58 deletions
| diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index cb623b29..4c0eeed2 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -17,6 +17,7 @@ mod state;  pub use axis::Axis;  pub use direction::Direction; +pub use node::Node;  pub use pane::Pane;  pub use split::Split;  pub use state::{Focus, State}; diff --git a/native/src/widget/pane_grid/node.rs b/native/src/widget/pane_grid/node.rs index 4d5970b8..1b6633fa 100644 --- a/native/src/widget/pane_grid/node.rs +++ b/native/src/widget/pane_grid/node.rs @@ -5,12 +5,12 @@ use crate::{  use std::collections::HashMap; -#[derive(Debug, Clone, Hash)] +#[derive(Debug, Clone)]  pub enum Node {      Split {          id: Split,          axis: Axis, -        ratio: u32, +        ratio: f32,          a: Box<Node>,          b: Box<Node>,      }, @@ -18,7 +18,49 @@ pub enum Node {  }  impl Node { -    pub fn find(&mut self, pane: &Pane) -> Option<&mut Node> { +    pub fn regions( +        &self, +        spacing: f32, +        size: Size, +    ) -> HashMap<Pane, Rectangle> { +        let mut regions = HashMap::new(); + +        self.compute_regions( +            spacing / 2.0, +            &Rectangle { +                x: 0.0, +                y: 0.0, +                width: size.width, +                height: size.height, +            }, +            &mut regions, +        ); + +        regions +    } + +    pub fn splits( +        &self, +        spacing: f32, +        size: Size, +    ) -> HashMap<Split, (Axis, Rectangle, f32)> { +        let mut splits = HashMap::new(); + +        self.compute_splits( +            spacing / 2.0, +            &Rectangle { +                x: 0.0, +                y: 0.0, +                width: size.width, +                height: size.height, +            }, +            &mut splits, +        ); + +        splits +    } + +    pub(crate) fn find(&mut self, pane: &Pane) -> Option<&mut Node> {          match self {              Node::Split { a, b, .. } => {                  a.find(pane).or_else(move || b.find(pane)) @@ -33,17 +75,17 @@ impl Node {          }      } -    pub fn split(&mut self, id: Split, axis: Axis, new_pane: Pane) { +    pub(crate) fn split(&mut self, id: Split, axis: Axis, new_pane: Pane) {          *self = Node::Split {              id,              axis, -            ratio: 500_000, +            ratio: 0.5,              a: Box::new(self.clone()),              b: Box::new(Node::Pane(new_pane)),          };      } -    pub fn update(&mut self, f: &impl Fn(&mut Node)) { +    pub(crate) fn update(&mut self, f: &impl Fn(&mut Node)) {          match self {              Node::Split { a, b, .. } => {                  a.update(f); @@ -55,13 +97,13 @@ impl Node {          f(self);      } -    pub fn resize(&mut self, split: &Split, percentage: f32) -> bool { +    pub(crate) fn resize(&mut self, split: &Split, percentage: f32) -> bool {          match self {              Node::Split {                  id, ratio, a, b, ..              } => {                  if id == split { -                    *ratio = (percentage * 1_000_000.0).round() as u32; +                    *ratio = percentage;                      true                  } else if a.resize(split, percentage) { @@ -74,7 +116,7 @@ impl Node {          }      } -    pub fn remove(&mut self, pane: &Pane) -> Option<Pane> { +    pub(crate) fn remove(&mut self, pane: &Pane) -> Option<Pane> {          match self {              Node::Split { a, b, .. } => {                  if a.pane() == Some(*pane) { @@ -91,56 +133,14 @@ impl Node {          }      } -    pub fn regions( -        &self, -        spacing: f32, -        size: Size, -    ) -> HashMap<Pane, Rectangle> { -        let mut regions = HashMap::new(); - -        self.compute_regions( -            spacing / 2.0, -            &Rectangle { -                x: 0.0, -                y: 0.0, -                width: size.width, -                height: size.height, -            }, -            &mut regions, -        ); - -        regions -    } - -    pub fn splits( -        &self, -        spacing: f32, -        size: Size, -    ) -> HashMap<Split, (Axis, Rectangle, f32)> { -        let mut splits = HashMap::new(); - -        self.compute_splits( -            spacing / 2.0, -            &Rectangle { -                x: 0.0, -                y: 0.0, -                width: size.width, -                height: size.height, -            }, -            &mut splits, -        ); - -        splits -    } - -    pub fn pane(&self) -> Option<Pane> { +    fn pane(&self) -> Option<Pane> {          match self {              Node::Split { .. } => None,              Node::Pane(pane) => Some(*pane),          }      } -    pub fn first_pane(&self) -> Pane { +    fn first_pane(&self) -> Pane {          match self {              Node::Split { a, .. } => a.first_pane(),              Node::Pane(pane) => *pane, @@ -157,9 +157,8 @@ impl Node {              Node::Split {                  axis, ratio, a, b, ..              } => { -                let ratio = *ratio as f32 / 1_000_000.0;                  let (region_a, region_b) = -                    axis.split(current, ratio, halved_spacing); +                    axis.split(current, *ratio, halved_spacing);                  a.compute_regions(halved_spacing, ®ion_a, regions);                  b.compute_regions(halved_spacing, ®ion_b, regions); @@ -184,11 +183,10 @@ impl Node {                  b,                  id,              } => { -                let ratio = *ratio as f32 / 1_000_000.0;                  let (region_a, region_b) = -                    axis.split(current, ratio, halved_spacing); +                    axis.split(current, *ratio, halved_spacing); -                let _ = splits.insert(*id, (*axis, *current, ratio)); +                let _ = splits.insert(*id, (*axis, *current, *ratio));                  a.compute_splits(halved_spacing, ®ion_a, splits);                  b.compute_splits(halved_spacing, ®ion_b, splits); @@ -197,3 +195,26 @@ impl Node {          }      }  } + +impl std::hash::Hash for Node { +    fn hash<H: std::hash::Hasher>(&self, state: &mut H) { +        match self { +            Node::Split { +                id, +                axis, +                ratio, +                a, +                b, +            } => { +                id.hash(state); +                axis.hash(state); +                ((ratio * 100_000.0) as u32).hash(state); +                a.hash(state); +                b.hash(state); +            } +            Node::Pane(pane) => { +                pane.hash(state); +            } +        } +    } +} diff --git a/native/src/widget/pane_grid/state.rs b/native/src/widget/pane_grid/state.rs index 6bb3fd9c..5cb41984 100644 --- a/native/src/widget/pane_grid/state.rs +++ b/native/src/widget/pane_grid/state.rs @@ -102,6 +102,13 @@ impl<T> State<T> {          self.panes.iter_mut()      } +    /// Returns the layout tree stored in the [`State`]. +    /// +    /// [`State`]: struct.State.html +    pub fn layout(&self) -> &Node { +        &self.internal.layout +    } +      /// Returns the active [`Pane`] of the [`State`], if there is one.      ///      /// A [`Pane`] is active if it is focused and is __not__ being dragged. | 
