use std::any::Any;
use std::fmt;
use std::rc::Rc;
use waft_embedder::{
Matrix4, Offset, ParagraphBuilder, PlaceholderAlignment, Size, TextBaseline, TextPosition,
};
use waft_foundation::{App, DiagnosticPropertiesBuilder, DiagnosticsProperty};
use waft_painting::{
Accumulator, InlineSpan, InlineSpanRef, K_DEFAULT_FONT_SIZE, PLACEHOLDER_CODE_UNIT,
PlaceholderDimensions, PlaceholderSpan, RenderComparison, TextScaler, TextStyle,
inline_span_debug_fill_properties, placeholder_span_debug_fill_properties, same_span,
};
use waft_rendering::{
AnyRenderBox, AnyRenderObject, BoxConstraints, BoxHitTestResult, ContainerLayer,
PaintingContext, RenderBox, RenderBoxData, RenderHandle, RenderObject, RenderObjectData,
RenderObjectWithChildData, RenderObjectWithChildMixin, TextParentData, TransformLayer,
};
use crate::framework::{
BuildContext, IntoWidget, KeyRef, ParentDataWidget, RenderObjectWidget,
SingleChildRenderObjectWidget, WidgetRef,
};
#[derive(Clone)]
pub struct WidgetSpan {
pub child: WidgetRef,
pub alignment: PlaceholderAlignment,
pub baseline: Option<TextBaseline>,
pub style: Option<TextStyle>,
}
impl WidgetSpan {
#[cfg_attr(debug_assertions, track_caller)]
pub fn new<K>(child: impl IntoWidget<K>) -> WidgetSpan {
WidgetSpan {
child: child.into_widget(),
alignment: PlaceholderAlignment::Bottom,
baseline: None,
style: None,
}
}
pub fn alignment(mut self, alignment: PlaceholderAlignment) -> WidgetSpan {
self.alignment = alignment;
self
}
pub fn baseline(mut self, baseline: TextBaseline) -> WidgetSpan {
self.baseline = Some(baseline);
self
}
pub fn style(mut self, style: TextStyle) -> WidgetSpan {
self.style = Some(style);
self
}
pub fn into_span(self) -> InlineSpanRef {
debug_assert!(
self.baseline.is_some()
|| !matches!(
self.alignment,
PlaceholderAlignment::AboveBaseline
| PlaceholderAlignment::BelowBaseline
| PlaceholderAlignment::Baseline
)
);
std::rc::Rc::new(self)
}
pub fn extract_from_inline_span(
span: &dyn InlineSpan,
text_scaler: &TextScaler,
) -> Vec<WidgetRef> {
let mut widgets = Vec::new();
let mut font_size_stack = vec![K_DEFAULT_FONT_SIZE];
visit_subtree(span, text_scaler, &mut font_size_stack, &mut widgets);
widgets
}
}
fn visit_subtree(
span: &dyn InlineSpan,
text_scaler: &TextScaler,
font_size_stack: &mut Vec<f64>,
widgets: &mut Vec<WidgetRef>,
) -> bool {
let font_size_to_push = span
.style()
.and_then(|style| style.font_size)
.filter(|size| Some(size) != font_size_stack.last());
if let Some(size) = font_size_to_push {
font_size_stack.push(size);
}
if let Some(widget_span) = span.as_any().downcast_ref::<WidgetSpan>() {
let font_size = *font_size_stack.last().expect("never empty");
let text_scale_factor = if font_size == 0.0 {
0.0
} else {
text_scaler.scale(font_size) / font_size
};
widgets.push(
WidgetSpanParentData {
span: widget_span.clone(),
child: AutoScaleInlineWidget {
span: widget_span.clone(),
text_scale_factor,
child: widget_span.child.clone(),
}
.into_widget(),
}
.into_widget(),
);
}
debug_assert!(
span.as_any().is::<WidgetSpan>() || span.as_placeholder_span().is_none(),
"{span:?} is a PlaceholderSpan but not a WidgetSpan subclass. This is currently not \
supported."
);
span.visit_direct_children(&mut |child| {
visit_subtree(&**child, text_scaler, font_size_stack, widgets)
});
if let Some(pushed) = font_size_to_push {
let popped_font_size = font_size_stack.pop();
debug_assert!(!font_size_stack.is_empty());
debug_assert_eq!(popped_font_size, Some(pushed));
}
true
}
impl fmt::Debug for WidgetSpan {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut s = f.debug_struct("WidgetSpan");
if let Some(style) = &self.style {
s.field("style", style);
}
s.field("alignment", &self.alignment)
.field("baseline", &self.baseline)
.field("widget", &self.child)
.finish()
}
}
impl PlaceholderSpan for WidgetSpan {
fn alignment(&self) -> PlaceholderAlignment {
self.alignment
}
fn baseline(&self) -> Option<TextBaseline> {
self.baseline
}
}
impl InlineSpan for WidgetSpan {
fn to_string_short(&self) -> String {
waft_foundation::describe_identity("WidgetSpan", self as *const WidgetSpan as usize as u64)
}
fn debug_fill_properties(&self, properties: &mut DiagnosticPropertiesBuilder) {
inline_span_debug_fill_properties(self.style(), properties);
placeholder_span_debug_fill_properties(self, properties);
properties.add(DiagnosticsProperty::<String>::new(
"widget",
Some("Instance of 'Widget'".to_string()),
));
}
fn style(&self) -> Option<&TextStyle> {
self.style.as_ref()
}
fn build(
&self,
builder: &mut ParagraphBuilder,
text_scaler: &TextScaler,
dimensions: Option<&[PlaceholderDimensions]>,
) {
debug_assert!(InlineSpan::debug_assert_is_valid(self));
let dimensions = dimensions.expect("a WidgetSpan is built with its dimensions");
let has_style = self.style.is_some();
if let Some(style) = &self.style {
builder.push_style(style.get_text_style_with(text_scaler));
}
debug_assert!((builder.placeholder_count() as usize) < dimensions.len());
let current_dimensions = dimensions[builder.placeholder_count() as usize];
builder.add_placeholder(
current_dimensions.size.width(),
current_dimensions.size.height(),
self.alignment,
1.0,
current_dimensions.baseline_offset,
current_dimensions.baseline,
);
if has_style {
builder.pop();
}
}
fn visit_children(self: Rc<Self>, visitor: &mut dyn FnMut(&InlineSpanRef) -> bool) -> bool {
visitor(&(self as InlineSpanRef))
}
fn visit_direct_children(&self, _visitor: &mut dyn FnMut(&InlineSpanRef) -> bool) -> bool {
true
}
fn get_span_for_position(self: Rc<Self>, _position: TextPosition) -> Option<InlineSpanRef> {
debug_assert!(InlineSpan::debug_assert_is_valid(&*self));
None
}
fn get_span_for_position_visitor(
self: Rc<Self>,
position: TextPosition,
offset: &mut Accumulator,
) -> Option<InlineSpanRef> {
if position.offset == offset.value() {
return Some(self);
}
offset.increment(1);
None
}
fn compute_to_plain_text(
&self,
buffer: &mut String,
include_semantics_labels: bool,
include_placeholders: bool,
) {
PlaceholderSpan::compute_to_plain_text(
self,
buffer,
include_semantics_labels,
include_placeholders,
);
}
fn code_unit_at_visitor(&self, index: i32, offset: &mut Accumulator) -> Option<u16> {
let local_offset = index - offset.value();
debug_assert!(local_offset >= 0);
offset.increment(1);
(local_offset == 0).then_some(PLACEHOLDER_CODE_UNIT)
}
fn debug_assert_is_valid(&self) -> bool {
true
}
fn compare_to(&self, other: &dyn InlineSpan) -> RenderComparison {
if same_span(self, other) {
return RenderComparison::Identical;
}
let Some(typed_other) = other.as_any().downcast_ref::<WidgetSpan>() else {
return RenderComparison::Layout;
};
if self.style.is_none() != typed_other.style.is_none() {
return RenderComparison::Layout;
}
if !std::rc::Rc::ptr_eq(&self.child, &typed_other.child)
|| self.alignment != typed_other.alignment
{
return RenderComparison::Layout;
}
let mut result = RenderComparison::Identical;
if let (Some(style), Some(other_style)) = (&self.style, &typed_other.style) {
let candidate = style.compare_to(other_style);
if candidate > result {
result = candidate;
}
if result == RenderComparison::Layout {
return result;
}
}
result
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_placeholder_span(&self) -> Option<&dyn PlaceholderSpan> {
Some(self)
}
fn eq_span(&self, other: &dyn InlineSpan) -> bool {
if same_span(self, other) {
return true;
}
let Some(other) = other.as_any().downcast_ref::<WidgetSpan>() else {
return false;
};
other.style == self.style
&& std::rc::Rc::ptr_eq(&other.child, &self.child)
&& other.alignment == self.alignment
&& other.baseline == self.baseline
}
}
#[derive(Debug)]
struct WidgetSpanParentData {
span: WidgetSpan,
child: WidgetRef,
}
impl ParentDataWidget for WidgetSpanParentData {
type ParentData = TextParentData;
fn child(&self) -> &WidgetRef {
&self.child
}
fn apply_parent_data(&self, app: &mut App, render_object: AnyRenderObject) {
let parent_data = render_object.parent_data_of_mut::<TextParentData>(app);
parent_data.span = Some(std::rc::Rc::new(self.span.clone()));
}
}
#[derive(Debug)]
struct AutoScaleInlineWidget {
span: WidgetSpan,
text_scale_factor: f64,
child: WidgetRef,
}
impl RenderObjectWidget for AutoScaleInlineWidget {
type RenderObject = RenderScaledInlineWidget;
fn key(&self) -> Option<&KeyRef> {
None
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderScaledInlineWidget::new(
app,
self.span.alignment,
self.span.baseline,
self.text_scale_factor,
)
.as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderScaledInlineWidget>,
) {
render_object.set_alignment(app, self.span.alignment);
render_object.set_baseline(app, self.span.baseline);
render_object.set_scale(app, self.text_scale_factor);
}
}
impl SingleChildRenderObjectWidget for AutoScaleInlineWidget {
fn child(&self) -> Option<&WidgetRef> {
Some(&self.child)
}
}
struct RenderScaledInlineWidget {
render_object: RenderObjectData,
render_box: RenderBoxData,
child: RenderObjectWithChildData<AnyRenderBox>,
scale: f64,
alignment: PlaceholderAlignment,
baseline: Option<TextBaseline>,
}
impl RenderScaledInlineWidget {
fn new(
app: &mut App,
alignment: PlaceholderAlignment,
baseline: Option<TextBaseline>,
scale: f64,
) -> RenderHandle<Self> {
RenderHandle::new_box(
app,
RenderScaledInlineWidget {
render_object: RenderObjectData::new(),
render_box: RenderBoxData::new(),
child: RenderObjectWithChildData::new(),
scale,
alignment,
baseline,
},
)
}
fn scale(self: RenderHandle<Self>, app: &App) -> f64 {
self.get(app).scale
}
fn set_scale(self: RenderHandle<Self>, app: &mut App, value: f64) {
if value == self.get(app).scale {
return;
}
debug_assert!(value > 0.0);
debug_assert!(value.is_finite());
self.get_mut(app).scale = value;
self.as_object().mark_needs_layout(app);
}
fn set_alignment(self: RenderHandle<Self>, app: &mut App, value: PlaceholderAlignment) {
if self.get(app).alignment == value {
return;
}
self.get_mut(app).alignment = value;
self.as_object().mark_needs_layout(app);
}
fn set_baseline(self: RenderHandle<Self>, app: &mut App, value: Option<TextBaseline>) {
if value == self.get(app).baseline {
return;
}
self.get_mut(app).baseline = value;
self.as_object().mark_needs_layout(app);
}
fn scale_matrix(self: RenderHandle<Self>, app: &App) -> Matrix4 {
let scale = self.scale(app) as f32;
Matrix4::scale(scale, scale)
}
}
impl RenderObjectWithChildMixin for RenderScaledInlineWidget {
type ChildType = AnyRenderBox;
fn child_data(self: RenderHandle<Self>, app: &App) -> &RenderObjectWithChildData<AnyRenderBox> {
&self.get(app).child
}
fn child_data_mut(
self: RenderHandle<Self>,
app: &mut App,
) -> &mut RenderObjectWithChildData<AnyRenderBox> {
&mut self.get_mut(app).child
}
}
impl RenderObject for RenderScaledInlineWidget {
waft_rendering::render_object_accessors!();
fn perform_layout(self: RenderHandle<Self>, app: &mut App) {
let Some(child) = self.child(app) else {
return;
};
let constraints = self.constraints(app);
debug_assert!(!constraints.has_bounded_height());
let scale = self.scale(app);
child.layout(
app,
BoxConstraints::new().max_width(constraints.max_width / scale),
true,
);
let size = constraints.constrain(child.size(app) * scale);
self.set_size(app, size);
}
fn paint(
self: RenderHandle<Self>,
app: &mut App,
context: &mut PaintingContext,
offset: Offset,
) {
let Some(child) = self.child(app) else {
self.as_object().set_layer(app, None);
return;
};
if self.scale(app) == 1.0 {
context.paint_child(app, child.as_object(), offset);
self.as_object().set_layer(app, None);
return;
}
let old_layer = self.as_object().layer_as::<TransformLayer>(app);
let layer = context.push_transform(
app,
self.as_object().needs_compositing(app),
offset,
self.scale_matrix(app),
|app, context, offset| context.paint_child(app, child.as_object(), offset),
old_layer,
);
self.as_object()
.set_layer(app, layer.map(|layer| layer.as_container_layer()));
}
fn visit_children(
self: RenderHandle<Self>,
app: &App,
visitor: &mut dyn FnMut(AnyRenderObject),
) {
if let Some(child) = self.child(app) {
visitor(child.as_object());
}
}
}
impl RenderBox for RenderScaledInlineWidget {
waft_rendering::render_box_accessors!();
fn compute_max_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
let scale = self.scale(app);
self.child(app).map_or(0.0, |child| {
child.get_max_intrinsic_height(app, width / scale)
}) * scale
}
fn compute_max_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
let scale = self.scale(app);
self.child(app).map_or(0.0, |child| {
child.get_max_intrinsic_width(app, height / scale)
}) * scale
}
fn compute_min_intrinsic_height(self: RenderHandle<Self>, app: &mut App, width: f64) -> f64 {
let scale = self.scale(app);
self.child(app).map_or(0.0, |child| {
child.get_min_intrinsic_height(app, width / scale)
}) * scale
}
fn compute_min_intrinsic_width(self: RenderHandle<Self>, app: &mut App, height: f64) -> f64 {
let scale = self.scale(app);
self.child(app).map_or(0.0, |child| {
child.get_min_intrinsic_width(app, height / scale)
}) * scale
}
fn compute_distance_to_actual_baseline(
self: RenderHandle<Self>,
app: &mut App,
baseline: TextBaseline,
) -> Option<f64> {
let scale = self.scale(app);
self.child(app)
.and_then(|child| child.get_distance_to_actual_baseline(app, baseline))
.map(|child_baseline| scale * child_baseline)
}
fn compute_dry_baseline(
self: RenderHandle<Self>,
app: &mut App,
constraints: BoxConstraints,
baseline: TextBaseline,
) -> Option<f64> {
let scale = self.scale(app);
let distance = self.child(app)?.get_dry_baseline(
app,
BoxConstraints::new().max_width(constraints.max_width / scale),
baseline,
);
distance.map(|distance| scale * distance)
}
fn compute_dry_layout(
self: RenderHandle<Self>,
app: &mut App,
constraints: BoxConstraints,
) -> Size {
debug_assert!(!constraints.has_bounded_height());
let scale = self.scale(app);
let unscaled_size = self.child(app).map_or(Size::ZERO, |child| {
child.get_dry_layout(
app,
BoxConstraints::new().max_width(constraints.max_width / scale),
)
});
constraints.constrain(unscaled_size * scale)
}
fn apply_paint_transform(
self: RenderHandle<Self>,
app: &App,
_child: AnyRenderObject,
transform: &mut Matrix4,
) {
*transform = transform.then(&self.scale_matrix(app));
}
fn hit_test_children(
self: RenderHandle<Self>,
app: &mut App,
result: &mut BoxHitTestResult<'_>,
position: Offset,
) -> bool {
let Some(child) = self.child(app) else {
return false;
};
let transform = self.scale_matrix(app);
result.add_with_paint_transform(Some(transform), position, |result, transformed| {
child.hit_test(app, result, transformed)
})
}
}
#[cfg(test)]
mod tests {
use waft_embedder::Color;
use waft_embedder::{TextDirection, TextLeadingDistribution};
use waft_foundation::AppCell;
use waft_gestures::HitTestResult;
use waft_painting::{PaintingBinding, TextOverflow, TextSpan};
use waft_rendering::{RenderColoredBox, RenderIntrinsicHeight, RenderParagraph};
use super::*;
use crate::test_harness::Harness;
use crate::widgets::basic::{
Center, ColoredBox, Directionality, IntrinsicHeight, Row, SizedBox,
};
use crate::widgets::text::{RichText, Text};
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 mount(app: &mut App, child: WidgetRef) -> Harness {
install_fonts(app);
let harness = Harness::mount(
app,
Directionality::new(TextDirection::Ltr, child).into_widget(),
);
harness.pump(app);
harness
}
fn only_child(app: &App, parent: AnyRenderObject) -> AnyRenderObject {
let mut children = Vec::new();
parent.visit_children(app, &mut |child| children.push(child));
assert_eq!(children.len(), 1, "one child");
children[0]
}
fn paragraph_under(app: &App, harness: &Harness) -> RenderHandle<RenderParagraph> {
only_child(app, harness.render_root(app).as_object())
.downcast::<RenderParagraph>(app)
.expect("a RenderParagraph")
}
fn sized_box(width: f64, height: f64) -> WidgetRef {
SizedBox::new().width(width).height(height).into_widget()
}
fn text(text: &str) -> InlineSpanRef {
TextSpan::new().text(text).into_span()
}
#[test]
fn a_widget_span_lays_its_child_out_between_the_text() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let span = TextSpan::new()
.children(vec![
text("ab"),
WidgetSpan::new(sized_box(30.0, 20.0)).into_span(),
text("cd"),
])
.into_span();
let harness = mount(
&mut app,
Center::new().child(Text::rich(span)).into_widget(),
);
let paragraph = only_child(&app, harness.render_root(&app).as_object())
.downcast::<waft_rendering::RenderPositionedBox>(&app)
.map(|center| only_child(&app, center.as_object()))
.and_then(|child| child.downcast::<RenderParagraph>(&app))
.expect("a RenderParagraph under the Center");
assert_eq!(
paragraph.text(&app).to_plain_text(true, true),
"ab\u{FFFC}cd"
);
let inline = only_child(&app, paragraph.as_object());
let parent_data = inline.parent_data_of::<TextParentData>(&app);
assert!(
parent_data.span.is_some(),
"the parent data widget set the span"
);
let offset = parent_data.offset().expect("laid out");
assert!(offset.dx() > 0.0, "after \"ab\"");
let inline = inline.as_box().expect("a box");
assert_eq!(inline.size(&app), Size::new(30.0, 20.0));
let width = paragraph.size(&app).width();
assert!(width > offset.dx() + 30.0, "\"cd\" follows the widget");
assert!(paragraph.size(&app).height() >= 20.0);
}
#[test]
fn inline_widgets_works_with_ellipsis() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let truncated =
RichText::new(text("widget should be truncated")).text_direction(TextDirection::Rtl);
let span = TextSpan::new()
.children(vec![
text("a very very very very very very very very very very long line"),
WidgetSpan::new(SizedBox::new().width(20.0).height(40.0).child(truncated))
.into_span(),
])
.into_span();
let harness = mount(
&mut app,
Text::rich(span)
.max_lines(1)
.overflow(TextOverflow::Ellipsis)
.into_widget(),
);
let paragraph = paragraph_under(&app, &harness);
let inline = only_child(&app, paragraph.as_object());
assert_eq!(
inline.parent_data_of::<TextParentData>(&app).offset(),
None,
"ellipsized away"
);
}
#[test]
fn a_hit_on_an_inline_widget_reaches_it_and_a_hit_on_the_text_lands_on_its_span() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let opaque = ColoredBox::new(Color::from_argb(255, 0, 0, 255)).child(sized_box(30.0, 20.0));
let span = TextSpan::new()
.children(vec![text("ab"), WidgetSpan::new(opaque).into_span()])
.into_span();
let harness = mount(
&mut app,
Center::new().child(Text::rich(span)).into_widget(),
);
let center = only_child(&app, harness.render_root(&app).as_object());
let paragraph = only_child(&app, center)
.downcast::<RenderParagraph>(&app)
.expect("a RenderParagraph");
let inline = only_child(&app, paragraph.as_object());
let offset = inline
.parent_data_of::<TextParentData>(&app)
.offset()
.expect("laid out");
let mut result = HitTestResult::new();
let hit = paragraph.as_box().hit_test(
&mut app,
&mut BoxHitTestResult::wrap(&mut result),
offset + Offset::new(15.0, 10.0),
);
assert!(hit);
assert!(
only_child(&app, inline)
.downcast::<RenderColoredBox>(&app)
.is_some()
);
assert_eq!(
result.path().len(),
3,
"the colored box, the scaled inline box and the paragraph"
);
let mut result = HitTestResult::new();
paragraph.as_box().hit_test(
&mut app,
&mut BoxHitTestResult::wrap(&mut result),
Offset::new(1.0, offset.dy() + 10.0),
);
assert_eq!(result.path().len(), 2, "the span and the paragraph");
let target: &dyn std::any::Any = result.path()[0].target();
assert_eq!(
target
.downcast_ref::<TextSpan>()
.and_then(|span| span.text.as_deref()),
Some("ab")
);
}
#[test]
fn inline_widgets_works_with_text_scaler() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let rich = |scale: f64| {
let span = TextSpan::new()
.children(vec![WidgetSpan::new(sized_box(30.0, 20.0)).into_span()])
.into_span();
Center::new()
.child(Text::rich(span).text_scaler(TextScaler::linear(scale)))
.into_widget()
};
let harness = mount(&mut app, rich(1.0));
let inline_size = |app: &App| {
let center = only_child(app, harness.render_root(app).as_object());
let paragraph = only_child(app, center);
only_child(app, paragraph)
.as_box()
.expect("a box")
.size(app)
};
assert_eq!(inline_size(&app), Size::new(30.0, 20.0));
harness.set_child(
&mut app,
Directionality::new(TextDirection::Ltr, rich(2.0)).into_widget(),
);
harness.pump(&mut app);
assert_eq!(inline_size(&app), Size::new(60.0, 40.0));
}
#[test]
fn widget_span_calculate_correct_intrinsic_heights() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let line = TextStyle::new()
.height(1.0)
.font_size(16.0)
.leading_distribution(TextLeadingDistribution::Even);
let span = TextSpan::new()
.children(vec![
TextSpan::new()
.text("Start\n")
.style(line.clone())
.into_span(),
WidgetSpan::new(Row::new().children([sized_box(16.0, 16.0)])).into_span(),
TextSpan::new().text("End").style(line).into_span(),
])
.into_span();
let harness = mount(
&mut app,
Center::new()
.child(IntrinsicHeight::new().child(RichText::new(span)))
.into_widget(),
);
let center = only_child(&app, harness.render_root(&app).as_object());
let intrinsic = only_child(&app, center)
.downcast::<RenderIntrinsicHeight>(&app)
.expect("a RenderIntrinsicHeight");
assert_eq!(intrinsic.size(&app).height(), 3.0 * 16.0);
}
struct TestSelectionRegistrar {
selectables: Vec<waft_rendering::AnySelectable>,
}
impl waft_rendering::SelectionRegistrar for TestSelectionRegistrar {
fn add(
self: waft_foundation::Handle<Self>,
app: &mut App,
selectable: waft_rendering::AnySelectable,
) {
app.get_mut(self).selectables.push(selectable);
}
fn remove(
self: waft_foundation::Handle<Self>,
app: &mut App,
selectable: waft_rendering::AnySelectable,
) {
app.get_mut(self).selectables.retain(|s| *s != selectable);
}
}
#[test]
fn can_handle_select_all_when_contains_widget_span() {
use waft_rendering::{ContainerRenderObjectMixin as _, SelectionEvent, SelectionStatus};
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let registrar = app.create(TestSelectionRegistrar {
selectables: Vec::new(),
});
let render_box = RenderParagraph::new(&mut app, text("widget"), TextDirection::Ltr, None);
let widget_span = WidgetSpan::new(Text::new("widget"));
let paragraph = RenderParagraph::new(
&mut app,
TextSpan::new()
.children(vec![
text("before the span"),
widget_span.clone().into_span(),
text("after the span"),
])
.into_span(),
TextDirection::Ltr,
None,
);
paragraph.set_registrar(&mut app, Some(registrar.erase()));
paragraph.add(&mut app, render_box.as_box());
render_box
.as_object()
.parent_data_of_mut::<TextParentData>(&mut app)
.span = Some(std::rc::Rc::new(widget_span));
waft_rendering::testing::layout(&mut app, paragraph.as_box());
let selectables = app.get(®istrar).selectables.clone();
assert_eq!(selectables.len(), 2);
for segment in selectables {
segment.dispatch_selection_event(&mut app, &SelectionEvent::SelectAll);
let geometry = segment.value(&app);
assert!(geometry.has_content);
assert_eq!(geometry.status, SelectionStatus::Uncollapsed);
}
}
}