1use std::ops::Deref;
4
5use waft_foundation::App;
6use waft_widgets::{BuildContext, InheritedTheme, InheritedWidget, IntoWidget, KeyRef, WidgetRef};
7
8use crate::menu_style::MenuStyle;
9use crate::menu_theme::MenuThemeData;
10use crate::theme::Theme;
11
12#[derive(Clone, Debug, Default, PartialEq)]
40pub struct MenuBarThemeData {
41 menu_theme_data: MenuThemeData,
42}
43
44impl MenuBarThemeData {
45 pub fn new() -> MenuBarThemeData {
47 MenuBarThemeData::default()
48 }
49
50 pub fn style(mut self, style: MenuStyle) -> MenuBarThemeData {
52 self.menu_theme_data.style = Some(style);
53 self
54 }
55
56 pub fn lerp(
58 a: Option<&MenuBarThemeData>,
59 b: Option<&MenuBarThemeData>,
60 t: f64,
61 ) -> Option<MenuBarThemeData> {
62 match (a, b) {
63 (None, None) => return None,
64 (Some(a), Some(b)) if std::ptr::eq(a, b) => return Some(a.clone()),
65 _ => {}
66 }
67 let mut data = MenuBarThemeData::new();
68 if let Some(style) = MenuStyle::lerp(
69 a.and_then(|a| a.style.as_ref()),
70 b.and_then(|b| b.style.as_ref()),
71 t,
72 ) {
73 data = data.style(style);
74 }
75 Some(data)
76 }
77}
78
79impl Deref for MenuBarThemeData {
80 type Target = MenuThemeData;
81
82 fn deref(&self) -> &MenuThemeData {
83 &self.menu_theme_data
84 }
85}
86
87#[derive(Debug)]
108pub struct MenuBarTheme {
109 pub key: Option<KeyRef>,
110 pub data: MenuBarThemeData,
112 pub child: WidgetRef,
113}
114
115impl MenuBarTheme {
116 #[cfg_attr(debug_assertions, track_caller)]
119 pub fn new<K>(data: MenuBarThemeData, child: impl IntoWidget<K>) -> MenuBarTheme {
120 MenuBarTheme {
121 key: None,
122 data,
123 child: child.into_widget(),
124 }
125 }
126
127 pub fn key(mut self, key: KeyRef) -> MenuBarTheme {
129 self.key = Some(key);
130 self
131 }
132
133 pub fn of(app: &mut App, context: BuildContext) -> MenuBarThemeData {
143 match context.depend_on_inherited_widget_of_exact_type::<MenuBarTheme>(app) {
144 Some(menu_bar_theme) => menu_bar_theme.data.clone(),
145 None => Theme::of(app, context).menu_bar_theme.clone(),
146 }
147 }
148}
149
150impl InheritedTheme for MenuBarTheme {
151 fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
152 MenuBarTheme::new(self.data.clone(), child).into_widget()
153 }
154}
155
156impl InheritedWidget for MenuBarTheme {
157 fn key(&self) -> Option<&KeyRef> {
158 self.key.as_ref()
159 }
160
161 fn child(&self) -> &WidgetRef {
162 &self.child
163 }
164
165 fn update_should_notify(&self, old_widget: &MenuBarTheme) -> bool {
166 self.data != old_widget.data
167 }
168
169 fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
170 Some(self)
171 }
172}
173
174impl waft_foundation::Diagnosticable for MenuBarThemeData {}
175
176impl std::fmt::Display for MenuBarThemeData {
177 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
178 f.write_str(&waft_foundation::Diagnosticable::to_string(self))
179 }
180}
181
182#[cfg(test)]
183mod tests {
184
185 use waft_embedder::Rect;
186 use waft_rendering::BoxConstraints;
187 use waft_widgets::{Axis, Builder, Column, ConstrainedBox, Expanded};
188
189 use waft_widgets::testing::{WidgetTester, find, test_widgets};
190
191 use super::*;
192 use crate::colors::Colors;
193 use crate::material::Material;
194 use crate::menu_anchor::MenuBar;
195 use crate::menu_theme::MenuTheme;
196 use crate::menu_theme::tests::{
197 ThemeTestMenus, assert_constructor_parameters_override_theme_parameters, green_menu_style,
198 menu_panels, overriding_bar, panel_material, red_stadium_menu_style, test_menu,
199 theme_test_menus,
200 };
201 use crate::test_support::material_app;
202 use crate::tooltip::tests::m2;
203
204 fn pump_themed_bar(
207 tester: &mut WidgetTester,
208 menu_theme: MenuThemeData,
209 bar_theme: MenuBarThemeData,
210 bar: impl Fn() -> WidgetRef + 'static,
211 ) {
212 let root = {
213 let app = tester.cell.borrow();
214 material_app(
215 m2(&app),
216 Material::new()
217 .child(Builder::new(move |_app, _context| {
218 MenuTheme::new(
219 menu_theme.clone(),
220 MenuBarTheme::new(
221 bar_theme.clone(),
222 Column::new().children(vec![
223 bar(),
224 Expanded::new(ConstrainedBox::new(BoxConstraints::expand(
225 None, None,
226 )))
227 .into_widget(),
228 ]),
229 ),
230 )
231 .into_widget()
232 }))
233 .into_widget(),
234 )
235 };
236 tester.pump_widget(root);
237 }
238
239 #[test]
241 fn lerp_of_nothing_is_nothing_and_of_one_theme_with_itself_is_that_theme() {
242 assert!(MenuBarThemeData::lerp(None, None, 0.0).is_none());
243 let data = MenuBarThemeData::new();
244 assert_eq!(
245 MenuBarThemeData::lerp(Some(&data), Some(&data), 0.5),
246 Some(data)
247 );
248 }
249
250 #[test]
252 fn a_menu_bar_theme_styles_the_bar_and_a_menu_theme_the_submenus() {
253 test_widgets(|tester| {
254 pump_themed_bar(
255 tester,
256 MenuThemeData::new().style(green_menu_style()),
257 MenuBarThemeData::new().style(red_stadium_menu_style()),
258 || MenuBar::new(theme_test_menus(ThemeTestMenus::default())).into_widget(),
259 );
260
261 tester.tap(find::text(test_menu::MAIN_MENU_1));
263 tester.pump();
264 assert_eq!(
265 tester.get_rect(menu_panels(Axis::Horizontal).first()),
266 Rect::from_ltrb(228.0, 0.0, 572.0, 68.0)
267 );
268 panel_material(tester, Axis::Horizontal, |material| {
269 assert_eq!(material.elevation, 15.0);
270 assert_eq!(material.color, Some(Colors::RED.primary()));
271 });
272
273 assert_eq!(
274 tester.get_rect(menu_panels(Axis::Vertical)),
275 Rect::from_ltrb(346.0, 58.0, 560.0, 218.0)
276 );
277 panel_material(tester, Axis::Vertical, |material| {
278 assert_eq!(material.elevation, 20.0);
279 assert_eq!(material.color, Some(Colors::GREEN.primary()));
280 });
281 });
282 }
283
284 #[test]
286 fn a_menu_bars_and_its_buttons_own_styles_override_the_themes() {
287 test_widgets(|tester| {
288 pump_themed_bar(
289 tester,
290 MenuThemeData::new().style(green_menu_style()),
291 MenuBarThemeData::new().style(red_stadium_menu_style()),
292 overriding_bar,
293 );
294
295 tester.tap(find::text(test_menu::MAIN_MENU_1));
297 tester.pump();
298
299 assert_constructor_parameters_override_theme_parameters(tester);
300 });
301 }
302}