Remove duplicated maximized state in pane_grid

This commit is contained in:
Héctor Ramón Jiménez 2024-10-24 13:47:28 +02:00
parent 5ebd8ac83f
commit 659669dd58
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
2 changed files with 59 additions and 45 deletions

View file

@ -92,8 +92,6 @@ use crate::core::{
Pixels, Point, Rectangle, Shell, Size, Theme, Vector, Widget, Pixels, Point, Rectangle, Shell, Size, Theme, Vector, Widget,
}; };
use std::borrow::Cow;
const DRAG_DEADBAND_DISTANCE: f32 = 10.0; const DRAG_DEADBAND_DISTANCE: f32 = 10.0;
const THICKNESS_RATIO: f32 = 25.0; const THICKNESS_RATIO: f32 = 25.0;
@ -162,7 +160,6 @@ pub struct PaneGrid<
internal: &'a state::Internal, internal: &'a state::Internal,
panes: Vec<Pane>, panes: Vec<Pane>,
contents: Vec<Content<'a, Message, Theme, Renderer>>, contents: Vec<Content<'a, Message, Theme, Renderer>>,
maximized: Option<Pane>,
width: Length, width: Length,
height: Length, height: Length,
spacing: f32, spacing: f32,
@ -189,8 +186,8 @@ where
let contents = state let contents = state
.panes .panes
.iter() .iter()
.map(|(pane, pane_state)| match &state.maximized { .map(|(pane, pane_state)| match state.maximized() {
Some(p) if pane == p => view(*pane, pane_state, true), Some(p) if *pane == p => view(*pane, pane_state, true),
_ => view(*pane, pane_state, false), _ => view(*pane, pane_state, false),
}) })
.collect(); .collect();
@ -199,7 +196,6 @@ where
internal: &state.internal, internal: &state.internal,
panes, panes,
contents, contents,
maximized: state.maximized,
width: Length::Fill, width: Length::Fill,
height: Length::Fill, height: Length::Fill,
spacing: 0.0, spacing: 0.0,
@ -244,7 +240,7 @@ where
where where
F: 'a + Fn(DragEvent) -> Message, F: 'a + Fn(DragEvent) -> Message,
{ {
if self.maximized.is_none() { if self.internal.maximized().is_none() {
self.on_drag = Some(Box::new(f)); self.on_drag = Some(Box::new(f));
} }
self self
@ -263,7 +259,7 @@ where
where where
F: 'a + Fn(ResizeEvent) -> Message, F: 'a + Fn(ResizeEvent) -> Message,
{ {
if self.maximized.is_none() { if self.internal.maximized().is_none() {
self.on_resize = Some((leeway.into().0, Box::new(f))); self.on_resize = Some((leeway.into().0, Box::new(f)));
} }
self self
@ -291,17 +287,12 @@ where
} }
fn drag_enabled(&self) -> bool { fn drag_enabled(&self) -> bool {
(self.maximized.is_none()) self.internal
.maximized()
.is_none()
.then(|| self.on_drag.is_some()) .then(|| self.on_drag.is_some())
.unwrap_or_default() .unwrap_or_default()
} }
fn node(&self) -> Cow<'_, Node> {
match self.maximized {
Some(pane) => Cow::Owned(Node::Pane(pane)),
None => Cow::Borrowed(&self.internal.layout),
}
}
} }
impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer> impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
@ -351,7 +342,6 @@ where
); );
let state::Widget { panes, .. } = tree.state.downcast_mut(); let state::Widget { panes, .. } = tree.state.downcast_mut();
panes.clone_from(&self.panes); panes.clone_from(&self.panes);
} }
@ -369,7 +359,7 @@ where
limits: &layout::Limits, limits: &layout::Limits,
) -> layout::Node { ) -> layout::Node {
let size = limits.resolve(self.width, self.height, Size::ZERO); let size = limits.resolve(self.width, self.height, Size::ZERO);
let regions = self.node().pane_regions(self.spacing, size); let regions = self.internal.layout().pane_regions(self.spacing, size);
let children = self let children = self
.panes .panes
@ -378,7 +368,11 @@ where
.zip(&self.contents) .zip(&self.contents)
.zip(tree.children.iter_mut()) .zip(tree.children.iter_mut())
.filter_map(|((pane, content), tree)| { .filter_map(|((pane, content), tree)| {
if self.maximized.is_some() && Some(pane) != self.maximized { if self
.internal
.maximized()
.is_some_and(|maximized| maximized != pane)
{
return Some(layout::Node::new(Size::ZERO)); return Some(layout::Node::new(Size::ZERO));
} }
@ -413,7 +407,9 @@ where
.zip(&mut tree.children) .zip(&mut tree.children)
.zip(layout.children()) .zip(layout.children())
.filter(|(((pane, _), _), _)| { .filter(|(((pane, _), _), _)| {
self.maximized.map_or(true, |maximized| *pane == maximized) self.internal
.maximized()
.map_or(true, |maximized| *pane == maximized)
}) })
.for_each(|(((_, content), state), layout)| { .for_each(|(((_, content), state), layout)| {
content.operate(state, layout, renderer, operation); content.operate(state, layout, renderer, operation);
@ -435,7 +431,7 @@ where
let mut event_status = event::Status::Ignored; let mut event_status = event::Status::Ignored;
let state::Widget { action, .. } = tree.state.downcast_mut(); let state::Widget { action, .. } = tree.state.downcast_mut();
let node = self.node(); let node = self.internal.layout();
let on_drag = if self.drag_enabled() { let on_drag = if self.drag_enabled() {
&self.on_drag &self.on_drag
@ -616,7 +612,9 @@ where
.zip(&mut tree.children) .zip(&mut tree.children)
.zip(layout.children()) .zip(layout.children())
.filter(|(((pane, _), _), _)| { .filter(|(((pane, _), _), _)| {
self.maximized.map_or(true, |maximized| *pane == maximized) self.internal
.maximized()
.map_or(true, |maximized| *pane == maximized)
}) })
.map(|(((pane, content), tree), layout)| { .map(|(((pane, content), tree), layout)| {
let is_picked = picked_pane == Some(pane); let is_picked = picked_pane == Some(pane);
@ -651,7 +649,7 @@ where
} }
let resize_leeway = self.on_resize.as_ref().map(|(leeway, _)| *leeway); let resize_leeway = self.on_resize.as_ref().map(|(leeway, _)| *leeway);
let node = self.node(); let node = self.internal.layout();
let resize_axis = let resize_axis =
action.picked_split().map(|(_, axis)| axis).or_else(|| { action.picked_split().map(|(_, axis)| axis).or_else(|| {
@ -690,7 +688,9 @@ where
.zip(&tree.children) .zip(&tree.children)
.zip(layout.children()) .zip(layout.children())
.filter(|(((pane, _), _), _)| { .filter(|(((pane, _), _), _)| {
self.maximized.map_or(true, |maximized| *pane == maximized) self.internal
.maximized()
.map_or(true, |maximized| *pane == maximized)
}) })
.map(|(((_, content), tree), layout)| { .map(|(((_, content), tree), layout)| {
content.mouse_interaction( content.mouse_interaction(
@ -718,7 +718,7 @@ where
) { ) {
let state::Widget { action, .. } = let state::Widget { action, .. } =
tree.state.downcast_ref::<state::Widget>(); tree.state.downcast_ref::<state::Widget>();
let node = self.node(); let node = self.internal.layout();
let resize_leeway = self.on_resize.as_ref().map(|(leeway, _)| *leeway); let resize_leeway = self.on_resize.as_ref().map(|(leeway, _)| *leeway);
let picked_pane = action.picked_pane().filter(|(_, origin)| { let picked_pane = action.picked_pane().filter(|(_, origin)| {
@ -797,7 +797,9 @@ where
.zip(&tree.children) .zip(&tree.children)
.zip(layout.children()) .zip(layout.children())
.filter(|(((pane, _), _), _)| { .filter(|(((pane, _), _), _)| {
self.maximized.map_or(true, |maximized| maximized == *pane) self.internal
.maximized()
.map_or(true, |maximized| maximized == *pane)
}) })
{ {
match picked_pane { match picked_pane {
@ -940,7 +942,11 @@ where
.zip(&mut tree.children) .zip(&mut tree.children)
.zip(layout.children()) .zip(layout.children())
.filter_map(|(((pane, content), state), layout)| { .filter_map(|(((pane, content), state), layout)| {
if self.maximized.is_some() && Some(pane) != self.maximized { if self
.internal
.maximized()
.is_some_and(|maximized| maximized != pane)
{
return None; return None;
} }

View file

@ -6,6 +6,7 @@ use crate::pane_grid::{
Axis, Configuration, Direction, Edge, Node, Pane, Region, Split, Target, Axis, Configuration, Direction, Edge, Node, Pane, Region, Split, Target,
}; };
use std::borrow::Cow;
use std::collections::BTreeMap; use std::collections::BTreeMap;
/// The state of a [`PaneGrid`]. /// The state of a [`PaneGrid`].
@ -31,11 +32,6 @@ pub struct State<T> {
/// ///
/// [`PaneGrid`]: super::PaneGrid /// [`PaneGrid`]: super::PaneGrid
pub internal: Internal, pub internal: Internal,
/// The maximized [`Pane`] of the [`PaneGrid`].
///
/// [`PaneGrid`]: super::PaneGrid
pub(super) maximized: Option<Pane>,
} }
impl<T> State<T> { impl<T> State<T> {
@ -57,11 +53,7 @@ impl<T> State<T> {
let internal = let internal =
Internal::from_configuration(&mut panes, config.into(), 0); Internal::from_configuration(&mut panes, config.into(), 0);
State { State { panes, internal }
panes,
internal,
maximized: None,
}
} }
/// Returns the total amount of panes in the [`State`]. /// Returns the total amount of panes in the [`State`].
@ -214,7 +206,7 @@ impl<T> State<T> {
} }
let _ = self.panes.insert(new_pane, state); let _ = self.panes.insert(new_pane, state);
let _ = self.maximized.take(); let _ = self.internal.maximized.take();
Some((new_pane, new_split)) Some((new_pane, new_split))
} }
@ -319,8 +311,8 @@ impl<T> State<T> {
/// Closes the given [`Pane`] and returns its internal state and its closest /// Closes the given [`Pane`] and returns its internal state and its closest
/// sibling, if it exists. /// sibling, if it exists.
pub fn close(&mut self, pane: Pane) -> Option<(T, Pane)> { pub fn close(&mut self, pane: Pane) -> Option<(T, Pane)> {
if self.maximized == Some(pane) { if self.internal.maximized == Some(pane) {
let _ = self.maximized.take(); let _ = self.internal.maximized.take();
} }
if let Some(sibling) = self.internal.layout.remove(pane) { if let Some(sibling) = self.internal.layout.remove(pane) {
@ -335,7 +327,7 @@ impl<T> State<T> {
/// ///
/// [`PaneGrid`]: super::PaneGrid /// [`PaneGrid`]: super::PaneGrid
pub fn maximize(&mut self, pane: Pane) { pub fn maximize(&mut self, pane: Pane) {
self.maximized = Some(pane); self.internal.maximized = Some(pane);
} }
/// Restore the currently maximized [`Pane`] to it's normal size. All panes /// Restore the currently maximized [`Pane`] to it's normal size. All panes
@ -343,14 +335,14 @@ impl<T> State<T> {
/// ///
/// [`PaneGrid`]: super::PaneGrid /// [`PaneGrid`]: super::PaneGrid
pub fn restore(&mut self) { pub fn restore(&mut self) {
let _ = self.maximized.take(); let _ = self.internal.maximized.take();
} }
/// Returns the maximized [`Pane`] of the [`PaneGrid`]. /// Returns the maximized [`Pane`] of the [`PaneGrid`].
/// ///
/// [`PaneGrid`]: super::PaneGrid /// [`PaneGrid`]: super::PaneGrid
pub fn maximized(&self) -> Option<Pane> { pub fn maximized(&self) -> Option<Pane> {
self.maximized self.internal.maximized
} }
} }
@ -359,8 +351,9 @@ impl<T> State<T> {
/// [`PaneGrid`]: super::PaneGrid /// [`PaneGrid`]: super::PaneGrid
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Internal { pub struct Internal {
pub(super) layout: Node, layout: Node,
last_id: usize, last_id: usize,
maximized: Option<Pane>,
} }
impl Internal { impl Internal {
@ -406,7 +399,22 @@ impl Internal {
} }
}; };
Self { layout, last_id } Self {
layout,
last_id,
maximized: None,
}
}
pub(super) fn layout(&self) -> Cow<'_, Node> {
match self.maximized {
Some(pane) => Cow::Owned(Node::Pane(pane)),
None => Cow::Borrowed(&self.layout),
}
}
pub(super) fn maximized(&self) -> Option<Pane> {
self.maximized
} }
} }