waft_widgets/widgets/
icon_theme.rs1use waft_foundation::{App, DiagnosticPropertiesBuilder};
4
5use crate::framework::{BuildContext, InheritedWidget, IntoWidget, KeyRef, WidgetRef};
6use crate::widgets::basic::Builder;
7use crate::widgets::icon_theme_data::IconThemeData;
8use crate::widgets::inherited_theme::InheritedTheme;
9
10#[derive(Debug)]
14pub struct IconTheme {
15 pub key: Option<KeyRef>,
17 pub data: IconThemeData,
19 pub child: WidgetRef,
21}
22
23impl IconTheme {
24 #[cfg_attr(debug_assertions, track_caller)]
26 pub fn new<K>(data: IconThemeData, child: impl IntoWidget<K>) -> IconTheme {
27 IconTheme {
28 key: None,
29 data,
30 child: child.into_widget(),
31 }
32 }
33
34 pub fn key(mut self, key: KeyRef) -> IconTheme {
36 self.key = Some(key);
37 self
38 }
39
40 #[cfg_attr(debug_assertions, track_caller)]
43 pub fn merge<K>(
44 key: Option<KeyRef>,
45 data: IconThemeData,
46 child: impl IntoWidget<K>,
47 ) -> WidgetRef {
48 let child = child.into_widget();
49 Builder::new(move |app, context| {
50 let merged = Self::get_inherited_icon_theme_data(app, context).merge(Some(&data));
51 let icon_theme = IconTheme::new(merged, child.clone());
52 match &key {
53 Some(key) => icon_theme.key(key.clone()),
54 None => icon_theme,
55 }
56 .into_widget()
57 })
58 .into_widget()
59 }
60
61 pub fn of(app: &mut App, context: BuildContext) -> IconThemeData {
79 let icon_theme_data =
80 Self::get_inherited_icon_theme_data(app, context).resolve(app, context);
81 if icon_theme_data.is_concrete() {
82 return icon_theme_data;
83 }
84 let fallback = IconThemeData::fallback();
85 let mut concrete = icon_theme_data.copy_with();
86 concrete.size = icon_theme_data.size.or(fallback.size);
87 concrete.fill = icon_theme_data.fill.or(fallback.fill);
88 concrete.weight = icon_theme_data.weight.or(fallback.weight);
89 concrete.grade = icon_theme_data.grade.or(fallback.grade);
90 concrete.optical_size = icon_theme_data.optical_size.or(fallback.optical_size);
91 concrete.color = icon_theme_data.color.or(fallback.color);
92 concrete.opacity = icon_theme_data.opacity.or(fallback.opacity);
93 concrete.shadows = icon_theme_data.shadows.clone().or(fallback.shadows);
94 concrete.apply_text_scaling = icon_theme_data
95 .apply_text_scaling
96 .or(fallback.apply_text_scaling);
97 concrete
98 }
99
100 fn get_inherited_icon_theme_data(app: &mut App, context: BuildContext) -> IconThemeData {
101 let icon_theme = context.depend_on_inherited_widget_of_exact_type::<IconTheme>(app);
102 icon_theme.map_or_else(IconThemeData::fallback, |icon_theme| {
103 icon_theme.data.clone()
104 })
105 }
106}
107
108impl InheritedWidget for IconTheme {
109 fn debug_fill_properties(&self, _app: &App, properties: &mut DiagnosticPropertiesBuilder) {
110 self.data.debug_fill_properties(properties);
111 }
112
113 fn key(&self) -> Option<&KeyRef> {
114 self.key.as_ref()
115 }
116
117 fn child(&self) -> &WidgetRef {
118 &self.child
119 }
120
121 fn update_should_notify(&self, old_widget: &IconTheme) -> bool {
122 self.data != old_widget.data
123 }
124
125 fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
126 Some(self)
127 }
128}
129
130impl InheritedTheme for IconTheme {
131 fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
132 IconTheme::new(self.data.clone(), child).into_widget()
133 }
134}
135
136#[cfg(test)]
137mod tests {
138 use std::cell::RefCell;
139 use std::rc::Rc;
140 use waft_foundation::AppCell;
141
142 use waft_embedder::Color;
143 use waft_foundation::ValueKey;
144
145 use super::*;
146 use crate::framework::StatelessWidget;
147 use crate::test_harness::Harness;
148 use crate::widgets::basic::SizedBox;
149
150 #[derive(Debug)]
152 struct IconThemeReader {
153 seen: Rc<RefCell<Vec<IconThemeData>>>,
154 }
155
156 impl StatelessWidget for IconThemeReader {
157 fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
158 self.seen.borrow_mut().push(IconTheme::of(app, context));
159 SizedBox::shrink().into_widget()
160 }
161 }
162
163 fn reader() -> (WidgetRef, Rc<RefCell<Vec<IconThemeData>>>) {
165 let seen = Rc::new(RefCell::new(Vec::new()));
166 let reader = IconThemeReader {
167 seen: Rc::clone(&seen),
168 }
169 .into_widget();
170 (reader, seen)
171 }
172
173 fn mount(app: &mut App, child: WidgetRef) -> Harness {
174 let harness = Harness::mount(app, child);
175 harness.pump(app);
176 harness
177 }
178
179 #[test]
180 fn of_returns_the_fallback_outside_any_icon_theme() {
181 let cell = AppCell::new();
182 let mut app = cell.borrow_mut();
183 let (reader, seen) = reader();
184 mount(&mut app, reader);
185 assert_eq!(seen.borrow().as_slice(), [IconThemeData::fallback()]);
186 }
187
188 #[test]
189 fn of_fills_a_partial_theme_from_the_fallback() {
190 let cell = AppCell::new();
191 let mut app = cell.borrow_mut();
192 let (reader, seen) = reader();
193 let partial = IconThemeData::new().size(16.0).opacity(0.5);
194 mount(&mut app, IconTheme::new(partial, reader).into_widget());
195 let concrete = IconThemeData::fallback().size(16.0).opacity(0.5);
196 assert!(concrete.is_concrete());
197 assert_eq!(seen.borrow().as_slice(), [concrete]);
198 }
199
200 #[test]
201 fn a_changed_data_notifies_the_dependent_and_an_equal_one_does_not() {
202 let cell = AppCell::new();
203 let mut app = cell.borrow_mut();
204 let (reader, seen) = reader();
205 let themed = |size: f64| {
206 IconTheme::new(IconThemeData::new().size(size), reader.clone()).into_widget()
207 };
208 let harness = mount(&mut app, themed(16.0));
209 assert_eq!(seen.borrow().len(), 1);
210 assert_eq!(seen.borrow()[0].size, Some(16.0));
211
212 harness.set_child(&mut app, themed(16.0));
213 harness.pump(&mut app);
214 assert_eq!(seen.borrow().len(), 1, "an equal data does not notify");
215
216 harness.set_child(&mut app, themed(20.0));
217 harness.pump(&mut app);
218 assert_eq!(
219 seen.borrow().len(),
220 2,
221 "a changed data rebuilds the dependent"
222 );
223 assert_eq!(seen.borrow()[1].size, Some(20.0));
224 }
225
226 #[test]
227 fn merge_layers_its_data_over_the_ambient_theme() {
228 let cell = AppCell::new();
229 let mut app = cell.borrow_mut();
230 let (reader, seen) = reader();
231 let red = Color::new(0xFFFF0000);
232 let key: KeyRef = ValueKey::new("merged").into();
233 mount(
234 &mut app,
235 IconTheme::new(
236 IconThemeData::new().size(16.0).color(red),
237 IconTheme::merge(Some(key), IconThemeData::new().size(20.0), reader),
238 )
239 .into_widget(),
240 );
241 assert_eq!(
242 seen.borrow().as_slice(),
243 [IconThemeData::fallback().size(20.0).color(red)]
244 );
245 }
246}