#![allow(
deprecated,
reason = "Dart deprecates `ButtonBarThemeData` and `ButtonBarTheme`, which this file implements"
)]
use waft_embedder::lerp_double;
use waft_foundation::{App, DiagnosticsProperty, DoubleProperty, FlagProperty};
use waft_painting::EdgeInsetsGeometry;
use waft_widgets::{
BuildContext, InheritedWidget, IntoWidget, KeyRef, MainAxisAlignment, MainAxisSize,
VerticalDirection, WidgetRef,
};
use crate::button_theme::{ButtonBarLayoutBehavior, ButtonTextTheme};
use crate::theme::Theme;
#[deprecated(note = "Use OverflowBar instead. This feature was deprecated after v3.21.0-10.0.pre.")]
#[derive(Clone, Debug, Default, PartialEq)]
pub struct ButtonBarThemeData {
pub alignment: Option<MainAxisAlignment>,
pub main_axis_size: Option<MainAxisSize>,
pub button_text_theme: Option<ButtonTextTheme>,
pub button_min_width: Option<f64>,
pub button_height: Option<f64>,
pub button_padding: Option<EdgeInsetsGeometry>,
pub button_aligned_dropdown: Option<bool>,
pub layout_behavior: Option<ButtonBarLayoutBehavior>,
pub overflow_direction: Option<VerticalDirection>,
}
impl ButtonBarThemeData {
pub fn new() -> ButtonBarThemeData {
ButtonBarThemeData::default()
}
pub fn with_alignment(mut self, alignment: MainAxisAlignment) -> ButtonBarThemeData {
self.alignment = Some(alignment);
self
}
pub fn with_main_axis_size(mut self, main_axis_size: MainAxisSize) -> ButtonBarThemeData {
self.main_axis_size = Some(main_axis_size);
self
}
pub fn with_button_text_theme(
mut self,
button_text_theme: ButtonTextTheme,
) -> ButtonBarThemeData {
self.button_text_theme = Some(button_text_theme);
self
}
pub fn with_button_min_width(mut self, button_min_width: f64) -> ButtonBarThemeData {
debug_assert!(button_min_width >= 0.0);
self.button_min_width = Some(button_min_width);
self
}
pub fn with_button_height(mut self, button_height: f64) -> ButtonBarThemeData {
debug_assert!(button_height >= 0.0);
self.button_height = Some(button_height);
self
}
pub fn with_button_padding(
mut self,
button_padding: impl Into<EdgeInsetsGeometry>,
) -> ButtonBarThemeData {
self.button_padding = Some(button_padding.into());
self
}
pub fn with_button_aligned_dropdown(
mut self,
button_aligned_dropdown: bool,
) -> ButtonBarThemeData {
self.button_aligned_dropdown = Some(button_aligned_dropdown);
self
}
pub fn with_layout_behavior(
mut self,
layout_behavior: ButtonBarLayoutBehavior,
) -> ButtonBarThemeData {
self.layout_behavior = Some(layout_behavior);
self
}
pub fn with_overflow_direction(
mut self,
overflow_direction: VerticalDirection,
) -> ButtonBarThemeData {
self.overflow_direction = Some(overflow_direction);
self
}
pub fn copy_with(&self) -> ButtonBarThemeData {
self.clone()
}
pub fn lerp(
a: Option<&ButtonBarThemeData>,
b: Option<&ButtonBarThemeData>,
t: f64,
) -> Option<ButtonBarThemeData> {
match (a, b) {
(None, None) => return None,
(Some(a), Some(b)) if std::ptr::eq(a, b) => return Some(a.clone()),
_ => {}
}
let a = a.cloned().unwrap_or_default();
let b = b.cloned().unwrap_or_default();
let pick = |t: f64| t < 0.5;
Some(ButtonBarThemeData {
alignment: if pick(t) { a.alignment } else { b.alignment },
main_axis_size: if pick(t) {
a.main_axis_size
} else {
b.main_axis_size
},
button_text_theme: if pick(t) {
a.button_text_theme
} else {
b.button_text_theme
},
button_min_width: lerp_double(a.button_min_width, b.button_min_width, t),
button_height: lerp_double(a.button_height, b.button_height, t),
button_padding: EdgeInsetsGeometry::lerp(a.button_padding, b.button_padding, t),
button_aligned_dropdown: if pick(t) {
a.button_aligned_dropdown
} else {
b.button_aligned_dropdown
},
layout_behavior: if pick(t) {
a.layout_behavior
} else {
b.layout_behavior
},
overflow_direction: if pick(t) {
a.overflow_direction
} else {
b.overflow_direction
},
})
}
}
#[deprecated(note = "Use OverflowBar instead. This feature was deprecated after v3.21.0-10.0.pre.")]
pub struct ButtonBarTheme {
pub key: Option<KeyRef>,
pub data: ButtonBarThemeData,
pub child: WidgetRef,
}
impl ButtonBarTheme {
#[cfg_attr(debug_assertions, track_caller)]
pub fn new<K>(data: ButtonBarThemeData, child: impl IntoWidget<K>) -> ButtonBarTheme {
ButtonBarTheme {
key: None,
data,
child: child.into_widget(),
}
}
pub fn key(mut self, key: KeyRef) -> ButtonBarTheme {
self.key = Some(key);
self
}
pub fn of(app: &mut App, context: BuildContext) -> ButtonBarThemeData {
if let Some(button_bar_theme) =
context.depend_on_inherited_widget_of_exact_type::<ButtonBarTheme>(app)
{
return button_bar_theme.data.clone();
}
Theme::of(app, context).button_bar_theme.clone()
}
}
impl std::fmt::Debug for ButtonBarTheme {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ButtonBarTheme")
.field("data", &self.data)
.finish_non_exhaustive()
}
}
impl InheritedWidget for ButtonBarTheme {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn child(&self) -> &WidgetRef {
&self.child
}
fn update_should_notify(&self, old_widget: &ButtonBarTheme) -> bool {
self.data != old_widget.data
}
}
impl waft_foundation::Diagnosticable for ButtonBarThemeData {
fn debug_fill_properties(&self, properties: &mut waft_foundation::DiagnosticPropertiesBuilder) {
properties.add(
DiagnosticsProperty::<waft_foundation::DartEnum<MainAxisAlignment>>::new(
"alignment",
self.alignment.map(waft_foundation::DartEnum),
)
.default_value(None),
);
properties.add(
DiagnosticsProperty::<waft_foundation::DartEnum<MainAxisSize>>::new(
"mainAxisSize",
self.main_axis_size.map(waft_foundation::DartEnum),
)
.default_value(None),
);
properties.add(
DiagnosticsProperty::<waft_foundation::DartEnum<ButtonTextTheme>>::new(
"textTheme",
self.button_text_theme.map(waft_foundation::DartEnum),
)
.default_value(None),
);
properties.add(DoubleProperty::new("minWidth", self.button_min_width).default_value(None));
properties.add(DoubleProperty::new("height", self.button_height).default_value(None));
properties.add(
DiagnosticsProperty::<EdgeInsetsGeometry>::new("padding", self.button_padding)
.default_value(None),
);
properties.add(
FlagProperty::new("buttonAlignedDropdown", self.button_aligned_dropdown)
.if_true("dropdown width matches button")
.default_value(None),
);
properties.add(
DiagnosticsProperty::<waft_foundation::DartEnum<ButtonBarLayoutBehavior>>::new(
"layoutBehavior",
self.layout_behavior.map(waft_foundation::DartEnum),
)
.default_value(None),
);
properties.add(
DiagnosticsProperty::<waft_foundation::DartEnum<VerticalDirection>>::new(
"overflowDirection",
self.overflow_direction.map(waft_foundation::DartEnum),
)
.default_value(None),
);
}
}
impl std::fmt::Display for ButtonBarThemeData {
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 waft_widgets::testing::test_widgets;
use waft_widgets::{Builder, Center, MainAxisAlignment, MainAxisSize, Text, VerticalDirection};
use super::*;
use crate::test_support::material_app;
use crate::theme_data::ThemeData;
#[test]
fn button_bar_theme_data_lerp_special_cases() {
assert_eq!(ButtonBarThemeData::lerp(None, None, 0.0), None);
let data = ButtonBarThemeData::new();
assert_eq!(
ButtonBarThemeData::lerp(Some(&data), Some(&data), 0.5),
Some(data)
);
}
#[test]
fn every_field_is_null_by_default() {
let theme = ButtonBarThemeData::new();
assert_eq!(theme.alignment, None);
assert_eq!(theme.main_axis_size, None);
assert_eq!(theme.button_text_theme, None);
assert_eq!(theme.button_min_width, None);
assert_eq!(theme.button_height, None);
assert_eq!(theme.button_padding, None);
assert_eq!(theme.button_aligned_dropdown, None);
assert_eq!(theme.layout_behavior, None);
assert_eq!(theme.overflow_direction, None);
}
#[test]
fn theme_data_uses_the_default_button_bar_theme_data() {
let cell = waft_foundation::AppCell::new();
let theme = ThemeData::builder().build(&cell.borrow());
assert_eq!(theme.button_bar_theme, ButtonBarThemeData::default());
}
#[test]
fn copy_with_and_equality_basics() {
assert_eq!(
ButtonBarThemeData::new(),
ButtonBarThemeData::new().copy_with()
);
}
#[test]
fn the_fields_lerp_correctly() {
let primary = ButtonBarThemeData::new()
.with_alignment(MainAxisAlignment::End)
.with_main_axis_size(MainAxisSize::Min)
.with_button_text_theme(ButtonTextTheme::Primary)
.with_button_min_width(20.0)
.with_button_height(20.0)
.with_button_padding(waft_painting::EdgeInsets::symmetric(5.0, 0.0))
.with_button_aligned_dropdown(false)
.with_layout_behavior(ButtonBarLayoutBehavior::Padded)
.with_overflow_direction(VerticalDirection::Down);
let accent = ButtonBarThemeData::new()
.with_alignment(MainAxisAlignment::Center)
.with_main_axis_size(MainAxisSize::Max)
.with_button_text_theme(ButtonTextTheme::Accent)
.with_button_min_width(10.0)
.with_button_height(40.0)
.with_button_padding(waft_painting::EdgeInsets::symmetric(0.0, 10.0))
.with_button_aligned_dropdown(true)
.with_layout_behavior(ButtonBarLayoutBehavior::Constrained)
.with_overflow_direction(VerticalDirection::Up);
let lerp = ButtonBarThemeData::lerp(Some(&primary), Some(&accent), 0.5)
.expect("two themes lerp to a theme");
assert_eq!(lerp.alignment, Some(MainAxisAlignment::Center));
assert_eq!(lerp.main_axis_size, Some(MainAxisSize::Max));
assert_eq!(lerp.button_text_theme, Some(ButtonTextTheme::Accent));
assert_eq!(lerp.button_min_width, Some(15.0));
assert_eq!(lerp.button_height, Some(30.0));
assert_eq!(
lerp.button_padding,
Some(waft_painting::EdgeInsets::from_ltrb(5.0, 2.5, 5.0, 2.5).into())
);
assert_eq!(lerp.button_aligned_dropdown, Some(true));
assert_eq!(
lerp.layout_behavior,
Some(ButtonBarLayoutBehavior::Constrained)
);
assert_eq!(lerp.overflow_direction, Some(VerticalDirection::Up));
}
#[test]
fn of_falls_back_to_the_theme_data_field() {
test_widgets(|tester| {
let data = ButtonBarThemeData::new().with_button_min_width(42.0);
let theme = ThemeData::builder()
.button_bar_theme(data.clone())
.build(&tester.cell.borrow());
let seen = std::rc::Rc::new(std::cell::RefCell::new(None));
let captured = std::rc::Rc::clone(&seen);
tester.pump_widget(material_app(
theme,
Builder::new(move |app, context| {
*captured.borrow_mut() = Some(ButtonBarTheme::of(app, context));
Center::new().child(Text::new("x")).into_widget()
})
.into_widget(),
));
assert_eq!(seen.borrow().clone(), Some(data));
assert_eq!(
seen.borrow()
.as_ref()
.and_then(|theme| theme.button_min_width),
Some(42.0)
);
});
}
#[test]
fn an_ancestor_button_bar_theme_overrides_the_theme_data_field() {
test_widgets(|tester| {
let default_bar_theme = ButtonBarThemeData::new().with_button_min_width(42.0);
let override_theme = ButtonBarThemeData::new().with_button_min_width(84.0);
let theme = ThemeData::builder()
.button_bar_theme(default_bar_theme)
.build(&tester.cell.borrow());
let seen = std::rc::Rc::new(std::cell::RefCell::new(None));
let captured = std::rc::Rc::clone(&seen);
let override_clone = override_theme.clone();
tester.pump_widget(material_app(
theme,
ButtonBarTheme::new(
override_clone,
Builder::new(move |app, context| {
*captured.borrow_mut() = Some(ButtonBarTheme::of(app, context));
Center::new().child(Text::new("x")).into_widget()
}),
)
.into_widget(),
));
assert_eq!(seen.borrow().clone(), Some(override_theme));
assert_eq!(
seen.borrow()
.as_ref()
.and_then(|theme| theme.button_min_width),
Some(84.0)
);
});
}
#[test]
fn a_button_bar_theme_data_describes_nothing_by_default() {
assert_eq!(
waft_widgets::testing::describe_properties(|properties| {
waft_foundation::Diagnosticable::debug_fill_properties(
&ButtonBarThemeData::new(),
properties,
)
}),
Vec::<String>::new()
);
}
#[test]
fn an_assigned_button_bar_theme_describes_every_property() {
let theme = ButtonBarThemeData::new()
.with_alignment(MainAxisAlignment::Center)
.with_main_axis_size(MainAxisSize::Max)
.with_button_text_theme(ButtonTextTheme::Accent)
.with_button_min_width(10.0)
.with_button_height(42.0)
.with_button_padding(EdgeInsetsGeometry::symmetric(0.0, 7.3))
.with_button_aligned_dropdown(true)
.with_layout_behavior(ButtonBarLayoutBehavior::Constrained)
.with_overflow_direction(VerticalDirection::Up);
let description = waft_widgets::testing::describe_properties(|properties| {
waft_foundation::Diagnosticable::debug_fill_properties(&theme, properties)
});
assert_eq!(
description,
[
"alignment: MainAxisAlignment.center".to_string(),
"mainAxisSize: MainAxisSize.max".to_string(),
"textTheme: ButtonTextTheme.accent".to_string(),
"minWidth: 10.0".to_string(),
"height: 42.0".to_string(),
"padding: EdgeInsets(7.3, 0.0, 7.3, 0.0)".to_string(),
"dropdown width matches button".to_string(),
"layoutBehavior: ButtonBarLayoutBehavior.constrained".to_string(),
"overflowDirection: VerticalDirection.up".to_string(),
]
);
}
}