use waft_embedder::{Color, FontWeight, TextAlign, TextDirection, TextHeightBehavior};
use waft_foundation::App;
use waft_painting::{
InlineSpanRef, StrutStyle, TextOverflow, TextScaler, TextSpan, TextStyle, TextWidthBasis,
};
use waft_rendering::{
AnyRenderObject, RenderBox, RenderHandle, RenderParagraph, SelectionRegistrarRef,
};
use crate::framework::{
BuildContext, InheritedWidget, IntoWidget, KeyRef, MultiChildRenderObjectWidget,
RenderObjectWidget, StatelessWidget, WidgetRef,
};
use crate::view::View;
use crate::widgets::basic::{Builder, Directionality, ExcludeSemantics, Semantics};
use crate::widgets::inherited_theme::InheritedTheme;
use crate::widgets::media_query::MediaQuery;
use crate::widgets::widget_span::WidgetSpan;
#[derive(Clone, Debug)]
pub struct DefaultTextStyle {
pub key: Option<KeyRef>,
pub style: TextStyle,
pub text_align: Option<TextAlign>,
pub soft_wrap: bool,
pub overflow: TextOverflow,
pub max_lines: Option<i32>,
pub text_width_basis: TextWidthBasis,
pub text_height_behavior: Option<TextHeightBehavior>,
pub child: WidgetRef,
}
impl DefaultTextStyle {
pub fn new<K>(style: TextStyle, child: impl IntoWidget<K>) -> DefaultTextStyle {
DefaultTextStyle {
key: None,
style,
text_align: None,
soft_wrap: true,
overflow: TextOverflow::Clip,
max_lines: None,
text_width_basis: TextWidthBasis::Parent,
text_height_behavior: None,
child: child.into_widget(),
}
}
pub fn fallback() -> DefaultTextStyle {
DefaultTextStyle::new(TextStyle::new(), NullWidget)
}
pub fn merge<K>(child: impl IntoWidget<K>) -> DefaultTextStyleMerge {
DefaultTextStyleMerge {
key: None,
style: None,
text_align: None,
soft_wrap: None,
overflow: None,
max_lines: None,
text_width_basis: None,
text_height_behavior: None,
child: child.into_widget(),
}
}
pub fn key(mut self, key: KeyRef) -> DefaultTextStyle {
self.key = Some(key);
self
}
pub fn text_align(mut self, text_align: TextAlign) -> DefaultTextStyle {
self.text_align = Some(text_align);
self
}
pub fn soft_wrap(mut self, soft_wrap: bool) -> DefaultTextStyle {
self.soft_wrap = soft_wrap;
self
}
pub fn overflow(mut self, overflow: TextOverflow) -> DefaultTextStyle {
self.overflow = overflow;
self
}
pub fn max_lines(mut self, max_lines: i32) -> DefaultTextStyle {
debug_assert!(max_lines > 0);
self.max_lines = Some(max_lines);
self
}
pub fn text_width_basis(mut self, text_width_basis: TextWidthBasis) -> DefaultTextStyle {
self.text_width_basis = text_width_basis;
self
}
pub fn text_height_behavior(
mut self,
text_height_behavior: TextHeightBehavior,
) -> DefaultTextStyle {
self.text_height_behavior = Some(text_height_behavior);
self
}
pub fn of(app: &mut App, context: BuildContext) -> DefaultTextStyle {
context
.depend_on_inherited_widget_of_exact_type::<DefaultTextStyle>(app)
.cloned()
.unwrap_or_else(DefaultTextStyle::fallback)
}
}
#[derive(Debug)]
pub struct DefaultTextStyleMerge {
key: Option<KeyRef>,
style: Option<TextStyle>,
text_align: Option<TextAlign>,
soft_wrap: Option<bool>,
overflow: Option<TextOverflow>,
max_lines: Option<i32>,
text_width_basis: Option<TextWidthBasis>,
text_height_behavior: Option<TextHeightBehavior>,
child: WidgetRef,
}
impl DefaultTextStyleMerge {
pub fn key(mut self, key: KeyRef) -> DefaultTextStyleMerge {
self.key = Some(key);
self
}
pub fn style(mut self, style: TextStyle) -> DefaultTextStyleMerge {
self.style = Some(style);
self
}
pub fn text_align(mut self, text_align: TextAlign) -> DefaultTextStyleMerge {
self.text_align = Some(text_align);
self
}
pub fn soft_wrap(mut self, soft_wrap: bool) -> DefaultTextStyleMerge {
self.soft_wrap = Some(soft_wrap);
self
}
pub fn overflow(mut self, overflow: TextOverflow) -> DefaultTextStyleMerge {
self.overflow = Some(overflow);
self
}
pub fn max_lines(mut self, max_lines: i32) -> DefaultTextStyleMerge {
debug_assert!(max_lines > 0);
self.max_lines = Some(max_lines);
self
}
pub fn text_width_basis(mut self, text_width_basis: TextWidthBasis) -> DefaultTextStyleMerge {
self.text_width_basis = Some(text_width_basis);
self
}
pub fn text_height_behavior(
mut self,
text_height_behavior: TextHeightBehavior,
) -> DefaultTextStyleMerge {
self.text_height_behavior = Some(text_height_behavior);
self
}
}
pub struct DefaultTextStyleMergeKind;
impl IntoWidget<DefaultTextStyleMergeKind> for DefaultTextStyleMerge {
fn into_widget(self) -> WidgetRef {
Builder::new(move |app, context| {
let parent = DefaultTextStyle::of(app, context);
DefaultTextStyle {
key: self.key.clone(),
style: parent.style.merge(self.style.as_ref()),
text_align: self.text_align.or(parent.text_align),
soft_wrap: self.soft_wrap.unwrap_or(parent.soft_wrap),
overflow: self.overflow.unwrap_or(parent.overflow),
max_lines: self.max_lines.or(parent.max_lines),
text_width_basis: self.text_width_basis.unwrap_or(parent.text_width_basis),
text_height_behavior: self.text_height_behavior.or(parent.text_height_behavior),
child: self.child.clone(),
}
.into_widget()
})
.into_widget()
}
}
impl InheritedWidget for DefaultTextStyle {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn child(&self) -> &WidgetRef {
&self.child
}
fn update_should_notify(&self, old_widget: &DefaultTextStyle) -> bool {
self.style != old_widget.style
|| self.text_align != old_widget.text_align
|| self.soft_wrap != old_widget.soft_wrap
|| self.overflow != old_widget.overflow
|| self.max_lines != old_widget.max_lines
|| self.text_width_basis != old_widget.text_width_basis
|| self.text_height_behavior != old_widget.text_height_behavior
}
fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
Some(self)
}
}
impl InheritedTheme for DefaultTextStyle {
fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
DefaultTextStyle {
key: None,
style: self.style.clone(),
text_align: self.text_align,
soft_wrap: self.soft_wrap,
overflow: self.overflow,
max_lines: self.max_lines,
text_width_basis: self.text_width_basis,
text_height_behavior: self.text_height_behavior,
child,
}
.into_widget()
}
}
#[derive(Debug)]
struct NullWidget;
impl StatelessWidget for NullWidget {
fn build(&self, _app: &mut App, _context: BuildContext) -> WidgetRef {
panic!(
"A DefaultTextStyle constructed with DefaultTextStyle::fallback cannot be \
incorporated into the widget tree, it is meant only to provide a fallback value \
returned by DefaultTextStyle::of() when no enclosing default text style is \
present in a BuildContext."
);
}
}
#[derive(Debug)]
pub struct DefaultTextHeightBehavior {
pub key: Option<KeyRef>,
pub text_height_behavior: TextHeightBehavior,
pub child: WidgetRef,
}
impl DefaultTextHeightBehavior {
pub fn new<K>(
text_height_behavior: TextHeightBehavior,
child: impl IntoWidget<K>,
) -> DefaultTextHeightBehavior {
DefaultTextHeightBehavior {
key: None,
text_height_behavior,
child: child.into_widget(),
}
}
pub fn key(mut self, key: KeyRef) -> DefaultTextHeightBehavior {
self.key = Some(key);
self
}
pub fn maybe_of(app: &mut App, context: BuildContext) -> Option<TextHeightBehavior> {
context
.depend_on_inherited_widget_of_exact_type::<DefaultTextHeightBehavior>(app)
.map(|widget| widget.text_height_behavior)
}
pub fn of(app: &mut App, context: BuildContext) -> TextHeightBehavior {
let behavior = Self::maybe_of(app, context);
behavior.expect(
"DefaultTextHeightBehavior::of() was called with a context that does not contain \
a DefaultTextHeightBehavior widget.\n\
No DefaultTextHeightBehavior widget ancestor could be found starting from the \
context that was passed to DefaultTextHeightBehavior::of(). This can happen \
because you are using a widget that looks for a DefaultTextHeightBehavior \
ancestor, but no such ancestor exists.",
)
}
}
impl InheritedWidget for DefaultTextHeightBehavior {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn child(&self) -> &WidgetRef {
&self.child
}
fn update_should_notify(&self, old_widget: &DefaultTextHeightBehavior) -> bool {
self.text_height_behavior != old_widget.text_height_behavior
}
fn as_inherited_theme(&self) -> Option<&dyn InheritedTheme> {
Some(self)
}
}
impl InheritedTheme for DefaultTextHeightBehavior {
fn wrap(&self, _app: &mut App, _context: BuildContext, child: WidgetRef) -> WidgetRef {
DefaultTextHeightBehavior::new(self.text_height_behavior, child).into_widget()
}
}
#[derive(Debug)]
pub struct Text {
pub key: Option<KeyRef>,
pub data: Option<String>,
pub text_span: Option<InlineSpanRef>,
pub style: Option<TextStyle>,
pub strut_style: Option<StrutStyle>,
pub text_align: Option<TextAlign>,
pub text_direction: Option<TextDirection>,
pub soft_wrap: Option<bool>,
pub overflow: Option<TextOverflow>,
pub text_scaler: Option<TextScaler>,
pub max_lines: Option<i32>,
pub semantics_label: Option<String>,
pub semantics_identifier: Option<String>,
pub text_width_basis: Option<TextWidthBasis>,
pub text_height_behavior: Option<TextHeightBehavior>,
}
impl Text {
pub fn new(data: impl Into<String>) -> Text {
Text::unstyled(Some(data.into()), None)
}
pub fn rich(text_span: InlineSpanRef) -> Text {
Text::unstyled(None, Some(text_span))
}
fn unstyled(data: Option<String>, text_span: Option<InlineSpanRef>) -> Text {
Text {
key: None,
data,
text_span,
style: None,
strut_style: None,
text_align: None,
text_direction: None,
soft_wrap: None,
overflow: None,
text_scaler: None,
max_lines: None,
semantics_label: None,
semantics_identifier: None,
text_width_basis: None,
text_height_behavior: None,
}
}
pub fn key(mut self, key: KeyRef) -> Text {
self.key = Some(key);
self
}
pub fn style(mut self, style: TextStyle) -> Text {
self.style = Some(style);
self
}
pub fn strut_style(mut self, strut_style: StrutStyle) -> Text {
self.strut_style = Some(strut_style);
self
}
pub fn text_align(mut self, text_align: TextAlign) -> Text {
self.text_align = Some(text_align);
self
}
pub fn text_direction(mut self, text_direction: TextDirection) -> Text {
self.text_direction = Some(text_direction);
self
}
pub fn soft_wrap(mut self, soft_wrap: bool) -> Text {
self.soft_wrap = Some(soft_wrap);
self
}
pub fn overflow(mut self, overflow: TextOverflow) -> Text {
self.overflow = Some(overflow);
self
}
pub fn text_scaler(mut self, text_scaler: TextScaler) -> Text {
self.text_scaler = Some(text_scaler);
self
}
pub fn max_lines(mut self, max_lines: i32) -> Text {
self.max_lines = Some(max_lines);
self
}
pub fn semantics_label(mut self, semantics_label: impl Into<String>) -> Text {
self.semantics_label = Some(semantics_label.into());
self
}
pub fn semantics_identifier(mut self, semantics_identifier: impl Into<String>) -> Text {
self.semantics_identifier = Some(semantics_identifier.into());
self
}
pub fn text_width_basis(mut self, text_width_basis: TextWidthBasis) -> Text {
self.text_width_basis = Some(text_width_basis);
self
}
pub fn text_height_behavior(mut self, text_height_behavior: TextHeightBehavior) -> Text {
self.text_height_behavior = Some(text_height_behavior);
self
}
}
impl StatelessWidget for Text {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
let default_text_style = DefaultTextStyle::of(app, context);
let mut effective_text_style = self.style.clone();
if self.style.as_ref().is_none_or(|style| style.inherit) {
effective_text_style = Some(default_text_style.style.merge(self.style.as_ref()));
}
if MediaQuery::bold_text_of(app, context) {
let bold = TextStyle::new().font_weight(FontWeight::BOLD);
effective_text_style = Some(
effective_text_style
.expect("the style is set above")
.merge(Some(&bold)),
);
}
let line_height_scale_factor =
MediaQuery::maybe_line_height_scale_factor_override_of(app, context);
let letter_spacing = MediaQuery::maybe_letter_spacing_override_of(app, context);
let word_spacing = MediaQuery::maybe_word_spacing_override_of(app, context);
let effective_text_span = apply_text_spacing_overrides(
line_height_scale_factor,
letter_spacing,
word_spacing,
TextSpan {
style: effective_text_style.clone(),
text: self.data.clone(),
children: self
.text_span
.as_ref()
.map(|text_span| vec![text_span.clone()]),
..TextSpan::default()
},
);
let effective_strut_style = self.strut_style.as_ref().map(|strut_style| {
line_height_scale_factor.map_or_else(
|| strut_style.clone(),
|height| strut_style.merge(Some(&StrutStyle::new().height(height))),
)
});
let text_scaler = self
.text_scaler
.clone()
.unwrap_or_else(|| MediaQuery::text_scaler_of(app, context));
let text_height_behavior = self
.text_height_behavior
.or(default_text_style.text_height_behavior)
.or_else(|| DefaultTextHeightBehavior::maybe_of(app, context));
let mut rich_text = RichText::new(effective_text_span.into_span())
.text_align(
self.text_align
.or(default_text_style.text_align)
.unwrap_or(TextAlign::Start),
)
.soft_wrap(self.soft_wrap.unwrap_or(default_text_style.soft_wrap))
.overflow(
self.overflow
.or(effective_text_style
.as_ref()
.and_then(|style| style.overflow))
.unwrap_or(default_text_style.overflow),
)
.text_scaler(text_scaler)
.text_width_basis(
self.text_width_basis
.unwrap_or(default_text_style.text_width_basis),
);
if let Some(text_direction) = self.text_direction {
rich_text = rich_text.text_direction(text_direction);
}
if let Some(max_lines) = self.max_lines.or(default_text_style.max_lines) {
rich_text = rich_text.max_lines(max_lines);
}
if let Some(text_height_behavior) = text_height_behavior {
rich_text = rich_text.text_height_behavior(text_height_behavior);
}
if let Some(strut_style) = effective_strut_style {
rich_text = rich_text.strut_style(strut_style);
}
let mut result = rich_text.into_widget();
if self.semantics_label.is_some() || self.semantics_identifier.is_some() {
let mut semantics = Semantics::new();
if let Some(text_direction) = self.text_direction {
semantics = semantics.text_direction(text_direction);
}
if let Some(semantics_label) = &self.semantics_label {
semantics = semantics.label(semantics_label.clone());
}
if let Some(semantics_identifier) = &self.semantics_identifier {
semantics = semantics.identifier(semantics_identifier.clone());
}
result = semantics
.child(
ExcludeSemantics::new()
.excluding(self.semantics_label.is_some())
.child(result),
)
.into_widget();
}
result
}
}
pub(crate) fn apply_text_spacing_overrides(
line_height_scale_factor: Option<f64>,
letter_spacing: Option<f64>,
word_spacing: Option<f64>,
text_span: TextSpan,
) -> TextSpan {
if line_height_scale_factor.is_none() && letter_spacing.is_none() && word_spacing.is_none() {
return text_span;
}
let mut override_text_style = TextStyle::new();
if let Some(height) = line_height_scale_factor {
override_text_style = override_text_style.height(height);
}
if let Some(letter_spacing) = letter_spacing {
override_text_style = override_text_style.letter_spacing(letter_spacing);
}
if let Some(word_spacing) = word_spacing {
override_text_style = override_text_style.word_spacing(word_spacing);
}
apply_text_style_overrides(&override_text_style, &text_span)
}
fn apply_text_style_overrides(override_text_style: &TextStyle, text_span: &TextSpan) -> TextSpan {
TextSpan {
text: text_span.text.clone(),
children: text_span.children.as_ref().map(|children| {
children
.iter()
.map(|child| match child.as_any().downcast_ref::<TextSpan>() {
Some(child) => {
apply_text_style_overrides(override_text_style, child).into_span()
}
None => child.clone(),
})
.collect()
}),
style: Some(match &text_span.style {
Some(style) => style.merge(Some(override_text_style)),
None => override_text_style.clone(),
}),
recognizer: text_span.recognizer.clone(),
mouse_cursor: text_span.mouse_cursor.clone(),
on_enter: text_span.on_enter.clone(),
on_exit: text_span.on_exit.clone(),
}
}
#[derive(Debug)]
pub struct RichText {
pub key: Option<KeyRef>,
pub text: InlineSpanRef,
children: Vec<WidgetRef>,
pub text_align: TextAlign,
pub text_direction: Option<TextDirection>,
pub soft_wrap: bool,
pub overflow: TextOverflow,
pub text_scaler: TextScaler,
pub max_lines: Option<i32>,
pub strut_style: Option<StrutStyle>,
pub text_width_basis: TextWidthBasis,
pub text_height_behavior: Option<TextHeightBehavior>,
pub selection_registrar: Option<SelectionRegistrarRef>,
pub selection_color: Option<Color>,
}
impl RichText {
pub fn new(text: InlineSpanRef) -> RichText {
RichText {
key: None,
children: WidgetSpan::extract_from_inline_span(&*text, &TextScaler::NO_SCALING),
text,
text_align: TextAlign::Start,
text_direction: None,
soft_wrap: true,
overflow: TextOverflow::Clip,
text_scaler: TextScaler::NO_SCALING,
max_lines: None,
strut_style: None,
text_width_basis: TextWidthBasis::Parent,
text_height_behavior: None,
selection_registrar: None,
selection_color: None,
}
}
pub fn key(mut self, key: KeyRef) -> RichText {
self.key = Some(key);
self
}
pub fn text_align(mut self, text_align: TextAlign) -> RichText {
self.text_align = text_align;
self
}
pub fn text_direction(mut self, text_direction: TextDirection) -> RichText {
self.text_direction = Some(text_direction);
self
}
pub fn soft_wrap(mut self, soft_wrap: bool) -> RichText {
self.soft_wrap = soft_wrap;
self
}
pub fn overflow(mut self, overflow: TextOverflow) -> RichText {
self.overflow = overflow;
self
}
pub fn text_scaler(mut self, text_scaler: TextScaler) -> RichText {
self.children = WidgetSpan::extract_from_inline_span(&*self.text, &text_scaler);
self.text_scaler = text_scaler;
self
}
pub fn max_lines(mut self, max_lines: i32) -> RichText {
debug_assert!(max_lines > 0);
self.max_lines = Some(max_lines);
self
}
pub fn strut_style(mut self, strut_style: StrutStyle) -> RichText {
self.strut_style = Some(strut_style);
self
}
pub fn text_width_basis(mut self, text_width_basis: TextWidthBasis) -> RichText {
self.text_width_basis = text_width_basis;
self
}
pub fn text_height_behavior(mut self, text_height_behavior: TextHeightBehavior) -> RichText {
self.text_height_behavior = Some(text_height_behavior);
self
}
pub fn selection_registrar(mut self, registrar: SelectionRegistrarRef) -> RichText {
self.selection_registrar = Some(registrar);
self
}
pub fn selection_color(mut self, color: Color) -> RichText {
self.selection_color = Some(color);
self
}
fn device_pixel_ratio(app: &mut App, context: BuildContext) -> f64 {
MediaQuery::maybe_device_pixel_ratio_of(app, context)
.or_else(|| View::maybe_of(app, context).map(|view| view.metrics().device_pixel_ratio))
.unwrap_or(1.0)
}
}
impl RenderObjectWidget for RichText {
type RenderObject = RenderParagraph;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn debug_check_constructor(&self) {
debug_assert!(self.selection_registrar.is_none() || self.selection_color.is_some());
}
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
let text_direction = self
.text_direction
.unwrap_or_else(|| Directionality::of(app, context));
let device_pixel_ratio = Self::device_pixel_ratio(app, context);
let render_object = RenderParagraph::new(app, self.text.clone(), text_direction, None);
render_object.set_text_align(app, self.text_align);
render_object.set_soft_wrap(app, self.soft_wrap);
render_object.set_overflow(app, self.overflow);
render_object.set_text_scaler(app, self.text_scaler.clone());
render_object.set_max_lines(app, self.max_lines);
render_object.set_text_width_basis(app, self.text_width_basis);
render_object.set_text_height_behavior(app, self.text_height_behavior);
render_object.set_strut_style(app, self.strut_style.clone());
render_object.set_device_pixel_ratio(app, device_pixel_ratio);
render_object.set_registrar(app, self.selection_registrar.clone());
render_object.set_selection_color(app, self.selection_color);
render_object.as_object()
}
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: RenderHandle<RenderParagraph>,
) {
let text_direction = self
.text_direction
.unwrap_or_else(|| Directionality::of(app, context));
render_object.set_text(app, self.text.clone());
render_object.set_text_align(app, self.text_align);
render_object.set_text_direction(app, text_direction);
render_object.set_soft_wrap(app, self.soft_wrap);
render_object.set_overflow(app, self.overflow);
render_object.set_text_scaler(app, self.text_scaler.clone());
render_object.set_max_lines(app, self.max_lines);
render_object.set_text_width_basis(app, self.text_width_basis);
render_object.set_text_height_behavior(app, self.text_height_behavior);
render_object.set_strut_style(app, self.strut_style.clone());
let device_pixel_ratio = Self::device_pixel_ratio(app, context);
render_object.set_device_pixel_ratio(app, device_pixel_ratio);
render_object.set_registrar(app, self.selection_registrar.clone());
render_object.set_selection_color(app, self.selection_color);
}
}
impl MultiChildRenderObjectWidget for RichText {
fn children(&self) -> &[WidgetRef] {
&self.children
}
}
#[cfg(test)]
mod tests {
use waft_foundation::AppCell;
use waft_embedder::{Color, Size, TextLeadingDistribution};
use waft_foundation::ValueKey;
use waft_painting::PaintingBinding;
use crate::{PlaceholderDimensions, TextPainter};
use super::*;
use crate::test_harness::Harness;
use crate::widgets::media_query::MediaQueryData;
fn install_fonts(app: &mut App) {
let binding = PaintingBinding::instance(app);
if !binding.has_fonts(app) {
binding.install_fonts(app, |fonts| {
fonts.add_source(valo_system_fonts::SystemFonts::load());
});
}
}
fn ltr(child: WidgetRef) -> WidgetRef {
Directionality::new(TextDirection::Ltr, child).into_widget()
}
fn mount(app: &mut App, child: WidgetRef) -> Harness {
install_fonts(app);
let harness = Harness::mount(app, ltr(child));
harness.pump(app);
harness
}
fn paragraph_under_root(harness: &Harness, app: &App) -> RenderHandle<RenderParagraph> {
harness
.render_root(app)
.child(app)
.expect("a child")
.as_object()
.downcast::<RenderParagraph>(app)
.expect("a RenderParagraph")
}
fn style_of(paragraph: RenderHandle<RenderParagraph>, app: &App) -> TextStyle {
paragraph
.text(app)
.style()
.cloned()
.expect("Text always styles its span")
}
fn plain_text_of(paragraph: RenderHandle<RenderParagraph>, app: &App) -> String {
paragraph.text(app).to_plain_text(true, true)
}
#[test]
fn a_text_lays_out_a_paragraph_with_the_fallback_style() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = mount(&mut app, Text::new("Hello").into_widget());
let paragraph = paragraph_under_root(&harness, &app);
let size = paragraph.size(&app);
assert!(size.width() > 0.0 && size.height() > 0.0);
assert_eq!(plain_text_of(paragraph, &app), "Hello");
assert_eq!(
style_of(paragraph, &app),
TextStyle::new(),
"DefaultTextStyle::fallback"
);
assert_eq!(paragraph.text_direction(&app), TextDirection::Ltr);
assert_eq!(paragraph.text_align(&app), TextAlign::Start);
assert_eq!(paragraph.max_lines(&app), None);
assert_eq!(paragraph.overflow(&app), TextOverflow::Clip);
}
#[test]
fn a_rich_text_span_becomes_the_child_of_the_styled_span() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let span = TextSpan::new().text("Hello").into_span();
let harness = mount(&mut app, Text::rich(span).into_widget());
let paragraph = paragraph_under_root(&harness, &app);
assert_eq!(plain_text_of(paragraph, &app), "Hello");
assert!(paragraph.size(&app).width() > 0.0);
}
#[test]
fn a_default_text_style_styles_the_text_and_notifies_on_change() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let text: WidgetRef = Text::new("Hello").into_widget();
let styled = |font_size: f64| {
DefaultTextStyle::new(TextStyle::new().font_size(font_size), text.clone()).into_widget()
};
let harness = mount(&mut app, styled(12.0));
let paragraph = paragraph_under_root(&harness, &app);
assert_eq!(style_of(paragraph, &app).font_size, Some(12.0));
let small = paragraph.size(&app);
harness.set_child(&mut app, ltr(styled(24.0)));
harness.pump(&mut app);
assert_eq!(
paragraph_under_root(&harness, &app),
paragraph,
"the same paragraph is restyled"
);
assert_eq!(style_of(paragraph, &app).font_size, Some(24.0));
assert!(paragraph.size(&app).height() > small.height());
}
#[test]
fn an_explicit_style_merges_into_the_default_unless_it_does_not_inherit() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let red = Color::from_argb(255, 255, 0, 0);
let under_default = |style: TextStyle| {
DefaultTextStyle::new(
TextStyle::new().font_size(12.0),
Text::new("Hello").style(style),
)
.into_widget()
};
let harness = mount(&mut app, under_default(TextStyle::new().color(red)));
let paragraph = paragraph_under_root(&harness, &app);
let merged = style_of(paragraph, &app);
assert_eq!(merged.font_size, Some(12.0));
assert_eq!(merged.color, Some(red.into()));
harness.set_child(
&mut app,
ltr(under_default(
TextStyle::new().inherit(false).font_size(30.0),
)),
);
harness.pump(&mut app);
let replaced = style_of(paragraph, &app);
assert_eq!(replaced.font_size, Some(30.0));
assert_eq!(
replaced.color, None,
"a non-inheriting style replaces the default"
);
}
#[test]
fn a_new_string_updates_the_same_paragraph() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = mount(&mut app, Text::new("Hello").into_widget());
let paragraph = paragraph_under_root(&harness, &app);
let short = paragraph.size(&app);
harness.set_child(&mut app, ltr(Text::new("Hello there, world").into_widget()));
harness.pump(&mut app);
assert_eq!(paragraph_under_root(&harness, &app), paragraph);
assert_eq!(plain_text_of(paragraph, &app), "Hello there, world");
assert!(paragraph.size(&app).width() > short.width());
}
#[test]
fn max_lines_and_overflow_reach_the_render_object() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = mount(
&mut app,
Text::new("Hello")
.max_lines(2)
.overflow(TextOverflow::Ellipsis)
.soft_wrap(false)
.text_direction(TextDirection::Rtl)
.text_align(TextAlign::End)
.into_widget(),
);
let paragraph = paragraph_under_root(&harness, &app);
assert_eq!(paragraph.max_lines(&app), Some(2));
assert_eq!(paragraph.overflow(&app), TextOverflow::Ellipsis);
assert!(!paragraph.soft_wrap(&app));
assert_eq!(paragraph.text_direction(&app), TextDirection::Rtl);
assert_eq!(paragraph.text_align(&app), TextAlign::End);
}
#[test]
fn a_default_text_style_supplies_what_the_text_leaves_unset() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let under_default = |text: Text| {
DefaultTextStyle::new(TextStyle::new(), text)
.max_lines(1)
.overflow(TextOverflow::Fade)
.text_align(TextAlign::Center)
.soft_wrap(false)
.into_widget()
};
let harness = mount(&mut app, under_default(Text::new("Hello")));
let paragraph = paragraph_under_root(&harness, &app);
assert_eq!(paragraph.max_lines(&app), Some(1));
assert_eq!(paragraph.overflow(&app), TextOverflow::Fade);
assert_eq!(paragraph.text_align(&app), TextAlign::Center);
assert!(!paragraph.soft_wrap(&app));
harness.set_child(
&mut app,
ltr(under_default(Text::new("Hello").max_lines(3))),
);
harness.pump(&mut app);
assert_eq!(
paragraph.max_lines(&app),
Some(3),
"the Text's own value wins"
);
}
#[test]
fn the_style_overflow_beats_the_default_text_style_overflow() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = mount(
&mut app,
DefaultTextStyle::new(
TextStyle::new(),
Text::new("Hello").style(TextStyle::new().overflow(TextOverflow::Ellipsis)),
)
.overflow(TextOverflow::Fade)
.into_widget(),
);
let paragraph = paragraph_under_root(&harness, &app);
assert_eq!(paragraph.overflow(&app), TextOverflow::Ellipsis);
}
#[test]
fn a_default_text_height_behavior_reaches_the_paragraph_and_notifies_on_change() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let text: WidgetRef = Text::new("Hello").into_widget();
let under_behavior = |leading_distribution: TextLeadingDistribution| {
DefaultTextHeightBehavior::new(
TextHeightBehavior {
leading_distribution,
..TextHeightBehavior::default()
},
text.clone(),
)
.into_widget()
};
let harness = mount(&mut app, under_behavior(TextLeadingDistribution::Even));
let paragraph = paragraph_under_root(&harness, &app);
let behavior = paragraph.text_height_behavior(&app).expect("inherited");
assert_eq!(behavior.leading_distribution, TextLeadingDistribution::Even);
harness.set_child(
&mut app,
ltr(under_behavior(TextLeadingDistribution::Proportional)),
);
harness.pump(&mut app);
let behavior = paragraph.text_height_behavior(&app).expect("inherited");
assert_eq!(
behavior.leading_distribution,
TextLeadingDistribution::Proportional
);
}
#[test]
fn a_rich_text_forwards_its_configuration_in_place() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let rich = |text: &str| {
RichText::new(TextSpan::new().text(text).into_span())
.text_align(TextAlign::Center)
.text_direction(TextDirection::Rtl)
.max_lines(1)
.into_widget()
};
let harness = mount(&mut app, rich("Hello"));
let paragraph = paragraph_under_root(&harness, &app);
assert_eq!(paragraph.text_align(&app), TextAlign::Center);
assert_eq!(paragraph.text_direction(&app), TextDirection::Rtl);
assert_eq!(paragraph.max_lines(&app), Some(1));
harness.set_child(&mut app, ltr(rich("Goodbye")));
harness.pump(&mut app);
assert_eq!(paragraph_under_root(&harness, &app), paragraph);
assert_eq!(plain_text_of(paragraph, &app), "Goodbye");
}
#[test]
fn a_changed_key_replaces_the_paragraph_where_the_same_key_keeps_it() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let keyed = |key: &str| {
Text::new("Hello")
.key(ValueKey::new(key.to_owned()).into())
.into_widget()
};
let harness = mount(&mut app, keyed("a"));
let paragraph = paragraph_under_root(&harness, &app);
harness.set_child(&mut app, ltr(keyed("a")));
harness.pump(&mut app);
assert_eq!(paragraph_under_root(&harness, &app), paragraph);
harness.set_child(&mut app, ltr(keyed("b")));
harness.pump(&mut app);
assert_ne!(paragraph_under_root(&harness, &app), paragraph);
}
#[test]
fn a_media_query_bolds_scales_and_spaces_the_text() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let data = MediaQueryData::new()
.bold_text(true)
.text_scaler(TextScaler::linear(2.0))
.line_height_scale_factor_override(1.5)
.letter_spacing_override(3.0)
.word_spacing_override(4.0);
let harness = mount(
&mut app,
MediaQuery::new(data, Text::new("Hello")).into_widget(),
);
let paragraph = paragraph_under_root(&harness, &app);
let style = style_of(paragraph, &app);
assert_eq!(style.font_weight, Some(FontWeight::BOLD));
assert_eq!(style.height, Some(1.5));
assert_eq!(style.letter_spacing, Some(3.0));
assert_eq!(style.word_spacing, Some(4.0));
assert_eq!(paragraph.text_scaler(&app), TextScaler::linear(2.0));
}
#[test]
fn force_strut_height_applies_to_widget_spans() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let fonts = PaintingBinding::instance(&mut app).install_flutter_test_fonts(&mut app);
let span = TextSpan::new()
.children(vec![
WidgetSpan::new(crate::widgets::basic::SizedBox::new()).into_span(),
])
.into_span();
let mut painter = TextPainter::new();
painter.set_text(Some(span));
painter.set_text_direction(Some(TextDirection::Ltr));
painter.set_strut_style(Some(
StrutStyle::new()
.height(10.0)
.font_size(10.0)
.force_strut_height(true),
));
painter.set_placeholder_dimensions(Some(vec![PlaceholderDimensions::new(
Size::new(1000.0, 1000.0),
waft_embedder::PlaceholderAlignment::Bottom,
)]));
painter.layout(app.get_mut(fonts), 0.0, f64::INFINITY);
assert_eq!(painter.height(), 100.0);
}
#[test]
fn text_rich_respects_the_media_query_spacing_overrides_on_its_strut() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let overridden = MediaQuery::new(
MediaQueryData::new()
.line_height_scale_factor_override(2.0)
.letter_spacing_override(2.0)
.word_spacing_override(2.0),
Text::rich(TextSpan::new().text("Hello").into_span())
.text_direction(TextDirection::Ltr)
.strut_style(StrutStyle::new().height(0.9)),
)
.into_widget();
let harness = mount(&mut app, overridden);
let paragraph = paragraph_under_root(&harness, &app);
let span_style = paragraph.text(&app).style().cloned();
assert_eq!(
span_style.as_ref().and_then(|style| style.height),
Some(2.0)
);
assert_eq!(
span_style.as_ref().and_then(|style| style.letter_spacing),
Some(2.0)
);
assert_eq!(
span_style.as_ref().and_then(|style| style.word_spacing),
Some(2.0)
);
assert_eq!(
paragraph
.strut_style(&app)
.and_then(|strut_style| strut_style.height),
Some(2.0)
);
harness.set_child(
&mut app,
ltr(Text::rich(TextSpan::new().text("Hello").into_span())
.text_direction(TextDirection::Ltr)
.strut_style(StrutStyle::new().height(0.9))
.into_widget()),
);
harness.pump(&mut app);
let paragraph = paragraph_under_root(&harness, &app);
let span_style = paragraph.text(&app).style().cloned();
assert_eq!(span_style.as_ref().and_then(|style| style.height), None);
assert_eq!(
span_style.as_ref().and_then(|style| style.letter_spacing),
None
);
assert_eq!(
span_style.as_ref().and_then(|style| style.word_spacing),
None
);
assert_eq!(
paragraph
.strut_style(&app)
.and_then(|strut_style| strut_style.height),
Some(0.9)
);
}
#[test]
fn rich_text_ignores_the_media_query_spacing_overrides() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let overridden = MediaQuery::new(
MediaQueryData::new()
.line_height_scale_factor_override(2.0)
.letter_spacing_override(2.0)
.word_spacing_override(2.0),
RichText::new(TextSpan::new().text("Hello").into_span())
.text_direction(TextDirection::Ltr)
.strut_style(StrutStyle::new().height(0.9)),
)
.into_widget();
let harness = mount(&mut app, overridden);
let paragraph = paragraph_under_root(&harness, &app);
let span_style = paragraph.text(&app).style().cloned();
assert_eq!(span_style.as_ref().and_then(|style| style.height), None);
assert_eq!(
span_style.as_ref().and_then(|style| style.letter_spacing),
None
);
assert_eq!(
span_style.as_ref().and_then(|style| style.word_spacing),
None
);
assert_eq!(
paragraph
.strut_style(&app)
.and_then(|strut_style| strut_style.height),
Some(0.9)
);
}
}