use std::rc::Rc; use gpui::{ Anchor, AnyElement, App, Context, ElementId, InteractiveElement as _, IntoElement, ParentElement, RenderOnce, StyleRefinement, Styled, Window, div, prelude::FluentBuilder as _, }; use gpui_base::HoverCard as BaseHoverCard; pub use gpui_base::HoverCardState; use instant::Duration; use crate::{StyledExt as _, popover::Popover}; /// A hover card element that displays content when hovering over a trigger element. /// /// Similar to Popover but triggered by mouse hover instead of click, with configurable delays /// for showing and hiding the content. On iOS and Android, tapping the trigger /// toggles the card and tapping outside dismisses it; hover delays are ignored. #[derive(IntoElement)] pub struct HoverCard { id: ElementId, style: StyleRefinement, anchor: Anchor, trigger: Option AnyElement + 'static>>, content: Option< Rc< dyn Fn(&mut HoverCardState, &mut Window, &mut Context) -> AnyElement + 'static, >, >, children: Vec, open_delay: Duration, close_delay: Duration, appearance: bool, on_open_change: Option>, } impl HoverCard { /// Create a new HoverCard. pub fn new(id: impl Into) -> Self { Self { id: id.into(), style: StyleRefinement::default(), anchor: Anchor::TopCenter, trigger: None, content: None, children: vec![], open_delay: Duration::from_secs_f64(0.6), close_delay: Duration::from_secs_f64(0.3), appearance: true, on_open_change: None, } } /// Set the anchor corner of the hover card, default is [`Anchor::TopCenter`]. pub fn anchor(mut self, anchor: impl Into) -> Self { self.anchor = anchor.into(); self } /// Set the trigger element of the hover card. pub fn trigger(mut self, trigger: T) -> Self where T: IntoElement + 'static, { self.trigger = Some(Box::new(|_, _| trigger.into_any_element())); self } /// Set the content builder of the hover card. pub fn content(mut self, content: F) -> Self where F: Fn(&mut HoverCardState, &mut Window, &mut Context) -> E + 'static, E: IntoElement + 'static, { self.content = Some(Rc::new(move |state, window, cx| { content(state, window, cx).into_any_element() })); self } /// Set the delay before showing the hover card, default is 600ms. pub fn open_delay(mut self, duration: Duration) -> Self { self.open_delay = duration; self } /// Set the delay before hiding the hover card, default is 300ms. pub fn close_delay(mut self, duration: Duration) -> Self { self.close_delay = duration; self } /// Set whether to apply default appearance styles, default is `true`. pub fn appearance(mut self, appearance: bool) -> Self { self.appearance = appearance; self } /// Set a callback to be called when the open state changes. pub fn on_open_change(mut self, callback: F) -> Self where F: Fn(&bool, &mut Window, &mut App) + 'static, { self.on_open_change = Some(Rc::new(callback)); self } } impl Styled for HoverCard { fn style(&mut self) -> &mut StyleRefinement { &mut self.style } } impl ParentElement for HoverCard { fn extend(&mut self, elements: impl IntoIterator) { self.children.extend(elements); } } impl RenderOnce for HoverCard { fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement { let Some(trigger) = self.trigger else { return div().id("empty").into_any_element(); }; let anchor = self.anchor; let appearance = self.appearance; let content = self.content; let children = self.children; let style = self.style; BaseHoverCard::new(self.id) .anchor(anchor) .open_delay(self.open_delay) .close_delay(self.close_delay) .trigger((trigger)(window, cx)) .content(move |state, window, cx| { Popover::render_popover_content(anchor, appearance, window, cx) .overflow_hidden() .when_some(content, |this, content| { this.child((content)(state, window, cx)) }) .children(children) .refine_style(&style) }) .when_some(self.on_open_change, |this, callback| { this.on_open_change(move |open, window, cx| callback(open, window, cx)) }) .into_any_element() } }