use waft_embedder::{Color, FontFeature, FontVariation, TextDecoration, TextDecorationStyle};
use waft_foundation::{App, DiagnosticsProperty};
use waft_painting::TextStyle;
use waft_widgets::BuildContext;
use crate::theme::Theme;
#[derive(Clone, Debug, Default, PartialEq)]
pub struct TextTheme {
pub display_large: Option<TextStyle>,
pub display_medium: Option<TextStyle>,
pub display_small: Option<TextStyle>,
pub headline_large: Option<TextStyle>,
pub headline_medium: Option<TextStyle>,
pub headline_small: Option<TextStyle>,
pub title_large: Option<TextStyle>,
pub title_medium: Option<TextStyle>,
pub title_small: Option<TextStyle>,
pub body_large: Option<TextStyle>,
pub body_medium: Option<TextStyle>,
pub body_small: Option<TextStyle>,
pub label_large: Option<TextStyle>,
pub label_medium: Option<TextStyle>,
pub label_small: Option<TextStyle>,
}
impl TextTheme {
pub fn new() -> TextTheme {
TextTheme::default()
}
pub fn display_large(mut self, display_large: TextStyle) -> TextTheme {
self.display_large = Some(display_large);
self
}
pub fn display_medium(mut self, display_medium: TextStyle) -> TextTheme {
self.display_medium = Some(display_medium);
self
}
pub fn display_small(mut self, display_small: TextStyle) -> TextTheme {
self.display_small = Some(display_small);
self
}
pub fn headline_large(mut self, headline_large: TextStyle) -> TextTheme {
self.headline_large = Some(headline_large);
self
}
pub fn headline_medium(mut self, headline_medium: TextStyle) -> TextTheme {
self.headline_medium = Some(headline_medium);
self
}
pub fn headline_small(mut self, headline_small: TextStyle) -> TextTheme {
self.headline_small = Some(headline_small);
self
}
pub fn title_large(mut self, title_large: TextStyle) -> TextTheme {
self.title_large = Some(title_large);
self
}
pub fn title_medium(mut self, title_medium: TextStyle) -> TextTheme {
self.title_medium = Some(title_medium);
self
}
pub fn title_small(mut self, title_small: TextStyle) -> TextTheme {
self.title_small = Some(title_small);
self
}
pub fn body_large(mut self, body_large: TextStyle) -> TextTheme {
self.body_large = Some(body_large);
self
}
pub fn body_medium(mut self, body_medium: TextStyle) -> TextTheme {
self.body_medium = Some(body_medium);
self
}
pub fn body_small(mut self, body_small: TextStyle) -> TextTheme {
self.body_small = Some(body_small);
self
}
pub fn label_large(mut self, label_large: TextStyle) -> TextTheme {
self.label_large = Some(label_large);
self
}
pub fn label_medium(mut self, label_medium: TextStyle) -> TextTheme {
self.label_medium = Some(label_medium);
self
}
pub fn label_small(mut self, label_small: TextStyle) -> TextTheme {
self.label_small = Some(label_small);
self
}
pub fn copy_with(&self) -> TextTheme {
self.clone()
}
pub fn merge(&self, other: Option<&TextTheme>) -> TextTheme {
let Some(other) = other else {
return self.clone();
};
fn merge_styles(
style: &Option<TextStyle>,
other_style: &Option<TextStyle>,
) -> Option<TextStyle> {
match style {
Some(style) => Some(style.merge(other_style.as_ref())),
None => other_style.clone(),
}
}
TextTheme {
display_large: merge_styles(&self.display_large, &other.display_large),
display_medium: merge_styles(&self.display_medium, &other.display_medium),
display_small: merge_styles(&self.display_small, &other.display_small),
headline_large: merge_styles(&self.headline_large, &other.headline_large),
headline_medium: merge_styles(&self.headline_medium, &other.headline_medium),
headline_small: merge_styles(&self.headline_small, &other.headline_small),
title_large: merge_styles(&self.title_large, &other.title_large),
title_medium: merge_styles(&self.title_medium, &other.title_medium),
title_small: merge_styles(&self.title_small, &other.title_small),
body_large: merge_styles(&self.body_large, &other.body_large),
body_medium: merge_styles(&self.body_medium, &other.body_medium),
body_small: merge_styles(&self.body_small, &other.body_small),
label_large: merge_styles(&self.label_large, &other.label_large),
label_medium: merge_styles(&self.label_medium, &other.label_medium),
label_small: merge_styles(&self.label_small, &other.label_small),
}
}
pub fn apply(&self) -> TextThemeApply<'_> {
TextThemeApply::new(self)
}
pub fn lerp(a: Option<&TextTheme>, b: Option<&TextTheme>, t: f64) -> TextTheme {
if let (Some(a), Some(b)) = (a, b)
&& std::ptr::eq(a, b)
{
return a.clone();
}
let lerp = |style: fn(&TextTheme) -> Option<&TextStyle>| {
TextStyle::lerp(a.and_then(style), b.and_then(style), t)
};
TextTheme {
display_large: lerp(|theme| theme.display_large.as_ref()),
display_medium: lerp(|theme| theme.display_medium.as_ref()),
display_small: lerp(|theme| theme.display_small.as_ref()),
headline_large: lerp(|theme| theme.headline_large.as_ref()),
headline_medium: lerp(|theme| theme.headline_medium.as_ref()),
headline_small: lerp(|theme| theme.headline_small.as_ref()),
title_large: lerp(|theme| theme.title_large.as_ref()),
title_medium: lerp(|theme| theme.title_medium.as_ref()),
title_small: lerp(|theme| theme.title_small.as_ref()),
body_large: lerp(|theme| theme.body_large.as_ref()),
body_medium: lerp(|theme| theme.body_medium.as_ref()),
body_small: lerp(|theme| theme.body_small.as_ref()),
label_large: lerp(|theme| theme.label_large.as_ref()),
label_medium: lerp(|theme| theme.label_medium.as_ref()),
label_small: lerp(|theme| theme.label_small.as_ref()),
}
}
pub fn of(app: &mut App, context: BuildContext) -> TextTheme {
Theme::of(app, context).text_theme.clone()
}
pub fn primary_of(app: &mut App, context: BuildContext) -> TextTheme {
Theme::of(app, context).primary_text_theme.clone()
}
}
pub struct TextThemeApply<'a> {
theme: &'a TextTheme,
font_family: Option<String>,
font_family_fallback: Option<Vec<String>>,
package: Option<String>,
font_size_factor: f64,
font_size_delta: f64,
letter_spacing_factor: f64,
letter_spacing_delta: f64,
word_spacing_factor: f64,
word_spacing_delta: f64,
height_factor: f64,
height_delta: f64,
display_color: Option<Color>,
body_color: Option<Color>,
decoration: Option<TextDecoration>,
decoration_color: Option<Color>,
decoration_style: Option<TextDecorationStyle>,
font_features: Option<Vec<FontFeature>>,
font_variations: Option<Vec<FontVariation>>,
}
impl<'a> TextThemeApply<'a> {
fn new(theme: &'a TextTheme) -> TextThemeApply<'a> {
TextThemeApply {
theme,
font_family: None,
font_family_fallback: None,
package: None,
font_size_factor: 1.0,
font_size_delta: 0.0,
letter_spacing_factor: 1.0,
letter_spacing_delta: 0.0,
word_spacing_factor: 1.0,
word_spacing_delta: 0.0,
height_factor: 1.0,
height_delta: 0.0,
display_color: None,
body_color: None,
decoration: None,
decoration_color: None,
decoration_style: None,
font_features: None,
font_variations: None,
}
}
pub fn font_family(mut self, font_family: impl Into<String>) -> TextThemeApply<'a> {
self.font_family = Some(font_family.into());
self
}
pub fn font_family_fallback(mut self, font_family_fallback: Vec<String>) -> TextThemeApply<'a> {
self.font_family_fallback = Some(font_family_fallback);
self
}
pub fn package(mut self, package: impl Into<String>) -> TextThemeApply<'a> {
self.package = Some(package.into());
self
}
pub fn font_size_factor(mut self, font_size_factor: f64) -> TextThemeApply<'a> {
self.font_size_factor = font_size_factor;
self
}
pub fn font_size_delta(mut self, font_size_delta: f64) -> TextThemeApply<'a> {
self.font_size_delta = font_size_delta;
self
}
pub fn letter_spacing_factor(mut self, letter_spacing_factor: f64) -> TextThemeApply<'a> {
self.letter_spacing_factor = letter_spacing_factor;
self
}
pub fn letter_spacing_delta(mut self, letter_spacing_delta: f64) -> TextThemeApply<'a> {
self.letter_spacing_delta = letter_spacing_delta;
self
}
pub fn word_spacing_factor(mut self, word_spacing_factor: f64) -> TextThemeApply<'a> {
self.word_spacing_factor = word_spacing_factor;
self
}
pub fn word_spacing_delta(mut self, word_spacing_delta: f64) -> TextThemeApply<'a> {
self.word_spacing_delta = word_spacing_delta;
self
}
pub fn height_factor(mut self, height_factor: f64) -> TextThemeApply<'a> {
self.height_factor = height_factor;
self
}
pub fn height_delta(mut self, height_delta: f64) -> TextThemeApply<'a> {
self.height_delta = height_delta;
self
}
pub fn display_color(mut self, display_color: Color) -> TextThemeApply<'a> {
self.display_color = Some(display_color);
self
}
pub fn body_color(mut self, body_color: Color) -> TextThemeApply<'a> {
self.body_color = Some(body_color);
self
}
pub fn decoration(mut self, decoration: TextDecoration) -> TextThemeApply<'a> {
self.decoration = Some(decoration);
self
}
pub fn decoration_color(mut self, decoration_color: Color) -> TextThemeApply<'a> {
self.decoration_color = Some(decoration_color);
self
}
pub fn decoration_style(mut self, decoration_style: TextDecorationStyle) -> TextThemeApply<'a> {
self.decoration_style = Some(decoration_style);
self
}
pub fn font_features(mut self, font_features: Vec<FontFeature>) -> TextThemeApply<'a> {
self.font_features = Some(font_features);
self
}
pub fn font_variations(mut self, font_variations: Vec<FontVariation>) -> TextThemeApply<'a> {
self.font_variations = Some(font_variations);
self
}
pub fn into_text_theme(self) -> TextTheme {
let theme = self.theme;
let display_color = self.display_color;
let body_color = self.body_color;
TextTheme {
display_large: self.apply_to(theme.display_large.as_ref(), display_color),
display_medium: self.apply_to(theme.display_medium.as_ref(), display_color),
display_small: self.apply_to(theme.display_small.as_ref(), display_color),
headline_large: self.apply_to(theme.headline_large.as_ref(), display_color),
headline_medium: self.apply_to(theme.headline_medium.as_ref(), display_color),
headline_small: self.apply_to(theme.headline_small.as_ref(), body_color),
title_large: self.apply_to(theme.title_large.as_ref(), body_color),
title_medium: self.apply_to(theme.title_medium.as_ref(), body_color),
title_small: self.apply_to(theme.title_small.as_ref(), body_color),
body_large: self.apply_to(theme.body_large.as_ref(), body_color),
body_medium: self.apply_to(theme.body_medium.as_ref(), body_color),
body_small: self.apply_to(theme.body_small.as_ref(), display_color),
label_large: self.apply_to(theme.label_large.as_ref(), body_color),
label_medium: self.apply_to(theme.label_medium.as_ref(), body_color),
label_small: self.apply_to(theme.label_small.as_ref(), body_color),
}
}
fn apply_to(&self, style: Option<&TextStyle>, color: Option<Color>) -> Option<TextStyle> {
let mut apply = style?
.apply()
.font_size_factor(self.font_size_factor)
.font_size_delta(self.font_size_delta)
.letter_spacing_delta(self.letter_spacing_delta)
.letter_spacing_factor(self.letter_spacing_factor)
.word_spacing_delta(self.word_spacing_delta)
.word_spacing_factor(self.word_spacing_factor)
.height_factor(self.height_factor)
.height_delta(self.height_delta);
if let Some(color) = color {
apply = apply.color(color);
}
if let Some(decoration) = self.decoration {
apply = apply.decoration(decoration);
}
if let Some(decoration_color) = self.decoration_color {
apply = apply.decoration_color(decoration_color);
}
if let Some(decoration_style) = self.decoration_style {
apply = apply.decoration_style(decoration_style);
}
if let Some(font_family) = &self.font_family {
apply = apply.font_family(font_family.clone());
}
if let Some(font_family_fallback) = &self.font_family_fallback {
apply = apply.font_family_fallback(font_family_fallback.clone());
}
if let Some(package) = &self.package {
apply = apply.package(package.clone());
}
if let Some(font_features) = &self.font_features {
apply = apply.font_features(font_features.clone());
}
if let Some(font_variations) = &self.font_variations {
apply = apply.font_variations(font_variations.clone());
}
Some(apply.into_style())
}
}
impl From<TextThemeApply<'_>> for TextTheme {
fn from(apply: TextThemeApply<'_>) -> TextTheme {
apply.into_text_theme()
}
}
impl waft_foundation::Diagnosticable for TextTheme {
fn debug_fill_properties(&self, properties: &mut waft_foundation::DiagnosticPropertiesBuilder) {
let default_theme = crate::typography::Typography::material2018().build().black;
properties.add(
DiagnosticsProperty::<TextStyle>::new("displayLarge", self.display_large.clone())
.default_value(default_theme.display_large.clone()),
);
properties.add(
DiagnosticsProperty::<TextStyle>::new("displayMedium", self.display_medium.clone())
.default_value(default_theme.display_medium.clone()),
);
properties.add(
DiagnosticsProperty::<TextStyle>::new("displaySmall", self.display_small.clone())
.default_value(default_theme.display_small.clone()),
);
properties.add(
DiagnosticsProperty::<TextStyle>::new("headlineLarge", self.headline_large.clone())
.default_value(default_theme.headline_large.clone()),
);
properties.add(
DiagnosticsProperty::<TextStyle>::new("headlineMedium", self.headline_medium.clone())
.default_value(default_theme.headline_medium.clone()),
);
properties.add(
DiagnosticsProperty::<TextStyle>::new("headlineSmall", self.headline_small.clone())
.default_value(default_theme.headline_small.clone()),
);
properties.add(
DiagnosticsProperty::<TextStyle>::new("titleLarge", self.title_large.clone())
.default_value(default_theme.title_large.clone()),
);
properties.add(
DiagnosticsProperty::<TextStyle>::new("titleMedium", self.title_medium.clone())
.default_value(default_theme.title_medium.clone()),
);
properties.add(
DiagnosticsProperty::<TextStyle>::new("titleSmall", self.title_small.clone())
.default_value(default_theme.title_small.clone()),
);
properties.add(
DiagnosticsProperty::<TextStyle>::new("bodyLarge", self.body_large.clone())
.default_value(default_theme.body_large.clone()),
);
properties.add(
DiagnosticsProperty::<TextStyle>::new("bodyMedium", self.body_medium.clone())
.default_value(default_theme.body_medium.clone()),
);
properties.add(
DiagnosticsProperty::<TextStyle>::new("bodySmall", self.body_small.clone())
.default_value(default_theme.body_small.clone()),
);
properties.add(
DiagnosticsProperty::<TextStyle>::new("labelLarge", self.label_large.clone())
.default_value(default_theme.label_large.clone()),
);
properties.add(
DiagnosticsProperty::<TextStyle>::new("labelMedium", self.label_medium.clone())
.default_value(default_theme.label_medium.clone()),
);
properties.add(
DiagnosticsProperty::<TextStyle>::new("labelSmall", self.label_small.clone())
.default_value(default_theme.label_small.clone()),
);
}
}
impl std::fmt::Display for TextTheme {
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_embedder::{Brightness, FontStyle};
use waft_widgets::IntoWidget;
use waft_widgets::testing::test_widgets;
use super::*;
use crate::colors::Colors;
use crate::test_support::probe;
use crate::theme::Theme;
use crate::theme_data::ThemeData;
use crate::typography::Typography;
fn styles(theme: &TextTheme) -> [&TextStyle; 15] {
[
&theme.display_large,
&theme.display_medium,
&theme.display_small,
&theme.headline_large,
&theme.headline_medium,
&theme.headline_small,
&theme.title_large,
&theme.title_medium,
&theme.title_small,
&theme.body_large,
&theme.body_medium,
&theme.body_small,
&theme.label_large,
&theme.label_medium,
&theme.label_small,
]
.map(|style| style.as_ref().expect("the text theme has this style"))
}
fn optional_styles(theme: &TextTheme) -> [&Option<TextStyle>; 15] {
[
&theme.display_large,
&theme.display_medium,
&theme.display_small,
&theme.headline_large,
&theme.headline_medium,
&theme.headline_small,
&theme.title_large,
&theme.title_medium,
&theme.title_small,
&theme.body_large,
&theme.body_medium,
&theme.body_small,
&theme.label_large,
&theme.label_medium,
&theme.label_small,
]
}
fn color(color: Color) -> Option<waft_painting::AnyColor> {
Some(color.into())
}
#[test]
fn copy_with_apply_and_merge_of_an_empty_text_theme_are_empty() {
assert_eq!(TextTheme::new(), TextTheme::new().copy_with());
assert_eq!(TextTheme::new(), TextTheme::new().apply().into_text_theme());
assert_eq!(TextTheme::new(), TextTheme::new().merge(None));
}
#[test]
fn lerp_special_cases() {
assert_eq!(TextTheme::lerp(None, None, 0.0), TextTheme::new());
let theme = TextTheme::new();
assert_eq!(TextTheme::lerp(Some(&theme), Some(&theme), 0.5), theme);
}
#[test]
fn copy_with_apply_and_merge_of_typography_black() {
let typography = Typography::material2018().build();
assert_eq!(*typography.black, typography.black.copy_with());
assert_eq!(
*typography.black,
typography.black.apply().into_text_theme()
);
assert_eq!(*typography.black, typography.black.merge(None));
assert_eq!(
*typography.black,
TextTheme::new().merge(Some(&typography.black))
);
assert_eq!(
*typography.black,
typography.black.merge(Some(&typography.black))
);
assert_eq!(
*typography.white,
typography.black.merge(Some(&typography.white))
);
assert_ne!(typography.black, typography.white);
}
#[test]
fn copy_with_replaces_every_style() {
let typography = Typography::material2018().build();
let white = &typography.white;
let style = |style: &Option<TextStyle>| style.clone().expect("a style");
let white_copy = typography
.black
.copy_with()
.display_large(style(&white.display_large))
.display_medium(style(&white.display_medium))
.display_small(style(&white.display_small))
.headline_large(style(&white.headline_large))
.headline_medium(style(&white.headline_medium))
.headline_small(style(&white.headline_small))
.title_large(style(&white.title_large))
.title_medium(style(&white.title_medium))
.title_small(style(&white.title_small))
.body_large(style(&white.body_large))
.body_medium(style(&white.body_medium))
.body_small(style(&white.body_small))
.label_large(style(&white.label_large))
.label_medium(style(&white.label_medium))
.label_small(style(&white.label_small));
assert_eq!(*typography.white, white_copy);
}
#[test]
fn merges_properly_in_the_presence_of_null_fields() {
test_widgets(|tester| {
let partial_theme =
TextTheme::new().title_large(TextStyle::new().color(Color::new(0xcafefeed)));
let full_theme = ThemeData::fallback(&tester.cell.borrow(), None)
.text_theme
.merge(Some(&partial_theme));
assert_eq!(
full_theme.title_large.and_then(|style| style.color),
color(Color::new(0xcafefeed))
);
});
let only_headline_small_and_title_large = TextTheme::new()
.headline_small(TextStyle::new().color(Color::new(0xcafefeed)))
.title_large(TextStyle::new().color(Color::new(0xbeefcafe)));
let only_body_medium_and_title_large = TextTheme::new()
.body_medium(TextStyle::new().color(Color::new(0xfeedfeed)))
.title_large(TextStyle::new().color(Color::new(0xdeadcafe)));
let merged =
only_headline_small_and_title_large.merge(Some(&only_body_medium_and_title_large));
assert!(merged.body_large.is_none());
assert_eq!(
merged.body_medium.and_then(|style| style.color),
color(Color::new(0xfeedfeed))
);
assert_eq!(
merged.headline_small.and_then(|style| style.color),
color(Color::new(0xcafefeed))
);
assert_eq!(
merged.title_large.and_then(|style| style.color),
color(Color::new(0xdeadcafe))
);
let merged = only_headline_small_and_title_large.merge(None);
assert_eq!(merged, only_headline_small_and_title_large);
}
#[test]
fn apply_applies_custom_properties() {
let display_color = Color::new(0x00000001);
let body_color = Color::new(0x00000002);
let font_family = "fontFamily";
let font_family_fallback: Vec<String> =
["font", "family", "fallback"].map(String::from).to_vec();
let decoration_color = Color::new(0x00000003);
let decoration_style = waft_embedder::TextDecorationStyle::Dashed;
let decoration =
TextDecoration::combine(&[TextDecoration::UNDERLINE, TextDecoration::LINE_THROUGH]);
let typography = Typography::material2018().build();
let theme = typography
.black
.apply()
.font_family(font_family)
.font_family_fallback(font_family_fallback.clone())
.display_color(display_color)
.body_color(body_color)
.decoration(decoration)
.decoration_color(decoration_color)
.decoration_style(decoration_style)
.into_text_theme();
let display = color(display_color);
let body = color(body_color);
let colors = styles(&theme).map(|style| style.color.clone());
assert_eq!(
colors,
[
display.clone(),
display.clone(),
display.clone(),
display.clone(),
display.clone(),
body.clone(),
body.clone(),
body.clone(),
body.clone(),
body.clone(),
body.clone(),
display,
body.clone(),
body.clone(),
body,
]
);
for style in styles(&theme) {
assert_eq!(style.font_family.as_deref(), Some(font_family));
assert_eq!(
style.font_family_fallback_list(),
Some(font_family_fallback.clone())
);
assert_eq!(style.decoration_color, color(decoration_color));
assert_eq!(style.decoration_style, Some(decoration_style));
assert_eq!(style.decoration, Some(decoration));
}
}
#[test]
fn apply_applies_package_font_family_and_font_family_fallback() {
let typography = Typography::material2018().build();
let theme = typography
.black
.apply()
.font_family("fontFamily")
.font_family_fallback(vec!["fallback".to_string()])
.package("package")
.into_text_theme();
for style in styles(&theme) {
assert_eq!(
style.font_family.as_deref(),
Some("packages/package/fontFamily")
);
assert_eq!(
style.font_family_fallback_list(),
Some(vec!["packages/package/fallback".to_string()])
);
}
}
#[test]
fn apply_leaves_null_styles_null() {
let body_color = Color::new(0x00000002);
let theme = TextTheme::new()
.body_medium(TextStyle::new().font_size(10.0))
.apply()
.body_color(body_color)
.display_color(body_color)
.font_family("fontFamily")
.into_text_theme();
assert_eq!(
theme,
TextTheme::new().body_medium(
TextStyle::new()
.font_size(10.0)
.color(body_color)
.font_family("fontFamily")
)
);
}
#[test]
fn apply_applies_font_features_and_font_variations() {
let font_features = vec![FontFeature::enable("smcp")];
let font_variations = vec![FontVariation::new("wght", 400.0)];
let typography = Typography::material2018().build();
let theme = typography
.black
.apply()
.font_features(font_features.clone())
.font_variations(font_variations.clone())
.into_text_theme();
for style in styles(&theme) {
assert_eq!(style.font_features, Some(font_features.clone()));
assert_eq!(style.font_variations, Some(font_variations.clone()));
}
}
#[test]
fn apply_without_font_features_or_font_variations_preserves_the_originals() {
let font_features = vec![FontFeature::enable("smcp")];
let font_variations = vec![FontVariation::new("wght", 400.0)];
let typography = Typography::material2018().build();
let display_large = typography
.black
.display_large
.as_ref()
.expect("a display style")
.copy_with()
.font_features(font_features.clone())
.font_variations(font_variations.clone());
let base_theme = typography.black.copy_with().display_large(display_large);
let theme = base_theme.apply().into_text_theme();
let display_large = theme.display_large.as_ref().expect("a display style");
assert_eq!(display_large.font_features, Some(font_features));
assert_eq!(display_large.font_variations, Some(font_variations));
let display_medium = theme.display_medium.as_ref().expect("a display style");
assert!(display_medium.font_features.is_none());
assert!(display_medium.font_variations.is_none());
}
#[test]
fn apply_font_size_factor_and_font_size_delta() {
let typography = Typography::material2018().build();
let base_theme = Typography::english_like2018().merge(Some(&typography.black));
let size_theme = base_theme
.apply()
.font_size_factor(2.0)
.font_size_delta(5.0)
.into_text_theme();
for (sized, base) in styles(&size_theme).into_iter().zip(styles(&base_theme)) {
assert_eq!(
sized.font_size,
Some(base.font_size.expect("a size") * 2.0 + 5.0)
);
}
}
#[test]
fn apply_letter_spacing_factor_and_letter_spacing_delta() {
let typography = Typography::material2018().build();
let base_theme = Typography::english_like2018().merge(Some(&typography.black));
let size_theme = base_theme
.apply()
.letter_spacing_factor(2.0)
.letter_spacing_delta(5.0)
.into_text_theme();
for (sized, base) in styles(&size_theme).into_iter().zip(styles(&base_theme)) {
assert_eq!(
sized.letter_spacing,
Some(base.letter_spacing.expect("a letter spacing") * 2.0 + 5.0)
);
}
}
#[test]
fn apply_word_spacing_factor_and_word_spacing_delta() {
let typography = Typography::material2018().build();
let base_theme = Typography::english_like2018().merge(Some(&typography.black));
let with_word_spacing = |style: &Option<TextStyle>| {
style
.as_ref()
.expect("a style")
.copy_with()
.word_spacing(1.0)
};
let mut base_theme_with_word_spacing = base_theme.copy_with();
for style in [
&mut base_theme_with_word_spacing.display_large,
&mut base_theme_with_word_spacing.display_medium,
&mut base_theme_with_word_spacing.display_small,
&mut base_theme_with_word_spacing.headline_large,
&mut base_theme_with_word_spacing.headline_medium,
&mut base_theme_with_word_spacing.headline_small,
&mut base_theme_with_word_spacing.title_large,
&mut base_theme_with_word_spacing.title_medium,
&mut base_theme_with_word_spacing.title_small,
&mut base_theme_with_word_spacing.body_large,
&mut base_theme_with_word_spacing.body_medium,
&mut base_theme_with_word_spacing.body_small,
&mut base_theme_with_word_spacing.label_large,
&mut base_theme_with_word_spacing.label_medium,
&mut base_theme_with_word_spacing.label_small,
] {
*style = Some(with_word_spacing(style));
}
let size_theme = base_theme_with_word_spacing
.apply()
.word_spacing_factor(2.0)
.word_spacing_delta(5.0)
.into_text_theme();
for (sized, base) in styles(&size_theme)
.into_iter()
.zip(styles(&base_theme_with_word_spacing))
{
assert_eq!(
sized.word_spacing,
Some(base.word_spacing.expect("a word spacing") * 2.0 + 5.0)
);
}
}
#[test]
fn apply_height_factor_and_height_delta() {
let typography = Typography::material2021().build();
let base_theme = Typography::english_like2021().merge(Some(&typography.black));
let size_theme = base_theme
.apply()
.height_factor(2.0)
.height_delta(5.0)
.into_text_theme();
for (sized, base) in styles(&size_theme).into_iter().zip(styles(&base_theme)) {
assert_eq!(
sized.height,
Some(base.height.expect("a height") * 2.0 + 5.0)
);
}
}
#[test]
fn lerp_with_one_parameter_null_lerps_each_style_with_null() {
let theme = Typography::material2018().build().black;
let lerped = TextTheme::lerp(Some(&theme), None, 0.25);
for (lerped, style) in optional_styles(&lerped)
.into_iter()
.zip(optional_styles(&theme))
{
assert_eq!(*lerped, TextStyle::lerp(style.as_ref(), None, 0.25));
}
let lerped = TextTheme::lerp(None, Some(&theme), 0.25);
for (lerped, style) in optional_styles(&lerped)
.into_iter()
.zip(optional_styles(&theme))
{
assert_eq!(*lerped, TextStyle::lerp(None, style.as_ref(), 0.25));
}
}
#[test]
fn lerp_with_null_parameters_is_empty() {
let lerped = TextTheme::lerp(None, None, 0.25);
for style in optional_styles(&lerped) {
assert!(style.is_none());
}
}
#[test]
fn text_theme_of_and_primary_of_are_the_ambient_theme_text_themes() {
test_widgets(|tester| {
let (theme, text_theme, primary_text_theme) = probe(
tester,
|app, child| {
let data = ThemeData::builder()
.text_theme(
TextTheme::new().display_large(
TextStyle::new()
.color(Colors::BLUE.primary())
.font_size(30.0),
),
)
.primary_text_theme(
TextTheme::new().display_large(
TextStyle::new()
.background_color(Colors::GREEN.primary())
.font_style(FontStyle::Italic),
),
)
.build(app);
Theme::new(data, child).into_widget()
},
|app, context| {
(
Theme::of(app, context),
TextTheme::of(app, context),
TextTheme::primary_of(app, context),
)
},
);
assert_eq!(text_theme, theme.text_theme);
assert_eq!(primary_text_theme, theme.primary_text_theme);
assert_eq!(theme.brightness(), Brightness::Light);
});
}
}