1use crate::{Font, TextAlignHorizontal, TextAlignVertical, TextStyle};
2use pax_message::{AnyCreatePatch, DropdownPatch, NativeInterrupt};
3use pax_runtime::api as pax_runtime_api;
4use pax_runtime::api::{use_RefCell, Layer, Property};
5use pax_runtime::{
6 BaseInstance, ExpandedNode, InstanceFlags, InstanceNode, InstantiationArgs, RuntimeContext,
7};
8use_RefCell!();
9use pax_runtime_api::*;
10
11use pax_engine::pax;
12use std::rc::Rc;
13
14use crate::common::{native_surface_opacity, patch_if_needed, patch_liquid_glass_if_needed};
15
16#[pax]
19#[engine_import_path("pax_engine")]
20#[primitive("pax_std::forms::dropdown::DropdownInstance")]
21#[custom(Default)]
22pub struct Dropdown {
23 pub stroke: Property<Stroke>,
25 pub options: Property<Vec<String>>,
27 pub selected_id: Property<u32>,
29 pub style: Property<TextStyle>,
31 pub background: Property<Color>,
33 pub corner_radius: Property<f64>,
35}
36
37impl Default for Dropdown {
38 fn default() -> Self {
39 Self {
40 options: Property::new(vec!["option 1".to_owned(), "option 2".to_owned()]),
41 selected_id: Property::new(0),
42 background: Property::new(Color::rgb(249.into(), 250.into(), 251.into())),
43 stroke: Property::new(Stroke {
44 color: Property::new(Color::rgb(209.into(), 213.into(), 219.into())),
45 width: Property::new(Size::Pixels(1.into())),
46 cap: Property::new(StrokeCap::default()),
47 join: Property::new(StrokeJoin::default()),
48 }),
49 corner_radius: Property::new(8.0.into()),
50 style: Property::new(TextStyle {
51 font: Property::new(Font::default()),
52 font_size: Property::new(Size::Pixels(Numeric::F64(14.0))),
53 fill: Property::new(Fill::Solid(Color::BLACK)),
54 underline: Property::new(false),
55 align_horizontal: Property::new(TextAlignHorizontal::Left),
56 align_multiline: Property::new(TextAlignHorizontal::Left),
57 align_vertical: Property::new(TextAlignVertical::Center),
58 }),
59 }
60 }
61}
62
63pub struct DropdownInstance {
65 base: BaseInstance,
66}
67
68impl InstanceNode for DropdownInstance {
69 fn instantiate(args: InstantiationArgs) -> Rc<Self>
70 where
71 Self: Sized,
72 {
73 Rc::new(Self {
74 base: BaseInstance::new(
75 args,
76 InstanceFlags {
77 invisible_to_slot: false,
78 invisible_to_raycasting: false,
79 layer: Layer::Native,
80 is_component: false,
81 is_slot: false,
82 },
83 ),
84 })
85 }
86
87 fn handle_mount(
88 self: Rc<Self>,
89 expanded_node: &Rc<ExpandedNode>,
90 context: &Rc<RuntimeContext>,
91 ) {
92 let id = expanded_node.id.clone();
94 context.enqueue_native_message(pax_message::NativeMessage::DropdownCreate(
95 AnyCreatePatch {
96 id: id.to_u32(),
97 parent_frame: expanded_node.parent_frame.get().map(|v| v.to_u32()),
98 render_layer_id: 0,
99 },
100 ));
101
102 let weak_self_ref = Rc::downgrade(&expanded_node);
104 let context = Rc::clone(context);
105 let last_patch = Rc::new(RefCell::new(DropdownPatch {
106 id: id.to_u32(),
107 ..Default::default()
108 }));
109
110 let deps: Vec<_> = borrow_mut!(expanded_node.properties_scope)
111 .values()
112 .cloned()
113 .map(|v| v.get_untyped_property().clone())
114 .chain([
115 expanded_node.transform_and_bounds.untyped(),
116 expanded_node.computed_opacity.untyped(),
117 expanded_node.occlusion.untyped(),
118 expanded_node.liquid_glass_scope.untyped(),
119 ])
120 .collect();
121 expanded_node
122 .changed_listener
123 .replace_with(Property::computed(
124 move || {
125 let Some(expanded_node) = weak_self_ref.upgrade() else {
126 return;
127 };
128 let id = expanded_node.id.clone();
129 let mut old_state = borrow_mut!(last_patch);
130
131 let mut patch = DropdownPatch {
132 id: id.to_u32(),
133 ..Default::default()
134 };
135 expanded_node.with_properties_unwrapped(|properties: &mut Dropdown| {
136 let computed_tab = expanded_node.transform_and_bounds.get();
137 let (width, height) = computed_tab.bounds;
138 let updates = [
139 patch_if_needed(&mut old_state.size_x, &mut patch.size_x, width),
140 patch_if_needed(&mut old_state.size_y, &mut patch.size_y, height),
141 patch_if_needed(
142 &mut old_state.parent_frame,
143 &mut patch.parent_frame,
144 expanded_node.parent_frame.get().map(|v| v.to_u32()),
145 ),
146 patch_if_needed(
147 &mut old_state.z_index,
148 &mut patch.z_index,
149 expanded_node.occlusion.get().z_index,
150 ),
151 patch_if_needed(
152 &mut old_state.transform,
153 &mut patch.transform,
154 computed_tab.transform.coeffs().to_vec(),
155 ),
156 patch_if_needed(
157 &mut old_state.style,
158 &mut patch.style,
159 (&properties.style.get()).into(),
160 ),
161 patch_if_needed(
162 &mut old_state.stroke_color,
163 &mut patch.stroke_color,
164 (&properties.stroke.get().color.get()).into(),
165 ),
166 patch_if_needed(
167 &mut old_state.stroke_width,
168 &mut patch.stroke_width,
169 properties.stroke.get().width.get().get_pixels(width),
170 ),
171 patch_if_needed(
172 &mut old_state.background,
173 &mut patch.background,
174 (&properties.background.get()).into(),
175 ),
176 patch_if_needed(
177 &mut old_state.selected_id,
178 &mut patch.selected_id,
179 properties.selected_id.get(),
180 ),
181 patch_if_needed(
182 &mut old_state.corner_radius,
183 &mut patch.corner_radius,
184 properties.corner_radius.get(),
185 ),
186 patch_if_needed(
187 &mut old_state.options,
188 &mut patch.options,
189 properties.options.get(),
190 ),
191 patch_if_needed(
192 &mut old_state.opacity,
193 &mut patch.opacity,
194 native_surface_opacity(&expanded_node, &context),
195 ),
196 patch_liquid_glass_if_needed(
197 &mut old_state.liquid_glass,
198 &mut patch.liquid_glass,
199 &expanded_node,
200 ),
201 ];
202 if updates.into_iter().any(|v| v == true) {
203 context.enqueue_native_message(
204 pax_message::NativeMessage::DropdownUpdate(patch),
205 );
206 }
207 });
208 ()
209 },
210 &deps,
211 ));
212 }
213
214 fn handle_unmount(&self, expanded_node: &Rc<ExpandedNode>, context: &Rc<RuntimeContext>) {
215 let id = expanded_node.id.clone();
216 expanded_node
217 .changed_listener
218 .replace_with(Property::default());
219 context.enqueue_native_message(pax_message::NativeMessage::DropdownDelete(id.to_u32()));
220 }
221
222 fn base(&self) -> &BaseInstance {
223 &self.base
224 }
225
226 fn resolve_debug(
227 &self,
228 f: &mut std::fmt::Formatter,
229 _expanded_node: Option<&ExpandedNode>,
230 ) -> std::fmt::Result {
231 f.debug_struct("Dropdown").finish_non_exhaustive()
232 }
233
234 fn property_requires_occlusion_recompute(&self, _property_name: &str) -> bool {
235 false
236 }
237
238 fn handle_native_interrupt(
239 &self,
240 expanded_node: &Rc<ExpandedNode>,
241 interrupt: &NativeInterrupt,
242 ) {
243 if let NativeInterrupt::FormDropdownChange(args) = interrupt {
244 expanded_node.with_properties_unwrapped(|props: &mut Dropdown| {
245 if props.selected_id.get() != args.selected_id {
246 props.selected_id.set(args.selected_id)
247 }
248 });
249 }
250 }
251}