Skip to main content

pax_std/forms/
tabs.rs

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