1use std::{
9 fmt,
10 hash::{Hash, Hasher},
11 rc::Rc,
12};
13
14use cranpose_core::{ProvidedValue, hash::default};
15use smallvec::SmallVec;
16
17mod alignment;
18mod background;
19mod blur;
20mod border;
21mod chain;
22mod clickable;
23mod coordinator_geometry;
24mod drag_and_drop;
25mod draw_cache;
26mod fill;
27mod focus;
28mod focus_ring;
29mod graphics_layer;
30mod key_input;
31mod local;
32mod minimum_interactive;
33mod offset;
34mod padding;
35mod pointer_icon;
36pub(crate) mod pointer_input;
37mod rotary_input;
38mod scroll;
39mod selectable;
40mod semantics;
41mod shadow;
42mod size;
43mod slices;
44mod toggleable;
45mod weight;
46mod window_root;
47mod wrap_content;
48
49pub use chain::{ModifierChainHandle, ModifierChainInspectorNode, ModifierLocalsHandle};
50pub(crate) use coordinator_geometry::{CoordinatorGeometry, CoordinatorRect};
51pub use cranpose_foundation::{
52 AnyModifierElement, DynModifierElement, FocusState, PointerEvent, PointerEventKind,
53 PointerSource, RotaryScrollEvent, SemanticsConfiguration, modifier_element,
54};
55use cranpose_foundation::{ModifierNodeElement, NodeCapabilities, ProgressBarRangeInfo};
56#[expect(unused_imports)]
57pub use cranpose_ui_graphics::{
58 BlendMode, BlurredEdgeTreatment, Brush, Color, ColorFilter, CompositingStrategy, CornerRadii,
59 CursorIcon, CustomPointerIcon, CutDirection, Dp, DpOffset, EdgeInsets, GradientCutMaskSpec,
60 GradientFadeMaskSpec, GraphicsLayer, LayerShape, Point, PointerIcon, PointerIconError, Rect,
61 RenderEffect, RoundedCornerShape, RuntimeShader, Shadow, ShadowScope, Size, TransformOrigin,
62};
63use cranpose_ui_layout::{Alignment, HorizontalAlignment, IntrinsicSize, VerticalAlignment};
64pub use drag_and_drop::{
65 DragAndDropEvent, DragAndDropOutcome, DragAndDropPayload, DragAndDropPoint, DragAndDropSource,
66 DragAndDropSourceElement, DragAndDropSourceNode, DragAndDropState, DragAndDropTarget,
67 DragAndDropTargetElement, DragAndDropTargetNode,
68};
69use focus::FocusTargetElement;
70pub use focus::{FocusDirection, FocusRequestError, FocusRequester, FocusRequesterElement};
71pub use graphics_layer::GlassMaterial;
72pub use key_input::KeyEventRoute;
73pub(crate) use local::{
74 ModifierLocalAncestorResolver, ModifierLocalSource, ModifierLocalToken, ResolvedModifierLocal,
75};
76use local::{ModifierLocalConsumerElement, ModifierLocalProviderElement};
77pub use local::{ModifierLocalKey, ModifierLocalReadScope};
78#[expect(unused_imports)]
79pub use pointer_input::{AwaitPointerEventScope, PointerInputScope};
80pub use rotary_input::RotaryInputModifierNode;
81#[cfg(test)]
82pub(crate) use scroll::lazy_scroll_semantics;
83#[cfg(feature = "test-helpers")]
84pub use scroll::{last_fling_velocity, reset_last_fling_velocity};
85use semantics::SemanticsElement;
86pub use semantics::{
87 SemanticsRequester, SemanticsRequesterElement, collect_semantics_from_chain,
88 collect_semantics_from_modifier, semantics_reach_of_chain,
89};
90pub(crate) use slices::SlicesSnapshot;
91pub use slices::{
92 ModifierNodeSlices, ModifierNodeSlicesDebugStats, SliceTextRef, collect_modifier_slices,
93 collect_slices_from_modifier,
94};
95pub use window_root::{
96 WindowRootDescriptor, WindowRootElement, WindowRootEntry, WindowRootNode, WindowRootRegistry,
97 WindowRootRoutingScratch, is_window_root, nearest_window_root, nearest_window_roots,
98 nearest_window_roots_into, window_roots, window_roots_revision,
99};
100
101pub use crate::draw::{DrawCacheBuilder, DrawCommand};
102use crate::modifier_nodes::ClipToBoundsElement;
103
104#[derive(Clone, Debug, Default)]
105pub struct InspectorInfo {
106 properties: Vec<InspectorProperty>,
107}
108
109impl InspectorInfo {
110 pub fn new() -> Self {
111 Self::default()
112 }
113
114 pub fn add_property<V: Into<String>>(&mut self, name: &'static str, value: V) {
115 self.properties.push(InspectorProperty {
116 name,
117 value: value.into(),
118 });
119 }
120
121 pub fn properties(&self) -> &[InspectorProperty] {
122 &self.properties
123 }
124
125 pub fn is_empty(&self) -> bool {
126 self.properties.is_empty()
127 }
128
129 pub fn add_dimension(&mut self, name: &'static str, constraint: DimensionConstraint) {
130 self.add_property(name, describe_dimension(constraint));
131 }
132
133 pub fn add_offset_components(
134 &mut self,
135 x_name: &'static str,
136 y_name: &'static str,
137 offset: Point,
138 ) {
139 self.add_property(x_name, offset.x.to_string());
140 self.add_property(y_name, offset.y.to_string());
141 }
142
143 pub fn add_alignment<A>(&mut self, name: &'static str, alignment: A)
144 where
145 A: fmt::Debug,
146 {
147 self.add_property(name, format!("{alignment:?}"));
148 }
149}
150
151#[derive(Clone, Debug, PartialEq)]
152pub struct InspectorProperty {
153 pub name: &'static str,
154 pub value: String,
155}
156
157#[derive(Clone, Debug, PartialEq)]
158pub struct ModifierInspectorRecord {
159 pub name: &'static str,
160 pub properties: Vec<InspectorProperty>,
161}
162
163#[derive(Clone, Debug)]
164pub(crate) struct InspectorMetadata {
165 name: &'static str,
166 info: InspectorInfo,
167}
168
169impl InspectorMetadata {
170 pub(crate) fn new<F>(name: &'static str, recorder: F) -> Self
171 where
172 F: FnOnce(&mut InspectorInfo),
173 {
174 let mut info = InspectorInfo::new();
175 recorder(&mut info);
176 Self { name, info }
177 }
178
179 fn is_empty(&self) -> bool {
180 self.info.is_empty()
181 }
182
183 fn to_record(&self) -> ModifierInspectorRecord {
184 ModifierInspectorRecord {
185 name: self.name,
186 properties: self.info.properties().to_vec(),
187 }
188 }
189}
190
191fn describe_dimension(constraint: DimensionConstraint) -> String {
192 match constraint {
193 DimensionConstraint::Unspecified => "unspecified".to_string(),
194 DimensionConstraint::Points(value) => value.to_string(),
195 DimensionConstraint::Fraction(value) => format!("fraction({value})"),
196 DimensionConstraint::Intrinsic(size) => format!("intrinsic({size:?})"),
197 }
198}
199
200fn inspector_slice(inspector: &Option<Rc<[InspectorMetadata]>>) -> &[InspectorMetadata] {
201 inspector.as_deref().unwrap_or(&[])
202}
203
204fn merged_inspector(
207 first: &[InspectorMetadata],
208 second: &[InspectorMetadata],
209) -> Option<Rc<[InspectorMetadata]>> {
210 if first.is_empty() && second.is_empty() {
211 return None;
212 }
213 Some(first.iter().chain(second).cloned().collect())
214}
215
216pub(crate) fn inspector_metadata<F>(name: &'static str, recorder: F) -> InspectorMetadata
217where
218 F: FnOnce(&mut InspectorInfo),
219{
220 if !inspector_metadata_enabled() {
221 return InspectorMetadata::new(name, |_| {});
222 }
223 InspectorMetadata::new(name, recorder)
224}
225
226pub(crate) fn modifier_debug_enabled() -> bool {
227 #[cfg(not(target_arch = "wasm32"))]
228 {
229 cranpose_core::env_flag!("COMPOSE_DEBUG_MODIFIERS")
230 }
231 #[cfg(target_arch = "wasm32")]
232 {
233 false
234 }
235}
236
237fn inspector_metadata_enabled() -> bool {
238 cfg!(any(test, feature = "inspection")) || modifier_debug_enabled()
239}
240
241type ModifierElements = SmallVec<[DynModifierElement; 4]>;
244
245#[derive(Clone)]
250enum Elements {
251 One(DynModifierElement),
252 Many(Rc<ModifierElements>),
253}
254
255impl Elements {
256 fn from_slice(elements: &[DynModifierElement]) -> Self {
257 match elements {
258 [element] => Self::One(element.clone()),
259 _ => Self::Many(Rc::new(elements.iter().cloned().collect())),
260 }
261 }
262
263 fn extend_from(&mut self, more: &[DynModifierElement]) {
264 match self {
265 Self::Many(shared) => match Rc::get_mut(shared) {
266 Some(owned) => owned.extend(more.iter().cloned()),
267 None => *shared = Rc::new(shared.iter().chain(more).cloned().collect()),
268 },
269 Self::One(first) => {
270 *self = Self::Many(Rc::new(
271 std::iter::once(&*first).chain(more).cloned().collect(),
272 ));
273 }
274 }
275 }
276
277 fn shares_storage(&self, other: &Self) -> bool {
279 match (self, other) {
280 (Self::One(a), Self::One(b)) => Rc::ptr_eq(a, b),
281 (Self::Many(a), Self::Many(b)) => Rc::ptr_eq(a, b),
282 _ => false,
283 }
284 }
285}
286
287impl std::ops::Deref for Elements {
288 type Target = [DynModifierElement];
289
290 fn deref(&self) -> &[DynModifierElement] {
291 match self {
292 Self::One(element) => std::slice::from_ref(element),
293 Self::Many(elements) => elements.as_slice(),
294 }
295 }
296}
297
298#[derive(Clone)]
299enum ModifierKind {
300 Empty,
301 Single {
302 elements: Elements,
303 inspector: Option<Rc<[InspectorMetadata]>>,
306 },
307}
308
309const FINGERPRINT_KIND_EMPTY: u8 = 0;
310const FINGERPRINT_KIND_SINGLE: u8 = 1;
311
312const FINGERPRINT_EMPTY_STRICT_SEED: u64 = 0x243f_6a88_85a3_08d3;
313const FINGERPRINT_EMPTY_STRUCTURAL_SEED: u64 = 0x1319_8a2e_0370_7344;
314const FINGERPRINT_SINGLE_STRICT_SEED: u64 = 0xa409_3822_299f_31d0;
315const FINGERPRINT_SINGLE_STRUCTURAL_SEED: u64 = 0x082e_fa98_ec4e_6c89;
316const FINGERPRINT_SEQUENCE_MUL: u64 = 0x9e37_79b1_85eb_ca87;
317const FINGERPRINT_STRICT_UPDATE_TAG: u64 = 0xdbe6_d5d5_fe4c_ce2f;
318const FINGERPRINT_STRUCTURAL_DRAW_ONLY_TAG: u64 = 0x94d0_49bb_1331_11eb;
319
320#[derive(Clone, Copy, Debug, PartialEq, Eq)]
321struct ModifierFingerprints {
322 strict: u64,
323 structural: u64,
324}
325
326#[inline]
327fn mix_fingerprint_bits(mut value: u64) -> u64 {
328 value ^= value >> 33;
329 value = value.wrapping_mul(0xff51_afd7_ed55_8ccd);
330 value ^= value >> 33;
331 value = value.wrapping_mul(0xc4ce_b9fe_1a85_ec53);
332 value ^ (value >> 33)
333}
334
335#[inline]
336fn fold_fingerprint(state: u64, value: u64) -> u64 {
337 mix_fingerprint_bits(state ^ value.wrapping_add(FINGERPRINT_SEQUENCE_MUL))
338 .wrapping_mul(FINGERPRINT_SEQUENCE_MUL)
339}
340
341#[inline]
342fn empty_fingerprints() -> ModifierFingerprints {
343 ModifierFingerprints {
344 strict: fold_fingerprint(FINGERPRINT_EMPTY_STRICT_SEED, FINGERPRINT_KIND_EMPTY as u64),
345 structural: fold_fingerprint(
346 FINGERPRINT_EMPTY_STRUCTURAL_SEED,
347 FINGERPRINT_KIND_EMPTY as u64,
348 ),
349 }
350}
351
352#[inline]
353fn single_fingerprint_seed() -> ModifierFingerprints {
354 let strict = fold_fingerprint(
355 FINGERPRINT_SINGLE_STRICT_SEED,
356 FINGERPRINT_KIND_SINGLE as u64,
357 );
358 let structural = fold_fingerprint(
359 FINGERPRINT_SINGLE_STRUCTURAL_SEED,
360 FINGERPRINT_KIND_SINGLE as u64,
361 );
362 ModifierFingerprints { strict, structural }
363}
364
365#[inline]
366fn element_common_fingerprint(element: &DynModifierElement) -> u64 {
367 let mut hasher = default::new();
368 element.element_type().hash(&mut hasher);
369 element.capabilities().bits().hash(&mut hasher);
370 hasher.finish()
371}
372
373#[inline]
374fn element_fingerprints(element: &DynModifierElement) -> ModifierFingerprints {
375 let common = element_common_fingerprint(element);
376 let requires_update = element.requires_update();
377 let strict_payload = if requires_update {
378 let element_ptr = Rc::as_ptr(element) as *const () as usize as u64;
379 element_ptr ^ FINGERPRINT_STRICT_UPDATE_TAG
380 } else {
381 element.hash_code()
382 };
383 let strict = mix_fingerprint_bits(common ^ strict_payload);
384
385 let is_draw_only = element.capabilities() == NodeCapabilities::DRAW;
386 let structural_payload = if is_draw_only {
387 FINGERPRINT_STRUCTURAL_DRAW_ONLY_TAG
388 } else {
389 element.hash_code()
390 };
391 let structural = mix_fingerprint_bits(common ^ structural_payload);
392
393 ModifierFingerprints { strict, structural }
394}
395
396#[inline]
397fn append_fingerprints(
398 mut fingerprints: ModifierFingerprints,
399 elements: &[DynModifierElement],
400) -> ModifierFingerprints {
401 for element in elements {
402 let element_fingerprints = element_fingerprints(element);
403 fingerprints.strict = fold_fingerprint(fingerprints.strict, element_fingerprints.strict);
404 fingerprints.structural =
405 fold_fingerprint(fingerprints.structural, element_fingerprints.structural);
406 }
407 fingerprints
408}
409
410fn single_fingerprints(elements: &[DynModifierElement]) -> ModifierFingerprints {
411 append_fingerprints(single_fingerprint_seed(), elements)
412}
413
414pub struct ModifierElementIterator<'a> {
415 inner: std::slice::Iter<'a, DynModifierElement>,
416}
417
418impl<'a> Iterator for ModifierElementIterator<'a> {
419 type Item = &'a DynModifierElement;
420
421 #[inline]
422 fn next(&mut self) -> Option<Self::Item> {
423 self.inner.next()
424 }
425
426 #[inline]
427 fn size_hint(&self) -> (usize, Option<usize>) {
428 self.inner.size_hint()
429 }
430}
431
432impl ExactSizeIterator for ModifierElementIterator<'_> {}
433
434pub(crate) struct ModifierInspectorIterator<'a> {
435 inner: std::slice::Iter<'a, InspectorMetadata>,
436}
437
438impl<'a> Iterator for ModifierInspectorIterator<'a> {
439 type Item = &'a InspectorMetadata;
440
441 #[inline]
442 fn next(&mut self) -> Option<Self::Item> {
443 self.inner.next()
444 }
445
446 #[inline]
447 fn size_hint(&self) -> (usize, Option<usize>) {
448 self.inner.size_hint()
449 }
450}
451
452impl ExactSizeIterator for ModifierInspectorIterator<'_> {}
453
454#[derive(Clone)]
472pub struct Modifier {
473 kind: ModifierKind,
474 strict_fingerprint: u64,
475 structural_fingerprint: u64,
476 element_count: usize,
477 provides_composition_locals: bool,
478}
479
480impl Default for Modifier {
481 fn default() -> Self {
482 let fingerprints = empty_fingerprints();
483 Self {
484 kind: ModifierKind::Empty,
485 strict_fingerprint: fingerprints.strict,
486 structural_fingerprint: fingerprints.structural,
487 element_count: 0,
488 provides_composition_locals: false,
489 }
490 }
491}
492
493impl Modifier {
494 pub fn empty() -> Self {
495 Self::default()
496 }
497
498 pub fn from_element<E>(element: E) -> Self
500 where
501 E: ModifierNodeElement,
502 {
503 Self::with_element(element)
504 }
505
506 pub fn clip_to_bounds(self) -> Self {
510 thread_local! {
511 static CLIP_TO_BOUNDS: Modifier = Modifier::with_element(ClipToBoundsElement::new());
512 }
513 let modifier = CLIP_TO_BOUNDS.with(Clone::clone);
514 let modifier =
515 modifier.with_inspector_metadata(inspector_metadata("clipToBounds", |info| {
516 info.add_property("clipToBounds", "true");
517 }));
518 self.then(modifier)
519 }
520
521 pub fn modifier_local_provider<T, F>(self, key: ModifierLocalKey<T>, value: F) -> Self
522 where
523 T: 'static,
524 F: Fn() -> T + 'static,
525 {
526 let element = ModifierLocalProviderElement::new(key, value);
527 let modifier = Modifier::from_parts(&[modifier_element(element)]);
528 self.then(modifier)
529 }
530
531 pub fn modifier_local_consumer<F>(self, consumer: F) -> Self
532 where
533 F: for<'scope> Fn(&mut ModifierLocalReadScope<'scope>) + 'static,
534 {
535 let element = ModifierLocalConsumerElement::new(consumer);
536 let modifier = Modifier::from_parts(&[modifier_element(element)]);
537 self.then(modifier)
538 }
539
540 pub fn semantics_spec(self, spec: cranpose_foundation::SemanticsSpec) -> Self {
551 self.stable_semantics(move |config: &mut SemanticsConfiguration| config.merge(&spec))
552 }
553
554 pub fn semantics<F>(self, recorder: F) -> Self
559 where
560 F: Fn(&mut SemanticsConfiguration) + 'static,
561 {
562 self.semantics_recorder(std::rc::Rc::new(recorder), true)
563 }
564
565 pub fn stable_semantics<F>(self, recorder: F) -> Self
572 where
573 F: Fn(&mut SemanticsConfiguration) + 'static,
574 {
575 self.semantics_recorder(std::rc::Rc::new(recorder), false)
576 }
577
578 fn semantics_recorder(
579 self,
580 recorder: std::rc::Rc<dyn Fn(&mut SemanticsConfiguration)>,
581 reads_live_state: bool,
582 ) -> Self {
583 let metadata = if inspector_metadata_enabled() {
584 let mut preview = SemanticsConfiguration::default();
585 recorder(&mut preview);
586 let description = preview.content_description.clone();
587 let state_description = preview.state_description.clone();
588 let role = preview.role;
589 let is_clickable = preview.is_activatable();
590 let canvas_children = preview.canvas_children.len();
591 inspector_metadata("semantics", move |info| {
592 if let Some(desc) = &description {
593 info.add_property("contentDescription", desc.clone());
594 }
595 if let Some(state) = &state_description {
596 info.add_property("stateDescription", state.clone());
597 }
598 if let Some(role) = role {
599 info.add_property("role", format!("{role:?}"));
600 }
601 if is_clickable {
602 info.add_property("isClickable", "true");
603 }
604 if canvas_children > 0 {
605 info.add_property("canvasSemanticsChildren", canvas_children.to_string());
606 }
607 })
608 } else {
609 inspector_metadata("semantics", |_| {})
610 };
611 let element = SemanticsElement::new(recorder, reads_live_state);
612 let modifier =
613 Modifier::from_parts(&[modifier_element(element)]).with_inspector_metadata(metadata);
614 self.then(modifier)
615 }
616
617 pub fn progress_semantics(self, current: f32, start: f32, end: f32, steps: u32) -> Self {
626 let info = ProgressBarRangeInfo::new(current, start, end, steps);
627 self.stable_semantics(move |config| config.progress = Some(info))
628 }
629
630 pub fn dropdown_list(self) -> Self {
634 self.role(cranpose_foundation::SemanticsWidgetRole::DropdownList)
635 }
636
637 pub fn value_picker(self) -> Self {
640 self.role(cranpose_foundation::SemanticsWidgetRole::ValuePicker)
641 }
642
643 pub fn expand(self, action: impl Fn() -> bool + 'static) -> Self {
648 let action = cranpose_foundation::SemanticsExpand::new(action);
649 self.stable_semantics(move |config| config.expand = Some(action.clone()))
650 }
651
652 pub fn on_long_click(
662 self,
663 label: impl Into<String>,
664 action: impl Fn() -> bool + 'static,
665 ) -> Self {
666 let label = label.into();
667 let action = cranpose_foundation::SemanticsLongClick::new(action);
668 self.stable_semantics(move |config| {
669 config.on_long_click_label = Some(label.clone());
670 config.on_long_click = Some(action.clone());
671 })
672 }
673
674 pub fn on_magic_tap(
679 self,
680 label: impl Into<String>,
681 action: impl Fn() -> bool + 'static,
682 ) -> Self {
683 let label = label.into();
684 let action = cranpose_foundation::SemanticsMagicTap::new(action);
685 self.stable_semantics(move |config| {
686 config.on_magic_tap_label = Some(label.clone());
687 config.on_magic_tap = Some(action.clone());
688 })
689 }
690
691 pub fn input_labels<S: Into<String>>(self, labels: impl IntoIterator<Item = S>) -> Self {
695 let labels: Vec<String> = labels.into_iter().map(Into::into).collect();
696 self.stable_semantics(move |config| config.input_labels.clone_from(&labels))
697 }
698
699 pub fn language(self, tag: impl Into<String>) -> Self {
703 let tag = tag.into();
704 self.stable_semantics(move |config| config.language = Some(tag.clone()))
705 }
706
707 pub fn collapse(self, action: impl Fn() -> bool + 'static) -> Self {
711 let action = cranpose_foundation::SemanticsExpand::new(action);
712 self.stable_semantics(move |config| config.collapse = Some(action.clone()))
713 }
714
715 pub fn dismiss(self, action: impl Fn() -> bool + 'static) -> Self {
720 let action = cranpose_foundation::SemanticsDismiss::new(action);
721 self.stable_semantics(move |config| config.dismiss = Some(action.clone()))
722 }
723
724 pub fn scroll_to_index(self, action: impl Fn(usize) -> bool + 'static) -> Self {
730 let action = cranpose_foundation::SemanticsScrollToIndex::new(action);
731 self.stable_semantics(move |config| config.scroll_to_index = Some(action.clone()))
732 }
733
734 pub fn traversal_index(self, index: f32) -> Self {
740 self.stable_semantics(move |config| config.traversal_index = index)
741 }
742
743 pub fn password(self) -> Self {
748 self.stable_semantics(|config| config.password = true)
749 }
750
751 pub fn error(self, message: impl Into<String>) -> Self {
755 let message = message.into();
756 self.stable_semantics(move |config| config.error = Some(message.clone()))
757 }
758
759 pub fn pane_title(self, title: impl Into<String>) -> Self {
763 let title = title.into();
764 self.stable_semantics(move |config| config.pane_title = Some(title.clone()))
765 }
766
767 pub fn selectable_group(self) -> Self {
772 self.stable_semantics(|config| config.selectable_group = true)
773 }
774
775 pub fn merge_descendants(self) -> Self {
780 self.stable_semantics(|config| config.merge_descendants = true)
781 }
782
783 pub fn hide_from_accessibility(self) -> Self {
788 self.stable_semantics(|config| config.hidden = true)
789 }
790
791 pub fn heading(self) -> Self {
796 self.role(cranpose_foundation::SemanticsWidgetRole::Header)
797 }
798
799 pub fn role(self, role: cranpose_foundation::SemanticsWidgetRole) -> Self {
804 self.stable_semantics(move |config| config.role = Some(role))
805 }
806
807 pub fn live_region(self, mode: cranpose_foundation::LiveRegionMode) -> Self {
813 self.stable_semantics(move |config| config.live_region = Some(mode))
814 }
815
816 pub fn content_description(self, description: impl Into<String>) -> Self {
821 let description = description.into();
822 self.stable_semantics(move |config| config.content_description = Some(description.clone()))
823 }
824
825 pub fn focus_target(self) -> Self {
831 let element = FocusTargetElement::new();
832 let modifier = Modifier::from_parts(&[modifier_element(element)]);
833 self.then(modifier)
834 }
835
836 pub fn on_focus_changed<F>(self, callback: F) -> Self
841 where
842 F: Fn(FocusState) + 'static,
843 {
844 let element = FocusTargetElement::with_callback(callback);
845 let modifier = Modifier::from_parts(&[modifier_element(element)]);
846 self.then(modifier)
847 }
848
849 pub fn focus_requester(self, requester: &FocusRequester) -> Self {
856 let element = FocusRequesterElement::new(requester.clone());
857 let modifier = Modifier::from_parts(&[modifier_element(element)]);
858 self.then(modifier)
859 }
860
861 pub fn semantics_requester(self, requester: &SemanticsRequester) -> Self {
869 let element = SemanticsRequesterElement::new(requester.clone());
870 let modifier = Modifier::from_parts(&[modifier_element(element)]);
871 self.then(modifier)
872 }
873
874 pub fn debug_chain(self, tag: &'static str) -> Self {
892 use cranpose_foundation::{ModifierNode, ModifierNodeContext, NodeCapabilities, NodeState};
893
894 #[derive(Clone)]
895 struct DebugChainElement {
896 tag: &'static str,
897 }
898
899 impl fmt::Debug for DebugChainElement {
900 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
901 f.debug_struct("DebugChainElement")
902 .field("tag", &self.tag)
903 .finish()
904 }
905 }
906
907 impl PartialEq for DebugChainElement {
908 fn eq(&self, other: &Self) -> bool {
909 self.tag == other.tag
910 }
911 }
912
913 impl Eq for DebugChainElement {}
914
915 impl std::hash::Hash for DebugChainElement {
916 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
917 self.tag.hash(state);
918 }
919 }
920
921 impl ModifierNodeElement for DebugChainElement {
922 type Node = DebugChainNode;
923
924 fn create(&self) -> Self::Node {
925 DebugChainNode::new(self.tag)
926 }
927
928 fn update(&self, node: &mut Self::Node) {
929 node.tag = self.tag;
930 }
931
932 fn capabilities(&self) -> NodeCapabilities {
933 NodeCapabilities::empty()
934 }
935 }
936
937 struct DebugChainNode {
938 tag: &'static str,
939 state: NodeState,
940 }
941
942 impl DebugChainNode {
943 fn new(tag: &'static str) -> Self {
944 Self {
945 tag,
946 state: NodeState::new(),
947 }
948 }
949 }
950
951 impl ModifierNode for DebugChainNode {
952 fn on_attach(&mut self, _context: &mut dyn ModifierNodeContext) {
953 eprintln!("[debug_chain:{}] Modifier chain attached", self.tag);
954 }
955
956 fn on_detach(&mut self) {
957 eprintln!("[debug_chain:{}] Modifier chain detached", self.tag);
958 }
959
960 fn on_reset(&mut self) {
961 eprintln!("[debug_chain:{}] Modifier chain reset", self.tag);
962 }
963 }
964
965 impl cranpose_foundation::DelegatableNode for DebugChainNode {
966 fn node_state(&self) -> &NodeState {
967 &self.state
968 }
969 }
970
971 let element = DebugChainElement { tag };
972 let modifier = Modifier::from_parts(&[modifier_element(element)]);
973 self.then(modifier)
974 .with_inspector_metadata(inspector_metadata("debugChain", move |info| {
975 info.add_property("tag", tag);
976 }))
977 }
978
979 pub fn then(self, next: Modifier) -> Modifier {
985 let Modifier {
986 kind,
987 strict_fingerprint,
988 structural_fingerprint,
989 element_count,
990 provides_composition_locals,
991 } = self;
992 let ModifierKind::Single {
993 mut elements,
994 inspector,
995 } = kind
996 else {
997 return next;
998 };
999 let ModifierKind::Single {
1000 elements: next_elements,
1001 inspector: next_inspector,
1002 } = &next.kind
1003 else {
1004 return Modifier {
1005 kind: ModifierKind::Single {
1006 elements,
1007 inspector,
1008 },
1009 strict_fingerprint,
1010 structural_fingerprint,
1011 element_count,
1012 provides_composition_locals,
1013 };
1014 };
1015
1016 let fingerprints = append_fingerprints(
1017 ModifierFingerprints {
1018 strict: strict_fingerprint,
1019 structural: structural_fingerprint,
1020 },
1021 next_elements,
1022 );
1023 elements.extend_from(next_elements);
1024 let inspector = if next_inspector.is_none() {
1025 inspector
1026 } else {
1027 merged_inspector(inspector_slice(&inspector), inspector_slice(next_inspector))
1028 };
1029 Modifier {
1030 kind: ModifierKind::Single {
1031 elements,
1032 inspector,
1033 },
1034 strict_fingerprint: fingerprints.strict,
1035 structural_fingerprint: fingerprints.structural,
1036 element_count: element_count + next.element_count,
1037 provides_composition_locals: provides_composition_locals
1038 || next.provides_composition_locals,
1039 }
1040 }
1041
1042 pub(crate) fn element_count(&self) -> usize {
1044 self.element_count
1045 }
1046
1047 pub(crate) fn iter_elements(&self) -> ModifierElementIterator<'_> {
1048 match &self.kind {
1049 ModifierKind::Empty => ModifierElementIterator { inner: [].iter() },
1050 ModifierKind::Single { elements, .. } => ModifierElementIterator {
1051 inner: elements.iter(),
1052 },
1053 }
1054 }
1055
1056 pub(crate) fn provided_composition_locals(&self) -> Vec<ProvidedValue> {
1057 if !self.provides_composition_locals {
1058 return Vec::new();
1059 }
1060 self.iter_elements()
1061 .flat_map(|element| element.provided_composition_locals())
1062 .collect()
1063 }
1064
1065 pub(crate) fn iter_inspector_metadata(&self) -> ModifierInspectorIterator<'_> {
1066 match &self.kind {
1067 ModifierKind::Empty => ModifierInspectorIterator { inner: [].iter() },
1068 ModifierKind::Single { inspector, .. } => ModifierInspectorIterator {
1069 inner: inspector_slice(inspector).iter(),
1070 },
1071 }
1072 }
1073
1074 #[cfg(test)]
1075 pub(crate) fn elements(&self) -> Vec<DynModifierElement> {
1076 match &self.kind {
1077 ModifierKind::Empty => Vec::new(),
1078 ModifierKind::Single { elements, .. } => elements.to_vec(),
1079 }
1080 }
1081
1082 pub(crate) fn inspector_metadata(&self) -> Vec<InspectorMetadata> {
1083 match &self.kind {
1084 ModifierKind::Empty => Vec::new(),
1085 ModifierKind::Single { inspector, .. } => {
1086 inspector.as_deref().map(<[_]>::to_vec).unwrap_or_default()
1087 }
1088 }
1089 }
1090
1091 pub fn total_padding(&self) -> f32 {
1092 let padding = self.padding_values();
1093 padding
1094 .left
1095 .max(padding.right)
1096 .max(padding.top)
1097 .max(padding.bottom)
1098 }
1099
1100 pub fn explicit_size(&self) -> Option<Size> {
1101 let props = self.layout_properties();
1102 match (props.width, props.height) {
1103 (DimensionConstraint::Points(width), DimensionConstraint::Points(height)) => {
1104 Some(Size { width, height })
1105 }
1106 _ => None,
1107 }
1108 }
1109
1110 pub fn padding_values(&self) -> EdgeInsets {
1111 self.resolved_modifiers().padding()
1112 }
1113
1114 pub(crate) fn layout_properties(&self) -> LayoutProperties {
1115 self.resolved_modifiers().layout_properties()
1116 }
1117
1118 pub fn box_alignment(&self) -> Option<Alignment> {
1119 self.layout_properties().box_alignment()
1120 }
1121
1122 pub fn column_alignment(&self) -> Option<HorizontalAlignment> {
1123 self.layout_properties().column_alignment()
1124 }
1125
1126 pub fn row_alignment(&self) -> Option<VerticalAlignment> {
1127 self.layout_properties().row_alignment()
1128 }
1129
1130 pub fn draw_commands(&self) -> Vec<DrawCommand> {
1131 collect_slices_from_modifier(self).draw_commands().to_vec()
1132 }
1133
1134 pub fn clips_to_bounds(&self) -> bool {
1135 collect_slices_from_modifier(self).clip_to_bounds()
1136 }
1137
1138 pub fn collect_inspector_records(&self) -> Vec<ModifierInspectorRecord> {
1140 self.inspector_metadata()
1141 .iter()
1142 .map(InspectorMetadata::to_record)
1143 .collect()
1144 }
1145
1146 pub fn resolved_modifiers(&self) -> ResolvedModifiers {
1147 let mut handle = ModifierChainHandle::new();
1148 let _ = handle.update(self);
1149 handle.resolved_modifiers()
1150 }
1151
1152 pub fn with_element<E>(element: E) -> Self
1155 where
1156 E: ModifierNodeElement,
1157 {
1158 Self::from_parts(&[modifier_element(element)])
1159 }
1160
1161 pub(crate) fn from_parts(elements: &[DynModifierElement]) -> Self {
1163 if elements.is_empty() {
1164 Self::default()
1165 } else {
1166 let element_count = elements.len();
1167 let provides_composition_locals = elements
1168 .iter()
1169 .any(|element| element.provides_composition_locals());
1170 let fingerprints = single_fingerprints(elements);
1171 Self {
1172 kind: ModifierKind::Single {
1173 elements: Elements::from_slice(elements),
1174 inspector: None,
1175 },
1176 strict_fingerprint: fingerprints.strict,
1177 structural_fingerprint: fingerprints.structural,
1178 element_count,
1179 provides_composition_locals,
1180 }
1181 }
1182 }
1183
1184 pub(crate) fn with_inspector_metadata(self, metadata: InspectorMetadata) -> Self {
1185 if metadata.is_empty() {
1186 return self;
1187 }
1188 match self.kind {
1189 ModifierKind::Empty => self,
1190 ModifierKind::Single {
1191 elements,
1192 inspector,
1193 } => {
1194 let new_inspector = inspector_slice(&inspector)
1195 .iter()
1196 .cloned()
1197 .chain(std::iter::once(metadata))
1198 .collect();
1199 Self {
1200 kind: ModifierKind::Single {
1201 elements,
1202 inspector: Some(new_inspector),
1203 },
1204 strict_fingerprint: self.strict_fingerprint,
1205 structural_fingerprint: self.structural_fingerprint,
1206 element_count: self.element_count,
1207 provides_composition_locals: self.provides_composition_locals,
1208 }
1209 }
1210 }
1211 }
1212
1213 pub fn structural_eq(&self, other: &Self) -> bool {
1218 self.eq_internal(other, false)
1219 }
1220
1221 pub(crate) fn differs_only_in<E: 'static>(&self, other: &Self) -> bool {
1225 if self.element_count != other.element_count {
1226 return false;
1227 }
1228 let differing = std::any::TypeId::of::<E>();
1229 self.iter_elements()
1230 .zip(other.iter_elements())
1231 .all(|(a, b)| {
1232 if a.requires_update() || b.requires_update() {
1233 false
1234 } else if a.element_type() == differing {
1235 b.element_type() == differing
1236 } else {
1237 a.equals_element(&**b)
1238 }
1239 })
1240 }
1241
1242 fn eq_internal(&self, other: &Self, consider_always_update: bool) -> bool {
1243 if self.element_count != other.element_count {
1244 return false;
1245 }
1246 if consider_always_update {
1247 if self.strict_fingerprint != other.strict_fingerprint {
1248 return false;
1249 }
1250 } else if self.structural_fingerprint != other.structural_fingerprint {
1251 return false;
1252 }
1253
1254 match (&self.kind, &other.kind) {
1255 (ModifierKind::Empty, ModifierKind::Empty) => true,
1256 (
1257 ModifierKind::Single {
1258 elements: e1,
1259 inspector: _,
1260 },
1261 ModifierKind::Single {
1262 elements: e2,
1263 inspector: _,
1264 },
1265 ) => {
1266 if e1.shares_storage(e2) {
1267 return true;
1268 }
1269
1270 if e1.len() != e2.len() {
1271 return false;
1272 }
1273
1274 for (a, b) in e1.iter().zip(e2.iter()) {
1275 if !consider_always_update
1276 && a.element_type() == b.element_type()
1277 && a.capabilities() == NodeCapabilities::DRAW
1278 && b.capabilities() == NodeCapabilities::DRAW
1279 {
1280 continue;
1281 }
1282
1283 if consider_always_update && (a.requires_update() || b.requires_update()) {
1284 if !Rc::ptr_eq(a, b) {
1285 return false;
1286 }
1287 continue;
1288 }
1289
1290 if !a.equals_element(&**b) {
1291 return false;
1292 }
1293 }
1294
1295 true
1296 }
1297 _ => false,
1298 }
1299 }
1300}
1301
1302impl PartialEq for Modifier {
1303 fn eq(&self, other: &Self) -> bool {
1304 self.eq_internal(other, true)
1305 }
1306}
1307
1308impl Eq for Modifier {}
1309
1310impl fmt::Display for Modifier {
1311 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1312 match &self.kind {
1313 ModifierKind::Empty => write!(f, "Modifier.empty"),
1314 ModifierKind::Single { elements, .. } => {
1315 if elements.is_empty() {
1316 return write!(f, "Modifier.empty");
1317 }
1318 write!(f, "Modifier[")?;
1319 for (index, element) in elements.iter().enumerate() {
1320 if index > 0 {
1321 write!(f, ", ")?;
1322 }
1323 let name = element.inspector_name();
1324 let mut properties = Vec::new();
1325 element.record_inspector_properties(&mut |prop, value| {
1326 properties.push(format!("{prop}={value}"));
1327 });
1328 if properties.is_empty() {
1329 write!(f, "{name}")?;
1330 } else {
1331 write!(f, "{name}({})", properties.join(", "))?;
1332 }
1333 }
1334 write!(f, "]")
1335 }
1336 }
1337 }
1338}
1339
1340impl fmt::Debug for Modifier {
1341 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1342 fmt::Display::fmt(self, f)
1343 }
1344}
1345
1346#[derive(Clone, Copy, Debug, PartialEq)]
1347pub struct ResolvedBackground {
1348 color: Color,
1349 shape: Option<RoundedCornerShape>,
1350}
1351
1352impl ResolvedBackground {
1353 pub fn new(color: Color, shape: Option<RoundedCornerShape>) -> Self {
1354 Self { color, shape }
1355 }
1356
1357 pub fn color(&self) -> Color {
1358 self.color
1359 }
1360
1361 pub fn shape(&self) -> Option<RoundedCornerShape> {
1362 self.shape
1363 }
1364
1365 pub fn set_shape(&mut self, shape: Option<RoundedCornerShape>) {
1366 self.shape = shape;
1367 }
1368}
1369
1370#[derive(Clone, Copy, Debug, PartialEq, Default)]
1371pub struct ResolvedModifiers {
1372 padding: EdgeInsets,
1373 layout: LayoutProperties,
1374 offset: Point,
1375}
1376
1377impl ResolvedModifiers {
1378 pub fn padding(&self) -> EdgeInsets {
1379 self.padding
1380 }
1381
1382 pub fn layout_properties(&self) -> LayoutProperties {
1383 self.layout
1384 }
1385
1386 pub fn offset(&self) -> Point {
1387 self.offset
1388 }
1389
1390 pub(crate) fn set_padding(&mut self, padding: EdgeInsets) {
1391 self.padding = padding;
1392 }
1393
1394 pub(crate) fn set_layout_properties(&mut self, layout: LayoutProperties) {
1395 self.layout = layout;
1396 }
1397
1398 pub(crate) fn set_offset(&mut self, offset: Point) {
1399 self.offset = offset;
1400 }
1401
1402 pub(crate) fn on_device_grid(&self, density: f32) -> Self {
1405 use cranpose_ui_layout::round_to_px;
1406
1407 let round = |value: Option<f32>| value.map(|value| round_to_px(value, density));
1408 let dimension = |dimension: DimensionConstraint| match dimension {
1409 DimensionConstraint::Points(value) => {
1410 DimensionConstraint::Points(round_to_px(value, density))
1411 }
1412 other => other,
1413 };
1414 Self {
1415 padding: crate::modifier_nodes::device_padding(self.padding, density),
1416 layout: LayoutProperties {
1417 padding: crate::modifier_nodes::device_padding(self.layout.padding, density),
1418 width: dimension(self.layout.width),
1419 height: dimension(self.layout.height),
1420 min_width: round(self.layout.min_width),
1421 min_height: round(self.layout.min_height),
1422 max_width: round(self.layout.max_width),
1423 max_height: round(self.layout.max_height),
1424 ..self.layout
1425 },
1426 offset: Point {
1427 x: round_to_px(self.offset.x, density),
1428 y: round_to_px(self.offset.y, density),
1429 },
1430 }
1431 }
1432}
1433
1434#[derive(Clone, Copy, Debug, Default, PartialEq)]
1435pub enum DimensionConstraint {
1436 #[default]
1437 Unspecified,
1438 Points(f32),
1439 Fraction(f32),
1440 Intrinsic(IntrinsicSize),
1441}
1442
1443#[derive(Clone, Copy, Debug, Default, PartialEq)]
1444pub struct LayoutWeight {
1445 pub weight: f32,
1446 pub fill: bool,
1447}
1448
1449#[derive(Clone, Copy, Debug, Default, PartialEq)]
1450pub struct LayoutProperties {
1451 padding: EdgeInsets,
1452 width: DimensionConstraint,
1453 height: DimensionConstraint,
1454 min_width: Option<f32>,
1455 min_height: Option<f32>,
1456 max_width: Option<f32>,
1457 max_height: Option<f32>,
1458 weight: Option<LayoutWeight>,
1459 box_alignment: Option<Alignment>,
1460 column_alignment: Option<HorizontalAlignment>,
1461 row_alignment: Option<VerticalAlignment>,
1462 row_baseline: bool,
1463}
1464
1465impl LayoutProperties {
1466 pub fn padding(&self) -> EdgeInsets {
1467 self.padding
1468 }
1469
1470 pub fn width(&self) -> DimensionConstraint {
1471 self.width
1472 }
1473
1474 pub fn height(&self) -> DimensionConstraint {
1475 self.height
1476 }
1477
1478 pub fn min_width(&self) -> Option<f32> {
1479 self.min_width
1480 }
1481
1482 pub fn min_height(&self) -> Option<f32> {
1483 self.min_height
1484 }
1485
1486 pub fn max_width(&self) -> Option<f32> {
1487 self.max_width
1488 }
1489
1490 pub fn max_height(&self) -> Option<f32> {
1491 self.max_height
1492 }
1493
1494 pub fn weight(&self) -> Option<LayoutWeight> {
1495 self.weight
1496 }
1497
1498 pub fn box_alignment(&self) -> Option<Alignment> {
1499 self.box_alignment
1500 }
1501
1502 pub fn column_alignment(&self) -> Option<HorizontalAlignment> {
1503 self.column_alignment
1504 }
1505
1506 pub fn row_alignment(&self) -> Option<VerticalAlignment> {
1507 self.row_alignment
1508 }
1509
1510 pub fn row_baseline(&self) -> bool {
1512 self.row_baseline
1513 }
1514}
1515
1516#[cfg(test)]
1517#[path = "tests/modifier_tests.rs"]
1518mod tests;