Skip to main content

pax_std/forms/
tabs.rs

1#[allow(unused)]
2use crate::*;
3use pax_engine::api::*;
4use pax_engine::*;
5
6/// A component displaying a list of tabs, e.g. for tabbed navigation.
7#[pax]
8#[engine_import_path("pax_engine")]
9#[inlined(
10    <Group x=50% height=30px width={100%-4px} @click=on_click>
11        for (name, i) in self._names_filled {
12            <Group x={(100.0*i/(self._slot_count - 1))%} width={(100.0/self._slot_count)%}>
13    			//highlight selected
14    			<Rectangle x=50% y=100% width={100%-4px} height={100%-2px} fill={rgba(255, 255, 255, 30*(i == self.selected))}
15                corner_radius=[10.0, 10.0, 0.0, 0.0]
16    			/>
17    			<Text align={TextAlignHorizontal::Center} width=100% height=100% text={name}/>
18    			<Rectangle x=50% y=100% width={100%-4px} height={100%-2px} fill={self.color}
19                corner_radius=[10.0, 10.0, 0.0, 0.0]
20    			/>
21    		</Group>
22    	}
23    </Group>
24
25    <Group y=30px height={100% - 30px}>
26    	slot(self.selected)
27    </Group>
28    <Rectangle y=30px height={100% - 30px} fill={self.color}/>
29
30    @settings {
31        @mount: on_mount
32    }
33)]
34pub struct Tabs {
35    /// A list of string labels for the tabs
36    pub names: Property<Vec<String>>,
37    /// The index of the currently selected tab
38    pub selected: Property<usize>,
39    /// The background color of the tabs
40    pub color: Property<Color>,
41
42    // Private slot count mirrored from `NodeContext`.
43    pub _slot_count: Property<usize>,
44    // Private tab labels after filling missing labels with placeholders.
45    pub _names_filled: Property<Vec<String>>,
46}
47
48impl Tabs {
49    // Mirrors slot count and derives fallback tab labels for the inline template.
50    pub fn on_mount(&mut self, ctx: &NodeContext) {
51        let slot_count = ctx.projected_children_count.clone();
52        let deps = [slot_count.untyped()];
53        self._slot_count
54            .replace_with(Property::computed(move || slot_count.get(), &deps));
55        let slot_count = ctx.projected_children_count.clone();
56        let names = self.names.clone();
57        let deps = [slot_count.untyped(), names.untyped()];
58        self._names_filled.replace_with(Property::computed(
59            move || {
60                let names = names.get();
61                let mut names_filled = vec![];
62                for i in 0..slot_count.get() {
63                    names_filled.push(
64                        names
65                            .get(i)
66                            .map(|s| s.as_str())
67                            .unwrap_or("[no name]")
68                            .to_owned(),
69                    );
70                }
71                names_filled
72            },
73            &deps,
74        ));
75    }
76
77    // Selects the tab segment under the click.
78    pub fn on_click(&mut self, ctx: &NodeContext, event: Event<Click>) {
79        let bounds = ctx.bounds_self.get();
80        let parts = self._slot_count.get();
81        let x = event.mouse.x;
82        let id = (x * parts as f64 / bounds.0) as usize;
83        self.selected.set(id);
84    }
85}