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