diff options
author | 2023-11-29 22:28:31 +0100 | |
---|---|---|
committer | 2023-11-29 22:28:31 +0100 | |
commit | e09b4e24dda51b8212d8ece52431dacaa3922a7b (patch) | |
tree | 7005e181528134ebdde5bbbe5909273db9f30174 /widget/src/pane_grid | |
parent | 83c7870c569a2976923ee6243a19813094d44673 (diff) | |
parent | 7f8b17604a31e00becc43130ec516c1a53552c88 (diff) | |
download | iced-e09b4e24dda51b8212d8ece52431dacaa3922a7b.tar.gz iced-e09b4e24dda51b8212d8ece52431dacaa3922a7b.tar.bz2 iced-e09b4e24dda51b8212d8ece52431dacaa3922a7b.zip |
Merge branch 'master' into feat/multi-window-support
Diffstat (limited to 'widget/src/pane_grid')
-rw-r--r-- | widget/src/pane_grid/configuration.rs | 4 | ||||
-rw-r--r-- | widget/src/pane_grid/content.rs | 22 | ||||
-rw-r--r-- | widget/src/pane_grid/node.rs | 16 | ||||
-rw-r--r-- | widget/src/pane_grid/pane.rs | 2 | ||||
-rw-r--r-- | widget/src/pane_grid/split.rs | 2 | ||||
-rw-r--r-- | widget/src/pane_grid/state.rs | 110 | ||||
-rw-r--r-- | widget/src/pane_grid/title_bar.rs | 31 |
7 files changed, 104 insertions, 83 deletions
diff --git a/widget/src/pane_grid/configuration.rs b/widget/src/pane_grid/configuration.rs index ddbc3bc2..b8aa2c7d 100644 --- a/widget/src/pane_grid/configuration.rs +++ b/widget/src/pane_grid/configuration.rs @@ -2,7 +2,7 @@ use crate::pane_grid::Axis; /// The arrangement of a [`PaneGrid`]. /// -/// [`PaneGrid`]: crate::widget::PaneGrid +/// [`PaneGrid`]: super::PaneGrid #[derive(Debug, Clone)] pub enum Configuration<T> { /// A split of the available space. @@ -21,6 +21,6 @@ pub enum Configuration<T> { }, /// A [`Pane`]. /// - /// [`Pane`]: crate::widget::pane_grid::Pane + /// [`Pane`]: super::Pane Pane(T), } diff --git a/widget/src/pane_grid/content.rs b/widget/src/pane_grid/content.rs index c28ae6e3..826ea663 100644 --- a/widget/src/pane_grid/content.rs +++ b/widget/src/pane_grid/content.rs @@ -10,7 +10,7 @@ use crate::pane_grid::{Draggable, TitleBar}; /// The content of a [`Pane`]. /// -/// [`Pane`]: crate::widget::pane_grid::Pane +/// [`Pane`]: super::Pane #[allow(missing_debug_implementations)] pub struct Content<'a, Message, Renderer = crate::Renderer> where @@ -87,7 +87,7 @@ where /// Draws the [`Content`] with the provided [`Renderer`] and [`Layout`]. /// - /// [`Renderer`]: crate::Renderer + /// [`Renderer`]: crate::core::Renderer pub fn draw( &self, tree: &Tree, @@ -150,18 +150,23 @@ where pub(crate) fn layout( &self, + tree: &mut Tree, renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { if let Some(title_bar) = &self.title_bar { let max_size = limits.max(); - let title_bar_layout = title_bar - .layout(renderer, &layout::Limits::new(Size::ZERO, max_size)); + let title_bar_layout = title_bar.layout( + &mut tree.children[1], + renderer, + &layout::Limits::new(Size::ZERO, max_size), + ); let title_bar_size = title_bar_layout.size(); let mut body_layout = self.body.as_widget().layout( + &mut tree.children[0], renderer, &layout::Limits::new( Size::ZERO, @@ -179,7 +184,11 @@ where vec![title_bar_layout, body_layout], ) } else { - self.body.as_widget().layout(renderer, limits) + self.body.as_widget().layout( + &mut tree.children[0], + renderer, + limits, + ) } } @@ -222,6 +231,7 @@ where renderer: &Renderer, clipboard: &mut dyn Clipboard, shell: &mut Shell<'_, Message>, + viewport: &Rectangle, is_picked: bool, ) -> event::Status { let mut event_status = event::Status::Ignored; @@ -237,6 +247,7 @@ where renderer, clipboard, shell, + viewport, ); children.next().unwrap() @@ -255,6 +266,7 @@ where renderer, clipboard, shell, + viewport, ) }; diff --git a/widget/src/pane_grid/node.rs b/widget/src/pane_grid/node.rs index 6de5920f..1f568f95 100644 --- a/widget/src/pane_grid/node.rs +++ b/widget/src/pane_grid/node.rs @@ -5,7 +5,7 @@ use std::collections::BTreeMap; /// A layout node of a [`PaneGrid`]. /// -/// [`PaneGrid`]: crate::widget::PaneGrid +/// [`PaneGrid`]: super::PaneGrid #[derive(Debug, Clone)] pub enum Node { /// The region of this [`Node`] is split into two. @@ -95,13 +95,13 @@ impl Node { splits } - pub(crate) fn find(&mut self, pane: &Pane) -> Option<&mut Node> { + 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 { + if *p == pane { Some(self) } else { None @@ -139,12 +139,12 @@ impl Node { f(self); } - pub(crate) 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 { + if *id == split { *ratio = percentage; true @@ -158,13 +158,13 @@ impl Node { } } - pub(crate) 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) { + if a.pane() == Some(pane) { *self = *b.clone(); Some(self.first_pane()) - } else if b.pane() == Some(*pane) { + } else if b.pane() == Some(pane) { *self = *a.clone(); Some(self.first_pane()) } else { diff --git a/widget/src/pane_grid/pane.rs b/widget/src/pane_grid/pane.rs index d6fbab83..cabf55c1 100644 --- a/widget/src/pane_grid/pane.rs +++ b/widget/src/pane_grid/pane.rs @@ -1,5 +1,5 @@ /// A rectangular region in a [`PaneGrid`] used to display widgets. /// -/// [`PaneGrid`]: crate::widget::PaneGrid +/// [`PaneGrid`]: super::PaneGrid #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Pane(pub(super) usize); diff --git a/widget/src/pane_grid/split.rs b/widget/src/pane_grid/split.rs index 8132272a..ce021978 100644 --- a/widget/src/pane_grid/split.rs +++ b/widget/src/pane_grid/split.rs @@ -1,5 +1,5 @@ /// A divider that splits a region in a [`PaneGrid`] into two different panes. /// -/// [`PaneGrid`]: crate::widget::PaneGrid +/// [`PaneGrid`]: super::PaneGrid #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Split(pub(super) usize); diff --git a/widget/src/pane_grid/state.rs b/widget/src/pane_grid/state.rs index 6fd15890..481cd770 100644 --- a/widget/src/pane_grid/state.rs +++ b/widget/src/pane_grid/state.rs @@ -1,6 +1,6 @@ //! The state of a [`PaneGrid`]. //! -//! [`PaneGrid`]: crate::widget::PaneGrid +//! [`PaneGrid`]: super::PaneGrid use crate::core::{Point, Size}; use crate::pane_grid::{ Axis, Configuration, Direction, Edge, Node, Pane, Region, Split, Target, @@ -18,23 +18,23 @@ use std::collections::HashMap; /// provided to the view function of [`PaneGrid::new`] for displaying each /// [`Pane`]. /// -/// [`PaneGrid`]: crate::widget::PaneGrid -/// [`PaneGrid::new`]: crate::widget::PaneGrid::new +/// [`PaneGrid`]: super::PaneGrid +/// [`PaneGrid::new`]: super::PaneGrid::new #[derive(Debug, Clone)] pub struct State<T> { /// The panes of the [`PaneGrid`]. /// - /// [`PaneGrid`]: crate::widget::PaneGrid + /// [`PaneGrid`]: super::PaneGrid pub panes: HashMap<Pane, T>, /// The internal state of the [`PaneGrid`]. /// - /// [`PaneGrid`]: crate::widget::PaneGrid + /// [`PaneGrid`]: super::PaneGrid pub internal: Internal, /// The maximized [`Pane`] of the [`PaneGrid`]. /// - /// [`PaneGrid`]: crate::widget::PaneGrid + /// [`PaneGrid`]: super::PaneGrid pub(super) maximized: Option<Pane>, } @@ -75,14 +75,14 @@ impl<T> State<T> { } /// Returns the internal state of the given [`Pane`], if it exists. - pub fn get(&self, pane: &Pane) -> Option<&T> { - self.panes.get(pane) + pub fn get(&self, pane: Pane) -> Option<&T> { + self.panes.get(&pane) } /// Returns the internal state of the given [`Pane`] with mutability, if it /// exists. - pub fn get_mut(&mut self, pane: &Pane) -> Option<&mut T> { - self.panes.get_mut(pane) + pub fn get_mut(&mut self, pane: Pane) -> Option<&mut T> { + self.panes.get_mut(&pane) } /// Returns an iterator over all the panes of the [`State`], alongside its @@ -104,13 +104,13 @@ impl<T> State<T> { /// Returns the adjacent [`Pane`] of another [`Pane`] in the given /// direction, if there is one. - pub fn adjacent(&self, pane: &Pane, direction: Direction) -> Option<Pane> { + pub fn adjacent(&self, pane: Pane, direction: Direction) -> Option<Pane> { let regions = self .internal .layout .pane_regions(0.0, Size::new(4096.0, 4096.0)); - let current_region = regions.get(pane)?; + let current_region = regions.get(&pane)?; let target = match direction { Direction::Left => { @@ -142,7 +142,7 @@ impl<T> State<T> { pub fn split( &mut self, axis: Axis, - pane: &Pane, + pane: Pane, state: T, ) -> Option<(Pane, Split)> { self.split_node(axis, Some(pane), state, false) @@ -151,32 +151,32 @@ impl<T> State<T> { /// Split a target [`Pane`] with a given [`Pane`] on a given [`Region`]. /// /// Panes will be swapped by default for [`Region::Center`]. - pub fn split_with(&mut self, target: &Pane, pane: &Pane, region: Region) { + pub fn split_with(&mut self, target: Pane, pane: Pane, region: Region) { match region { Region::Center => self.swap(pane, target), Region::Edge(edge) => match edge { Edge::Top => { - self.split_and_swap(Axis::Horizontal, target, pane, true) + self.split_and_swap(Axis::Horizontal, target, pane, true); } Edge::Bottom => { - self.split_and_swap(Axis::Horizontal, target, pane, false) + self.split_and_swap(Axis::Horizontal, target, pane, false); } Edge::Left => { - self.split_and_swap(Axis::Vertical, target, pane, true) + self.split_and_swap(Axis::Vertical, target, pane, true); } Edge::Right => { - self.split_and_swap(Axis::Vertical, target, pane, false) + self.split_and_swap(Axis::Vertical, target, pane, false); } }, } } /// Drops the given [`Pane`] into the provided [`Target`]. - pub fn drop(&mut self, pane: &Pane, target: Target) { + pub fn drop(&mut self, pane: Pane, target: Target) { match target { Target::Edge(edge) => self.move_to_edge(pane, edge), Target::Pane(target, region) => { - self.split_with(&target, pane, region) + self.split_with(target, pane, region); } } } @@ -184,7 +184,7 @@ impl<T> State<T> { fn split_node( &mut self, axis: Axis, - pane: Option<&Pane>, + pane: Option<Pane>, state: T, inverse: bool, ) -> Option<(Pane, Split)> { @@ -222,33 +222,35 @@ impl<T> State<T> { fn split_and_swap( &mut self, axis: Axis, - target: &Pane, - pane: &Pane, + target: Pane, + pane: Pane, swap: bool, ) { if let Some((state, _)) = self.close(pane) { if let Some((new_pane, _)) = self.split(axis, target, state) { if swap { - self.swap(target, &new_pane); + self.swap(target, new_pane); } } } } /// Move [`Pane`] to an [`Edge`] of the [`PaneGrid`]. - pub fn move_to_edge(&mut self, pane: &Pane, edge: Edge) { + /// + /// [`PaneGrid`]: super::PaneGrid + pub fn move_to_edge(&mut self, pane: Pane, edge: Edge) { match edge { Edge::Top => { - self.split_major_node_and_swap(Axis::Horizontal, pane, true) + self.split_major_node_and_swap(Axis::Horizontal, pane, true); } Edge::Bottom => { - self.split_major_node_and_swap(Axis::Horizontal, pane, false) + self.split_major_node_and_swap(Axis::Horizontal, pane, false); } Edge::Left => { - self.split_major_node_and_swap(Axis::Vertical, pane, true) + self.split_major_node_and_swap(Axis::Vertical, pane, true); } Edge::Right => { - self.split_major_node_and_swap(Axis::Vertical, pane, false) + self.split_major_node_and_swap(Axis::Vertical, pane, false); } } } @@ -256,7 +258,7 @@ impl<T> State<T> { fn split_major_node_and_swap( &mut self, axis: Axis, - pane: &Pane, + pane: Pane, swap: bool, ) { if let Some((state, _)) = self.close(pane) { @@ -269,16 +271,16 @@ impl<T> State<T> { /// If you want to swap panes on drag and drop in your [`PaneGrid`], you /// will need to call this method when handling a [`DragEvent`]. /// - /// [`PaneGrid`]: crate::widget::PaneGrid - /// [`DragEvent`]: crate::widget::pane_grid::DragEvent - pub fn swap(&mut self, a: &Pane, b: &Pane) { + /// [`PaneGrid`]: super::PaneGrid + /// [`DragEvent`]: super::DragEvent + pub fn swap(&mut self, a: Pane, b: Pane) { self.internal.layout.update(&|node| match node { Node::Split { .. } => {} Node::Pane(pane) => { - if pane == a { - *node = Node::Pane(*b); - } else if pane == b { - *node = Node::Pane(*a); + if *pane == a { + *node = Node::Pane(b); + } else if *pane == b { + *node = Node::Pane(a); } } }); @@ -292,21 +294,21 @@ impl<T> State<T> { /// If you want to enable resize interactions in your [`PaneGrid`], you will /// need to call this method when handling a [`ResizeEvent`]. /// - /// [`PaneGrid`]: crate::widget::PaneGrid - /// [`ResizeEvent`]: crate::widget::pane_grid::ResizeEvent - pub fn resize(&mut self, split: &Split, ratio: f32) { + /// [`PaneGrid`]: super::PaneGrid + /// [`ResizeEvent`]: super::ResizeEvent + pub fn resize(&mut self, split: Split, ratio: f32) { let _ = self.internal.layout.resize(split, ratio); } /// Closes the given [`Pane`] and returns its internal state and its closest /// sibling, if it exists. - pub fn close(&mut self, pane: &Pane) -> Option<(T, Pane)> { - if self.maximized == Some(*pane) { + pub fn close(&mut self, pane: Pane) -> Option<(T, Pane)> { + if self.maximized == Some(pane) { let _ = self.maximized.take(); } if let Some(sibling) = self.internal.layout.remove(pane) { - self.panes.remove(pane).map(|state| (state, sibling)) + self.panes.remove(&pane).map(|state| (state, sibling)) } else { None } @@ -315,22 +317,22 @@ impl<T> State<T> { /// Maximize the given [`Pane`]. Only this pane will be rendered by the /// [`PaneGrid`] until [`Self::restore()`] is called. /// - /// [`PaneGrid`]: crate::widget::PaneGrid - pub fn maximize(&mut self, pane: &Pane) { - self.maximized = Some(*pane); + /// [`PaneGrid`]: super::PaneGrid + pub fn maximize(&mut self, pane: Pane) { + self.maximized = Some(pane); } /// Restore the currently maximized [`Pane`] to it's normal size. All panes /// will be rendered by the [`PaneGrid`]. /// - /// [`PaneGrid`]: crate::widget::PaneGrid + /// [`PaneGrid`]: super::PaneGrid pub fn restore(&mut self) { let _ = self.maximized.take(); } /// Returns the maximized [`Pane`] of the [`PaneGrid`]. /// - /// [`PaneGrid`]: crate::widget::PaneGrid + /// [`PaneGrid`]: super::PaneGrid pub fn maximized(&self) -> Option<Pane> { self.maximized } @@ -338,7 +340,7 @@ impl<T> State<T> { /// The internal state of a [`PaneGrid`]. /// -/// [`PaneGrid`]: crate::widget::PaneGrid +/// [`PaneGrid`]: super::PaneGrid #[derive(Debug, Clone)] pub struct Internal { layout: Node, @@ -349,7 +351,7 @@ impl Internal { /// Initializes the [`Internal`] state of a [`PaneGrid`] from a /// [`Configuration`]. /// - /// [`PaneGrid`]: crate::widget::PaneGrid + /// [`PaneGrid`]: super::PaneGrid pub fn from_configuration<T>( panes: &mut HashMap<Pane, T>, content: Configuration<T>, @@ -394,16 +396,16 @@ impl Internal { /// The current action of a [`PaneGrid`]. /// -/// [`PaneGrid`]: crate::widget::PaneGrid +/// [`PaneGrid`]: super::PaneGrid #[derive(Debug, Clone, Copy, PartialEq)] pub enum Action { /// The [`PaneGrid`] is idle. /// - /// [`PaneGrid`]: crate::widget::PaneGrid + /// [`PaneGrid`]: super::PaneGrid Idle, /// A [`Pane`] in the [`PaneGrid`] is being dragged. /// - /// [`PaneGrid`]: crate::widget::PaneGrid + /// [`PaneGrid`]: super::PaneGrid Dragging { /// The [`Pane`] being dragged. pane: Pane, @@ -412,7 +414,7 @@ pub enum Action { }, /// A [`Split`] in the [`PaneGrid`] is being dragged. /// - /// [`PaneGrid`]: crate::widget::PaneGrid + /// [`PaneGrid`]: super::PaneGrid Resizing { /// The [`Split`] being dragged. split: Split, diff --git a/widget/src/pane_grid/title_bar.rs b/widget/src/pane_grid/title_bar.rs index 2fe79f80..f4dbb6b1 100644 --- a/widget/src/pane_grid/title_bar.rs +++ b/widget/src/pane_grid/title_bar.rs @@ -11,7 +11,7 @@ use crate::core::{ /// The title bar of a [`Pane`]. /// -/// [`Pane`]: crate::widget::pane_grid::Pane +/// [`Pane`]: super::Pane #[allow(missing_debug_implementations)] pub struct TitleBar<'a, Message, Renderer = crate::Renderer> where @@ -75,7 +75,7 @@ where /// [`TitleBar`] is hovered. /// /// [`controls`]: Self::controls - /// [`Pane`]: crate::widget::pane_grid::Pane + /// [`Pane`]: super::Pane pub fn always_show_controls(mut self) -> Self { self.always_show_controls = true; self @@ -114,7 +114,7 @@ where /// Draws the [`TitleBar`] with the provided [`Renderer`] and [`Layout`]. /// - /// [`Renderer`]: crate::Renderer + /// [`Renderer`]: crate::core::Renderer pub fn draw( &self, tree: &Tree, @@ -213,23 +213,27 @@ where pub(crate) fn layout( &self, + tree: &mut Tree, renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { let limits = limits.pad(self.padding); let max_size = limits.max(); - let title_layout = self - .content - .as_widget() - .layout(renderer, &layout::Limits::new(Size::ZERO, max_size)); + let title_layout = self.content.as_widget().layout( + &mut tree.children[0], + renderer, + &layout::Limits::new(Size::ZERO, max_size), + ); let title_size = title_layout.size(); let mut node = if let Some(controls) = &self.controls { - let mut controls_layout = controls - .as_widget() - .layout(renderer, &layout::Limits::new(Size::ZERO, max_size)); + let mut controls_layout = controls.as_widget().layout( + &mut tree.children[1], + renderer, + &layout::Limits::new(Size::ZERO, max_size), + ); let controls_size = controls_layout.size(); let space_before_controls = max_size.width - controls_size.width; @@ -282,7 +286,7 @@ where controls_layout, renderer, operation, - ) + ); }; if show_title { @@ -291,7 +295,7 @@ where title_layout, renderer, operation, - ) + ); } } @@ -304,6 +308,7 @@ where renderer: &Renderer, clipboard: &mut dyn Clipboard, shell: &mut Shell<'_, Message>, + viewport: &Rectangle, ) -> event::Status { let mut children = layout.children(); let padded = children.next().unwrap(); @@ -328,6 +333,7 @@ where renderer, clipboard, shell, + viewport, ) } else { event::Status::Ignored @@ -342,6 +348,7 @@ where renderer, clipboard, shell, + viewport, ) } else { event::Status::Ignored |