use std::any::Any;
use std::fmt::{self, Debug};
use std::rc::Rc;
use waft_embedder::{Canvas, Paint, Path, Rect};
use waft_painting::{BorderSide, EdgeInsetsGeometry, ShapeBorder, ShapeBorderRef, TextDirection};
use waft_widgets::{
WidgetPropertyResolver, WidgetState, WidgetStateBorderSide, WidgetStateColor, WidgetStateMap,
WidgetStateMapper, WidgetStateMouseCursor, WidgetStateOutlinedBorder, WidgetStateProperty,
WidgetStatePropertyAll, WidgetStateTextStyle, WidgetStates, WidgetStatesController,
};
use crate::input_border::{InputBorder, InputBorderRef, OutlineInputBorder, UnderlineInputBorder};
#[deprecated(
note = "Use WidgetState instead. Moved to the Widgets layer to make code available outside of Material. This feature was deprecated after v3.19.0-0.3.pre."
)]
pub type MaterialState = WidgetState;
#[deprecated(
note = "Use WidgetPropertyResolver instead. Moved to the Widgets layer to make code available outside of Material. This feature was deprecated after v3.19.0-0.3.pre."
)]
pub type MaterialPropertyResolver<T> = WidgetPropertyResolver<T>;
#[deprecated(
note = "Use WidgetStateColor instead. Moved to the Widgets layer to make code available outside of Material. This feature was deprecated after v3.19.0-0.3.pre."
)]
pub type MaterialStateColor = WidgetStateColor;
#[deprecated(
note = "Use WidgetStateMouseCursor instead. Moved to the Widgets layer to make code available outside of Material. This feature was deprecated after v3.19.0-0.3.pre."
)]
pub type MaterialStateMouseCursor = WidgetStateMouseCursor;
#[deprecated(
note = "Use WidgetStateBorderSide instead. Moved to the Widgets layer to make code available outside of Material. This feature was deprecated after v3.19.0-0.3.pre."
)]
pub type MaterialStateBorderSide = WidgetStateBorderSide;
#[deprecated(
note = "Use WidgetStateOutlinedBorder instead. Moved to the Widgets layer to make code available outside of Material. This feature was deprecated after v3.19.0-0.3.pre."
)]
pub type MaterialStateOutlinedBorder = WidgetStateOutlinedBorder;
#[deprecated(
note = "Use WidgetStateTextStyle instead. Moved to the Widgets layer to make code available outside of Material. This feature was deprecated after v3.19.0-0.3.pre."
)]
pub type MaterialStateTextStyle = WidgetStateTextStyle;
macro_rules! state_input_border {
($name:ty, $debug:literal) => {
#[allow(deprecated)]
impl ShapeBorder for $name {
fn dimensions(&self) -> EdgeInsetsGeometry {
self.border.dimensions()
}
fn scale(&self, t: f64) -> ShapeBorderRef {
ShapeBorder::scale(&self.border, t)
}
fn lerp_from(&self, a: Option<&dyn ShapeBorder>, t: f64) -> Option<ShapeBorderRef> {
self.border.lerp_from(a, t)
}
fn lerp_to(&self, b: Option<&dyn ShapeBorder>, t: f64) -> Option<ShapeBorderRef> {
self.border.lerp_to(b, t)
}
fn get_outer_path(&self, rect: Rect, text_direction: Option<TextDirection>) -> Path {
self.border.get_outer_path(rect, text_direction)
}
fn get_inner_path(&self, rect: Rect, text_direction: Option<TextDirection>) -> Path {
self.border.get_inner_path(rect, text_direction)
}
fn paint_interior(
&self,
canvas: &mut Canvas,
rect: Rect,
paint: &Paint,
text_direction: Option<TextDirection>,
) {
self.border
.paint_interior(canvas, rect, paint, text_direction);
}
fn prefer_paint_interior(&self) -> bool {
self.border.prefer_paint_interior()
}
fn paint(
&self,
canvas: &mut Canvas,
rect: Rect,
text_direction: Option<TextDirection>,
) {
ShapeBorder::paint(&self.border, canvas, rect, text_direction);
}
fn as_any(&self) -> &dyn Any {
self
}
fn eq_shape(&self, other: &dyn ShapeBorder) -> bool {
other
.as_any()
.downcast_ref::<$name>()
.is_some_and(|other| self.eq_subclass(other))
}
}
#[allow(deprecated)]
impl InputBorder for $name {
fn border_side(&self) -> BorderSide {
self.border.border_side
}
fn copy_with(&self, border_side: Option<BorderSide>) -> InputBorderRef {
InputBorder::copy_with(&self.border, border_side)
}
fn is_outline(&self) -> bool {
self.border.is_outline()
}
fn paint(
&self,
canvas: &mut Canvas,
rect: Rect,
gap_start: Option<f64>,
gap_extent: f64,
gap_percentage: f64,
text_direction: Option<TextDirection>,
) {
InputBorder::paint(
&self.border,
canvas,
rect,
gap_start,
gap_extent,
gap_percentage,
text_direction,
);
}
fn as_widget_state_property(&self) -> Option<&dyn WidgetStateProperty<InputBorderRef>> {
Some(self)
}
}
#[allow(deprecated)]
impl Debug for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str($debug)
}
}
};
}
#[deprecated(
note = "Use WidgetStateInputBorder instead. Renamed to match other WidgetStateProperty objects. This feature was deprecated after v3.26.0-0.1.pre."
)]
#[derive(Clone)]
pub struct MaterialStateOutlineInputBorder {
resolve: WidgetPropertyResolver<InputBorderRef>,
border: OutlineInputBorder,
}
#[allow(deprecated)]
impl MaterialStateOutlineInputBorder {
#[deprecated(
note = "Use WidgetStateInputBorder.resolveWith() instead. Renamed to match other WidgetStateProperty objects. This feature was deprecated after v3.26.0-0.1.pre."
)]
pub fn resolve_with(
callback: impl Fn(&WidgetStates) -> InputBorderRef + 'static,
) -> MaterialStateOutlineInputBorder {
MaterialStateOutlineInputBorder {
resolve: Rc::new(callback),
border: OutlineInputBorder::new(),
}
}
fn eq_subclass(&self, other: &MaterialStateOutlineInputBorder) -> bool {
self.border == other.border
}
}
#[allow(deprecated)]
impl WidgetStateProperty<InputBorderRef> for MaterialStateOutlineInputBorder {
fn resolve(&self, states: &WidgetStates) -> InputBorderRef {
(self.resolve)(states)
}
fn as_any(&self) -> &dyn Any {
self
}
fn eq_property(&self, other: &dyn WidgetStateProperty<InputBorderRef>) -> bool {
other
.as_any()
.downcast_ref::<Self>()
.is_some_and(|other| self.eq_subclass(other))
}
}
state_input_border!(
MaterialStateOutlineInputBorder,
"_MaterialStateOutlineInputBorder()"
);
#[deprecated(
note = "Use WidgetStateInputBorder instead. Renamed to match other WidgetStateProperty objects. This feature was deprecated after v3.26.0-0.1.pre."
)]
#[derive(Clone)]
pub struct MaterialStateUnderlineInputBorder {
resolve: WidgetPropertyResolver<InputBorderRef>,
border: UnderlineInputBorder,
}
#[allow(deprecated)]
impl MaterialStateUnderlineInputBorder {
#[deprecated(
note = "Use WidgetStateInputBorder.resolveWith() instead. Renamed to match other WidgetStateProperty objects. This feature was deprecated after v3.26.0-0.1.pre."
)]
pub fn resolve_with(
callback: impl Fn(&WidgetStates) -> InputBorderRef + 'static,
) -> MaterialStateUnderlineInputBorder {
MaterialStateUnderlineInputBorder {
resolve: Rc::new(callback),
border: UnderlineInputBorder::new(),
}
}
fn eq_subclass(&self, other: &MaterialStateUnderlineInputBorder) -> bool {
self.border == other.border
}
}
#[allow(deprecated)]
impl WidgetStateProperty<InputBorderRef> for MaterialStateUnderlineInputBorder {
fn resolve(&self, states: &WidgetStates) -> InputBorderRef {
(self.resolve)(states)
}
fn as_any(&self) -> &dyn Any {
self
}
fn eq_property(&self, other: &dyn WidgetStateProperty<InputBorderRef>) -> bool {
other
.as_any()
.downcast_ref::<Self>()
.is_some_and(|other| self.eq_subclass(other))
}
}
state_input_border!(
MaterialStateUnderlineInputBorder,
"_MaterialStateUnderlineInputBorder()"
);
#[derive(Clone)]
pub struct WidgetStateInputBorder {
subclass: InputBorderSubclass,
border: OutlineInputBorder,
}
#[derive(Clone)]
enum InputBorderSubclass {
ResolveWith(WidgetPropertyResolver<InputBorderRef>),
Mapper(Rc<WidgetStateMapper<InputBorderRef>>),
}
impl WidgetStateInputBorder {
pub fn resolve_with(
callback: impl Fn(&WidgetStates) -> InputBorderRef + 'static,
) -> WidgetStateInputBorder {
WidgetStateInputBorder {
subclass: InputBorderSubclass::ResolveWith(Rc::new(callback)),
border: OutlineInputBorder::new(),
}
}
pub fn from_map(map: WidgetStateMap<InputBorderRef>) -> WidgetStateInputBorder {
WidgetStateInputBorder {
subclass: InputBorderSubclass::Mapper(Rc::new(WidgetStateMapper::new(map))),
border: OutlineInputBorder::new(),
}
}
fn eq_subclass(&self, other: &WidgetStateInputBorder) -> bool {
match (&self.subclass, &other.subclass) {
(InputBorderSubclass::ResolveWith(_), InputBorderSubclass::ResolveWith(_)) => {
self.border == other.border
}
(InputBorderSubclass::Mapper(mine), InputBorderSubclass::Mapper(theirs)) => {
mine == theirs
}
_ => false,
}
}
}
impl WidgetStateProperty<InputBorderRef> for WidgetStateInputBorder {
fn resolve(&self, states: &WidgetStates) -> InputBorderRef {
match &self.subclass {
InputBorderSubclass::ResolveWith(resolve) => resolve(states),
InputBorderSubclass::Mapper(mapper) => mapper.resolve(states),
}
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_mapper(&self) -> Option<&WidgetStateMapper<InputBorderRef>> {
match &self.subclass {
InputBorderSubclass::Mapper(mapper) => Some(mapper.as_ref()),
InputBorderSubclass::ResolveWith(_) => None,
}
}
fn eq_property(&self, other: &dyn WidgetStateProperty<InputBorderRef>) -> bool {
if let InputBorderSubclass::Mapper(mapper) = &self.subclass {
return mapper.eq_property(other);
}
other
.as_any()
.downcast_ref::<Self>()
.is_some_and(|other| self.eq_subclass(other))
}
}
state_input_border!(WidgetStateInputBorder, "_WidgetStateInputBorder()");
#[deprecated(
note = "Use WidgetStateProperty instead. Moved to the Widgets layer to make code available outside of Material. This feature was deprecated after v3.19.0-0.3.pre."
)]
pub type MaterialStateProperty<T> = dyn WidgetStateProperty<T>;
#[deprecated(
note = "Use WidgetStatePropertyAll instead. Moved to the Widgets layer to make code available outside of Material. This feature was deprecated after v3.19.0-0.3.pre."
)]
pub type MaterialStatePropertyAll<T> = WidgetStatePropertyAll<T>;
#[deprecated(
note = "Use WidgetStatesController instead. Moved to the Widgets layer to make code available outside of Material. This feature was deprecated after v3.19.0-0.3.pre."
)]
pub type MaterialStatesController = WidgetStatesController;
#[cfg(test)]
#[allow(deprecated)]
mod tests {
use std::cell::Cell;
use std::rc::Rc;
use waft_embedder::Color;
use waft_foundation::{AppCell, ChangeNotifier, Listenable, Listener};
use waft_painting::TextStyle;
use waft_widgets::WidgetStates;
use super::*;
const EVERY_STATE: [MaterialState; 7] = [
MaterialState::Hovered,
MaterialState::Focused,
MaterialState::Pressed,
MaterialState::Dragged,
MaterialState::Selected,
MaterialState::Disabled,
MaterialState::Error,
];
fn states(states: impl IntoIterator<Item = MaterialState>) -> WidgetStates {
states.into_iter().collect()
}
#[test]
fn resolve_with_resolves_through_the_callback() {
let value: Rc<MaterialStateProperty<MaterialState>> =
<MaterialStateProperty<MaterialState>>::resolve_with(|states| {
states.iter().next().copied().expect("one state")
});
for state in EVERY_STATE {
assert_eq!(value.resolve(&states([state])), state);
}
}
#[test]
fn all_resolves_to_its_value_for_every_state() {
let value: Rc<MaterialStateProperty<i32>> = <MaterialStateProperty<i32>>::all(123);
for state in EVERY_STATE {
assert_eq!(value.resolve(&states([state])), 123);
}
}
#[test]
fn material_state_property_all_resolves_to_its_value_for_every_state() {
let value = MaterialStatePropertyAll::new(123);
assert_eq!(value.resolve(&WidgetStates::new()), 123);
for state in EVERY_STATE {
assert_eq!(value.resolve(&states([state])), 123);
}
}
#[test]
fn a_material_state_property_all_prints_as_a_widget_state_property_all() {
let color_property = MaterialStatePropertyAll::new(Some(Color::new(0xFFFFFFFF)));
assert_eq!(
format!("{color_property:?}"),
format!("WidgetStatePropertyAll({:?})", Some(Color::new(0xffffffff)))
);
}
#[test]
fn lerp_interpolates_the_resolved_values() {
let enabled = WidgetStates::new();
let text_style_1: Rc<MaterialStateProperty<TextStyle>> = Rc::new(
MaterialStatePropertyAll::new(TextStyle::new().font_size(14.0)),
);
let text_style_2: Rc<MaterialStateProperty<TextStyle>> = Rc::new(
MaterialStatePropertyAll::new(TextStyle::new().font_size(20.0)),
);
let lerp = |t: f64| {
<MaterialStateProperty<TextStyle>>::lerp(
Some(Rc::clone(&text_style_1)),
Some(Rc::clone(&text_style_2)),
t,
|a, b, t| TextStyle::lerp(a.as_ref(), b.as_ref(), t),
)
.expect("one side")
.resolve(&enabled)
.expect("both sides")
.font_size
};
assert_eq!(lerp(0.0), Some(14.0));
assert_eq!(lerp(0.5), Some(17.0));
assert_eq!(lerp(1.0), Some(20.0));
}
#[test]
fn controller_starts_with_the_given_states() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let none = MaterialStatesController::new(&mut app, None);
assert_eq!(*app.get(&none).value(), WidgetStates::new());
let empty = MaterialStatesController::new(&mut app, Some(WidgetStates::new()));
assert_eq!(*app.get(&empty).value(), WidgetStates::new());
let selected =
MaterialStatesController::new(&mut app, Some(states([MaterialState::Selected])));
assert_eq!(
*app.get(&selected).value(),
states([MaterialState::Selected])
);
}
#[test]
fn controller_update_notifies_only_on_change() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let count = Rc::new(Cell::new(0));
let value_changed = Listener::new({
let count = Rc::clone(&count);
move |_app| count.set(count.get() + 1)
});
let controller = MaterialStatesController::new(&mut app, None);
controller.add_listener(&mut app, value_changed.clone());
let expect = |app: &waft_foundation::App, expected: &[MaterialState], expected_count| {
assert_eq!(
*app.get(&controller).value(),
states(expected.iter().copied())
);
assert_eq!(count.get(), expected_count);
};
controller.update(&mut app, MaterialState::Selected, true);
expect(&app, &[MaterialState::Selected], 1);
controller.update(&mut app, MaterialState::Selected, true);
expect(&app, &[MaterialState::Selected], 1);
controller.update(&mut app, MaterialState::Hovered, false);
expect(&app, &[MaterialState::Selected], 1);
controller.update(&mut app, MaterialState::Selected, false);
expect(&app, &[], 2);
controller.update(&mut app, MaterialState::Hovered, true);
expect(&app, &[MaterialState::Hovered], 3);
controller.update(&mut app, MaterialState::Hovered, true);
expect(&app, &[MaterialState::Hovered], 3);
controller.update(&mut app, MaterialState::Pressed, true);
expect(&app, &[MaterialState::Hovered, MaterialState::Pressed], 4);
controller.update(&mut app, MaterialState::Selected, true);
expect(
&app,
&[
MaterialState::Hovered,
MaterialState::Pressed,
MaterialState::Selected,
],
5,
);
controller.update(&mut app, MaterialState::Selected, false);
expect(&app, &[MaterialState::Hovered, MaterialState::Pressed], 6);
controller.update(&mut app, MaterialState::Selected, false);
expect(&app, &[MaterialState::Hovered, MaterialState::Pressed], 6);
controller.update(&mut app, MaterialState::Pressed, false);
expect(&app, &[MaterialState::Hovered], 7);
controller.update(&mut app, MaterialState::Hovered, false);
expect(&app, &[], 8);
controller.remove_listener(&mut app, &value_changed);
controller.update(&mut app, MaterialState::Selected, true);
expect(&app, &[MaterialState::Selected], 8);
}
#[test]
fn controller_with_an_initial_value_notifies_only_on_change() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let count = Rc::new(Cell::new(0));
let controller =
MaterialStatesController::new(&mut app, Some(states([MaterialState::Selected])));
controller.add_listener(&mut app, {
let count = Rc::clone(&count);
Listener::new(move |_app| count.set(count.get() + 1))
});
controller.update(&mut app, MaterialState::Selected, true);
assert_eq!(
*app.get(&controller).value(),
states([MaterialState::Selected])
);
assert_eq!(count.get(), 0);
controller.update(&mut app, MaterialState::Selected, false);
assert_eq!(*app.get(&controller).value(), WidgetStates::new());
assert_eq!(count.get(), 1);
controller.dispose(&mut app);
assert!(!app.get(&controller).change_notifier_data().has_listeners());
}
}