gpui-material-components 0.4.2

Material 3 Expressive desktop 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
mod appearance;
mod interaction;
mod label;
mod render;
mod surface;

use std::{rc::Rc, time::Duration};

use gpui::{
    Anchor, AnyElement, App, Bounds, ElementId, Entity, FocusHandle, IntoElement, KeyBinding,
    Pixels, Point, SharedString, Size, Window, px,
};
use gpui_material_theme::{CornerShape, Direction, Elevation};

use appearance::Appearance;

pub use appearance::{TooltipCaret, TooltipColors, TooltipPlacement};
pub use interaction::TooltipState;
pub use label::Label;

use crate::control_style::finite_length;

#[derive(Clone, Copy, Debug, PartialEq)]
enum PositionerStrategy {
    Corner {
        anchor: Anchor,
        position: Point<Pixels>,
    },
    Side {
        trigger_bounds: Bounds<Pixels>,
        placement: Option<TooltipPlacement>,
        offset: Pixels,
    },
}

/// Positioning specification returned from [`Tooltip::positioner`].
///
/// Choose [`TooltipPositioner::side`] to place the tooltip along a side of a
/// trigger with automatic viewport flipping, or [`TooltipPositioner::corner`]
/// to anchor a corner of the tooltip at an explicit window coordinate. The
/// tooltip host applies viewport margins and pointer occlusion automatically.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TooltipPositioner {
    strategy: PositionerStrategy,
}

impl TooltipPositioner {
    /// Places the tooltip on a side of `trigger_bounds`, flipping when needed.
    pub fn side(trigger_bounds: Bounds<Pixels>) -> Self {
        Self {
            strategy: PositionerStrategy::Side {
                trigger_bounds,
                placement: None,
                offset: px(0.),
            },
        }
    }

    /// Places `anchor`'s corner of the tooltip at `position`, clamping to the viewport.
    pub fn corner(anchor: Anchor, position: Point<Pixels>) -> Self {
        Self {
            strategy: PositionerStrategy::Corner { anchor, position },
        }
    }

    /// Updates the requested corner position. Has no effect on a side-positioned tooltip.
    pub fn position(mut self, position: Point<Pixels>) -> Self {
        if let PositionerStrategy::Corner {
            position: current, ..
        } = &mut self.strategy
        {
            *current = position;
        }
        self
    }

    /// Sets the preferred side. Only meaningful for [`TooltipPositioner::side`].
    pub fn placement(mut self, placement: TooltipPlacement) -> Self {
        if let PositionerStrategy::Side {
            placement: slot, ..
        } = &mut self.strategy
        {
            *slot = Some(placement);
        }
        self
    }

    /// Sets the gap between the trigger and the tooltip in logical pixels.
    /// Only meaningful for [`TooltipPositioner::side`].
    pub fn offset(mut self, offset: Pixels) -> Self {
        if let PositionerStrategy::Side { offset: slot, .. } = &mut self.strategy {
            *slot = finite_length(offset);
        }
        self
    }

    pub(super) fn into_base(self, direction: Direction) -> gpui_base::Positioner {
        match self.strategy {
            PositionerStrategy::Corner { anchor, position } => {
                gpui_base::Positioner::corner(anchor, position)
            }
            PositionerStrategy::Side {
                trigger_bounds,
                placement,
                offset,
            } => {
                let mut base = gpui_base::Positioner::side(trigger_bounds).offset(offset);
                if let Some(placement) = placement {
                    base = base.placement(placement.physical(direction));
                }
                base
            }
        }
    }
}

gpui::actions!(
    material_tooltip,
    [
        FocusTooltipAction,
        FocusTooltipTrigger,
        DismissTooltip,
        ActivateTooltipAction
    ]
);

pub(crate) fn init(cx: &mut App) {
    cx.bind_keys([
        KeyBinding::new("tab", FocusTooltipAction, Some("MaterialTooltipTrigger")),
        KeyBinding::new(
            "shift-tab",
            FocusTooltipTrigger,
            Some("MaterialTooltipContent"),
        ),
        KeyBinding::new("escape", DismissTooltip, Some("MaterialTooltip")),
        KeyBinding::new(
            "enter",
            ActivateTooltipAction,
            Some("MaterialTooltipAction"),
        ),
        KeyBinding::new(
            "space",
            ActivateTooltipAction,
            Some("MaterialTooltipAction"),
        ),
    ]);
}

pub(super) type TooltipCallback = Rc<dyn Fn(&mut Window, &mut App)>;
pub(super) type PositionProvider =
    Box<dyn Fn(Bounds<Pixels>, Size<Pixels>, Direction) -> TooltipPositioner>;

pub(super) struct TooltipAction {
    pub(super) label: SharedString,
    pub(super) focus: FocusHandle,
    pub(super) callback: TooltipCallback,
}

/// A transient plain or rich Material tooltip, or a persistent rich tooltip
/// with caller-owned visibility.
#[derive(IntoElement)]
pub struct Tooltip {
    pub(super) id: ElementId,
    pub(super) trigger: Option<AnyElement>,
    pub(super) text: SharedString,
    pub(super) title: Option<SharedString>,
    pub(super) shortcut: Option<SharedString>,
    pub(super) action: Option<TooltipAction>,
    pub(super) title_content: Option<AnyElement>,
    pub(super) body_content: Option<AnyElement>,
    pub(super) action_content: Option<AnyElement>,
    pub(super) link: Option<(SharedString, TooltipCallback)>,
    pub(super) rich: bool,
    pub(super) trigger_focus: Option<FocusHandle>,
    pub(super) focus_target: Option<FocusHandle>,
    pub(super) focusable: bool,
    pub(super) enable_user_input: bool,
    pub(super) persistent: Option<Entity<TooltipState>>,
    pub(super) label_visible: Option<bool>,
    pub(super) on_dismiss_request: Option<TooltipCallback>,
    pub(super) placement: TooltipPlacement,
    pub(super) offset: Pixels,
    positioner: Option<PositionProvider>,
    appearance: Appearance,
    show_delay: Duration,
    pub(super) hide_delay: Duration,
}

impl Tooltip {
    /// Creates a plain tooltip anchored to `trigger` with the given descriptive text.
    pub fn plain(
        id: impl Into<ElementId>,
        trigger: impl IntoElement,
        text: impl Into<SharedString>,
    ) -> Self {
        Self {
            id: id.into(),
            trigger: Some(trigger.into_any_element()),
            text: text.into(),
            title: None,
            shortcut: None,
            action: None,
            title_content: None,
            body_content: None,
            action_content: None,
            link: None,
            rich: false,
            trigger_focus: None,
            focus_target: None,
            focusable: false,
            enable_user_input: true,
            persistent: None,
            label_visible: None,
            on_dismiss_request: None,
            placement: TooltipPlacement::Above,
            offset: px(4.),
            positioner: None,
            appearance: Appearance::default(),
            show_delay: Duration::from_millis(500),
            hide_delay: Duration::from_millis(200),
        }
    }

    /// Creates a rich tooltip anchored to `trigger` with a title and body text.
    pub fn rich(
        id: impl Into<ElementId>,
        trigger: impl IntoElement,
        title: impl Into<SharedString>,
        text: impl Into<SharedString>,
    ) -> Self {
        Self {
            title: Some(title.into()),
            rich: true,
            ..Self::plain(id, trigger, text)
        }
    }

    /// Appends a subtle formatted desktop shortcut hint beside plain tooltip
    /// text or in a rich tooltip header/body while keeping the constructor's
    /// text as the accessible description.
    pub fn shortcut(mut self, text: impl Into<SharedString>) -> Self {
        self.shortcut = Some(text.into());
        self
    }

    /// Adds a text button to a rich tooltip.
    pub fn action(
        mut self,
        label: impl Into<SharedString>,
        focus: &FocusHandle,
        callback: impl Fn(&mut Window, &mut App) + 'static,
    ) -> Self {
        self.rich = true;
        self.action = Some(TooltipAction {
            label: label.into(),
            focus: focus.clone(),
            callback: Rc::new(callback),
        });
        self
    }

    /// Replaces the rich tooltip title with custom content.
    pub fn title_content(mut self, content: impl IntoElement) -> Self {
        self.rich = true;
        self.title_content = Some(content.into_any_element());
        self
    }

    /// Replaces the tooltip body text with custom content while keeping the constructor text as the accessible description.
    pub fn body_content(mut self, content: impl IntoElement) -> Self {
        self.body_content = Some(content.into_any_element());
        self
    }

    /// Replaces the standard action with caller-authored interactive content.
    pub fn action_content(mut self, content: impl IntoElement) -> Self {
        self.rich = true;
        self.action_content = Some(content.into_any_element());
        self
    }

    /// Adds a semantic link. The callback owns the destination and navigation.
    pub fn link(
        mut self,
        label: impl Into<SharedString>,
        callback: impl Fn(&mut Window, &mut App) + 'static,
    ) -> Self {
        self.rich = true;
        self.link = Some((label.into(), Rc::new(callback)));
        self
    }

    /// Overrides the pointer hover delay before showing a transient tooltip (default 500 ms).
    pub fn show_delay(mut self, delay: Duration) -> Self {
        self.show_delay = delay;
        self
    }

    /// Overrides the pointer leave grace delay before hiding a transient tooltip (default 200 ms).
    pub fn hide_delay(mut self, delay: Duration) -> Self {
        self.hide_delay = delay;
        self
    }

    /// Uses the trigger's focus handle for keyboard activation and restoration.
    pub fn track_focus(mut self, focus: &FocusHandle) -> Self {
        self.trigger_focus = Some(focus.clone());
        self
    }

    /// Uses the caller's first focus target inside the tooltip.
    pub fn focus_target(mut self, focus: &FocusHandle) -> Self {
        self.focus_target = Some(focus.clone());
        self
    }

    /// Moves focus to the tooltip when it opens. The default preserves focus.
    pub fn focusable(mut self, focusable: bool) -> Self {
        self.focusable = focusable;
        self
    }

    /// Enables automatic trigger input for the chosen visibility mode.
    pub fn enable_user_input(mut self, enable: bool) -> Self {
        self.enable_user_input = enable;
        self
    }

    /// Uses caller-owned visibility for a persistent rich tooltip.
    pub fn persistent(mut self, state: &Entity<TooltipState>) -> Self {
        self.rich = true;
        self.persistent = Some(state.clone());
        self
    }

    /// Handles Escape or a persistent tooltip's outside press. A persistent
    /// tooltip with a handler closes when the owner accepts the request.
    pub fn on_dismiss_request(
        mut self,
        callback: impl Fn(&mut Window, &mut App) + 'static,
    ) -> Self {
        self.on_dismiss_request = Some(Rc::new(callback));
        self
    }

    /// Sets the preferred side relative to the trigger (default [`TooltipPlacement::Above`]).
    pub fn placement(mut self, placement: TooltipPlacement) -> Self {
        self.placement = placement;
        self
    }

    /// Gap between the trigger and surface in logical pixels.
    pub fn offset(mut self, offset: Pixels) -> Self {
        self.offset = finite_length(offset);
        self
    }

    /// Replaces the standard placement with a positioner built from the trigger
    /// bounds, viewport size, and direction. The host applies viewport margins.
    pub fn positioner(
        mut self,
        positioner: impl Fn(Bounds<Pixels>, Size<Pixels>, Direction) -> TooltipPositioner + 'static,
    ) -> Self {
        self.positioner = Some(Box::new(positioner));
        self
    }

    /// Renders a pointer caret pointing toward the trigger.
    pub fn caret(mut self, caret: TooltipCaret) -> Self {
        self.appearance.caret = Some(caret);
        self
    }

    /// Overrides the maximum width of the tooltip surface in logical pixels.
    pub fn max_width(mut self, width: Pixels) -> Self {
        self.appearance.max_width = Some(finite_length(width));
        self
    }

    /// Overrides the corner shape of the tooltip surface.
    pub fn shape(mut self, shape: CornerShape) -> Self {
        self.appearance.shape = Some(shape);
        self
    }

    /// Overrides the tooltip's container, content, title, and action colors.
    pub fn colors(mut self, colors: TooltipColors) -> Self {
        self.appearance.colors = colors;
        self
    }

    /// Sets the tonal elevation used to tint the tooltip surface.
    pub fn tonal_elevation(mut self, elevation: Elevation) -> Self {
        self.appearance.tonal_elevation = elevation;
        self
    }

    /// Overrides the shadow elevation cast by the tooltip surface.
    pub fn shadow_elevation(mut self, elevation: Elevation) -> Self {
        self.appearance.shadow_elevation = Some(elevation);
        self
    }
}

#[cfg(test)]
mod positioner_tests {
    use super::*;
    use gpui::{point, size};

    #[test]
    fn tooltip_positioner_is_material_owned_and_sanitizes_builders() {
        assert_ne!(
            std::any::TypeId::of::<TooltipPositioner>(),
            std::any::TypeId::of::<gpui_base::Positioner>()
        );
        let bounds = Bounds::new(point(px(10.), px(20.)), size(px(80.), px(30.)));
        let side = TooltipPositioner::side(bounds)
            .placement(TooltipPlacement::Start)
            .offset(px(12.));
        let invalid_offset = TooltipPositioner::side(bounds)
            .placement(TooltipPlacement::Start)
            .offset(px(f32::NAN));
        assert_ne!(side, invalid_offset);
        assert_eq!(
            invalid_offset,
            TooltipPositioner::side(bounds)
                .placement(TooltipPlacement::Start)
                .offset(px(0.))
        );
        let corner = TooltipPositioner::corner(Anchor::TopLeft, point(px(1.), px(2.)))
            .position(point(px(30.), px(40.)));
        assert_eq!(
            corner,
            TooltipPositioner::corner(Anchor::TopLeft, point(px(30.), px(40.)))
        );
        // Ensure conversion succeeds for both LTR and RTL without panicking.
        let _ = side.into_base(Direction::Ltr);
        let _ = side.into_base(Direction::Rtl);
        let _ = corner.into_base(Direction::Ltr);
    }
}