diff --git a/crates/gpui/src/elements.rs b/crates/gpui/src/elements.rs index 231339d9e07ed232bb61b1ac7c0ba38c2685c949..97e7e1bec221be4a6e4ebb0ac4f3d1570ff750e3 100644 --- a/crates/gpui/src/elements.rs +++ b/crates/gpui/src/elements.rs @@ -16,13 +16,14 @@ mod overlay; mod stack; mod svg; mod text; +mod tooltip; mod uniform_list; use self::expanded::Expanded; pub use self::{ align::*, canvas::*, constrained_box::*, container::*, empty::*, event_handler::*, flex::*, hook::*, image::*, keystroke_label::*, label::*, list::*, mouse_event_handler::*, overlay::*, - stack::*, svg::*, text::*, uniform_list::*, + stack::*, svg::*, text::*, tooltip::*, uniform_list::*, }; pub use crate::presenter::ChildView; use crate::{ diff --git a/crates/gpui/src/elements/tooltip.rs b/crates/gpui/src/elements/tooltip.rs new file mode 100644 index 0000000000000000000000000000000000000000..3ab433f161008b788a6d89e6faaace518949b654 --- /dev/null +++ b/crates/gpui/src/elements/tooltip.rs @@ -0,0 +1,87 @@ +use super::{ContainerStyle, Element, ElementBox}; +use crate::{ + geometry::{rect::RectF, vector::Vector2F}, + json::{json, ToJson}, + ElementStateHandle, LayoutContext, PaintContext, RenderContext, SizeConstraint, View, +}; + +pub struct Tooltip { + state: ElementStateHandle, + child: ElementBox, + style: ContainerStyle, + text: String, +} + +#[derive(Default)] +struct TooltipState {} + +impl Tooltip { + pub fn new( + id: usize, + child: ElementBox, + text: String, + cx: &mut RenderContext, + ) -> Self { + Self { + state: cx.element_state::(id), + child, + text, + style: Default::default(), + } + } + + pub fn with_style(mut self, style: ContainerStyle) -> Self { + self.style = style; + self + } +} + +impl Element for Tooltip { + type LayoutState = (); + type PaintState = (); + + fn layout( + &mut self, + constraint: SizeConstraint, + cx: &mut LayoutContext, + ) -> (Vector2F, Self::LayoutState) { + let size = self.child.layout(constraint, cx); + (size, ()) + } + + fn paint( + &mut self, + bounds: RectF, + visible_bounds: RectF, + _: &mut Self::LayoutState, + cx: &mut PaintContext, + ) { + self.child.paint(bounds.origin(), visible_bounds, cx); + } + + fn dispatch_event( + &mut self, + event: &crate::Event, + _: RectF, + _: RectF, + _: &mut Self::LayoutState, + _: &mut Self::PaintState, + cx: &mut crate::EventContext, + ) -> bool { + self.child.dispatch_event(event, cx) + } + + fn debug( + &self, + _: RectF, + _: &Self::LayoutState, + _: &Self::PaintState, + cx: &crate::DebugContext, + ) -> serde_json::Value { + json!({ + "child": self.child.debug(cx), + "style": self.style.to_json(), + "text": &self.text, + }) + } +}