1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright 2026 the Xilem Authors
// SPDX-License-Identifier: Apache-2.0
use crate::core::{ClassSet, Property, PropertyCache, PropertySet, PropertyStack};
use crate::util::AnyMap;
/// Reference to a collection of [properties](Property) that a widget has access to.
///
/// Used by the [`Widget`](crate::core::Widget) trait during rendering passes and in some search methods.
#[derive(Clone, Copy, Debug)]
pub struct PropertiesRef<'a> {
pub(crate) local: &'a PropertySet,
pub(crate) default_map: &'a AnyMap,
pub(crate) stack: &'a PropertyStack,
pub(crate) class_set: &'a ClassSet,
}
// TODO - Better document local vs default properties.
impl PropertiesRef<'_> {
/// Returns `true` if the widget has a local property of type `P`.
///
/// Does not check default properties.
pub fn contains<P: Property>(&self) -> bool {
self.local.map.contains::<P>()
}
/// Returns value of property `P`.
///
/// Checks local properties first, then the property stack,
/// then default properties, then [`Property::static_default()`].
pub fn get<P: Property>(&self, cache: &mut PropertyCache) -> &P {
// 1. Local properties
if let Some(p) = self.local.map.get::<P>() {
return p;
}
// 2. Property stack (writes to cache on miss)
if let Some(p) = self.stack.resolve::<P>(cache, self.class_set) {
return p;
}
// 3. Default properties
if let Some(p) = self.default_map.get::<P>() {
return p;
}
// 4. Static default
P::static_default()
}
/// Returns value of property `P`, without updating the cache.
///
/// Useful for cases where you don't have mutable access to the [`PropertyCache`].
pub(crate) fn get_without_saving<P: Property>(&self, cache: &PropertyCache) -> &P {
// 1. Local properties
if let Some(p) = self.local.map.get::<P>() {
return p;
}
// 2. Property stack (reads from cache without writing on miss)
if let Some(p) = self
.stack
.resolve_without_saving::<P>(cache, self.class_set)
{
return p;
}
// 3. Default properties
if let Some(p) = self.default_map.get::<P>() {
return p;
}
// 4. Static default
P::static_default()
}
/// Returns a reference to the local properties for direct access.
pub fn local_properties(&self) -> &PropertySet {
self.local
}
}