egui_stack 0.22.2

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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
use std::{ops::RangeInclusive, sync::Arc};

use egui::{
    Align2, CornerRadius, Galley, Id, NumExt, Painter, Pos2, Rect, Stroke, StrokeKind, Vec2,
    Visuals, epaint::TextShape, vec2,
};

use crate::{
    Split, Style, TabDestination, TabStyle,
    utils::{rect_set_size_centered, rect_stroke_box},
};

/// Paints the parts of a [`DockArea`](crate::DockArea): its tabs, tab bars, separators and drop
/// overlay. Set one with [`DockArea::painter`](crate::DockArea::painter). It paints the bands and
/// boundaries of a [`Sections`](crate::sections::Sections) stack too
/// ([`Sections::painter`](crate::sections::Sections::painter)), so that one painter gives a dock
/// and its section stacks one look.
///
/// Every method has a default, the painting of [`StylePainter`] from the [`Style`] the part
/// receives, so an implementation overrides only the parts it paints itself. A method overridden
/// can still hand some cases to the default by calling it on [`StylePainter`].
///
/// The dock lays the parts out and takes their input; the painter only paints, except
/// [`tab_width`](Self::tab_width), which measures a tab before it is laid out. The parameter
/// structs are `#[non_exhaustive]`: they may gain fields in minor releases.
///
/// The buttons of the tab bars (add, close all, collapse), the hidden tab bar's handle, the tab
/// bar's scroll bar and the tab bodies are still painted from [`Style`].
pub trait DockPainter {
    /// The width of a tab, padding and close button included. A tab bar filling its width
    /// ([`TabBarStyle::fill_tab_bar`](crate::TabBarStyle::fill_tab_bar)) can still make it
    /// wider.
    fn tab_width(&self, tab: &TabMeasure<'_>) -> f32 {
        let tab_style = tab.tab_style;
        let text_width = tab.title.size().x + 2.0 * tab_style.padding.at_least(0.0);
        tab_style
            .minimum_width
            .unwrap_or(0.0)
            .at_least(text_width + tab.close_width)
    }

    /// A tab bar's background, painted before its tabs.
    fn tab_bar(&self, painter: &Painter, bar: &TabBarPaint<'_>) {
        painter.rect_filled(
            bar.rect,
            bar.style.tab_bar.corner_radius,
            bar.style.tab_bar.bg_fill,
        );
    }

    /// A tab, with its title and its close button if it has one.
    ///
    /// A dragged tab is painted on a layer of its own, moved with the pointer.
    fn tab(&self, painter: &Painter, tab: &TabPaint<'_>) {
        let state = tab.state;
        let selected = state.contains(PartState::SELECTED) || state.contains(PartState::DRAGGED);
        let dragged = state.contains(PartState::DRAGGED);
        let kb_focus = state.contains(PartState::FOCUSED);
        let tab_style = tab.tab_style;
        let look = if (selected && state.contains(PartState::LEAF_FOCUSED)) || dragged {
            if kb_focus {
                &tab_style.focused_with_kb_focus
            } else {
                &tab_style.focused
            }
        } else if selected {
            if kb_focus {
                &tab_style.active_with_kb_focus
            } else {
                &tab_style.active
            }
        } else if state.contains(PartState::HOVERED) {
            &tab_style.hovered
        } else if kb_focus {
            &tab_style.inactive_with_kb_focus
        } else {
            &tab_style.inactive
        };
        let rect = tab.rect;

        // The whole tab first, then its outline over it, so that they do not mix.
        painter.rect_filled(rect, look.corner_radius, look.bg_fill);
        let stroke_rect = rect_stroke_box(rect, 1.0);
        painter.rect_stroke(
            stroke_rect,
            look.corner_radius,
            Stroke::new(1.0_f32, look.outline_color),
            StrokeKind::Inside,
        );
        if !dragged {
            // Joins the tab to its body.
            painter.hline(
                RangeInclusive::new(
                    stroke_rect.min.x + f32::max(look.corner_radius.sw.into(), 1.5),
                    stroke_rect.max.x - f32::max(look.corner_radius.se.into(), 1.5),
                ),
                stroke_rect.bottom(),
                Stroke::new(2.0_f32, look.bg_fill),
            );
        }

        let close_width = if tab.close.is_some() {
            Style::TAB_CLOSE_BUTTON_SIZE.min(tab.style.tab_bar.height)
        } else {
            0.0
        };
        let mut text_rect = rect;
        text_rect.set_width(text_rect.width() - close_width);
        let x_spacing = tab_style.padding.at_least(0.0);
        let text_pos = Align2::CENTER_CENTER.pos_in_rect(&text_rect.shrink2(vec2(x_spacing, 0.0)))
            - tab.title.size() / 2.0;
        painter.add(TextShape::new(
            text_pos,
            Arc::clone(tab.title),
            look.text_color,
        ));

        if let Some(close) = tab.close {
            let buttons = &tab.style.buttons;
            let lit = close.state.contains(PartState::HOVERED)
                || close.state.contains(PartState::FOCUSED);
            if lit {
                let mut corner_radius = look.corner_radius;
                corner_radius.nw = 0;
                corner_radius.sw = 0;
                painter.rect_filled(close.rect, corner_radius, buttons.close_tab_bg_fill);
            }
            let color = if lit {
                buttons.close_tab_active_color
            } else {
                buttons.close_tab_color
            };
            let mut x_rect = close.rect;
            rect_set_size_centered(&mut x_rect, Vec2::splat(Style::TAB_CLOSE_X_SIZE));
            painter.line_segment(
                [x_rect.left_top(), x_rect.right_bottom()],
                Stroke::new(1.0_f32, color),
            );
            painter.line_segment(
                [x_rect.right_top(), x_rect.left_bottom()],
                Stroke::new(1.0_f32, color),
            );
        }

        // The line under the bar, under every tab but the selected one.
        if !dragged && (!selected || tab_style.hline_below_active_tab_name) {
            let px = painter.pixels_per_point().recip();
            painter.hline(
                rect.x_range(),
                tab.bar.bottom() - px,
                (px, tab.style.tab_bar.hline_color),
            );
        }
    }

    /// The part of a tab bar right of its last tab, painted after the tabs. Its rect is empty
    /// when the tabs fill the bar.
    fn tab_bar_tail(&self, painter: &Painter, tail: &TabBarTailPaint<'_>) {
        let px = painter.pixels_per_point().recip();
        painter.hline(
            tail.rect.x_range(),
            tail.rect.bottom() - px,
            (px, tail.style.tab_bar.hline_color),
        );
    }

    /// A separator between two nodes.
    fn separator(&self, painter: &Painter, separator: &SeparatorPaint<'_>) {
        let style = &separator.style.separator;
        let state = separator.state;
        let color = if state.contains(PartState::DRAGGED) {
            style.color_dragged
        } else if state.contains(PartState::HOVERED) || state.contains(PartState::FOCUSED) {
            style.color_hovered
        } else {
            style.color_idle
        };
        painter.rect_filled(separator.rect, CornerRadius::ZERO, color);
    }

    /// While a tab is dragged, the node or surface under the pointer, painted first.
    fn drop_target(&self, painter: &Painter, target: &DropTargetPaint<'_>) {
        let highlight = &target.style.overlay.hovered_leaf_highlight;
        painter.rect(
            target.rect.expand(highlight.expansion),
            highlight.corner_radius,
            highlight.color,
            highlight.stroke,
            StrokeKind::Inside,
        );
    }

    /// One of the drop overlay's buttons ([`OverlayType::Widgets`](crate::OverlayType::Widgets)):
    /// the middle one (no split) or a split's.
    fn drop_button(&self, painter: &Painter, button: &DropButtonPaint<'_>) {
        let visuals = &button.style.overlay;
        let button_stroke = Stroke::new(1.0_f32, visuals.button_color);
        painter.rect_stroke(
            button.rect,
            0.0,
            visuals.button_border_stroke,
            StrokeKind::Inside,
        );
        let rect = button.rect.shrink(button.rect.width() * 0.1);
        painter.rect_stroke(rect, 0.0, button_stroke, StrokeKind::Inside);
        let rim = Rect::from_two_pos(rect.min, rect.lerp_inside(vec2(1.0, 0.1)));
        painter.rect(
            rim,
            0.0,
            visuals.button_color,
            Stroke::NONE,
            StrokeKind::Inside,
        );

        if let Some(split) = button.split {
            for line in DASHED_LINE_ALPHAS.chunks(2) {
                let start = rect.lerp_inside(lerp_vec(split, line[0]));
                let end = rect.lerp_inside(lerp_vec(split, line[1]));
                painter.line_segment([start, end], button_stroke);
            }
        }
        if button.hovered {
            let vertical_alphas = vec2(1.0, 0.5);
            let horizontal_alphas = vec2(0.5, 1.0);
            let rect = match button.split {
                Some(Split::Above) => Rect::from_min_size(rect.min, rect.size() * vertical_alphas),
                Some(Split::Left) => Rect::from_min_size(rect.min, rect.size() * horizontal_alphas),
                Some(Split::Below) => {
                    let min = rect.lerp_inside(lerp_vec(Split::Below, 0.0));
                    Rect::from_min_size(min, rect.size() * vertical_alphas)
                }
                Some(Split::Right) => {
                    let min = rect.lerp_inside(lerp_vec(Split::Right, 0.0));
                    Rect::from_min_size(min, rect.size() * horizontal_alphas)
                }
                None => rect,
            };
            painter.rect_filled(rect, 0.0, visuals.selection_color);
        }
    }

    /// Where a dragged tab would land if released now, or why it may not
    /// ([`TabViewer::drop_allowed`](crate::TabViewer::drop_allowed)).
    fn drop_preview(&self, painter: &Painter, drop: &DropPaint<'_>) {
        let overlay = &drop.style.overlay;
        if let TabDestination::Window(_) = drop.destination {
            painter.rect_stroke(
                drop.rect,
                0.0,
                Stroke::new(overlay.selection_stroke_width, overlay.selection_color),
                StrokeKind::Inside,
            );
        } else if !drop.from_buttons {
            painter.rect_filled(drop.rect, 0.0, overlay.selection_color);
        }

        let Some(refusal) = drop.refusal else {
            return;
        };
        let visuals = drop.visuals;
        let color = visuals.error_fg_color;
        painter.rect(
            drop.rect,
            0.0,
            color.gamma_multiply(0.25),
            Stroke::new(overlay.selection_stroke_width.max(1.0), color),
            StrokeKind::Inside,
        );
        if let Some(reason) = &refusal.reason {
            let margin = vec2(6.0, 4.0);
            let min = refusal.pointer + vec2(16.0, 16.0);
            let frame = Rect::from_min_size(min, reason.size() + 2.0 * margin);
            painter.rect(
                frame,
                visuals.menu_corner_radius,
                visuals.window_fill(),
                Stroke::new(1.0, color),
                StrokeKind::Inside,
            );
            painter.galley(min + margin, Arc::clone(reason), visuals.text_color());
        }
    }

    /// The title band of a section in a [`Sections`](crate::sections::Sections) stack: its
    /// background, its disclosure mark, its title and its count. Painted from the band's
    /// [`egui::Style`], as a section stack has no dock [`Style`].
    fn section_band(&self, painter: &Painter, band: &SectionBandPaint<'_>) {
        let visuals = &band.style.visuals;
        let state = band.state;
        let rect = band.rect;
        let look = if state.contains(PartState::PRESSED) {
            &visuals.widgets.active
        } else if state.contains(PartState::HOVERED) {
            &visuals.widgets.hovered
        } else {
            &visuals.widgets.inactive
        };
        let fill = if state.contains(PartState::HOVERED) || state.contains(PartState::PRESSED) {
            look.weak_bg_fill
        } else {
            visuals.faint_bg_color
        };
        painter.rect_filled(rect, CornerRadius::ZERO, fill);
        // The line over the band, which parts it from the body above.
        let px = painter.pixels_per_point().recip();
        painter.hline(
            rect.x_range(),
            rect.top() + px / 2.0,
            (px, visuals.widgets.noninteractive.bg_stroke.color),
        );

        let padding = band.style.spacing.item_spacing.x.max(4.0);
        let text_color = look.fg_stroke.color;

        // The disclosure mark: a triangle pointing right when folded, down when open.
        let side = (rect.height() * 0.4).clamp(4.0, 10.0);
        let mark = Rect::from_center_size(
            Pos2::new(rect.left() + padding + side / 2.0, rect.center().y),
            Vec2::splat(side),
        );
        let points = if band.open {
            vec![mark.left_top(), mark.right_top(), mark.center_bottom()]
        } else {
            vec![mark.left_top(), mark.right_center(), mark.left_bottom()]
        };
        painter.add(egui::Shape::convex_polygon(
            points,
            text_color,
            Stroke::NONE,
        ));

        // The count at the end, in a pill.
        let mut end = rect.right() - padding;
        if let Some(count) = band.count_text() {
            let font = egui::TextStyle::Small.resolve(band.style);
            let galley = painter.layout_no_wrap(count, font, text_color);
            let pill_size = galley.size() + vec2(8.0, 2.0);
            let pill = Rect::from_min_size(
                Pos2::new(end - pill_size.x, rect.center().y - pill_size.y / 2.0),
                pill_size,
            );
            painter.rect_filled(pill, pill.height() / 2.0, visuals.widgets.inactive.bg_fill);
            painter.galley(pill.center() - galley.size() / 2.0, galley, text_color);
            end = pill.left() - padding;
        }

        // The title between them, clipped to its room.
        let start = mark.right() + padding;
        let font = egui::TextStyle::Button.resolve(band.style);
        let galley = painter.layout_no_wrap(band.title.to_owned(), font, text_color);
        let room = Rect::from_x_y_ranges(start..=end.max(start), rect.y_range());
        painter
            .with_clip_rect(room.intersect(painter.clip_rect()))
            .galley(
                Pos2::new(start, rect.center().y - galley.size().y / 2.0),
                galley,
                text_color,
            );

        if state.contains(PartState::FOCUSED) {
            painter.rect_stroke(
                rect.shrink(1.0),
                CornerRadius::ZERO,
                visuals.selection.stroke,
                StrokeKind::Inside,
            );
        }
    }

    /// A boundary between two sections of a [`Sections`](crate::sections::Sections) stack, which
    /// resizes the open section above it. Painted after the bands, so that a focus ring shows
    /// over them. The default paints nothing at rest: the bands' own lines part the sections.
    fn section_boundary(&self, painter: &Painter, boundary: &SectionBoundaryPaint<'_>) {
        let visuals = &boundary.style.visuals;
        let state = boundary.state;
        if state.contains(PartState::FOCUSED) {
            painter.rect_stroke(
                boundary.rect,
                CornerRadius::ZERO,
                visuals.selection.stroke,
                StrokeKind::Inside,
            );
        } else if state.contains(PartState::DRAGGED)
            || state.contains(PartState::PRESSED)
            || state.contains(PartState::HOVERED)
        {
            painter.hline(
                boundary.rect.x_range(),
                boundary.line,
                visuals.widgets.hovered.bg_stroke,
            );
        }
    }
}

/// The painter a [`DockArea`](crate::DockArea) uses unless given another: every part painted
/// from the [`Style`], as before painters existed.
#[derive(Clone, Copy, Debug, Default)]
pub struct StylePainter;

impl DockPainter for StylePainter {}

const DASHED_LINE_ALPHAS: [f32; 8] = [
    0.0625, 0.1875, 0.3125, 0.4375, 0.5625, 0.6875, 0.8125, 0.9375,
];

#[inline(always)]
const fn lerp_vec(split: Split, alpha: f32) -> Vec2 {
    if split.is_top_bottom() {
        vec2(alpha, 0.5)
    } else {
        vec2(0.5, alpha)
    }
}

/// The state of a painted part, as flags: a tab uses all of them, a separator
/// [`HOVERED`](Self::HOVERED), [`PRESSED`](Self::PRESSED), [`FOCUSED`](Self::FOCUSED) and
/// [`DRAGGED`](Self::DRAGGED), a tab's close button [`HOVERED`](Self::HOVERED),
/// [`PRESSED`](Self::PRESSED) and [`FOCUSED`](Self::FOCUSED). A section's band and a boundary
/// between sections use the same four as a separator.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct PartState(u8);

impl PartState {
    /// No flag.
    pub const NONE: Self = Self(0);
    /// The tab is its leaf's active one, whose body shows.
    pub const SELECTED: Self = Self(1);
    /// The pointer is over the part.
    pub const HOVERED: Self = Self(1 << 1);
    /// A pointer button is held down on the part.
    pub const PRESSED: Self = Self(1 << 2);
    /// The part has the keyboard focus.
    pub const FOCUSED: Self = Self(1 << 3);
    /// The part is being dragged.
    pub const DRAGGED: Self = Self(1 << 4);
    /// The tab's leaf is the dock's focused one, the last one interacted with
    /// ([`DockState::focused_leaf`](crate::DockState::focused_leaf)).
    pub const LEAF_FOCUSED: Self = Self(1 << 5);

    /// Whether every flag of `other` is set.
    #[inline]
    pub const fn contains(self, other: Self) -> bool {
        self.0 & other.0 == other.0
    }

    /// These flags with `flag` set or cleared.
    #[inline]
    #[must_use]
    pub const fn with(self, flag: Self, on: bool) -> Self {
        if on {
            Self(self.0 | flag.0)
        } else {
            Self(self.0 & !flag.0)
        }
    }
}

impl std::ops::BitOr for PartState {
    type Output = Self;

    #[inline]
    fn bitor(self, rhs: Self) -> Self {
        Self(self.0 | rhs.0)
    }
}

impl std::ops::BitOrAssign for PartState {
    #[inline]
    fn bitor_assign(&mut self, rhs: Self) {
        self.0 |= rhs.0;
    }
}

/// A tab to measure, for [`DockPainter::tab_width`].
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct TabMeasure<'a> {
    /// The tab's title, laid out.
    pub title: &'a Arc<Galley>,
    /// The flags known before the tab is laid out: [`PartState::SELECTED`],
    /// [`PartState::DRAGGED`] and [`PartState::LEAF_FOCUSED`].
    pub state: PartState,
    /// The width of the tab's close button, `0.0` without one.
    pub close_width: f32,
    /// The dock's style.
    pub style: &'a Style,
    /// The tab's style, after [`TabViewer::tab_style_override`](crate::TabViewer::tab_style_override).
    pub tab_style: &'a TabStyle,
}

/// A tab bar, for [`DockPainter::tab_bar`].
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct TabBarPaint<'a> {
    /// The whole bar.
    pub rect: Rect,
    /// How far its tabs are scrolled to the left, in points: `0.0` when not scrolled.
    pub scrolled: f32,
    /// The dock's style.
    pub style: &'a Style,
}

/// The end of a tab bar, for [`DockPainter::tab_bar_tail`].
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct TabBarTailPaint<'a> {
    /// From the right edge of the last tab to the right of the bar.
    pub rect: Rect,
    /// The bar, inside its margins.
    pub bar: Rect,
    /// The dock's style.
    pub style: &'a Style,
}

/// A tab, for [`DockPainter::tab`].
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct TabPaint<'a> {
    /// The tab, as wide as [`DockPainter::tab_width`] measured (or wider in a filled bar).
    pub rect: Rect,
    /// Its bar, inside the bar's margins.
    pub bar: Rect,
    /// Its title, laid out.
    pub title: &'a Arc<Galley>,
    /// Its state.
    pub state: PartState,
    /// Its close button, if it shows one.
    pub close: Option<TabClose>,
    /// The dock's style.
    pub style: &'a Style,
    /// The tab's style, after [`TabViewer::tab_style_override`](crate::TabViewer::tab_style_override).
    pub tab_style: &'a TabStyle,
}

/// A tab's close button, in [`TabPaint`].
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct TabClose {
    /// Where it is, at the tab's right end.
    pub rect: Rect,
    /// Its state.
    pub state: PartState,
}

/// Which way a separator runs.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SeparatorAxis {
    /// A vertical separator, between a left and a right node; it moves sideways.
    Vertical,
    /// A horizontal separator, between a node above and one below; it moves up and down.
    Horizontal,
}

/// A separator, for [`DockPainter::separator`].
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct SeparatorPaint<'a> {
    /// The separator, [`SeparatorStyle::width`](crate::SeparatorStyle::width) thick.
    pub rect: Rect,
    /// Which way it runs.
    pub axis: SeparatorAxis,
    /// Its state.
    pub state: PartState,
    /// The dock's style.
    pub style: &'a Style,
}

/// The node or surface under a dragged tab, for [`DockPainter::drop_target`].
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct DropTargetPaint<'a> {
    /// The node or surface.
    pub rect: Rect,
    /// The dock's style.
    pub style: &'a Style,
}

/// A drop overlay button, for [`DockPainter::drop_button`].
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct DropButtonPaint<'a> {
    /// The button.
    pub rect: Rect,
    /// The split it makes, `None` for the middle button, which adds the tab to the node.
    pub split: Option<Split>,
    /// Whether the pointer picks this button.
    pub hovered: bool,
    /// The dock's style.
    pub style: &'a Style,
}

/// Where a dragged tab would land, for [`DockPainter::drop_preview`].
#[derive(Clone, Copy)]
#[non_exhaustive]
pub struct DropPaint<'a> {
    /// Where the tab would go: the hovered tab or tab bar, the node, the half of it a split
    /// would give, or the new window.
    pub rect: Rect,
    /// The destination.
    pub destination: &'a TabDestination,
    /// Whether the destination was picked on the overlay's buttons, which then already show
    /// it: the default paints no fill but a new window's outline.
    pub from_buttons: bool,
    /// Why the tab may not go there, if refused.
    pub refusal: Option<&'a DropRefusal>,
    /// The dock's style.
    pub style: &'a Style,
    /// The visuals of the [`Ui`](egui::Ui) the dock is in.
    pub visuals: &'a Visuals,
}

/// A refused drop, in [`DropPaint`].
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct DropRefusal {
    /// The reason [`TabViewer::drop_allowed`](crate::TabViewer::drop_allowed) gave, laid out.
    pub reason: Option<Arc<Galley>>,
    /// The pointer's position.
    pub pointer: Pos2,
}

/// A section's title band, for [`DockPainter::section_band`].
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct SectionBandPaint<'a> {
    /// The whole band, as wide as the stack.
    pub rect: Rect,
    /// The band's widget id: what its response, its focus and its animations go by.
    pub id: Id,
    /// The title as shown, in capitals unless the stack keeps its case
    /// ([`Sections::capitals`](crate::sections::Sections::capitals)).
    pub title: &'a str,
    /// The count to show on the band, if any.
    pub count: Option<u64>,
    /// Whether the section is open.
    pub open: bool,
    /// Its state: [`PartState::HOVERED`], [`PartState::PRESSED`], [`PartState::FOCUSED`] and,
    /// for a title that drags ([`Sections::grab_titles`](crate::sections::Sections::grab_titles)),
    /// [`PartState::DRAGGED`].
    pub state: PartState,
    /// The style of the [`Ui`](egui::Ui) the stack is in.
    pub style: &'a egui::Style,
}

impl SectionBandPaint<'_> {
    /// The count as shown: `99+` past 99.
    pub fn count_text(&self) -> Option<String> {
        self.count.map(|count| {
            if count > 99 {
                "99+".to_owned()
            } else {
                count.to_string()
            }
        })
    }
}

/// A boundary between two sections, for [`DockPainter::section_boundary`].
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct SectionBoundaryPaint<'a> {
    /// Where the boundary takes the pointer: a few points on either side of the line, or above
    /// it alone when the titles drag.
    pub rect: Rect,
    /// The line between the body above and the band below, as a `y`.
    pub line: f32,
    /// The boundary's widget id.
    pub id: Id,
    /// Its state: [`PartState::HOVERED`], [`PartState::PRESSED`], [`PartState::FOCUSED`] and
    /// [`PartState::DRAGGED`].
    pub state: PartState,
    /// The style of the [`Ui`](egui::Ui) the stack is in.
    pub style: &'a egui::Style,
}