pax_std/layout/
resizable.rs1#[allow(unused)]
2use crate::*;
3use pax_engine::api::*;
4use pax_engine::*;
5use std::cmp::Ordering;
6
7#[pax]
12#[engine_import_path("pax_engine")]
13#[inlined(
14 <Group
15 @mouse_down=on_mouse_down
16 @mouse_move=on_mouse_move
17 @mouse_up=on_mouse_up
18 >
19 for s in self._sections {
20 <Group
21 x={s.x}
22 y={s.y}
23 width={s.width}
24 height={s.height}
25 anchor_x=0%
26 anchor_y=0%
27 >
28 slot(s.i)
29 </Group>
30 }
31 <Rectangle fill=rgb(20, 20, 20)/>
32 </Group>
33
34 @settings {
35 @mount: on_mount
36 }
37)]
38pub struct Resizable {
39 pub dividers: Property<Vec<Size>>,
41 pub direction: Property<ResizableDirection>,
43
44 pub _sections: Property<Vec<Section>>,
46 pub _index_moving: Property<Option<usize>>,
48}
49
50#[pax]
52#[engine_import_path("pax_engine")]
53pub enum ResizableDirection {
54 Vertical,
56 #[default]
58 Horizontal,
59}
60
61#[pax]
63#[engine_import_path("pax_engine")]
64pub struct Section {
65 pub x: Size,
67 pub y: Size,
69 pub width: Size,
71 pub height: Size,
73 pub i: usize,
75}
76
77impl Resizable {
78 pub fn on_mount(&mut self, ctx: &NodeContext) {
80 let slot_count = ctx.projected_children_count.clone();
81 let dividers = self.dividers.clone();
82 let direction = self.direction.clone();
83 let deps = [
84 slot_count.untyped(),
85 dividers.untyped(),
86 direction.untyped(),
87 ];
88
89 self._sections.replace_with(Property::computed(
90 move || {
91 let divs = dividers.get();
92 if slot_count.get() != divs.len() + 1 {
93 log::warn!("slots in Resizable doesn't match number of Segments")
94 };
95
96 let mut positions = Vec::new();
97 positions.push(Size::ZERO());
98 positions.extend(divs);
99 positions.push(Size::default());
100 let mut sections = vec![];
101 for (i, w) in positions.windows(2).enumerate() {
102 let (pos_main, extent_main) = (w[0].clone(), w[1] - w[0]);
103 let (pos_off, extent_off) = (Size::ZERO(), Size::default());
104 let (x, width, y, height) = match direction.get() {
105 ResizableDirection::Vertical => {
106 (pos_off, extent_off, pos_main, extent_main)
107 }
108 ResizableDirection::Horizontal => {
109 (pos_main, extent_main, pos_off, extent_off)
110 }
111 };
112 sections.push(Section {
113 x,
114 width,
115 y,
116 height,
117 i,
118 })
119 }
120 sections
121 },
122 &deps,
123 ));
124 }
125
126 pub fn on_mouse_down(&mut self, ctx: &NodeContext, event: Event<MouseDown>) {
128 let bounds = ctx.bounds_self.get();
129
130 let (dim, axis) = match self.direction.get() {
131 ResizableDirection::Vertical => (event.mouse.y, Axis::Y),
132 ResizableDirection::Horizontal => (event.mouse.x, Axis::X),
133 };
134 let (closest_ind, distance) = self
135 .dividers
136 .get()
137 .iter()
138 .map(|d| (d.evaluate(bounds, axis) - dim).abs())
139 .enumerate()
140 .min_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(Ordering::Equal))
141 .unwrap_or_default();
142 if distance < 10.0 {
143 self._index_moving.set(Some(closest_ind));
144 }
145 }
146
147 pub fn on_mouse_move(&mut self, ctx: &NodeContext, event: Event<MouseMove>) {
149 let bounds = ctx.bounds_self.get();
150
151 let (dim, bound) = match self.direction.get() {
152 ResizableDirection::Vertical => (event.mouse.y, bounds.1),
153 ResizableDirection::Horizontal => (event.mouse.x, bounds.0),
154 };
155
156 if let Some(ind) = self._index_moving.get() {
157 self.dividers.update(|dividers| {
158 let divider = &mut dividers[ind];
159 *divider = match divider.clone() {
160 Size::Percent(_) => Size::Percent((100.0 * dim / bound).into()),
161 Size::Pixels(_) => Size::Pixels(dim.into()),
162 Size::Combined(_, perc) => {
163 Size::Combined((dim - perc.to_float() * bound / 100.0).into(), perc)
164 }
165 };
166 });
167 }
168 }
169
170 pub fn on_mouse_up(&mut self, _ctx: &NodeContext, _event: Event<MouseUp>) {
172 self._index_moving.set(None);
173 }
174}