waft_material/menu_button_theme.rs
1//! Flutter counterpart: material_ui's `menu_button_theme.dart`.
2
3use waft_foundation::{App, DiagnosticsProperty};
4use waft_widgets::{BuildContext, InheritedTheme, InheritedWidget, IntoWidget, KeyRef, WidgetRef};
5
6use crate::button_style::ButtonStyle;
7use crate::theme::Theme;
8
9/// A [`ButtonStyle`] theme that overrides the default appearance of
10/// `SubmenuButton`s and `MenuItemButton`s.
11///
12/// Descendant widgets obtain the current [`MenuButtonThemeData`] object
13/// using [`MenuButtonTheme::of`].
14///
15/// A [`MenuButtonThemeData`] is often specified as part of the
16/// overall `Theme` with `ThemeData::menu_button_theme`.
17///
18/// The [`style`](Self::style)'s properties override `MenuItemButton`'s and `SubmenuButton`'s
19/// default style, i.e. the [`ButtonStyle`] returned by
20/// `MenuItemButton::default_style_of` and `SubmenuButton::default_style_of`. Only the
21/// style's non-null property values or resolved non-null
22/// `WidgetStateProperty` values are used.
23///
24/// See also:
25///
26/// * [`MenuButtonTheme`], the theme which is configured with this class.
27/// * `MenuTheme`, the theme used to configure the look of the menus these
28/// buttons reside in.
29/// * `MenuItemButton::default_style_of` and `SubmenuButton::default_style_of` which
30/// return the default [`ButtonStyle`]s for menu buttons.
31/// * `MenuItemButton::style_from` and `SubmenuButton::style_from`, which converts
32/// simple values into a [`ButtonStyle`] that's consistent with their respective
33/// defaults.
34/// * `WidgetStateProperty::resolve`, "resolve" a material state property to a
35/// simple value based on a set of `WidgetState`s.
36/// * `ThemeData::menu_button_theme`, which can be used to override the default
37/// [`ButtonStyle`] for `MenuItemButton`s and `SubmenuButton`s below the overall
38/// `Theme`.
39/// * `MenuAnchor`, a widget which hosts cascading menus.
40/// * `MenuBar`, a widget which defines a menu bar of buttons hosting cascading
41/// menus.
42#[derive(Clone, Debug, Default, PartialEq)]
43pub struct MenuButtonThemeData {
44 /// Overrides for `SubmenuButton` and `MenuItemButton`'s default style.
45 ///
46 /// Non-null properties or non-null resolved `WidgetStateProperty` values
47 /// override the [`ButtonStyle`] returned by `SubmenuButton::default_style_of` or
48 /// `MenuItemButton::default_style_of`.
49 ///
50 /// If [`style`](Self::style) is null, then this theme doesn't override anything.
51 pub style: Option<ButtonStyle>,
52}
53
54impl MenuButtonThemeData {
55 /// Creates a [`MenuButtonThemeData`].
56 ///
57 /// The [`style`](Self::style) may be null.
58 pub fn new() -> MenuButtonThemeData {
59 MenuButtonThemeData::default()
60 }
61
62 /// Dart `MenuButtonThemeData(style:)`.
63 pub fn style(mut self, style: ButtonStyle) -> MenuButtonThemeData {
64 self.style = Some(style);
65 self
66 }
67
68 /// Linearly interpolate between two menu button themes.
69 pub fn lerp(
70 a: Option<&MenuButtonThemeData>,
71 b: Option<&MenuButtonThemeData>,
72 t: f64,
73 ) -> Option<MenuButtonThemeData> {
74 match (a, b) {
75 (None, None) => return None,
76 (Some(a), Some(b)) if std::ptr::eq(a, b) => return Some(a.clone()),
77 _ => {}
78 }
79 Some(MenuButtonThemeData {
80 style: ButtonStyle::lerp(
81 a.and_then(|a| a.style.as_ref()),
82 b.and_then(|b| b.style.as_ref()),
83 t,
84 ),
85 })
86 }
87}
88
89/// Overrides the default [`ButtonStyle`] of its `MenuItemButton` and
90/// `SubmenuButton` descendants.
91///
92/// See also:
93///
94/// * [`MenuButtonThemeData`], which is used to configure this theme.
95/// * `MenuTheme`, the theme used to configure the look of the menus themselves.
96/// * `MenuItemButton::default_style_of` and `SubmenuButton::default_style_of` which
97/// return the default [`ButtonStyle`]s for menu buttons.
98/// * `MenuItemButton::style_from` and `SubmenuButton::style_from`, which converts
99/// simple values into a [`ButtonStyle`] that's consistent with their respective
100/// defaults.
101/// * `ThemeData::menu_button_theme`, which can be used to override the default
102/// [`ButtonStyle`] for `MenuItemButton`s and `SubmenuButton`s below the overall
103/// `Theme`.
104#[derive(Debug)]
105pub struct MenuButtonTheme {
106 pub key: Option<KeyRef>,
107 /// The configuration of this theme.
108 pub data: MenuButtonThemeData,
109 pub child: WidgetRef,
110}
111
112impl MenuButtonTheme {
113 /// Create a [`MenuButtonTheme`].
114 #[cfg_attr(debug_assertions, track_caller)]
115 pub fn new<K>(data: MenuButtonThemeData, child: impl IntoWidget<K>) -> MenuButtonTheme {
116 MenuButtonTheme {
117 key: None,
118 data,
119 child: child.into_widget(),
120 }
121 }
122
123 /// Dart `MenuButtonTheme(key:)`.
124 pub fn key(mut self, key: KeyRef) -> MenuButtonTheme {
125 self.key = Some(key);
126 self
127 }
128
129 /// The closest instance of this class that encloses the given context.
130 ///
131 /// If there is no enclosing [`MenuButtonTheme`] widget, then
132 /// `ThemeData::menu_button_theme` is used.
133 ///
134 /// Typical usage is as follows:
135 ///
136 /// ```text
137 /// let theme = MenuButtonTheme::of(app, context);
138 /// ```
139 pub fn of(app: &mut App, context: BuildContext) -> MenuButtonThemeData {
140 match context.depend_on_inherited_widget_of_exact_type::<MenuButtonTheme>(app) {
141 Some(button_theme) => button_theme.data.clone(),
142 None => Theme::of(app, context).menu_button_theme.clone(),
143 }
144 }
145}
146
147impl InheritedTheme for MenuButtonTheme {
148 fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
149 MenuButtonTheme::new(self.data.clone(), child).into_widget()
150 }
151}
152
153impl InheritedWidget for MenuButtonTheme {
154 fn key(&self) -> Option<&KeyRef> {
155 self.key.as_ref()
156 }
157
158 fn child(&self) -> &WidgetRef {
159 &self.child
160 }
161
162 fn update_should_notify(&self, old_widget: &MenuButtonTheme) -> bool {
163 self.data != old_widget.data
164 }
165
166 fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
167 Some(self)
168 }
169}
170
171impl waft_foundation::Diagnosticable for MenuButtonThemeData {
172 fn debug_fill_properties(&self, properties: &mut waft_foundation::DiagnosticPropertiesBuilder) {
173 properties.add(
174 DiagnosticsProperty::<crate::button_style::ButtonStyle>::new(
175 "style",
176 self.style.clone(),
177 )
178 .default_value(None),
179 );
180 }
181}
182
183impl std::fmt::Display for MenuButtonThemeData {
184 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
185 f.write_str(&waft_foundation::Diagnosticable::to_string(self))
186 }
187}
188
189#[cfg(test)]
190mod tests {
191 use super::*;
192
193 // menu_button_theme_test.dart 'MenuButtonThemeData lerp special cases'.
194 #[test]
195 fn lerp_of_nothing_is_nothing_and_of_one_theme_with_itself_is_that_theme() {
196 assert!(MenuButtonThemeData::lerp(None, None, 0.0).is_none());
197 let data = MenuButtonThemeData::new();
198 assert_eq!(
199 MenuButtonThemeData::lerp(Some(&data), Some(&data), 0.5),
200 Some(data)
201 );
202 }
203 // Flutter's "Default MenuButtonThemeData debugFillProperties".
204 #[test]
205 fn a_menu_button_theme_data_describes_nothing_by_default() {
206 assert_eq!(
207 waft_widgets::testing::describe_properties(|properties| {
208 waft_foundation::Diagnosticable::debug_fill_properties(
209 &MenuButtonThemeData::new(),
210 properties,
211 )
212 }),
213 Vec::<String>::new()
214 );
215 }
216}