add tooltip

This commit is contained in:
2026-10-29 20:16:27 +07:00
parent 2f81f2848d
commit ae39ee35f7
5 changed files with 42 additions and 12 deletions
+1
View File
@@ -28,6 +28,7 @@ full = [
"input",
"popover",
"scroll_area",
"tooltip",
"menu",
"dialog",
"sheet",
+18 -1
View File
@@ -2,17 +2,20 @@
use gpui::prelude::FluentBuilder as _;
use gpui::{
App, BoxShadow, ClickEvent, ElementId, InteractiveElement as _, IntoElement,
AnyView, App, BoxShadow, ClickEvent, ElementId, InteractiveElement as _, IntoElement,
ParentElement as _, RenderOnce, Role, SharedString, StatefulInteractiveElement as _,
Styled as _, Window, px, relative,
};
use gpui_native_icons::{IconRole, icon};
use gpui_native_theme::{ActiveTheme as _, Size};
use std::rc::Rc;
use crate::common::{Sizable, h_flex};
/// The single activation handler, shared by pointer, Enter, and Space.
type ClickHandler = Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>;
/// The tooltip view builder gpui's `.tooltip(...)` expects.
type TooltipBuilder = Rc<dyn Fn(&mut Window, &mut App) -> AnyView>;
/// Which visual treatment a button uses.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
@@ -38,6 +41,7 @@ pub struct Button {
size: Size,
disabled: bool,
a11y_label: Option<SharedString>,
tooltip: Option<TooltipBuilder>,
on_click: Option<ClickHandler>,
}
@@ -55,6 +59,7 @@ impl Button {
size: Size::default(),
disabled: false,
a11y_label: None,
tooltip: None,
on_click: None,
}
}
@@ -74,6 +79,15 @@ impl Button {
self
}
/// Shows a tooltip view next to the button after it is hovered.
pub fn tooltip<F>(mut self, tooltip: F) -> Self
where
F: Fn(&mut Window, &mut App) -> AnyView + 'static,
{
self.tooltip = Some(Rc::new(tooltip));
self
}
/// Sets the visual treatment.
pub fn variant(mut self, variant: ButtonVariant) -> Self {
self.variant = variant;
@@ -173,6 +187,9 @@ impl RenderOnce for Button {
})
.when_some(self.label, |button, label| button.child(label))
.when(self.disabled, |button| button.opacity(0.5))
.when_some(self.tooltip, |button, tooltip| {
button.tooltip(move |window, cx| tooltip(window, cx))
})
.when(!self.disabled, |button| {
button
.cursor_pointer()
+3
View File
@@ -45,6 +45,9 @@ pub mod popover;
#[cfg(feature = "scroll_area")]
pub mod scroll_area;
#[cfg(feature = "tooltip")]
pub mod tooltip;
/// Initializes everything the components need at startup.
pub fn init(cx: &mut App) {
gpui_native_theme::Theme::init(cx);