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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//! Flutter counterpart: material_ui's `menu_bar_theme.dart`.
use std::ops::Deref;
use waft_foundation::App;
use waft_widgets::{BuildContext, InheritedTheme, InheritedWidget, IntoWidget, KeyRef, WidgetRef};
use crate::menu_style::MenuStyle;
use crate::menu_theme::MenuThemeData;
use crate::theme::Theme;
/// A data class that [`MenuBarTheme`] uses to define the visual properties of
/// `MenuBar` widgets.
///
/// This class defines the visual properties of `MenuBar` widgets themselves,
/// but not their submenus. Those properties are defined by [`MenuThemeData`] or
/// `MenuButtonThemeData` instead.
///
/// Descendant widgets obtain the current [`MenuBarThemeData`] object using
/// [`MenuBarTheme::of`].
///
/// Typically, a [`MenuBarThemeData`] is specified as part of the overall `Theme`
/// with `ThemeData::menu_bar_theme`. Otherwise, `MenuTheme` can be used to
/// configure its own widget subtree.
///
/// All [`MenuBarThemeData`] properties are `null` by default. If any of these
/// properties are null, the menu bar will provide its own defaults.
///
/// Dart's `MenuBarThemeData extends MenuThemeData`: it holds the [`MenuThemeData`] it
/// derefs to, whose `submenu_icon` its constructor never sets.
///
/// See also:
///
/// * [`MenuThemeData`], which describes the theme for the submenus of a
/// `MenuBar`.
/// * `MenuButtonThemeData`, which describes the theme for the `MenuItemButton`s
/// in a menu.
/// * `ThemeData`, which describes the overall theme for the application.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct MenuBarThemeData {
menu_theme_data: MenuThemeData,
}
impl MenuBarThemeData {
/// Creates a const set of properties used to configure `MenuTheme`.
pub fn new() -> MenuBarThemeData {
MenuBarThemeData::default()
}
/// Dart `MenuBarThemeData(style:)`.
pub fn style(mut self, style: MenuStyle) -> MenuBarThemeData {
self.menu_theme_data.style = Some(style);
self
}
/// Linearly interpolate between two `MenuBar` themes.
pub fn lerp(
a: Option<&MenuBarThemeData>,
b: Option<&MenuBarThemeData>,
t: f64,
) -> Option<MenuBarThemeData> {
match (a, b) {
(None, None) => return None,
(Some(a), Some(b)) if std::ptr::eq(a, b) => return Some(a.clone()),
_ => {}
}
let mut data = MenuBarThemeData::new();
if let Some(style) = MenuStyle::lerp(
a.and_then(|a| a.style.as_ref()),
b.and_then(|b| b.style.as_ref()),
t,
) {
data = data.style(style);
}
Some(data)
}
}
impl Deref for MenuBarThemeData {
type Target = MenuThemeData;
fn deref(&self) -> &MenuThemeData {
&self.menu_theme_data
}
}
/// An inherited widget that defines the configuration for the `MenuBar` widgets
/// in this widget's descendants.
///
/// This class defines the visual properties of `MenuBar` widgets themselves,
/// but not their submenus. Those properties are defined by `MenuTheme` or
/// `MenuButtonTheme` instead.
///
/// Values specified here are used for `MenuBar`'s properties that are not given
/// an explicit non-null value.
///
/// See also:
/// * [`MenuStyle`], a configuration object that holds attributes of a menu, and
/// is used by this theme to define those attributes.
/// * `MenuTheme`, which does the same thing for the menus created by a
/// `SubmenuButton` or `MenuAnchor`.
/// * `MenuButtonTheme`, which does the same thing for the `MenuItemButton`s
/// inside of the menus.
/// * `SubmenuButton`, a button that manages a submenu that uses these
/// properties.
/// * `MenuBar`, a widget that creates a menu bar that can use `SubmenuButton`s.
#[derive(Debug)]
pub struct MenuBarTheme {
pub key: Option<KeyRef>,
/// The properties to set for `MenuBar` in this widget's descendants.
pub data: MenuBarThemeData,
pub child: WidgetRef,
}
impl MenuBarTheme {
/// Creates a theme that controls the configurations for `MenuBar` and
/// `MenuItemButton` in its widget subtree.
pub fn new<K>(data: MenuBarThemeData, child: impl IntoWidget<K>) -> MenuBarTheme {
MenuBarTheme {
key: None,
data,
child: child.into_widget(),
}
}
/// Dart `MenuBarTheme(key:)`.
pub fn key(mut self, key: KeyRef) -> MenuBarTheme {
self.key = Some(key);
self
}
/// Returns the closest instance of this class's [`data`](Self::data) value that encloses
/// the given context. If there is no ancestor, it returns
/// `ThemeData::menu_bar_theme`.
///
/// Typical usage is as follows:
///
/// ```text
/// let theme = MenuBarTheme::of(app, context);
/// ```
pub fn of(app: &mut App, context: BuildContext) -> MenuBarThemeData {
match context.depend_on_inherited_widget_of_exact_type::<MenuBarTheme>(app) {
Some(menu_bar_theme) => menu_bar_theme.data.clone(),
None => Theme::of(app, context).menu_bar_theme.clone(),
}
}
}
impl InheritedTheme for MenuBarTheme {
fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
MenuBarTheme::new(self.data.clone(), child).into_widget()
}
}
impl InheritedWidget for MenuBarTheme {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn child(&self) -> &WidgetRef {
&self.child
}
fn update_should_notify(&self, old_widget: &MenuBarTheme) -> bool {
self.data != old_widget.data
}
fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
Some(self)
}
}
#[cfg(test)]
mod tests {
use waft_embedder::Rect;
use waft_rendering::BoxConstraints;
use waft_widgets::{Axis, Builder, Column, ConstrainedBox, Expanded};
use waft_widgets::testing::{WidgetTester, find, test_widgets};
use super::*;
use crate::colors::Colors;
use crate::material::Material;
use crate::menu_anchor::MenuBar;
use crate::menu_theme::MenuTheme;
use crate::menu_theme::tests::{
ThemeTestMenus, assert_constructor_parameters_override_theme_parameters, green_menu_style,
menu_panels, overriding_bar, panel_material, red_stadium_menu_style, test_menu,
theme_test_menus,
};
use crate::test_support::material_app;
use crate::tooltip::tests::m2;
/// The bar of `menu_bar_theme_test.dart`'s two widget cases: `MenuTheme(menu_style)` over
/// `MenuBarTheme(bar_style)` over `Column([bar, Expanded(Placeholder())])`.
fn pump_themed_bar(
tester: &mut WidgetTester,
menu_theme: MenuThemeData,
bar_theme: MenuBarThemeData,
bar: impl Fn() -> WidgetRef + 'static,
) {
let root = {
let app = tester.cell.borrow();
material_app(
m2(&app),
Material::new()
.child(Builder::new(move |_app, _context| {
MenuTheme::new(
menu_theme.clone(),
MenuBarTheme::new(
bar_theme.clone(),
Column::new().children(vec![
bar(),
Expanded::new(ConstrainedBox::new(BoxConstraints::expand(
None, None,
)))
.into_widget(),
]),
),
)
.into_widget()
}))
.into_widget(),
)
};
tester.pump_widget(root);
}
// menu_bar_theme_test.dart 'MenuBarThemeData lerp special cases'.
#[test]
fn lerp_of_nothing_is_nothing_and_of_one_theme_with_itself_is_that_theme() {
assert!(MenuBarThemeData::lerp(None, None, 0.0).is_none());
let data = MenuBarThemeData::new();
assert_eq!(
MenuBarThemeData::lerp(Some(&data), Some(&data), 0.5),
Some(data)
);
}
// menu_bar_theme_test.dart 'theme is honored'.
#[test]
fn a_menu_bar_theme_styles_the_bar_and_a_menu_theme_the_submenus() {
test_widgets(|tester| {
pump_themed_bar(
tester,
MenuThemeData::new().style(green_menu_style()),
MenuBarThemeData::new().style(red_stadium_menu_style()),
|| MenuBar::new(theme_test_menus(ThemeTestMenus::default())).into_widget(),
);
// Open a test menu.
tester.tap(find::text(test_menu::MAIN_MENU_1));
tester.pump();
assert_eq!(
tester.get_rect(menu_panels(Axis::Horizontal).first()),
Rect::from_ltrb(228.0, 0.0, 572.0, 68.0)
);
panel_material(tester, Axis::Horizontal, |material| {
assert_eq!(material.elevation, 15.0);
assert_eq!(material.color, Some(Colors::RED.primary()));
});
assert_eq!(
tester.get_rect(menu_panels(Axis::Vertical)),
Rect::from_ltrb(346.0, 58.0, 560.0, 218.0)
);
panel_material(tester, Axis::Vertical, |material| {
assert_eq!(material.elevation, 20.0);
assert_eq!(material.color, Some(Colors::GREEN.primary()));
});
});
}
// menu_bar_theme_test.dart 'Constructor parameters override theme parameters'.
#[test]
fn a_menu_bars_and_its_buttons_own_styles_override_the_themes() {
test_widgets(|tester| {
pump_themed_bar(
tester,
MenuThemeData::new().style(green_menu_style()),
MenuBarThemeData::new().style(red_stadium_menu_style()),
overriding_bar,
);
// Open a test menu.
tester.tap(find::text(test_menu::MAIN_MENU_1));
tester.pump();
assert_constructor_parameters_override_theme_parameters(tester);
});
}
}