use waft_foundation::App;
use crate::framework::{BuildContext, InheritedWidget, IntoWidget, KeyRef, WidgetRef};
use crate::widgets::basic::Builder;
use crate::widgets::icon_theme_data::IconThemeData;
use crate::widgets::inherited_theme::InheritedTheme;
#[derive(Debug)]
pub struct IconTheme {
pub key: Option<KeyRef>,
pub data: IconThemeData,
pub child: WidgetRef,
}
impl IconTheme {
pub fn new<K>(data: IconThemeData, child: impl IntoWidget<K>) -> IconTheme {
IconTheme {
key: None,
data,
child: child.into_widget(),
}
}
pub fn key(mut self, key: KeyRef) -> IconTheme {
self.key = Some(key);
self
}
pub fn merge<K>(
key: Option<KeyRef>,
data: IconThemeData,
child: impl IntoWidget<K>,
) -> WidgetRef {
let child = child.into_widget();
Builder::new(move |app, context| {
let merged = Self::get_inherited_icon_theme_data(app, context).merge(Some(&data));
let icon_theme = IconTheme::new(merged, child.clone());
match &key {
Some(key) => icon_theme.key(key.clone()),
None => icon_theme,
}
.into_widget()
})
.into_widget()
}
pub fn of(app: &mut App, context: BuildContext) -> IconThemeData {
let icon_theme_data =
Self::get_inherited_icon_theme_data(app, context).resolve(app, context);
if icon_theme_data.is_concrete() {
return icon_theme_data;
}
let fallback = IconThemeData::fallback();
let mut concrete = icon_theme_data.copy_with();
concrete.size = icon_theme_data.size.or(fallback.size);
concrete.fill = icon_theme_data.fill.or(fallback.fill);
concrete.weight = icon_theme_data.weight.or(fallback.weight);
concrete.grade = icon_theme_data.grade.or(fallback.grade);
concrete.optical_size = icon_theme_data.optical_size.or(fallback.optical_size);
concrete.color = icon_theme_data.color.or(fallback.color);
concrete.opacity = icon_theme_data.opacity.or(fallback.opacity);
concrete.shadows = icon_theme_data.shadows.clone().or(fallback.shadows);
concrete.apply_text_scaling = icon_theme_data
.apply_text_scaling
.or(fallback.apply_text_scaling);
concrete
}
fn get_inherited_icon_theme_data(app: &mut App, context: BuildContext) -> IconThemeData {
let icon_theme = context.depend_on_inherited_widget_of_exact_type::<IconTheme>(app);
icon_theme.map_or_else(IconThemeData::fallback, |icon_theme| {
icon_theme.data.clone()
})
}
}
impl InheritedWidget for IconTheme {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn child(&self) -> &WidgetRef {
&self.child
}
fn update_should_notify(&self, old_widget: &IconTheme) -> bool {
self.data != old_widget.data
}
fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
Some(self)
}
}
impl InheritedTheme for IconTheme {
fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
IconTheme::new(self.data.clone(), child).into_widget()
}
}
#[cfg(test)]
mod tests {
use std::cell::RefCell;
use std::rc::Rc;
use waft_foundation::AppCell;
use waft_embedder::Color;
use waft_foundation::ValueKey;
use super::*;
use crate::framework::StatelessWidget;
use crate::test_harness::Harness;
use crate::widgets::basic::SizedBox;
#[derive(Debug)]
struct IconThemeReader {
seen: Rc<RefCell<Vec<IconThemeData>>>,
}
impl StatelessWidget for IconThemeReader {
fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
self.seen.borrow_mut().push(IconTheme::of(app, context));
SizedBox::shrink().into_widget()
}
}
fn reader() -> (WidgetRef, Rc<RefCell<Vec<IconThemeData>>>) {
let seen = Rc::new(RefCell::new(Vec::new()));
let reader = IconThemeReader {
seen: Rc::clone(&seen),
}
.into_widget();
(reader, seen)
}
fn mount(app: &mut App, child: WidgetRef) -> Harness {
let harness = Harness::mount(app, child);
harness.pump(app);
harness
}
#[test]
fn of_returns_the_fallback_outside_any_icon_theme() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let (reader, seen) = reader();
mount(&mut app, reader);
assert_eq!(seen.borrow().as_slice(), [IconThemeData::fallback()]);
}
#[test]
fn of_fills_a_partial_theme_from_the_fallback() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let (reader, seen) = reader();
let partial = IconThemeData::new().size(16.0).opacity(0.5);
mount(&mut app, IconTheme::new(partial, reader).into_widget());
let concrete = IconThemeData::fallback().size(16.0).opacity(0.5);
assert!(concrete.is_concrete());
assert_eq!(seen.borrow().as_slice(), [concrete]);
}
#[test]
fn a_changed_data_notifies_the_dependent_and_an_equal_one_does_not() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let (reader, seen) = reader();
let themed = |size: f64| {
IconTheme::new(IconThemeData::new().size(size), reader.clone()).into_widget()
};
let harness = mount(&mut app, themed(16.0));
assert_eq!(seen.borrow().len(), 1);
assert_eq!(seen.borrow()[0].size, Some(16.0));
harness.set_child(&mut app, themed(16.0));
harness.pump(&mut app);
assert_eq!(seen.borrow().len(), 1, "an equal data does not notify");
harness.set_child(&mut app, themed(20.0));
harness.pump(&mut app);
assert_eq!(
seen.borrow().len(),
2,
"a changed data rebuilds the dependent"
);
assert_eq!(seen.borrow()[1].size, Some(20.0));
}
#[test]
fn merge_layers_its_data_over_the_ambient_theme() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let (reader, seen) = reader();
let red = Color::new(0xFFFF0000);
let key: KeyRef = ValueKey::new("merged").into();
mount(
&mut app,
IconTheme::new(
IconThemeData::new().size(16.0).color(red),
IconTheme::merge(Some(key), IconThemeData::new().size(20.0), reader),
)
.into_widget(),
);
assert_eq!(
seen.borrow().as_slice(),
[IconThemeData::fallback().size(20.0).color(red)]
);
}
}