Skip to main content

pax_std/forms/
tooltip.rs

1#[allow(unused)]
2use crate::*;
3use pax_engine::api::{Click, Event, MouseOut, MouseOver, Property};
4use pax_engine::*;
5use pax_runtime::api::NodeContext;
6use pax_runtime::{
7    bind_content_measurement_effect, resolve_axis_autosize, sync_content_autosize_with_axes,
8    ContentMeasurementGeometry,
9};
10
11/// A simple hover tooltip that renders slotted content plus a floating text tip.
12#[pax]
13#[engine_import_path("pax_engine")]
14#[inlined(
15    <Group>
16        // Keep the trigger owned by Tooltip so hover/click reach the wrapper
17        // even when the slotted content belongs to an outer component.
18        <Rectangle width=100% height=100% fill=TRANSPARENT/>
19        for i in 0..self._projected_children_count {
20            slot(i)
21        }
22    </Group>
23    if self._showing {
24        <Frame x=0px y={100% + 8px} anchor_x=0% anchor_y=0% width=220px autosize_y=true corner_radius=8.0 layout_role=LayoutRole::Breakout>
25            <Text x=12px y=10px width={100% - 24px} id=text text={self.tip}/>
26            <Rectangle
27                width=100%
28                height=100%
29                fill=rgb(12.5%, 12.5%, 12.5%)
30                corner_radius=8.00/>
31        </Frame>
32    }
33    @settings {
34        @click: self.toggle
35        @mouse_over: self.mouse_over
36        @mouse_out: self.mouse_out
37        @mount: on_mount
38        #text {
39            selectable: false,
40            style: {
41                    font: {
42                        family: "ff-real-headline-pro"
43                        url: "https://use.typekit.net/ivu7epf.css"
44                        weight: 200
45                    },
46                    font_size: 16px,
47                    fill: WHITE,
48                    align_vertical: TextAlignVertical::Top,
49                    align_horizontal: TextAlignHorizontal::Left,
50                    align_multiline: TextAlignHorizontal::Left
51            }
52        }
53    }
54
55)]
56pub struct Tooltip {
57    /// Tooltip text.
58    pub tip: Property<String>,
59    /// Automatically sizes the trigger wrapper to its slotted content when possible.
60    pub autosize: Property<bool>,
61    /// Optional override for whether autosize manages the `x` axis.
62    pub autosize_x: Property<Option<bool>>,
63    /// Optional override for whether autosize manages the `y` axis.
64    pub autosize_y: Property<Option<bool>>,
65    // Private visibility flag driven by hover.
66    pub _showing: Property<bool>,
67    // Private slot count mirrored from `NodeContext`.
68    pub _projected_children_count: Property<usize>,
69}
70
71impl Tooltip {
72    // Mirrors slot count and reactive autosize behavior for the inline template.
73    pub fn on_mount(&mut self, ctx: &NodeContext) {
74        let projected_children_count = ctx.projected_children_count.clone();
75        let deps = [projected_children_count.untyped()];
76        self._projected_children_count
77            .replace_with(Property::computed(
78                move || projected_children_count.get(),
79                &deps,
80            ));
81        let Some(expanded_node) = ctx.expanded_node.upgrade() else {
82            return;
83        };
84        let autosize = self.autosize.clone();
85        let autosize_x = self.autosize_x.clone();
86        let autosize_y = self.autosize_y.clone();
87        let deps = [
88            autosize.untyped(),
89            autosize_x.untyped(),
90            autosize_y.untyped(),
91        ];
92        bind_content_measurement_effect(
93            &expanded_node,
94            ctx,
95            "tooltip autosize",
96            ContentMeasurementGeometry::Placed,
97            &deps,
98            move |node, node_ctx| {
99                sync_content_autosize_with_axes(
100                    node,
101                    node_ctx,
102                    resolve_axis_autosize(autosize.get(), autosize_x.get(), true),
103                    resolve_axis_autosize(autosize.get(), autosize_y.get(), true),
104                );
105            },
106        );
107    }
108
109    // Toggles the tooltip on click/tap for touch-first environments.
110    pub fn toggle(&mut self, _ctx: &NodeContext, _event: Event<Click>) {
111        self._showing.set(!self._showing.get());
112    }
113
114    // Shows the tooltip on hover.
115    pub fn mouse_over(&mut self, _ctx: &NodeContext, _event: Event<MouseOver>) {
116        self._showing.set(true);
117    }
118
119    // Hides the tooltip when hover exits.
120    pub fn mouse_out(&mut self, _ctx: &NodeContext, _event: Event<MouseOut>) {
121        self._showing.set(false);
122    }
123}