Expose Node
and State::layout
for PaneGrid
This commit is contained in:
parent
d1f2a18439
commit
f3d54a0f33
@ -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};
|
||||
|
@ -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,79 +18,6 @@ pub enum Node {
|
||||
}
|
||||
|
||||
impl Node {
|
||||
pub fn find(&mut self, pane: &Pane) -> Option<&mut Node> {
|
||||
match self {
|
||||
Node::Split { a, b, .. } => {
|
||||
a.find(pane).or_else(move || b.find(pane))
|
||||
}
|
||||
Node::Pane(p) => {
|
||||
if p == pane {
|
||||
Some(self)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn split(&mut self, id: Split, axis: Axis, new_pane: Pane) {
|
||||
*self = Node::Split {
|
||||
id,
|
||||
axis,
|
||||
ratio: 500_000,
|
||||
a: Box::new(self.clone()),
|
||||
b: Box::new(Node::Pane(new_pane)),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn update(&mut self, f: &impl Fn(&mut Node)) {
|
||||
match self {
|
||||
Node::Split { a, b, .. } => {
|
||||
a.update(f);
|
||||
b.update(f);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
f(self);
|
||||
}
|
||||
|
||||
pub 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;
|
||||
|
||||
true
|
||||
} else if a.resize(split, percentage) {
|
||||
true
|
||||
} else {
|
||||
b.resize(split, percentage)
|
||||
}
|
||||
}
|
||||
Node::Pane(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn remove(&mut self, pane: &Pane) -> Option<Pane> {
|
||||
match self {
|
||||
Node::Split { a, b, .. } => {
|
||||
if a.pane() == Some(*pane) {
|
||||
*self = *b.clone();
|
||||
Some(self.first_pane())
|
||||
} else if b.pane() == Some(*pane) {
|
||||
*self = *a.clone();
|
||||
Some(self.first_pane())
|
||||
} else {
|
||||
a.remove(pane).or_else(|| b.remove(pane))
|
||||
}
|
||||
}
|
||||
Node::Pane(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn regions(
|
||||
&self,
|
||||
spacing: f32,
|
||||
@ -133,14 +60,87 @@ impl Node {
|
||||
splits
|
||||
}
|
||||
|
||||
pub fn pane(&self) -> Option<Pane> {
|
||||
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))
|
||||
}
|
||||
Node::Pane(p) => {
|
||||
if p == pane {
|
||||
Some(self)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn split(&mut self, id: Split, axis: Axis, new_pane: Pane) {
|
||||
*self = Node::Split {
|
||||
id,
|
||||
axis,
|
||||
ratio: 0.5,
|
||||
a: Box::new(self.clone()),
|
||||
b: Box::new(Node::Pane(new_pane)),
|
||||
};
|
||||
}
|
||||
|
||||
pub(crate) fn update(&mut self, f: &impl Fn(&mut Node)) {
|
||||
match self {
|
||||
Node::Split { a, b, .. } => {
|
||||
a.update(f);
|
||||
b.update(f);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
f(self);
|
||||
}
|
||||
|
||||
pub(crate) fn resize(&mut self, split: &Split, percentage: f32) -> bool {
|
||||
match self {
|
||||
Node::Split {
|
||||
id, ratio, a, b, ..
|
||||
} => {
|
||||
if id == split {
|
||||
*ratio = percentage;
|
||||
|
||||
true
|
||||
} else if a.resize(split, percentage) {
|
||||
true
|
||||
} else {
|
||||
b.resize(split, percentage)
|
||||
}
|
||||
}
|
||||
Node::Pane(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn remove(&mut self, pane: &Pane) -> Option<Pane> {
|
||||
match self {
|
||||
Node::Split { a, b, .. } => {
|
||||
if a.pane() == Some(*pane) {
|
||||
*self = *b.clone();
|
||||
Some(self.first_pane())
|
||||
} else if b.pane() == Some(*pane) {
|
||||
*self = *a.clone();
|
||||
Some(self.first_pane())
|
||||
} else {
|
||||
a.remove(pane).or_else(|| b.remove(pane))
|
||||
}
|
||||
}
|
||||
Node::Pane(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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.
|
||||
|
@ -11,8 +11,8 @@
|
||||
use crate::Renderer;
|
||||
|
||||
pub use iced_native::pane_grid::{
|
||||
Axis, Direction, DragEvent, Focus, KeyPressEvent, Pane, ResizeEvent, Split,
|
||||
State,
|
||||
Axis, Direction, DragEvent, Focus, KeyPressEvent, Node, Pane, ResizeEvent,
|
||||
Split, State,
|
||||
};
|
||||
|
||||
/// A collection of panes distributed using either vertical or horizontal splits
|
||||
|
Loading…
Reference in New Issue
Block a user