ferroui_base/
i_data_context_provider.rs1use crate::{BoxedValue, FerroObject, StyledElement, TypeInfo};
2use std::cell::RefCell;
3
4crate::ferro_thread_local! {
5 static PROVIDER_TYPES: RefCell<Vec<&'static TypeInfo>> = const { RefCell::new(Vec::new()) };
6}
7
8pub trait IDataContextProvider {
10 fn data_context(&self) -> Option<BoxedValue>;
12
13 fn set_data_context(&self, value: Option<BoxedValue>);
15}
16
17impl dyn IDataContextProvider {
18 pub fn register_type(type_: &'static TypeInfo) {
24 PROVIDER_TYPES.with_borrow_mut(|types| {
25 if !types.iter().any(|t| std::ptr::eq(*t, type_)) {
26 types.push(type_);
27 }
28 });
29 }
30
31 pub fn is_implemented_by(object: &FerroObject) -> bool {
33 if object.is::<StyledElement>() {
34 return true;
35 }
36 let type_ = object.get_type();
37 PROVIDER_TYPES.with_borrow(|types| types.iter().any(|t| t.is_assignable_from(type_)))
38 }
39}
40
41impl IDataContextProvider for StyledElement {
42 fn data_context(&self) -> Option<BoxedValue> {
43 StyledElement::data_context(self)
44 }
45
46 fn set_data_context(&self, value: Option<BoxedValue>) {
47 StyledElement::set_data_context(self, value)
48 }
49}