gpui-base 0.7.0

Behavior, interaction, and infrastructure foundations for GPUI applications.
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
use std::{cell::Cell, rc::Rc};

use gpui::{
    AnyElement, App, Axis, Element, ElementId, Entity, GlobalElementId, Hitbox, HitboxBehavior,
    InteractiveElement, IntoElement, MouseDownEvent, MouseMoveEvent, MouseUpEvent,
    ParentElement as _, Pixels, Point, Render, StatefulInteractiveElement, Styled as _, Window,
    div, prelude::FluentBuilder as _, px,
};

use crate::{AxisExt as _, theme::ActiveTheme as _};

pub(crate) const HANDLE_PADDING: Pixels = px(4.);
pub(crate) const HANDLE_SIZE: Pixels = px(1.);

/// Create a resize handle for a resizable panel.
#[doc(hidden)]
pub fn resize_handle<T: 'static, E: 'static + Render>(
    id: impl Into<ElementId>,
    axis: Axis,
) -> ResizeHandle<T, E> {
    ResizeHandle::new(id, axis)
}

/// Draws the visible part of a resize handle.
///
/// Returning `None` keeps the built-in line, so a renderer can override some
/// handles and leave the rest alone.
pub type ResizeHandleRenderer =
    Rc<dyn Fn(&ResizeHandleContext, &mut Window, &mut App) -> Option<AnyElement>>;

/// What a [`ResizeHandleRenderer`] is told about the handle it is drawing.
///
/// The hit area, the cursor and the drag itself stay with the handle; a
/// renderer only supplies what is painted inside it.
pub struct ResizeHandleContext {
    axis: Axis,
    edge: Option<HandleEdge>,
    state: ResizeHandleState,
}

impl ResizeHandleContext {
    /// The axis the handle resizes along: `Horizontal` for a vertical divider
    /// between two side-by-side panels.
    pub fn axis(&self) -> Axis {
        self.axis
    }

    /// The edge of its container this handle hugs, or `None` for a handle
    /// straddling the boundary it resizes.
    ///
    /// A hugging handle's line is the container's outermost pixel, so anything
    /// a renderer centres on it crosses the boundary; see [`HandleEdge`] for
    /// what the container does with that.
    pub fn edge(&self) -> Option<HandleEdge> {
        self.edge
    }

    /// Whether the pointer currently owns this handle.
    pub fn is_active(&self) -> bool {
        self.state.is_active()
    }

    /// How far the pointer has gone with this handle.
    pub fn state(&self) -> ResizeHandleState {
        self.state
    }
}

/// How far the pointer has gone with a resize handle.
///
/// A drag takes the pointer out of the handle's own band within a pixel or
/// two, so GPUI's hover reads false for most of a drag and cannot stand in for
/// `Dragging`. Base tracks the progression instead, and a renderer reads it
/// through [`ResizeHandleContext::state`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ResizeHandleState {
    /// The pointer is somewhere else.
    #[default]
    Idle,
    /// The pointer is over the handle's band.
    Hovered,
    /// The pointer went down on the handle and has not moved since.
    Pressed,
    /// The handle is being dragged.
    Dragging,
}

impl ResizeHandleState {
    /// Whether the pointer owns the handle -- pressed on it, or dragging it.
    pub fn is_active(self) -> bool {
        matches!(self, Self::Pressed | Self::Dragging)
    }
}

/// Which edge of its own container a handle hugs.
///
/// A handle named no edge straddles the boundary it resizes, half its band on
/// either side, which is what a divider between two panels of a group wants.
/// A dock's own edge handle cannot: [`dock_frame`] clips to the dock's box, so
/// the half hanging outside is cut away -- what it paints and what it
/// hit-tests alike, which is why the outer half of a dock's grab band has
/// never actually been grabbable. Naming the edge moves the whole band inside.
///
/// The hairline stays on the boundary itself: it is the container's outermost
/// pixel, the one the neighbour's content butts up against. Moving it inward
/// by even a pixel leaves that pixel of the container showing past the line
/// on one side, or a gap before it on the other, along the whole seam. What a
/// renderer paints on top of the line -- an indicator thicker than the line,
/// centred on it -- overhangs the boundary, and the container's clip takes
/// the outer half off it unless the renderer defers that part. Only that part:
/// a deferred element paints after the whole tree, and no priority puts it
/// beneath the application's own deferred content, so a deferred line would
/// cut straight through a popover opened at the default priority from a panel
/// drawn before this container. A renderer learns which edge it is drawing for
/// from [`ResizeHandleContext::edge`].
///
/// [`dock_frame`]: crate::dock::dock_frame
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum HandleEdge {
    /// Where the axis starts: the left edge of a horizontal handle's
    /// container, the top edge of a vertical one's.
    Leading,
    /// Where the axis ends.
    Trailing,
}

#[doc(hidden)]
pub struct ResizeHandle<T: 'static, E: 'static + Render> {
    id: ElementId,
    axis: Axis,
    drag_value: Option<Rc<T>>,
    edge: Option<HandleEdge>,
    on_drag: Option<Rc<dyn Fn(&Point<Pixels>, &mut Window, &mut App) -> Entity<E>>>,
    appearance: Option<ResizeHandleRenderer>,
}

impl<T: 'static, E: 'static + Render> ResizeHandle<T, E> {
    fn new(id: impl Into<ElementId>, axis: Axis) -> Self {
        let id = id.into();
        Self {
            id: id.clone(),
            on_drag: None,
            drag_value: None,
            edge: None,
            appearance: None,
            axis,
        }
    }

    /// Hand the painted part of this handle to `appearance`.
    pub fn with_appearance(mut self, appearance: ResizeHandleRenderer) -> Self {
        self.appearance = Some(appearance);
        self
    }

    pub fn on_drag(
        mut self,
        value: T,
        f: impl Fn(Rc<T>, &Point<Pixels>, &mut Window, &mut App) -> Entity<E> + 'static,
    ) -> Self {
        let value = Rc::new(value);
        self.drag_value = Some(value.clone());
        self.on_drag = Some(Rc::new(move |p, window, cx| {
            f(value.clone(), p, window, cx)
        }));
        self
    }

    /// Keep the whole handle inside its container, hugging `edge`, instead of
    /// straddling the boundary it resizes.
    pub fn inside(mut self, edge: HandleEdge) -> Self {
        self.edge = Some(edge);
        self
    }
}

/// One handle's [`ResizeHandleState`], shared between the element and the
/// mouse listeners it registers.
///
/// The `Rc` is load-bearing. `with_element_state` hands back a clone and a
/// bare `Cell` clones by value, so a listener holding one wrote its progress
/// into a copy that died with the event: the stored state never left `Idle`
/// and `is_active` never once read true.
#[derive(Default, Debug, Clone)]
struct SharedHandleState {
    state: Rc<Cell<ResizeHandleState>>,
}

impl SharedHandleState {
    fn get(&self) -> ResizeHandleState {
        self.state.get()
    }

    /// Reports whether the state actually changed, so a listener repaints the
    /// window only when there is something new to paint.
    fn set(&self, state: ResizeHandleState) -> bool {
        let changed = self.state.get() != state;
        self.state.set(state);
        changed
    }
}

impl<T: 'static, E: 'static + Render> IntoElement for ResizeHandle<T, E> {
    type Element = ResizeHandle<T, E>;
    fn into_element(self) -> Self::Element {
        self
    }
}

impl<T: 'static, E: 'static + Render> Element for ResizeHandle<T, E> {
    type RequestLayoutState = AnyElement;
    /// The band's own hitbox, so the listeners in `paint` can ask whether the
    /// pointer is really on the handle rather than merely within its bounds.
    type PrepaintState = Hitbox;

    fn id(&self) -> Option<ElementId> {
        Some(self.id.clone())
    }

    fn source_location(&self) -> Option<&'static std::panic::Location<'static>> {
        None
    }

    fn request_layout(
        &mut self,
        id: Option<&GlobalElementId>,
        _: Option<&gpui::InspectorElementId>,
        window: &mut Window,
        cx: &mut App,
    ) -> (gpui::LayoutId, Self::RequestLayoutState) {
        let neg_offset = -HANDLE_PADDING;
        let axis = self.axis;
        let edge = self.edge;
        // Sizes are border-box: the extent has to name the whole band, padding
        // included, or the content box resolves to zero and the hairline
        // overflows into the padding, landing wherever the padding happens to
        // push it.
        let hug_extent = HANDLE_SIZE + HANDLE_PADDING;
        let straddle_extent = HANDLE_SIZE + HANDLE_PADDING * 2.;

        window.with_element_state(id.unwrap(), |state, window| {
            let state: SharedHandleState = state.unwrap_or_default();

            let bg_color = handle_color(&cx.theme(), state.get().is_active());

            let mut el = div()
                .id(self.id.clone())
                .occlude()
                .absolute()
                .flex_shrink_0()
                .group("handle")
                .when_some(self.on_drag.clone(), |this, on_drag| {
                    this.on_drag(
                        self.drag_value.clone().unwrap(),
                        move |_, position, window, cx| on_drag(&position, window, cx),
                    )
                })
                .map(|this| match (edge, axis) {
                    // Hugging an edge: the whole band is inside the container,
                    // padded on the inner side only, so the hairline is the
                    // container's outermost pixel -- the seam itself.
                    // FIXME: Improve this to let the scroll bar have px(HANDLE_PADDING)
                    (Some(HandleEdge::Trailing), Axis::Horizontal) => this
                        .cursor_col_resize()
                        .top_0()
                        .right_0()
                        .h_full()
                        .w(hug_extent)
                        .pl(HANDLE_PADDING),
                    (Some(HandleEdge::Leading), Axis::Horizontal) => this
                        .cursor_col_resize()
                        .top_0()
                        .left_0()
                        .h_full()
                        .w(hug_extent)
                        .pr(HANDLE_PADDING),
                    (Some(HandleEdge::Trailing), Axis::Vertical) => this
                        .cursor_row_resize()
                        .bottom_0()
                        .left_0()
                        .w_full()
                        .h(hug_extent)
                        .pt(HANDLE_PADDING),
                    (Some(HandleEdge::Leading), Axis::Vertical) => this
                        .cursor_row_resize()
                        .top_0()
                        .left_0()
                        .w_full()
                        .h(hug_extent)
                        .pb(HANDLE_PADDING),
                    // Straddling the boundary: half the band on either side.
                    (None, Axis::Horizontal) => this
                        .cursor_col_resize()
                        .top_0()
                        .left(neg_offset)
                        .h_full()
                        .w(straddle_extent)
                        .px(HANDLE_PADDING),
                    (None, Axis::Vertical) => this
                        .cursor_row_resize()
                        .top(neg_offset)
                        .left_0()
                        .w_full()
                        .h(straddle_extent)
                        .py(HANDLE_PADDING),
                })
                .child(
                    // A renderer that declines — or is absent — leaves the
                    // built-in line, so overriding one handle never obliges a
                    // caller to redraw them all. What it paints stays in tree
                    // order, under the container's own mask: a divider is
                    // part of the panel it edges, and anything the
                    // application floats over that panel has to cover it.
                    self.appearance
                        .as_ref()
                        .and_then(|appearance| {
                            appearance(
                                &ResizeHandleContext {
                                    axis,
                                    edge,
                                    state: state.get(),
                                },
                                window,
                                cx,
                            )
                        })
                        .unwrap_or_else(|| {
                            div()
                                // The line fills the handle's content box
                                // exactly, so it has nothing to give: a
                                // shrinkable child collapses with it.
                                .flex_none()
                                .bg(bg_color)
                                .group_hover("handle", |this| this.bg(bg_color))
                                .when(axis.is_horizontal(), |this| this.h_full().w(HANDLE_SIZE))
                                .when(axis.is_vertical(), |this| this.w_full().h(HANDLE_SIZE))
                                .into_any_element()
                        }),
                )
                .into_any_element();

            let layout_id = el.request_layout(window, cx);

            ((layout_id, el), state)
        })
    }

    fn prepaint(
        &mut self,
        _: Option<&GlobalElementId>,
        _: Option<&gpui::InspectorElementId>,
        bounds: gpui::Bounds<Pixels>,
        request_layout: &mut Self::RequestLayoutState,
        window: &mut Window,
        cx: &mut App,
    ) -> Self::PrepaintState {
        request_layout.prepaint(window, cx);
        // After the child, deliberately. The band's own div occludes, and a
        // hit test stops at the first hitbox that does, so a hitbox inserted
        // before it would sit underneath and never read as hovered. Inserted
        // here it sits directly on top of the band and directly under whatever
        // is painted after this handle -- a sheet's overlay, a toast -- which
        // is exactly the ordering `is_hovered_at` should answer from.
        window.insert_hitbox(bounds, HitboxBehavior::Normal)
    }

    fn paint(
        &mut self,
        id: Option<&GlobalElementId>,
        _: Option<&gpui::InspectorElementId>,
        _: gpui::Bounds<Pixels>,
        request_layout: &mut Self::RequestLayoutState,
        hitbox: &mut Self::PrepaintState,
        window: &mut Window,
        cx: &mut App,
    ) {
        request_layout.paint(window, cx);

        // Hovered and pressed are answered by the hitbox, not by the bounds.
        // Bounds containment reads true through anything painted over the
        // handle, so a divider under a sheet lit up as the pointer crossed
        // where it lay; the hitbox is occluded by that sheet and says no.
        let hitbox = hitbox.clone();

        window.with_element_state(id.unwrap(), |state: Option<SharedHandleState>, window| {
            let state = state.unwrap_or_default();

            window.on_mouse_event({
                let state = state.clone();
                let hitbox = hitbox.clone();
                move |ev: &MouseDownEvent, phase, window, _| {
                    if phase.bubble()
                        && hitbox.is_hovered_at(ev.position, window)
                        && state.set(ResizeHandleState::Pressed)
                    {
                        window.refresh();
                    }
                }
            });

            window.on_mouse_event({
                let state = state.clone();
                let hitbox = hitbox.clone();
                move |ev: &MouseMoveEvent, phase, window, _| {
                    if !phase.bubble() {
                        return;
                    }

                    // A press that moves is a drag, and stays one until the
                    // button comes back up: by the second frame the pointer is
                    // outside this nine-pixel band, so where it is says nothing
                    // about whether the handle is still being dragged.
                    let next = match state.get() {
                        engaged if engaged.is_active() => ResizeHandleState::Dragging,
                        _ if hitbox.is_hovered_at(ev.position, window) => {
                            ResizeHandleState::Hovered
                        }
                        _ => ResizeHandleState::Idle,
                    };
                    if state.set(next) {
                        window.refresh();
                    }
                }
            });

            window.on_mouse_event({
                let state = state.clone();
                let hitbox = hitbox.clone();
                move |ev: &MouseUpEvent, _, window, _| {
                    if !state.get().is_active() {
                        return;
                    }

                    // Releasing over the handle leaves it hovered. Going
                    // straight to idle there would drop the indicator for one
                    // frame and bring it back under a pointer that never left.
                    let next = if hitbox.is_hovered_at(ev.position, window) {
                        ResizeHandleState::Hovered
                    } else {
                        ResizeHandleState::Idle
                    };
                    if state.set(next) {
                        window.refresh();
                    }
                }
            });

            ((), state)
        });
    }
}

/// What a resize handle paints, given the active theme.
///
/// Projected colors win; without them the handle resolves from the tokens that
/// already mean these two states everywhere else -- `border` for a divider at
/// rest, `ring` for the thing the pointer currently owns. Before this the
/// unprojected answer was `Hsla::default()`, which is transparent, so a
/// consumer with no styled façade had no divider at all.
pub(crate) fn handle_color(theme: &crate::Theme, active: bool) -> gpui::Hsla {
    if active {
        theme
            .resizable
            .active_handle
            .unwrap_or(theme.tokens.colors.ring)
    } else {
        theme.resizable.handle.unwrap_or(theme.tokens.colors.border)
    }
}

#[cfg(test)]
mod tests {
    use std::{cell::Cell, rc::Rc};

    use gpui::{
        AnyElement, App, Axis, Bounds, Context, Empty, IntoElement, ParentElement as _, Pixels,
        Render, Styled as _, TestAppContext, Window, deferred, div, hsla,
        prelude::FluentBuilder as _, px,
    };

    use super::{
        HandleEdge, ResizeHandleContext, ResizeHandleState, SharedHandleState, handle_color,
        resize_handle,
    };
    use crate::{ElementExt as _, ResizableTheme, Theme};

    /// What a hugging handle's renderer was told and drew, and where the
    /// drawing landed in the frame: under which mask, and before or after a
    /// popover the application deferred from a panel drawn ahead of the dock.
    #[derive(Default)]
    struct Probe {
        edge: Cell<Option<Option<HandleEdge>>>,
        line: Cell<Option<Bounds<Pixels>>>,
        mask: Cell<Option<Bounds<Pixels>>>,
        /// Ticked by every prepaint hook below, in the order the frame reaches
        /// them.
        prepaints: Cell<usize>,
        line_prepainted: Cell<Option<usize>>,
        popover_prepainted: Cell<Option<usize>>,
    }

    impl Probe {
        fn tick(&self) -> usize {
            let n = self.prepaints.get();
            self.prepaints.set(n + 1);
            n
        }
    }

    /// A hairline filling the handle's content box, the way a styled divider
    /// rests.
    fn hairline(axis: Axis, probe: Rc<Probe>) -> AnyElement {
        div()
            .flex_none()
            .map(|line| match axis {
                Axis::Horizontal => line.w(px(1.)).h_full(),
                Axis::Vertical => line.h(px(1.)).w_full(),
            })
            .on_prepaint(move |bounds, window, _| {
                probe.line.set(Some(bounds));
                probe.mask.set(Some(window.content_mask().bounds));
                probe.line_prepainted.set(Some(probe.tick()));
            })
            .into_any_element()
    }

    /// A drag payload for a handle nobody drags in these tests.
    struct NoDrag;

    impl Render for NoDrag {
        fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
            Empty
        }
    }

    /// A dock-shaped box: 200px along the axis, clipped to itself the way
    /// `dock_frame` is, sitting between two 100px neighbours so both of its
    /// edges are seams. The handle hugs one of them.
    ///
    /// The first neighbour floats a popover across both seams, deferred at the
    /// default priority the way an application's own `deferred(anchored())`
    /// is -- a panel drawn before the dock, opening something over it.
    struct HuggingHarness {
        axis: Axis,
        edge: HandleEdge,
        probe: Rc<Probe>,
    }

    impl Render for HuggingHarness {
        fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
            let axis = self.axis;
            let probe = self.probe.clone();
            let neighbour = || match axis {
                Axis::Horizontal => div().w(px(100.)).h_full(),
                Axis::Vertical => div().h(px(100.)).w_full(),
            };
            let popover = div()
                .absolute()
                .map(|popover| match axis {
                    Axis::Horizontal => popover.top_0().left(px(50.)).w(px(300.)).h_full(),
                    Axis::Vertical => popover.left_0().top(px(50.)).h(px(300.)).w_full(),
                })
                .on_prepaint({
                    let probe = probe.clone();
                    move |_, _, _| probe.popover_prepainted.set(Some(probe.tick()))
                });
            div()
                .flex()
                .map(|row| match axis {
                    Axis::Horizontal => row.flex_row().w(px(400.)).h(px(100.)),
                    Axis::Vertical => row.flex_col().h(px(400.)).w(px(100.)),
                })
                .child(neighbour().child(deferred(popover)))
                .child(
                    div()
                        .relative()
                        .overflow_hidden()
                        .map(|dock| match axis {
                            Axis::Horizontal => dock.w(px(200.)).h_full(),
                            Axis::Vertical => dock.h(px(200.)).w_full(),
                        })
                        .child(
                            resize_handle::<(), NoDrag>("hugging", axis)
                                .inside(self.edge)
                                .with_appearance(Rc::new(
                                    move |handle: &ResizeHandleContext,
                                          _: &mut Window,
                                          _: &mut App| {
                                        probe.edge.set(Some(handle.edge()));
                                        Some(hairline(handle.axis(), probe.clone()))
                                    },
                                )),
                        ),
                )
                .child(neighbour())
        }
    }

    fn draw_hugging(cx: &mut TestAppContext, axis: Axis, edge: HandleEdge) -> Rc<Probe> {
        let probe = Rc::new(Probe::default());
        let (_, cx) = cx.add_window_view({
            let probe = probe.clone();
            move |_, _| HuggingHarness { axis, edge, probe }
        });
        cx.update(|window, cx| window.draw(cx).clear(cx));
        probe
    }

    /// The seam a hugging handle marks, along its axis.
    ///
    /// The dock spans 100..300 in the harness, so its leading seam is at 100
    /// and its trailing one at 300.
    fn seam(edge: HandleEdge) -> Pixels {
        match edge {
            HandleEdge::Leading => px(100.),
            HandleEdge::Trailing => px(300.),
        }
    }

    fn along(axis: Axis, bounds: Bounds<Pixels>) -> (Pixels, Pixels) {
        match axis {
            Axis::Horizontal => (bounds.left(), bounds.right()),
            Axis::Vertical => (bounds.top(), bounds.bottom()),
        }
    }

    /// A hugging handle's hairline is the container's outermost pixel.
    ///
    /// This is the regression. The line was set one pixel in from the
    /// boundary, to make room for an indicator to overhang it, and along the
    /// whole seam that pixel of the dock showed past the line on one side of
    /// the area and opened a gap before it on the other.
    #[gpui::test]
    fn a_hugging_handle_draws_its_line_on_the_seam(cx: &mut TestAppContext) {
        for axis in [Axis::Horizontal, Axis::Vertical] {
            for edge in [HandleEdge::Leading, HandleEdge::Trailing] {
                let probe = draw_hugging(cx, axis, edge);
                let line = probe.line.get().expect("the renderer was asked to draw");
                let (start, end) = along(axis, line);
                let expected = match edge {
                    HandleEdge::Leading => (seam(edge), seam(edge) + px(1.)),
                    HandleEdge::Trailing => (seam(edge) - px(1.), seam(edge)),
                };
                assert_eq!(
                    (start, end),
                    expected,
                    "{axis:?} {edge:?}: the hairline has to be the pixel against the seam"
                );
            }
        }
    }

    /// A hugging handle's appearance is painted in tree order, under its
    /// container's mask -- beneath whatever the application floats over the
    /// panels around it.
    ///
    /// This is the regression. The appearance was deferred so that an
    /// indicator centred on the hairline would keep the pixel overhanging the
    /// container, and a deferred element paints after the whole tree at a
    /// priority no lower than the default. A popover an application defers
    /// from a panel drawn before the dock -- at that default priority, as
    /// `deferred(anchored())` is -- was painted first, and the dock's divider
    /// ran straight through it.
    #[gpui::test]
    fn a_hugging_handle_paints_beneath_a_popover_deferred_before_it(cx: &mut TestAppContext) {
        for axis in [Axis::Horizontal, Axis::Vertical] {
            for edge in [HandleEdge::Leading, HandleEdge::Trailing] {
                let probe = draw_hugging(cx, axis, edge);
                let line = probe
                    .line_prepainted
                    .get()
                    .expect("the line was prepainted");
                let popover = probe
                    .popover_prepainted
                    .get()
                    .expect("the popover was prepainted");
                assert!(
                    line < popover,
                    "{axis:?} {edge:?}: the line (prepaint #{line}) has to go down before \
                     the popover (#{popover}), or it is painted over it"
                );

                // The dock spans 100..300 along the axis; a deferred line would
                // have been prepainted under the window's mask instead.
                let mask = probe.mask.get().expect("the line was prepainted");
                assert_eq!(
                    along(axis, mask),
                    (px(100.), px(300.)),
                    "{axis:?} {edge:?}: the line is painted under its container's clip"
                );
            }
        }
    }

    /// A renderer is told which edge the handle hugs, so it can keep what it
    /// centres on the line clear of the container's clip itself.
    #[gpui::test]
    fn a_renderer_is_told_the_edge_a_handle_hugs(cx: &mut TestAppContext) {
        for axis in [Axis::Horizontal, Axis::Vertical] {
            for edge in [HandleEdge::Leading, HandleEdge::Trailing] {
                let probe = draw_hugging(cx, axis, edge);
                assert_eq!(probe.edge.get(), Some(Some(edge)), "{axis:?} {edge:?}");
            }
        }
    }

    #[test]
    fn a_listener_writes_its_progress_back_into_the_stored_state() {
        let stored = SharedHandleState::default();
        // What `paint` hands each mouse listener.
        let listener = stored.clone();

        assert!(listener.set(ResizeHandleState::Pressed));

        // The regression this pins down: the state used to be a bare `Cell`,
        // which clones by value, so a listener wrote into a copy that died
        // with the event and the handle never left `Idle`.
        assert_eq!(stored.get(), ResizeHandleState::Pressed);
        assert!(stored.get().is_active());
    }

    #[test]
    fn setting_the_state_it_already_has_asks_for_no_repaint() {
        let state = SharedHandleState::default();

        assert!(state.set(ResizeHandleState::Hovered));
        assert!(!state.set(ResizeHandleState::Hovered));
    }

    #[test]
    fn only_a_held_handle_is_active() {
        assert!(!ResizeHandleState::Idle.is_active());
        assert!(!ResizeHandleState::Hovered.is_active());
        assert!(ResizeHandleState::Pressed.is_active());
        assert!(ResizeHandleState::Dragging.is_active());
    }

    #[gpui::test]
    fn an_unprojected_handle_resolves_from_the_theme_tokens(cx: &mut TestAppContext) {
        cx.update(|cx| {
            let border = hsla(0., 0., 0.5, 1.0);
            let ring = hsla(0.6, 0.5, 0.5, 1.0);
            let theme = Theme::global_mut(cx);
            theme.tokens.colors.border = border;
            theme.tokens.colors.ring = ring;
            theme.resizable = ResizableTheme::default();

            let theme = Theme::global(cx);
            assert_eq!(handle_color(&theme, false), border);
            assert_eq!(handle_color(&theme, true), ring);
            // The point of the change: the default used to be transparent, so
            // a divider with nothing projected onto it was not drawn at all.
            assert_ne!(handle_color(&theme, false), gpui::Hsla::default());
        });
    }

    #[gpui::test]
    fn a_projected_handle_still_wins(cx: &mut TestAppContext) {
        cx.update(|cx| {
            let projected = hsla(0.3, 0.4, 0.5, 1.0);
            let active = hsla(0.9, 0.4, 0.5, 1.0);
            let theme = Theme::global_mut(cx);
            theme.tokens.colors.border = hsla(0., 0., 0.5, 1.0);
            theme.resizable = ResizableTheme {
                handle: Some(projected),
                active_handle: Some(active),
            };

            let theme = Theme::global(cx);
            assert_eq!(handle_color(&theme, false), projected);
            assert_eq!(handle_color(&theme, true), active);
        });
    }
}