Skip to main content

gpui_material_components/
ext.rs

1#[cfg(feature = "feedback")]
2use gpui::{App, ElementId, Window};
3use gpui::{IntoElement, SharedString};
4
5use crate::Badge;
6#[cfg(feature = "feedback")]
7use crate::{Menu, MenuItem, Tooltip};
8
9/// Fluent postfix composition helpers for attaching Material badges, tooltips,
10/// and context menus to any GPUI element.
11pub trait MaterialElementExt: IntoElement + Sized {
12    /// Anchors a small dot [`Badge`] to this element's upper trailing edge.
13    fn badge_dot(self) -> Badge {
14        Badge::dot(self)
15    }
16
17    /// Anchors a text [`Badge`] to this element's upper trailing edge.
18    fn badge_label(self, text: impl Into<SharedString>) -> Badge {
19        Badge::label(self, text)
20    }
21
22    /// Anchors a capped numeric [`Badge`] to this element's upper trailing edge.
23    fn badge_count(self, count: usize, maximum: usize) -> Badge {
24        Badge::count(self, count, maximum)
25    }
26
27    /// Wraps this element as the trigger of a plain [`Tooltip`].
28    #[cfg(feature = "feedback")]
29    fn tooltip(self, id: impl Into<ElementId>, text: impl Into<SharedString>) -> Tooltip {
30        Tooltip::plain(id, self, text)
31    }
32
33    /// Wraps this element as the trigger of a secondary-click context [`Menu`].
34    #[cfg(feature = "feedback")]
35    fn context_menu(
36        self,
37        id: impl Into<ElementId>,
38        items: impl IntoIterator<Item = MenuItem>,
39    ) -> Menu {
40        Menu::new(id, self, items).context()
41    }
42
43    /// Wraps this element as the trigger of a secondary-click context [`Menu`]
44    /// whose items are built lazily only while the menu is open.
45    #[cfg(feature = "feedback")]
46    fn context_menu_with<I>(
47        self,
48        id: impl Into<ElementId>,
49        build: impl FnOnce(&mut Window, &mut App) -> I + 'static,
50    ) -> Menu
51    where
52        I: IntoIterator<Item = MenuItem>,
53    {
54        Menu::new_with(id, self, build).context()
55    }
56}
57
58impl<T: IntoElement + Sized> MaterialElementExt for T {}