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
// Copyright 2026 the Xilem Authors
// SPDX-License-Identifier: Apache-2.0
use std::{any::TypeId, collections::HashMap, sync::Arc};
use crate::core::{DefaultProperties, PropertyStack, PropertyStackId};
/// An arena for storing [`PropertyStack`]s, which represent cascading sets of properties that can be applied to widgets.
#[derive(Default, Debug)]
pub struct PropertyArena {
pub(crate) arena: HashMap<PropertyStackId, PropertyStack>,
/// Default values that properties will have if not defined per-widget.
pub(crate) default_properties: Arc<DefaultProperties>,
}
impl PropertyArena {
/// Creates an empty `PropertyArena` with the given default properties.
pub fn new(default_properties: Arc<DefaultProperties>) -> Self {
Self {
arena: HashMap::new(),
default_properties,
}
}
/// Inserts a `PropertyStack` into the arena and returns its unique `PropertyStackId`.
pub fn insert(&mut self, stack: PropertyStack) -> PropertyStackId {
let id = PropertyStackId::next();
self.arena.insert(id, stack);
id
}
/// Retrieves a reference to a `PropertyStack` by its `PropertyStackId`.
///
/// If the `id` is `None` or not found in the arena, returns the default property stack for the given `widget_type` defined in [`DefaultProperties`].
///
/// If no default property stack is defined for the `widget_type`, returns an empty `PropertyStack`.
pub fn get(&self, id: Option<PropertyStackId>, widget_type: TypeId) -> &PropertyStack {
if let Some(id) = id
&& let Some(stack) = self.arena.get(&id)
{
stack
} else {
self.default_properties.stack_for_widget(widget_type)
}
}
}