Skip to main content

gpui_material_components/overlay/tooltip/
label.rs

1use gpui::{
2    App, ElementId, Entity, EntityId, IntoElement, Pixels, RenderOnce, SharedString, Subscription,
3    Window,
4};
5use gpui_material_foundation::InteractionSource;
6
7use super::{Tooltip, TooltipCaret, TooltipPlacement};
8
9/// An overlay annotation for arbitrary anchor content and its own interaction source.
10#[derive(IntoElement)]
11pub struct Label {
12    tooltip: Tooltip,
13    source: Option<(Entity<InteractionSource>, ElementId)>,
14    persistent: bool,
15}
16
17impl Label {
18    /// Creates an overlay label anchored to `anchor` with the given accessible description.
19    pub fn new(
20        id: impl Into<ElementId>,
21        anchor: impl IntoElement,
22        description: impl Into<SharedString>,
23    ) -> Self {
24        Self {
25            tooltip: Tooltip::plain(id, anchor, description),
26            source: None,
27            persistent: false,
28        }
29    }
30
31    /// Supplies decorative content. The constructor's description names the overlay.
32    pub fn content(mut self, content: impl IntoElement) -> Self {
33        self.tooltip = self.tooltip.body_content(content);
34        self
35    }
36
37    /// Observes one control under the given ID in this label's window.
38    pub fn interaction_source(
39        mut self,
40        source: &Entity<InteractionSource>,
41        control_id: impl Into<ElementId>,
42    ) -> Self {
43        self.source = Some((source.clone(), control_id.into()));
44        self
45    }
46
47    /// Shows the label continuously while mounted.
48    pub fn persistent(mut self, persistent: bool) -> Self {
49        self.persistent = persistent;
50        self
51    }
52
53    /// Sets the preferred side relative to the anchor.
54    pub fn placement(mut self, placement: TooltipPlacement) -> Self {
55        self.tooltip = self.tooltip.placement(placement);
56        self
57    }
58
59    /// Sets the gap between the anchor and label surface in logical pixels.
60    pub fn offset(mut self, offset: Pixels) -> Self {
61        self.tooltip = self.tooltip.offset(offset);
62        self
63    }
64
65    /// Overrides the maximum width of the label surface in logical pixels.
66    pub fn max_width(mut self, width: Pixels) -> Self {
67        self.tooltip = self.tooltip.max_width(width);
68        self
69    }
70
71    /// Renders a pointer caret pointing toward the anchor.
72    pub fn caret(mut self, caret: TooltipCaret) -> Self {
73        self.tooltip = self.tooltip.caret(caret);
74        self
75    }
76}
77
78impl RenderOnce for Label {
79    fn render(mut self, window: &mut Window, cx: &mut App) -> impl IntoElement {
80        let observed = self.source.as_ref().filter(|_| !self.persistent);
81        let observation = window.use_keyed_state(
82            (self.tooltip.id.clone(), "label-interaction-source"),
83            cx,
84            |_, _| None::<(EntityId, Subscription)>,
85        );
86        let source_id = observed.map(|(source, _)| source.entity_id());
87        observation.update(cx, |observation, cx| {
88            if observation.as_ref().map(|(id, _)| *id) != source_id {
89                *observation = observed.map(|(source, _)| {
90                    (
91                        source.entity_id(),
92                        window.observe(source, cx, |_, window, _| window.refresh()),
93                    )
94                });
95            }
96        });
97        let window_id = window.window_handle().window_id();
98        let active = observed
99            .and_then(|(source, control)| source.read(cx).state(window_id, control))
100            .is_some_and(|state| {
101                !state.disabled
102                    && (state.hovered || state.pressed || state.dragged || state.focus_visible)
103            });
104        self.tooltip.label_visible = Some(self.persistent || active);
105        self.tooltip.render(window, cx)
106    }
107}