egui_stack 0.25.1

Docking system for egui, continuing egui_dock
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
use egui::Rect;

use crate::TabIndex;

mod grid;
mod leaf;
mod linear;
pub(crate) mod shares;
pub use grid::{GridLayout, GridNode};
pub use leaf::LeafNode;
pub use linear::{LinearDir, LinearNode};

/// Represents an abstract node of a [`Tree`](crate::Tree).
///
/// More kinds of containers may come: the enum is `#[non_exhaustive]`, so a `match` outside
/// this crate needs a wildcard arm, and [`Node::children`] walks any container.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[non_exhaustive]
pub enum Node<Tab> {
    /// Empty node.
    Empty,

    /// Contains the actual tabs.
    Leaf(LeafNode<Tab>),

    /// Parent node: two or more children in a row or a column, each with a share of the space.
    Linear(LinearNode),

    /// Parent node: two or more children in rows and columns, each column and each row with a
    /// share of the space.
    Grid(GridNode),
}

impl<Tab> Node<Tab> {
    /// Constructs a leaf node with a given `tab`.
    #[inline(always)]
    pub fn leaf(tab: Tab) -> Self {
        Self::Leaf(LeafNode::new(vec![tab]))
    }

    /// Get immutable access to the leaf data of this node, if it contains any (i.e is a leaf)
    pub fn get_leaf(&self) -> Option<&LeafNode<Tab>> {
        match self {
            Node::Leaf(leaf_node) => Some(leaf_node),
            _ => None,
        }
    }

    /// Get mutable access to the leaf data of this node, if it contains any (i.e is a leaf)
    pub fn get_leaf_mut(&mut self) -> Option<&mut LeafNode<Tab>> {
        match self {
            Node::Leaf(leaf_node) => Some(leaf_node),
            _ => None,
        }
    }

    /// Constructs a leaf node with a given list of `tabs`.
    #[inline(always)]
    pub const fn leaf_with(tabs: Vec<Tab>) -> Self {
        Self::Leaf(LeafNode::new(tabs))
    }

    /// Sets the area occupied by the node.
    ///
    /// If the node is a ``Node::Empty``, this will do nothing.
    #[inline]
    pub fn set_rect(&mut self, new_rect: Rect) {
        match self {
            Self::Empty => (),
            Self::Leaf(leaf) => leaf.set_rect(new_rect),
            Self::Linear(linear) => linear.set_rect(new_rect),
            Self::Grid(grid) => grid.set_rect(new_rect),
        }
    }

    /// Get a [`Rect`] occupied by the node, could be used e.g. to draw a highlight rect around a node.
    ///
    /// Returns [`None`] if node is of the [`Empty`](Node::Empty) variant.
    #[inline]
    pub fn rect(&self) -> Option<Rect> {
        match self {
            Self::Empty => None,
            Self::Leaf(leaf) => Some(leaf.rect()),
            Self::Linear(linear) => Some(linear.rect()),
            Self::Grid(grid) => Some(grid.rect()),
        }
    }

    /// Returns `true` if the node is a [`Empty`](Node::Empty), otherwise `false`.
    #[inline(always)]
    pub const fn is_empty(&self) -> bool {
        matches!(self, Self::Empty)
    }

    /// Returns `true` if the node is a [`Leaf`](Node::Leaf), otherwise `false`.
    #[inline(always)]
    pub const fn is_leaf(&self) -> bool {
        matches!(self, Self::Leaf { .. })
    }

    /// Returns `true` if the node is a [`Linear`](Node::Linear) laying out its children
    /// side by side, otherwise `false`.
    #[inline(always)]
    pub const fn is_horizontal(&self) -> bool {
        matches!(
            self,
            Self::Linear(LinearNode {
                dir: LinearDir::Horizontal,
                ..
            })
        )
    }

    /// Returns `true` if the node is a [`Linear`](Node::Linear) laying out its children one
    /// above the other, otherwise `false`.
    #[inline(always)]
    pub const fn is_vertical(&self) -> bool {
        matches!(
            self,
            Self::Linear(LinearNode {
                dir: LinearDir::Vertical,
                ..
            })
        )
    }

    /// Returns `true` if the node has children, otherwise `false`.
    #[inline(always)]
    pub const fn is_parent(&self) -> bool {
        matches!(self, Self::Linear(_) | Self::Grid(_))
    }

    /// Returns `true` if the node is a [`Grid`](Node::Grid).
    #[inline(always)]
    pub const fn is_grid(&self) -> bool {
        matches!(self, Self::Grid(_))
    }

    /// Get immutable access to the grid data of this node, if it is a [`Grid`](Node::Grid).
    pub fn get_grid(&self) -> Option<&GridNode> {
        match self {
            Node::Grid(grid) => Some(grid),
            _ => None,
        }
    }

    /// Get mutable access to the grid data of this node, if it is a [`Grid`](Node::Grid).
    pub fn get_grid_mut(&mut self) -> Option<&mut GridNode> {
        match self {
            Node::Grid(grid) => Some(grid),
            _ => None,
        }
    }

    /// Get immutable access to the linear data of this node, if it is a [`Linear`](Node::Linear).
    pub fn get_linear(&self) -> Option<&LinearNode> {
        match self {
            Node::Linear(linear) => Some(linear),
            _ => None,
        }
    }

    /// Get mutable access to the linear data of this node, if it is a [`Linear`](Node::Linear).
    pub fn get_linear_mut(&mut self) -> Option<&mut LinearNode> {
        match self {
            Node::Linear(linear) => Some(linear),
            _ => None,
        }
    }

    /// The children of this node: none unless it is a [`Linear`](Node::Linear) or a
    /// [`Grid`](Node::Grid).
    pub fn children(&self) -> &[crate::NodeIndex] {
        match self {
            Node::Linear(linear) => linear.children(),
            Node::Grid(grid) => grid.children(),
            _ => &[],
        }
    }

    /// The children of a container, to replace one in place.
    pub(crate) fn children_mut(&mut self) -> Option<&mut Vec<crate::NodeIndex>> {
        match self {
            Node::Linear(linear) => Some(&mut linear.children),
            Node::Grid(grid) => Some(&mut grid.children),
            _ => None,
        }
    }

    /// Returns `true` if the node is collapsed, otherwise `false`.
    #[inline(always)]
    pub fn is_collapsed(&self) -> bool {
        match self {
            Node::Leaf(leaf) => leaf.collapsed,
            Node::Linear(linear) => linear.fully_collapsed,
            Node::Grid(grid) => grid.fully_collapsed,
            Node::Empty => false,
        }
    }

    /// Returns the number of layers of collapsed leaf subnodes.
    pub fn collapsed_leaf_count(&self) -> i32 {
        match self {
            Node::Linear(linear) => linear.collapsed_leaf_count,
            Node::Grid(grid) => grid.collapsed_leaf_count,
            Node::Leaf(leaf) => {
                if leaf.collapsed {
                    1
                } else {
                    0
                }
            }
            Node::Empty => 0,
        }
    }

    /// Provides an immutable slice of the tabs inside this node.
    ///
    /// Returns [`None`] if the node is not a [`Leaf`](Node::Leaf).
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use egui_stack::{DockState, NodeIndex};
    /// let mut dock_state = DockState::new(vec![1, 2, 3, 4, 5, 6]);
    /// assert!(dock_state.main_surface().root_node().unwrap().tabs().unwrap().contains(&4));
    /// ```
    #[inline]
    pub fn tabs(&self) -> Option<&[Tab]> {
        match self {
            Node::Leaf(leaf) => Some(leaf.tabs()),
            _ => None,
        }
    }

    /// Provides an mutable slice of the tabs inside this node.
    ///
    /// Returns [`None`] if the node is not a [`Leaf`](Node::Leaf).
    ///
    /// # Examples
    ///
    /// Modifying tabs inside a node:
    /// ```rust
    /// # use egui_stack::{DockState, NodeIndex};
    /// let mut dock_state = DockState::new(vec![1, 2, 3, 4, 5, 6]);
    /// let mut tabs = dock_state
    ///     .main_surface_mut()
    ///     .root_node_mut()
    ///     .unwrap()
    ///     .tabs_mut()
    ///     .unwrap();
    ///
    /// tabs[0] = 7;
    /// tabs[5] = 8;
    ///
    /// assert_eq!(&tabs, &[7, 2, 3, 4, 5, 8]);
    /// ```
    #[inline]
    pub fn tabs_mut(&mut self) -> Option<&mut [Tab]> {
        match self {
            Node::Leaf(leaf) => Some(leaf.tabs_mut()),
            _ => None,
        }
    }

    /// Returns an [`Iterator`] of tabs in this node.
    ///
    /// If this node is not a [`Leaf`](Self::Leaf), then the returned [`Iterator`] will be empty.
    #[inline]
    pub fn iter_tabs(&self) -> impl Iterator<Item = &Tab> {
        match self.tabs() {
            Some(tabs) => tabs.iter(),
            None => core::slice::Iter::default(),
        }
    }

    /// Returns an [`Iterator`] of tabs in this node with their corresponding [`TabIndex`].
    pub fn iter_tabs_indexed(&self) -> impl Iterator<Item = (TabIndex, &Tab)> {
        self.iter_tabs()
            .enumerate()
            .map(|(index, tab)| (TabIndex(index), tab))
    }

    /// Returns a mutable [`Iterator`] of tabs in this node.
    ///
    /// If this node is not a [`Leaf`](Self::Leaf), then the returned [`Iterator`] will be empty.
    #[inline]
    pub fn iter_tabs_mut(&mut self) -> impl Iterator<Item = &mut Tab> {
        match self.tabs_mut() {
            Some(tabs) => tabs.iter_mut(),
            None => core::slice::IterMut::default(),
        }
    }

    /// Returns a mutable [`Iterator`] of tabs in this node with their corresponding [`TabIndex`].
    pub fn iter_tabs_mut_indexed(&mut self) -> impl Iterator<Item = (TabIndex, &mut Tab)> {
        self.iter_tabs_mut()
            .enumerate()
            .map(|(index, tab)| (TabIndex(index), tab))
    }

    /// Adds `tab` to the node and sets it as the active tab.
    ///
    /// # Panics
    ///
    /// If the new capacity of `tabs` exceeds `isize::MAX` bytes.
    ///
    /// If `self` is not a [`Leaf`](Node::Leaf) node.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use egui_stack::{DockState, NodeIndex};
    /// let mut dock_state = DockState::new(vec!["a tab"]);
    /// assert_eq!(dock_state.main_surface().root_node().unwrap().tabs_count(), 1);
    ///
    /// dock_state.main_surface_mut().root_node_mut().unwrap().append_tab("another tab");
    /// assert_eq!(dock_state.main_surface().root_node().unwrap().tabs_count(), 2);
    /// ```
    #[track_caller]
    #[inline]
    pub fn append_tab(&mut self, tab: Tab) {
        match self {
            Node::Leaf(leaf) => leaf.append_tab(tab),
            _ => panic!("node was not a leaf"),
        }
    }

    /// Sets the collapsing state of the node.
    ///
    /// # Panics
    ///
    /// Panics if `self` is an [`Empty`](Node::Empty) node.
    #[inline]
    pub fn set_collapsed(&mut self, collapsed: bool) {
        match self {
            Node::Leaf(leaf) => leaf.collapsed = collapsed,
            Node::Linear(linear) => linear.fully_collapsed = collapsed,
            Node::Grid(grid) => grid.fully_collapsed = collapsed,
            Node::Empty => panic!("node was empty"),
        }
    }

    /// Returns `true` if the node is a leaf with a hidden tab bar, otherwise `false`.
    #[inline(always)]
    pub fn is_tab_bar_hidden(&self) -> bool {
        match self {
            Node::Leaf(leaf) => leaf.tab_bar_hidden,
            _ => false,
        }
    }

    /// Sets the tab bar hidden state of the node.
    ///
    /// # Panics
    ///
    /// Panics if `self` is not a [`Leaf`](Node::Leaf) node.
    #[inline]
    pub fn set_tab_bar_hidden(&mut self, tab_bar_hidden: bool) {
        match self {
            Node::Leaf(leaf) => leaf.tab_bar_hidden = tab_bar_hidden,
            _ => panic!("node was not a leaf"),
        }
    }

    /// Sets the number of layers of collapsed leaf subnodes.
    ///
    /// # Panics
    ///
    /// Panics if `self` is not a [`Linear`](Node::Linear) or a [`Grid`](Node::Grid) node.
    #[inline]
    pub fn set_collapsed_leaf_count(&mut self, count: i32) {
        match self {
            Node::Linear(linear) => linear.collapsed_leaf_count = count,
            Node::Grid(grid) => grid.collapsed_leaf_count = count,
            _ => panic!("node was not a container"),
        }
    }

    /// Adds a `tab` to the node.
    ///
    /// # Panics
    ///
    /// Panics if the new capacity of `tabs` exceeds `isize::MAX` bytes, or `index > tabs_count()`.
    #[track_caller]
    #[inline]
    pub fn insert_tab(&mut self, index: TabIndex, tab: Tab) {
        match self {
            Node::Leaf(leaf) => leaf.insert_tab(index, tab),
            _ => panic!("node was not a leaf!"),
        }
    }

    /// Removes a tab at given `index` from the node.
    /// Returns the removed tab if the node is a `Leaf`, or `None` otherwise.
    ///
    /// # Panics
    ///
    /// Panics if `index` is out of bounds.
    #[inline]
    pub fn remove_tab(&mut self, tab_index: TabIndex) -> Option<Tab> {
        match self {
            Node::Leaf(leaf) => leaf.remove_tab(tab_index),
            _ => None,
        }
    }

    /// Gets the number of tabs in the node.
    #[inline]
    pub fn tabs_count(&self) -> usize {
        match self {
            Node::Leaf(leaf) => leaf.tabs.len(),
            _ => Default::default(),
        }
    }

    /// Returns a new [`Node`] while mapping and filtering the tab type.
    /// If this [`Node`] remains empty, it will change to [`Node::Empty`].
    pub fn filter_map_tabs<F, NewTab>(&self, function: F) -> Node<NewTab>
    where
        F: FnMut(&Tab) -> Option<NewTab>,
    {
        match self {
            Node::Leaf(leaf) => {
                let LeafNode {
                    rect,
                    viewport,
                    tabs,
                    active,
                    scroll,
                    collapsed,
                    tab_bar_hidden,
                } = leaf;
                let tabs: Vec<_> = tabs.iter().filter_map(function).collect();
                if tabs.is_empty() {
                    Node::Empty
                } else {
                    Node::Leaf(LeafNode {
                        rect: *rect,
                        viewport: *viewport,
                        tabs,
                        active: *active,
                        scroll: *scroll,
                        collapsed: *collapsed,
                        tab_bar_hidden: *tab_bar_hidden,
                    })
                }
            }
            Node::Empty => Node::Empty,
            Node::Linear(linear) => Node::Linear(linear.clone()),
            Node::Grid(grid) => Node::Grid(grid.clone()),
        }
    }

    /// Returns a new [`Node`] while mapping the tab type.
    pub fn map_tabs<F, NewTab>(&self, mut function: F) -> Node<NewTab>
    where
        F: FnMut(&Tab) -> NewTab,
    {
        self.filter_map_tabs(move |tab| Some(function(tab)))
    }

    /// Returns a new [`Node`] while filtering the tab type.
    /// If this [`Node`] remains empty, it will change to [`Node::Empty`].
    pub fn filter_tabs<F>(&self, mut predicate: F) -> Node<Tab>
    where
        F: FnMut(&Tab) -> bool,
        Tab: Clone,
    {
        self.filter_map_tabs(move |tab| predicate(tab).then(|| tab.clone()))
    }

    /// Removes all tabs for which `predicate` returns `false`.
    /// If this [`Node`] remains empty, it will change to [`Node::Empty`].
    pub fn retain_tabs<F>(&mut self, predicate: F)
    where
        F: FnMut(&mut Tab) -> bool,
    {
        if let Node::Leaf(leaf) = self {
            leaf.retain_tabs(predicate);
            if leaf.tabs.is_empty() {
                *self = Node::Empty;
            }
        }
    }
}