use std::rc::Rc;
use std::time::Duration;
use waft_animation::Animatable;
use waft_cupertino::{CupertinoTheme, CupertinoThemeData, InheritedCupertinoTheme};
use waft_embedder::Brightness;
use waft_foundation::{App, Handle, Ref};
use waft_painting::AnyColor;
use waft_scheduler::{Ticker, TickerCallback, TickerProviderObject};
use waft_widgets::{
AnimatedWidgetBaseState, BuildContext, DefaultSelectionStyle, IconTheme,
ImplicitlyAnimatedWidget, ImplicitlyAnimatedWidgetData, ImplicitlyAnimatedWidgetState,
ImplicitlyAnimatedWidgetStateData, InheritedWidget, IntoWidget, KeyRef, Localizations,
MediaQuery, SingleTickerProviderStateMixin, SingleTickerProviderStateMixinData, State,
StateData, StatefulWidget, StatelessWidget, TweenSlot, TweenValue, TweenVisitor, WidgetRef,
};
use crate::material_localizations::MaterialLocalizations;
use crate::theme_data::{
CupertinoBasedMaterialThemeData, MaterialBasedCupertinoThemeData, ThemeData,
};
use crate::typography::ScriptCategory;
pub const K_THEME_ANIMATION_DURATION: Duration = Duration::from_millis(200);
#[derive(Clone, Debug)]
pub struct Theme {
pub key: Option<KeyRef>,
pub data: Rc<ThemeData>,
pub child: WidgetRef,
}
#[derive(Default)]
struct CachedFallbackTheme {
theme: Option<Rc<ThemeData>>,
}
impl Theme {
pub fn new<K>(data: impl Into<Rc<ThemeData>>, child: impl IntoWidget<K>) -> Theme {
Theme {
key: None,
data: data.into(),
child: child.into_widget(),
}
}
pub fn key(mut self, key: KeyRef) -> Theme {
self.key = Some(key);
self
}
fn fallback_theme(app: &mut App) -> Rc<ThemeData> {
let cache = app.singleton::<CachedFallbackTheme>();
let platform = app.platform().target_platform();
let stale = app
.get(cache)
.theme
.as_ref()
.is_none_or(|theme| theme.platform != platform);
if stale {
let theme = Rc::new(ThemeData::fallback(app, None));
app.get_mut(cache).theme = Some(theme);
}
app.get(cache).theme.clone().expect("filled above")
}
pub fn of(app: &mut App, context: BuildContext) -> Rc<ThemeData> {
let inherited_theme = context
.depend_on_inherited_widget_of_exact_type::<InheritedTheme>(app)
.map(|inherited_theme| Rc::clone(&inherited_theme.theme.data));
let localizations = Localizations::of::<dyn MaterialLocalizations>(app, context);
let category = localizations
.map(|localizations| localizations.script_category())
.unwrap_or(ScriptCategory::EnglishLike);
let inherited_cupertino_theme = context
.depend_on_inherited_widget_of_exact_type::<InheritedCupertinoTheme>(app)
.filter(|_| inherited_theme.is_none())
.map(|inherited_cupertino_theme| inherited_cupertino_theme.theme.data.clone());
let theme = match (inherited_theme, inherited_cupertino_theme) {
(Some(theme), _) => theme,
(None, Some(cupertino_theme)) => {
CupertinoBasedMaterialThemeData::new(app, &cupertino_theme).material_theme
}
(None, None) => Theme::fallback_theme(app),
};
let local_text_geometry = Rc::clone(theme.typography.geometry_theme_for(category));
ThemeData::localize(app, &theme, &local_text_geometry)
}
fn wraps_widget_themes(
&self,
app: &mut App,
context: BuildContext,
child: WidgetRef,
) -> WidgetRef {
let selection_style = DefaultSelectionStyle::of(app, context);
let text_selection_theme = &self.data.text_selection_theme;
let mut default_selection_style = DefaultSelectionStyle::new(child);
if let Some(selection_color) = text_selection_theme
.selection_color
.map(AnyColor::new)
.or(selection_style.selection_color)
{
default_selection_style = default_selection_style.selection_color(selection_color);
}
if let Some(cursor_color) = text_selection_theme
.cursor_color
.map(AnyColor::new)
.or(selection_style.cursor_color)
{
default_selection_style = default_selection_style.cursor_color(cursor_color);
}
IconTheme::new(self.data.icon_theme.clone(), default_selection_style).into_widget()
}
fn inherited_cupertino_theme_data(
&self,
app: &mut App,
context: BuildContext,
) -> CupertinoThemeData {
let inherited_theme = context
.depend_on_inherited_widget_of_exact_type::<InheritedCupertinoTheme>(app)
.map(|inherited_theme| inherited_theme.theme.data.clone());
inherited_theme
.unwrap_or_else(|| MaterialBasedCupertinoThemeData::new(&self.data))
.resolve_from(app, context)
}
pub fn brightness_of(app: &mut App, context: BuildContext) -> Brightness {
let brightness = context
.depend_on_inherited_widget_of_exact_type::<InheritedTheme>(app)
.map(|inherited_theme| inherited_theme.theme.data.brightness());
brightness.unwrap_or_else(|| MediaQuery::platform_brightness_of(app, context))
}
pub fn maybe_brightness_of(app: &mut App, context: BuildContext) -> Option<Brightness> {
let brightness = context
.depend_on_inherited_widget_of_exact_type::<InheritedTheme>(app)
.map(|inherited_theme| inherited_theme.theme.data.brightness());
brightness.or_else(|| MediaQuery::maybe_platform_brightness_of(app, context))
}
}
impl StatelessWidget for Theme {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
let cupertino_theme_data = self.inherited_cupertino_theme_data(app, context);
let child = self.wraps_widget_themes(app, context, self.child.clone());
InheritedTheme {
key: None,
theme: self.clone(),
child: CupertinoTheme::new(cupertino_theme_data, child).into_widget(),
}
.into_widget()
}
}
#[derive(Debug)]
struct InheritedTheme {
key: Option<KeyRef>,
theme: Theme,
child: WidgetRef,
}
impl waft_widgets::InheritedTheme for InheritedTheme {
fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
Theme::new(Rc::clone(&self.theme.data), child).into_widget()
}
}
impl InheritedWidget for InheritedTheme {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn child(&self) -> &WidgetRef {
&self.child
}
fn update_should_notify(&self, old_widget: &InheritedTheme) -> bool {
self.theme.data != old_widget.theme.data
}
fn as_inherited_theme(&self) -> Option<&dyn waft_widgets::InheritedTheme> {
Some(self)
}
}
#[derive(Clone, Debug)]
pub struct ThemeDataTween {
pub begin: Option<Rc<ThemeData>>,
pub end: Option<Rc<ThemeData>>,
}
impl ThemeDataTween {
pub fn new(begin: Option<Rc<ThemeData>>, end: Option<Rc<ThemeData>>) -> ThemeDataTween {
ThemeDataTween { begin, end }
}
pub fn lerp(&self, t: f64) -> Rc<ThemeData> {
Rc::new(ThemeData::lerp(
self.begin
.as_ref()
.expect("Tween.begin must be set before use"),
self.end.as_ref().expect("Tween.end must be set before use"),
t,
))
}
}
impl Animatable<Rc<ThemeData>> for ThemeDataTween {
fn transform(&self, _app: &App, t: f64) -> Rc<ThemeData> {
if t == 0.0 {
return self
.begin
.clone()
.expect("Tween.begin must be set before use");
}
if t == 1.0 {
return self.end.clone().expect("Tween.end must be set before use");
}
self.lerp(t)
}
}
impl TweenValue for ThemeData {
fn same(&self, other: &ThemeData) -> bool {
self == other
}
}
impl TweenSlot for ThemeDataTween {
type Value = Rc<ThemeData>;
fn begin(&self, _app: &App) -> Option<Rc<ThemeData>> {
self.begin.clone()
}
fn set_begin(&mut self, _app: &mut App, begin: Option<Rc<ThemeData>>) {
self.begin = begin;
}
fn end(&self, _app: &App) -> Option<Rc<ThemeData>> {
self.end.clone()
}
fn set_end(&mut self, _app: &mut App, end: Option<Rc<ThemeData>>) {
self.end = end;
}
}
#[derive(Debug)]
pub struct AnimatedTheme {
implicitly_animated_widget: ImplicitlyAnimatedWidgetData,
pub data: Rc<ThemeData>,
pub child: WidgetRef,
}
impl AnimatedTheme {
pub fn new<K>(data: impl Into<Rc<ThemeData>>, child: impl IntoWidget<K>) -> AnimatedTheme {
AnimatedTheme {
implicitly_animated_widget: ImplicitlyAnimatedWidgetData::new(
K_THEME_ANIMATION_DURATION,
),
data: data.into(),
child: child.into_widget(),
}
}
pub fn duration(mut self, duration: Duration) -> AnimatedTheme {
self.implicitly_animated_widget.duration = duration;
self
}
waft_widgets::implicitly_animated_widget_setters!(AnimatedTheme);
}
impl ImplicitlyAnimatedWidget for AnimatedTheme {
waft_widgets::implicitly_animated_widget_accessors!();
}
impl StatefulWidget for AnimatedTheme {
type State = AnimatedThemeState;
fn key(&self) -> Option<&KeyRef> {
self.implicitly_animated_widget.key.as_ref()
}
fn create_state(&self) -> AnimatedThemeState {
AnimatedThemeState {
state: StateData::new(),
single_ticker_provider: SingleTickerProviderStateMixinData::new(),
implicitly_animated_widget_state: ImplicitlyAnimatedWidgetStateData::new(),
data: None,
}
}
}
pub struct AnimatedThemeState {
state: StateData<AnimatedTheme>,
single_ticker_provider: SingleTickerProviderStateMixinData,
implicitly_animated_widget_state: ImplicitlyAnimatedWidgetStateData,
data: Option<ThemeDataTween>,
}
impl SingleTickerProviderStateMixin for AnimatedThemeState {
fn single_ticker_provider_data(
self: Handle<Self>,
app: &App,
) -> &SingleTickerProviderStateMixinData {
&app.get(self).single_ticker_provider
}
fn single_ticker_provider_data_mut(
self: Handle<Self>,
app: &mut App,
) -> &mut SingleTickerProviderStateMixinData {
&mut app.get_mut(self).single_ticker_provider
}
}
impl TickerProviderObject for AnimatedThemeState {
fn create_ticker(self: Handle<Self>, app: &mut App, on_tick: TickerCallback) -> Ref<Ticker> {
SingleTickerProviderStateMixin::create_ticker(self, app, on_tick)
}
}
impl ImplicitlyAnimatedWidgetState for AnimatedThemeState {
waft_widgets::implicitly_animated_widget_state_accessors!();
fn for_each_tween<V: TweenVisitor>(self: Handle<Self>, app: &mut App, visitor: &mut V) {
let tween = app.get(self).data.clone();
let target = Some(Rc::clone(&self.widget(app).data));
app.get_mut(self).data = visitor.visit(app, tween, target, |_app, value| {
ThemeDataTween::new(Some(Rc::clone(value)), None)
});
}
}
impl AnimatedWidgetBaseState for AnimatedThemeState {}
impl State for AnimatedThemeState {
type Widget = AnimatedTheme;
waft_widgets::state_accessors!();
fn init_state(self: Handle<Self>, app: &mut App) {
AnimatedWidgetBaseState::init_state(self, app);
}
fn activate(self: Handle<Self>, app: &mut App) {
SingleTickerProviderStateMixin::activate(self, app);
}
fn did_update_widget(self: Handle<Self>, app: &mut App, old_widget: &AnimatedTheme) {
ImplicitlyAnimatedWidgetState::did_update_widget(self, app, old_widget);
}
fn dispose(self: Handle<Self>, app: &mut App) {
ImplicitlyAnimatedWidgetState::dispose(self, app);
}
fn build(self: Handle<Self>, app: &mut App, _context: BuildContext) -> WidgetRef {
let animation = self.animation(app);
let data = app
.get(self)
.data
.clone()
.expect("for_each_tween made the tween");
let child = self.widget(app).child.clone();
Theme::new(data.evaluate(app, animation), child).into_widget()
}
}
#[cfg(test)]
mod tests {
use std::cell::{Cell, RefCell};
use std::rc::Rc;
use waft_cupertino::{CupertinoColors, CupertinoThemeData};
use waft_embedder::ViewRef;
use waft_embedder::{Color, TargetPlatform};
use waft_painting::AnyColor;
use waft_scheduler::{FrameTarget, SchedulerBinding};
use waft_widgets::{Builder, IconThemeData, MediaQueryData, RawView, SizedBox, WidgetsBinding};
use waft_widgets::testing::{
TargetPlatformVariant, WidgetTester, find, test_widgets, test_widgets_variant,
};
use super::*;
use crate::color_scheme::ColorScheme;
use crate::colors::{Colors, MaterialColor};
use crate::test_support::probe;
use crate::text_selection_theme::TextSelectionThemeData;
use crate::theme_data::ThemeDataBuilder;
use crate::typography::Typography;
fn themed(data: ThemeData) -> impl FnOnce(&App, WidgetRef) -> WidgetRef {
move |_app, child| Theme::new(data, child).into_widget()
}
fn build_without_media_query(tester: &mut WidgetTester, child: WidgetRef) {
{
let mut app = tester.cell.borrow_mut();
let view = Rc::clone(tester.view()) as ViewRef;
let view_id = view.id();
let root = RawView::new(view, child).into_widget();
WidgetsBinding::instance(&mut app).attach_root_widget(&mut app, root);
SchedulerBinding::schedule_frame(&mut app, FrameTarget::View(view_id));
}
tester.pump();
}
#[test]
fn theme_data_tween_control_test() {
test_widgets(|tester| {
let app = tester.cell.borrow();
let light = Rc::new(ThemeData::new(&app));
let dark = Rc::new(ThemeData::dark(&app, None));
let tween = ThemeDataTween::new(Some(Rc::clone(&light)), Some(Rc::clone(&dark)));
assert_eq!(*tween.lerp(0.25), ThemeData::lerp(&light, &dark, 0.25));
});
}
#[test]
fn brightness_of_is_the_media_query_brightness_without_a_theme() {
test_widgets(|tester| {
let (brightness, maybe_brightness) = probe(
tester,
|_app, child| {
MediaQuery::new(
MediaQueryData::new().platform_brightness(Brightness::Dark),
child,
)
.into_widget()
},
|app, context| {
(
Theme::brightness_of(app, context),
Theme::maybe_brightness_of(app, context),
)
},
);
assert_eq!(brightness, Brightness::Dark);
assert_eq!(maybe_brightness, Some(Brightness::Dark));
});
}
#[test]
fn brightness_of_prefers_the_theme_brightness_over_the_media_query() {
test_widgets(|tester| {
let (brightness, maybe_brightness) = probe(
tester,
|app, child| {
let data = ThemeData::builder()
.brightness(Brightness::Light)
.build(app);
MediaQuery::new(
MediaQueryData::new().platform_brightness(Brightness::Dark),
Theme::new(data, child),
)
.into_widget()
},
|app, context| {
(
Theme::brightness_of(app, context),
Theme::maybe_brightness_of(app, context),
)
},
);
assert_eq!(brightness, Brightness::Light);
assert_eq!(maybe_brightness, Some(Brightness::Light));
});
}
#[test]
fn brightness_of_is_light_and_maybe_brightness_of_is_none_without_a_theme_or_media_query() {
test_widgets(|tester| {
let seen = Rc::new(Cell::new(None));
let probe = Builder::new({
let seen = Rc::clone(&seen);
move |app, context| {
seen.set(Some((
Theme::brightness_of(app, context),
Theme::maybe_brightness_of(app, context),
)));
SizedBox::shrink().into_widget()
}
});
build_without_media_query(tester, probe.into_widget());
assert_eq!(seen.get(), Some((Brightness::Light, None)));
});
}
#[test]
fn fallback_theme_is_the_localized_fallback() {
for use_material3 in [false, true] {
test_widgets(|tester| {
let theme = probe(
tester,
move |app, child| {
let data = ThemeData::builder().use_material3(use_material3).build(app);
Theme::new(data, child).into_widget()
},
Theme::of,
);
let mut app = tester.cell.borrow_mut();
let fallback = Rc::new(ThemeData::fallback(&app, Some(use_material3)));
let geometry = Rc::new(if use_material3 {
Typography::english_like2021()
} else {
Typography::english_like2014()
});
assert_eq!(theme, ThemeData::localize(&mut app, &fallback, &geometry));
});
}
}
#[test]
fn theme_data_localize_memoizes_the_result() {
test_widgets(|tester| {
let mut app = tester.cell.borrow_mut();
let light = Rc::new(ThemeData::new(&app));
let dark = Rc::new(ThemeData::dark(&app, None));
let default_geometry_theme = Rc::new(Typography::english_like2014());
let tall2014 = Rc::new(Typography::tall2014());
let first = ThemeData::localize(&mut app, &light, &default_geometry_theme);
let second = ThemeData::localize(&mut app, &light, &default_geometry_theme);
assert!(Rc::ptr_eq(&first, &second));
let tall = ThemeData::localize(&mut app, &light, &tall2014);
assert!(!Rc::ptr_eq(&first, &tall));
let dark_localized = ThemeData::localize(&mut app, &dark, &default_geometry_theme);
assert!(!Rc::ptr_eq(&first, &dark_localized));
});
}
#[test]
fn fallback_theme_follows_the_target_platform() {
for platform in [
TargetPlatform::Android,
TargetPlatform::Fuchsia,
TargetPlatform::IOS,
TargetPlatform::Linux,
TargetPlatform::MacOS,
TargetPlatform::Windows,
] {
test_widgets_variant(TargetPlatformVariant::only(platform), |tester| {
let theme = probe(tester, |_app, child| child, Theme::of);
assert_eq!(theme.platform, platform);
});
}
}
#[test]
fn theme_data_with_null_typography_uses_proper_defaults() {
test_widgets(|tester| {
let app = tester.cell.borrow();
let m2_theme = ThemeData::builder().use_material3(false).build(&app);
assert_eq!(m2_theme.typography, Typography::material2014().build());
let m3_theme = ThemeData::new(&app);
assert_eq!(
m3_theme.typography,
Typography::material2021()
.color_scheme(m3_theme.color_scheme.clone())
.build()
);
});
}
#[test]
fn icon_themes_are_applied_and_animated() {
test_widgets(|tester| {
let seen = Rc::new(Cell::new(None));
let child = Builder::new({
let seen = Rc::clone(&seen);
move |app, context| {
let icon_theme = IconTheme::of(app, context);
seen.set(Some((
icon_theme.color.as_ref().map(AnyColor::color),
icon_theme.size,
)));
SizedBox::shrink().into_widget()
}
})
.into_widget();
let animated = |app: &App, color: Color, size: f64| {
let data = ThemeData::builder()
.icon_theme(IconThemeData::new().color(color).size(size))
.build(app);
AnimatedTheme::new(data, child.clone()).into_widget()
};
let green = Colors::GREEN.primary();
let orange = Colors::ORANGE.primary();
let tree = animated(&tester.cell.borrow(), green, 10.0);
tester.pump_widget(tree);
assert_eq!(seen.get(), Some((Some(green), Some(10.0))));
let tree = animated(&tester.cell.borrow(), orange, 20.0);
tester.pump_widget(tree);
tester.pump_by(Duration::from_millis(100));
let (color, size) = seen.get().expect("the child built");
assert_eq!(color, Color::lerp(Some(green), Some(orange), 0.5));
assert_eq!(size, Some(15.0));
tester.pump_by(Duration::from_millis(100));
assert_eq!(seen.get(), Some((Some(orange), Some(20.0))));
});
}
#[test]
fn same_theme_data_reapplied_does_not_trigger_descendant_rebuilds() {
test_widgets(|tester| {
let builds = Rc::new(Cell::new(0));
let child = Builder::new({
let builds = Rc::clone(&builds);
move |app, context| {
Theme::of(app, context);
builds.set(builds.get() + 1);
SizedBox::shrink().into_widget()
}
})
.into_widget();
let cell = Rc::clone(&tester.cell);
let build_theme = |primary_color: u32| {
let data = ThemeData::builder()
.primary_color(Color::new(primary_color))
.build(&cell.borrow());
Theme::new(data, child.clone()).into_widget()
};
tester.pump_widget(build_theme(0xFF000000));
assert_eq!(builds.get(), 1);
tester.pump_widget(build_theme(0xFF000000));
assert_eq!(builds.get(), 1);
tester.pump_widget(build_theme(0xFF222222));
assert_eq!(builds.get(), 2);
});
}
#[test]
fn text_geometry_set_in_theme_has_higher_precedence_than_that_of_localizations() {
test_widgets(|tester| {
let k_magic_font_size = 4321.0;
let custom_theme = {
let fallback = ThemeData::fallback(&tester.cell.borrow(), None);
let body_medium = fallback
.primary_text_theme
.body_medium
.as_ref()
.expect("a body style")
.copy_with()
.font_size(k_magic_font_size);
fallback
.copy_with()
.primary_text_theme(
fallback
.primary_text_theme
.copy_with()
.body_medium(body_medium),
)
.build()
};
let actual_font_size = probe(tester, themed(custom_theme), |app, context| {
Theme::of(app, context)
.primary_text_theme
.body_medium
.as_ref()
.and_then(|style| style.font_size)
});
assert_eq!(actual_font_size, Some(k_magic_font_size));
});
}
#[test]
fn material3_default_theme_provides_all_basic_text_style_properties() {
test_widgets(|tester| {
let theme = probe(
tester,
|app, child| themed(ThemeData::new(app))(app, child),
Theme::of,
);
for text_theme in [&theme.text_theme, &theme.primary_text_theme] {
let styles = [
&text_theme.display_large,
&text_theme.display_medium,
&text_theme.display_small,
&text_theme.headline_large,
&text_theme.headline_medium,
&text_theme.headline_small,
&text_theme.title_large,
&text_theme.title_medium,
&text_theme.body_large,
&text_theme.body_medium,
&text_theme.body_small,
&text_theme.label_large,
&text_theme.label_medium,
];
for style in styles {
let style = style.as_ref().expect("a style");
assert!(!style.inherit);
assert!(style.color.is_some());
assert!(style.font_family.is_some());
assert!(style.font_size.is_some());
assert!(style.font_weight.is_some());
assert!(style.font_style.is_none());
assert!(style.letter_spacing.is_some());
assert!(style.word_spacing.is_none());
assert!(style.text_baseline.is_some());
assert!(style.height.is_some());
assert_eq!(style.decoration, Some(waft_embedder::TextDecoration::NONE));
assert!(style.decoration_color.is_some());
assert!(style.decoration_style.is_none());
assert!(style.debug_label.is_some());
assert!(style.background.is_none());
}
}
assert_eq!(
theme
.text_theme
.display_large
.as_ref()
.and_then(|style| style.debug_label.clone())
.as_deref(),
Some(
"(englishLike displayLarge 2021).merge((blackMountainView displayLarge).apply)"
)
);
});
}
#[test]
fn inherited_theme_capture_reproduces_the_theme_elsewhere() {
test_widgets(|tester| {
let (captured, theme) = probe(
tester,
|app, child| {
let data = ThemeData::builder()
.primary_color(Colors::RED.primary())
.build(app);
Theme::new(data, child).into_widget()
},
|app, context| {
(
<dyn waft_widgets::InheritedTheme>::capture(app, context, None),
Theme::of(app, context),
)
},
);
let recaptured = probe(tester, |_app, child| captured.wrap(child), Theme::of);
assert_eq!(recaptured, theme);
assert_eq!(recaptured.primary_color, Colors::RED.primary());
});
}
fn cupertino_theme_of(
tester: &mut WidgetTester,
data: impl FnOnce(&App) -> ThemeData,
) -> CupertinoThemeData {
probe(
tester,
|app, child| Theme::new(data(app), child).into_widget(),
CupertinoTheme::of,
)
}
fn any(color: Color) -> AnyColor {
AnyColor::new(color)
}
#[test]
fn material2_default_light_and_dark_cupertino_themes_have_defaults() {
test_widgets(|tester| {
let light = cupertino_theme_of(tester, |app| {
ThemeData::builder().use_material3(false).build(app)
});
assert_eq!(light.brightness(), Some(Brightness::Light));
assert_eq!(light.primary_color(), any(Colors::BLUE.primary()));
assert_eq!(
light.scaffold_background_color(),
any(Colors::GREY.shade50())
);
assert_eq!(light.primary_contrasting_color(), any(Colors::WHITE));
let text_style = light.text_theme().text_style();
assert_eq!(
text_style.font_family.as_deref(),
Some("CupertinoSystemText")
);
assert_eq!(text_style.font_size, Some(17.0));
let dark = cupertino_theme_of(tester, |app| ThemeData::dark(app, Some(false)));
assert_eq!(dark.brightness(), Some(Brightness::Dark));
assert_eq!(dark.primary_color(), any(Colors::BLUE.primary()));
assert_eq!(dark.primary_contrasting_color(), any(Colors::WHITE));
assert_eq!(
dark.scaffold_background_color(),
any(Colors::GREY.get(&850).expect("grey 850"))
);
let text_style = dark.text_theme().text_style();
assert_eq!(
text_style.font_family.as_deref(),
Some("CupertinoSystemText")
);
assert_eq!(text_style.font_size, Some(17.0));
});
}
#[test]
fn material3_default_light_and_dark_cupertino_themes_have_defaults() {
test_widgets(|tester| {
let light = cupertino_theme_of(tester, ThemeData::new);
assert_eq!(light.brightness(), Some(Brightness::Light));
assert_eq!(light.primary_color(), any(Color::new(0xff6750a4)));
assert_eq!(
light.scaffold_background_color(),
any(Color::new(0xfffef7ff))
);
assert_eq!(light.primary_contrasting_color(), any(Colors::WHITE));
let text_style = light.text_theme().text_style();
assert_eq!(
text_style.font_family.as_deref(),
Some("CupertinoSystemText")
);
assert_eq!(text_style.font_size, Some(17.0));
let dark = cupertino_theme_of(tester, |app| ThemeData::dark(app, None));
assert_eq!(dark.brightness(), Some(Brightness::Dark));
assert_eq!(dark.primary_color(), any(Color::new(0xffd0bcff)));
assert_eq!(
dark.primary_contrasting_color(),
any(Color::new(0xff381e72))
);
assert_eq!(
dark.scaffold_background_color(),
any(Color::new(0xff141218))
);
});
}
#[test]
fn material_theme_overrides_the_cupertino_brightness() {
test_widgets(|tester| {
let mut brightness_of = |data: fn(&App) -> ThemeData| {
probe(
tester,
|app, child| Theme::new(data(app), child).into_widget(),
CupertinoTheme::brightness_of,
)
};
assert_eq!(
brightness_of(|app| ThemeData::dark(app, None)),
Brightness::Dark
);
assert_eq!(brightness_of(ThemeData::new), Brightness::Light);
assert_eq!(
brightness_of(|app| {
ThemeData::builder()
.brightness(Brightness::Light)
.cupertino_override_theme(
CupertinoThemeData::new()
.with_brightness(Brightness::Dark)
.no_default(),
)
.build(app)
}),
Brightness::Dark
);
assert_eq!(
brightness_of(|app| {
ThemeData::builder()
.brightness(Brightness::Dark)
.cupertino_override_theme(
CupertinoThemeData::new()
.with_brightness(Brightness::Light)
.no_default(),
)
.build(app)
}),
Brightness::Light
);
});
}
#[test]
fn cupertino_override_theme_overrides_the_material_theme_and_keeps_the_rest_cascaded() {
for use_material3 in [false, true] {
test_widgets(|tester| {
let theme = cupertino_theme_of(tester, move |app| {
ThemeData::builder()
.cupertino_override_theme(
CupertinoThemeData::new()
.with_scaffold_background_color(
CupertinoColors::LIGHT_BACKGROUND_GRAY,
)
.no_default(),
)
.use_material3(use_material3)
.build(app)
});
assert_eq!(theme.brightness(), Some(Brightness::Light));
let primary = if use_material3 {
Color::new(0xff6750a4)
} else {
Colors::BLUE.primary()
};
assert_eq!(theme.primary_color(), any(primary));
assert_eq!(theme.primary_contrasting_color(), any(Colors::WHITE));
assert_eq!(
theme.scaffold_background_color(),
CupertinoColors::LIGHT_BACKGROUND_GRAY
);
let text_style = theme.text_theme().text_style();
assert_eq!(
text_style.font_family.as_deref(),
Some("CupertinoSystemText")
);
assert_eq!(text_style.font_size, Some(17.0));
});
}
}
#[test]
fn cupertino_override_theme_overrides_properties_that_are_independent_of_material() {
for use_material3 in [false, true] {
test_widgets(|tester| {
let theme = cupertino_theme_of(tester, move |app| {
ThemeData::builder()
.cupertino_override_theme(
CupertinoThemeData::new()
.with_bar_background_color(CupertinoColors::BLACK)
.no_default(),
)
.use_material3(use_material3)
.build(app)
});
let primary = if use_material3 {
Color::new(0xff6750a4)
} else {
Colors::BLUE.primary()
};
assert_eq!(theme.primary_color(), any(primary));
assert_eq!(theme.bar_background_color(), CupertinoColors::BLACK);
});
}
}
#[test]
fn changing_the_material_theme_rebuilds_cupertino_dependents() {
test_widgets(|tester| {
let builds = Rc::new(Cell::new(0));
let primary = Rc::new(Cell::new(None));
let child = Builder::new({
let builds = Rc::clone(&builds);
let primary = Rc::clone(&primary);
move |app, context| {
builds.set(builds.get() + 1);
primary.set(Some(
CupertinoTheme::of(app, context).primary_color().color(),
));
SizedBox::shrink().into_widget()
}
})
.into_widget();
let cell = Rc::clone(&tester.cell);
let build_theme = |color: Color| {
let data = ThemeData::builder()
.color_scheme(ColorScheme::light().with_primary(color))
.build(&cell.borrow());
Theme::new(data, child.clone()).into_widget()
};
tester.pump_widget(build_theme(Colors::RED.primary()));
assert_eq!(builds.get(), 1);
assert_eq!(primary.get(), Some(Colors::RED.primary()));
tester.pump_widget(build_theme(Colors::ORANGE.primary()));
assert_eq!(builds.get(), 2);
assert_eq!(primary.get(), Some(Colors::ORANGE.primary()));
});
}
#[test]
fn cupertino_theme_data_does_not_override_the_material_icon_theme() {
test_widgets(|tester| {
let material_icon_color = Colors::BLUE.primary();
let cupertino_icon_color = Colors::BLACK;
let icon_color = probe(
tester,
move |app, child| {
let data = ThemeData::builder()
.icon_theme(IconThemeData::new().color(material_icon_color))
.cupertino_override_theme(
CupertinoThemeData::new()
.with_primary_color(cupertino_icon_color)
.no_default(),
)
.build(app);
Theme::new(data, child).into_widget()
},
|app, context| IconTheme::of(app, context).color,
);
assert_eq!(icon_color, Some(any(material_icon_color)));
});
}
struct CupertinoThemeTester {
build_count: Rc<Cell<usize>>,
actual_theme: Rc<RefCell<Option<CupertinoThemeData>>>,
singleton_theme_subtree: WidgetRef,
}
impl CupertinoThemeTester {
fn new() -> CupertinoThemeTester {
let build_count = Rc::new(Cell::new(0));
let actual_theme = Rc::new(RefCell::new(None));
let singleton_theme_subtree = Builder::new({
let build_count = Rc::clone(&build_count);
let actual_theme = Rc::clone(&actual_theme);
move |app, context| {
build_count.set(build_count.get() + 1);
*actual_theme.borrow_mut() = Some(CupertinoTheme::of(app, context));
SizedBox::shrink().into_widget()
}
})
.into_widget();
CupertinoThemeTester {
build_count,
actual_theme,
singleton_theme_subtree,
}
}
fn test_theme(
&self,
tester: &mut WidgetTester,
theme: impl FnOnce(&App) -> ThemeData,
) -> CupertinoThemeData {
let data = theme(&tester.cell.borrow());
tester
.pump_widget(Theme::new(data, self.singleton_theme_subtree.clone()).into_widget());
let actual_theme = self.actual_theme.borrow().clone();
actual_theme.expect("the subtree built")
}
fn build_count(&self) -> usize {
self.build_count.get()
}
}
fn material_theme_with_primary(
use_material3: bool,
primary: MaterialColor,
) -> ThemeDataBuilder {
if use_material3 {
ThemeData::builder().color_scheme(ColorScheme::light().with_primary(primary.primary()))
} else {
ThemeData::builder()
.use_material3(false)
.primary_swatch(primary)
}
}
#[test]
fn changing_cupertino_theme_override_triggers_rebuilds() {
test_widgets(|tester| {
let subtree = CupertinoThemeTester::new();
let theme = subtree.test_theme(tester, |app| {
ThemeData::builder()
.primary_swatch(Colors::PURPLE)
.cupertino_override_theme(
CupertinoThemeData::new()
.with_primary_color(CupertinoColors::ACTIVE_ORANGE)
.no_default(),
)
.build(app)
});
assert_eq!(subtree.build_count(), 1);
assert_eq!(theme.primary_color(), CupertinoColors::ACTIVE_ORANGE);
let theme = subtree.test_theme(tester, |app| {
ThemeData::builder()
.primary_swatch(Colors::PURPLE)
.cupertino_override_theme(
CupertinoThemeData::new()
.with_primary_color(CupertinoColors::ACTIVE_GREEN)
.no_default(),
)
.build(app)
});
assert_eq!(subtree.build_count(), 2);
assert_eq!(theme.primary_color(), CupertinoColors::ACTIVE_GREEN);
});
}
#[test]
fn cupertino_theme_override_blocks_derivative_changes() {
test_widgets(|tester| {
let subtree = CupertinoThemeTester::new();
let theme = subtree.test_theme(tester, |app| {
ThemeData::builder()
.primary_swatch(Colors::PURPLE)
.cupertino_override_theme(
CupertinoThemeData::new()
.with_primary_color(CupertinoColors::ACTIVE_ORANGE)
.no_default(),
)
.build(app)
});
assert_eq!(subtree.build_count(), 1);
assert_eq!(theme.primary_color(), CupertinoColors::ACTIVE_ORANGE);
let theme = subtree.test_theme(tester, |app| {
ThemeData::builder()
.primary_swatch(Colors::BLUE)
.cupertino_override_theme(
CupertinoThemeData::new()
.with_primary_color(CupertinoColors::SYSTEM_RED)
.no_default(),
)
.build(app)
});
assert_eq!(subtree.build_count(), 2);
assert_eq!(theme.primary_color(), CupertinoColors::SYSTEM_RED);
});
}
#[test]
fn cupertino_overrides_do_not_block_derivatives_triggering_rebuilds_when_derivatives_are_not_overridden()
{
for use_material3 in [false, true] {
test_widgets(|tester| {
let subtree = CupertinoThemeTester::new();
let theme = subtree.test_theme(tester, |app| {
material_theme_with_primary(use_material3, Colors::PURPLE)
.cupertino_override_theme(
CupertinoThemeData::new()
.with_primary_contrasting_color(CupertinoColors::DESTRUCTIVE_RED)
.no_default(),
)
.build(app)
});
assert_eq!(subtree.build_count(), 1);
assert_eq!(
theme.text_theme().action_text_style().color,
Some(any(Colors::PURPLE.primary()))
);
assert_eq!(
theme.primary_contrasting_color(),
CupertinoColors::DESTRUCTIVE_RED
);
let theme = subtree.test_theme(tester, |app| {
material_theme_with_primary(use_material3, Colors::GREEN)
.cupertino_override_theme(
CupertinoThemeData::new()
.with_primary_contrasting_color(CupertinoColors::DESTRUCTIVE_RED)
.no_default(),
)
.build(app)
});
assert_eq!(subtree.build_count(), 2);
assert_eq!(
theme.text_theme().action_text_style().color,
Some(any(Colors::GREEN.primary()))
);
assert_eq!(
theme.primary_contrasting_color(),
CupertinoColors::DESTRUCTIVE_RED
);
});
}
}
#[test]
fn copy_with_only_copies_the_overrides_not_the_material_or_cupertino_derivatives() {
for use_material3 in [false, true] {
test_widgets(|tester| {
let subtree = CupertinoThemeTester::new();
let original_theme = subtree.test_theme(tester, |app| {
material_theme_with_primary(use_material3, Colors::PURPLE)
.cupertino_override_theme(
CupertinoThemeData::new()
.with_primary_contrasting_color(CupertinoColors::ACTIVE_ORANGE)
.no_default(),
)
.build(app)
});
let copied_theme = original_theme
.copy_with()
.with_bar_background_color(CupertinoColors::DESTRUCTIVE_RED);
let theme = subtree.test_theme(tester, |app| {
material_theme_with_primary(use_material3, Colors::BLUE)
.cupertino_override_theme(copied_theme.no_default())
.build(app)
});
assert_eq!(theme.primary_color(), any(Colors::BLUE.primary()));
assert_eq!(
theme.primary_contrasting_color(),
CupertinoColors::ACTIVE_ORANGE
);
assert_eq!(
theme.bar_background_color(),
CupertinoColors::DESTRUCTIVE_RED
);
});
}
}
#[test]
fn material_themes_with_no_cupertino_overrides_can_also_be_copy_with_ed() {
for use_material3 in [false, true] {
test_widgets(|tester| {
let subtree = CupertinoThemeTester::new();
let original_theme = subtree.test_theme(tester, |app| {
material_theme_with_primary(use_material3, Colors::PURPLE).build(app)
});
let copied_theme = original_theme
.copy_with()
.with_primary_contrasting_color(CupertinoColors::DESTRUCTIVE_RED);
let theme = subtree.test_theme(tester, |app| {
material_theme_with_primary(use_material3, Colors::BLUE)
.cupertino_override_theme(copied_theme.no_default())
.build(app)
});
assert_eq!(theme.primary_color(), any(Colors::BLUE.primary()));
assert_eq!(
theme.primary_contrasting_color(),
CupertinoColors::DESTRUCTIVE_RED
);
});
}
}
#[test]
fn material_based_cupertino_theme_data_resolves_from_the_material_theme() {
test_widgets(|tester| {
let (material, resolved) = probe(
tester,
|_app, child| child,
|app, context| {
let material = Rc::new(ThemeData::dark(app, None));
let resolved =
MaterialBasedCupertinoThemeData::new(&material).resolve_from(app, context);
(material, resolved)
},
);
assert_eq!(resolved.brightness(), Some(Brightness::Dark));
assert_eq!(
resolved.primary_color(),
any(material.color_scheme.primary())
);
assert_eq!(
resolved.primary_contrasting_color(),
any(material.color_scheme.on_primary())
);
assert_eq!(
resolved.scaffold_background_color(),
any(material.scaffold_background_color)
);
let no_default = resolved.no_default();
assert!(no_default.primary_color.is_none());
assert!(no_default.text_theme.is_some());
assert_eq!(
resolved.text_theme().action_text_style().color,
Some(any(material.color_scheme.primary()))
);
assert_eq!(
resolved
.copy_with()
.with_primary_color(Colors::RED.primary())
.primary_color(),
any(Colors::RED.primary())
);
assert_eq!(
resolved.bar_background_color(),
CupertinoThemeData::new().bar_background_color()
);
});
}
fn selection_colors(
app: &mut App,
context: BuildContext,
) -> (Option<AnyColor>, Option<AnyColor>) {
let style = DefaultSelectionStyle::of(app, context);
(style.selection_color, style.cursor_color)
}
#[test]
fn theme_provides_a_default_selection_style_from_its_text_selection_theme() {
test_widgets(|tester| {
let seen = probe(
tester,
|app, child| {
let data = ThemeData::builder()
.text_selection_theme(
TextSelectionThemeData::new()
.selection_color(Color::new(0x88888888))
.cursor_color(Color::new(0xffaabbcc)),
)
.build(app);
Theme::new(data, child).into_widget()
},
selection_colors,
);
assert_eq!(
seen,
(
Some(any(Color::new(0x88888888))),
Some(any(Color::new(0xffaabbcc)))
)
);
});
}
#[test]
fn theme_keeps_the_ambient_selection_style_where_its_text_selection_theme_is_unset() {
test_widgets(|tester| {
let seen = probe(
tester,
|app, child| {
let data = ThemeData::builder()
.text_selection_theme(
TextSelectionThemeData::new().cursor_color(Color::new(0xffaabbcc)),
)
.build(app);
DefaultSelectionStyle::new(Theme::new(data, child))
.selection_color(Color::new(0xffaa1111))
.cursor_color(Color::new(0x00cc2222))
.into_widget()
},
selection_colors,
);
assert_eq!(
seen,
(
Some(any(Color::new(0xffaa1111))),
Some(any(Color::new(0xffaabbcc)))
)
);
});
}
#[test]
fn material_based_cupertino_theme_data_takes_the_selection_handle_color_from_the_text_selection_theme()
{
test_widgets(|tester| {
let (from_material, overridden) = probe(
tester,
|_app, child| child,
|app, _context| {
let material = ThemeData::builder()
.text_selection_theme(
TextSelectionThemeData::new()
.selection_handle_color(Color::new(0xaabbccdd)),
)
.build(app);
let theme = MaterialBasedCupertinoThemeData::new(&Rc::new(material));
(
theme.selection_handle_color(),
theme
.copy_with()
.with_selection_handle_color(Colors::RED.primary())
.selection_handle_color(),
)
},
);
assert_eq!(from_material, any(Color::new(0xaabbccdd)));
assert_eq!(overridden, any(Colors::RED.primary()));
});
}
#[test]
fn cupertino_widgets_correctly_get_the_right_text_theme_in_dark_mode() {
test_widgets(|tester| {
let text_field_key = waft_widgets::GlobalKey::new();
let theme = ThemeData::dark(&tester.cell.borrow(), None);
tester.pump_widget(crate::test_support::material_app(
theme,
waft_cupertino::CupertinoTextField::new()
.key(text_field_key.clone().into())
.into_widget(),
));
tester.pump_and_settle();
let editable_text = find::by_type::<waft_widgets::EditableText>().evaluate(tester);
assert_eq!(editable_text.len(), 1);
let style_color = tester
.widget::<waft_widgets::EditableText, _>(editable_text[0], |editable_text| {
editable_text.style.color.clone()
});
let cupertino_theme_data = {
let mut app = tester.cell.borrow_mut();
let context = text_field_key
.current_context(&app)
.expect("the text field is mounted");
CupertinoTheme::of(&mut app, context)
};
let text_style_color = cupertino_theme_data
.text_theme()
.text_style()
.color
.expect("a text colour");
let theme_text_style_color = text_style_color
.extension::<waft_cupertino::CupertinoDynamicColor>()
.expect("a CupertinoDynamicColor");
assert_eq!(
style_color.map(|color| color.to_argb32()),
Some(theme_text_style_color.dark_color.to_argb32())
);
});
}
}