1use crate::*;
2
3impl PartialEq for AttributeValue {
10 fn eq(&self, other: &Self) -> bool {
11 match (self, other) {
12 (AttributeValue::Text(a_val), AttributeValue::Text(b_val)) => a_val == b_val,
13 (AttributeValue::Signal(a_sig), AttributeValue::Signal(b_sig)) => {
14 a_sig.get() == b_sig.get()
15 }
16 (AttributeValue::Signal(a_sig), AttributeValue::Text(b_val)) => a_sig.get() == *b_val,
17 (AttributeValue::Text(a_val), AttributeValue::Signal(b_sig)) => *a_val == b_sig.get(),
18 (AttributeValue::Event(_), AttributeValue::Event(_)) => true,
19 (AttributeValue::Css(a_css), AttributeValue::Css(b_css)) => {
20 a_css.get_name() == b_css.get_name()
21 }
22 (AttributeValue::Dynamic(a_dyn), AttributeValue::Dynamic(b_dyn)) => a_dyn == b_dyn,
23 _ => false,
24 }
25 }
26}
27
28impl PartialEq for AttributeEntry {
33 fn eq(&self, other: &Self) -> bool {
34 self.get_name() == other.get_name() && self.get_value() == other.get_value()
35 }
36}
37
38impl PartialEq for TextNode {
43 fn eq(&self, other: &Self) -> bool {
44 self.get_content() == other.get_content()
45 }
46}
47
48impl PartialEq for CssClass {
53 fn eq(&self, other: &Self) -> bool {
54 self.get_name() == other.get_name()
55 }
56}
57
58impl PartialEq for VirtualNode {
68 fn eq(&self, other: &Self) -> bool {
69 match (self, other) {
70 (VirtualNode::Text(a_text), VirtualNode::Text(b_text)) => a_text == b_text,
71 (
72 VirtualNode::Element {
73 tag: a_tag,
74 attributes: a_attrs,
75 children: a_children,
76 ..
77 },
78 VirtualNode::Element {
79 tag: b_tag,
80 attributes: b_attrs,
81 children: b_children,
82 ..
83 },
84 ) => {
85 a_tag == b_tag
86 && a_attrs.len() == b_attrs.len()
87 && a_attrs.iter().zip(b_attrs.iter()).all(|(a, b)| a == b)
88 && a_children.len() == b_children.len()
89 && a_children
90 .iter()
91 .zip(b_children.iter())
92 .all(|(a, b)| a == b)
93 }
94 (VirtualNode::Fragment(a_children), VirtualNode::Fragment(b_children)) => {
95 a_children.len() == b_children.len()
96 && a_children
97 .iter()
98 .zip(b_children.iter())
99 .all(|(a, b)| a == b)
100 }
101 (VirtualNode::Dynamic(_), VirtualNode::Dynamic(_)) => false,
102 (VirtualNode::Empty, VirtualNode::Empty) => true,
103 _ => false,
104 }
105 }
106}
107
108impl Attribute {
110 pub fn as_str(&self) -> String {
112 match self {
113 Attribute::AccessKey => "accesskey".to_string(),
114 Attribute::Action => "action".to_string(),
115 Attribute::Alt => "alt".to_string(),
116 Attribute::AriaLabel => "aria-label".to_string(),
117 Attribute::AutoComplete => "autocomplete".to_string(),
118 Attribute::AutoFocus => "autofocus".to_string(),
119 Attribute::Checked => "checked".to_string(),
120 Attribute::Class => "class".to_string(),
121 Attribute::Cols => "cols".to_string(),
122 Attribute::ContentEditable => "contenteditable".to_string(),
123 Attribute::Data(name) => format!("data-{}", name),
124 Attribute::Dir => "dir".to_string(),
125 Attribute::Disabled => "disabled".to_string(),
126 Attribute::Draggable => "draggable".to_string(),
127 Attribute::EncType => "enctype".to_string(),
128 Attribute::For => "for".to_string(),
129 Attribute::Form => "form".to_string(),
130 Attribute::Height => "height".to_string(),
131 Attribute::Hidden => "hidden".to_string(),
132 Attribute::Href => "href".to_string(),
133 Attribute::Id => "id".to_string(),
134 Attribute::Lang => "lang".to_string(),
135 Attribute::Max => "max".to_string(),
136 Attribute::MaxLength => "maxlength".to_string(),
137 Attribute::Method => "method".to_string(),
138 Attribute::Min => "min".to_string(),
139 Attribute::MinLength => "minlength".to_string(),
140 Attribute::Multiple => "multiple".to_string(),
141 Attribute::Name => "name".to_string(),
142 Attribute::Pattern => "pattern".to_string(),
143 Attribute::Placeholder => "placeholder".to_string(),
144 Attribute::ReadOnly => "readonly".to_string(),
145 Attribute::Required => "required".to_string(),
146 Attribute::Rows => "rows".to_string(),
147 Attribute::Selected => "selected".to_string(),
148 Attribute::Size => "size".to_string(),
149 Attribute::SpellCheck => "spellcheck".to_string(),
150 Attribute::Src => "src".to_string(),
151 Attribute::Step => "step".to_string(),
152 Attribute::Style => "style".to_string(),
153 Attribute::TabIndex => "tabindex".to_string(),
154 Attribute::Target => "target".to_string(),
155 Attribute::Title => "title".to_string(),
156 Attribute::Type => "type".to_string(),
157 Attribute::Value => "value".to_string(),
158 Attribute::Width => "width".to_string(),
159 Attribute::Other(name) => name.clone(),
160 }
161 }
162}
163
164impl Default for DynamicNode {
166 fn default() -> Self {
167 let node: DynamicNode = DynamicNode {
168 render_fn: Rc::new(RefCell::new(|| VirtualNode::Empty)),
169 hook_context: HookContext::default(),
170 };
171 node
172 }
173}
174
175impl Clone for DynamicNode {
177 fn clone(&self) -> Self {
178 DynamicNode {
179 render_fn: Rc::clone(self.get_render_fn()),
180 hook_context: self.hook_context,
181 }
182 }
183}
184
185impl AsNode for VirtualNode {
187 fn as_node(&self) -> Option<VirtualNode> {
188 Some(self.clone())
189 }
190}
191
192impl AsNode for &VirtualNode {
194 fn as_node(&self) -> Option<VirtualNode> {
195 Some((*self).clone())
196 }
197}
198
199impl AsNode for String {
201 fn as_node(&self) -> Option<VirtualNode> {
202 Some(VirtualNode::Text(TextNode::new(self.clone(), None)))
203 }
204}
205
206impl AsNode for &str {
208 fn as_node(&self) -> Option<VirtualNode> {
209 Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
210 }
211}
212
213impl AsNode for i32 {
215 fn as_node(&self) -> Option<VirtualNode> {
216 Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
217 }
218}
219
220impl AsNode for i64 {
222 fn as_node(&self) -> Option<VirtualNode> {
223 Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
224 }
225}
226
227impl AsNode for usize {
229 fn as_node(&self) -> Option<VirtualNode> {
230 Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
231 }
232}
233
234impl AsNode for f32 {
236 fn as_node(&self) -> Option<VirtualNode> {
237 Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
238 }
239}
240
241impl AsNode for f64 {
243 fn as_node(&self) -> Option<VirtualNode> {
244 Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
245 }
246}
247
248impl AsNode for bool {
250 fn as_node(&self) -> Option<VirtualNode> {
251 Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
252 }
253}
254
255impl<T> AsNode for Signal<T>
257where
258 T: Clone + PartialEq + std::fmt::Display + 'static,
259{
260 fn as_node(&self) -> Option<VirtualNode> {
261 Some(self.as_reactive_text())
262 }
263}
264
265impl IntoNode for VirtualNode {
267 fn into_node(self) -> VirtualNode {
268 self
269 }
270}
271
272impl<F> IntoNode for F
277where
278 F: FnMut() -> VirtualNode + 'static,
279{
280 fn into_node(self) -> VirtualNode {
281 VirtualNode::Dynamic(DynamicNode {
282 render_fn: Rc::new(RefCell::new(self)),
283 hook_context: crate::reactive::create_hook_context(),
284 })
285 }
286}
287
288impl IntoNode for String {
290 fn into_node(self) -> VirtualNode {
291 VirtualNode::Text(TextNode::new(self, None))
292 }
293}
294
295impl IntoNode for &str {
297 fn into_node(self) -> VirtualNode {
298 VirtualNode::Text(TextNode::new(self.to_string(), None))
299 }
300}
301
302impl IntoNode for i32 {
304 fn into_node(self) -> VirtualNode {
305 VirtualNode::Text(TextNode::new(self.to_string(), None))
306 }
307}
308
309impl IntoNode for usize {
311 fn into_node(self) -> VirtualNode {
312 VirtualNode::Text(TextNode::new(self.to_string(), None))
313 }
314}
315
316impl IntoNode for bool {
318 fn into_node(self) -> VirtualNode {
319 VirtualNode::Text(TextNode::new(self.to_string(), None))
320 }
321}
322
323impl<T> IntoNode for Signal<T>
325where
326 T: Clone + PartialEq + std::fmt::Display + 'static,
327{
328 fn into_node(self) -> VirtualNode {
329 self.as_reactive_text()
330 }
331}
332
333impl VirtualNode {
335 pub fn needs_patch(old: &VirtualNode, new: &VirtualNode) -> bool {
343 match (old, new) {
344 (VirtualNode::Text(old_text), VirtualNode::Text(new_text)) => {
345 old_text.get_content() != new_text.get_content()
346 }
347 (
348 VirtualNode::Element {
349 tag: old_tag,
350 attributes: old_attrs,
351 children: old_children,
352 key: _old_key,
353 },
354 VirtualNode::Element {
355 tag: new_tag,
356 attributes: new_attrs,
357 children: new_children,
358 key: _new_key,
359 },
360 ) => {
361 if old_tag != new_tag {
362 return true;
363 }
364 if old_attrs.len() != new_attrs.len() {
365 return true;
366 }
367 for (old_attr, new_attr) in old_attrs.iter().zip(new_attrs.iter()) {
368 if old_attr.get_name() != new_attr.get_name()
369 || old_attr.get_value() != new_attr.get_value()
370 {
371 return true;
372 }
373 }
374 if old_children.len() != new_children.len() {
375 return true;
376 }
377 for (old_child, new_child) in old_children.iter().zip(new_children.iter()) {
378 if Self::needs_patch(old_child, new_child) {
379 return true;
380 }
381 }
382 false
383 }
384 (VirtualNode::Fragment(old_children), VirtualNode::Fragment(new_children)) => {
385 if old_children.len() != new_children.len() {
386 return true;
387 }
388 for (old_child, new_child) in old_children.iter().zip(new_children.iter()) {
389 if Self::needs_patch(old_child, new_child) {
390 return true;
391 }
392 }
393 false
394 }
395 (VirtualNode::Dynamic(_), VirtualNode::Dynamic(_)) => true,
396 (VirtualNode::Empty, VirtualNode::Empty) => false,
397 _ => true,
398 }
399 }
400
401 pub fn get_element_node(tag_name: &str) -> Self {
403 VirtualNode::Element {
404 tag: Tag::Element(tag_name.to_string()),
405 attributes: Vec::new(),
406 children: Vec::new(),
407 key: None,
408 }
409 }
410
411 pub fn get_text_node(content: &str) -> Self {
413 VirtualNode::Text(TextNode::new(content.to_string(), None))
414 }
415
416 pub fn with_attribute(mut self, name: &str, value: AttributeValue) -> Self {
418 if let VirtualNode::Element {
419 ref mut attributes, ..
420 } = self
421 {
422 attributes.push(AttributeEntry::new(name.to_string(), value));
423 }
424 self
425 }
426
427 pub fn with_child(mut self, child: VirtualNode) -> Self {
429 if let VirtualNode::Element {
430 ref mut children, ..
431 } = self
432 {
433 children.push(child);
434 }
435 self
436 }
437
438 pub fn is_component(&self) -> bool {
440 matches!(
441 self,
442 VirtualNode::Element {
443 tag: Tag::Component(_),
444 ..
445 }
446 )
447 }
448
449 pub fn tag_name(&self) -> Option<String> {
451 match self {
452 VirtualNode::Element { tag, .. } => match tag {
453 Tag::Element(name) => Some(name.clone()),
454 Tag::Component(name) => Some(name.clone()),
455 },
456 _ => None,
457 }
458 }
459
460 pub fn try_get_prop(&self, name: &Attribute) -> Option<String> {
462 let name_str: String = name.as_str();
463 if let VirtualNode::Element { attributes, .. } = self {
464 for attr in attributes {
465 if attr.get_name() == &name_str {
466 match attr.get_value() {
467 AttributeValue::Text(value) => return Some(value.clone()),
468 AttributeValue::Signal(signal) => return Some(signal.get()),
469 _ => {}
470 }
471 }
472 }
473 }
474 None
475 }
476
477 pub fn try_get_signal_prop(&self, name: &Attribute) -> Option<Signal<String>> {
482 let name_str: String = name.as_str();
483 if let VirtualNode::Element { attributes, .. } = self {
484 for attr in attributes {
485 if attr.get_name() == &name_str
486 && let AttributeValue::Signal(signal) = attr.get_value()
487 {
488 return Some(*signal);
489 }
490 }
491 }
492 None
493 }
494
495 pub fn get_children(&self) -> Vec<VirtualNode> {
497 if let VirtualNode::Element { children, .. } = self {
498 children.clone()
499 } else {
500 Vec::new()
501 }
502 }
503
504 pub fn try_get_text(&self) -> Option<String> {
506 match self {
507 VirtualNode::Text(text_node) => Some(text_node.get_content().clone()),
508 VirtualNode::Element { children, .. } => {
509 children.first().and_then(VirtualNode::try_get_text)
510 }
511 _ => None,
512 }
513 }
514
515 pub fn try_get_event(
517 &self,
518 name: &NativeEventName,
519 ) -> Option<crate::event::NativeEventHandler> {
520 let name_str: String = name.as_str();
521 if let VirtualNode::Element { attributes, .. } = self {
522 for attr in attributes {
523 if attr.get_name() == &name_str
524 && let AttributeValue::Event(handler) = attr.get_value()
525 {
526 return Some(handler.clone());
527 }
528 }
529 }
530 None
531 }
532
533 pub fn try_get_callback(&self, name: &str) -> Option<crate::event::NativeEventHandler> {
535 if let VirtualNode::Element { attributes, .. } = self {
536 for attr in attributes {
537 if attr.get_name() == name
538 && let AttributeValue::Event(handler) = attr.get_value()
539 {
540 return Some(handler.clone());
541 }
542 }
543 }
544 None
545 }
546}
547
548impl<T> AsReactiveText for Signal<T>
550where
551 T: Clone + PartialEq + std::fmt::Display + 'static,
552{
553 fn as_reactive_text(&self) -> VirtualNode {
554 let signal: Signal<T> = *self;
555 let initial: String = signal.get().to_string();
556 let string_signal: Signal<String> = {
557 let boxed: Box<SignalInner<String>> = Box::new(SignalInner::new(initial.clone()));
558 Signal::from_inner(Box::leak(boxed) as *mut SignalInner<String>)
559 };
560 let source_signal: Signal<T> = *self;
561 let string_signal_clone: Signal<String> = string_signal;
562 source_signal.subscribe({
563 let source_signal: Signal<T> = source_signal;
564 move || {
565 let new_value: String = source_signal.get().to_string();
566 string_signal_clone.set(new_value);
567 }
568 });
569 VirtualNode::Text(TextNode::new(initial, Some(string_signal)))
570 }
571}
572
573impl Style {
575 pub fn property<N, V>(mut self, name: N, value: V) -> Self
580 where
581 N: AsRef<str>,
582 V: AsRef<str>,
583 {
584 self.get_mut_properties().push(StyleProperty::new(
585 name.as_ref().replace('_', "-"),
586 value.as_ref().to_string(),
587 ));
588 self
589 }
590
591 pub fn to_css_string(&self) -> String {
593 self.get_properties()
594 .iter()
595 .map(|p| format!("{}: {};", p.get_name(), p.get_value()))
596 .collect::<Vec<String>>()
597 .join(" ")
598 }
599}
600
601impl Default for Style {
603 fn default() -> Self {
604 Self::new(Vec::new())
605 }
606}
607
608impl CssClass {
610 pub fn new(name: String, style: String) -> Self {
614 let mut css_class: CssClass = CssClass::default();
615 css_class.set_name(name);
616 css_class.set_style(style);
617 css_class.inject_style();
618 css_class
619 }
620
621 pub fn inject_style(&self) {
628 #[cfg(target_arch = "wasm32")]
629 {
630 let style_id: &str = "euv-css-injected";
631 let document: web_sys::Document = web_sys::window()
632 .expect("no global window exists")
633 .document()
634 .expect("no document exists");
635 let style_element: web_sys::HtmlStyleElement = match document
636 .get_element_by_id(style_id)
637 {
638 Some(el) => el.dyn_into::<web_sys::HtmlStyleElement>().unwrap(),
639 None => {
640 let el: web_sys::HtmlStyleElement = document
641 .create_element("style")
642 .unwrap()
643 .dyn_into::<web_sys::HtmlStyleElement>()
644 .unwrap();
645 el.set_id(style_id);
646 let keyframes: &str = "@keyframes euv-spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } @keyframes euv-fade-in { from { opacity: 0; } to { opacity: 1; } } @keyframes euv-scale-in { from { transform: scale(0.9); opacity: 0; } to { transform: scale(1); opacity: 1; } } @keyframes euv-pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.2); } } @keyframes euv-slide-up { from { transform: translateY(100%); } to { transform: translateY(0); } } @keyframes euv-slide-left { from { transform: translateX(-100%); } to { transform: translateX(0); } } @keyframes euv-fade-in-up { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }";
647 let global: &str = "html, body, #app { height: 100%; margin: 0; padding: 0; overflow: hidden; } * { -webkit-tap-highlight-color: transparent; }";
648 let media_queries: &str = "@media (max-width: 767px) { .c_app_nav { display: none; } .c_app_main { padding: 20px 16px; max-width: 100%; } .c_page_title { font-size: 22px; } .c_page_subtitle { font-size: 14px; } .c_card { padding: 16px; margin: 12px 0; border-radius: 10px; } .c_card_title { font-size: 16px; } .c_form_grid { grid-template-columns: 1fr; } .c_browser_api_row { grid-template-columns: 1fr; } .c_modal_content { max-width: 100%; width: calc(100% - 32px); border-radius: 16px 16px 0 0; position: fixed; bottom: 0; left: 16px; height: 80vh; animation: euv-slide-up 0.25s ease; } .c_modal_overlay { align-items: flex-end; } .c_event_stats { gap: 12px; flex-wrap: wrap; } .c_event_section_row { gap: 12px; flex-wrap: wrap; } .c_event_section_col { min-width: 100%; } .c_counter_value { font-size: 20px; } .c_timer_value { font-size: 36px; } .c_not_found_code { font-size: 56px; } .c_not_found_container { padding: 40px 20px; } .c_list_input_row { flex-direction: column; } .c_vconsole_button { bottom: 16px; right: 16px; width: 44px; height: 44px; border-radius: 12px; } .c_tab_bar { flex-wrap: wrap; } .c_primary_button { padding: 10px 18px; font-size: 14px; } .c_badge { padding: 4px 10px; font-size: 11px; } .c_badge_outline { padding: 4px 10px; font-size: 11px; } .c_browser_info_grid { grid-template-columns: 1fr; } .c_anim_spin { font-size: 36px; } .c_anim_spin_stopped { font-size: 36px; } .c_anim_pulse { font-size: 36px; } .c_anim_pulse_stopped { font-size: 36px; } }";
649 el.set_inner_text(&format!("{} {} {}", global, keyframes, media_queries));
650 document.head().unwrap().append_child(&el).unwrap();
651 el
652 }
653 };
654 let existing_css: String = style_element.inner_text();
655 let class_rule: String = format!(".{} {{ {} }}", self.get_name(), self.get_style());
656 if !existing_css.contains(&class_rule) {
657 let new_css: String = if existing_css.is_empty() {
658 class_rule
659 } else {
660 format!("{}\n{}", existing_css, class_rule)
661 };
662 style_element.set_inner_text(&new_css);
663 }
664 }
665 }
666}
667
668impl std::fmt::Display for CssClass {
673 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
674 write!(f, "{}", self.get_name())
675 }
676}