tooltip.rs

 1use super::{ContainerStyle, Element, ElementBox};
 2use crate::{
 3    geometry::{rect::RectF, vector::Vector2F},
 4    json::{json, ToJson},
 5    ElementStateHandle, LayoutContext, PaintContext, RenderContext, SizeConstraint, View,
 6};
 7
 8pub struct Tooltip {
 9    state: ElementStateHandle<TooltipState>,
10    child: ElementBox,
11    style: ContainerStyle,
12    text: String,
13}
14
15#[derive(Default)]
16struct TooltipState {}
17
18impl Tooltip {
19    pub fn new<T: View>(
20        id: usize,
21        child: ElementBox,
22        text: String,
23        cx: &mut RenderContext<T>,
24    ) -> Self {
25        Self {
26            state: cx.element_state::<Self, _>(id),
27            child,
28            text,
29            style: Default::default(),
30        }
31    }
32
33    pub fn with_style(mut self, style: ContainerStyle) -> Self {
34        self.style = style;
35        self
36    }
37}
38
39impl Element for Tooltip {
40    type LayoutState = ();
41    type PaintState = ();
42
43    fn layout(
44        &mut self,
45        constraint: SizeConstraint,
46        cx: &mut LayoutContext,
47    ) -> (Vector2F, Self::LayoutState) {
48        let size = self.child.layout(constraint, cx);
49        (size, ())
50    }
51
52    fn paint(
53        &mut self,
54        bounds: RectF,
55        visible_bounds: RectF,
56        _: &mut Self::LayoutState,
57        cx: &mut PaintContext,
58    ) {
59        self.child.paint(bounds.origin(), visible_bounds, cx);
60    }
61
62    fn dispatch_event(
63        &mut self,
64        event: &crate::Event,
65        _: RectF,
66        _: RectF,
67        _: &mut Self::LayoutState,
68        _: &mut Self::PaintState,
69        cx: &mut crate::EventContext,
70    ) -> bool {
71        self.child.dispatch_event(event, cx)
72    }
73
74    fn debug(
75        &self,
76        _: RectF,
77        _: &Self::LayoutState,
78        _: &Self::PaintState,
79        cx: &crate::DebugContext,
80    ) -> serde_json::Value {
81        json!({
82            "child": self.child.debug(cx),
83            "style": self.style.to_json(),
84            "text": &self.text,
85        })
86    }
87}