Skip to main content

ferroui_diagnostics/diagnostics/view_models/
resource_setter_view_model.rs

1//! Port of `Diagnostics/ViewModels/ResourceSetterViewModel.cs`.
2
3use super::SetterViewModel;
4use ferroui_base::data::core::ValueTypes;
5use ferroui_base::data::model::{Event, INotifyPropertyChanged};
6use ferroui_base::input::platform::IClipboard;
7use ferroui_base::media::{Brushes, IBrush};
8use ferroui_base::{ferro_markup_type, BoxedValue, FerroProperty};
9use std::ops::Deref;
10use std::rc::Rc;
11
12/// A setter whose value is a resource: the key of the resource and the value it has for the
13/// control, with a tint and a tooltip that tell a dynamic resource from a static one.
14pub struct ResourceSetterViewModel {
15    base: SetterViewModel,
16    key: BoxedValue,
17    tint: Rc<dyn IBrush>,
18    value_type_tooltip: String,
19}
20
21impl PartialEq for ResourceSetterViewModel {
22    fn eq(&self, other: &Self) -> bool {
23        std::ptr::eq(self, other)
24    }
25}
26
27impl Deref for ResourceSetterViewModel {
28    type Target = SetterViewModel;
29
30    fn deref(&self) -> &SetterViewModel {
31        &self.base
32    }
33}
34
35impl INotifyPropertyChanged for ResourceSetterViewModel {
36    fn property_changed(&self) -> &Event<str> {
37        self.base.property_changed()
38    }
39}
40
41impl ResourceSetterViewModel {
42    pub fn new(
43        property: &'static FerroProperty,
44        resource_key: BoxedValue,
45        resource_value: Option<BoxedValue>,
46        is_dynamic: bool,
47        clipboard: Option<Rc<dyn IClipboard>>,
48    ) -> Rc<Self> {
49        let tint: Rc<dyn IBrush> = if is_dynamic { Brushes::orange() } else { Brushes::brown() };
50
51        Rc::new(Self {
52            base: SetterViewModel::construct(property, resource_value, clipboard),
53            key: resource_key,
54            tint,
55            value_type_tooltip: if is_dynamic { "Dynamic Resource" } else { "Static Resource" }.to_string(),
56        })
57    }
58
59    pub fn key(&self) -> BoxedValue {
60        self.key.clone()
61    }
62
63    pub fn tint(&self) -> Rc<dyn IBrush> {
64        self.tint.clone()
65    }
66
67    pub fn value_type_tooltip(&self) -> String {
68        self.value_type_tooltip.clone()
69    }
70
71    /// The `CopyValue` of the base class (this class does not override it).
72    pub fn copy_value(&self) {
73        self.base.copy_value();
74    }
75
76    pub fn copy_resource_key(&self) {
77        // `Key?.ToString()`: the key is never null.
78        let text_to_copy = ValueTypes::to_display_string(Some(&self.key));
79
80        self.base.copy_to_clipboard(&text_to_copy);
81    }
82}
83
84ferro_markup_type!(class ResourceSetterViewModel {
85    this: Rc<ResourceSetterViewModel>,
86    handles: [ResourceSetterViewModel, Rc<ResourceSetterViewModel>, Option<Rc<ResourceSetterViewModel>>],
87    base: SetterViewModel,
88    properties: [
89        Property: &'static FerroProperty { get: |this: &Rc<ResourceSetterViewModel>| this.property() },
90        Name: String { get: |this: &Rc<ResourceSetterViewModel>| this.name() },
91        Value: Option<BoxedValue> { get: |this: &Rc<ResourceSetterViewModel>| this.value() },
92        IsActive: bool {
93            get: |this: &Rc<ResourceSetterViewModel>| this.is_active(),
94            set: |this: &Rc<ResourceSetterViewModel>, value: bool| this.set_is_active(value)
95        },
96        IsVisible: bool {
97            get: |this: &Rc<ResourceSetterViewModel>| this.is_visible(),
98            set: |this: &Rc<ResourceSetterViewModel>, value: bool| this.set_is_visible(value)
99        },
100        Key: Option<BoxedValue> { get: |this: &Rc<ResourceSetterViewModel>| Some(this.key()) },
101        Tint: Rc<dyn IBrush> { get: ResourceSetterViewModel::tint },
102        ValueTypeTooltip: String { get: ResourceSetterViewModel::value_type_tooltip },
103    ],
104    methods: [
105        fn CopyValue() => ResourceSetterViewModel::copy_value,
106        fn CopyPropertyName() => |this: &Rc<ResourceSetterViewModel>| this.copy_property_name(),
107        fn CopyResourceKey() => ResourceSetterViewModel::copy_resource_key,
108    ],
109    notify_property_changed: ResourceSetterViewModel,
110});