mod appearance;
mod interaction;
mod label;
mod render;
mod surface;
use std::{rc::Rc, time::Duration};
use gpui::{
AnyElement, App, Bounds, ElementId, Entity, FocusHandle, IntoElement, KeyBinding, Pixels,
SharedString, Size, Window, px,
};
use gpui_material_theme::{CornerShape, Direction, Elevation};
use appearance::Appearance;
pub use appearance::{TooltipCaret, TooltipColors, TooltipPlacement};
pub use gpui_base::Positioner as TooltipPositioner;
pub use interaction::TooltipState;
pub use label::Label;
use crate::control_style::finite_length;
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,
}
#[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 {
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),
}
}
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)
}
}
pub fn shortcut(mut self, text: impl Into<SharedString>) -> Self {
self.shortcut = Some(text.into());
self
}
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
}
pub fn title_content(mut self, content: impl IntoElement) -> Self {
self.rich = true;
self.title_content = Some(content.into_any_element());
self
}
pub fn body_content(mut self, content: impl IntoElement) -> Self {
self.body_content = Some(content.into_any_element());
self
}
pub fn action_content(mut self, content: impl IntoElement) -> Self {
self.rich = true;
self.action_content = Some(content.into_any_element());
self
}
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
}
pub fn show_delay(mut self, delay: Duration) -> Self {
self.show_delay = delay;
self
}
pub fn hide_delay(mut self, delay: Duration) -> Self {
self.hide_delay = delay;
self
}
pub fn track_focus(mut self, focus: &FocusHandle) -> Self {
self.trigger_focus = Some(focus.clone());
self
}
pub fn focus_target(mut self, focus: &FocusHandle) -> Self {
self.focus_target = Some(focus.clone());
self
}
pub fn focusable(mut self, focusable: bool) -> Self {
self.focusable = focusable;
self
}
pub fn enable_user_input(mut self, enable: bool) -> Self {
self.enable_user_input = enable;
self
}
pub fn persistent(mut self, state: &Entity<TooltipState>) -> Self {
self.rich = true;
self.persistent = Some(state.clone());
self
}
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
}
pub fn placement(mut self, placement: TooltipPlacement) -> Self {
self.placement = placement;
self
}
pub fn offset(mut self, offset: Pixels) -> Self {
self.offset = finite_length(offset);
self
}
pub fn positioner(
mut self,
positioner: impl Fn(Bounds<Pixels>, Size<Pixels>, Direction) -> TooltipPositioner + 'static,
) -> Self {
self.positioner = Some(Box::new(positioner));
self
}
pub fn caret(mut self, caret: TooltipCaret) -> Self {
self.appearance.caret = Some(caret);
self
}
pub fn max_width(mut self, width: Pixels) -> Self {
self.appearance.max_width = Some(finite_length(width));
self
}
pub fn shape(mut self, shape: CornerShape) -> Self {
self.appearance.shape = Some(shape);
self
}
pub fn colors(mut self, colors: TooltipColors) -> Self {
self.appearance.colors = colors;
self
}
pub fn tonal_elevation(mut self, elevation: Elevation) -> Self {
self.appearance.tonal_elevation = elevation;
self
}
pub fn shadow_elevation(mut self, elevation: Elevation) -> Self {
self.appearance.shadow_elevation = Some(elevation);
self
}
}