Implement pure version of PaneGrid widget

This commit is contained in:
Héctor Ramón Jiménez 2022-03-10 19:25:57 +07:00
parent 9f27969d14
commit 6dd187ff08
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
13 changed files with 2107 additions and 417 deletions

View file

@ -1,3 +1,4 @@
//! The state of a [`PaneGrid`].
use crate::widget::pane_grid::{
Axis, Configuration, Direction, Node, Pane, Split,
};
@ -19,8 +20,13 @@ use std::collections::{BTreeMap, HashMap};
/// [`PaneGrid::new`]: crate::widget::PaneGrid::new
#[derive(Debug, Clone)]
pub struct State<T> {
pub(super) panes: HashMap<Pane, T>,
pub(super) internal: Internal,
/// The panes of the [`PaneGrid`].
pub panes: HashMap<Pane, T>,
/// The internal state of the [`PaneGrid`].
pub internal: Internal,
pub(super) action: Action,
}
impl<T> State<T> {
@ -39,16 +45,13 @@ impl<T> State<T> {
pub fn with_configuration(config: impl Into<Configuration<T>>) -> Self {
let mut panes = HashMap::new();
let (layout, last_id) =
Self::distribute_content(&mut panes, config.into(), 0);
let internal =
Internal::from_configuration(&mut panes, config.into(), 0);
State {
panes,
internal: Internal {
layout,
last_id,
action: Action::Idle,
},
internal,
action: Action::Idle,
}
}
@ -192,16 +195,34 @@ impl<T> State<T> {
None
}
}
}
fn distribute_content(
/// The internal state of a [`PaneGrid`].
#[derive(Debug, Clone)]
pub struct Internal {
layout: Node,
last_id: usize,
}
impl Internal {
/// Initializes the [`Internal`] state of a [`PaneGrid`] from a
/// [`Configuration`].
pub fn from_configuration<T>(
panes: &mut HashMap<Pane, T>,
content: Configuration<T>,
next_id: usize,
) -> (Node, usize) {
match content {
) -> Self {
let (layout, last_id) = match content {
Configuration::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);
let Internal {
layout: a,
last_id: next_id,
} = Self::from_configuration(panes, *a, next_id);
let Internal {
layout: b,
last_id: next_id,
} = Self::from_configuration(panes, *b, next_id);
(
Node::Split {
@ -220,39 +241,53 @@ impl<T> State<T> {
(Node::Pane(id), next_id + 1)
}
}
};
Self { layout, last_id }
}
}
#[derive(Debug, Clone)]
pub struct Internal {
layout: Node,
last_id: usize,
action: Action,
}
/// The current action of a [`PaneGrid`].
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Action {
/// The [`PaneGrid`] is idle.
Idle,
Dragging { pane: Pane, origin: Point },
Resizing { split: Split, axis: Axis },
/// A [`Pane`] in the [`PaneGrid`] is being dragged.
Dragging {
/// The [`Pane`] being dragged.
pane: Pane,
/// The starting [`Point`] of the drag interaction.
origin: Point,
},
/// A [`Split`] in the [`PaneGrid`] is being dragged.
Resizing {
/// The [`Split`] being dragged.
split: Split,
/// The [`Axis`] of the [`Split`].
axis: Axis,
},
}
impl Internal {
impl Action {
/// Returns the current [`Pane`] that is being dragged, if any.
pub fn picked_pane(&self) -> Option<(Pane, Point)> {
match self.action {
match *self {
Action::Dragging { pane, origin, .. } => Some((pane, origin)),
_ => None,
}
}
/// Returns the current [`Split`] that is being dragged, if any.
pub fn picked_split(&self) -> Option<(Split, Axis)> {
match self.action {
match *self {
Action::Resizing { split, axis, .. } => Some((split, axis)),
_ => None,
}
}
}
impl Internal {
/// Calculates the current [`Pane`] regions from the [`PaneGrid`] layout.
pub fn pane_regions(
&self,
spacing: f32,
@ -261,6 +296,7 @@ impl Internal {
self.layout.pane_regions(spacing, size)
}
/// Calculates the current [`Split`] regions from the [`PaneGrid`] layout.
pub fn split_regions(
&self,
spacing: f32,
@ -268,28 +304,4 @@ impl Internal {
) -> BTreeMap<Split, (Axis, Rectangle, f32)> {
self.layout.split_regions(spacing, size)
}
pub fn pick_pane(&mut self, pane: &Pane, origin: Point) {
self.action = Action::Dragging {
pane: *pane,
origin,
};
}
pub fn pick_split(&mut self, split: &Split, axis: Axis) {
// TODO: Obtain `axis` from layout itself. Maybe we should implement
// `Node::find_split`
if self.picked_pane().is_some() {
return;
}
self.action = Action::Resizing {
split: *split,
axis,
};
}
pub fn idle(&mut self) {
self.action = Action::Idle;
}
}