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
//! Flutter counterpart: material_ui's `menu_button_theme.dart`.
use waft_foundation::{App, DiagnosticsProperty};
use waft_widgets::{BuildContext, InheritedTheme, InheritedWidget, IntoWidget, KeyRef, WidgetRef};
use crate::button_style::ButtonStyle;
use crate::theme::Theme;
/// A [`ButtonStyle`] theme that overrides the default appearance of
/// `SubmenuButton`s and `MenuItemButton`s.
///
/// Descendant widgets obtain the current [`MenuButtonThemeData`] object
/// using [`MenuButtonTheme::of`].
///
/// A [`MenuButtonThemeData`] is often specified as part of the
/// overall `Theme` with `ThemeData::menu_button_theme`.
///
/// The [`style`](Self::style)'s properties override `MenuItemButton`'s and `SubmenuButton`'s
/// default style, i.e. the [`ButtonStyle`] returned by
/// `MenuItemButton::default_style_of` and `SubmenuButton::default_style_of`. Only the
/// style's non-null property values or resolved non-null
/// `WidgetStateProperty` values are used.
///
/// See also:
///
/// * [`MenuButtonTheme`], the theme which is configured with this class.
/// * `MenuTheme`, the theme used to configure the look of the menus these
/// buttons reside in.
/// * `MenuItemButton::default_style_of` and `SubmenuButton::default_style_of` which
/// return the default [`ButtonStyle`]s for menu buttons.
/// * `MenuItemButton::style_from` and `SubmenuButton::style_from`, which converts
/// simple values into a [`ButtonStyle`] that's consistent with their respective
/// defaults.
/// * `WidgetStateProperty::resolve`, "resolve" a material state property to a
/// simple value based on a set of `WidgetState`s.
/// * `ThemeData::menu_button_theme`, which can be used to override the default
/// [`ButtonStyle`] for `MenuItemButton`s and `SubmenuButton`s below the overall
/// `Theme`.
/// * `MenuAnchor`, a widget which hosts cascading menus.
/// * `MenuBar`, a widget which defines a menu bar of buttons hosting cascading
/// menus.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct MenuButtonThemeData {
/// Overrides for `SubmenuButton` and `MenuItemButton`'s default style.
///
/// Non-null properties or non-null resolved `WidgetStateProperty` values
/// override the [`ButtonStyle`] returned by `SubmenuButton::default_style_of` or
/// `MenuItemButton::default_style_of`.
///
/// If [`style`](Self::style) is null, then this theme doesn't override anything.
pub style: Option<ButtonStyle>,
}
impl MenuButtonThemeData {
/// Creates a [`MenuButtonThemeData`].
///
/// The [`style`](Self::style) may be null.
pub fn new() -> MenuButtonThemeData {
MenuButtonThemeData::default()
}
/// Dart `MenuButtonThemeData(style:)`.
pub fn style(mut self, style: ButtonStyle) -> MenuButtonThemeData {
self.style = Some(style);
self
}
/// Linearly interpolate between two menu button themes.
pub fn lerp(
a: Option<&MenuButtonThemeData>,
b: Option<&MenuButtonThemeData>,
t: f64,
) -> Option<MenuButtonThemeData> {
match (a, b) {
(None, None) => return None,
(Some(a), Some(b)) if std::ptr::eq(a, b) => return Some(a.clone()),
_ => {}
}
Some(MenuButtonThemeData {
style: ButtonStyle::lerp(
a.and_then(|a| a.style.as_ref()),
b.and_then(|b| b.style.as_ref()),
t,
),
})
}
}
/// Overrides the default [`ButtonStyle`] of its `MenuItemButton` and
/// `SubmenuButton` descendants.
///
/// See also:
///
/// * [`MenuButtonThemeData`], which is used to configure this theme.
/// * `MenuTheme`, the theme used to configure the look of the menus themselves.
/// * `MenuItemButton::default_style_of` and `SubmenuButton::default_style_of` which
/// return the default [`ButtonStyle`]s for menu buttons.
/// * `MenuItemButton::style_from` and `SubmenuButton::style_from`, which converts
/// simple values into a [`ButtonStyle`] that's consistent with their respective
/// defaults.
/// * `ThemeData::menu_button_theme`, which can be used to override the default
/// [`ButtonStyle`] for `MenuItemButton`s and `SubmenuButton`s below the overall
/// `Theme`.
#[derive(Debug)]
pub struct MenuButtonTheme {
pub key: Option<KeyRef>,
/// The configuration of this theme.
pub data: MenuButtonThemeData,
pub child: WidgetRef,
}
impl MenuButtonTheme {
/// Create a [`MenuButtonTheme`].
#[cfg_attr(debug_assertions, track_caller)]
pub fn new<K>(data: MenuButtonThemeData, child: impl IntoWidget<K>) -> MenuButtonTheme {
MenuButtonTheme {
key: None,
data,
child: child.into_widget(),
}
}
/// Dart `MenuButtonTheme(key:)`.
pub fn key(mut self, key: KeyRef) -> MenuButtonTheme {
self.key = Some(key);
self
}
/// The closest instance of this class that encloses the given context.
///
/// If there is no enclosing [`MenuButtonTheme`] widget, then
/// `ThemeData::menu_button_theme` is used.
///
/// Typical usage is as follows:
///
/// ```text
/// let theme = MenuButtonTheme::of(app, context);
/// ```
pub fn of(app: &mut App, context: BuildContext) -> MenuButtonThemeData {
match context.depend_on_inherited_widget_of_exact_type::<MenuButtonTheme>(app) {
Some(button_theme) => button_theme.data.clone(),
None => Theme::of(app, context).menu_button_theme.clone(),
}
}
}
impl InheritedTheme for MenuButtonTheme {
fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
MenuButtonTheme::new(self.data.clone(), child).into_widget()
}
}
impl InheritedWidget for MenuButtonTheme {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn child(&self) -> &WidgetRef {
&self.child
}
fn update_should_notify(&self, old_widget: &MenuButtonTheme) -> bool {
self.data != old_widget.data
}
fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
Some(self)
}
}
impl waft_foundation::Diagnosticable for MenuButtonThemeData {
fn debug_fill_properties(&self, properties: &mut waft_foundation::DiagnosticPropertiesBuilder) {
properties.add(
DiagnosticsProperty::<crate::button_style::ButtonStyle>::new(
"style",
self.style.clone(),
)
.default_value(None),
);
}
}
impl std::fmt::Display for MenuButtonThemeData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&waft_foundation::Diagnosticable::to_string(self))
}
}
#[cfg(test)]
mod tests {
use super::*;
// menu_button_theme_test.dart 'MenuButtonThemeData lerp special cases'.
#[test]
fn lerp_of_nothing_is_nothing_and_of_one_theme_with_itself_is_that_theme() {
assert!(MenuButtonThemeData::lerp(None, None, 0.0).is_none());
let data = MenuButtonThemeData::new();
assert_eq!(
MenuButtonThemeData::lerp(Some(&data), Some(&data), 0.5),
Some(data)
);
}
// Flutter's "Default MenuButtonThemeData debugFillProperties".
#[test]
fn a_menu_button_theme_data_describes_nothing_by_default() {
assert_eq!(
waft_widgets::testing::describe_properties(|properties| {
waft_foundation::Diagnosticable::debug_fill_properties(
&MenuButtonThemeData::new(),
properties,
)
}),
Vec::<String>::new()
);
}
}