1use crate::{Font, TextAlignHorizontal, TextAlignVertical, TextStyle};
2use pax_message::{AnyCreatePatch, TextboxPatch};
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_engine::pax;
10use pax_runtime_api::*;
11use std::rc::Rc;
12
13use crate::common::{native_surface_opacity, patch_if_needed, patch_liquid_glass_if_needed};
14
15#[pax]
18#[engine_import_path("pax_engine")]
19#[primitive("pax_std::forms::textbox::TextboxInstance")]
20#[custom(Default)]
21pub struct Textbox {
22 pub text: Property<String>,
24 pub background: Property<Color>,
26 pub placeholder: Property<String>,
28 pub stroke: Property<Stroke>,
30 pub corner_radius: Property<f64>,
32 pub style: Property<TextStyle>,
34 pub outline: Property<Stroke>,
36 pub focus_on_mount: Property<bool>,
38 pub multiline: Property<bool>,
40}
41
42impl Default for Textbox {
43 fn default() -> Self {
44 Self {
45 text: Default::default(),
46 background: Property::new(Color::rgb(249.into(), 250.into(), 251.into())),
47 stroke: Property::new(Stroke {
48 color: Property::new(Color::rgb(209.into(), 213.into(), 219.into())),
49 width: Property::new(Size::Pixels(1.into())),
50 cap: Property::new(StrokeCap::default()),
51 join: Property::new(StrokeJoin::default()),
52 }),
53 outline: Property::new(Stroke {
54 color: Property::new(Color::rgb(0.into(), 0.into(), 0.into())),
55 width: Property::new(Size::Pixels(1.into())),
56 cap: Property::new(StrokeCap::default()),
57 join: Property::new(StrokeJoin::default()),
58 }),
59 placeholder: Property::new("".into()),
60 corner_radius: Property::new(8.0.into()),
61 style: Property::new(TextStyle {
62 font: Property::new(Font::default()),
63 font_size: Property::new(Size::Pixels(Numeric::F64(14.0))),
64 fill: Property::new(Fill::Solid(Color::BLACK)),
65 underline: Property::new(false),
66 align_horizontal: Property::new(TextAlignHorizontal::Left),
67 align_multiline: Property::new(TextAlignHorizontal::Left),
68 align_vertical: Property::new(TextAlignVertical::Center),
69 }),
70 focus_on_mount: Property::new(false),
71 multiline: Property::new(false),
72 }
73 }
74}
75pub struct TextboxInstance {
77 base: BaseInstance,
78}
79
80impl InstanceNode for TextboxInstance {
81 fn instantiate(args: InstantiationArgs) -> Rc<Self>
82 where
83 Self: Sized,
84 {
85 Rc::new(Self {
86 base: BaseInstance::new(
87 args,
88 InstanceFlags {
89 invisible_to_slot: false,
90 invisible_to_raycasting: false,
91 layer: Layer::Native,
92 is_component: false,
93 is_slot: false,
94 },
95 ),
96 })
97 }
98
99 fn handle_mount(
100 self: Rc<Self>,
101 expanded_node: &Rc<ExpandedNode>,
102 context: &Rc<RuntimeContext>,
103 ) {
104 let id = expanded_node.id.clone();
106 context.enqueue_native_message(pax_message::NativeMessage::TextboxCreate(AnyCreatePatch {
107 id: id.to_u32(),
108 parent_frame: expanded_node.parent_frame.get().map(|v| v.to_u32()),
109 render_layer_id: 0,
110 }));
111
112 let weak_self_ref = Rc::downgrade(&expanded_node);
114 let context = Rc::clone(context);
115 let last_patch = Rc::new(RefCell::new(TextboxPatch {
116 id: id.to_u32(),
117 ..Default::default()
118 }));
119
120 let deps: Vec<_> = borrow_mut!(expanded_node.properties_scope)
121 .values()
122 .cloned()
123 .map(|v| v.get_untyped_property().clone())
124 .chain([
125 expanded_node.transform_and_bounds.untyped(),
126 expanded_node.computed_opacity.untyped(),
127 expanded_node.occlusion.untyped(),
128 expanded_node.liquid_glass_scope.untyped(),
129 ])
130 .collect();
131 expanded_node
132 .changed_listener
133 .replace_with(Property::computed(
134 move || {
135 let Some(expanded_node) = weak_self_ref.upgrade() else {
136 return;
137 };
138 let id = expanded_node.id.clone();
139 let mut old_state = borrow_mut!(last_patch);
140
141 let mut patch = TextboxPatch {
142 id: id.to_u32(),
143 ..Default::default()
144 };
145 expanded_node.with_properties_unwrapped(|properties: &mut Textbox| {
146 let computed_tab = expanded_node.transform_and_bounds.get();
147 let (width, height) = computed_tab.bounds;
148 let updates = [
149 patch_if_needed(
150 &mut old_state.text,
151 &mut patch.text,
152 properties.text.get(),
153 ),
154 patch_if_needed(&mut old_state.size_x, &mut patch.size_x, width),
155 patch_if_needed(&mut old_state.size_y, &mut patch.size_y, height),
156 patch_if_needed(
157 &mut old_state.parent_frame,
158 &mut patch.parent_frame,
159 expanded_node.parent_frame.get().map(|v| v.to_u32()),
160 ),
161 patch_if_needed(
162 &mut old_state.z_index,
163 &mut patch.z_index,
164 expanded_node.occlusion.get().z_index,
165 ),
166 patch_if_needed(
167 &mut old_state.transform,
168 &mut patch.transform,
169 computed_tab.transform.coeffs().to_vec(),
170 ),
171 patch_if_needed(
172 &mut old_state.style,
173 &mut patch.style,
174 (&properties.style.get()).into(),
175 ),
176 patch_if_needed(
177 &mut old_state.stroke_color,
178 &mut patch.stroke_color,
179 (&properties.stroke.get().color.get()).into(),
180 ),
181 patch_if_needed(
182 &mut old_state.stroke_width,
183 &mut patch.stroke_width,
184 properties.stroke.get().width.get().get_pixels(width),
185 ),
186 patch_if_needed(
187 &mut old_state.background,
188 &mut patch.background,
189 (&properties.background.get()).into(),
190 ),
191 patch_if_needed(
192 &mut old_state.corner_radius,
193 &mut patch.corner_radius,
194 properties.corner_radius.get(),
195 ),
196 patch_if_needed(
197 &mut old_state.focus_on_mount,
198 &mut patch.focus_on_mount,
199 properties.focus_on_mount.get(),
200 ),
201 patch_if_needed(
202 &mut old_state.is_text_area,
203 &mut patch.is_text_area,
204 properties.multiline.get(),
205 ),
206 patch_if_needed(
207 &mut old_state.placeholder,
208 &mut patch.placeholder,
209 properties.placeholder.get(),
210 ),
211 patch_if_needed(
212 &mut old_state.outline_color,
213 &mut patch.outline_color,
214 (&properties.outline.get().color.get()).into(),
215 ),
216 patch_if_needed(
217 &mut old_state.outline_width,
218 &mut patch.outline_width,
219 properties.outline.get().width.get().get_pixels(width),
220 ),
221 patch_if_needed(
222 &mut old_state.opacity,
223 &mut patch.opacity,
224 native_surface_opacity(&expanded_node, &context),
225 ),
226 patch_liquid_glass_if_needed(
227 &mut old_state.liquid_glass,
228 &mut patch.liquid_glass,
229 &expanded_node,
230 ),
231 ];
232 if updates.into_iter().any(|v| v == true) {
233 context.enqueue_native_message(
234 pax_message::NativeMessage::TextboxUpdate(patch),
235 );
236 }
237 });
238 ()
239 },
240 &deps,
241 ));
242 }
243
244 fn handle_unmount(&self, expanded_node: &Rc<ExpandedNode>, context: &Rc<RuntimeContext>) {
245 let id = expanded_node.id.clone();
246 expanded_node
247 .changed_listener
248 .replace_with(Property::default());
249 context.enqueue_native_message(pax_message::NativeMessage::TextboxDelete(id.to_u32()));
250 }
251
252 fn base(&self) -> &BaseInstance {
253 &self.base
254 }
255
256 fn property_requires_occlusion_recompute(&self, _property_name: &str) -> bool {
257 false
258 }
259
260 fn handle_native_interrupt(
261 &self,
262 expanded_node: &Rc<ExpandedNode>,
263 interrupt: &pax_message::NativeInterrupt,
264 ) {
265 if let pax_message::NativeInterrupt::FormTextboxInput(args) = interrupt {
266 expanded_node.with_properties_unwrapped(|textbox: &mut Textbox| {
267 textbox.text.set(args.text.clone());
268 })
269 } else {
270 log::warn!("textbox element was handed interrupt it doesn't use");
271 }
272 }
273
274 fn resolve_debug(
275 &self,
276 f: &mut std::fmt::Formatter,
277 _expanded_node: Option<&ExpandedNode>,
278 ) -> std::fmt::Result {
279 f.debug_struct("Textbox").finish_non_exhaustive()
280 }
281}