iced_widget/pane_grid/
state.rs

1//! The state of a [`PaneGrid`].
2//!
3//! [`PaneGrid`]: super::PaneGrid
4use crate::core::{Point, Size};
5use crate::pane_grid::{
6    Axis, Configuration, Direction, Edge, Node, Pane, Region, Split, Target,
7};
8
9use std::borrow::Cow;
10use std::collections::BTreeMap;
11
12/// The state of a [`PaneGrid`].
13///
14/// It keeps track of the state of each [`Pane`] and the position of each
15/// [`Split`].
16///
17/// The [`State`] needs to own any mutable contents a [`Pane`] may need. This is
18/// why this struct is generic over the type `T`. Values of this type are
19/// provided to the view function of [`PaneGrid::new`] for displaying each
20/// [`Pane`].
21///
22/// [`PaneGrid`]: super::PaneGrid
23/// [`PaneGrid::new`]: super::PaneGrid::new
24#[derive(Debug, Clone)]
25pub struct State<T> {
26    /// The panes of the [`PaneGrid`].
27    ///
28    /// [`PaneGrid`]: super::PaneGrid
29    pub panes: BTreeMap<Pane, T>,
30
31    /// The internal state of the [`PaneGrid`].
32    ///
33    /// [`PaneGrid`]: super::PaneGrid
34    pub internal: Internal,
35}
36
37impl<T> State<T> {
38    /// Creates a new [`State`], initializing the first pane with the provided
39    /// state.
40    ///
41    /// Alongside the [`State`], it returns the first [`Pane`] identifier.
42    pub fn new(first_pane_state: T) -> (Self, Pane) {
43        (
44            Self::with_configuration(Configuration::Pane(first_pane_state)),
45            Pane(0),
46        )
47    }
48
49    /// Creates a new [`State`] with the given [`Configuration`].
50    pub fn with_configuration(config: impl Into<Configuration<T>>) -> Self {
51        let mut panes = BTreeMap::default();
52
53        let internal =
54            Internal::from_configuration(&mut panes, config.into(), 0);
55
56        State { panes, internal }
57    }
58
59    /// Returns the total amount of panes in the [`State`].
60    pub fn len(&self) -> usize {
61        self.panes.len()
62    }
63
64    /// Returns `true` if the amount of panes in the [`State`] is 0.
65    pub fn is_empty(&self) -> bool {
66        self.len() == 0
67    }
68
69    /// Returns the internal state of the given [`Pane`], if it exists.
70    pub fn get(&self, pane: Pane) -> Option<&T> {
71        self.panes.get(&pane)
72    }
73
74    /// Returns the internal state of the given [`Pane`] with mutability, if it
75    /// exists.
76    pub fn get_mut(&mut self, pane: Pane) -> Option<&mut T> {
77        self.panes.get_mut(&pane)
78    }
79
80    /// Returns an iterator over all the panes of the [`State`], alongside its
81    /// internal state.
82    pub fn iter(&self) -> impl Iterator<Item = (&Pane, &T)> {
83        self.panes.iter()
84    }
85
86    /// Returns a mutable iterator over all the panes of the [`State`],
87    /// alongside its internal state.
88    pub fn iter_mut(&mut self) -> impl Iterator<Item = (&Pane, &mut T)> {
89        self.panes.iter_mut()
90    }
91
92    /// Returns the layout of the [`State`].
93    pub fn layout(&self) -> &Node {
94        &self.internal.layout
95    }
96
97    /// Returns the adjacent [`Pane`] of another [`Pane`] in the given
98    /// direction, if there is one.
99    pub fn adjacent(&self, pane: Pane, direction: Direction) -> Option<Pane> {
100        let regions = self
101            .internal
102            .layout
103            .pane_regions(0.0, Size::new(4096.0, 4096.0));
104
105        let current_region = regions.get(&pane)?;
106
107        let target = match direction {
108            Direction::Left => {
109                Point::new(current_region.x - 1.0, current_region.y + 1.0)
110            }
111            Direction::Right => Point::new(
112                current_region.x + current_region.width + 1.0,
113                current_region.y + 1.0,
114            ),
115            Direction::Up => {
116                Point::new(current_region.x + 1.0, current_region.y - 1.0)
117            }
118            Direction::Down => Point::new(
119                current_region.x + 1.0,
120                current_region.y + current_region.height + 1.0,
121            ),
122        };
123
124        let mut colliding_regions =
125            regions.iter().filter(|(_, region)| region.contains(target));
126
127        let (pane, _) = colliding_regions.next()?;
128
129        Some(*pane)
130    }
131
132    /// Splits the given [`Pane`] into two in the given [`Axis`] and
133    /// initializing the new [`Pane`] with the provided internal state.
134    pub fn split(
135        &mut self,
136        axis: Axis,
137        pane: Pane,
138        state: T,
139    ) -> Option<(Pane, Split)> {
140        self.split_node(axis, Some(pane), state, false)
141    }
142
143    /// Split a target [`Pane`] with a given [`Pane`] on a given [`Region`].
144    ///
145    /// Panes will be swapped by default for [`Region::Center`].
146    pub fn split_with(&mut self, target: Pane, pane: Pane, region: Region) {
147        match region {
148            Region::Center => self.swap(pane, target),
149            Region::Edge(edge) => match edge {
150                Edge::Top => {
151                    self.split_and_swap(Axis::Horizontal, target, pane, true);
152                }
153                Edge::Bottom => {
154                    self.split_and_swap(Axis::Horizontal, target, pane, false);
155                }
156                Edge::Left => {
157                    self.split_and_swap(Axis::Vertical, target, pane, true);
158                }
159                Edge::Right => {
160                    self.split_and_swap(Axis::Vertical, target, pane, false);
161                }
162            },
163        }
164    }
165
166    /// Drops the given [`Pane`] into the provided [`Target`].
167    pub fn drop(&mut self, pane: Pane, target: Target) {
168        match target {
169            Target::Edge(edge) => self.move_to_edge(pane, edge),
170            Target::Pane(target, region) => {
171                self.split_with(target, pane, region);
172            }
173        }
174    }
175
176    fn split_node(
177        &mut self,
178        axis: Axis,
179        pane: Option<Pane>,
180        state: T,
181        inverse: bool,
182    ) -> Option<(Pane, Split)> {
183        let node = if let Some(pane) = pane {
184            self.internal.layout.find(pane)?
185        } else {
186            // Major node
187            &mut self.internal.layout
188        };
189
190        let new_pane = {
191            self.internal.last_id = self.internal.last_id.checked_add(1)?;
192
193            Pane(self.internal.last_id)
194        };
195
196        let new_split = {
197            self.internal.last_id = self.internal.last_id.checked_add(1)?;
198
199            Split(self.internal.last_id)
200        };
201
202        if inverse {
203            node.split_inverse(new_split, axis, new_pane);
204        } else {
205            node.split(new_split, axis, new_pane);
206        }
207
208        let _ = self.panes.insert(new_pane, state);
209        let _ = self.internal.maximized.take();
210
211        Some((new_pane, new_split))
212    }
213
214    fn split_and_swap(
215        &mut self,
216        axis: Axis,
217        target: Pane,
218        pane: Pane,
219        swap: bool,
220    ) {
221        if let Some((state, _)) = self.close(pane) {
222            if let Some((new_pane, _)) = self.split(axis, target, state) {
223                // Ensure new node corresponds to original closed `Pane` for state continuity
224                self.relabel(new_pane, pane);
225
226                if swap {
227                    self.swap(target, pane);
228                }
229            }
230        }
231    }
232
233    /// Move [`Pane`] to an [`Edge`] of the [`PaneGrid`].
234    ///
235    /// [`PaneGrid`]: super::PaneGrid
236    pub fn move_to_edge(&mut self, pane: Pane, edge: Edge) {
237        match edge {
238            Edge::Top => {
239                self.split_major_node_and_swap(Axis::Horizontal, pane, true);
240            }
241            Edge::Bottom => {
242                self.split_major_node_and_swap(Axis::Horizontal, pane, false);
243            }
244            Edge::Left => {
245                self.split_major_node_and_swap(Axis::Vertical, pane, true);
246            }
247            Edge::Right => {
248                self.split_major_node_and_swap(Axis::Vertical, pane, false);
249            }
250        }
251    }
252
253    fn split_major_node_and_swap(
254        &mut self,
255        axis: Axis,
256        pane: Pane,
257        inverse: bool,
258    ) {
259        if let Some((state, _)) = self.close(pane) {
260            if let Some((new_pane, _)) =
261                self.split_node(axis, None, state, inverse)
262            {
263                // Ensure new node corresponds to original closed `Pane` for state continuity
264                self.relabel(new_pane, pane);
265            }
266        }
267    }
268
269    fn relabel(&mut self, target: Pane, label: Pane) {
270        self.swap(target, label);
271
272        let _ = self
273            .panes
274            .remove(&target)
275            .and_then(|state| self.panes.insert(label, state));
276    }
277
278    /// Swaps the position of the provided panes in the [`State`].
279    ///
280    /// If you want to swap panes on drag and drop in your [`PaneGrid`], you
281    /// will need to call this method when handling a [`DragEvent`].
282    ///
283    /// [`PaneGrid`]: super::PaneGrid
284    /// [`DragEvent`]: super::DragEvent
285    pub fn swap(&mut self, a: Pane, b: Pane) {
286        self.internal.layout.update(&|node| match node {
287            Node::Split { .. } => {}
288            Node::Pane(pane) => {
289                if *pane == a {
290                    *node = Node::Pane(b);
291                } else if *pane == b {
292                    *node = Node::Pane(a);
293                }
294            }
295        });
296    }
297
298    /// Resizes two panes by setting the position of the provided [`Split`].
299    ///
300    /// The ratio is a value in [0, 1], representing the exact position of a
301    /// [`Split`] between two panes.
302    ///
303    /// If you want to enable resize interactions in your [`PaneGrid`], you will
304    /// need to call this method when handling a [`ResizeEvent`].
305    ///
306    /// [`PaneGrid`]: super::PaneGrid
307    /// [`ResizeEvent`]: super::ResizeEvent
308    pub fn resize(&mut self, split: Split, ratio: f32) {
309        let _ = self.internal.layout.resize(split, ratio);
310    }
311
312    /// Closes the given [`Pane`] and returns its internal state and its closest
313    /// sibling, if it exists.
314    pub fn close(&mut self, pane: Pane) -> Option<(T, Pane)> {
315        if self.internal.maximized == Some(pane) {
316            let _ = self.internal.maximized.take();
317        }
318
319        if let Some(sibling) = self.internal.layout.remove(pane) {
320            self.panes.remove(&pane).map(|state| (state, sibling))
321        } else {
322            None
323        }
324    }
325
326    /// Maximize the given [`Pane`]. Only this pane will be rendered by the
327    /// [`PaneGrid`] until [`Self::restore()`] is called.
328    ///
329    /// [`PaneGrid`]: super::PaneGrid
330    pub fn maximize(&mut self, pane: Pane) {
331        self.internal.maximized = Some(pane);
332    }
333
334    /// Restore the currently maximized [`Pane`] to it's normal size. All panes
335    /// will be rendered by the [`PaneGrid`].
336    ///
337    /// [`PaneGrid`]: super::PaneGrid
338    pub fn restore(&mut self) {
339        let _ = self.internal.maximized.take();
340    }
341
342    /// Returns the maximized [`Pane`] of the [`PaneGrid`].
343    ///
344    /// [`PaneGrid`]: super::PaneGrid
345    pub fn maximized(&self) -> Option<Pane> {
346        self.internal.maximized
347    }
348}
349
350/// The internal state of a [`PaneGrid`].
351///
352/// [`PaneGrid`]: super::PaneGrid
353#[derive(Debug, Clone)]
354pub struct Internal {
355    layout: Node,
356    last_id: usize,
357    maximized: Option<Pane>,
358}
359
360impl Internal {
361    /// Initializes the [`Internal`] state of a [`PaneGrid`] from a
362    /// [`Configuration`].
363    ///
364    /// [`PaneGrid`]: super::PaneGrid
365    pub fn from_configuration<T>(
366        panes: &mut BTreeMap<Pane, T>,
367        content: Configuration<T>,
368        next_id: usize,
369    ) -> Self {
370        let (layout, last_id) = match content {
371            Configuration::Split { axis, ratio, a, b } => {
372                let Internal {
373                    layout: a,
374                    last_id: next_id,
375                    ..
376                } = Self::from_configuration(panes, *a, next_id);
377
378                let Internal {
379                    layout: b,
380                    last_id: next_id,
381                    ..
382                } = Self::from_configuration(panes, *b, next_id);
383
384                (
385                    Node::Split {
386                        id: Split(next_id),
387                        axis,
388                        ratio,
389                        a: Box::new(a),
390                        b: Box::new(b),
391                    },
392                    next_id + 1,
393                )
394            }
395            Configuration::Pane(state) => {
396                let id = Pane(next_id);
397                let _ = panes.insert(id, state);
398
399                (Node::Pane(id), next_id + 1)
400            }
401        };
402
403        Self {
404            layout,
405            last_id,
406            maximized: None,
407        }
408    }
409
410    pub(super) fn layout(&self) -> Cow<'_, Node> {
411        match self.maximized {
412            Some(pane) => Cow::Owned(Node::Pane(pane)),
413            None => Cow::Borrowed(&self.layout),
414        }
415    }
416
417    pub(super) fn maximized(&self) -> Option<Pane> {
418        self.maximized
419    }
420}
421
422/// The current action of a [`PaneGrid`].
423///
424/// [`PaneGrid`]: super::PaneGrid
425#[derive(Debug, Clone, Copy, PartialEq, Default)]
426pub enum Action {
427    /// The [`PaneGrid`] is idle.
428    ///
429    /// [`PaneGrid`]: super::PaneGrid
430    #[default]
431    Idle,
432    /// A [`Pane`] in the [`PaneGrid`] is being dragged.
433    ///
434    /// [`PaneGrid`]: super::PaneGrid
435    Dragging {
436        /// The [`Pane`] being dragged.
437        pane: Pane,
438        /// The starting [`Point`] of the drag interaction.
439        origin: Point,
440    },
441    /// A [`Split`] in the [`PaneGrid`] is being dragged.
442    ///
443    /// [`PaneGrid`]: super::PaneGrid
444    Resizing {
445        /// The [`Split`] being dragged.
446        split: Split,
447        /// The [`Axis`] of the [`Split`].
448        axis: Axis,
449    },
450}
451
452impl Action {
453    /// Returns the current [`Pane`] that is being dragged, if any.
454    pub fn picked_pane(&self) -> Option<(Pane, Point)> {
455        match *self {
456            Action::Dragging { pane, origin, .. } => Some((pane, origin)),
457            _ => None,
458        }
459    }
460
461    /// Returns the current [`Split`] that is being dragged, if any.
462    pub fn picked_split(&self) -> Option<(Split, Axis)> {
463        match *self {
464            Action::Resizing { split, axis, .. } => Some((split, axis)),
465            _ => None,
466        }
467    }
468}