use std::any::Any;
use std::rc::Rc;
use std::sync::Arc;
use waft_embedder::{Clip, Matrix4, Offset, Path, Size, TextDirection};
use waft_foundation::App;
use waft_painting::{AlignmentGeometry, AnyColor, Decoration, EdgeInsetsGeometry};
use waft_rendering::{
AnyRenderObject, BoxConstraints, Constraints, CustomClipper, DecorationPosition, RenderBox,
RenderDecoratedBox, RenderHandle,
};
use crate::framework::{
BuildContext, IntoWidget, KeyRef, RenderObjectWidget, SingleChildRenderObjectWidget,
StatelessWidget, WidgetRef,
};
use crate::widgets::basic::{
Align, ClipPath, ColoredBox, ConstrainedBox, Directionality, LimitedBox, Padding, Transform,
};
use crate::widgets::image::create_local_image_configuration;
#[derive(Debug)]
pub struct DecoratedBox {
pub key: Option<KeyRef>,
pub decoration: Box<dyn Decoration>,
pub position: DecorationPosition,
pub child: Option<WidgetRef>,
}
impl DecoratedBox {
pub fn new(decoration: impl Decoration + 'static) -> DecoratedBox {
DecoratedBox {
key: None,
decoration: Box::new(decoration),
position: DecorationPosition::Background,
child: None,
}
}
pub fn key(mut self, key: KeyRef) -> DecoratedBox {
self.key = Some(key);
self
}
pub fn position(mut self, position: DecorationPosition) -> DecoratedBox {
self.position = position;
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> DecoratedBox {
self.child = Some(child.into_widget());
self
}
}
impl RenderObjectWidget for DecoratedBox {
type RenderObject = RenderDecoratedBox;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
let configuration = create_local_image_configuration(app, context, None);
RenderDecoratedBox::new(
app,
self.decoration.clone_box(),
self.position,
configuration,
None,
)
.as_object()
}
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: RenderHandle<RenderDecoratedBox>,
) {
render_object.set_decoration(app, self.decoration.clone_box());
let configuration = create_local_image_configuration(app, context, None);
render_object.set_configuration(app, configuration);
render_object.set_position(app, self.position);
}
}
impl SingleChildRenderObjectWidget for DecoratedBox {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
#[derive(Debug)]
pub struct Container {
pub key: Option<KeyRef>,
pub alignment: Option<AlignmentGeometry>,
pub padding: Option<EdgeInsetsGeometry>,
pub color: Option<AnyColor>,
pub is_anti_alias: bool,
pub decoration: Option<Box<dyn Decoration>>,
pub foreground_decoration: Option<Box<dyn Decoration>>,
pub constraints: Option<BoxConstraints>,
given_constraints: Option<BoxConstraints>,
width: Option<f64>,
height: Option<f64>,
pub margin: Option<EdgeInsetsGeometry>,
pub clip_behavior: Clip,
pub transform: Option<Matrix4>,
pub transform_alignment: Option<AlignmentGeometry>,
pub child: Option<WidgetRef>,
}
impl Container {
pub fn new() -> Container {
Container::default()
}
pub fn key(mut self, key: KeyRef) -> Container {
self.key = Some(key);
self
}
pub fn alignment(mut self, alignment: AlignmentGeometry) -> Container {
self.alignment = Some(alignment);
self
}
pub fn padding(mut self, padding: EdgeInsetsGeometry) -> Container {
debug_assert!(padding.is_non_negative());
self.padding = Some(padding);
self
}
pub fn color(mut self, color: impl Into<AnyColor>) -> Container {
debug_assert!(self.decoration.is_none(), "{CANNOT_PROVIDE_BOTH}");
self.color = Some(color.into());
self
}
pub fn is_anti_alias(mut self, is_anti_alias: bool) -> Container {
self.is_anti_alias = is_anti_alias;
self
}
pub fn decoration(mut self, decoration: impl Decoration + 'static) -> Container {
debug_assert!(self.color.is_none(), "{CANNOT_PROVIDE_BOTH}");
debug_assert!(decoration.debug_assert_is_valid());
self.decoration = Some(Box::new(decoration));
self
}
pub fn foreground_decoration(
mut self,
foreground_decoration: impl Decoration + 'static,
) -> Container {
self.foreground_decoration = Some(Box::new(foreground_decoration));
self
}
pub fn width(mut self, width: f64) -> Container {
self.width = Some(width);
self.fold_constraints();
self
}
pub fn height(mut self, height: f64) -> Container {
self.height = Some(height);
self.fold_constraints();
self
}
pub fn constraints(mut self, constraints: BoxConstraints) -> Container {
debug_assert!(constraints.debug_assert_is_valid(false));
self.given_constraints = Some(constraints);
self.fold_constraints();
self
}
pub fn margin(mut self, margin: EdgeInsetsGeometry) -> Container {
debug_assert!(margin.is_non_negative());
self.margin = Some(margin);
self
}
pub fn clip_behavior(mut self, clip_behavior: Clip) -> Container {
self.clip_behavior = clip_behavior;
self
}
pub fn transform(mut self, transform: Matrix4) -> Container {
self.transform = Some(transform);
self
}
pub fn transform_alignment(mut self, transform_alignment: AlignmentGeometry) -> Container {
self.transform_alignment = Some(transform_alignment);
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> Container {
self.child = Some(child.into_widget());
self
}
fn fold_constraints(&mut self) {
self.constraints = if self.width.is_some() || self.height.is_some() {
Some(match self.given_constraints {
Some(constraints) => constraints.tighten(self.width, self.height),
None => BoxConstraints::tight_for(self.width, self.height),
})
} else {
self.given_constraints
};
}
fn padding_including_decoration(&self) -> Option<EdgeInsetsGeometry> {
let decoration_padding = self
.decoration
.as_ref()
.map(|decoration| decoration.padding());
match (self.padding, decoration_padding) {
(None, padding) => padding,
(padding, None) => padding,
(Some(padding), Some(decoration_padding)) => Some(padding.add(decoration_padding)),
}
}
}
const CANNOT_PROVIDE_BOTH: &str = "Cannot provide both a color and a decoration.\n\
The color argument is just a shorthand for \"decoration: BoxDecoration(color: color)\".\n\
To use both a color and other decoration properties, set the color in the BoxDecoration \
instead.";
impl Default for Container {
fn default() -> Container {
Container {
key: None,
alignment: None,
padding: None,
color: None,
is_anti_alias: true,
decoration: None,
foreground_decoration: None,
constraints: None,
given_constraints: None,
width: None,
height: None,
margin: None,
clip_behavior: Clip::None,
transform: None,
transform_alignment: None,
child: None,
}
}
}
struct DecorationClipper {
text_direction: TextDirection,
decoration: Box<dyn Decoration>,
}
impl DecorationClipper {
fn new(text_direction: Option<TextDirection>, decoration: Box<dyn Decoration>) -> Self {
DecorationClipper {
text_direction: text_direction.unwrap_or(TextDirection::Ltr),
decoration,
}
}
}
impl CustomClipper<Arc<Path>> for DecorationClipper {
fn get_clip(&self, size: Size) -> Arc<Path> {
self.decoration
.get_clip_path(Offset::ZERO & size, self.text_direction)
}
fn should_reclip(&self, old_clipper: &dyn CustomClipper<Arc<Path>>) -> bool {
let Some(old_clipper) = old_clipper.as_any().downcast_ref::<DecorationClipper>() else {
return true;
};
!old_clipper.decoration.eq_decoration(&*self.decoration)
|| old_clipper.text_direction != self.text_direction
}
fn as_any(&self) -> &dyn Any {
self
}
}
impl StatelessWidget for Container {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
debug_assert!(self.decoration.is_some() || self.clip_behavior == Clip::None);
let mut current = self.child.clone();
if self.child.is_none()
&& self
.constraints
.is_none_or(|constraints| !constraints.is_tight())
{
current = Some(
LimitedBox::new()
.max_width(0.0)
.max_height(0.0)
.child(ConstrainedBox::new(BoxConstraints::expand(None, None)))
.into_widget(),
);
} else if let Some(alignment) = self.alignment {
current = Some(
Align {
alignment,
child: current,
..Align::new()
}
.into_widget(),
);
}
if let Some(effective_padding) = self.padding_including_decoration() {
current = Some(
Padding {
key: None,
padding: effective_padding,
child: current,
}
.into_widget(),
);
}
if let Some(color) = &self.color {
current = Some(
ColoredBox {
child: current,
..ColoredBox::new(color.clone()).is_anti_alias(self.is_anti_alias)
}
.into_widget(),
);
}
if self.clip_behavior != Clip::None {
let decoration = self
.decoration
.as_ref()
.expect("clip_behavior needs a decoration");
let text_direction = Directionality::maybe_of(app, context);
current = Some(
ClipPath {
key: None,
clipper: Some(Rc::new(DecorationClipper::new(
text_direction,
decoration.clone_box(),
))),
clip_behavior: self.clip_behavior,
child: current,
}
.into_widget(),
);
}
if let Some(decoration) = &self.decoration {
current = Some(
DecoratedBox {
key: None,
decoration: decoration.clone_box(),
position: DecorationPosition::Background,
child: current,
}
.into_widget(),
);
}
if let Some(foreground_decoration) = &self.foreground_decoration {
current = Some(
DecoratedBox {
key: None,
decoration: foreground_decoration.clone_box(),
position: DecorationPosition::Foreground,
child: current,
}
.into_widget(),
);
}
if let Some(constraints) = self.constraints {
current = Some(
ConstrainedBox {
key: None,
constraints,
child: current,
}
.into_widget(),
);
}
if let Some(margin) = self.margin {
current = Some(
Padding {
key: None,
padding: margin,
child: current,
}
.into_widget(),
);
}
if let Some(transform) = self.transform {
current = Some(
Transform {
alignment: self.transform_alignment,
child: current,
..Transform::new(transform)
}
.into_widget(),
);
}
current.expect("a Container builds at least one widget")
}
}
#[cfg(test)]
mod tests {
use waft_embedder::{Clip, Color, Offset, SceneBuilder, Size, TextDirection};
use waft_foundation::AppCell;
use waft_painting::{Border, BorderSide, BorderStyle, BoxDecoration};
use waft_rendering::{
AnyRenderBox, BoxParentData, RenderAligningShiftedBox, RenderBox, RenderColoredBox,
RenderConstrainedBox, RenderDecoratedBox, RenderHandle, RenderLimitedBox, RenderObject,
RenderObjectWithChildMixin, RenderPadding, RenderPositionedBox, RenderTransform,
};
use super::*;
use crate::test_harness::Harness;
use crate::widgets::basic::SizedBox;
fn typed_child<P, C: RenderObject>(parent: RenderHandle<P>, app: &App) -> RenderHandle<C>
where
P: RenderObject + RenderObjectWithChildMixin<ChildType = AnyRenderBox>,
{
parent
.child(app)
.expect("a child")
.as_object()
.downcast::<C>(app)
.expect("the expected render object")
}
fn root_child<T: RenderObject>(harness: &Harness, app: &App) -> RenderHandle<T> {
harness
.render_root(app)
.child(app)
.expect("a child")
.as_object()
.downcast::<T>(app)
.expect("the expected render object")
}
#[test]
fn decorated_box_paints_its_decoration_and_updates_in_place() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let red = Color::from_argb(255, 255, 0, 0);
let blue = Color::from_argb(255, 0, 0, 255);
let decorated = |color: Color| {
DecoratedBox {
key: None,
decoration: Box::new(BoxDecoration::new().color(color)),
position: DecorationPosition::Background,
child: Some(SizedBox::new().width(10.0).height(10.0).into_widget()),
}
.into_widget()
};
let harness = Harness::mount(&mut app, decorated(red));
harness.pump(&mut app);
let render_object = root_child::<RenderDecoratedBox>(&harness, &app);
assert!(!render_object.as_object().debug_needs_paint(&app));
let scene = harness
.render_root(&app)
.as_object()
.debug_layer(&app)
.expect("painted")
.build_scene(&mut app, SceneBuilder::new());
let ops = waft_embedder::flattened_ops(
&waft_embedder::slices(&scene, |_| None).picture_with_room(),
);
assert!(
ops.iter()
.any(|op| matches!(op, waft_embedder::valo::Op::DrawRect { .. })),
"the decoration's fill: {ops:?}"
);
harness.set_child(&mut app, decorated(blue));
harness.pump(&mut app);
let same = root_child::<RenderDecoratedBox>(&harness, &app);
assert_eq!(
same, render_object,
"the same RenderDecoratedBox is reconfigured"
);
let decoration = render_object
.decoration(&app)
.as_any()
.downcast_ref::<BoxDecoration>()
.expect("a BoxDecoration");
assert_eq!(decoration.color, Some(blue.into()));
}
#[test]
fn a_container_builds_dart_s_chain_outside_in() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
Container::new()
.alignment(AlignmentGeometry::TOP_LEFT)
.padding(EdgeInsetsGeometry::all(4.0))
.color(Color::from_argb(255, 255, 0, 0))
.width(80.0)
.height(60.0)
.margin(EdgeInsetsGeometry::all(2.0))
.transform(Matrix4::scale(2.0, 2.0))
.transform_alignment(AlignmentGeometry::CENTER)
.child(SizedBox::new().width(10.0).height(10.0))
.into_widget(),
);
harness.pump(&mut app);
let transform = root_child::<RenderTransform>(&harness, &app);
assert_eq!(transform.alignment(&app), Some(AlignmentGeometry::CENTER));
let margin = typed_child::<_, RenderPadding>(transform, &app);
assert_eq!(margin.padding(&app), EdgeInsetsGeometry::all(2.0));
let constrained = typed_child::<_, RenderConstrainedBox>(margin, &app);
assert_eq!(
constrained.additional_constraints(&app),
BoxConstraints::tight_for(Some(80.0), Some(60.0)),
"width and height fold into the constraints"
);
let colored = typed_child::<_, RenderColoredBox>(constrained, &app);
assert!(colored.is_anti_alias(&app));
let padding = typed_child::<_, RenderPadding>(colored, &app);
assert_eq!(padding.padding(&app), EdgeInsetsGeometry::all(4.0));
let align = typed_child::<_, RenderPositionedBox>(padding, &app);
assert_eq!(align.alignment(&app), AlignmentGeometry::TOP_LEFT);
let child = typed_child::<_, RenderConstrainedBox>(align, &app);
assert_eq!(child.size(&app), Size::new(10.0, 10.0));
assert_eq!(
transform.size(&app),
Size::new(84.0, 64.0),
"the margin is outside the constrained extent"
);
}
#[test]
fn a_childless_container_expands_inside_a_limited_box() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(&mut app, Container::new().into_widget());
harness.pump(&mut app);
let limited = root_child::<RenderLimitedBox>(&harness, &app);
assert_eq!(limited.max_width(&app), 0.0);
assert_eq!(limited.max_height(&app), 0.0);
let expanded = typed_child::<_, RenderConstrainedBox>(limited, &app);
assert_eq!(
expanded.additional_constraints(&app),
BoxConstraints::expand(None, None)
);
assert_eq!(
limited.size(&app),
Size::new(300.0, 200.0),
"bounded constraints win over the zero limits"
);
}
#[test]
fn a_decoration_adds_its_padding_and_paints_behind_the_child() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let decoration = BoxDecoration::new()
.color(Color::from_argb(255, 0, 0, 255))
.border(Border::all(
Color::from_argb(255, 0, 0, 0),
3.0,
BorderStyle::Solid,
BorderSide::STROKE_ALIGN_INSIDE,
));
let decoration_padding = decoration.padding();
let harness = Harness::mount(
&mut app,
Container::new()
.padding(EdgeInsetsGeometry::all(4.0))
.decoration(decoration)
.child(SizedBox::new().width(10.0).height(10.0))
.into_widget(),
);
harness.pump(&mut app);
let decorated = root_child::<RenderDecoratedBox>(&harness, &app);
let padding = typed_child::<_, RenderPadding>(decorated, &app);
assert_eq!(
padding.padding(&app),
EdgeInsetsGeometry::all(4.0).add(decoration_padding),
"Dart's _paddingIncludingDecoration"
);
assert_eq!(
decorated.size(&app),
Size::new(24.0, 24.0),
"the child, the padding, and the border's own inset"
);
}
#[test]
fn an_aligned_container_positions_its_child() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
Container::new()
.alignment(AlignmentGeometry::BOTTOM_RIGHT)
.child(SizedBox::new().width(10.0).height(10.0))
.into_widget(),
);
harness.pump(&mut app);
let align = root_child::<RenderPositionedBox>(&harness, &app);
assert_eq!(
align.size(&app),
Size::new(300.0, 200.0),
"an alignment expands to fill the parent"
);
let child = typed_child::<_, RenderConstrainedBox>(align, &app);
assert_eq!(
child
.as_object()
.parent_data_of::<BoxParentData>(&app)
.offset,
Offset::new(290.0, 190.0)
);
}
#[test]
fn width_and_height_tighten_the_constraints_in_any_setter_order() {
let container = Container::new()
.constraints(BoxConstraints::new().min_width(20.0).max_width(200.0))
.width(80.0);
assert_eq!(
container.constraints,
Some(BoxConstraints::new().min_width(80.0).max_width(80.0)),
"Dart's constraints.tighten(width: ..)"
);
let reordered = Container::new()
.width(80.0)
.constraints(BoxConstraints::new().min_width(20.0).max_width(200.0));
assert_eq!(reordered.constraints, container.constraints);
let unconstrained = Container::new().height(60.0);
assert_eq!(
unconstrained.constraints,
Some(BoxConstraints::tight_for(None, Some(60.0))),
"Dart's BoxConstraints.tightFor(height: ..)"
);
}
#[test]
fn a_clip_behavior_clips_the_decoration_through_its_clip_path() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
Directionality::new(
TextDirection::Ltr,
Container::new()
.decoration(
BoxDecoration::new()
.color(Color::new(0xFF00FF00))
.border_radius(waft_painting::BorderRadiusGeometry::circular(6.0)),
)
.clip_behavior(Clip::AntiAlias)
.child(SizedBox::shrink()),
)
.into_widget(),
);
harness.pump(&mut app);
let decorated = harness
.render_root(&app)
.child(&app)
.expect("the container's tree")
.as_object()
.downcast::<waft_rendering::RenderDecoratedBox>(&app)
.expect("the background DecoratedBox");
let clip = decorated
.child(&app)
.expect("the clip under the decoration")
.as_object()
.downcast::<waft_rendering::RenderClipPath>(&app)
.expect("a RenderClipPath");
assert_eq!(
waft_rendering::RenderCustomClip::clip_behavior(clip, &app),
Clip::AntiAlias
);
}
}