use std::{fmt, rc::Rc};
use cranpose_core::ProvidedValue;
use smallvec::SmallVec;
mod alignment;
mod background;
mod blur;
mod border;
mod chain;
mod clickable;
mod coordinator_rect;
mod drag_and_drop;
mod draw_cache;
mod element_match;
mod fill;
pub(crate) mod focus;
mod focus_ring;
mod graphics_layer;
mod key_input;
mod layout_data;
mod local;
mod long_press;
mod minimum_interactive;
mod offset;
mod padding;
mod pointer_icon;
pub(crate) mod pointer_input;
mod pointer_opaque;
mod resolved_modifiers;
mod rotary_input;
mod scroll;
mod selectable;
mod semantics;
mod shadow;
mod size;
mod slices;
mod toggleable;
mod weight;
mod window_root;
mod wrap_content;
mod z_index;
pub(crate) use chain::{
InPlaceReach, InPlaceUpdate, ModifierChainHandle, ModifierChainInspectorNode,
ModifierLocalsHandle, SharedParentData,
};
pub(crate) use coordinator_rect::CoordinatorRect;
use cranpose_foundation::{
DynModifierElement, ModifierNodeElement, ProgressBarRangeInfo, SemanticsConfiguration,
modifier_element, plain_element,
};
pub use cranpose_foundation::{
PointerButton, PointerButtons, PointerEvent, PointerEventKind, PointerSource, RotaryScrollEvent,
};
pub use cranpose_ui_graphics::{
BlendMode, Brush, Color, CompositingStrategy, CornerRadii, DpOffset, EdgeInsets, GraphicsLayer,
LayerShape, Point, Rect, RenderEffect, RoundedCornerShape, RuntimeShader, Shadow, ShadowScope,
Size, TransformOrigin,
};
use cranpose_ui_layout::{Alignment, HorizontalAlignment, VerticalAlignment};
pub use drag_and_drop::{
DragAndDropEvent, DragAndDropOutcome, DragAndDropPayload, DragAndDropPoint, DragAndDropSource,
DragAndDropSourceElement, DragAndDropSourceNode, DragAndDropState, DragAndDropTarget,
DragAndDropTargetElement, DragAndDropTargetNode,
};
pub(crate) use element_match::ElementMatch;
#[cfg(test)]
pub(crate) use focus::FocusRequesterElement;
pub use focus::{FocusDirection, FocusRequestError, FocusRequester};
pub use graphics_layer::GlassMaterial;
pub use key_input::KeyEventRoute;
pub(crate) use local::{
ModifierLocalAncestorResolver, ModifierLocalSource, ModifierLocalToken, ResolvedModifierLocal,
};
use local::{ModifierLocalConsumerElement, ModifierLocalProviderElement};
pub use local::{ModifierLocalKey, ModifierLocalReadScope};
pub use pointer_input::PointerInputScope;
pub(crate) use resolved_modifiers::{DimensionConstraint, LayoutProperties, LayoutWeight};
pub use resolved_modifiers::{ResolvedBackground, ResolvedModifiers};
pub use rotary_input::RotaryInputModifierNode;
#[cfg(feature = "test-helpers")]
pub use scroll::{last_fling_velocity, reset_last_fling_velocity};
use semantics::SemanticsElement;
pub use semantics::{SemanticsRequester, collect_semantics_from_modifier};
pub(crate) use semantics::{
SemanticsRequesterElement, collect_semantics_from_chain, semantics_reach_of_chain,
};
pub(crate) use slices::SlicesSnapshot;
pub use slices::{
ModifierNodeSlices, ModifierNodeSlicesDebugStats, SliceTextRef, collect_modifier_slices,
collect_slices_from_modifier,
};
pub use window_root::{
WindowRootDescriptor, WindowRootElement, WindowRootEntry, WindowRootNode, WindowRootRegistry,
WindowRootRoutingScratch, is_window_root, nearest_window_root, nearest_window_roots,
nearest_window_roots_into, window_roots, window_roots_revision,
};
pub use z_index::{z_index_in_use, z_index_modifier_in_use};
pub(crate) use crate::draw::{DrawCacheBuilder, DrawCommand};
use crate::modifier_nodes::ClipToBoundsElement;
#[derive(Clone, Debug, Default)]
pub(crate) struct InspectorInfo {
properties: Vec<InspectorProperty>,
}
impl InspectorInfo {
pub(crate) fn new() -> Self {
Self::default()
}
pub(crate) fn add_property<V: Into<String>>(&mut self, name: &'static str, value: V) {
self.properties.push(InspectorProperty {
name,
value: value.into(),
});
}
pub(crate) fn properties(&self) -> &[InspectorProperty] {
&self.properties
}
pub(crate) fn is_empty(&self) -> bool {
self.properties.is_empty()
}
pub(crate) fn add_dimension(&mut self, name: &'static str, constraint: DimensionConstraint) {
self.add_property(name, describe_dimension(constraint));
}
pub(crate) fn add_offset_components(
&mut self,
x_name: &'static str,
y_name: &'static str,
offset: Point,
) {
self.add_property(x_name, offset.x.to_string());
self.add_property(y_name, offset.y.to_string());
}
pub(crate) fn add_alignment<A>(&mut self, name: &'static str, alignment: A)
where
A: fmt::Debug,
{
self.add_property(name, format!("{alignment:?}"));
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct InspectorProperty {
pub name: &'static str,
pub value: String,
}
#[derive(Clone, Debug, PartialEq)]
pub struct ModifierInspectorRecord {
pub name: &'static str,
pub properties: Vec<InspectorProperty>,
}
#[derive(Clone, Debug)]
pub(crate) struct InspectorMetadata {
name: &'static str,
info: InspectorInfo,
}
impl InspectorMetadata {
pub(crate) fn new<F>(name: &'static str, recorder: F) -> Self
where
F: FnOnce(&mut InspectorInfo),
{
let mut info = InspectorInfo::new();
recorder(&mut info);
Self { name, info }
}
fn is_empty(&self) -> bool {
self.info.is_empty()
}
fn to_record(&self) -> ModifierInspectorRecord {
ModifierInspectorRecord {
name: self.name,
properties: self.info.properties().to_vec(),
}
}
}
fn describe_dimension(constraint: DimensionConstraint) -> String {
match constraint {
DimensionConstraint::Unspecified => "unspecified".to_string(),
DimensionConstraint::Points(value) => value.to_string(),
DimensionConstraint::Fraction(value) => format!("fraction({value})"),
DimensionConstraint::Intrinsic(size) => format!("intrinsic({size:?})"),
}
}
fn inspector_slice(inspector: &Option<Rc<Vec<InspectorMetadata>>>) -> &[InspectorMetadata] {
inspector.as_deref().map_or(&[], Vec::as_slice)
}
fn merged_inspector(
first: &[InspectorMetadata],
second: &[InspectorMetadata],
) -> Option<Rc<Vec<InspectorMetadata>>> {
if first.is_empty() && second.is_empty() {
return None;
}
Some(Rc::new(first.iter().chain(second).cloned().collect()))
}
pub(crate) fn inspector_metadata<F>(name: &'static str, recorder: F) -> InspectorMetadata
where
F: FnOnce(&mut InspectorInfo),
{
if !inspector_metadata_enabled() {
return InspectorMetadata::new(name, |_| {});
}
InspectorMetadata::new(name, recorder)
}
pub(crate) fn modifier_debug_enabled() -> bool {
#[cfg(not(target_arch = "wasm32"))]
{
cranpose_core::DIAGNOSTICS.compose_debug_modifiers.is_set()
}
#[cfg(target_arch = "wasm32")]
{
false
}
}
fn inspector_metadata_enabled() -> bool {
cfg!(any(test, feature = "inspection")) || modifier_debug_enabled()
}
type ModifierElements = SmallVec<[DynModifierElement; 4]>;
#[derive(Clone)]
enum Elements {
One(DynModifierElement),
Many(Rc<ModifierElements>),
}
impl Elements {
fn joined(self, more: Self) -> Self {
let mut chain = match self {
Self::One(first) => {
let mut chain = ModifierElements::new();
chain.push(first);
Rc::new(chain)
}
Self::Many(chain) => chain,
};
match Rc::get_mut(&mut chain) {
Some(owned) => more.move_into(owned),
None => {
let mut copy: ModifierElements = chain.iter().cloned().collect();
more.move_into(&mut copy);
chain = Rc::new(copy);
}
}
Self::Many(chain)
}
fn move_into(self, chain: &mut ModifierElements) {
match self {
Self::One(element) => chain.push(element),
Self::Many(shared) => match Rc::try_unwrap(shared) {
Ok(owned) => chain.extend(owned),
Err(shared) => chain.extend(shared.iter().cloned()),
},
}
}
fn shares_storage(&self, other: &Self) -> bool {
match (self, other) {
(Self::One(a), Self::One(b)) => Rc::ptr_eq(a, b),
(Self::Many(a), Self::Many(b)) => Rc::ptr_eq(a, b),
_ => false,
}
}
}
impl std::ops::Deref for Elements {
type Target = [DynModifierElement];
fn deref(&self) -> &[DynModifierElement] {
match self {
Self::One(element) => std::slice::from_ref(element),
Self::Many(elements) => elements.as_slice(),
}
}
}
#[derive(Clone)]
enum ModifierKind {
Empty,
Single {
elements: Elements,
inspector: Option<Rc<Vec<InspectorMetadata>>>,
},
}
pub(crate) struct ModifierElementIterator<'a> {
inner: std::slice::Iter<'a, DynModifierElement>,
}
impl<'a> Iterator for ModifierElementIterator<'a> {
type Item = &'a DynModifierElement;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
}
impl ExactSizeIterator for ModifierElementIterator<'_> {}
pub(crate) struct ModifierInspectorIterator<'a> {
inner: std::slice::Iter<'a, InspectorMetadata>,
}
impl<'a> Iterator for ModifierInspectorIterator<'a> {
type Item = &'a InspectorMetadata;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
}
impl ExactSizeIterator for ModifierInspectorIterator<'_> {}
#[derive(Clone)]
#[must_use = "a modifier does nothing until a composable receives it"]
pub struct Modifier {
kind: ModifierKind,
provides_composition_locals: bool,
}
impl Default for Modifier {
fn default() -> Self {
Self {
kind: ModifierKind::Empty,
provides_composition_locals: false,
}
}
}
impl Modifier {
pub fn empty() -> Self {
Self::default()
}
pub fn from_element<E>(element: E) -> Self
where
E: ModifierNodeElement,
{
Self::with_element(element)
}
pub fn clip_to_bounds(self) -> Self {
let modifier = crate::thread_state::with(|state| {
state
.clip_to_bounds
.get_or_init(|| Self::from_dyn_element(plain_element(ClipToBoundsElement)))
.clone()
});
let modifier =
modifier.with_inspector_metadata(inspector_metadata("clipToBounds", |info| {
info.add_property("clipToBounds", "true");
}));
self.then(modifier)
}
pub fn modifier_local_provider<T, F>(self, key: ModifierLocalKey<T>, value: F) -> Self
where
T: 'static,
F: Fn() -> T + 'static,
{
let element = ModifierLocalProviderElement::new(key, value);
let modifier = Self::from_dyn_element(modifier_element(element));
self.then(modifier)
}
pub fn modifier_local_consumer<F>(self, consumer: F) -> Self
where
F: for<'scope> Fn(&mut ModifierLocalReadScope<'scope>) + 'static,
{
let element = ModifierLocalConsumerElement::new(consumer);
let modifier = Self::from_dyn_element(modifier_element(element));
self.then(modifier)
}
pub fn semantics_spec(self, spec: cranpose_foundation::SemanticsSpec) -> Self {
self.stable_semantics(move |config: &mut SemanticsConfiguration| config.merge(&spec))
}
pub fn semantics<F>(self, recorder: F) -> Self
where
F: Fn(&mut SemanticsConfiguration) + 'static,
{
self.semantics_recorder(Rc::new(recorder), true)
}
pub fn stable_semantics<F>(self, recorder: F) -> Self
where
F: Fn(&mut SemanticsConfiguration) + 'static,
{
self.semantics_recorder(Rc::new(recorder), false)
}
fn semantics_recorder(
self,
recorder: Rc<dyn Fn(&mut SemanticsConfiguration)>,
reads_live_state: bool,
) -> Self {
let metadata = if inspector_metadata_enabled() {
let mut preview = SemanticsConfiguration::default();
recorder(&mut preview);
let description = preview.content_description.clone();
let state_description = preview.state_description.clone();
let role = preview.role;
let is_clickable = preview.is_activatable();
let canvas_children = preview.canvas_children.len();
inspector_metadata("semantics", move |info| {
if let Some(desc) = &description {
info.add_property("contentDescription", desc.clone());
}
if let Some(state) = &state_description {
info.add_property("stateDescription", state.clone());
}
if let Some(role) = role {
info.add_property("role", format!("{role:?}"));
}
if is_clickable {
info.add_property("isClickable", "true");
}
if canvas_children > 0 {
info.add_property("canvasSemanticsChildren", canvas_children.to_string());
}
})
} else {
inspector_metadata("semantics", |_| {})
};
let element = SemanticsElement::new(recorder, reads_live_state);
let modifier =
Self::from_dyn_element(modifier_element(element)).with_inspector_metadata(metadata);
self.then(modifier)
}
pub fn progress_semantics(self, current: f32, start: f32, end: f32, steps: u32) -> Self {
let info = ProgressBarRangeInfo::new(current, start, end, steps);
self.stable_semantics(move |config| config.progress = Some(info))
}
pub fn dropdown_list(self) -> Self {
self.role(cranpose_foundation::SemanticsWidgetRole::DropdownList)
}
pub fn value_picker(self) -> Self {
self.role(cranpose_foundation::SemanticsWidgetRole::ValuePicker)
}
pub fn expand(self, action: impl Fn() -> bool + 'static) -> Self {
let action = cranpose_foundation::SemanticsExpand::new(action);
self.stable_semantics(move |config| config.expand = Some(action.clone()))
}
pub fn on_long_click(
self,
label: impl Into<String>,
action: impl Fn() -> bool + 'static,
) -> Self {
let label = label.into();
let action = cranpose_foundation::SemanticsLongClick::new(action);
self.stable_semantics(move |config| {
config.on_long_click_label = Some(label.clone());
config.on_long_click = Some(action.clone());
})
}
pub fn on_magic_tap(
self,
label: impl Into<String>,
action: impl Fn() -> bool + 'static,
) -> Self {
let label = label.into();
let action = cranpose_foundation::SemanticsMagicTap::new(action);
self.stable_semantics(move |config| {
config.on_magic_tap_label = Some(label.clone());
config.on_magic_tap = Some(action.clone());
})
}
pub fn input_labels<S: Into<String>>(self, labels: impl IntoIterator<Item = S>) -> Self {
let labels: Vec<String> = labels.into_iter().map(Into::into).collect();
self.stable_semantics(move |config| config.input_labels.clone_from(&labels))
}
pub fn language(self, tag: impl Into<String>) -> Self {
let tag = tag.into();
self.stable_semantics(move |config| config.language = Some(tag.clone()))
}
pub fn collapse(self, action: impl Fn() -> bool + 'static) -> Self {
let action = cranpose_foundation::SemanticsExpand::new(action);
self.stable_semantics(move |config| config.collapse = Some(action.clone()))
}
pub fn dismiss(self, action: impl Fn() -> bool + 'static) -> Self {
let action = cranpose_foundation::SemanticsDismiss::new(action);
self.stable_semantics(move |config| config.dismiss = Some(action.clone()))
}
pub fn scroll_to_index(self, action: impl Fn(usize) -> bool + 'static) -> Self {
let action = cranpose_foundation::SemanticsScrollToIndex::new(action);
self.stable_semantics(move |config| config.scroll_to_index = Some(action.clone()))
}
pub fn traversal_index(self, index: f32) -> Self {
self.stable_semantics(move |config| config.traversal_index = index)
}
pub fn password(self) -> Self {
self.stable_semantics(|config| config.password = true)
}
pub fn error(self, message: impl Into<String>) -> Self {
let message = message.into();
self.stable_semantics(move |config| config.error = Some(message.clone()))
}
pub fn hint_text(self, hint: impl Into<String>) -> Self {
let hint = hint.into();
self.stable_semantics(move |config| config.hint_text = Some(hint.clone()))
}
pub fn pane_title(self, title: impl Into<String>) -> Self {
let title = title.into();
self.stable_semantics(move |config| config.pane_title = Some(title.clone()))
}
pub fn selectable_group(self) -> Self {
self.stable_semantics(|config| config.selectable_group = true)
}
pub fn merge_descendants(self) -> Self {
self.stable_semantics(|config| config.merge_descendants = true)
}
pub fn hide_from_accessibility(self) -> Self {
self.stable_semantics(|config| config.hidden = true)
}
pub fn heading(self) -> Self {
self.role(cranpose_foundation::SemanticsWidgetRole::Header)
}
pub fn role(self, role: cranpose_foundation::SemanticsWidgetRole) -> Self {
self.stable_semantics(move |config| config.role = Some(role))
}
pub fn live_region(self, mode: cranpose_foundation::LiveRegionMode) -> Self {
self.stable_semantics(move |config| config.live_region = Some(mode))
}
pub fn content_description(self, description: impl Into<String>) -> Self {
let description = description.into();
self.stable_semantics(move |config| config.content_description = Some(description.clone()))
}
pub fn test_tag(self, tag: impl Into<String>) -> Self {
let tag = tag.into();
self.stable_semantics(move |config| config.test_tag = Some(tag.clone()))
}
pub fn semantics_requester(self, requester: &SemanticsRequester) -> Self {
let element = SemanticsRequesterElement::new(requester.clone());
let modifier = Self::from_dyn_element(modifier_element(element));
self.then(modifier)
}
pub fn debug_chain(self, tag: &'static str) -> Self {
use cranpose_foundation::{ModifierNode, ModifierNodeContext, NodeCapabilities, NodeState};
#[derive(Clone)]
struct DebugChainElement {
tag: &'static str,
}
impl fmt::Debug for DebugChainElement {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DebugChainElement")
.field("tag", &self.tag)
.finish()
}
}
impl PartialEq for DebugChainElement {
fn eq(&self, other: &Self) -> bool {
self.tag == other.tag
}
}
impl Eq for DebugChainElement {}
impl std::hash::Hash for DebugChainElement {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.tag.hash(state);
}
}
impl ModifierNodeElement for DebugChainElement {
type Node = DebugChainNode;
fn create(&self) -> Self::Node {
DebugChainNode::new(self.tag)
}
fn update(&self, node: &mut Self::Node) {
node.tag = self.tag;
}
fn capabilities(&self) -> NodeCapabilities {
NodeCapabilities::empty()
}
}
struct DebugChainNode {
tag: &'static str,
state: NodeState,
}
impl DebugChainNode {
fn new(tag: &'static str) -> Self {
Self {
tag,
state: NodeState::new(),
}
}
}
#[expect(clippy::print_stderr, reason = "debug_chain prints its lifecycle")]
impl ModifierNode for DebugChainNode {
fn on_attach(&mut self, _context: &mut dyn ModifierNodeContext) {
eprintln!("[debug_chain:{}] Modifier chain attached", self.tag);
}
fn on_detach(&mut self) {
eprintln!("[debug_chain:{}] Modifier chain detached", self.tag);
}
fn on_reset(&mut self) {
eprintln!("[debug_chain:{}] Modifier chain reset", self.tag);
}
}
impl cranpose_foundation::DelegatableNode for DebugChainNode {
fn node_state(&self) -> &NodeState {
&self.state
}
}
let element = DebugChainElement { tag };
let modifier = Self::from_dyn_element(modifier_element(element));
self.then(modifier)
.with_inspector_metadata(inspector_metadata("debugChain", move |info| {
info.add_property("tag", tag);
}))
}
pub fn then(self, next: Self) -> Self {
let ModifierKind::Single {
elements,
inspector,
} = self.kind
else {
return next;
};
let ModifierKind::Single {
elements: next_elements,
inspector: next_inspector,
} = next.kind
else {
return Self {
kind: ModifierKind::Single {
elements,
inspector,
},
..self
};
};
let inspector = if next_inspector.is_none() {
inspector
} else {
merged_inspector(
inspector_slice(&inspector),
inspector_slice(&next_inspector),
)
};
Self {
kind: ModifierKind::Single {
elements: elements.joined(next_elements),
inspector,
},
provides_composition_locals: self.provides_composition_locals
|| next.provides_composition_locals,
}
}
pub(crate) fn element_count(&self) -> usize {
match &self.kind {
ModifierKind::Empty => 0,
ModifierKind::Single { elements, .. } => elements.len(),
}
}
pub(crate) fn iter_elements(&self) -> ModifierElementIterator<'_> {
ModifierElementIterator {
inner: self.as_elements().iter(),
}
}
pub(crate) fn provided_composition_locals(&self) -> Vec<ProvidedValue> {
if !self.provides_composition_locals {
return Vec::new();
}
self.iter_elements()
.flat_map(|element| element.provided_composition_locals())
.collect()
}
pub(crate) fn iter_inspector_metadata(&self) -> ModifierInspectorIterator<'_> {
match &self.kind {
ModifierKind::Empty => ModifierInspectorIterator { inner: [].iter() },
ModifierKind::Single { inspector, .. } => ModifierInspectorIterator {
inner: inspector_slice(inspector).iter(),
},
}
}
#[cfg(test)]
pub(crate) fn elements(&self) -> Vec<DynModifierElement> {
match &self.kind {
ModifierKind::Empty => Vec::new(),
ModifierKind::Single { elements, .. } => elements.to_vec(),
}
}
pub(crate) fn inspector_metadata(&self) -> Vec<InspectorMetadata> {
match &self.kind {
ModifierKind::Empty => Vec::new(),
ModifierKind::Single { inspector, .. } => {
inspector.as_deref().cloned().unwrap_or_default()
}
}
}
pub fn total_padding(&self) -> f32 {
let padding = self.padding_values();
padding
.left
.max(padding.right)
.max(padding.top)
.max(padding.bottom)
}
pub fn explicit_size(&self) -> Option<Size> {
let props = self.layout_properties();
match (props.width, props.height) {
(DimensionConstraint::Points(width), DimensionConstraint::Points(height)) => {
Some(Size { width, height })
}
_ => None,
}
}
pub fn padding_values(&self) -> EdgeInsets {
self.resolved_modifiers().padding()
}
pub(crate) fn layout_properties(&self) -> LayoutProperties {
self.resolved_modifiers().layout_properties()
}
pub fn box_alignment(&self) -> Option<Alignment> {
self.layout_properties().box_alignment()
}
pub fn column_alignment(&self) -> Option<HorizontalAlignment> {
self.layout_properties().column_alignment()
}
pub fn row_alignment(&self) -> Option<VerticalAlignment> {
self.layout_properties().row_alignment()
}
pub fn draw_commands(&self) -> Vec<DrawCommand> {
collect_slices_from_modifier(self).draw_commands().to_vec()
}
pub fn clips_to_bounds(&self) -> bool {
collect_slices_from_modifier(self).clip_to_bounds()
}
pub fn collect_inspector_records(&self) -> Vec<ModifierInspectorRecord> {
self.inspector_metadata()
.iter()
.map(InspectorMetadata::to_record)
.collect()
}
pub fn resolved_modifiers(&self) -> ResolvedModifiers {
let mut handle = ModifierChainHandle::new();
let _ = handle.update(self);
handle.resolved_modifiers()
}
pub fn with_element<E>(element: E) -> Self
where
E: ModifierNodeElement,
{
Self::from_dyn_element(modifier_element(element))
}
pub(crate) fn from_dyn_element(element: DynModifierElement) -> Self {
Self {
provides_composition_locals: element.provides_composition_locals(),
kind: ModifierKind::Single {
elements: Elements::One(element),
inspector: None,
},
}
}
pub(crate) fn with_inspector_metadata(self, metadata: InspectorMetadata) -> Self {
if metadata.is_empty() {
return self;
}
match self.kind {
ModifierKind::Empty => self,
ModifierKind::Single {
elements,
inspector,
} => {
let mut list = inspector.as_deref().cloned().unwrap_or_default();
list.push(metadata);
let new_inspector = Rc::new(list);
Self {
kind: ModifierKind::Single {
elements,
inspector: Some(new_inspector),
},
provides_composition_locals: self.provides_composition_locals,
}
}
}
}
pub fn structural_eq(&self, other: &Self) -> bool {
element_match::modifiers_match(self, other, ElementMatch::Structural)
}
fn as_elements(&self) -> &[DynModifierElement] {
match &self.kind {
ModifierKind::Empty => &[],
ModifierKind::Single { elements, .. } => elements,
}
}
}
impl PartialEq for Modifier {
fn eq(&self, other: &Self) -> bool {
element_match::modifiers_match(self, other, ElementMatch::Equal)
}
}
impl Eq for Modifier {}
impl fmt::Display for Modifier {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.kind {
ModifierKind::Empty => write!(f, "Modifier.empty"),
ModifierKind::Single { elements, .. } => {
if elements.is_empty() {
return write!(f, "Modifier.empty");
}
write!(f, "Modifier[")?;
for (index, element) in elements.iter().enumerate() {
if index > 0 {
write!(f, ", ")?;
}
let name = element.inspector_name();
let mut properties = Vec::new();
element.record_inspector_properties(&mut |prop, value| {
properties.push(format!("{prop}={value}"));
});
if properties.is_empty() {
write!(f, "{name}")?;
} else {
write!(f, "{name}({})", properties.join(", "))?;
}
}
write!(f, "]")
}
}
}
}
impl fmt::Debug for Modifier {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
#[cfg(test)]
#[path = "tests/modifier_tests.rs"]
mod tests;