Skip to main content

pax_std/forms/
tooltip.rs

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