1pub(crate) mod focus;
30mod lazy;
31mod widgets;
32
33pub use crate::boundary::LaunchBuilder;
34pub use crate::brush::{Brush, Stop, brush};
35pub use crate::clipboard::{Clipboard, use_clipboard};
36pub use crate::drawing::{DrawCommand, DrawList, DrawListBuilder};
37pub use crate::font_features::FontFeature;
38pub use crate::message::{Message, show_message};
39pub use crate::runtime::{application, launch};
40pub use crate::schema::SemanticsRole as Role;
41pub use crate::schema::{
42 Alignment, Arrangement, BackgroundRepeat, ButtonKind, ButtonVariant, Color, ColorRole,
43 ColorScheme, CompactNavigation, ContentScale, DesignSystem, HorizontalAlignment, IconRole, Key,
44 MaterialRole, MessageDuration, MessageHorizontal, MessagePlacement, MessageVertical,
45 MotionRole, Paint, PopoverAlign, PopoverPlacement, SemanticsRole, ShapeRole, SheetPresentation,
46 Snap, SpaceRole, TextAlign, TextOverflow, Theme, TypeRole, VerticalAlignment,
47 WindowHeightClass, WindowSizeClass,
48};
49pub use crate::spans::TextSpans;
50pub use crate::theme::{ThemeHandle, current_theme};
51
52pub fn theme() -> ThemeHandle {
55 ThemeHandle::default()
56}
57pub use crate::asset::AssetId;
58pub use crate::window::{NodeSize, WindowSafeArea, WindowSize};
59pub use crate::{
60 Cursor, FileDrop, KeyEvent, PointerEvent, PointerKind, PointerKinds, RangeRequest, UserSelect,
61 asset, pointer_button,
62};
63pub use focus::{FocusHandle, use_focus_handle};
64pub use lazy::{LazyColumn, LazyGrid, LazyList, LazyListScope, LazyRow};
65pub use widgets::*;
66
67use crate::runtime::composer::{EventCallback, OwnedAttr};
68use crate::runtime::{group_guard, with_composer, with_composer_if_idle};
69use crate::schema::WidgetKind;
70
71pub const MAX_SHADOWS: usize = 4;
73
74#[derive(Clone, Copy, Debug, PartialEq)]
77pub struct Shadow {
78 pub x: f32,
79 pub y: f32,
80 pub blur: f32,
81 pub spread: f32,
82 pub paint: Paint,
83}
84
85impl Shadow {
86 pub const fn new(x: f32, y: f32, blur: f32, spread: f32, paint: Paint) -> Self {
87 Self {
88 x,
89 y,
90 blur,
91 spread,
92 paint,
93 }
94 }
95}
96
97pub const MAX_BACKGROUND_BRUSHES: usize = 4;
99
100#[derive(Clone, Copy, Debug, PartialEq)]
105pub struct BackgroundBrush {
106 pub x: f32,
107 pub y: f32,
108 pub width: f32,
109 pub height: f32,
110 pub paint: Paint,
111 pub repeat: BackgroundRepeat,
112 pub tile_width: f32,
114 pub tile_height: f32,
115}
116
117impl BackgroundBrush {
118 pub const fn new(x: f32, y: f32, width: f32, height: f32, paint: Paint) -> Self {
120 Self {
121 x,
122 y,
123 width,
124 height,
125 paint,
126 repeat: BackgroundRepeat::NoRepeat,
127 tile_width: width,
128 tile_height: height,
129 }
130 }
131
132 pub const fn repeat(mut self, repeat: BackgroundRepeat) -> Self {
135 self.repeat = repeat;
136 self
137 }
138
139 pub const fn step(mut self, width: f32, height: f32) -> Self {
142 self.tile_width = width;
143 self.tile_height = height;
144 self
145 }
146}
147
148#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
151pub struct SemanticsState {
152 pub checked: Option<bool>,
153 pub selected: Option<bool>,
154 pub expanded: Option<bool>,
155 pub disabled: Option<bool>,
156 pub focused: Option<bool>,
157 pub hidden: bool,
158 pub click_action: bool,
159 pub focus_action: bool,
160 pub has_popup: bool,
161}
162
163impl SemanticsState {
164 const NONE: Self = Self {
165 checked: None,
166 selected: None,
167 expanded: None,
168 disabled: None,
169 focused: None,
170 hidden: false,
171 click_action: false,
172 focus_action: false,
173 has_popup: false,
174 };
175
176 pub const fn checked(checked: bool) -> Self {
177 Self {
178 checked: Some(checked),
179 ..Self::NONE
180 }
181 }
182
183 pub const fn selected(selected: bool) -> Self {
184 Self {
185 selected: Some(selected),
186 ..Self::NONE
187 }
188 }
189
190 pub const fn expanded(expanded: bool) -> Self {
191 Self {
192 expanded: Some(expanded),
193 ..Self::NONE
194 }
195 }
196
197 pub const fn disabled(disabled: bool) -> Self {
198 Self {
199 disabled: Some(disabled),
200 ..Self::NONE
201 }
202 }
203
204 pub const fn focused(focused: bool) -> Self {
205 Self {
206 focused: Some(focused),
207 ..Self::NONE
208 }
209 }
210
211 pub const fn hidden(hidden: bool) -> Self {
212 Self {
213 hidden,
214 ..Self::NONE
215 }
216 }
217
218 pub const fn click_action(exposed: bool) -> Self {
219 Self {
220 click_action: exposed,
221 ..Self::NONE
222 }
223 }
224
225 pub const fn focus_action(exposed: bool) -> Self {
226 Self {
227 focus_action: exposed,
228 ..Self::NONE
229 }
230 }
231
232 pub const fn has_popup(has_popup: bool) -> Self {
233 Self {
234 has_popup,
235 ..Self::NONE
236 }
237 }
238
239 pub(crate) fn bits(self) -> u32 {
241 use crate::schema::*;
242 let tri = |value: Option<bool>| value.map_or(0_u32, |on| if on { 2 } else { 1 });
243 (tri(self.checked) << SEMANTICS_CHECKED_SHIFT)
244 | (tri(self.selected) << SEMANTICS_SELECTED_SHIFT)
245 | (tri(self.expanded) << SEMANTICS_EXPANDED_SHIFT)
246 | (tri(self.disabled) << SEMANTICS_DISABLED_SHIFT)
247 | (tri(self.focused) << SEMANTICS_FOCUSED_SHIFT)
248 | if self.hidden { SEMANTICS_HIDDEN_BIT } else { 0 }
249 | if self.click_action {
250 SEMANTICS_CLICK_ACTION_BIT
251 } else {
252 0
253 }
254 | if self.focus_action {
255 SEMANTICS_FOCUS_ACTION_BIT
256 } else {
257 0
258 }
259 | if self.has_popup {
260 SEMANTICS_HAS_POPUP_BIT
261 } else {
262 0
263 }
264 }
265}
266
267use std::sync::Arc;
268
269pub(crate) struct LabelCache {
271 entries: Vec<Arc<str>>,
272 capacity: usize,
273}
274
275pub(crate) const DEFAULT_LABEL_CACHE_CAPACITY: usize = 128;
276
277impl LabelCache {
278 pub(crate) fn new() -> Self {
279 Self::with_capacity(DEFAULT_LABEL_CACHE_CAPACITY)
280 }
281
282 pub(crate) fn with_capacity(capacity: usize) -> Self {
283 Self {
284 entries: Vec::with_capacity(capacity),
285 capacity,
286 }
287 }
288
289 pub(crate) fn intern(&mut self, label: &str) -> Arc<str> {
293 if let Some(pos) = self
294 .entries
295 .iter()
296 .position(|entry| entry.as_ref() == label)
297 {
298 let entry = self.entries.remove(pos);
299 self.entries.push(Arc::clone(&entry));
300 return entry;
301 }
302 let entry: Arc<str> = Arc::from(label);
303 if self.entries.len() >= self.capacity && self.capacity > 0 {
304 self.entries.remove(0);
305 }
306 if self.capacity > 0 {
307 self.entries.push(Arc::clone(&entry));
308 }
309 entry
310 }
311
312 #[cfg(test)]
313 pub(crate) fn len(&self) -> usize {
314 self.entries.len()
315 }
316}
317
318fn keep_label(label: &str) -> Option<Arc<str>> {
319 use std::cell::RefCell;
320
321 if label.is_empty() {
322 return None;
323 }
324
325 thread_local! {
326 static LAST: RefCell<Option<Arc<str>>> = const { RefCell::new(None) };
327 static CACHE: RefCell<LabelCache> = RefCell::new(LabelCache::new());
328 }
329
330 if let Some(last) = LAST.with(|cell| {
331 let borrowed = cell.borrow();
332 if let Some(last) = borrowed.as_ref() {
333 if last.as_ref() == label {
334 return Some(Arc::clone(last));
335 }
336 }
337 None
338 }) {
339 return Some(last);
340 }
341
342 let kept = CACHE.with(|cache| cache.borrow_mut().intern(label));
343 LAST.with(|cell| {
344 *cell.borrow_mut() = Some(Arc::clone(&kept));
345 });
346 Some(kept)
347}
348
349#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
351pub enum Side {
352 Top,
353 End,
355 Bottom,
356 Start,
358}
359
360#[derive(Clone, Debug, Default, PartialEq)]
373pub struct Modifier {
374 observe_size: Option<i64>,
375 observe_size_every_dp: bool,
376 motion: Option<MotionRole>,
377 material: Option<MaterialRole>,
378 weight: Option<f32>,
379 width: Option<f32>,
380 height: Option<f32>,
381 padding: Option<f32>,
382 padding_role: Option<SpaceRole>,
383 background: Option<Paint>,
384 shape_role: Option<ShapeRole>,
385 corner_radius: Option<f32>,
386 corner_radii: Option<[f32; 4]>,
387 corner_curve: Option<f32>,
388 min_touch_target: Option<f32>,
389 overlay: Option<Paint>,
390 border_width: Option<f32>,
391 border_color: Option<Paint>,
392 border_each: Option<[(f32, Paint); 4]>,
393 elevation: Option<f32>,
394 alpha: Option<f32>,
395 press_feedback: Option<(f32, f32)>,
396 semantics_role: Option<SemanticsRole>,
397 semantics_label: Option<Arc<str>>,
398 semantics_state: SemanticsState,
399 transform: Option<[f32; 8]>,
400 animated_alpha: Option<crate::Animated<f32>>,
401 animated_background: Option<crate::Animated<Paint>>,
402 animated_offset: Option<crate::Animated<(f32, f32)>>,
403 cursor: Option<crate::Cursor>,
404 user_select: Option<crate::UserSelect>,
405 clip: bool,
406 window_drag: Option<bool>,
407 offset: Option<(f32, f32)>,
408 shadows: [Option<Shadow>; MAX_SHADOWS],
409 background_brushes: [Option<BackgroundBrush>; MAX_BACKGROUND_BRUSHES],
410 fill_max_width: bool,
411 match_parent_size: bool,
412 fill_max_height: bool,
413 sticky: bool,
414 unbounded_width: bool,
415 size_in: Option<[f32; 4]>,
416 aspect_ratio: Option<(f32, bool)>,
417}
418
419#[allow(non_upper_case_globals)]
421pub const Modifier: Modifier = Modifier::new();
422
423impl Modifier {
424 pub const fn new() -> Self {
426 Self {
427 observe_size: None,
428 observe_size_every_dp: false,
429 motion: None,
430 material: None,
431 weight: None,
432 width: None,
433 height: None,
434 padding: None,
435 padding_role: None,
436 background: None,
437 shape_role: None,
438 corner_radius: None,
439 corner_radii: None,
440 corner_curve: None,
441 min_touch_target: None,
442 overlay: None,
443 border_width: None,
444 border_color: None,
445 border_each: None,
446 elevation: None,
447 alpha: None,
448 press_feedback: None,
449 semantics_role: None,
450 semantics_label: None,
451 semantics_state: SemanticsState::NONE,
452 transform: None,
453 animated_alpha: None,
454 animated_background: None,
455 animated_offset: None,
456 cursor: None,
457 user_select: None,
458 clip: false,
459 window_drag: None,
460 offset: None,
461 shadows: [None; MAX_SHADOWS],
462 background_brushes: [None; MAX_BACKGROUND_BRUSHES],
463 fill_max_width: false,
464 match_parent_size: false,
465 fill_max_height: false,
466 sticky: false,
467 unbounded_width: false,
468 size_in: None,
469 aspect_ratio: None,
470 }
471 }
472
473 pub const fn weight(mut self, weight: f32) -> Self {
475 self.weight = Some(weight);
476 self
477 }
478
479 pub const fn width(mut self, dp: f32) -> Self {
480 self.width = Some(dp);
481 self
482 }
483
484 pub const fn height(mut self, dp: f32) -> Self {
485 self.height = Some(dp);
486 self
487 }
488
489 pub const fn size(self, width: f32, height: f32) -> Self {
491 self.width(width).height(height)
492 }
493
494 pub const fn width_if(mut self, dp: Option<f32>) -> Self {
496 self.width = dp;
497 self
498 }
499
500 pub const fn match_parent_size(mut self) -> Self {
502 self.match_parent_size = true;
503 self
504 }
505
506 pub const fn sticky(mut self) -> Self {
509 self.sticky = true;
510 self
511 }
512
513 pub const fn fill_max_width(mut self) -> Self {
514 self.fill_max_width = true;
515 self
516 }
517
518 pub const fn fill_max_height(mut self) -> Self {
519 self.fill_max_height = true;
520 self
521 }
522
523 pub const fn fill_max_size(self) -> Self {
524 self.fill_max_width().fill_max_height()
525 }
526
527 pub const fn unbounded_width(mut self) -> Self {
536 self.unbounded_width = true;
537 self
538 }
539
540 pub const fn wrap_content_width(mut self, unbounded: bool) -> Self {
544 self.unbounded_width = unbounded;
545 self
546 }
547
548 pub const fn size_in(
552 mut self,
553 min_width: f32,
554 max_width: f32,
555 min_height: f32,
556 max_height: f32,
557 ) -> Self {
558 self.size_in = Some([min_width, max_width, min_height, max_height]);
559 self
560 }
561
562 pub const fn width_in(mut self, min: f32, max: f32) -> Self {
564 let (min_h, max_h) = match self.size_in {
565 Some([_, _, min_h, max_h]) => (min_h, max_h),
566 None => (f32::NAN, f32::NAN),
567 };
568 self.size_in = Some([min, max, min_h, max_h]);
569 self
570 }
571
572 pub const fn height_in(mut self, min: f32, max: f32) -> Self {
574 let (min_w, max_w) = match self.size_in {
575 Some([min_w, max_w, _, _]) => (min_w, max_w),
576 None => (f32::NAN, f32::NAN),
577 };
578 self.size_in = Some([min_w, max_w, min, max]);
579 self
580 }
581
582 pub const fn max_width(self, max: f32) -> Self {
584 self.width_in(f32::NAN, max)
585 }
586
587 pub const fn max_height(self, max: f32) -> Self {
589 self.height_in(f32::NAN, max)
590 }
591
592 pub const fn min_width(self, min: f32) -> Self {
594 self.width_in(min, f32::NAN)
595 }
596
597 pub const fn min_height(self, min: f32) -> Self {
599 self.height_in(min, f32::NAN)
600 }
601
602 pub const fn aspect_ratio(mut self, ratio: f32) -> Self {
604 self.aspect_ratio = Some((ratio, false));
605 self
606 }
607
608 pub const fn aspect_ratio_match_height(
610 mut self,
611 ratio: f32,
612 match_height_constraints_first: bool,
613 ) -> Self {
614 self.aspect_ratio = Some((ratio, match_height_constraints_first));
615 self
616 }
617
618 pub const fn fill_max_width_if(mut self, fill: bool) -> Self {
620 self.fill_max_width = fill;
621 self
622 }
623
624 pub const fn padding(mut self, dp: f32) -> Self {
626 self.padding = Some(dp);
627 self
628 }
629
630 pub const fn padding_role(mut self, role: SpaceRole) -> Self {
632 self.padding_role = Some(role);
633 self
634 }
635
636 pub const fn padding_role_if(mut self, role: Option<SpaceRole>) -> Self {
638 self.padding_role = role;
639 self
640 }
641
642 pub const fn background(mut self, paint: Paint) -> Self {
643 self.background = Some(paint);
644 self
645 }
646
647 pub const fn background_if(mut self, paint: Option<Paint>) -> Self {
649 self.background = paint;
650 self
651 }
652
653 pub const fn overlay(mut self, paint: Paint) -> Self {
659 self.overlay = Some(paint);
660 self
661 }
662
663 pub const fn overlay_if(mut self, paint: Option<Paint>) -> Self {
665 self.overlay = paint;
666 self
667 }
668
669 pub const fn shape_role(mut self, role: ShapeRole) -> Self {
672 self.shape_role = Some(role);
673 self
674 }
675
676 pub const fn corner_radius(mut self, dp: f32) -> Self {
678 self.corner_radius = Some(dp);
679 self.corner_radii = None;
680 self
681 }
682
683 pub const fn corner_radii(mut self, radii: [f32; 4]) -> Self {
686 self.corner_radii = Some(radii);
687 self.corner_radius = None;
688 self
689 }
690
691 pub const fn clip(mut self, clip: bool) -> Self {
693 self.clip = clip;
694 self
695 }
696
697 pub const fn window_drag(mut self, enabled: bool) -> Self {
699 self.window_drag = Some(enabled);
700 self
701 }
702
703 pub const fn border(mut self, width: f32, paint: Paint) -> Self {
706 self.border_width = Some(width);
707 self.border_color = Some(paint);
708 self.border_each = None;
709 self
710 }
711
712 pub const fn border_each(
719 mut self,
720 top: (f32, Paint),
721 right: (f32, Paint),
722 bottom: (f32, Paint),
723 left: (f32, Paint),
724 ) -> Self {
725 self.border_each = Some([top, right, bottom, left]);
726 self.border_width = None;
727 self.border_color = None;
728 self
729 }
730
731 pub const fn border_side(mut self, side: Side, width: f32, paint: Paint) -> Self {
740 let mut sides = match self.border_each {
741 Some(sides) => sides,
742 None => [(0.0, paint); 4],
743 };
744 let index = match side {
745 Side::Top => 0,
746 Side::End => 1,
747 Side::Bottom => 2,
748 Side::Start => 3,
749 };
750 sides[index] = (width, paint);
751 self.border_each = Some(sides);
752 self.border_width = None;
753 self.border_color = None;
754 self
755 }
756
757 pub const fn elevation(mut self, dp: f32) -> Self {
759 self.elevation = Some(dp);
760 self
761 }
762
763 pub const fn observe_size(mut self, token: i64) -> Self {
770 self.observe_size = Some(token);
771 self.observe_size_every_dp = false;
772 self
773 }
774
775 pub const fn observe_size_every_dp(mut self, token: i64) -> Self {
779 self.observe_size = Some(token);
780 self.observe_size_every_dp = true;
781 self
782 }
783
784 pub const fn motion(mut self, role: MotionRole) -> Self {
786 self.motion = Some(role);
787 self
788 }
789
790 pub const fn material(mut self, role: MaterialRole) -> Self {
792 self.material = Some(role);
793 self
794 }
795
796 pub const fn offset(mut self, x: f32, y: f32) -> Self {
798 self.offset = Some((x, y));
799 self
800 }
801
802 pub const fn shadow(mut self, shadow: Shadow) -> Self {
805 let mut index = 0;
806 while index < MAX_SHADOWS {
807 if self.shadows[index].is_none() {
808 self.shadows[index] = Some(shadow);
809 break;
810 }
811 index += 1;
812 }
813 self
814 }
815
816 pub fn shadows(mut self, shadows: impl IntoIterator<Item = Shadow>) -> Self {
818 for shadow in shadows {
819 self = self.shadow(shadow);
820 }
821 self
822 }
823
824 pub const fn background_brush(mut self, layer: BackgroundBrush) -> Self {
828 let mut index = 0;
829 while index < MAX_BACKGROUND_BRUSHES {
830 if self.background_brushes[index].is_none() {
831 self.background_brushes[index] = Some(layer);
832 break;
833 }
834 index += 1;
835 }
836 self
837 }
838
839 pub fn background_brushes(mut self, layers: impl IntoIterator<Item = BackgroundBrush>) -> Self {
842 for layer in layers {
843 self = self.background_brush(layer);
844 }
845 self
846 }
847
848 pub const fn alpha(mut self, alpha: f32) -> Self {
850 self.alpha = Some(alpha);
851 self
852 }
853
854 pub fn corner_curve(mut self, smoothing: f32) -> Self {
861 self.corner_curve = Some(crate::schema::corner_smoothing(smoothing));
862 self
863 }
864
865 pub const fn min_touch_target(mut self, dp: f32) -> Self {
871 self.min_touch_target = Some(dp);
872 self
873 }
874
875 pub const fn press_feedback(mut self, scale: f32, tint_alpha: f32) -> Self {
877 self.press_feedback = Some((scale, tint_alpha));
878 self
879 }
880
881 pub fn label(mut self, label: &str) -> Self {
884 self.semantics_label = keep_label(label);
885 self
886 }
887
888 pub const fn role(mut self, role: Role) -> Self {
891 self.semantics_role = Some(role);
892 self
893 }
894
895 pub const fn checked(mut self, checked: bool) -> Self {
897 self.semantics_state.checked = Some(checked);
898 self
899 }
900
901 pub const fn selected(mut self, selected: bool) -> Self {
903 self.semantics_state.selected = Some(selected);
904 self
905 }
906
907 pub const fn expanded(mut self, expanded: bool) -> Self {
909 self.semantics_state.expanded = Some(expanded);
910 self
911 }
912
913 pub const fn disabled(mut self, disabled: bool) -> Self {
915 self.semantics_state.disabled = Some(disabled);
916 self
917 }
918
919 pub const fn focused(mut self, focused: bool) -> Self {
921 self.semantics_state.focused = Some(focused);
922 self
923 }
924
925 pub const fn hidden(mut self, hidden: bool) -> Self {
927 self.semantics_state.hidden = hidden;
928 self
929 }
930
931 pub const fn click_action(mut self, exposed: bool) -> Self {
933 self.semantics_state.click_action = exposed;
934 self
935 }
936
937 pub const fn focus_action(mut self, exposed: bool) -> Self {
939 self.semantics_state.focus_action = exposed;
940 self
941 }
942
943 pub const fn has_popup(mut self, has_popup: bool) -> Self {
945 self.semantics_state.has_popup = has_popup;
946 self
947 }
948
949 pub fn semantics(self, role: Role, label: &str, state: SemanticsState) -> Self {
951 let mut named = self.role(role).label(label);
952 named.semantics_state = state;
953 named
954 }
955
956 pub fn graphics_layer(mut self, layer: crate::GraphicsLayer) -> Self {
960 if let crate::schema::Modifier::Transform {
961 a,
962 b,
963 c,
964 d,
965 e,
966 f,
967 origin_x,
968 origin_y,
969 } = layer.transform()
970 {
971 self.transform = Some([a, b, c, d, e, f, origin_x, origin_y]);
972 }
973 self.alpha = Some(layer.alpha);
974 self
975 }
976
977 pub const fn animated_alpha(mut self, value: crate::Animated<f32>) -> Self {
980 self.animated_alpha = Some(value);
981 self
982 }
983
984 pub const fn animated_background(mut self, value: crate::Animated<Paint>) -> Self {
987 self.animated_background = Some(value);
988 self
989 }
990
991 pub const fn animated_offset(mut self, value: crate::Animated<(f32, f32)>) -> Self {
994 self.animated_offset = Some(value);
995 self
996 }
997
998 pub const fn cursor(mut self, cursor: crate::Cursor) -> Self {
1002 self.cursor = Some(cursor);
1003 self
1004 }
1005
1006 pub const fn user_select(mut self, user_select: crate::UserSelect) -> Self {
1009 self.user_select = Some(user_select);
1010 self
1011 }
1012}
1013
1014#[derive(Clone, Copy, PartialEq, Eq)]
1017pub(crate) enum Mods {
1018 Container,
1020 Standard,
1022 Chip,
1024 Scaffold,
1026 None,
1028}
1029
1030pub(crate) type Attrs = Vec<(&'static str, OwnedAttr)>;
1031
1032fn float(value: Option<f32>) -> OwnedAttr {
1033 value.map_or(OwnedAttr::None, |value| OwnedAttr::Float(f64::from(value)))
1034}
1035
1036fn role(value: Option<impl Into<u16>>) -> OwnedAttr {
1037 value.map_or(OwnedAttr::None, |value| {
1038 OwnedAttr::Int(i64::from(value.into()))
1039 })
1040}
1041
1042fn paint(value: Option<Paint>) -> OwnedAttr {
1043 value.map_or(OwnedAttr::None, |paint| {
1044 OwnedAttr::Int(paint.to_bits() as i64)
1045 })
1046}
1047
1048pub(crate) fn role_or_zero(value: Option<impl Into<u16>>) -> OwnedAttr {
1050 OwnedAttr::Int(value.map_or(0, |value| i64::from(value.into())))
1051}
1052
1053pub(crate) fn dp_or_zero(value: Option<f32>) -> OwnedAttr {
1055 OwnedAttr::Float(f64::from(value.unwrap_or(0.0)))
1056}
1057
1058pub(crate) fn paint_or_zero(value: Option<Paint>) -> OwnedAttr {
1060 OwnedAttr::Int(value.map_or(0, |paint| paint.to_bits() as i64))
1061}
1062
1063pub(crate) fn optional_text(value: Option<String>) -> OwnedAttr {
1064 value.map_or(OwnedAttr::None, OwnedAttr::Text)
1065}
1066
1067fn semantics_bits(modifier: &Modifier) -> Option<i64> {
1070 let state = modifier.semantics_state;
1071 if modifier.semantics_role.is_none() && state == SemanticsState::NONE {
1072 return None;
1073 }
1074 let role = i64::from(u16::from(
1075 modifier.semantics_role.unwrap_or(SemanticsRole::None),
1076 ));
1077 Some(role | (i64::from(state.bits()) << 16))
1078}
1079
1080pub(crate) fn push_modifiers(modifier: &Modifier, mods: Mods, out: &mut Attrs) {
1082 let container = mods == Mods::Container;
1083 let standard = container || mods == Mods::Standard;
1084 let chip = standard || mods == Mods::Chip;
1085 let scaffold = mods == Mods::Scaffold;
1086 let mut push = |always: bool, name: &'static str, value: OwnedAttr| {
1087 let set = !matches!(value, OwnedAttr::None | OwnedAttr::Bool(false));
1088 if always || set {
1089 out.push((name, value));
1090 }
1091 };
1092 let (plain, every_dp) = match modifier.observe_size {
1095 Some(token) if modifier.observe_size_every_dp => (OwnedAttr::None, OwnedAttr::Int(token)),
1096 Some(token) => (OwnedAttr::Int(token), OwnedAttr::None),
1097 None => (OwnedAttr::None, OwnedAttr::None),
1098 };
1099 push(container, "observe_size", plain);
1100 push(container, "observe_size_every_dp", every_dp);
1101 push(container, "motion", role(modifier.motion));
1102 push(container || scaffold, "material", role(modifier.material));
1103 push(chip, "weight", float(modifier.weight));
1104 push(chip, "width", float(modifier.width));
1105 push(chip, "height", float(modifier.height));
1106 push(chip, "padding", float(modifier.padding));
1107 push(chip, "padding_role", role(modifier.padding_role));
1108 push(
1110 standard || scaffold,
1111 "background",
1112 paint(if modifier.animated_background.is_some() {
1113 None
1114 } else {
1115 modifier.background
1116 }),
1117 );
1118 push(standard, "shape_role", role(modifier.shape_role));
1119 push(standard, "corner_radius", float(modifier.corner_radius));
1120 push(
1121 false,
1122 "corner_radii",
1123 modifier.corner_radii.map_or(OwnedAttr::None, |values| {
1124 OwnedAttr::Bytes(
1125 values
1126 .iter()
1127 .flat_map(|value| value.to_le_bytes())
1128 .collect(),
1129 )
1130 }),
1131 );
1132 push(false, "corner_curve", float(modifier.corner_curve));
1133 push(false, "min_touch_target", float(modifier.min_touch_target));
1134 push(false, "overlay", paint(modifier.overlay));
1135 push(false, "clip", OwnedAttr::Bool(modifier.clip));
1136 push(
1137 true,
1138 "window_drag",
1139 modifier
1140 .window_drag
1141 .map_or(OwnedAttr::None, OwnedAttr::Bool),
1142 );
1143 push(standard, "border_width", float(modifier.border_width));
1144 push(standard, "border_color", paint(modifier.border_color));
1145 push(
1149 false,
1150 "border_each",
1151 modifier.border_each.map_or(OwnedAttr::None, |sides| {
1152 let mut bytes = Vec::with_capacity(48);
1153 for (width, _) in sides {
1154 bytes.extend_from_slice(&width.to_le_bytes());
1155 }
1156 for (_, paint) in sides {
1157 bytes.extend_from_slice(&paint.to_bits().to_le_bytes());
1158 }
1159 OwnedAttr::Bytes(bytes)
1160 }),
1161 );
1162 push(standard, "elevation", float(modifier.elevation));
1163 push(
1164 false,
1165 "match_parent_size",
1166 OwnedAttr::Bool(modifier.match_parent_size),
1167 );
1168 push(false, "sticky", OwnedAttr::Bool(modifier.sticky));
1169 push(
1170 chip,
1171 "fill_max_width",
1172 OwnedAttr::Bool(modifier.fill_max_width),
1173 );
1174 push(
1175 standard,
1176 "fill_max_height",
1177 OwnedAttr::Bool(modifier.fill_max_height),
1178 );
1179 push(
1180 false,
1181 "unbounded_width",
1182 OwnedAttr::Bool(modifier.unbounded_width),
1183 );
1184 push(
1187 false,
1188 "press_feedback",
1189 modifier
1190 .press_feedback
1191 .map_or(OwnedAttr::None, |(scale, alpha)| {
1192 OwnedAttr::Bytes([scale.to_le_bytes(), alpha.to_le_bytes()].concat())
1193 }),
1194 );
1195 push(
1198 false,
1199 "semantics",
1200 semantics_bits(modifier).map_or(OwnedAttr::None, OwnedAttr::Int),
1201 );
1202 push(
1203 false,
1204 "accessible_name",
1205 modifier
1206 .semantics_label
1207 .as_ref()
1208 .map_or(OwnedAttr::None, |label| OwnedAttr::Label(Arc::clone(label))),
1209 );
1210 push(
1211 false,
1212 "alpha",
1213 float(if modifier.animated_alpha.is_some() {
1214 None
1215 } else {
1216 modifier.alpha
1217 }),
1218 );
1219 push(
1220 false,
1221 "transform",
1222 match (modifier.animated_offset, modifier.transform) {
1223 (None, Some(values)) => OwnedAttr::Bytes(
1224 values
1225 .iter()
1226 .flat_map(|value| value.to_le_bytes())
1227 .collect(),
1228 ),
1229 _ => OwnedAttr::None,
1230 },
1231 );
1232 push(
1234 false,
1235 "cursor",
1236 match modifier.cursor {
1237 Some(crate::Cursor::Auto) | None => OwnedAttr::None,
1238 Some(cursor) => OwnedAttr::Int(i64::from(u16::from(cursor))),
1239 },
1240 );
1241 push(
1242 false,
1243 "user_select",
1244 match modifier.user_select {
1245 Some(crate::UserSelect::Auto) | None => OwnedAttr::None,
1246 Some(select) => OwnedAttr::Int(i64::from(u16::from(select))),
1247 },
1248 );
1249 push(
1251 false,
1252 "offset",
1253 modifier.offset.map_or(OwnedAttr::None, |(x, y)| {
1254 let mut bytes = Vec::with_capacity(8);
1255 bytes.extend_from_slice(&x.to_le_bytes());
1256 bytes.extend_from_slice(&y.to_le_bytes());
1257 OwnedAttr::Bytes(bytes)
1258 }),
1259 );
1260 push(
1261 false,
1262 "shadows",
1263 if modifier.shadows[0].is_none() {
1264 OwnedAttr::None
1265 } else {
1266 let mut bytes = Vec::new();
1267 for shadow in modifier.shadows.iter().flatten() {
1268 for value in [shadow.x, shadow.y, shadow.blur, shadow.spread] {
1269 bytes.extend_from_slice(&value.to_le_bytes());
1270 }
1271 bytes.extend_from_slice(&shadow.paint.to_bits().to_le_bytes());
1272 }
1273 OwnedAttr::Bytes(bytes)
1274 },
1275 );
1276 push(
1277 false,
1278 "background_brushes",
1279 if modifier.background_brushes[0].is_none() {
1280 OwnedAttr::None
1281 } else {
1282 let mut bytes = Vec::new();
1283 for layer in modifier.background_brushes.iter().flatten() {
1284 for value in [layer.x, layer.y, layer.width, layer.height] {
1285 bytes.extend_from_slice(&value.to_le_bytes());
1286 }
1287 bytes.extend_from_slice(&layer.paint.to_bits().to_le_bytes());
1288 bytes.extend_from_slice(&u32::from(u16::from(layer.repeat)).to_le_bytes());
1289 bytes.extend_from_slice(&layer.tile_width.to_le_bytes());
1290 bytes.extend_from_slice(&layer.tile_height.to_le_bytes());
1291 bytes.extend_from_slice(&[0; 4]);
1294 }
1295 OwnedAttr::Bytes(bytes)
1296 },
1297 );
1298 push(
1299 false,
1300 "animated_alpha",
1301 modifier.animated_alpha.map_or(OwnedAttr::None, |value| {
1302 OwnedAttr::Bytes(crate::animation::animated_bytes(
1303 &[value.target.to_bits()],
1304 &value.spec,
1305 ))
1306 }),
1307 );
1308 push(
1309 false,
1310 "animated_background",
1311 modifier
1312 .animated_background
1313 .map_or(OwnedAttr::None, |value| {
1314 let bits = value.target.to_bits();
1315 OwnedAttr::Bytes(crate::animation::animated_bytes(
1316 &[bits as u32, (bits >> 32) as u32],
1317 &value.spec,
1318 ))
1319 }),
1320 );
1321 push(
1322 false,
1323 "animated_offset",
1324 modifier.animated_offset.map_or(OwnedAttr::None, |value| {
1325 OwnedAttr::Bytes(crate::animation::animated_bytes(
1326 &[value.target.0.to_bits(), value.target.1.to_bits()],
1327 &value.spec,
1328 ))
1329 }),
1330 );
1331 push(
1332 false,
1333 "size_in",
1334 modifier
1335 .size_in
1336 .map_or(OwnedAttr::None, |[min_w, max_w, min_h, max_h]| {
1337 let mut bytes = Vec::with_capacity(16);
1338 bytes.extend_from_slice(&min_w.to_le_bytes());
1339 bytes.extend_from_slice(&max_w.to_le_bytes());
1340 bytes.extend_from_slice(&min_h.to_le_bytes());
1341 bytes.extend_from_slice(&max_h.to_le_bytes());
1342 OwnedAttr::Bytes(bytes)
1343 }),
1344 );
1345 push(
1346 false,
1347 "aspect_ratio",
1348 modifier
1349 .aspect_ratio
1350 .map_or(OwnedAttr::None, |(ratio, match_height)| {
1351 let mut bytes = Vec::with_capacity(5);
1352 bytes.extend_from_slice(&ratio.to_le_bytes());
1353 bytes.push(u8::from(match_height));
1354 OwnedAttr::Bytes(bytes)
1355 }),
1356 );
1357}
1358
1359pub(crate) fn emit(
1365 key: u64,
1366 widget: WidgetKind,
1367 fill: impl FnOnce(&mut Attrs),
1368 listeners: Vec<(&'static str, EventCallback)>,
1369 content: impl FnOnce(),
1370) {
1371 let _group = group_guard(key);
1372 emit_node(widget, fill, listeners, content);
1373}
1374
1375pub(crate) fn emit_node(
1378 widget: WidgetKind,
1379 fill: impl FnOnce(&mut Attrs),
1380 listeners: Vec<(&'static str, EventCallback)>,
1381 content: impl FnOnce(),
1382) {
1383 let node = with_composer_if_idle(|composer| {
1384 if !composer.composing() {
1385 return None;
1386 }
1387 let mut attrs = composer.take_attrs();
1388 fill(&mut attrs);
1389 Some(composer.update_node(widget, attrs, listeners))
1390 })
1391 .flatten();
1392 let Some(node) = node else {
1393 return;
1394 };
1395 with_composer(|composer| composer.push_node_context(node));
1396 let _context = NodeContextGuard;
1397 content();
1398}
1399
1400struct NodeContextGuard;
1402
1403impl Drop for NodeContextGuard {
1404 fn drop(&mut self) {
1405 with_composer_if_idle(|composer| composer.pop_node_context());
1406 }
1407}
1408
1409pub(crate) const fn widget_key(name: &str) -> u64 {
1411 crate::runtime::composer::call_site(name, 0)
1412}
1413
1414#[cfg(test)]
1415mod fr19_9_tests {
1416 use super::*;
1417
1418 #[test]
1419 fn fr19_9_window_drag_builder_preserves_false() {
1420 for enabled in [false, true] {
1421 let mut attrs = Vec::new();
1422 push_modifiers(
1423 &Modifier::new().window_drag(enabled),
1424 Mods::Standard,
1425 &mut attrs,
1426 );
1427 assert!(attrs.iter().any(|(name, value)| *name == "window_drag"
1428 && matches!(value, OwnedAttr::Bool(value) if *value == enabled)));
1429 }
1430 }
1431}
1432
1433#[cfg(test)]
1434mod fr68_label_tests {
1435 use super::*;
1436
1437 #[test]
1438 fn fr68_repeated_labels_do_not_grow_the_cache() {
1439 let mut cache = LabelCache::new();
1440 let first = cache.intern("Refresh");
1441 for _ in 0..10_000 {
1442 let again = cache.intern("Refresh");
1443 assert!(Arc::ptr_eq(&first, &again));
1444 }
1445 assert_eq!(cache.len(), 1);
1446 }
1447
1448 #[test]
1449 fn fr68_cache_evicts_old_entries_without_dropping_new_labels() {
1450 let mut cache = LabelCache::with_capacity(32);
1451 for n in 0..100 {
1452 let label = cache.intern(&format!("label {n}"));
1453 assert_eq!(label.as_ref(), format!("label {n}"));
1454 }
1455 assert_eq!(cache.len(), 32);
1456 let re_interned = cache.intern("label 0");
1458 assert_eq!(re_interned.as_ref(), "label 0");
1459 assert_eq!(cache.len(), 32);
1460 }
1461}