gpui-component 0.6.2

GPUI Component: the styled component library of GPUI Kit, with 60+ desktop UI components for GPUI.
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
//! The pointer and wheel event surface used by [`super::CarouselContent`].
//!
//! The mask is deliberately a sibling of the scrolled content.  A mask nested
//! inside the content would receive the content's scroll offset and would stop
//! covering the viewport after the first scroll.

use std::cell::RefCell;
use std::panic::Location;
use std::rc::Rc;

use gpui::{
    App, Axis, Bounds, ContentMask, Element, ElementId, GlobalElementId, Hitbox, IntoElement,
    IsZero as _, LayoutId, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent,
    OngoingScroll, Position, ScrollWheelEvent, Style, TouchPhase, Window, relative,
};

use super::state::CarouselState;
use crate::{AxisExt as _, OngoingScrollExt as _, global_state::GlobalState};

/// A viewport-sized, invisible event surface for a Carousel.
///
/// This surface owns pointer, wheel, and trackpad input so that a Carousel
/// nested in a list can lock its own axis while allowing cross-axis gestures
/// to continue to an ancestor.
pub(super) struct CarouselScrollMask {
    axis: Axis,
    id: ElementId,
    state: gpui::Entity<CarouselState>,
}

impl CarouselScrollMask {
    #[track_caller]
    pub(super) fn new(axis: Axis, state: &gpui::Entity<CarouselState>) -> Self {
        Self {
            axis,
            id: caller_id(),
            state: state.clone(),
        }
    }

    pub(super) fn id(mut self, id: impl Into<ElementId>) -> Self {
        self.id = id.into();
        self
    }
}

impl IntoElement for CarouselScrollMask {
    type Element = Self;

    fn into_element(self) -> Self::Element {
        self
    }
}

impl Element for CarouselScrollMask {
    type RequestLayoutState = ();
    type PrepaintState = Hitbox;

    fn id(&self) -> Option<ElementId> {
        let axis = if self.axis.is_horizontal() {
            "horizontal"
        } else {
            "vertical"
        };
        Some((self.id.clone(), axis).into())
    }

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

    fn request_layout(
        &mut self,
        _: Option<&GlobalElementId>,
        _: Option<&gpui::InspectorElementId>,
        window: &mut Window,
        cx: &mut App,
    ) -> (LayoutId, Self::RequestLayoutState) {
        let mut style = Style::default();
        style.position = Position::Absolute;
        style.inset.top = gpui::px(0.).into();
        style.inset.left = gpui::px(0.).into();
        style.size.width = relative(1.).into();
        style.size.height = relative(1.).into();
        (window.request_layout(style, None, cx), ())
    }

    fn prepaint(
        &mut self,
        _: Option<&GlobalElementId>,
        _: Option<&gpui::InspectorElementId>,
        bounds: Bounds<gpui::Pixels>,
        _: &mut Self::RequestLayoutState,
        window: &mut Window,
        _: &mut App,
    ) -> Self::PrepaintState {
        window.insert_hitbox(bounds, gpui::HitboxBehavior::Normal)
    }

    fn paint(
        &mut self,
        global_id: Option<&GlobalElementId>,
        _: Option<&gpui::InspectorElementId>,
        _: Bounds<gpui::Pixels>,
        _: &mut Self::RequestLayoutState,
        hitbox: &mut Self::PrepaintState,
        window: &mut Window,
        _: &mut App,
    ) {
        let axis = self.axis;
        let hitbox_id = hitbox.id;
        let bounds = hitbox.bounds;
        let state = self.state.clone();
        let ongoing_scroll = global_id
            .map(|global_id| {
                window.with_element_state::<Rc<RefCell<OngoingScroll>>, _>(global_id, |value, _| {
                    let value = value.unwrap_or_default();
                    (value.clone(), value)
                })
            })
            .unwrap_or_default();

        window.with_content_mask(Some(ContentMask { bounds }), |window| {
            let pointer_state = state.clone();
            window.on_mouse_event(move |event: &MouseDownEvent, phase, window, cx| {
                if phase.capture()
                    && event.button == MouseButton::Left
                    && hitbox_id.should_handle_scroll(window)
                {
                    let started =
                        pointer_state.update(cx, |state, cx| state.begin_drag(event.position, cx));
                    if started {
                        GlobalState::suppress_text_selection(cx);
                    }
                }
            });

            let pointer_state = state.clone();
            window.on_mouse_event(move |event: &MouseMoveEvent, phase, _, cx| {
                if !phase.capture() || event.pressed_button != Some(MouseButton::Left) {
                    return;
                }

                let handled =
                    pointer_state.update(cx, |state, cx| state.update_drag(event.position, cx));
                if handled {
                    cx.stop_propagation();
                }
            });

            let pointer_state = state.clone();
            let mut suppress_release = false;
            window.on_mouse_event(move |event: &MouseUpEvent, phase, window, cx| {
                if event.button != MouseButton::Left {
                    return;
                }

                if phase.capture() {
                    let snapshot = pointer_state.read(cx);
                    let handled = snapshot.is_pointer_drag_locked();
                    suppress_release = handled || snapshot.should_suppress_pointer_click();
                    pointer_state.update(cx, |state, cx| {
                        state.finish_drag(cx);
                    });
                    if suppress_release {
                        // The release may be stopped before TextSelectionLayer's
                        // bubble listener. Clear its observable selection state
                        // here so a Carousel drag cannot leave a stale drag or
                        // participant-local selection behind.
                        gpui_base::TextSelection::clear(window, cx);
                    }
                } else if suppress_release {
                    // All capture handlers have now cleared their pressed
                    // state. Stop before descendant click handlers run.
                    suppress_release = false;
                    cx.stop_propagation();
                }
            });

            window.on_mouse_event(move |event: &ScrollWheelEvent, phase, window, cx| {
                if !phase.capture() || !hitbox_id.should_handle_scroll(window) {
                    return;
                }

                let mut delta = event.delta.pixel_delta(window.line_height());
                if event.delta.precise() {
                    ongoing_scroll
                        .borrow_mut()
                        .lock_axis(&mut delta, event.touch_phase);
                }

                if !delta.x.is_zero() && !delta.y.is_zero() {
                    if delta.x.abs() > delta.y.abs() {
                        delta.y = gpui::Pixels::ZERO;
                    } else {
                        delta.x = gpui::Pixels::ZERO;
                    }
                }

                // Ignore the secondary axis. The lock above keeps this stable
                // throughout a precise trackpad gesture.
                if axis.is_horizontal() {
                    delta.y = gpui::Pixels::ZERO;
                } else {
                    delta.x = gpui::Pixels::ZERO;
                }

                let precise = event.delta.precise();
                let primary_delta = if axis.is_horizontal() {
                    delta.x
                } else {
                    delta.y
                };
                let consumed = if primary_delta.is_zero() {
                    false
                } else if !precise {
                    state.update(cx, |state, cx| {
                        state.handle_wheel_step(axis, primary_delta, cx)
                    })
                } else if matches!(event.touch_phase, TouchPhase::Ended | TouchPhase::Cancelled) {
                    false
                } else {
                    // A gesture this carousel already owns stays here even at
                    // an edge, so its leftover never scrolls an ancestor. One
                    // that begins at an edge belongs to the ancestor until it
                    // ends.
                    let owned = state.read(cx).has_scroll_gesture();
                    let moved = state.update(cx, |state, cx| {
                        state.handle_scroll_delta(axis, primary_delta, event.touch_phase, cx)
                    });
                    if !moved && !owned {
                        state.update(cx, |state, cx| state.defer_scroll_to_ancestor(cx));
                    }
                    moved || owned
                };

                if precise {
                    match event.touch_phase {
                        TouchPhase::Ended => {
                            state.update(cx, |state, cx| state.finish_scroll(false, cx));
                        }
                        TouchPhase::Cancelled => {
                            state.update(cx, |state, cx| state.finish_scroll(true, cx));
                        }
                        TouchPhase::Started | TouchPhase::Moved => {}
                    }
                }

                // Horizontal carousels retain every gesture at their edge. A
                // vertical carousel hands a gesture that begins at an edge to
                // an ancestor so the surrounding document keeps scrolling.
                if consumed || (axis.is_horizontal() && !primary_delta.is_zero()) {
                    cx.stop_propagation();
                }
            });
        });
    }
}

#[track_caller]
fn caller_id() -> ElementId {
    ElementId::CodeLocation(*Location::caller())
}

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

    use gpui::{
        AppContext as _, Axis, Context, Entity, InteractiveElement as _, IntoElement, Modifiers,
        MouseButton, ParentElement as _, Render, ScrollDelta, ScrollHandle, ScrollWheelEvent,
        StatefulInteractiveElement as _, Styled as _, TestAppContext, TouchPhase,
        VisualTestContext, Window, div, point, px,
    };

    use crate::{
        button::Button,
        carousel::{
            Carousel, CarouselContent, CarouselItem, CarouselState, state::SCROLL_EVENT_SEPARATION,
        },
    };

    struct ButtonDragHarness {
        state: Entity<CarouselState>,
        clicks: Rc<Cell<usize>>,
    }

    impl Render for ButtonDragHarness {
        fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
            let clicks = self.clicks.clone();
            Carousel::new("button-drag-carousel", &self.state)
                .w(px(100.))
                .h(px(40.))
                .child(
                    CarouselContent::new(&self.state)
                        .h(px(40.))
                        .child(
                            CarouselItem::new("button-slide", 0, &self.state)
                                .h(px(40.))
                                .child(
                                    Button::new("slide-button")
                                        .w_full()
                                        .h(px(40.))
                                        .on_click(move |_, _, _| clicks.set(clicks.get() + 1)),
                                ),
                        )
                        .child(
                            CarouselItem::new("plain-slide", 1, &self.state)
                                .h(px(40.))
                                .child("Second"),
                        ),
                )
        }
    }

    #[gpui::test]
    fn dragging_over_child_button_suppresses_click_without_leaving_it_pending(
        cx: &mut TestAppContext,
    ) {
        cx.update(crate::init);
        let state = cx.update(|cx| cx.new(|_| CarouselState::new(2)));
        let clicks = Rc::new(Cell::new(0));
        let (_, cx) = cx.add_window_view({
            let state = state.clone();
            let clicks = clicks.clone();
            move |_, _| ButtonDragHarness { state, clicks }
        });
        let cx: &mut VisualTestContext = cx;
        cx.update(|window, cx| window.draw(cx).clear(cx));

        cx.simulate_click(point(px(10.), px(10.)), Modifiers::default());
        assert_eq!(clicks.get(), 1);
        clicks.set(0);

        cx.simulate_mouse_down(
            point(px(30.), px(10.)),
            MouseButton::Left,
            Modifiers::default(),
        );
        assert!(state.read_with(cx, |state, _| state.is_interacting()));
        cx.simulate_mouse_move(
            point(px(10.), px(10.)),
            Some(MouseButton::Left),
            Modifiers::default(),
        );
        assert!(state.read_with(cx, |state, _| state.is_pointer_drag_locked()));
        cx.simulate_mouse_up(
            point(px(10.), px(10.)),
            MouseButton::Left,
            Modifiers::default(),
        );
        assert_eq!(clicks.get(), 0);

        cx.update(|window, cx| window.draw(cx).clear(cx));
        cx.simulate_click(point(px(10.), px(10.)), Modifiers::default());
        assert_eq!(clicks.get(), 1);
    }

    struct NestedVerticalCarouselHarness {
        state: Entity<CarouselState>,
        outer_handle: ScrollHandle,
    }

    impl Render for NestedVerticalCarouselHarness {
        fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
            div()
                .id("outer-scroll")
                .w(px(100.))
                .h(px(100.))
                .overflow_y_scroll()
                .track_scroll(&self.outer_handle)
                .child(
                    Carousel::new("vertical-carousel", &self.state)
                        .w(px(100.))
                        .h(px(60.))
                        .child(
                            CarouselContent::new(&self.state)
                                .h_full()
                                .children((0..2).map(|index| {
                                    CarouselItem::new(("vertical-slide", index), index, &self.state)
                                        .child(index.to_string())
                                })),
                        ),
                )
                .child(div().w_full().h(px(400.)))
        }
    }

    fn nested_vertical_carousel<'a>(
        cx: &'a mut TestAppContext,
        state: &Entity<CarouselState>,
    ) -> (ScrollHandle, &'a mut VisualTestContext) {
        let outer_handle = ScrollHandle::new();
        let (_, cx) = cx.add_window_view({
            let state = state.clone();
            let outer_handle = outer_handle.clone();
            move |_, _| NestedVerticalCarouselHarness {
                state,
                outer_handle,
            }
        });
        cx.update(|window, cx| window.draw(cx).clear(cx));
        (outer_handle, cx)
    }

    fn wheel_line(cx: &mut VisualTestContext, lines: f32) {
        cx.simulate_event(ScrollWheelEvent {
            position: point(px(20.), px(20.)),
            delta: ScrollDelta::Lines(point(0., lines)),
            ..Default::default()
        });
    }

    fn swipe(cx: &mut VisualTestContext, delta: f32, touch_phase: TouchPhase) {
        cx.simulate_event(ScrollWheelEvent {
            position: point(px(20.), px(20.)),
            delta: ScrollDelta::Pixels(point(px(0.), px(delta))),
            touch_phase,
            ..Default::default()
        });
    }

    #[gpui::test]
    fn vertical_carousel_hands_scroll_to_parent_at_edge(cx: &mut TestAppContext) {
        cx.update(crate::init);
        let state = cx.update(|cx| {
            cx.new(|_| {
                CarouselState::new(2)
                    .with_axis(Axis::Vertical)
                    .with_selected_index(1)
            })
        });
        let (outer_handle, cx) = nested_vertical_carousel(cx, &state);

        wheel_line(cx, -1.);

        assert_eq!(
            state.read_with(cx, |state, _| state.selected_index()),
            Some(1)
        );
        assert!(outer_handle.offset().y < px(0.));
    }

    #[gpui::test]
    fn vertical_carousel_keeps_a_wheel_burst_away_from_the_parent(cx: &mut TestAppContext) {
        cx.update(crate::init);
        let state = cx.update(|cx| cx.new(|_| CarouselState::new(2).with_axis(Axis::Vertical)));
        let (outer_handle, cx) = nested_vertical_carousel(cx, &state);

        // One notch steps once; the rest of its burst stays here.
        wheel_line(cx, -1.);
        wheel_line(cx, -1.);
        assert_eq!(
            state.read_with(cx, |state, _| state.selected_index()),
            Some(1)
        );
        assert_eq!(outer_handle.offset().y, px(0.));

        // A new notch at the edge scrolls the surrounding document instead.
        cx.executor().advance_clock(SCROLL_EVENT_SEPARATION);
        cx.run_until_parked();
        wheel_line(cx, -1.);
        assert_eq!(
            state.read_with(cx, |state, _| state.selected_index()),
            Some(1)
        );
        assert!(outer_handle.offset().y < px(0.));
    }

    #[gpui::test]
    fn vertical_carousel_owns_a_trackpad_gesture_until_it_ends(cx: &mut TestAppContext) {
        cx.update(crate::init);
        let state = cx.update(|cx| cx.new(|_| CarouselState::new(2).with_axis(Axis::Vertical)));
        let (outer_handle, cx) = nested_vertical_carousel(cx, &state);

        // The swipe overshoots the last item; the leftover stays here.
        swipe(cx, -50., TouchPhase::Started);
        swipe(cx, -50., TouchPhase::Moved);
        swipe(cx, -50., TouchPhase::Moved);
        swipe(cx, 0., TouchPhase::Ended);
        assert_eq!(outer_handle.offset().y, px(0.));
        assert_eq!(
            state.read_with(cx, |state, _| state.selected_index()),
            Some(1)
        );

        // A gesture that begins at the edge scrolls the document instead.
        swipe(cx, -50., TouchPhase::Started);
        assert!(outer_handle.offset().y < px(0.));
    }
}