use std::any::Any;
use std::fmt::{self, Debug};
use std::rc::Rc;
use std::sync::Arc;
use waft_embedder::{
BlendMode, Clip, ImageFilter, Matrix4, Offset, Path, RRect, RSuperellipse, Rect, Size,
TextBaseline, TextDirection,
};
use waft_foundation::{App, Handle};
use waft_gestures::{
PointerCancelEventListener, PointerDownEventListener, PointerEnterEventListener,
PointerExitEventListener, PointerHoverEventListener, PointerMoveEventListener,
PointerPanZoomEndEventListener, PointerPanZoomStartEventListener,
PointerPanZoomUpdateEventListener, PointerSignalEventListener, PointerUpEventListener,
};
use waft_painting::{
Alignment, AlignmentGeometry, AnyColor, Axis, AxisDirection, BorderRadiusGeometry, BoxFit,
EdgeInsetsGeometry, ShapeBorder, VerticalDirection, flip_axis_direction,
text_direction_to_axis_direction,
};
use waft_rendering::{
AnyRenderObject, BackdropKey, BoxConstraints, BoxConstraintsTransform, ChildLayoutId,
CrossAxisAlignment, CustomClipper, CustomPainter, FlexFit, FlexParentData, HitTestBehavior,
ImageFilterConfig, LayerLink, MainAxisAlignment, MainAxisSize, MultiChildLayoutDelegate,
MultiChildLayoutParentData, OverflowBoxFit, RelativeRect, RenderAbsorbPointer,
RenderAligningShiftedBox, RenderAspectRatio, RenderBackdropFilter, RenderBaseline, RenderBox,
RenderClipOval, RenderClipPath, RenderClipRRect, RenderClipRSuperellipse, RenderClipRect,
RenderColoredBox, RenderConstrainedBox, RenderConstrainedOverflowBox,
RenderConstraintsTransformBox, RenderCustomClip, RenderCustomMultiChildLayoutBox,
RenderCustomPaint, RenderCustomSingleChildLayoutBox, RenderFittedBox, RenderFlex,
RenderFollowerLayer, RenderFractionalTranslation, RenderFractionallySizedOverflowBox,
RenderHandle, RenderIgnorePointer, RenderIndexedStack, RenderIntrinsicHeight,
RenderIntrinsicWidth, RenderLeaderLayer, RenderLimitedBox, RenderMetaData, RenderMouseRegion,
RenderOffstage, RenderOpacity, RenderPadding, RenderPointerListener, RenderPositionedBox,
RenderRepaintBoundary, RenderSizedOverflowBox, RenderSliver, RenderSliverPadding,
RenderSliverToBoxAdapter, RenderStack, RenderStackBase, RenderTransform, RenderWrap,
ShapeBorderClipper, SingleChildLayoutDelegate, StackFit, StackParentData, WrapAlignment,
WrapCrossAlignment,
};
use waft_services::{MouseCursor, MouseCursorRef};
use crate::framework::{
BuildContext, InheritedWidget, IntoWidget, KeyRef, MultiChildRenderObjectWidget,
ParentDataWidget, RenderObjectWidget, SingleChildRenderObjectWidget, StatelessWidget,
WidgetRef,
};
use crate::widgets::focus_scope::ExcludeFocus;
use crate::widgets::overlay::TheaterParentData;
#[derive(Debug)]
pub struct Directionality {
pub key: Option<KeyRef>,
pub text_direction: TextDirection,
pub child: WidgetRef,
}
impl Directionality {
pub fn new<K>(text_direction: TextDirection, child: impl IntoWidget<K>) -> Directionality {
Directionality {
key: None,
text_direction,
child: child.into_widget(),
}
}
pub fn key(mut self, key: KeyRef) -> Directionality {
self.key = Some(key);
self
}
pub fn of(app: &mut App, context: BuildContext) -> TextDirection {
let widget = context
.depend_on_inherited_widget_of_exact_type::<Directionality>(app)
.expect(
"No Directionality widget found. This widget requires a Directionality widget \
ancestor. Typically, the Directionality widget is introduced by the WidgetsApp \
widget at the top of your application widget tree. It determines the ambient \
reading direction and is used, for example, to determine how to lay out text, \
how to interpret \"start\" and \"end\" values, and to resolve \
EdgeInsetsDirectional, AlignmentDirectional, and other *Directional objects.",
);
widget.text_direction
}
pub fn maybe_of(app: &mut App, context: BuildContext) -> Option<TextDirection> {
let widget = context.depend_on_inherited_widget_of_exact_type::<Directionality>(app);
widget.map(|widget| widget.text_direction)
}
}
impl InheritedWidget for Directionality {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn child(&self) -> &WidgetRef {
&self.child
}
fn update_should_notify(&self, old_widget: &Directionality) -> bool {
self.text_direction != old_widget.text_direction
}
}
#[derive(Debug)]
pub struct CompositedTransformTarget {
pub key: Option<KeyRef>,
pub link: Handle<LayerLink>,
pub child: Option<WidgetRef>,
}
impl CompositedTransformTarget {
pub fn new(link: Handle<LayerLink>) -> CompositedTransformTarget {
CompositedTransformTarget {
key: None,
link,
child: None,
}
}
pub fn key(mut self, key: KeyRef) -> CompositedTransformTarget {
self.key = Some(key);
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> CompositedTransformTarget {
self.child = Some(child.into_widget());
self
}
}
impl RenderObjectWidget for CompositedTransformTarget {
type RenderObject = RenderLeaderLayer;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderLeaderLayer::new(app, self.link, None).as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderLeaderLayer>,
) {
render_object.set_link(app, self.link);
}
}
impl SingleChildRenderObjectWidget for CompositedTransformTarget {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
#[derive(Debug)]
pub struct CompositedTransformFollower {
pub key: Option<KeyRef>,
pub link: Handle<LayerLink>,
pub show_when_unlinked: bool,
pub target_anchor: Alignment,
pub follower_anchor: Alignment,
pub offset: Offset,
pub child: Option<WidgetRef>,
}
impl CompositedTransformFollower {
pub fn new(link: Handle<LayerLink>) -> CompositedTransformFollower {
CompositedTransformFollower {
key: None,
link,
show_when_unlinked: true,
target_anchor: Alignment::TOP_LEFT,
follower_anchor: Alignment::TOP_LEFT,
offset: Offset::ZERO,
child: None,
}
}
pub fn key(mut self, key: KeyRef) -> CompositedTransformFollower {
self.key = Some(key);
self
}
pub fn show_when_unlinked(mut self, show_when_unlinked: bool) -> CompositedTransformFollower {
self.show_when_unlinked = show_when_unlinked;
self
}
pub fn target_anchor(mut self, target_anchor: Alignment) -> CompositedTransformFollower {
self.target_anchor = target_anchor;
self
}
pub fn follower_anchor(mut self, follower_anchor: Alignment) -> CompositedTransformFollower {
self.follower_anchor = follower_anchor;
self
}
pub fn offset(mut self, offset: Offset) -> CompositedTransformFollower {
self.offset = offset;
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> CompositedTransformFollower {
self.child = Some(child.into_widget());
self
}
}
impl RenderObjectWidget for CompositedTransformFollower {
type RenderObject = RenderFollowerLayer;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderFollowerLayer::new(
app,
self.link,
self.show_when_unlinked,
self.offset,
self.target_anchor,
self.follower_anchor,
None,
)
.as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderFollowerLayer>,
) {
render_object.set_link(app, self.link);
render_object.set_show_when_unlinked(app, self.show_when_unlinked);
render_object.set_offset(app, self.offset);
render_object.set_leader_anchor(app, self.target_anchor);
render_object.set_follower_anchor(app, self.follower_anchor);
}
}
impl SingleChildRenderObjectWidget for CompositedTransformFollower {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
#[derive(Debug)]
pub struct Opacity {
pub key: Option<KeyRef>,
pub opacity: f64,
pub child: Option<WidgetRef>,
}
impl Opacity {
pub fn new(opacity: f64) -> Opacity {
Opacity {
key: None,
opacity,
child: None,
}
}
pub fn key(mut self, key: KeyRef) -> Opacity {
self.key = Some(key);
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> Opacity {
self.child = Some(child.into_widget());
self
}
}
impl RenderObjectWidget for Opacity {
type RenderObject = RenderOpacity;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderOpacity::new(app, self.opacity, None).as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderOpacity>,
) {
render_object.set_opacity(app, self.opacity);
}
}
impl SingleChildRenderObjectWidget for Opacity {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
pub struct CustomPaint {
pub key: Option<KeyRef>,
pub painter: Option<Rc<dyn CustomPainter>>,
pub foreground_painter: Option<Rc<dyn CustomPainter>>,
pub size: Size,
pub is_complex: bool,
pub will_change: bool,
pub child: Option<WidgetRef>,
}
impl CustomPaint {
pub fn new() -> CustomPaint {
CustomPaint::default()
}
pub fn key(mut self, key: KeyRef) -> CustomPaint {
self.key = Some(key);
self
}
pub fn painter(mut self, painter: impl CustomPainter) -> CustomPaint {
self.painter = Some(Rc::new(painter));
self
}
pub fn foreground_painter(mut self, foreground_painter: impl CustomPainter) -> CustomPaint {
self.foreground_painter = Some(Rc::new(foreground_painter));
self
}
pub fn size(mut self, size: Size) -> CustomPaint {
self.size = size;
self
}
pub fn is_complex(mut self, is_complex: bool) -> CustomPaint {
self.is_complex = is_complex;
self
}
pub fn will_change(mut self, will_change: bool) -> CustomPaint {
self.will_change = will_change;
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> CustomPaint {
self.child = Some(child.into_widget());
self
}
fn debug_check_hints(&self) {
debug_assert!(
self.painter.is_some()
|| self.foreground_painter.is_some()
|| (!self.is_complex && !self.will_change)
);
}
}
impl Default for CustomPaint {
fn default() -> CustomPaint {
CustomPaint {
key: None,
painter: None,
foreground_painter: None,
size: Size::ZERO,
is_complex: false,
will_change: false,
child: None,
}
}
}
impl RenderObjectWidget for CustomPaint {
type RenderObject = RenderCustomPaint;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
self.debug_check_hints();
RenderCustomPaint::new(
app,
self.painter.clone(),
self.foreground_painter.clone(),
self.size,
self.is_complex,
self.will_change,
None,
)
.as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderCustomPaint>,
) {
self.debug_check_hints();
render_object.set_painter(app, self.painter.clone());
render_object.set_foreground_painter(app, self.foreground_painter.clone());
render_object.set_preferred_size(app, self.size);
render_object.set_is_complex(app, self.is_complex);
render_object.set_will_change(app, self.will_change);
}
fn did_unmount_render_object(
&self,
app: &mut App,
render_object: RenderHandle<RenderCustomPaint>,
) {
render_object.set_painter(app, None);
render_object.set_foreground_painter(app, None);
}
}
impl SingleChildRenderObjectWidget for CustomPaint {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
impl Debug for CustomPaint {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CustomPaint")
.field("key", &self.key)
.field("has_painter", &self.painter.is_some())
.field("has_foreground_painter", &self.foreground_painter.is_some())
.field("size", &self.size)
.field("is_complex", &self.is_complex)
.field("will_change", &self.will_change)
.field("child", &self.child)
.finish()
}
}
pub struct ClipRect {
pub key: Option<KeyRef>,
pub clipper: Option<Rc<dyn CustomClipper<Rect>>>,
pub clip_behavior: Clip,
pub child: Option<WidgetRef>,
}
impl ClipRect {
pub fn new() -> ClipRect {
ClipRect::default()
}
pub fn key(mut self, key: KeyRef) -> ClipRect {
self.key = Some(key);
self
}
pub fn clipper(mut self, clipper: impl CustomClipper<Rect>) -> ClipRect {
self.clipper = Some(Rc::new(clipper));
self
}
pub fn clip_behavior(mut self, clip_behavior: Clip) -> ClipRect {
self.clip_behavior = clip_behavior;
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> ClipRect {
self.child = Some(child.into_widget());
self
}
}
impl Default for ClipRect {
fn default() -> ClipRect {
ClipRect {
key: None,
clipper: None,
clip_behavior: Clip::HardEdge,
child: None,
}
}
}
impl RenderObjectWidget for ClipRect {
type RenderObject = RenderClipRect;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderClipRect::new(app, self.clipper.clone(), self.clip_behavior, None).as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderClipRect>,
) {
render_object.set_clipper(app, self.clipper.clone());
render_object.set_clip_behavior(app, self.clip_behavior);
}
fn did_unmount_render_object(
&self,
app: &mut App,
render_object: RenderHandle<RenderClipRect>,
) {
render_object.set_clipper(app, None);
}
}
impl SingleChildRenderObjectWidget for ClipRect {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
impl Debug for ClipRect {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ClipRect")
.field("key", &self.key)
.field("has_clipper", &self.clipper.is_some())
.field("clip_behavior", &self.clip_behavior)
.field("child", &self.child)
.finish()
}
}
pub struct ClipRRect {
pub key: Option<KeyRef>,
pub border_radius: BorderRadiusGeometry,
pub clipper: Option<Rc<dyn CustomClipper<RRect>>>,
pub clip_behavior: Clip,
pub child: Option<WidgetRef>,
}
impl ClipRRect {
pub fn new() -> ClipRRect {
ClipRRect::default()
}
pub fn key(mut self, key: KeyRef) -> ClipRRect {
self.key = Some(key);
self
}
pub fn border_radius(mut self, border_radius: BorderRadiusGeometry) -> ClipRRect {
self.border_radius = border_radius;
self
}
pub fn clipper(mut self, clipper: impl CustomClipper<RRect>) -> ClipRRect {
self.clipper = Some(Rc::new(clipper));
self
}
pub fn clip_behavior(mut self, clip_behavior: Clip) -> ClipRRect {
self.clip_behavior = clip_behavior;
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> ClipRRect {
self.child = Some(child.into_widget());
self
}
}
impl Default for ClipRRect {
fn default() -> ClipRRect {
ClipRRect {
key: None,
border_radius: BorderRadiusGeometry::ZERO,
clipper: None,
clip_behavior: Clip::AntiAlias,
child: None,
}
}
}
impl RenderObjectWidget for ClipRRect {
type RenderObject = RenderClipRRect;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
let text_direction = Directionality::maybe_of(app, context);
RenderClipRRect::new(
app,
self.border_radius,
self.clipper.clone(),
self.clip_behavior,
text_direction,
None,
)
.as_object()
}
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: RenderHandle<RenderClipRRect>,
) {
render_object.set_border_radius(app, self.border_radius);
render_object.set_clip_behavior(app, self.clip_behavior);
render_object.set_clipper(app, self.clipper.clone());
let text_direction = Directionality::maybe_of(app, context);
render_object.set_text_direction(app, text_direction);
}
}
impl SingleChildRenderObjectWidget for ClipRRect {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
impl Debug for ClipRRect {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ClipRRect")
.field("key", &self.key)
.field("border_radius", &self.border_radius)
.field("has_clipper", &self.clipper.is_some())
.field("clip_behavior", &self.clip_behavior)
.field("child", &self.child)
.finish()
}
}
pub struct ClipRSuperellipse {
pub key: Option<KeyRef>,
pub border_radius: BorderRadiusGeometry,
pub clipper: Option<Rc<dyn CustomClipper<RSuperellipse>>>,
pub clip_behavior: Clip,
pub child: Option<WidgetRef>,
}
impl ClipRSuperellipse {
pub fn new() -> ClipRSuperellipse {
ClipRSuperellipse::default()
}
pub fn key(mut self, key: KeyRef) -> ClipRSuperellipse {
self.key = Some(key);
self
}
pub fn border_radius(mut self, border_radius: BorderRadiusGeometry) -> ClipRSuperellipse {
self.border_radius = border_radius;
self
}
pub fn clipper(mut self, clipper: impl CustomClipper<RSuperellipse>) -> ClipRSuperellipse {
self.clipper = Some(Rc::new(clipper));
self
}
pub fn clip_behavior(mut self, clip_behavior: Clip) -> ClipRSuperellipse {
self.clip_behavior = clip_behavior;
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> ClipRSuperellipse {
self.child = Some(child.into_widget());
self
}
}
impl Default for ClipRSuperellipse {
fn default() -> ClipRSuperellipse {
ClipRSuperellipse {
key: None,
border_radius: BorderRadiusGeometry::ZERO,
clipper: None,
clip_behavior: Clip::AntiAlias,
child: None,
}
}
}
impl RenderObjectWidget for ClipRSuperellipse {
type RenderObject = RenderClipRSuperellipse;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
let text_direction = Directionality::maybe_of(app, context);
RenderClipRSuperellipse::new(
app,
self.border_radius,
self.clipper.clone(),
self.clip_behavior,
text_direction,
None,
)
.as_object()
}
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: RenderHandle<RenderClipRSuperellipse>,
) {
render_object.set_border_radius(app, self.border_radius);
render_object.set_clip_behavior(app, self.clip_behavior);
render_object.set_clipper(app, self.clipper.clone());
let text_direction = Directionality::maybe_of(app, context);
render_object.set_text_direction(app, text_direction);
}
}
impl SingleChildRenderObjectWidget for ClipRSuperellipse {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
impl Debug for ClipRSuperellipse {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ClipRSuperellipse")
.field("key", &self.key)
.field("border_radius", &self.border_radius)
.field("has_clipper", &self.clipper.is_some())
.field("clip_behavior", &self.clip_behavior)
.field("child", &self.child)
.finish()
}
}
pub struct ClipOval {
pub key: Option<KeyRef>,
pub clipper: Option<Rc<dyn CustomClipper<Rect>>>,
pub clip_behavior: Clip,
pub child: Option<WidgetRef>,
}
impl ClipOval {
pub fn new() -> ClipOval {
ClipOval::default()
}
pub fn key(mut self, key: KeyRef) -> ClipOval {
self.key = Some(key);
self
}
pub fn clipper(mut self, clipper: Rc<dyn CustomClipper<Rect>>) -> ClipOval {
self.clipper = Some(clipper);
self
}
pub fn clip_behavior(mut self, clip_behavior: Clip) -> ClipOval {
self.clip_behavior = clip_behavior;
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> ClipOval {
self.child = Some(child.into_widget());
self
}
}
impl Default for ClipOval {
fn default() -> ClipOval {
ClipOval {
key: None,
clipper: None,
clip_behavior: Clip::AntiAlias,
child: None,
}
}
}
impl RenderObjectWidget for ClipOval {
type RenderObject = RenderClipOval;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderClipOval::new(app, self.clipper.clone(), self.clip_behavior, None).as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderClipOval>,
) {
render_object.set_clipper(app, self.clipper.clone());
render_object.set_clip_behavior(app, self.clip_behavior);
}
fn did_unmount_render_object(
&self,
app: &mut App,
render_object: RenderHandle<RenderClipOval>,
) {
render_object.set_clipper(app, None);
}
}
impl SingleChildRenderObjectWidget for ClipOval {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
impl Debug for ClipOval {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ClipOval")
.field("key", &self.key)
.field("clipper", &self.clipper.as_ref().map(|_| "..."))
.field("clip_behavior", &self.clip_behavior)
.field("child", &self.child)
.finish()
}
}
pub struct ClipPath {
pub key: Option<KeyRef>,
pub clipper: Option<Rc<dyn CustomClipper<Arc<Path>>>>,
pub clip_behavior: Clip,
pub child: Option<WidgetRef>,
}
impl ClipPath {
pub fn new() -> ClipPath {
ClipPath::default()
}
pub fn shape(shape: Box<dyn ShapeBorder>) -> ClipPathShape {
ClipPathShape {
key: None,
shape,
clip_behavior: Clip::AntiAlias,
child: None,
}
}
pub fn key(mut self, key: KeyRef) -> ClipPath {
self.key = Some(key);
self
}
pub fn clipper(mut self, clipper: Rc<dyn CustomClipper<Arc<Path>>>) -> ClipPath {
self.clipper = Some(clipper);
self
}
pub fn clip_behavior(mut self, clip_behavior: Clip) -> ClipPath {
self.clip_behavior = clip_behavior;
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> ClipPath {
self.child = Some(child.into_widget());
self
}
}
impl Default for ClipPath {
fn default() -> ClipPath {
ClipPath {
key: None,
clipper: None,
clip_behavior: Clip::AntiAlias,
child: None,
}
}
}
impl RenderObjectWidget for ClipPath {
type RenderObject = RenderClipPath;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderClipPath::new(app, self.clipper.clone(), self.clip_behavior, None).as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderClipPath>,
) {
render_object.set_clipper(app, self.clipper.clone());
render_object.set_clip_behavior(app, self.clip_behavior);
}
fn did_unmount_render_object(
&self,
app: &mut App,
render_object: RenderHandle<RenderClipPath>,
) {
render_object.set_clipper(app, None);
}
}
impl SingleChildRenderObjectWidget for ClipPath {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
impl Debug for ClipPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ClipPath")
.field("key", &self.key)
.field("clipper", &self.clipper.as_ref().map(|_| "..."))
.field("clip_behavior", &self.clip_behavior)
.field("child", &self.child)
.finish()
}
}
pub struct ClipPathShape {
key: Option<KeyRef>,
shape: Box<dyn ShapeBorder>,
clip_behavior: Clip,
child: Option<WidgetRef>,
}
impl ClipPathShape {
pub fn key(mut self, key: KeyRef) -> ClipPathShape {
self.key = Some(key);
self
}
pub fn clip_behavior(mut self, clip_behavior: Clip) -> ClipPathShape {
self.clip_behavior = clip_behavior;
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> ClipPathShape {
self.child = Some(child.into_widget());
self
}
}
pub struct ClipPathShapeKind;
impl IntoWidget<ClipPathShapeKind> for ClipPathShape {
fn into_widget(self) -> WidgetRef {
let ClipPathShape {
key,
shape,
clip_behavior,
child,
} = self;
let builder = Builder::new(move |app, context| {
let text_direction = Directionality::maybe_of(app, context);
ClipPath {
key: None,
clipper: Some(Rc::new(ShapeBorderClipper::new(
shape.clone_box(),
text_direction,
))),
clip_behavior,
child: child.clone(),
}
.into_widget()
});
match key {
Some(key) => builder.key(key).into_widget(),
None => builder.into_widget(),
}
}
}
#[derive(Debug)]
pub struct Transform {
pub key: Option<KeyRef>,
pub transform: Matrix4,
pub origin: Option<Offset>,
pub alignment: Option<AlignmentGeometry>,
pub transform_hit_tests: bool,
pub child: Option<WidgetRef>,
}
impl Transform {
pub fn new(transform: Matrix4) -> Transform {
Transform {
key: None,
transform,
origin: None,
alignment: None,
transform_hit_tests: true,
child: None,
}
}
pub fn rotate(angle: f64) -> Transform {
Transform::new(Transform::compute_rotation(angle)).alignment(AlignmentGeometry::CENTER)
}
pub fn translate(offset: Offset) -> Transform {
Transform::new(Matrix4::translation(offset.dx() as f32, offset.dy() as f32))
}
pub fn scale(scale: Option<f64>, scale_x: Option<f64>, scale_y: Option<f64>) -> Transform {
debug_assert!(
!(scale.is_none() && scale_x.is_none() && scale_y.is_none()),
"At least one of 'scale', 'scale_x' and 'scale_y' is required to be non-None"
);
debug_assert!(
scale.is_none() || (scale_x.is_none() && scale_y.is_none()),
"If 'scale' is non-None then 'scale_x' and 'scale_y' must be left None"
);
let transform = Matrix4::scale(
scale.or(scale_x).unwrap_or(1.0) as f32,
scale.or(scale_y).unwrap_or(1.0) as f32,
);
Transform::new(transform).alignment(AlignmentGeometry::CENTER)
}
pub fn flip(flip_x: bool, flip_y: bool) -> Transform {
let transform = Matrix4::scale(
if flip_x { -1.0 } else { 1.0 },
if flip_y { -1.0 } else { 1.0 },
);
Transform::new(transform).alignment(AlignmentGeometry::CENTER)
}
pub fn key(mut self, key: KeyRef) -> Transform {
self.key = Some(key);
self
}
pub fn origin(mut self, origin: Offset) -> Transform {
self.origin = Some(origin);
self
}
pub fn alignment(mut self, alignment: AlignmentGeometry) -> Transform {
self.alignment = Some(alignment);
self
}
pub fn transform_hit_tests(mut self, transform_hit_tests: bool) -> Transform {
self.transform_hit_tests = transform_hit_tests;
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> Transform {
self.child = Some(child.into_widget());
self
}
fn compute_rotation(radians: f64) -> Matrix4 {
debug_assert!(
radians.is_finite(),
"Cannot compute the rotation matrix for a non-finite angle: {radians}"
);
if radians == 0.0 {
return Matrix4::IDENTITY;
}
let sin = radians.sin();
if sin == 1.0 {
return Transform::create_z_rotation(1.0, 0.0);
}
if sin == -1.0 {
return Transform::create_z_rotation(-1.0, 0.0);
}
let cos = radians.cos();
if cos == -1.0 {
return Transform::create_z_rotation(0.0, -1.0);
}
Transform::create_z_rotation(sin, cos)
}
fn create_z_rotation(sin: f64, cos: f64) -> Matrix4 {
let (sin, cos) = (sin as f32, cos as f32);
#[rustfmt::skip]
let rotation = Matrix4::from_flutter_array(&[
cos, sin, 0.0, 0.0,
-sin, cos, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
]);
rotation
}
}
impl RenderObjectWidget for Transform {
type RenderObject = RenderTransform;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
let text_direction = Directionality::maybe_of(app, context);
RenderTransform::new(
app,
self.transform,
self.origin,
self.alignment,
text_direction,
self.transform_hit_tests,
None,
)
.as_object()
}
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: RenderHandle<RenderTransform>,
) {
render_object.set_transform(app, self.transform);
render_object.set_origin(app, self.origin);
render_object.set_alignment(app, self.alignment);
let text_direction = Directionality::maybe_of(app, context);
render_object.set_text_direction(app, text_direction);
render_object.set_transform_hit_tests(app, self.transform_hit_tests);
}
}
impl SingleChildRenderObjectWidget for Transform {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
#[derive(Debug)]
pub struct FittedBox {
pub key: Option<KeyRef>,
pub fit: BoxFit,
pub alignment: AlignmentGeometry,
pub clip_behavior: Clip,
pub child: Option<WidgetRef>,
}
impl FittedBox {
pub fn new() -> FittedBox {
FittedBox::default()
}
pub fn key(mut self, key: KeyRef) -> FittedBox {
self.key = Some(key);
self
}
pub fn fit(mut self, fit: BoxFit) -> FittedBox {
self.fit = fit;
self
}
pub fn alignment(mut self, alignment: AlignmentGeometry) -> FittedBox {
self.alignment = alignment;
self
}
pub fn clip_behavior(mut self, clip_behavior: Clip) -> FittedBox {
self.clip_behavior = clip_behavior;
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> FittedBox {
self.child = Some(child.into_widget());
self
}
}
impl Default for FittedBox {
fn default() -> FittedBox {
FittedBox {
key: None,
fit: BoxFit::Contain,
alignment: AlignmentGeometry::CENTER,
clip_behavior: Clip::None,
child: None,
}
}
}
impl RenderObjectWidget for FittedBox {
type RenderObject = RenderFittedBox;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
let text_direction = Directionality::maybe_of(app, context);
let render_object =
RenderFittedBox::new(app, self.fit, self.alignment, text_direction, None);
render_object.set_clip_behavior(app, self.clip_behavior);
render_object.as_object()
}
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: RenderHandle<RenderFittedBox>,
) {
render_object.set_fit(app, self.fit);
render_object.set_alignment(app, self.alignment);
let text_direction = Directionality::maybe_of(app, context);
render_object.set_text_direction(app, text_direction);
render_object.set_clip_behavior(app, self.clip_behavior);
}
}
impl SingleChildRenderObjectWidget for FittedBox {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
#[derive(Debug)]
pub struct FractionalTranslation {
pub key: Option<KeyRef>,
pub translation: Offset,
pub transform_hit_tests: bool,
pub child: Option<WidgetRef>,
}
impl FractionalTranslation {
pub fn new(translation: Offset) -> FractionalTranslation {
FractionalTranslation {
key: None,
translation,
transform_hit_tests: true,
child: None,
}
}
pub fn key(mut self, key: KeyRef) -> FractionalTranslation {
self.key = Some(key);
self
}
pub fn transform_hit_tests(mut self, transform_hit_tests: bool) -> FractionalTranslation {
self.transform_hit_tests = transform_hit_tests;
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> FractionalTranslation {
self.child = Some(child.into_widget());
self
}
}
impl RenderObjectWidget for FractionalTranslation {
type RenderObject = RenderFractionalTranslation;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderFractionalTranslation::new(app, self.translation, self.transform_hit_tests, None)
.as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderFractionalTranslation>,
) {
render_object.set_translation(app, self.translation);
render_object.set_transform_hit_tests(app, self.transform_hit_tests);
}
}
impl SingleChildRenderObjectWidget for FractionalTranslation {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
#[derive(Debug)]
pub struct Padding {
pub key: Option<KeyRef>,
pub padding: EdgeInsetsGeometry,
pub child: Option<WidgetRef>,
}
impl Padding {
pub fn new(padding: EdgeInsetsGeometry) -> Padding {
Padding {
key: None,
padding,
child: None,
}
}
pub fn key(mut self, key: KeyRef) -> Padding {
self.key = Some(key);
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> Padding {
self.child = Some(child.into_widget());
self
}
}
impl RenderObjectWidget for Padding {
type RenderObject = RenderPadding;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
let text_direction = Directionality::maybe_of(app, context);
RenderPadding::new(app, self.padding, text_direction, None).as_object()
}
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: RenderHandle<RenderPadding>,
) {
render_object.set_padding(app, self.padding);
let text_direction = Directionality::maybe_of(app, context);
render_object.set_text_direction(app, text_direction);
}
}
impl SingleChildRenderObjectWidget for Padding {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
#[derive(Debug)]
pub struct Align {
pub key: Option<KeyRef>,
pub alignment: AlignmentGeometry,
pub width_factor: Option<f64>,
pub height_factor: Option<f64>,
pub child: Option<WidgetRef>,
}
impl Align {
pub fn new() -> Align {
Align::default()
}
pub fn key(mut self, key: KeyRef) -> Align {
self.key = Some(key);
self
}
pub fn alignment(mut self, alignment: AlignmentGeometry) -> Align {
self.alignment = alignment;
self
}
pub fn width_factor(mut self, width_factor: f64) -> Align {
self.width_factor = Some(width_factor);
self
}
pub fn height_factor(mut self, height_factor: f64) -> Align {
self.height_factor = Some(height_factor);
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> Align {
self.child = Some(child.into_widget());
self
}
fn create_positioned_box(
app: &mut App,
context: BuildContext,
alignment: AlignmentGeometry,
width_factor: Option<f64>,
height_factor: Option<f64>,
) -> AnyRenderObject {
let text_direction = Directionality::maybe_of(app, context);
RenderPositionedBox::new(
app,
alignment,
text_direction,
width_factor,
height_factor,
None,
)
.as_object()
}
fn update_positioned_box(
app: &mut App,
context: BuildContext,
render_object: RenderHandle<RenderPositionedBox>,
alignment: AlignmentGeometry,
width_factor: Option<f64>,
height_factor: Option<f64>,
) {
render_object.set_alignment(app, alignment);
render_object.set_width_factor(app, width_factor);
render_object.set_height_factor(app, height_factor);
let text_direction = Directionality::maybe_of(app, context);
render_object.set_text_direction(app, text_direction);
}
}
impl Default for Align {
fn default() -> Align {
Align {
key: None,
alignment: AlignmentGeometry::CENTER,
width_factor: None,
height_factor: None,
child: None,
}
}
}
impl RenderObjectWidget for Align {
type RenderObject = RenderPositionedBox;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
Align::create_positioned_box(
app,
context,
self.alignment,
self.width_factor,
self.height_factor,
)
}
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: RenderHandle<RenderPositionedBox>,
) {
Align::update_positioned_box(
app,
context,
render_object,
self.alignment,
self.width_factor,
self.height_factor,
);
}
}
impl SingleChildRenderObjectWidget for Align {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
#[derive(Debug, Default)]
pub struct Center {
pub key: Option<KeyRef>,
pub width_factor: Option<f64>,
pub height_factor: Option<f64>,
pub child: Option<WidgetRef>,
}
impl Center {
pub fn new() -> Center {
Center::default()
}
pub fn key(mut self, key: KeyRef) -> Center {
self.key = Some(key);
self
}
pub fn width_factor(mut self, width_factor: f64) -> Center {
self.width_factor = Some(width_factor);
self
}
pub fn height_factor(mut self, height_factor: f64) -> Center {
self.height_factor = Some(height_factor);
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> Center {
self.child = Some(child.into_widget());
self
}
pub fn alignment(&self) -> AlignmentGeometry {
AlignmentGeometry::CENTER
}
}
impl RenderObjectWidget for Center {
type RenderObject = RenderPositionedBox;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
Align::create_positioned_box(
app,
context,
self.alignment(),
self.width_factor,
self.height_factor,
)
}
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: RenderHandle<RenderPositionedBox>,
) {
Align::update_positioned_box(
app,
context,
render_object,
self.alignment(),
self.width_factor,
self.height_factor,
);
}
}
impl SingleChildRenderObjectWidget for Center {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
#[derive(Debug)]
pub struct CustomSingleChildLayout {
pub key: Option<KeyRef>,
pub delegate: Rc<dyn SingleChildLayoutDelegate>,
pub child: Option<WidgetRef>,
}
impl CustomSingleChildLayout {
pub fn new(delegate: Rc<dyn SingleChildLayoutDelegate>) -> CustomSingleChildLayout {
CustomSingleChildLayout {
key: None,
delegate,
child: None,
}
}
pub fn key(mut self, key: KeyRef) -> CustomSingleChildLayout {
self.key = Some(key);
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> CustomSingleChildLayout {
self.child = Some(child.into_widget());
self
}
}
impl RenderObjectWidget for CustomSingleChildLayout {
type RenderObject = RenderCustomSingleChildLayoutBox;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderCustomSingleChildLayoutBox::new(app, Rc::clone(&self.delegate), None).as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderCustomSingleChildLayoutBox>,
) {
render_object.set_delegate(app, Rc::clone(&self.delegate));
}
}
impl SingleChildRenderObjectWidget for CustomSingleChildLayout {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
#[derive(Debug)]
pub struct LayoutId {
pub key: Option<KeyRef>,
pub id: ChildLayoutId,
pub child: WidgetRef,
}
impl LayoutId {
pub fn new<K>(id: ChildLayoutId, child: impl IntoWidget<K>) -> LayoutId {
LayoutId {
key: None,
id,
child: child.into_widget(),
}
}
pub fn key(mut self, key: KeyRef) -> LayoutId {
self.key = Some(key);
self
}
}
impl ParentDataWidget for LayoutId {
type ParentData = MultiChildLayoutParentData;
fn key(&self) -> Option<&KeyRef> {
Some(self.key.as_ref().unwrap_or(&self.id))
}
fn child(&self) -> &WidgetRef {
&self.child
}
fn apply_parent_data(&self, app: &mut App, render_object: AnyRenderObject) {
debug_assert!(render_object.parent_data_is::<MultiChildLayoutParentData>(app));
let parent_data = render_object.parent_data_of_mut::<MultiChildLayoutParentData>(app);
if parent_data.id.as_ref() != Some(&self.id) {
parent_data.id = Some(Rc::clone(&self.id));
if let Some(parent) = render_object.parent(app) {
parent.mark_needs_layout(app);
}
}
}
}
#[derive(Debug)]
pub struct CustomMultiChildLayout {
pub key: Option<KeyRef>,
pub delegate: Rc<dyn MultiChildLayoutDelegate>,
pub children: Vec<WidgetRef>,
}
impl CustomMultiChildLayout {
pub fn new(delegate: Rc<dyn MultiChildLayoutDelegate>) -> CustomMultiChildLayout {
CustomMultiChildLayout {
key: None,
delegate,
children: Vec::new(),
}
}
pub fn key(mut self, key: KeyRef) -> CustomMultiChildLayout {
self.key = Some(key);
self
}
pub fn children(
mut self,
children: impl IntoIterator<Item = WidgetRef>,
) -> CustomMultiChildLayout {
self.children = children.into_iter().collect();
self
}
}
impl RenderObjectWidget for CustomMultiChildLayout {
type RenderObject = RenderCustomMultiChildLayoutBox;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderCustomMultiChildLayoutBox::new(app, Rc::clone(&self.delegate)).as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderCustomMultiChildLayoutBox>,
) {
render_object.set_delegate(app, Rc::clone(&self.delegate));
}
}
impl MultiChildRenderObjectWidget for CustomMultiChildLayout {
fn children(&self) -> &[WidgetRef] {
&self.children
}
}
#[derive(Default)]
pub struct SizedBox {
pub key: Option<KeyRef>,
pub width: Option<f64>,
pub height: Option<f64>,
pub child: Option<WidgetRef>,
}
impl SizedBox {
pub fn new() -> SizedBox {
SizedBox::default()
}
pub fn key(mut self, key: KeyRef) -> SizedBox {
self.key = Some(key);
self
}
pub fn width(mut self, width: f64) -> SizedBox {
self.width = Some(width);
self
}
pub fn height(mut self, height: f64) -> SizedBox {
self.height = Some(height);
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> SizedBox {
self.child = Some(child.into_widget());
self
}
pub fn expand() -> SizedBox {
SizedBox {
key: None,
width: Some(f64::INFINITY),
height: Some(f64::INFINITY),
child: None,
}
}
pub fn shrink() -> SizedBox {
SizedBox {
key: None,
width: Some(0.0),
height: Some(0.0),
child: None,
}
}
pub fn from_size(size: Option<Size>) -> SizedBox {
SizedBox {
key: None,
width: size.map(|size| size.width()),
height: size.map(|size| size.height()),
child: None,
}
}
pub fn square(dimension: Option<f64>) -> SizedBox {
SizedBox {
key: None,
width: dimension,
height: dimension,
child: None,
}
}
fn additional_constraints(&self) -> BoxConstraints {
BoxConstraints::tight_for(self.width, self.height)
}
fn short_name(&self) -> &'static str {
match (self.width, self.height) {
(Some(f64::INFINITY), Some(f64::INFINITY)) => "SizedBox.expand",
(Some(0.0), Some(0.0)) => "SizedBox.shrink",
_ => "SizedBox",
}
}
}
impl RenderObjectWidget for SizedBox {
type RenderObject = RenderConstrainedBox;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderConstrainedBox::new(app, self.additional_constraints(), None).as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderConstrainedBox>,
) {
render_object.set_additional_constraints(app, self.additional_constraints());
}
}
impl SingleChildRenderObjectWidget for SizedBox {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
impl Debug for SizedBox {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut debug = f.debug_struct(self.short_name());
debug.field("key", &self.key);
if self.short_name() == "SizedBox" {
debug.field("width", &self.width);
debug.field("height", &self.height);
}
debug.field("child", &self.child).finish()
}
}
#[derive(Debug)]
pub struct ConstrainedBox {
pub key: Option<KeyRef>,
pub constraints: BoxConstraints,
pub child: Option<WidgetRef>,
}
impl ConstrainedBox {
pub fn new(constraints: BoxConstraints) -> ConstrainedBox {
ConstrainedBox {
key: None,
constraints,
child: None,
}
}
pub fn key(mut self, key: KeyRef) -> ConstrainedBox {
self.key = Some(key);
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> ConstrainedBox {
self.child = Some(child.into_widget());
self
}
}
impl RenderObjectWidget for ConstrainedBox {
type RenderObject = RenderConstrainedBox;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderConstrainedBox::new(app, self.constraints, None).as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderConstrainedBox>,
) {
render_object.set_additional_constraints(app, self.constraints);
}
}
impl SingleChildRenderObjectWidget for ConstrainedBox {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
#[derive(Debug)]
pub struct ConstraintsTransformBox {
pub key: Option<KeyRef>,
pub text_direction: Option<TextDirection>,
pub alignment: AlignmentGeometry,
pub constraints_transform: BoxConstraintsTransform,
pub clip_behavior: Clip,
pub child: Option<WidgetRef>,
}
impl ConstraintsTransformBox {
pub fn new(constraints_transform: BoxConstraintsTransform) -> ConstraintsTransformBox {
ConstraintsTransformBox {
key: None,
text_direction: None,
alignment: AlignmentGeometry::CENTER,
constraints_transform,
clip_behavior: Clip::None,
child: None,
}
}
pub fn key(mut self, key: KeyRef) -> ConstraintsTransformBox {
self.key = Some(key);
self
}
pub fn text_direction(mut self, text_direction: TextDirection) -> ConstraintsTransformBox {
self.text_direction = Some(text_direction);
self
}
pub fn alignment(mut self, alignment: AlignmentGeometry) -> ConstraintsTransformBox {
self.alignment = alignment;
self
}
pub fn clip_behavior(mut self, clip_behavior: Clip) -> ConstraintsTransformBox {
self.clip_behavior = clip_behavior;
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> ConstraintsTransformBox {
self.child = Some(child.into_widget());
self
}
pub fn unmodified(constraints: BoxConstraints) -> BoxConstraints {
constraints
}
pub fn unconstrained(_constraints: BoxConstraints) -> BoxConstraints {
BoxConstraints::new()
}
pub fn width_unconstrained(constraints: BoxConstraints) -> BoxConstraints {
constraints.height_constraints()
}
pub fn height_unconstrained(constraints: BoxConstraints) -> BoxConstraints {
constraints.width_constraints()
}
pub fn max_height_unconstrained(constraints: BoxConstraints) -> BoxConstraints {
constraints.copy_with().max_height(f64::INFINITY)
}
pub fn max_width_unconstrained(constraints: BoxConstraints) -> BoxConstraints {
constraints.copy_with().max_width(f64::INFINITY)
}
pub fn max_unconstrained(constraints: BoxConstraints) -> BoxConstraints {
constraints
.copy_with()
.max_width(f64::INFINITY)
.max_height(f64::INFINITY)
}
}
impl RenderObjectWidget for ConstraintsTransformBox {
type RenderObject = RenderConstraintsTransformBox;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
let text_direction = self
.text_direction
.or_else(|| Directionality::maybe_of(app, context));
let render_object = RenderConstraintsTransformBox::new(
app,
self.alignment,
text_direction,
self.constraints_transform,
None,
);
render_object.set_clip_behavior(app, self.clip_behavior);
render_object.as_object()
}
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: RenderHandle<RenderConstraintsTransformBox>,
) {
let text_direction = self
.text_direction
.or_else(|| Directionality::maybe_of(app, context));
render_object.set_text_direction(app, text_direction);
render_object.set_constraints_transform(app, self.constraints_transform);
render_object.set_alignment(app, self.alignment);
render_object.set_clip_behavior(app, self.clip_behavior);
}
}
impl SingleChildRenderObjectWidget for ConstraintsTransformBox {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
#[derive(Debug)]
pub struct UnconstrainedBox {
pub key: Option<KeyRef>,
pub text_direction: Option<TextDirection>,
pub alignment: AlignmentGeometry,
pub constrained_axis: Option<Axis>,
pub clip_behavior: Clip,
pub child: Option<WidgetRef>,
}
impl UnconstrainedBox {
pub fn new() -> UnconstrainedBox {
UnconstrainedBox::default()
}
pub fn key(mut self, key: KeyRef) -> UnconstrainedBox {
self.key = Some(key);
self
}
pub fn text_direction(mut self, text_direction: TextDirection) -> UnconstrainedBox {
self.text_direction = Some(text_direction);
self
}
pub fn alignment(mut self, alignment: AlignmentGeometry) -> UnconstrainedBox {
self.alignment = alignment;
self
}
pub fn constrained_axis(mut self, constrained_axis: Axis) -> UnconstrainedBox {
self.constrained_axis = Some(constrained_axis);
self
}
pub fn clip_behavior(mut self, clip_behavior: Clip) -> UnconstrainedBox {
self.clip_behavior = clip_behavior;
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> UnconstrainedBox {
self.child = Some(child.into_widget());
self
}
fn axis_to_transform(constrained_axis: Option<Axis>) -> BoxConstraintsTransform {
match constrained_axis {
Some(Axis::Horizontal) => ConstraintsTransformBox::height_unconstrained,
Some(Axis::Vertical) => ConstraintsTransformBox::width_unconstrained,
None => ConstraintsTransformBox::unconstrained,
}
}
}
impl Default for UnconstrainedBox {
fn default() -> UnconstrainedBox {
UnconstrainedBox {
key: None,
text_direction: None,
alignment: AlignmentGeometry::CENTER,
constrained_axis: None,
clip_behavior: Clip::None,
child: None,
}
}
}
impl StatelessWidget for UnconstrainedBox {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn build(&self, _app: &mut App, _context: BuildContext) -> WidgetRef {
let mut transform_box = ConstraintsTransformBox::new(UnconstrainedBox::axis_to_transform(
self.constrained_axis,
))
.alignment(self.alignment)
.clip_behavior(self.clip_behavior);
if let Some(text_direction) = self.text_direction {
transform_box = transform_box.text_direction(text_direction);
}
if let Some(child) = &self.child {
transform_box = transform_box.child(child.clone());
}
transform_box.into_widget()
}
}
#[derive(Debug)]
pub struct FractionallySizedBox {
pub key: Option<KeyRef>,
pub alignment: AlignmentGeometry,
pub width_factor: Option<f64>,
pub height_factor: Option<f64>,
pub child: Option<WidgetRef>,
}
impl FractionallySizedBox {
pub fn new() -> FractionallySizedBox {
FractionallySizedBox::default()
}
pub fn key(mut self, key: KeyRef) -> FractionallySizedBox {
self.key = Some(key);
self
}
pub fn alignment(mut self, alignment: AlignmentGeometry) -> FractionallySizedBox {
self.alignment = alignment;
self
}
pub fn width_factor(mut self, width_factor: f64) -> FractionallySizedBox {
debug_assert!(width_factor >= 0.0);
self.width_factor = Some(width_factor);
self
}
pub fn height_factor(mut self, height_factor: f64) -> FractionallySizedBox {
debug_assert!(height_factor >= 0.0);
self.height_factor = Some(height_factor);
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> FractionallySizedBox {
self.child = Some(child.into_widget());
self
}
}
impl Default for FractionallySizedBox {
fn default() -> FractionallySizedBox {
FractionallySizedBox {
key: None,
alignment: AlignmentGeometry::CENTER,
width_factor: None,
height_factor: None,
child: None,
}
}
}
impl RenderObjectWidget for FractionallySizedBox {
type RenderObject = RenderFractionallySizedOverflowBox;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
let text_direction = Directionality::maybe_of(app, context);
let render_object =
RenderFractionallySizedOverflowBox::new(app, self.alignment, text_direction, None);
render_object.set_width_factor(app, self.width_factor);
render_object.set_height_factor(app, self.height_factor);
render_object.as_object()
}
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: RenderHandle<RenderFractionallySizedOverflowBox>,
) {
render_object.set_alignment(app, self.alignment);
render_object.set_width_factor(app, self.width_factor);
render_object.set_height_factor(app, self.height_factor);
let text_direction = Directionality::maybe_of(app, context);
render_object.set_text_direction(app, text_direction);
}
}
impl SingleChildRenderObjectWidget for FractionallySizedBox {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
#[derive(Debug)]
pub struct LimitedBox {
pub key: Option<KeyRef>,
pub max_width: f64,
pub max_height: f64,
pub child: Option<WidgetRef>,
}
impl LimitedBox {
pub fn new() -> LimitedBox {
LimitedBox::default()
}
pub fn key(mut self, key: KeyRef) -> LimitedBox {
self.key = Some(key);
self
}
pub fn max_width(mut self, max_width: f64) -> LimitedBox {
debug_assert!(max_width >= 0.0);
self.max_width = max_width;
self
}
pub fn max_height(mut self, max_height: f64) -> LimitedBox {
debug_assert!(max_height >= 0.0);
self.max_height = max_height;
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> LimitedBox {
self.child = Some(child.into_widget());
self
}
}
impl Default for LimitedBox {
fn default() -> LimitedBox {
LimitedBox {
key: None,
max_width: f64::INFINITY,
max_height: f64::INFINITY,
child: None,
}
}
}
impl RenderObjectWidget for LimitedBox {
type RenderObject = RenderLimitedBox;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderLimitedBox::new(app, self.max_width, self.max_height, None).as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderLimitedBox>,
) {
render_object.set_max_width(app, self.max_width);
render_object.set_max_height(app, self.max_height);
}
}
impl SingleChildRenderObjectWidget for LimitedBox {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
#[derive(Debug)]
pub struct OverflowBox {
pub key: Option<KeyRef>,
pub alignment: AlignmentGeometry,
pub min_width: Option<f64>,
pub max_width: Option<f64>,
pub min_height: Option<f64>,
pub max_height: Option<f64>,
pub fit: OverflowBoxFit,
pub child: Option<WidgetRef>,
}
impl OverflowBox {
pub fn new() -> OverflowBox {
OverflowBox::default()
}
pub fn key(mut self, key: KeyRef) -> OverflowBox {
self.key = Some(key);
self
}
pub fn alignment(mut self, alignment: AlignmentGeometry) -> OverflowBox {
self.alignment = alignment;
self
}
pub fn min_width(mut self, min_width: f64) -> OverflowBox {
self.min_width = Some(min_width);
self
}
pub fn max_width(mut self, max_width: f64) -> OverflowBox {
self.max_width = Some(max_width);
self
}
pub fn min_height(mut self, min_height: f64) -> OverflowBox {
self.min_height = Some(min_height);
self
}
pub fn max_height(mut self, max_height: f64) -> OverflowBox {
self.max_height = Some(max_height);
self
}
pub fn fit(mut self, fit: OverflowBoxFit) -> OverflowBox {
self.fit = fit;
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> OverflowBox {
self.child = Some(child.into_widget());
self
}
}
impl Default for OverflowBox {
fn default() -> OverflowBox {
OverflowBox {
key: None,
alignment: AlignmentGeometry::CENTER,
min_width: None,
max_width: None,
min_height: None,
max_height: None,
fit: OverflowBoxFit::Max,
child: None,
}
}
}
impl RenderObjectWidget for OverflowBox {
type RenderObject = RenderConstrainedOverflowBox;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
let text_direction = Directionality::maybe_of(app, context);
RenderConstrainedOverflowBox::new(
app,
self.min_width,
self.max_width,
self.min_height,
self.max_height,
self.fit,
self.alignment,
text_direction,
None,
)
.as_object()
}
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: RenderHandle<RenderConstrainedOverflowBox>,
) {
render_object.set_alignment(app, self.alignment);
render_object.set_min_width(app, self.min_width);
render_object.set_max_width(app, self.max_width);
render_object.set_min_height(app, self.min_height);
render_object.set_max_height(app, self.max_height);
render_object.set_fit(app, self.fit);
let text_direction = Directionality::maybe_of(app, context);
render_object.set_text_direction(app, text_direction);
}
}
impl SingleChildRenderObjectWidget for OverflowBox {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
#[derive(Debug)]
pub struct SizedOverflowBox {
pub key: Option<KeyRef>,
pub alignment: AlignmentGeometry,
pub size: Size,
pub child: Option<WidgetRef>,
}
impl SizedOverflowBox {
pub fn new(size: Size) -> SizedOverflowBox {
SizedOverflowBox {
key: None,
alignment: AlignmentGeometry::CENTER,
size,
child: None,
}
}
pub fn key(mut self, key: KeyRef) -> SizedOverflowBox {
self.key = Some(key);
self
}
pub fn alignment(mut self, alignment: AlignmentGeometry) -> SizedOverflowBox {
self.alignment = alignment;
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> SizedOverflowBox {
self.child = Some(child.into_widget());
self
}
}
impl RenderObjectWidget for SizedOverflowBox {
type RenderObject = RenderSizedOverflowBox;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
let text_direction = Directionality::of(app, context);
RenderSizedOverflowBox::new(app, self.size, self.alignment, Some(text_direction), None)
.as_object()
}
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: RenderHandle<RenderSizedOverflowBox>,
) {
render_object.set_alignment(app, self.alignment);
render_object.set_requested_size(app, self.size);
let text_direction = Directionality::of(app, context);
render_object.set_text_direction(app, Some(text_direction));
}
}
impl SingleChildRenderObjectWidget for SizedOverflowBox {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
#[derive(Debug)]
pub struct Offstage {
pub key: Option<KeyRef>,
pub offstage: bool,
pub child: Option<WidgetRef>,
}
impl Offstage {
pub fn new() -> Offstage {
Offstage::default()
}
pub fn key(mut self, key: KeyRef) -> Offstage {
self.key = Some(key);
self
}
pub fn offstage(mut self, offstage: bool) -> Offstage {
self.offstage = offstage;
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> Offstage {
self.child = Some(child.into_widget());
self
}
}
impl Default for Offstage {
fn default() -> Offstage {
Offstage {
key: None,
offstage: true,
child: None,
}
}
}
impl RenderObjectWidget for Offstage {
type RenderObject = RenderOffstage;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderOffstage::new(app, self.offstage, None).as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderOffstage>,
) {
render_object.set_offstage(app, self.offstage);
}
}
impl SingleChildRenderObjectWidget for Offstage {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
#[derive(Debug)]
pub struct AspectRatio {
pub key: Option<KeyRef>,
pub aspect_ratio: f64,
pub child: Option<WidgetRef>,
}
impl AspectRatio {
pub fn new(aspect_ratio: f64) -> AspectRatio {
debug_assert!(aspect_ratio > 0.0);
AspectRatio {
key: None,
aspect_ratio,
child: None,
}
}
pub fn key(mut self, key: KeyRef) -> AspectRatio {
self.key = Some(key);
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> AspectRatio {
self.child = Some(child.into_widget());
self
}
}
impl RenderObjectWidget for AspectRatio {
type RenderObject = RenderAspectRatio;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderAspectRatio::new(app, self.aspect_ratio, None).as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderAspectRatio>,
) {
render_object.set_aspect_ratio(app, self.aspect_ratio);
}
}
impl SingleChildRenderObjectWidget for AspectRatio {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
#[derive(Debug, Default)]
pub struct IntrinsicWidth {
pub key: Option<KeyRef>,
pub step_width: Option<f64>,
pub step_height: Option<f64>,
pub child: Option<WidgetRef>,
}
impl IntrinsicWidth {
pub fn new() -> IntrinsicWidth {
IntrinsicWidth::default()
}
pub fn key(mut self, key: KeyRef) -> IntrinsicWidth {
self.key = Some(key);
self
}
pub fn step_width(mut self, step_width: f64) -> IntrinsicWidth {
debug_assert!(step_width >= 0.0);
self.step_width = Some(step_width);
self
}
pub fn step_height(mut self, step_height: f64) -> IntrinsicWidth {
debug_assert!(step_height >= 0.0);
self.step_height = Some(step_height);
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> IntrinsicWidth {
self.child = Some(child.into_widget());
self
}
fn effective_step_width(&self) -> Option<f64> {
self.step_width.filter(|step| *step != 0.0)
}
fn effective_step_height(&self) -> Option<f64> {
self.step_height.filter(|step| *step != 0.0)
}
}
impl RenderObjectWidget for IntrinsicWidth {
type RenderObject = RenderIntrinsicWidth;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderIntrinsicWidth::new(
app,
self.effective_step_width(),
self.effective_step_height(),
None,
)
.as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderIntrinsicWidth>,
) {
render_object.set_step_width(app, self.effective_step_width());
render_object.set_step_height(app, self.effective_step_height());
}
}
impl SingleChildRenderObjectWidget for IntrinsicWidth {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
#[derive(Debug, Default)]
pub struct IntrinsicHeight {
pub key: Option<KeyRef>,
pub child: Option<WidgetRef>,
}
impl IntrinsicHeight {
pub fn new() -> IntrinsicHeight {
IntrinsicHeight::default()
}
pub fn key(mut self, key: KeyRef) -> IntrinsicHeight {
self.key = Some(key);
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> IntrinsicHeight {
self.child = Some(child.into_widget());
self
}
}
impl RenderObjectWidget for IntrinsicHeight {
type RenderObject = RenderIntrinsicHeight;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderIntrinsicHeight::new(app, None).as_object()
}
}
impl SingleChildRenderObjectWidget for IntrinsicHeight {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
#[derive(Debug)]
pub struct Baseline {
pub key: Option<KeyRef>,
pub baseline: f64,
pub baseline_type: TextBaseline,
pub child: Option<WidgetRef>,
}
impl Baseline {
pub fn new(baseline: f64, baseline_type: TextBaseline) -> Baseline {
Baseline {
key: None,
baseline,
baseline_type,
child: None,
}
}
pub fn key(mut self, key: KeyRef) -> Baseline {
self.key = Some(key);
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> Baseline {
self.child = Some(child.into_widget());
self
}
}
impl RenderObjectWidget for Baseline {
type RenderObject = RenderBaseline;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderBaseline::new(app, self.baseline, self.baseline_type, None).as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderBaseline>,
) {
render_object.set_baseline(app, self.baseline);
render_object.set_baseline_type(app, self.baseline_type);
}
}
impl SingleChildRenderObjectWidget for Baseline {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
#[derive(Debug, Default)]
pub struct SliverToBoxAdapter {
pub key: Option<KeyRef>,
pub child: Option<WidgetRef>,
}
impl SliverToBoxAdapter {
pub fn new() -> SliverToBoxAdapter {
SliverToBoxAdapter::default()
}
pub fn key(mut self, key: KeyRef) -> SliverToBoxAdapter {
self.key = Some(key);
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> SliverToBoxAdapter {
self.child = Some(child.into_widget());
self
}
}
impl RenderObjectWidget for SliverToBoxAdapter {
type RenderObject = RenderSliverToBoxAdapter;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderSliverToBoxAdapter::new(app, None).as_object()
}
}
impl SingleChildRenderObjectWidget for SliverToBoxAdapter {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
#[derive(Debug)]
pub struct SliverPadding {
pub key: Option<KeyRef>,
pub padding: EdgeInsetsGeometry,
pub sliver: Option<WidgetRef>,
}
impl SliverPadding {
pub fn new(padding: EdgeInsetsGeometry) -> SliverPadding {
SliverPadding {
key: None,
padding,
sliver: None,
}
}
pub fn key(mut self, key: KeyRef) -> SliverPadding {
self.key = Some(key);
self
}
pub fn sliver<K>(mut self, sliver: impl IntoWidget<K>) -> SliverPadding {
self.sliver = Some(sliver.into_widget());
self
}
}
impl RenderObjectWidget for SliverPadding {
type RenderObject = RenderSliverPadding;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
let text_direction = Directionality::of(app, context);
RenderSliverPadding::new(app, self.padding, Some(text_direction), None).as_object()
}
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: RenderHandle<RenderSliverPadding>,
) {
render_object.set_padding(app, self.padding);
let text_direction = Directionality::of(app, context);
render_object.set_text_direction(app, Some(text_direction));
}
}
impl SingleChildRenderObjectWidget for SliverPadding {
fn child(&self) -> Option<&WidgetRef> {
self.sliver.as_ref()
}
}
pub struct Listener {
pub key: Option<KeyRef>,
pub on_pointer_down: Option<PointerDownEventListener>,
pub on_pointer_move: Option<PointerMoveEventListener>,
pub on_pointer_up: Option<PointerUpEventListener>,
pub on_pointer_hover: Option<PointerHoverEventListener>,
pub on_pointer_cancel: Option<PointerCancelEventListener>,
pub on_pointer_pan_zoom_start: Option<PointerPanZoomStartEventListener>,
pub on_pointer_pan_zoom_update: Option<PointerPanZoomUpdateEventListener>,
pub on_pointer_pan_zoom_end: Option<PointerPanZoomEndEventListener>,
pub on_pointer_signal: Option<PointerSignalEventListener>,
pub behavior: HitTestBehavior,
pub child: Option<WidgetRef>,
}
impl Listener {
pub fn new() -> Listener {
Listener::default()
}
pub fn key(mut self, key: KeyRef) -> Listener {
self.key = Some(key);
self
}
pub fn on_pointer_down(mut self, on_pointer_down: PointerDownEventListener) -> Listener {
self.on_pointer_down = Some(on_pointer_down);
self
}
pub fn on_pointer_move(mut self, on_pointer_move: PointerMoveEventListener) -> Listener {
self.on_pointer_move = Some(on_pointer_move);
self
}
pub fn on_pointer_up(mut self, on_pointer_up: PointerUpEventListener) -> Listener {
self.on_pointer_up = Some(on_pointer_up);
self
}
pub fn on_pointer_hover(mut self, on_pointer_hover: PointerHoverEventListener) -> Listener {
self.on_pointer_hover = Some(on_pointer_hover);
self
}
pub fn on_pointer_cancel(mut self, on_pointer_cancel: PointerCancelEventListener) -> Listener {
self.on_pointer_cancel = Some(on_pointer_cancel);
self
}
pub fn on_pointer_pan_zoom_start(
mut self,
on_pointer_pan_zoom_start: PointerPanZoomStartEventListener,
) -> Listener {
self.on_pointer_pan_zoom_start = Some(on_pointer_pan_zoom_start);
self
}
pub fn on_pointer_pan_zoom_update(
mut self,
on_pointer_pan_zoom_update: PointerPanZoomUpdateEventListener,
) -> Listener {
self.on_pointer_pan_zoom_update = Some(on_pointer_pan_zoom_update);
self
}
pub fn on_pointer_pan_zoom_end(
mut self,
on_pointer_pan_zoom_end: PointerPanZoomEndEventListener,
) -> Listener {
self.on_pointer_pan_zoom_end = Some(on_pointer_pan_zoom_end);
self
}
pub fn on_pointer_signal(mut self, on_pointer_signal: PointerSignalEventListener) -> Listener {
self.on_pointer_signal = Some(on_pointer_signal);
self
}
pub fn behavior(mut self, behavior: HitTestBehavior) -> Listener {
self.behavior = behavior;
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> Listener {
self.child = Some(child.into_widget());
self
}
fn apply_listeners(&self, app: &mut App, render_object: RenderHandle<RenderPointerListener>) {
render_object.set_on_pointer_down(app, self.on_pointer_down.clone());
render_object.set_on_pointer_move(app, self.on_pointer_move.clone());
render_object.set_on_pointer_up(app, self.on_pointer_up.clone());
render_object.set_on_pointer_hover(app, self.on_pointer_hover.clone());
render_object.set_on_pointer_cancel(app, self.on_pointer_cancel.clone());
render_object.set_on_pointer_pan_zoom_start(app, self.on_pointer_pan_zoom_start.clone());
render_object.set_on_pointer_pan_zoom_update(app, self.on_pointer_pan_zoom_update.clone());
render_object.set_on_pointer_pan_zoom_end(app, self.on_pointer_pan_zoom_end.clone());
render_object.set_on_pointer_signal(app, self.on_pointer_signal.clone());
}
fn listener_names(&self) -> Vec<&'static str> {
let listeners = [
(self.on_pointer_down.is_some(), "down"),
(self.on_pointer_move.is_some(), "move"),
(self.on_pointer_up.is_some(), "up"),
(self.on_pointer_hover.is_some(), "hover"),
(self.on_pointer_cancel.is_some(), "cancel"),
(self.on_pointer_pan_zoom_start.is_some(), "panZoomStart"),
(self.on_pointer_pan_zoom_update.is_some(), "panZoomUpdate"),
(self.on_pointer_pan_zoom_end.is_some(), "panZoomEnd"),
(self.on_pointer_signal.is_some(), "signal"),
];
listeners
.into_iter()
.filter_map(|(is_set, name)| is_set.then_some(name))
.collect()
}
}
impl Default for Listener {
fn default() -> Listener {
Listener {
key: None,
on_pointer_down: None,
on_pointer_move: None,
on_pointer_up: None,
on_pointer_hover: None,
on_pointer_cancel: None,
on_pointer_pan_zoom_start: None,
on_pointer_pan_zoom_update: None,
on_pointer_pan_zoom_end: None,
on_pointer_signal: None,
behavior: HitTestBehavior::DeferToChild,
child: None,
}
}
}
impl RenderObjectWidget for Listener {
type RenderObject = RenderPointerListener;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
let render_object = RenderPointerListener::new(app, self.behavior, None);
self.apply_listeners(app, render_object);
render_object.as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderPointerListener>,
) {
self.apply_listeners(app, render_object);
render_object.set_behavior(app, self.behavior);
}
}
impl SingleChildRenderObjectWidget for Listener {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
impl Debug for Listener {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Listener")
.field("key", &self.key)
.field("listeners", &self.listener_names())
.field("behavior", &self.behavior)
.field("child", &self.child)
.finish()
}
}
pub struct MouseRegion {
pub key: Option<KeyRef>,
pub on_enter: Option<PointerEnterEventListener>,
pub on_hover: Option<PointerHoverEventListener>,
pub on_exit: Option<PointerExitEventListener>,
pub cursor: MouseCursorRef,
pub opaque: bool,
pub hit_test_behavior: Option<HitTestBehavior>,
pub child: Option<WidgetRef>,
}
impl MouseRegion {
pub fn new() -> MouseRegion {
MouseRegion::default()
}
pub fn key(mut self, key: KeyRef) -> MouseRegion {
self.key = Some(key);
self
}
pub fn on_enter(mut self, on_enter: PointerEnterEventListener) -> MouseRegion {
self.on_enter = Some(on_enter);
self
}
pub fn on_hover(mut self, on_hover: PointerHoverEventListener) -> MouseRegion {
self.on_hover = Some(on_hover);
self
}
pub fn on_exit(mut self, on_exit: PointerExitEventListener) -> MouseRegion {
self.on_exit = Some(on_exit);
self
}
pub fn cursor(mut self, cursor: MouseCursorRef) -> MouseRegion {
self.cursor = cursor;
self
}
pub fn opaque(mut self, opaque: bool) -> MouseRegion {
self.opaque = opaque;
self
}
pub fn hit_test_behavior(mut self, hit_test_behavior: HitTestBehavior) -> MouseRegion {
self.hit_test_behavior = Some(hit_test_behavior);
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> MouseRegion {
self.child = Some(child.into_widget());
self
}
fn apply_configuration(&self, app: &mut App, render_object: RenderHandle<RenderMouseRegion>) {
render_object.set_on_enter(app, self.on_enter.clone());
render_object.set_on_hover(app, self.on_hover.clone());
render_object.set_on_exit(app, self.on_exit.clone());
render_object.set_cursor(app, Rc::clone(&self.cursor));
render_object.set_opaque(app, self.opaque);
render_object.set_hit_test_behavior(app, self.hit_test_behavior);
}
fn listener_names(&self) -> Vec<&'static str> {
let listeners = [
(self.on_enter.is_some(), "enter"),
(self.on_exit.is_some(), "exit"),
(self.on_hover.is_some(), "hover"),
];
listeners
.into_iter()
.filter_map(|(is_set, name)| is_set.then_some(name))
.collect()
}
}
impl Default for MouseRegion {
fn default() -> MouseRegion {
MouseRegion {
key: None,
on_enter: None,
on_hover: None,
on_exit: None,
cursor: <dyn MouseCursor>::defer(),
opaque: true,
hit_test_behavior: None,
child: None,
}
}
}
impl RenderObjectWidget for MouseRegion {
type RenderObject = RenderMouseRegion;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
let render_object = RenderMouseRegion::new(app, true, None);
self.apply_configuration(app, render_object);
render_object.as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderMouseRegion>,
) {
self.apply_configuration(app, render_object);
}
}
impl SingleChildRenderObjectWidget for MouseRegion {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
impl Debug for MouseRegion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MouseRegion")
.field("key", &self.key)
.field("listeners", &self.listener_names())
.field("cursor", &self.cursor)
.field("opaque", &self.opaque)
.field("child", &self.child)
.finish()
}
}
#[derive(Debug, Default)]
pub struct RepaintBoundary {
pub key: Option<KeyRef>,
pub child: Option<WidgetRef>,
}
impl RepaintBoundary {
pub fn new() -> RepaintBoundary {
RepaintBoundary::default()
}
pub fn key(mut self, key: KeyRef) -> RepaintBoundary {
self.key = Some(key);
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> RepaintBoundary {
self.child = Some(child.into_widget());
self
}
}
impl RenderObjectWidget for RepaintBoundary {
type RenderObject = RenderRepaintBoundary;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderRepaintBoundary::new(app, None).as_object()
}
}
impl SingleChildRenderObjectWidget for RepaintBoundary {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
#[derive(Debug)]
pub struct IgnorePointer {
pub key: Option<KeyRef>,
pub ignoring: bool,
pub child: Option<WidgetRef>,
}
impl IgnorePointer {
pub fn new() -> IgnorePointer {
IgnorePointer::default()
}
pub fn key(mut self, key: KeyRef) -> IgnorePointer {
self.key = Some(key);
self
}
pub fn ignoring(mut self, ignoring: bool) -> IgnorePointer {
self.ignoring = ignoring;
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> IgnorePointer {
self.child = Some(child.into_widget());
self
}
}
impl Default for IgnorePointer {
fn default() -> IgnorePointer {
IgnorePointer {
key: None,
ignoring: true,
child: None,
}
}
}
impl RenderObjectWidget for IgnorePointer {
type RenderObject = RenderIgnorePointer;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderIgnorePointer::new(app, self.ignoring, None).as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderIgnorePointer>,
) {
render_object.set_ignoring(app, self.ignoring);
}
}
impl SingleChildRenderObjectWidget for IgnorePointer {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
#[derive(Debug)]
pub struct AbsorbPointer {
pub key: Option<KeyRef>,
pub absorbing: bool,
pub child: Option<WidgetRef>,
}
impl AbsorbPointer {
pub fn new() -> AbsorbPointer {
AbsorbPointer::default()
}
pub fn key(mut self, key: KeyRef) -> AbsorbPointer {
self.key = Some(key);
self
}
pub fn absorbing(mut self, absorbing: bool) -> AbsorbPointer {
self.absorbing = absorbing;
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> AbsorbPointer {
self.child = Some(child.into_widget());
self
}
}
impl Default for AbsorbPointer {
fn default() -> AbsorbPointer {
AbsorbPointer {
key: None,
absorbing: true,
child: None,
}
}
}
impl RenderObjectWidget for AbsorbPointer {
type RenderObject = RenderAbsorbPointer;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderAbsorbPointer::new(app, self.absorbing, None).as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderAbsorbPointer>,
) {
render_object.set_absorbing(app, self.absorbing);
}
}
impl SingleChildRenderObjectWidget for AbsorbPointer {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
pub struct MetaData {
pub key: Option<KeyRef>,
pub meta_data: Option<Rc<dyn Any>>,
pub behavior: HitTestBehavior,
pub child: Option<WidgetRef>,
}
impl MetaData {
pub fn new() -> MetaData {
MetaData::default()
}
pub fn key(mut self, key: KeyRef) -> MetaData {
self.key = Some(key);
self
}
pub fn meta_data(mut self, meta_data: Rc<dyn Any>) -> MetaData {
self.meta_data = Some(meta_data);
self
}
pub fn behavior(mut self, behavior: HitTestBehavior) -> MetaData {
self.behavior = behavior;
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> MetaData {
self.child = Some(child.into_widget());
self
}
}
impl Default for MetaData {
fn default() -> MetaData {
MetaData {
key: None,
meta_data: None,
behavior: HitTestBehavior::DeferToChild,
child: None,
}
}
}
impl RenderObjectWidget for MetaData {
type RenderObject = RenderMetaData;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderMetaData::new(app, self.meta_data.clone(), self.behavior, None).as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderMetaData>,
) {
render_object.set_meta_data(app, self.meta_data.clone());
render_object.set_behavior(app, self.behavior);
}
}
impl SingleChildRenderObjectWidget for MetaData {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
#[derive(Debug)]
pub struct BackdropGroup {
pub key: Option<KeyRef>,
pub backdrop_key: BackdropKey,
pub child: WidgetRef,
}
impl BackdropGroup {
pub fn new<K>(child: impl IntoWidget<K>) -> BackdropGroup {
BackdropGroup {
key: None,
backdrop_key: BackdropKey::new(),
child: child.into_widget(),
}
}
pub fn key(mut self, key: KeyRef) -> BackdropGroup {
self.key = Some(key);
self
}
pub fn backdrop_key(mut self, backdrop_key: BackdropKey) -> BackdropGroup {
self.backdrop_key = backdrop_key;
self
}
pub fn of(app: &mut App, context: BuildContext) -> Option<&BackdropGroup> {
context.depend_on_inherited_widget_of_exact_type::<BackdropGroup>(app)
}
}
impl InheritedWidget for BackdropGroup {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn child(&self) -> &WidgetRef {
&self.child
}
fn update_should_notify(&self, old_widget: &BackdropGroup) -> bool {
old_widget.backdrop_key != self.backdrop_key
}
}
pub struct BackdropFilter {
pub key: Option<KeyRef>,
pub filter_config: ImageFilterConfig,
pub blend_mode: BlendMode,
pub enabled: bool,
pub backdrop_group_key: Option<BackdropKey>,
pub child: Option<WidgetRef>,
use_shared_key: bool,
}
impl BackdropFilter {
pub fn new(filter_config: ImageFilterConfig) -> BackdropFilter {
BackdropFilter {
key: None,
filter_config,
blend_mode: BlendMode::SrcOver,
enabled: true,
backdrop_group_key: None,
child: None,
use_shared_key: false,
}
}
pub fn filter(filter: ImageFilter) -> BackdropFilter {
BackdropFilter::new(ImageFilterConfig::new(filter))
}
pub fn grouped(filter_config: ImageFilterConfig) -> BackdropFilter {
BackdropFilter {
use_shared_key: true,
..BackdropFilter::new(filter_config)
}
}
pub fn key(mut self, key: KeyRef) -> BackdropFilter {
self.key = Some(key);
self
}
pub fn blend_mode(mut self, blend_mode: BlendMode) -> BackdropFilter {
self.blend_mode = blend_mode;
self
}
pub fn enabled(mut self, enabled: bool) -> BackdropFilter {
self.enabled = enabled;
self
}
pub fn backdrop_group_key(mut self, backdrop_group_key: BackdropKey) -> BackdropFilter {
debug_assert!(!self.use_shared_key);
self.backdrop_group_key = Some(backdrop_group_key);
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> BackdropFilter {
self.child = Some(child.into_widget());
self
}
fn get_backdrop_group_key(&self, app: &mut App, context: BuildContext) -> Option<BackdropKey> {
if self.use_shared_key {
return BackdropGroup::of(app, context).map(|group| group.backdrop_key);
}
self.backdrop_group_key
}
}
impl RenderObjectWidget for BackdropFilter {
type RenderObject = RenderBackdropFilter;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
let backdrop_key = self.get_backdrop_group_key(app, context);
RenderBackdropFilter::new(
app,
self.filter_config.clone(),
self.blend_mode,
self.enabled,
backdrop_key,
None,
)
.as_object()
}
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: RenderHandle<RenderBackdropFilter>,
) {
render_object.set_filter_config(app, self.filter_config.clone());
render_object.set_enabled(app, self.enabled);
render_object.set_blend_mode(app, self.blend_mode);
let backdrop_key = self.get_backdrop_group_key(app, context);
render_object.set_backdrop_key(app, backdrop_key);
}
}
impl SingleChildRenderObjectWidget for BackdropFilter {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
impl Debug for BackdropFilter {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BackdropFilter")
.field("key", &self.key)
.field("filter_config", &self.filter_config)
.field("blend_mode", &self.blend_mode)
.field("enabled", &self.enabled)
.field("child", &self.child)
.finish_non_exhaustive()
}
}
impl Debug for MetaData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MetaData")
.field("key", &self.key)
.field("meta_data", &self.meta_data.as_ref().map(|_| "..."))
.field("behavior", &self.behavior)
.field("child", &self.child)
.finish()
}
}
#[derive(Debug)]
pub struct KeyedSubtree {
pub key: Option<KeyRef>,
pub child: WidgetRef,
}
impl KeyedSubtree {
pub fn new<K>(child: impl IntoWidget<K>) -> KeyedSubtree {
KeyedSubtree {
key: None,
child: child.into_widget(),
}
}
pub fn key(mut self, key: KeyRef) -> KeyedSubtree {
self.key = Some(key);
self
}
}
impl StatelessWidget for KeyedSubtree {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn build(&self, _app: &mut App, _context: BuildContext) -> WidgetRef {
self.child.clone()
}
}
pub type WidgetBuilder = Rc<dyn Fn(&mut App, BuildContext) -> WidgetRef>;
pub struct Builder {
pub key: Option<KeyRef>,
pub builder: WidgetBuilder,
}
impl Builder {
pub fn new(builder: impl Fn(&mut App, BuildContext) -> WidgetRef + 'static) -> Builder {
Builder {
key: None,
builder: Rc::new(builder),
}
}
pub fn key(mut self, key: KeyRef) -> Builder {
self.key = Some(key);
self
}
}
impl StatelessWidget for Builder {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
(self.builder)(app, context)
}
}
impl Debug for Builder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Builder")
.field("key", &self.key)
.finish_non_exhaustive()
}
}
#[derive(Debug)]
pub struct ColoredBox {
pub key: Option<KeyRef>,
pub color: AnyColor,
pub is_anti_alias: bool,
pub child: Option<WidgetRef>,
}
impl ColoredBox {
pub fn new(color: impl Into<AnyColor>) -> ColoredBox {
ColoredBox {
key: None,
color: color.into(),
is_anti_alias: true,
child: None,
}
}
pub fn key(mut self, key: KeyRef) -> ColoredBox {
self.key = Some(key);
self
}
pub fn is_anti_alias(mut self, is_anti_alias: bool) -> ColoredBox {
self.is_anti_alias = is_anti_alias;
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> ColoredBox {
self.child = Some(child.into_widget());
self
}
}
impl RenderObjectWidget for ColoredBox {
type RenderObject = RenderColoredBox;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderColoredBox::new(app, self.color.color(), self.is_anti_alias, None).as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderColoredBox>,
) {
render_object.set_color(app, self.color.color());
render_object.set_is_anti_alias(app, self.is_anti_alias);
}
}
impl SingleChildRenderObjectWidget for ColoredBox {
fn child(&self) -> Option<&WidgetRef> {
self.child.as_ref()
}
}
pub fn get_axis_direction_from_axis_reverse_and_directionality(
app: &mut App,
context: BuildContext,
axis: Axis,
reverse: bool,
) -> AxisDirection {
match axis {
Axis::Horizontal => {
let text_direction = Directionality::of(app, context);
let axis_direction = text_direction_to_axis_direction(text_direction);
if reverse {
flip_axis_direction(axis_direction)
} else {
axis_direction
}
}
Axis::Vertical => {
if reverse {
AxisDirection::Up
} else {
AxisDirection::Down
}
}
}
}
#[derive(Debug)]
pub struct Stack {
pub key: Option<KeyRef>,
pub alignment: AlignmentGeometry,
pub text_direction: Option<TextDirection>,
pub fit: StackFit,
pub clip_behavior: Clip,
pub children: Vec<WidgetRef>,
}
impl Stack {
pub fn new() -> Stack {
Stack::default()
}
pub fn key(mut self, key: KeyRef) -> Stack {
self.key = Some(key);
self
}
pub fn alignment(mut self, alignment: AlignmentGeometry) -> Stack {
self.alignment = alignment;
self
}
pub fn text_direction(mut self, text_direction: TextDirection) -> Stack {
self.text_direction = Some(text_direction);
self
}
pub fn fit(mut self, fit: StackFit) -> Stack {
self.fit = fit;
self
}
pub fn clip_behavior(mut self, clip_behavior: Clip) -> Stack {
self.clip_behavior = clip_behavior;
self
}
pub fn children(mut self, children: impl IntoIterator<Item = WidgetRef>) -> Stack {
self.children = children.into_iter().collect();
self
}
fn resolved_text_direction(
text_direction: Option<TextDirection>,
app: &mut App,
context: BuildContext,
) -> Option<TextDirection> {
text_direction.or_else(|| Directionality::maybe_of(app, context))
}
}
impl Default for Stack {
fn default() -> Stack {
Stack {
key: None,
alignment: AlignmentGeometry::TOP_START,
text_direction: None,
fit: StackFit::Loose,
clip_behavior: Clip::HardEdge,
children: Vec::new(),
}
}
}
impl RenderObjectWidget for Stack {
type RenderObject = RenderStack;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
let render_object = RenderStack::new(app);
render_object.set_alignment(app, self.alignment);
let text_direction = Stack::resolved_text_direction(self.text_direction, app, context);
render_object.set_text_direction(app, text_direction);
render_object.set_fit(app, self.fit);
render_object.set_clip_behavior(app, self.clip_behavior);
render_object.as_object()
}
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: RenderHandle<RenderStack>,
) {
render_object.set_alignment(app, self.alignment);
let text_direction = Stack::resolved_text_direction(self.text_direction, app, context);
render_object.set_text_direction(app, text_direction);
render_object.set_fit(app, self.fit);
render_object.set_clip_behavior(app, self.clip_behavior);
}
}
impl MultiChildRenderObjectWidget for Stack {
fn children(&self) -> &[WidgetRef] {
&self.children
}
}
#[derive(Debug)]
pub struct Positioned {
pub key: Option<KeyRef>,
pub left: Option<f64>,
pub top: Option<f64>,
pub right: Option<f64>,
pub bottom: Option<f64>,
pub width: Option<f64>,
pub height: Option<f64>,
pub child: WidgetRef,
}
impl Positioned {
pub fn new<K>(child: impl IntoWidget<K>) -> Positioned {
Positioned {
key: None,
left: None,
top: None,
right: None,
bottom: None,
width: None,
height: None,
child: child.into_widget(),
}
}
pub fn from_rect<K>(rect: Rect, child: impl IntoWidget<K>) -> Positioned {
Positioned {
key: None,
left: Some(rect.left),
top: Some(rect.top),
right: None,
bottom: None,
width: Some(rect.width()),
height: Some(rect.height()),
child: child.into_widget(),
}
}
pub fn from_relative_rect<K>(rect: RelativeRect, child: impl IntoWidget<K>) -> Positioned {
Positioned {
key: None,
left: Some(rect.left),
top: Some(rect.top),
right: Some(rect.right),
bottom: Some(rect.bottom),
width: None,
height: None,
child: child.into_widget(),
}
}
pub fn fill<K>(child: impl IntoWidget<K>) -> Positioned {
Positioned {
key: None,
left: Some(0.0),
top: Some(0.0),
right: Some(0.0),
bottom: Some(0.0),
width: None,
height: None,
child: child.into_widget(),
}
}
pub fn directional<K>(
text_direction: TextDirection,
start: Option<f64>,
end: Option<f64>,
child: impl IntoWidget<K>,
) -> Positioned {
let (left, right) = match text_direction {
TextDirection::Rtl => (end, start),
TextDirection::Ltr => (start, end),
};
Positioned {
key: None,
left,
top: None,
right,
bottom: None,
width: None,
height: None,
child: child.into_widget(),
}
}
pub fn key(mut self, key: KeyRef) -> Positioned {
self.key = Some(key);
self
}
pub fn left(mut self, left: f64) -> Positioned {
self.left = Some(left);
self.debug_check_axes();
self
}
pub fn top(mut self, top: f64) -> Positioned {
self.top = Some(top);
self.debug_check_axes();
self
}
pub fn right(mut self, right: f64) -> Positioned {
self.right = Some(right);
self.debug_check_axes();
self
}
pub fn bottom(mut self, bottom: f64) -> Positioned {
self.bottom = Some(bottom);
self.debug_check_axes();
self
}
pub fn width(mut self, width: f64) -> Positioned {
self.width = Some(width);
self.debug_check_axes();
self
}
pub fn height(mut self, height: f64) -> Positioned {
self.height = Some(height);
self.debug_check_axes();
self
}
fn debug_check_axes(&self) {
debug_assert!(self.left.is_none() || self.right.is_none() || self.width.is_none());
debug_assert!(self.top.is_none() || self.bottom.is_none() || self.height.is_none());
}
}
impl ParentDataWidget for Positioned {
type ParentData = StackParentData;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn child(&self) -> &WidgetRef {
&self.child
}
fn debug_is_valid_render_object(&self, app: &App, render_object: AnyRenderObject) -> bool {
render_object.parent_data_is::<StackParentData>(app)
|| render_object.parent_data_is::<TheaterParentData>(app)
}
fn apply_parent_data(&self, app: &mut App, render_object: AnyRenderObject) {
let parent_data = if render_object.parent_data_is::<StackParentData>(app) {
render_object.parent_data_of_mut::<StackParentData>(app)
} else {
render_object
.parent_data_of_mut::<TheaterParentData>(app)
.stack_mut()
};
let mut needs_layout = false;
if parent_data.left != self.left {
parent_data.left = self.left;
needs_layout = true;
}
if parent_data.top != self.top {
parent_data.top = self.top;
needs_layout = true;
}
if parent_data.right != self.right {
parent_data.right = self.right;
needs_layout = true;
}
if parent_data.bottom != self.bottom {
parent_data.bottom = self.bottom;
needs_layout = true;
}
if parent_data.width != self.width {
parent_data.width = self.width;
needs_layout = true;
}
if parent_data.height != self.height {
parent_data.height = self.height;
needs_layout = true;
}
if needs_layout && let Some(parent) = render_object.parent(app) {
parent.mark_needs_layout(app);
}
}
}
#[derive(Clone, Copy, Debug)]
struct FlexConfiguration {
direction: Axis,
main_axis_alignment: MainAxisAlignment,
main_axis_size: MainAxisSize,
cross_axis_alignment: CrossAxisAlignment,
text_direction: Option<TextDirection>,
vertical_direction: VerticalDirection,
text_baseline: Option<TextBaseline>,
clip_behavior: Clip,
spacing: f64,
}
impl FlexConfiguration {
fn new(direction: Axis) -> FlexConfiguration {
FlexConfiguration {
direction,
main_axis_alignment: MainAxisAlignment::Start,
main_axis_size: MainAxisSize::Max,
cross_axis_alignment: CrossAxisAlignment::Center,
text_direction: None,
vertical_direction: VerticalDirection::Down,
text_baseline: None,
clip_behavior: Clip::None,
spacing: 0.0,
}
}
fn need_text_direction(&self) -> bool {
match self.direction {
Axis::Horizontal => true,
Axis::Vertical => {
self.cross_axis_alignment == CrossAxisAlignment::Start
|| self.cross_axis_alignment == CrossAxisAlignment::End
}
}
}
fn get_effective_text_direction(
&self,
app: &mut App,
context: BuildContext,
) -> Option<TextDirection> {
self.text_direction.or_else(|| {
self.need_text_direction()
.then(|| Directionality::maybe_of(app, context))
.flatten()
})
}
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
let render_object = RenderFlex::new(app);
self.push(app, context, render_object);
render_object.as_object()
}
fn push(&self, app: &mut App, context: BuildContext, render_object: RenderHandle<RenderFlex>) {
debug_assert!(
self.cross_axis_alignment != CrossAxisAlignment::Baseline
|| self.text_baseline.is_some(),
"textBaseline is required if you specify the crossAxisAlignment with \
CrossAxisAlignment.baseline"
);
render_object.set_direction(app, self.direction);
render_object.set_main_axis_alignment(app, self.main_axis_alignment);
render_object.set_main_axis_size(app, self.main_axis_size);
render_object.set_cross_axis_alignment(app, self.cross_axis_alignment);
let text_direction = self.get_effective_text_direction(app, context);
render_object.set_text_direction(app, text_direction);
render_object.set_vertical_direction(app, self.vertical_direction);
render_object.set_text_baseline(app, self.text_baseline);
render_object.set_clip_behavior(app, self.clip_behavior);
render_object.set_spacing(app, self.spacing);
}
}
#[derive(Debug)]
pub struct Flex {
pub key: Option<KeyRef>,
pub direction: Axis,
pub main_axis_alignment: MainAxisAlignment,
pub main_axis_size: MainAxisSize,
pub cross_axis_alignment: CrossAxisAlignment,
pub text_direction: Option<TextDirection>,
pub vertical_direction: VerticalDirection,
pub text_baseline: Option<TextBaseline>,
pub clip_behavior: Clip,
pub spacing: f64,
pub children: Vec<WidgetRef>,
}
impl Flex {
pub fn new(direction: Axis) -> Flex {
let configuration = FlexConfiguration::new(direction);
Flex {
key: None,
direction: configuration.direction,
main_axis_alignment: configuration.main_axis_alignment,
main_axis_size: configuration.main_axis_size,
cross_axis_alignment: configuration.cross_axis_alignment,
text_direction: configuration.text_direction,
vertical_direction: configuration.vertical_direction,
text_baseline: configuration.text_baseline,
clip_behavior: configuration.clip_behavior,
spacing: configuration.spacing,
children: Vec::new(),
}
}
pub fn key(mut self, key: KeyRef) -> Flex {
self.key = Some(key);
self
}
pub fn main_axis_alignment(mut self, main_axis_alignment: MainAxisAlignment) -> Flex {
self.main_axis_alignment = main_axis_alignment;
self
}
pub fn main_axis_size(mut self, main_axis_size: MainAxisSize) -> Flex {
self.main_axis_size = main_axis_size;
self
}
pub fn cross_axis_alignment(mut self, cross_axis_alignment: CrossAxisAlignment) -> Flex {
self.cross_axis_alignment = cross_axis_alignment;
self
}
pub fn text_direction(mut self, text_direction: TextDirection) -> Flex {
self.text_direction = Some(text_direction);
self
}
pub fn vertical_direction(mut self, vertical_direction: VerticalDirection) -> Flex {
self.vertical_direction = vertical_direction;
self
}
pub fn text_baseline(mut self, text_baseline: TextBaseline) -> Flex {
self.text_baseline = Some(text_baseline);
self
}
pub fn clip_behavior(mut self, clip_behavior: Clip) -> Flex {
self.clip_behavior = clip_behavior;
self
}
pub fn spacing(mut self, spacing: f64) -> Flex {
self.spacing = spacing;
self
}
pub fn children(mut self, children: impl IntoIterator<Item = WidgetRef>) -> Flex {
self.children = children.into_iter().collect();
self
}
pub fn get_effective_text_direction(
&self,
app: &mut App,
context: BuildContext,
) -> Option<TextDirection> {
self.configuration()
.get_effective_text_direction(app, context)
}
fn configuration(&self) -> FlexConfiguration {
FlexConfiguration {
direction: self.direction,
main_axis_alignment: self.main_axis_alignment,
main_axis_size: self.main_axis_size,
cross_axis_alignment: self.cross_axis_alignment,
text_direction: self.text_direction,
vertical_direction: self.vertical_direction,
text_baseline: self.text_baseline,
clip_behavior: self.clip_behavior,
spacing: self.spacing,
}
}
}
impl RenderObjectWidget for Flex {
type RenderObject = RenderFlex;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
self.configuration().create_render_object(app, context)
}
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: RenderHandle<RenderFlex>,
) {
self.configuration().push(app, context, render_object);
}
}
impl MultiChildRenderObjectWidget for Flex {
fn children(&self) -> &[WidgetRef] {
&self.children
}
}
#[derive(Debug)]
pub struct Row {
pub key: Option<KeyRef>,
pub main_axis_alignment: MainAxisAlignment,
pub main_axis_size: MainAxisSize,
pub cross_axis_alignment: CrossAxisAlignment,
pub text_direction: Option<TextDirection>,
pub vertical_direction: VerticalDirection,
pub text_baseline: Option<TextBaseline>,
pub spacing: f64,
pub children: Vec<WidgetRef>,
}
impl Row {
pub fn new() -> Row {
Row::default()
}
pub fn key(mut self, key: KeyRef) -> Row {
self.key = Some(key);
self
}
pub fn main_axis_alignment(mut self, main_axis_alignment: MainAxisAlignment) -> Row {
self.main_axis_alignment = main_axis_alignment;
self
}
pub fn main_axis_size(mut self, main_axis_size: MainAxisSize) -> Row {
self.main_axis_size = main_axis_size;
self
}
pub fn cross_axis_alignment(mut self, cross_axis_alignment: CrossAxisAlignment) -> Row {
self.cross_axis_alignment = cross_axis_alignment;
self
}
pub fn text_direction(mut self, text_direction: TextDirection) -> Row {
self.text_direction = Some(text_direction);
self
}
pub fn vertical_direction(mut self, vertical_direction: VerticalDirection) -> Row {
self.vertical_direction = vertical_direction;
self
}
pub fn text_baseline(mut self, text_baseline: TextBaseline) -> Row {
self.text_baseline = Some(text_baseline);
self
}
pub fn spacing(mut self, spacing: f64) -> Row {
self.spacing = spacing;
self
}
pub fn children(mut self, children: impl IntoIterator<Item = WidgetRef>) -> Row {
self.children = children.into_iter().collect();
self
}
pub fn direction(&self) -> Axis {
Axis::Horizontal
}
pub fn get_effective_text_direction(
&self,
app: &mut App,
context: BuildContext,
) -> Option<TextDirection> {
self.configuration()
.get_effective_text_direction(app, context)
}
fn configuration(&self) -> FlexConfiguration {
FlexConfiguration {
direction: self.direction(),
main_axis_alignment: self.main_axis_alignment,
main_axis_size: self.main_axis_size,
cross_axis_alignment: self.cross_axis_alignment,
text_direction: self.text_direction,
vertical_direction: self.vertical_direction,
text_baseline: self.text_baseline,
spacing: self.spacing,
..FlexConfiguration::new(self.direction())
}
}
}
impl Default for Row {
fn default() -> Row {
let configuration = FlexConfiguration::new(Axis::Horizontal);
Row {
key: None,
main_axis_alignment: configuration.main_axis_alignment,
main_axis_size: configuration.main_axis_size,
cross_axis_alignment: configuration.cross_axis_alignment,
text_direction: configuration.text_direction,
vertical_direction: configuration.vertical_direction,
text_baseline: configuration.text_baseline,
spacing: configuration.spacing,
children: Vec::new(),
}
}
}
impl RenderObjectWidget for Row {
type RenderObject = RenderFlex;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
self.configuration().create_render_object(app, context)
}
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: RenderHandle<RenderFlex>,
) {
self.configuration().push(app, context, render_object);
}
}
impl MultiChildRenderObjectWidget for Row {
fn children(&self) -> &[WidgetRef] {
&self.children
}
}
#[derive(Debug)]
pub struct Column {
pub key: Option<KeyRef>,
pub main_axis_alignment: MainAxisAlignment,
pub main_axis_size: MainAxisSize,
pub cross_axis_alignment: CrossAxisAlignment,
pub text_direction: Option<TextDirection>,
pub vertical_direction: VerticalDirection,
pub text_baseline: Option<TextBaseline>,
pub spacing: f64,
pub children: Vec<WidgetRef>,
}
impl Column {
pub fn new() -> Column {
Column::default()
}
pub fn key(mut self, key: KeyRef) -> Column {
self.key = Some(key);
self
}
pub fn main_axis_alignment(mut self, main_axis_alignment: MainAxisAlignment) -> Column {
self.main_axis_alignment = main_axis_alignment;
self
}
pub fn main_axis_size(mut self, main_axis_size: MainAxisSize) -> Column {
self.main_axis_size = main_axis_size;
self
}
pub fn cross_axis_alignment(mut self, cross_axis_alignment: CrossAxisAlignment) -> Column {
self.cross_axis_alignment = cross_axis_alignment;
self
}
pub fn text_direction(mut self, text_direction: TextDirection) -> Column {
self.text_direction = Some(text_direction);
self
}
pub fn vertical_direction(mut self, vertical_direction: VerticalDirection) -> Column {
self.vertical_direction = vertical_direction;
self
}
pub fn text_baseline(mut self, text_baseline: TextBaseline) -> Column {
self.text_baseline = Some(text_baseline);
self
}
pub fn spacing(mut self, spacing: f64) -> Column {
self.spacing = spacing;
self
}
pub fn children(mut self, children: impl IntoIterator<Item = WidgetRef>) -> Column {
self.children = children.into_iter().collect();
self
}
pub fn direction(&self) -> Axis {
Axis::Vertical
}
pub fn get_effective_text_direction(
&self,
app: &mut App,
context: BuildContext,
) -> Option<TextDirection> {
self.configuration()
.get_effective_text_direction(app, context)
}
fn configuration(&self) -> FlexConfiguration {
FlexConfiguration {
direction: self.direction(),
main_axis_alignment: self.main_axis_alignment,
main_axis_size: self.main_axis_size,
cross_axis_alignment: self.cross_axis_alignment,
text_direction: self.text_direction,
vertical_direction: self.vertical_direction,
text_baseline: self.text_baseline,
spacing: self.spacing,
..FlexConfiguration::new(self.direction())
}
}
}
impl Default for Column {
fn default() -> Column {
let configuration = FlexConfiguration::new(Axis::Vertical);
Column {
key: None,
main_axis_alignment: configuration.main_axis_alignment,
main_axis_size: configuration.main_axis_size,
cross_axis_alignment: configuration.cross_axis_alignment,
text_direction: configuration.text_direction,
vertical_direction: configuration.vertical_direction,
text_baseline: configuration.text_baseline,
spacing: configuration.spacing,
children: Vec::new(),
}
}
}
impl RenderObjectWidget for Column {
type RenderObject = RenderFlex;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
self.configuration().create_render_object(app, context)
}
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: RenderHandle<RenderFlex>,
) {
self.configuration().push(app, context, render_object);
}
}
impl MultiChildRenderObjectWidget for Column {
fn children(&self) -> &[WidgetRef] {
&self.children
}
}
#[derive(Debug)]
pub struct Flexible {
pub key: Option<KeyRef>,
pub flex: i32,
pub fit: FlexFit,
pub child: WidgetRef,
}
impl Flexible {
pub fn new<K>(child: impl IntoWidget<K>) -> Flexible {
Flexible {
key: None,
flex: 1,
fit: FlexFit::Loose,
child: child.into_widget(),
}
}
pub fn key(mut self, key: KeyRef) -> Flexible {
self.key = Some(key);
self
}
pub fn flex(mut self, flex: i32) -> Flexible {
self.flex = flex;
self
}
pub fn fit(mut self, fit: FlexFit) -> Flexible {
self.fit = fit;
self
}
fn apply_flex_parent_data(
app: &mut App,
render_object: AnyRenderObject,
flex: i32,
fit: FlexFit,
) {
debug_assert!(render_object.parent_data_is::<FlexParentData>(app));
let parent_data = render_object.parent_data_of_mut::<FlexParentData>(app);
let mut needs_layout = false;
if parent_data.flex != Some(flex) {
parent_data.flex = Some(flex);
needs_layout = true;
}
if parent_data.fit != Some(fit) {
parent_data.fit = Some(fit);
needs_layout = true;
}
if needs_layout && let Some(parent) = render_object.parent(app) {
parent.mark_needs_layout(app);
}
}
}
impl ParentDataWidget for Flexible {
type ParentData = FlexParentData;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn child(&self) -> &WidgetRef {
&self.child
}
fn apply_parent_data(&self, app: &mut App, render_object: AnyRenderObject) {
Flexible::apply_flex_parent_data(app, render_object, self.flex, self.fit);
}
}
#[derive(Debug)]
pub struct Expanded {
pub key: Option<KeyRef>,
pub flex: i32,
pub child: WidgetRef,
}
impl Expanded {
pub fn new<K>(child: impl IntoWidget<K>) -> Expanded {
Expanded {
key: None,
flex: 1,
child: child.into_widget(),
}
}
pub fn key(mut self, key: KeyRef) -> Expanded {
self.key = Some(key);
self
}
pub fn flex(mut self, flex: i32) -> Expanded {
self.flex = flex;
self
}
pub fn fit(&self) -> FlexFit {
FlexFit::Tight
}
}
impl ParentDataWidget for Expanded {
type ParentData = FlexParentData;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn child(&self) -> &WidgetRef {
&self.child
}
fn apply_parent_data(&self, app: &mut App, render_object: AnyRenderObject) {
Flexible::apply_flex_parent_data(app, render_object, self.flex, self.fit());
}
}
#[derive(Debug)]
pub struct Wrap {
pub key: Option<KeyRef>,
pub direction: Axis,
pub alignment: WrapAlignment,
pub spacing: f64,
pub run_alignment: WrapAlignment,
pub run_spacing: f64,
pub cross_axis_alignment: WrapCrossAlignment,
pub text_direction: Option<TextDirection>,
pub vertical_direction: VerticalDirection,
pub clip_behavior: Clip,
pub children: Vec<WidgetRef>,
}
impl Wrap {
pub fn new() -> Wrap {
Wrap::default()
}
pub fn key(mut self, key: KeyRef) -> Wrap {
self.key = Some(key);
self
}
pub fn direction(mut self, direction: Axis) -> Wrap {
self.direction = direction;
self
}
pub fn alignment(mut self, alignment: WrapAlignment) -> Wrap {
self.alignment = alignment;
self
}
pub fn spacing(mut self, spacing: f64) -> Wrap {
self.spacing = spacing;
self
}
pub fn run_alignment(mut self, run_alignment: WrapAlignment) -> Wrap {
self.run_alignment = run_alignment;
self
}
pub fn run_spacing(mut self, run_spacing: f64) -> Wrap {
self.run_spacing = run_spacing;
self
}
pub fn cross_axis_alignment(mut self, cross_axis_alignment: WrapCrossAlignment) -> Wrap {
self.cross_axis_alignment = cross_axis_alignment;
self
}
pub fn text_direction(mut self, text_direction: TextDirection) -> Wrap {
self.text_direction = Some(text_direction);
self
}
pub fn vertical_direction(mut self, vertical_direction: VerticalDirection) -> Wrap {
self.vertical_direction = vertical_direction;
self
}
pub fn clip_behavior(mut self, clip_behavior: Clip) -> Wrap {
self.clip_behavior = clip_behavior;
self
}
pub fn children(mut self, children: impl IntoIterator<Item = WidgetRef>) -> Wrap {
self.children = children.into_iter().collect();
self
}
}
impl Default for Wrap {
fn default() -> Wrap {
Wrap {
key: None,
direction: Axis::Horizontal,
alignment: WrapAlignment::Start,
spacing: 0.0,
run_alignment: WrapAlignment::Start,
run_spacing: 0.0,
cross_axis_alignment: WrapCrossAlignment::Start,
text_direction: None,
vertical_direction: VerticalDirection::Down,
clip_behavior: Clip::None,
children: Vec::new(),
}
}
}
impl RenderObjectWidget for Wrap {
type RenderObject = RenderWrap;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
let render_object = RenderWrap::new(app);
self.update_render_object(app, context, render_object);
render_object.as_object()
}
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: RenderHandle<RenderWrap>,
) {
render_object.set_direction(app, self.direction);
render_object.set_alignment(app, self.alignment);
render_object.set_spacing(app, self.spacing);
render_object.set_run_alignment(app, self.run_alignment);
render_object.set_run_spacing(app, self.run_spacing);
render_object.set_cross_axis_alignment(app, self.cross_axis_alignment);
let text_direction = self
.text_direction
.or_else(|| Directionality::maybe_of(app, context));
render_object.set_text_direction(app, text_direction);
render_object.set_vertical_direction(app, self.vertical_direction);
render_object.set_clip_behavior(app, self.clip_behavior);
}
}
impl MultiChildRenderObjectWidget for Wrap {
fn children(&self) -> &[WidgetRef] {
&self.children
}
}
#[derive(Debug)]
pub struct Spacer {
pub key: Option<KeyRef>,
pub flex: i32,
}
impl Spacer {
pub fn new() -> Spacer {
Spacer::default()
}
pub fn key(mut self, key: KeyRef) -> Spacer {
self.key = Some(key);
self
}
pub fn flex(mut self, flex: i32) -> Spacer {
debug_assert!(flex > 0);
self.flex = flex;
self
}
}
impl Default for Spacer {
fn default() -> Spacer {
Spacer { key: None, flex: 1 }
}
}
impl StatelessWidget for Spacer {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn build(&self, _app: &mut App, _context: BuildContext) -> WidgetRef {
Expanded::new(SizedBox::shrink())
.flex(self.flex)
.into_widget()
}
}
#[derive(Debug)]
pub struct IndexedStack {
pub key: Option<KeyRef>,
pub alignment: AlignmentGeometry,
pub text_direction: Option<TextDirection>,
pub clip_behavior: Clip,
pub sizing: StackFit,
pub index: Option<i32>,
pub children: Vec<WidgetRef>,
}
impl IndexedStack {
pub fn new() -> IndexedStack {
IndexedStack::default()
}
pub fn key(mut self, key: KeyRef) -> IndexedStack {
self.key = Some(key);
self
}
pub fn alignment(mut self, alignment: AlignmentGeometry) -> IndexedStack {
self.alignment = alignment;
self
}
pub fn text_direction(mut self, text_direction: TextDirection) -> IndexedStack {
self.text_direction = Some(text_direction);
self
}
pub fn clip_behavior(mut self, clip_behavior: Clip) -> IndexedStack {
self.clip_behavior = clip_behavior;
self
}
pub fn sizing(mut self, sizing: StackFit) -> IndexedStack {
self.sizing = sizing;
self
}
pub fn index(mut self, index: Option<i32>) -> IndexedStack {
self.index = index;
self
}
pub fn children(mut self, children: impl IntoIterator<Item = WidgetRef>) -> IndexedStack {
self.children = children.into_iter().collect();
self
}
}
impl Default for IndexedStack {
fn default() -> IndexedStack {
IndexedStack {
key: None,
alignment: AlignmentGeometry::TOP_START,
text_direction: None,
clip_behavior: Clip::HardEdge,
sizing: StackFit::Loose,
index: Some(0),
children: Vec::new(),
}
}
}
impl StatelessWidget for IndexedStack {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn build(&self, _app: &mut App, _context: BuildContext) -> WidgetRef {
let wrapped_children = self.children.iter().enumerate().map(|(i, child)| {
let is_selected = Some(i as i32) == self.index;
VisibilityScope {
is_visible: is_selected,
child: ExcludeFocus::new(child.clone())
.excluding(!is_selected)
.into_widget(),
}
.into_widget()
});
RawIndexedStack::new(self.sizing)
.alignment(self.alignment)
.index(self.index)
.children(wrapped_children)
.clip_behavior(self.clip_behavior)
.with_text_direction(self.text_direction)
.into_widget()
}
}
#[derive(Debug)]
struct RawIndexedStack {
key: Option<KeyRef>,
alignment: AlignmentGeometry,
text_direction: Option<TextDirection>,
clip_behavior: Clip,
fit: StackFit,
index: Option<i32>,
children: Vec<WidgetRef>,
}
impl RawIndexedStack {
fn new(sizing: StackFit) -> RawIndexedStack {
let stack = Stack::new();
RawIndexedStack {
key: None,
alignment: stack.alignment,
text_direction: stack.text_direction,
clip_behavior: stack.clip_behavior,
fit: sizing,
index: Some(0),
children: Vec::new(),
}
}
fn alignment(mut self, alignment: AlignmentGeometry) -> RawIndexedStack {
self.alignment = alignment;
self
}
fn with_text_direction(mut self, text_direction: Option<TextDirection>) -> RawIndexedStack {
self.text_direction = text_direction;
self
}
fn clip_behavior(mut self, clip_behavior: Clip) -> RawIndexedStack {
self.clip_behavior = clip_behavior;
self
}
fn index(mut self, index: Option<i32>) -> RawIndexedStack {
self.index = index;
self
}
fn children(mut self, children: impl IntoIterator<Item = WidgetRef>) -> RawIndexedStack {
self.children = children.into_iter().collect();
self
}
fn debug_check_index(&self) {
debug_assert!(
self.index.is_none_or(|index| {
(index == 0 && self.children.is_empty())
|| (index >= 0 && (index as usize) < self.children.len())
}),
"The index must be null or within the range of children."
);
}
}
impl RenderObjectWidget for RawIndexedStack {
type RenderObject = RenderIndexedStack;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, context: BuildContext) -> AnyRenderObject {
self.debug_check_index();
let render_object = RenderIndexedStack::new(app);
render_object.set_index(app, self.index);
render_object.set_fit(app, self.fit);
render_object.set_clip_behavior(app, self.clip_behavior);
render_object.set_alignment(app, self.alignment);
let text_direction = Stack::resolved_text_direction(self.text_direction, app, context);
render_object.set_text_direction(app, text_direction);
render_object.as_object()
}
fn update_render_object(
&self,
app: &mut App,
context: BuildContext,
render_object: RenderHandle<RenderIndexedStack>,
) {
self.debug_check_index();
render_object.set_index(app, self.index);
render_object.set_fit(app, self.fit);
render_object.set_clip_behavior(app, self.clip_behavior);
render_object.set_alignment(app, self.alignment);
let text_direction = Stack::resolved_text_direction(self.text_direction, app, context);
render_object.set_text_direction(app, text_direction);
}
}
impl MultiChildRenderObjectWidget for RawIndexedStack {
fn children(&self) -> &[WidgetRef] {
&self.children
}
}
#[derive(Debug)]
pub(crate) struct VisibilityScope {
pub(crate) is_visible: bool,
pub(crate) child: WidgetRef,
}
impl InheritedWidget for VisibilityScope {
fn child(&self) -> &WidgetRef {
&self.child
}
fn update_should_notify(&self, old_widget: &VisibilityScope) -> bool {
self.is_visible != old_widget.is_visible
}
}
#[cfg(test)]
mod tests {
use std::any::Any;
use std::cell::Cell;
use std::rc::Rc;
use waft_embedder::{Color, Offset, Radius, Size};
use waft_foundation::{App, AppCell, Handle};
use waft_gestures::{HitTestEntry, HitTestResult, PointerDownEvent, PointerEvent};
use waft_rendering::{
AnyRenderBox, BoxHitTestEntry, BoxHitTestResult, BoxParentData, ContainerBoxParentData,
ContainerRenderObjectMixin, ErasedRenderObject, RenderBox, RenderObject,
RenderObjectWithChildMixin,
};
use waft_services::SystemMouseCursors;
use super::*;
use crate::framework::{
GlobalKey, IntoWidget, State, StateData, StatefulWidget, downcast_widget,
};
use crate::test_harness::{Harness, padding_under_root};
fn render_object_under_root<T: RenderObject>(harness: &Harness, app: &App) -> RenderHandle<T> {
harness
.render_root(app)
.child(app)
.expect("a child")
.as_object()
.downcast::<T>(app)
.expect("the widget's render object")
}
fn child_offset<T: RenderObject + RenderObjectWithChildMixin>(
parent: RenderHandle<T>,
app: &App,
) -> Offset {
parent
.child(app)
.expect("a child")
.as_object()
.parent_data_of::<BoxParentData>(app)
.offset
}
fn sized(width: f64, height: f64) -> WidgetRef {
SizedBox {
width: Some(width),
height: Some(height),
..Default::default()
}
.into_widget()
}
#[derive(Debug)]
struct DirectionReader {
builds: Rc<Cell<u32>>,
seen: Rc<Cell<Option<TextDirection>>>,
}
impl StatelessWidget for DirectionReader {
fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
self.builds.set(self.builds.get() + 1);
self.seen.set(Some(Directionality::of(app, context)));
sized(10.0, 10.0)
}
}
#[test]
fn padding_insets_its_child_and_updates_in_place() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let padded = |inset: f64| {
Padding {
key: None,
padding: EdgeInsetsGeometry::all(inset),
child: Some(sized(10.0, 10.0)),
}
.into_widget()
};
let harness = Harness::mount(&mut app, padded(4.0));
harness.pump(&mut app);
let before = padding_under_root(&harness, &app);
assert_eq!(before.size(&app), Size::new(18.0, 18.0));
assert_eq!(
before.text_direction(&app),
None,
"no Directionality ancestor"
);
harness.set_child(&mut app, padded(6.0));
harness.pump(&mut app);
let after = padding_under_root(&harness, &app);
assert_eq!(after, before, "the same RenderPadding is reconfigured");
assert_eq!(after.padding(&app), EdgeInsetsGeometry::all(6.0));
assert_eq!(after.size(&app), Size::new(22.0, 22.0));
}
#[test]
fn padding_resolves_directional_insets_through_directionality() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let directional = |text_direction: TextDirection| {
Directionality {
key: None,
text_direction,
child: Padding {
key: None,
padding: EdgeInsetsGeometry::directional(8.0, 0.0, 0.0, 0.0),
child: Some(sized(10.0, 10.0)),
}
.into_widget(),
}
.into_widget()
};
let harness = Harness::mount(&mut app, directional(TextDirection::Rtl));
harness.pump(&mut app);
let padding = padding_under_root(&harness, &app);
assert_eq!(padding.text_direction(&app), Some(TextDirection::Rtl));
assert_eq!(
child_offset(padding, &app),
Offset::new(0.0, 0.0),
"start is the right"
);
harness.set_child(&mut app, directional(TextDirection::Ltr));
harness.pump(&mut app);
assert_eq!(padding.text_direction(&app), Some(TextDirection::Ltr));
assert_eq!(
child_offset(padding, &app),
Offset::new(8.0, 0.0),
"start is the left"
);
}
#[test]
fn center_positions_its_child_in_the_middle() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
Center {
child: Some(sized(10.0, 10.0)),
..Default::default()
}
.into_widget(),
);
harness.pump(&mut app);
let positioned = render_object_under_root::<RenderPositionedBox>(&harness, &app);
assert_eq!(positioned.alignment(&app), AlignmentGeometry::CENTER);
assert_eq!(
positioned.size(&app),
Size::new(300.0, 200.0),
"fills the root"
);
assert_eq!(child_offset(positioned, &app), Offset::new(145.0, 95.0));
}
#[test]
fn align_positions_its_child_and_updates_in_place() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let aligned = |alignment: AlignmentGeometry, width_factor: Option<f64>| {
Align {
alignment,
width_factor,
child: Some(sized(10.0, 10.0)),
..Default::default()
}
.into_widget()
};
let harness = Harness::mount(&mut app, aligned(AlignmentGeometry::BOTTOM_RIGHT, None));
harness.pump(&mut app);
let positioned = render_object_under_root::<RenderPositionedBox>(&harness, &app);
assert_eq!(child_offset(positioned, &app), Offset::new(290.0, 190.0));
harness.set_child(&mut app, aligned(AlignmentGeometry::TOP_LEFT, Some(2.0)));
harness.pump(&mut app);
let same = render_object_under_root::<RenderPositionedBox>(&harness, &app);
assert_eq!(
same, positioned,
"the same RenderPositionedBox is reconfigured"
);
assert_eq!(
positioned.size(&app),
Size::new(20.0, 200.0),
"twice the child's width"
);
assert_eq!(child_offset(positioned, &app), Offset::new(0.0, 0.0));
}
#[test]
fn sized_box_constrains_its_child() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(&mut app, sized(50.0, 20.0));
harness.pump(&mut app);
let constrained = render_object_under_root::<RenderConstrainedBox>(&harness, &app);
assert_eq!(
constrained.additional_constraints(&app),
BoxConstraints::tight_for(Some(50.0), Some(20.0))
);
assert_eq!(constrained.size(&app), Size::new(50.0, 20.0));
harness.set_child(&mut app, SizedBox::expand().into_widget());
harness.pump(&mut app);
assert_eq!(
constrained.size(&app),
Size::new(300.0, 200.0),
"as large as the root allows"
);
harness.set_child(&mut app, SizedBox::shrink().into_widget());
harness.pump(&mut app);
assert_eq!(constrained.size(&app), Size::new(0.0, 0.0));
}
#[test]
fn sized_box_named_constructors_match_dart() {
let square = SizedBox::square(Some(7.0));
assert_eq!((square.width, square.height), (Some(7.0), Some(7.0)));
let from_size = SizedBox::from_size(Some(Size::new(3.0, 4.0)));
assert_eq!((from_size.width, from_size.height), (Some(3.0), Some(4.0)));
let unspecified = SizedBox::from_size(None);
assert_eq!((unspecified.width, unspecified.height), (None, None));
assert!(format!("{:?}", SizedBox::expand()).starts_with("SizedBox.expand"));
assert!(format!("{:?}", SizedBox::shrink()).starts_with("SizedBox.shrink"));
assert!(format!("{square:?}").starts_with("SizedBox {"));
}
#[test]
fn constrained_box_imposes_its_constraints() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
ConstrainedBox {
key: None,
constraints: BoxConstraints::new().min_width(120.0).min_height(30.0),
child: Some(sized(10.0, 10.0)),
}
.into_widget(),
);
harness.pump(&mut app);
let constrained = render_object_under_root::<RenderConstrainedBox>(&harness, &app);
assert_eq!(constrained.size(&app), Size::new(120.0, 30.0));
}
#[test]
fn listener_receives_a_pointer_down_through_its_render_object() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let downs = Rc::new(Cell::new(0));
let seen_at = Rc::new(Cell::new(Offset::ZERO));
let harness = Harness::mount(
&mut app,
Listener {
on_pointer_down: Some(Rc::new({
let downs = Rc::clone(&downs);
let seen_at = Rc::clone(&seen_at);
move |_app: &mut App, event: PointerDownEvent| {
downs.set(downs.get() + 1);
seen_at.set(event.position);
}
})),
behavior: HitTestBehavior::Opaque,
child: Some(sized(10.0, 10.0)),
..Default::default()
}
.into_widget(),
);
harness.pump(&mut app);
let listener = render_object_under_root::<RenderPointerListener>(&harness, &app);
assert!(listener.on_pointer_down(&app).is_some());
assert!(listener.on_pointer_up(&app).is_none());
let event = PointerEvent::Down(PointerDownEvent {
position: Offset::new(3.0, 4.0),
..PointerDownEvent::default()
});
let entry = BoxHitTestEntry::new(listener.as_box(), Offset::new(3.0, 4.0));
listener.as_box().handle_event(&mut app, &event, &entry);
assert_eq!(downs.get(), 1);
assert_eq!(seen_at.get(), Offset::new(3.0, 4.0));
harness.set_child(
&mut app,
Listener {
child: Some(sized(10.0, 10.0)),
..Default::default()
}
.into_widget(),
);
harness.pump(&mut app);
assert!(
listener.on_pointer_down(&app).is_none(),
"the callback is cleared in place"
);
listener.as_box().handle_event(&mut app, &event, &entry);
assert_eq!(downs.get(), 1);
}
#[test]
fn mouse_region_forwards_its_configuration() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
MouseRegion {
on_enter: Some(Rc::new(|_app, _event| {})),
cursor: Rc::new(SystemMouseCursors::CLICK),
opaque: false,
hit_test_behavior: Some(HitTestBehavior::Translucent),
child: Some(sized(10.0, 10.0)),
..Default::default()
}
.into_widget(),
);
harness.pump(&mut app);
let region = render_object_under_root::<RenderMouseRegion>(&harness, &app);
assert!(region.on_enter(&app).is_some());
assert!(region.on_hover(&app).is_none());
assert!(!region.cursor(&app).is_defer());
assert!(!region.opaque(&app));
assert_eq!(
region.hit_test_behavior(&app),
Some(HitTestBehavior::Translucent)
);
harness.set_child(
&mut app,
MouseRegion {
child: Some(sized(10.0, 10.0)),
..Default::default()
}
.into_widget(),
);
harness.pump(&mut app);
assert!(region.on_enter(&app).is_none());
assert!(region.cursor(&app).is_defer());
assert!(region.opaque(&app));
assert_eq!(
region.hit_test_behavior(&app),
Some(HitTestBehavior::Opaque)
);
}
#[test]
fn opacity_forwards_its_value() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let faded = |opacity: f64| {
Opacity {
key: None,
opacity,
child: Some(sized(10.0, 10.0)),
}
.into_widget()
};
let harness = Harness::mount(&mut app, faded(0.5));
harness.pump(&mut app);
let opacity = render_object_under_root::<RenderOpacity>(&harness, &app);
assert_eq!(opacity.opacity(&app), 0.5);
harness.set_child(&mut app, faded(0.25));
harness.pump(&mut app);
assert_eq!(opacity.opacity(&app), 0.25);
}
#[test]
fn nested_opacities_appearing_together_paint_inside_a_clip() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let nested = |outer: f64, inner: f64| {
Opacity::new(outer)
.child(
ClipRRect::new()
.border_radius(crate::BorderRadius::circular(4.0).into())
.child(Opacity::new(inner).child(sized(20.0, 20.0))),
)
.into_widget()
};
let harness = Harness::mount(&mut app, nested(1.0, 1.0));
harness.pump(&mut app);
harness.set_child(&mut app, nested(0.0, 0.0));
harness.pump(&mut app);
harness.set_child(&mut app, nested(1.0, 1.0));
harness.pump(&mut app);
}
#[test]
fn repaint_boundary_isolates_repaints() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
RepaintBoundary {
key: None,
child: Some(sized(10.0, 10.0)),
}
.into_widget(),
);
harness.pump(&mut app);
let boundary = render_object_under_root::<RenderRepaintBoundary>(&harness, &app);
assert!(boundary.as_object().is_repaint_boundary(&app));
assert_eq!(boundary.size(&app), Size::new(10.0, 10.0));
}
#[test]
fn directionality_of_resolves_through_the_tree_and_notifies_on_change() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let builds = Rc::new(Cell::new(0));
let seen = Rc::new(Cell::new(None));
let reader: WidgetRef = DirectionReader {
builds: Rc::clone(&builds),
seen: Rc::clone(&seen),
}
.into_widget();
let directional = |text_direction: TextDirection| {
Directionality {
key: None,
text_direction,
child: reader.clone(),
}
.into_widget()
};
let harness = Harness::mount(&mut app, directional(TextDirection::Ltr));
harness.pump(&mut app);
assert_eq!(builds.get(), 1);
assert_eq!(seen.get(), Some(TextDirection::Ltr));
harness.set_child(&mut app, directional(TextDirection::Ltr));
harness.pump(&mut app);
assert_eq!(builds.get(), 1, "an equal direction does not notify");
harness.set_child(&mut app, directional(TextDirection::Rtl));
harness.pump(&mut app);
assert_eq!(
builds.get(),
2,
"a changed direction rebuilds the dependent"
);
assert_eq!(seen.get(), Some(TextDirection::Rtl));
}
#[test]
fn builder_builds_with_its_context() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
Builder {
key: None,
builder: Rc::new(|_app, context| {
let widget = context.widget(_app).clone();
assert!(
downcast_widget::<Builder>(&*widget).is_some(),
"the context is the Builder's own element"
);
Padding {
key: None,
padding: EdgeInsetsGeometry::all(2.0),
child: Some(sized(10.0, 10.0)),
}
.into_widget()
}),
}
.into_widget(),
);
harness.pump(&mut app);
let padding = padding_under_root(&harness, &app);
assert_eq!(padding.padding(&app), EdgeInsetsGeometry::all(2.0));
assert_eq!(padding.size(&app), Size::new(14.0, 14.0));
}
fn container_children<T: ContainerRenderObjectMixin<ChildType = AnyRenderBox>>(
parent: RenderHandle<T>,
app: &App,
) -> Vec<AnyRenderBox> {
let mut children = Vec::new();
let mut child = parent.first_child(app);
while let Some(current) = child {
children.push(current);
child = parent.child_after(app, current);
}
children
}
fn flex_offset(child: AnyRenderBox, app: &App) -> Offset {
child
.as_object()
.parent_data_of::<FlexParentData>(app)
.offset()
}
fn stack_offset(child: AnyRenderBox, app: &App) -> Offset {
child
.as_object()
.parent_data_of::<StackParentData>(app)
.offset()
}
fn hit_boxes(result: &HitTestResult) -> Vec<AnyRenderBox> {
result
.path()
.iter()
.filter_map(|entry: &HitTestEntry| {
let target: &dyn Any = entry.target();
target
.downcast_ref::<BoxHitTestEntry>()
.map(BoxHitTestEntry::target)
})
.collect()
}
fn ltr(child: WidgetRef) -> WidgetRef {
Directionality::new(TextDirection::Ltr, child).into_widget()
}
fn opaque(width: f64, height: f64) -> WidgetRef {
Listener {
behavior: HitTestBehavior::Opaque,
child: Some(sized(width, height)),
..Default::default()
}
.into_widget()
}
fn tile_state(harness: &Harness, app: &App, key: &GlobalKey) -> Option<Handle<TileState>> {
harness
.owner
.global_key_element(app, key.identity())
.and_then(|element| element.state_handle::<TileState>(app))
}
#[derive(Debug)]
struct Tile {
key: Option<KeyRef>,
width: f64,
}
impl Tile {
fn keyed(key: &GlobalKey, width: f64) -> WidgetRef {
Tile {
key: Some(Rc::new(key.clone())),
width,
}
.into_widget()
}
}
impl StatefulWidget for Tile {
type State = TileState;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_state(&self) -> TileState {
TileState {
state: StateData::new(),
builds: 0,
}
}
}
struct TileState {
state: StateData<Tile>,
builds: u32,
}
impl State for TileState {
type Widget = Tile;
crate::state_accessors!();
fn build(self: Handle<Self>, app: &mut App, _context: BuildContext) -> WidgetRef {
app.get_mut(self).builds += 1;
let width = self.widget(app).width;
sized(width, 20.0)
}
}
#[test]
fn row_lays_out_fixed_and_expanded_children() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
ltr(Row::new()
.children([
sized(50.0, 20.0),
Expanded::new(sized(10.0, 20.0)).into_widget(),
sized(30.0, 20.0),
])
.into_widget()),
);
harness.pump(&mut app);
let row = render_object_under_root::<RenderFlex>(&harness, &app);
assert_eq!(row.size(&app), Size::new(300.0, 20.0), "mainAxisSize.max");
let children = container_children(row, &app);
assert_eq!(children.len(), 3);
assert_eq!(children[0].size(&app), Size::new(50.0, 20.0));
assert_eq!(
children[1].size(&app),
Size::new(220.0, 20.0),
"the expanded child takes the free space, tightly"
);
assert_eq!(children[2].size(&app), Size::new(30.0, 20.0));
assert_eq!(flex_offset(children[0], &app), Offset::new(0.0, 0.0));
assert_eq!(flex_offset(children[1], &app), Offset::new(50.0, 0.0));
assert_eq!(flex_offset(children[2], &app), Offset::new(270.0, 0.0));
}
#[test]
fn a_loose_flexible_child_keeps_its_own_size() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
ltr(Row::new()
.children([
sized(50.0, 20.0),
Flexible::new(sized(10.0, 20.0))
.fit(FlexFit::Loose)
.into_widget(),
])
.into_widget()),
);
harness.pump(&mut app);
let row = render_object_under_root::<RenderFlex>(&harness, &app);
let children = container_children(row, &app);
assert_eq!(
children[1].size(&app),
Size::new(10.0, 20.0),
"FlexFit::Loose only offers the space, it does not force it"
);
assert_eq!(flex_offset(children[1], &app), Offset::new(50.0, 0.0));
harness.set_child(
&mut app,
ltr(Row::new()
.children([
sized(50.0, 20.0),
Flexible::new(sized(10.0, 20.0))
.fit(FlexFit::Tight)
.into_widget(),
])
.into_widget()),
);
harness.pump(&mut app);
let children = container_children(row, &app);
assert_eq!(
children[1].size(&app),
Size::new(250.0, 20.0),
"FlexFit::Tight fills the offered space"
);
}
#[test]
fn a_column_space_between_spreads_its_children() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
Column::new()
.main_axis_alignment(MainAxisAlignment::SpaceBetween)
.children([sized(10.0, 20.0), sized(10.0, 30.0)])
.into_widget(),
);
harness.pump(&mut app);
let column = render_object_under_root::<RenderFlex>(&harness, &app);
assert_eq!(
column.text_direction(&app),
None,
"a centred column needs no text direction"
);
assert_eq!(column.size(&app), Size::new(10.0, 200.0));
let children = container_children(column, &app);
assert_eq!(flex_offset(children[0], &app), Offset::new(0.0, 0.0));
assert_eq!(flex_offset(children[1], &app), Offset::new(0.0, 170.0));
}
#[test]
fn a_baseline_aligned_row_puts_texts_of_different_sizes_on_one_baseline() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let binding = waft_painting::PaintingBinding::instance(&mut app);
binding.install_fonts(&mut app, |fonts| {
fonts.add_source(valo_system_fonts::SystemFonts::load());
});
let text = |data: &str, font_size: f64| {
crate::Text::new(data)
.style(
waft_painting::TextStyle::new()
.font_size(font_size)
.height(1.0),
)
.into_widget()
};
let harness = Harness::mount(
&mut app,
ltr(Row::new()
.cross_axis_alignment(CrossAxisAlignment::Baseline)
.text_baseline(TextBaseline::Alphabetic)
.children([
text("big text", 52.0),
text("one\ntwo\nthree\nfour\nfive\nsix\nseven", 12.0),
])
.into_widget()),
);
harness.pump(&mut app);
let row = render_object_under_root::<RenderFlex>(&harness, &app);
let texts = container_children(row, &app);
let mut above_baseline = |text: AnyRenderBox| {
text.get_distance_to_baseline(&mut app, TextBaseline::Alphabetic, true)
.expect("a text has a baseline")
};
let (above_baseline_1, above_baseline_2) =
(above_baseline(texts[0]), above_baseline(texts[1]));
let below_baseline_1 = texts[0].size(&app).height() - above_baseline_1;
let below_baseline_2 = texts[1].size(&app).height() - above_baseline_2;
assert!(above_baseline_1 > above_baseline_2 && below_baseline_2 > below_baseline_1);
assert!(row.size(&app).height() > texts[0].size(&app).height());
assert!(row.size(&app).height() > texts[1].size(&app).height());
assert_eq!(row.size(&app).height(), above_baseline_1 + below_baseline_2);
assert_eq!(flex_offset(texts[0], &app).dy(), 0.0);
assert_eq!(
flex_offset(texts[1], &app).dy(),
above_baseline_1 - above_baseline_2
);
}
#[test]
#[should_panic(expected = "textBaseline is required")]
fn a_baseline_aligned_row_needs_a_text_baseline() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
ltr(Row::new()
.cross_axis_alignment(CrossAxisAlignment::Baseline)
.children([sized(10.0, 10.0)])
.into_widget()),
);
harness.pump(&mut app);
}
#[test]
fn reordering_keyed_children_moves_elements_and_render_children() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let (first, second, third) = (GlobalKey::new(), GlobalKey::new(), GlobalKey::new());
let row = |order: [&GlobalKey; 3]| {
let widths = [10.0, 20.0, 30.0];
let by_key = |key: &GlobalKey| {
let index = [&first, &second, &third]
.iter()
.position(|candidate| candidate.identity() == key.identity())
.expect("one of the three keys");
Tile::keyed(key, widths[index])
};
ltr(Row::new().children(order.map(by_key)).into_widget())
};
let harness = Harness::mount(&mut app, row([&first, &second, &third]));
harness.pump(&mut app);
let flex = render_object_under_root::<RenderFlex>(&harness, &app);
let before = container_children(flex, &app);
assert_eq!(
before
.iter()
.map(|child| child.size(&app).width())
.collect::<Vec<f64>>(),
vec![10.0, 20.0, 30.0]
);
let state = tile_state(&harness, &app, &first).expect("the first tile's state");
assert_eq!(app.get(state).builds, 1);
harness.set_child(&mut app, row([&third, &first, &second]));
harness.pump(&mut app);
assert_eq!(
tile_state(&harness, &app, &first),
Some(state),
"the moved element keeps its State"
);
assert_eq!(
app.get(state).builds,
2,
"it was rebuilt in place, not created afresh"
);
let after = container_children(flex, &app);
assert_eq!(
after,
vec![before[2], before[0], before[1]],
"the same render objects, in the new order"
);
assert_eq!(flex_offset(after[0], &app), Offset::new(0.0, 0.0));
assert_eq!(flex_offset(after[1], &app), Offset::new(30.0, 0.0));
assert_eq!(flex_offset(after[2], &app), Offset::new(40.0, 0.0));
}
#[test]
fn a_child_can_be_added_and_removed_in_the_middle() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let (first, middle, last) = (GlobalKey::new(), GlobalKey::new(), GlobalKey::new());
let ends = || {
ltr(Row::new()
.children([Tile::keyed(&first, 10.0), Tile::keyed(&last, 30.0)])
.into_widget())
};
let all_three = || {
ltr(Row::new()
.children([
Tile::keyed(&first, 10.0),
Tile::keyed(&middle, 20.0),
Tile::keyed(&last, 30.0),
])
.into_widget())
};
let harness = Harness::mount(&mut app, ends());
harness.pump(&mut app);
let flex = render_object_under_root::<RenderFlex>(&harness, &app);
let before = container_children(flex, &app);
assert_eq!(before.len(), 2);
harness.set_child(&mut app, all_three());
harness.pump(&mut app);
let inserted = container_children(flex, &app);
assert_eq!(inserted.len(), 3);
assert_eq!(inserted[0], before[0], "the first child was kept");
assert_eq!(inserted[2], before[1], "the last child was kept");
assert_eq!(inserted[1].size(&app).width(), 20.0);
assert_eq!(flex_offset(inserted[2], &app), Offset::new(30.0, 0.0));
harness.set_child(&mut app, ends());
harness.pump(&mut app);
let removed = container_children(flex, &app);
assert_eq!(removed, vec![before[0], before[1]]);
assert_eq!(flex_offset(removed[1], &app), Offset::new(10.0, 0.0));
assert!(
tile_state(&harness, &app, &middle).is_none(),
"the removed child was unmounted"
);
}
#[test]
fn a_wrap_starts_a_new_run_for_a_child_that_does_not_fit() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
ltr(Align::new()
.alignment(AlignmentGeometry::TOP_LEFT)
.child(
Wrap::new()
.spacing(10.0)
.run_spacing(10.0)
.children((0..4).map(|_| sized(80.0, 40.0))),
)
.into_widget()),
);
harness.pump(&mut app);
let align = render_object_under_root::<RenderPositionedBox>(&harness, &app);
let wrap = align
.child(&app)
.expect("the wrap")
.as_object()
.downcast::<RenderWrap>(&app)
.expect("a RenderWrap");
assert_eq!(wrap.text_direction(&app), Some(TextDirection::Ltr));
assert_eq!(wrap.size(&app), Size::new(260.0, 90.0));
let offsets: Vec<Offset> = container_children(wrap, &app)
.into_iter()
.map(|child| child.box_parent_data(&app).offset)
.collect();
assert_eq!(
offsets,
[
Offset::new(0.0, 0.0),
Offset::new(90.0, 0.0),
Offset::new(180.0, 0.0),
Offset::new(0.0, 50.0)
]
);
}
#[test]
fn a_stack_sizes_its_non_positioned_child_by_its_fit() {
for (fit, child_size, stack_size) in [
(StackFit::Loose, Size::new(10.0, 5.0), Size::new(20.0, 10.0)),
(
StackFit::Expand,
Size::new(100.0, 80.0),
Size::new(100.0, 80.0),
),
(
StackFit::Passthrough,
Size::new(20.0, 10.0),
Size::new(20.0, 10.0),
),
] {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
ltr(ConstrainedBox::new(
BoxConstraints::new()
.min_width(20.0)
.max_width(100.0)
.min_height(10.0)
.max_height(80.0),
)
.child(Stack::new().fit(fit).children([
sized(10.0, 5.0),
Positioned::fill(sized(1.0, 1.0)).into_widget(),
]))
.into_widget()),
);
harness.pump(&mut app);
let constrained = render_object_under_root::<RenderConstrainedBox>(&harness, &app);
let stack: RenderHandle<RenderStack> = constrained
.child(&app)
.expect("a child")
.as_object()
.downcast(&app)
.expect("a RenderStack");
assert_eq!(stack.size(&app), stack_size, "{fit:?}");
let children = container_children(stack, &app);
assert_eq!(children[0].size(&app), child_size, "{fit:?}");
assert_eq!(stack_offset(children[0], &app), Offset::ZERO, "{fit:?}");
assert_eq!(
children[1].size(&app),
stack_size,
"Positioned.fill fills the stack ({fit:?})"
);
assert_eq!(stack_offset(children[1], &app), Offset::ZERO, "{fit:?}");
}
}
#[test]
fn an_indexed_stack_lays_out_every_child_but_shows_one() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let stack = |index: i32| {
ltr(IndexedStack::new()
.index(Some(index))
.children([opaque(40.0, 30.0), opaque(20.0, 50.0)])
.into_widget())
};
let harness = Harness::mount(&mut app, stack(1));
harness.pump(&mut app);
let indexed = render_object_under_root::<RenderIndexedStack>(&harness, &app);
assert_eq!(indexed.index(&app), Some(1));
let children = container_children(indexed, &app);
assert_eq!(children.len(), 2, "every child is laid out");
assert_eq!(children[0].size(&app), Size::new(40.0, 30.0));
assert_eq!(children[1].size(&app), Size::new(20.0, 50.0));
assert_eq!(
indexed.size(&app),
Size::new(40.0, 50.0),
"as big as the largest child"
);
let mut result = HitTestResult::new();
assert!(!indexed.hit_test(
&mut app,
&mut BoxHitTestResult::wrap(&mut result),
Offset::new(30.0, 10.0)
));
let mut result = HitTestResult::new();
assert!(indexed.hit_test(
&mut app,
&mut BoxHitTestResult::wrap(&mut result),
Offset::new(10.0, 10.0)
));
assert_eq!(hit_boxes(&result), vec![children[1], indexed.as_box()]);
harness.set_child(&mut app, stack(0));
harness.pump(&mut app);
assert_eq!(indexed.index(&app), Some(0));
let mut result = HitTestResult::new();
assert!(indexed.hit_test(
&mut app,
&mut BoxHitTestResult::wrap(&mut result),
Offset::new(30.0, 10.0)
));
assert_eq!(hit_boxes(&result), vec![children[0], indexed.as_box()]);
}
#[test]
fn clip_rect_and_clip_rrect_configure_their_render_objects() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let clipped = |behavior: Clip| {
ClipRect::new()
.clip_behavior(behavior)
.child(sized(10.0, 10.0))
.into_widget()
};
let harness = Harness::mount(&mut app, clipped(Clip::HardEdge));
harness.pump(&mut app);
let clip = render_object_under_root::<RenderClipRect>(&harness, &app);
assert_eq!(clip.clip_behavior(&app), Clip::HardEdge, "Dart's default");
assert!(clip.clipper(&app).is_none());
harness.set_child(&mut app, clipped(Clip::AntiAliasWithSaveLayer));
harness.pump(&mut app);
assert_eq!(
render_object_under_root::<RenderClipRect>(&harness, &app),
clip,
"the same RenderClipRect is reconfigured"
);
assert_eq!(clip.clip_behavior(&app), Clip::AntiAliasWithSaveLayer);
assert_eq!(clip.size(&app), Size::new(10.0, 10.0), "sized to its child");
}
#[test]
fn clip_rrect_resolves_its_radius_through_directionality() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let corner = Radius::circular(4.0);
let radius = BorderRadiusGeometry::directional(corner, corner, corner, corner);
let rounded = |text_direction: TextDirection| {
Directionality::new(
text_direction,
ClipRRect::new()
.border_radius(radius)
.child(sized(10.0, 10.0)),
)
.into_widget()
};
let harness = Harness::mount(&mut app, rounded(TextDirection::Rtl));
harness.pump(&mut app);
let clip = render_object_under_root::<RenderClipRRect>(&harness, &app);
assert_eq!(clip.clip_behavior(&app), Clip::AntiAlias, "Dart's default");
assert_eq!(clip.border_radius(&app), radius);
assert_eq!(clip.text_direction(&app), Some(TextDirection::Rtl));
harness.set_child(&mut app, rounded(TextDirection::Ltr));
harness.pump(&mut app);
assert_eq!(clip.text_direction(&app), Some(TextDirection::Ltr));
}
#[test]
fn transform_named_constructors_match_dart() {
assert_eq!(Transform::rotate(0.0).transform, Matrix4::IDENTITY);
assert_eq!(
Transform::rotate(std::f64::consts::FRAC_PI_2).transform,
Matrix4::from_flutter_array(&[
0.0, 1.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
]),
"a quarter turn is exact"
);
assert_eq!(
Transform::rotate(0.0).alignment,
Some(AlignmentGeometry::CENTER)
);
let translated = Transform::translate(Offset::new(3.0, 4.0));
assert_eq!(translated.transform, Matrix4::translation(3.0, 4.0));
assert_eq!(translated.alignment, None, "Dart leaves it unset");
assert_eq!(translated.origin, None);
assert_eq!(
Transform::scale(Some(0.5), None, None).transform,
Matrix4::scale(0.5, 0.5)
);
assert_eq!(
Transform::scale(None, Some(2.0), None).transform,
Matrix4::scale(2.0, 1.0),
"the unset axis stays at 1.0"
);
assert_eq!(
Transform::flip(true, false).transform,
Matrix4::scale(-1.0, 1.0)
);
assert_eq!(
Transform::flip(true, false).alignment,
Some(AlignmentGeometry::CENTER)
);
}
#[test]
fn transform_moves_hit_tests_with_its_child() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let translated = |transform_hit_tests: bool| {
Transform::translate(Offset::new(20.0, 0.0))
.transform_hit_tests(transform_hit_tests)
.child(opaque(10.0, 10.0))
.into_widget()
};
let harness = Harness::mount(&mut app, translated(true));
harness.pump(&mut app);
let transform = render_object_under_root::<RenderTransform>(&harness, &app);
assert!(transform.transform_hit_tests(&app));
let mut result = HitTestResult::new();
assert!(
transform.hit_test(
&mut app,
&mut BoxHitTestResult::wrap(&mut result),
Offset::new(25.0, 5.0),
),
"the child is hit where it is painted"
);
let mut result = HitTestResult::new();
assert!(!transform.hit_test(
&mut app,
&mut BoxHitTestResult::wrap(&mut result),
Offset::new(5.0, 5.0),
));
harness.set_child(&mut app, translated(false));
harness.pump(&mut app);
assert!(!transform.transform_hit_tests(&app));
let mut result = HitTestResult::new();
assert!(
transform.hit_test(
&mut app,
&mut BoxHitTestResult::wrap(&mut result),
Offset::new(5.0, 5.0),
),
"the child is hit where it was laid out"
);
}
#[test]
fn fractional_translation_forwards_its_configuration() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
FractionalTranslation::new(Offset::new(0.25, 0.0))
.transform_hit_tests(false)
.child(sized(40.0, 10.0))
.into_widget(),
);
harness.pump(&mut app);
let translation = render_object_under_root::<RenderFractionalTranslation>(&harness, &app);
assert_eq!(translation.translation(&app), Offset::new(0.25, 0.0));
assert!(!translation.transform_hit_tests(&app));
harness.set_child(
&mut app,
FractionalTranslation::new(Offset::new(0.5, 0.5))
.child(sized(40.0, 10.0))
.into_widget(),
);
harness.pump(&mut app);
assert_eq!(translation.translation(&app), Offset::new(0.5, 0.5));
assert!(
translation.transform_hit_tests(&app),
"back to Dart's default"
);
}
#[test]
fn ignore_pointer_and_absorb_pointer_stop_hit_tests_differently() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
IgnorePointer::new().child(opaque(10.0, 10.0)).into_widget(),
);
harness.pump(&mut app);
let ignoring = render_object_under_root::<RenderIgnorePointer>(&harness, &app);
assert!(ignoring.ignoring(&app), "Dart's default");
let mut result = HitTestResult::new();
assert!(!ignoring.hit_test(
&mut app,
&mut BoxHitTestResult::wrap(&mut result),
Offset::new(5.0, 5.0),
));
assert!(hit_boxes(&result).is_empty());
harness.set_child(
&mut app,
IgnorePointer::new()
.ignoring(false)
.child(opaque(10.0, 10.0))
.into_widget(),
);
harness.pump(&mut app);
assert!(!ignoring.ignoring(&app));
let mut result = HitTestResult::new();
assert!(ignoring.hit_test(
&mut app,
&mut BoxHitTestResult::wrap(&mut result),
Offset::new(5.0, 5.0),
));
assert!(!hit_boxes(&result).is_empty(), "the child answers again");
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
AbsorbPointer::new().child(opaque(10.0, 10.0)).into_widget(),
);
harness.pump(&mut app);
let absorbing = render_object_under_root::<RenderAbsorbPointer>(&harness, &app);
assert!(absorbing.absorbing(&app), "Dart's default");
let mut result = HitTestResult::new();
assert!(
absorbing.hit_test(
&mut app,
&mut BoxHitTestResult::wrap(&mut result),
Offset::new(5.0, 5.0),
),
"absorbing itself answers the hit"
);
assert!(
hit_boxes(&result).is_empty(),
"the subtree is not reached, and nothing is added to the path"
);
}
#[test]
fn limited_box_caps_an_unbounded_child() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let limited = |max_width: f64| {
ltr(Row::new()
.children([LimitedBox::new()
.max_width(max_width)
.child(SizedBox::expand())
.into_widget()])
.into_widget())
};
let harness = Harness::mount(&mut app, limited(50.0));
harness.pump(&mut app);
let flex = render_object_under_root::<RenderFlex>(&harness, &app);
let child = container_children(flex, &app)[0]
.as_object()
.downcast::<RenderLimitedBox>(&app)
.expect("a RenderLimitedBox");
assert_eq!(child.max_width(&app), 50.0);
assert_eq!(child.max_height(&app), f64::INFINITY, "Dart's default");
assert_eq!(
child.size(&app).width(),
50.0,
"the row's unbounded width is capped"
);
harness.set_child(&mut app, limited(30.0));
harness.pump(&mut app);
assert_eq!(child.max_width(&app), 30.0);
assert_eq!(child.size(&app).width(), 30.0);
}
#[test]
fn colored_box_forwards_its_color_and_anti_aliasing() {
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 harness = Harness::mount(
&mut app,
ColoredBox::new(red).child(sized(10.0, 10.0)).into_widget(),
);
harness.pump(&mut app);
let colored = render_object_under_root::<RenderColoredBox>(&harness, &app);
assert_eq!(colored.color(&app), red);
assert!(colored.is_anti_alias(&app), "Dart's default");
harness.set_child(
&mut app,
ColoredBox::new(blue)
.is_anti_alias(false)
.child(sized(10.0, 10.0))
.into_widget(),
);
harness.pump(&mut app);
assert_eq!(colored.color(&app), blue, "the same box is reconfigured");
assert!(!colored.is_anti_alias(&app));
}
#[test]
fn offstage_takes_no_room_and_hides_its_child() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let staged = |offstage: bool| {
Offstage::new()
.offstage(offstage)
.child(opaque(10.0, 10.0))
.into_widget()
};
let harness = Harness::mount(&mut app, staged(true));
harness.pump(&mut app);
let offstage = render_object_under_root::<RenderOffstage>(&harness, &app);
assert!(offstage.offstage(&app), "Dart's default");
assert_eq!(offstage.size(&app), Size::ZERO, "no room in the parent");
let child = offstage.child(&app).expect("laid out as if in the tree");
assert_eq!(child.size(&app), Size::new(10.0, 10.0));
assert!(!offstage.paints_child(&app, child));
harness.set_child(&mut app, staged(false));
harness.pump(&mut app);
assert_eq!(offstage.size(&app), Size::new(10.0, 10.0));
assert!(offstage.paints_child(&app, child));
}
#[derive(Debug)]
struct RecordingPainter {
paints: Rc<Cell<u32>>,
painted: Rc<Cell<Size>>,
}
impl CustomPainter for RecordingPainter {
fn paint(&self, _app: &mut App, _canvas: &mut waft_embedder::Canvas, size: Size) {
self.paints.set(self.paints.get() + 1);
self.painted.set(size);
}
fn should_repaint(&self, _app: &App, _old_delegate: &dyn CustomPainter) -> bool {
true
}
fn as_any(&self) -> &dyn Any {
self
}
}
#[test]
fn custom_paint_sizes_itself_and_runs_its_painter() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let paints = Rc::new(Cell::new(0));
let painted = Rc::new(Cell::new(Size::ZERO));
let painter = || RecordingPainter {
paints: Rc::clone(&paints),
painted: Rc::clone(&painted),
};
let harness = Harness::mount(
&mut app,
CustomPaint::new()
.painter(painter())
.size(Size::new(40.0, 30.0))
.into_widget(),
);
harness.pump(&mut app);
let custom = render_object_under_root::<RenderCustomPaint>(&harness, &app);
assert_eq!(
custom.size(&app),
Size::new(40.0, 30.0),
"the preferred size, with no child"
);
assert_eq!(paints.get(), 1);
assert_eq!(painted.get(), Size::new(40.0, 30.0));
assert!(custom.foreground_painter(&app).is_none());
harness.set_child(
&mut app,
CustomPaint::new()
.foreground_painter(painter())
.child(sized(10.0, 10.0))
.into_widget(),
);
harness.pump(&mut app);
assert!(custom.painter(&app).is_none(), "the painter is cleared");
assert!(custom.foreground_painter(&app).is_some());
assert_eq!(
custom.size(&app),
Size::new(10.0, 10.0),
"the child's size wins"
);
assert_eq!(paints.get(), 2);
assert_eq!(painted.get(), Size::new(10.0, 10.0));
}
#[test]
#[cfg(debug_assertions)]
#[should_panic(expected = "Incorrect use of ParentDataWidget")]
fn a_flexible_under_a_non_flex_parent_asserts() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
Padding::new(EdgeInsetsGeometry::all(1.0))
.child(Flexible::new(sized(10.0, 10.0)))
.into_widget(),
);
harness.pump(&mut app);
}
#[test]
fn meta_data_reaches_its_render_object_and_updates() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
MetaData::new()
.meta_data(Rc::new(String::from("tag")))
.behavior(HitTestBehavior::Opaque)
.child(SizedBox::shrink())
.into_widget(),
);
harness.pump(&mut app);
let render_object = render_object_under_root::<RenderMetaData>(&harness, &app);
assert_eq!(
render_object
.meta_data(&app)
.and_then(|data| data.downcast::<String>().ok())
.as_deref(),
Some(&String::from("tag"))
);
harness.set_child(
&mut app,
MetaData::new().child(SizedBox::shrink()).into_widget(),
);
harness.pump(&mut app);
assert!(render_object.meta_data(&app).is_none());
}
#[test]
fn a_backdrop_filter_reaches_its_render_object_and_a_grouped_one_shares_the_group_key() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
BackdropFilter::filter(ImageFilter::blur(3.0, 3.0))
.child(SizedBox::shrink())
.into_widget(),
);
harness.pump(&mut app);
let render_object = render_object_under_root::<RenderBackdropFilter>(&harness, &app);
assert_eq!(render_object.filter(&app), ImageFilter::blur(3.0, 3.0));
assert!(render_object.backdrop_key(&app).is_none());
let key = BackdropKey::new();
harness.set_child(
&mut app,
BackdropGroup::new(
BackdropFilter::grouped(ImageFilterConfig::blur().sigma_x(2.0).sigma_y(2.0))
.child(SizedBox::shrink()),
)
.backdrop_key(key)
.into_widget(),
);
harness.pump(&mut app);
let inherited = harness
.render_root(&app)
.child(&app)
.expect("the filter under the group")
.as_object()
.downcast::<RenderBackdropFilter>(&app)
.expect("a RenderBackdropFilter");
assert_eq!(inherited.backdrop_key(&app), Some(key));
}
#[test]
fn a_sliver_padding_insets_its_child_on_every_side() {
use waft_embedder::TextDirection;
use waft_rendering::{
ContainerRenderObjectMixin, FixedViewportOffset, RenderSliver, RenderSliverPadding,
RenderSliverToBoxAdapter, RenderViewport, ScrollCacheExtent, ViewportOffset,
};
use crate::widgets::viewport::Viewport;
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let offset = FixedViewportOffset::zero(&mut app);
let tree = Directionality::new(
TextDirection::Ltr,
Viewport::new(offset.as_viewport_offset())
.scroll_cache_extent(ScrollCacheExtent::Pixels(0.0))
.slivers(vec![
SliverPadding::new(EdgeInsetsGeometry::only(10.0, 20.0, 30.0, 40.0))
.sliver(SliverToBoxAdapter::new().child(SizedBox::new().height(50.0)))
.into_widget(),
]),
)
.into_widget();
let harness = Harness::mount(&mut app, tree);
harness.pump(&mut app);
let padding = render_object_under_root::<RenderViewport>(&harness, &app)
.first_child(&app)
.expect("the padding")
.as_object()
.downcast::<RenderSliverPadding>(&app)
.expect("a RenderSliverPadding");
assert_eq!(padding.geometry(&app).scroll_extent, 110.0);
let adapter = padding
.child(&app)
.expect("the adapter")
.as_object()
.downcast::<RenderSliverToBoxAdapter>(&app)
.expect("a RenderSliverToBoxAdapter");
assert_eq!(
adapter.child(&app).expect("the box").size(&app).width(),
260.0
);
}
#[test]
fn fitted_box_scales_its_child_to_the_space_it_is_given() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
FittedBox::new().child(sized(600.0, 400.0)).into_widget(),
);
harness.pump(&mut app);
let fitted = render_object_under_root::<RenderFittedBox>(&harness, &app);
assert_eq!(
fitted.size(&app),
Size::new(300.0, 200.0),
"the 3:2 child fills the 300x200 root"
);
let child = fitted.child(&app).expect("a child");
assert_eq!(
child.size(&app),
Size::new(600.0, 400.0),
"the child lays out unconstrained"
);
assert_eq!(
child.local_to_global(&app, Offset::new(600.0, 400.0), Some(fitted.as_object())),
Offset::new(300.0, 200.0),
"half scale"
);
}
#[test]
fn constraints_transform_box_transforms_are_dart_s() {
let constraints = BoxConstraints::new()
.min_width(10.0)
.max_width(20.0)
.min_height(30.0)
.max_height(40.0);
assert_eq!(
ConstraintsTransformBox::unmodified(constraints),
constraints
);
assert_eq!(
ConstraintsTransformBox::unconstrained(constraints),
BoxConstraints::new()
);
assert_eq!(
ConstraintsTransformBox::width_unconstrained(constraints),
constraints.height_constraints()
);
assert_eq!(
ConstraintsTransformBox::height_unconstrained(constraints),
constraints.width_constraints()
);
assert_eq!(
ConstraintsTransformBox::max_height_unconstrained(constraints),
constraints.copy_with().max_height(f64::INFINITY)
);
assert_eq!(
ConstraintsTransformBox::max_width_unconstrained(constraints),
constraints.copy_with().max_width(f64::INFINITY)
);
assert_eq!(
ConstraintsTransformBox::max_unconstrained(constraints),
constraints
.copy_with()
.max_width(f64::INFINITY)
.max_height(f64::INFINITY)
);
}
#[test]
fn unconstrained_box_lets_its_child_size_itself_under_a_tight_parent() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let tight = |child: WidgetRef| {
SizedBox::new()
.width(50.0)
.height(50.0)
.child(child)
.into_widget()
};
let harness = Harness::mount(
&mut app,
tight(
UnconstrainedBox::new()
.child(sized(120.0, 30.0))
.into_widget(),
),
);
harness.pump(&mut app);
let unconstrained = render_object_under_root::<RenderConstrainedBox>(&harness, &app)
.child(&app)
.expect("the transform box")
.as_object()
.downcast::<RenderConstraintsTransformBox>(&app)
.expect("a RenderConstraintsTransformBox");
assert_eq!(
unconstrained.size(&app),
Size::new(50.0, 50.0),
"the box still obeys the tight parent"
);
assert_eq!(
unconstrained.child(&app).expect("a child").size(&app),
Size::new(120.0, 30.0),
"the child is laid out unconstrained"
);
assert_eq!(
unconstrained
.child(&app)
.expect("a child")
.constraints(&app),
BoxConstraints::new(),
"no axis keeps its constraints"
);
harness.set_child(
&mut app,
tight(
UnconstrainedBox::new()
.constrained_axis(Axis::Horizontal)
.child(sized(120.0, 30.0))
.into_widget(),
),
);
harness.pump(&mut app);
let horizontal = render_object_under_root::<RenderConstrainedBox>(&harness, &app)
.child(&app)
.expect("the transform box")
.as_object()
.downcast::<RenderConstraintsTransformBox>(&app)
.expect("a RenderConstraintsTransformBox");
assert_eq!(
horizontal.child(&app).expect("a child").constraints(&app),
BoxConstraints::new().min_width(50.0).max_width(50.0),
"the horizontal constraints are retained"
);
assert_eq!(
horizontal.child(&app).expect("a child").size(&app),
Size::new(50.0, 30.0)
);
}
#[test]
fn fractionally_sized_box_tightens_its_child_to_a_fraction() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
FractionallySizedBox::new()
.width_factor(0.5)
.height_factor(0.25)
.child(SizedBox::expand())
.into_widget(),
);
harness.pump(&mut app);
let fraction =
render_object_under_root::<RenderFractionallySizedOverflowBox>(&harness, &app);
assert_eq!(
fraction.child(&app).expect("a child").size(&app),
Size::new(150.0, 50.0),
"half the width and a quarter of the height of the root"
);
assert_eq!(fraction.size(&app), Size::new(150.0, 50.0));
}
#[test]
fn overflow_box_gives_its_child_its_own_constraints() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
OverflowBox::new()
.min_width(400.0)
.max_width(400.0)
.min_height(300.0)
.max_height(300.0)
.child(SizedBox::expand())
.into_widget(),
);
harness.pump(&mut app);
let overflow = render_object_under_root::<RenderConstrainedOverflowBox>(&harness, &app);
assert_eq!(
overflow.size(&app),
Size::new(300.0, 200.0),
"the box is as large as the root allows"
);
assert_eq!(
overflow.child(&app).expect("a child").size(&app),
Size::new(400.0, 300.0),
"the child overflows"
);
assert_eq!(child_offset(overflow, &app), Offset::new(-50.0, -50.0));
harness.set_child(
&mut app,
OverflowBox::new()
.fit(OverflowBoxFit::DeferToChild)
.max_width(40.0)
.max_height(20.0)
.child(SizedBox::expand())
.into_widget(),
);
harness.pump(&mut app);
assert_eq!(
render_object_under_root::<RenderConstrainedOverflowBox>(&harness, &app).size(&app),
Size::new(40.0, 20.0),
"deferring to the child takes the child's size"
);
}
#[test]
fn overflow_box_can_start_deferring_to_its_child() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
Center::new()
.child(
OverflowBox::new()
.fit(OverflowBoxFit::DeferToChild)
.max_width(40.0)
.max_height(20.0)
.child(SizedBox::expand()),
)
.into_widget(),
);
harness.pump(&mut app);
let center = render_object_under_root::<RenderPositionedBox>(&harness, &app);
assert_eq!(
center.child(&app).expect("the overflow box").size(&app),
Size::new(40.0, 20.0)
);
}
#[test]
fn sized_overflow_box_takes_its_requested_size() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
Directionality::new(
TextDirection::Ltr,
SizedOverflowBox::new(Size::new(40.0, 20.0)).child(sized(100.0, 60.0)),
)
.into_widget(),
);
harness.pump(&mut app);
let sized_overflow = render_object_under_root::<RenderSizedOverflowBox>(&harness, &app);
assert_eq!(sized_overflow.size(&app), Size::new(40.0, 20.0));
assert_eq!(
sized_overflow.child(&app).expect("a child").size(&app),
Size::new(100.0, 60.0),
"the original constraints reach the child"
);
}
#[test]
fn aspect_ratio_picks_the_widest_size_that_keeps_the_ratio() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(&mut app, AspectRatio::new(2.0).into_widget());
harness.pump(&mut app);
assert_eq!(
render_object_under_root::<RenderAspectRatio>(&harness, &app).size(&app),
Size::new(300.0, 150.0)
);
harness.set_child(&mut app, AspectRatio::new(0.5).into_widget());
harness.pump(&mut app);
assert_eq!(
render_object_under_root::<RenderAspectRatio>(&harness, &app).size(&app),
Size::new(100.0, 200.0),
"the height is clamped and the width follows from it"
);
}
#[test]
fn intrinsic_width_sizes_to_the_child_s_max_intrinsic_width() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
IntrinsicWidth::new().child(sized(40.0, 10.0)).into_widget(),
);
harness.pump(&mut app);
assert_eq!(
render_object_under_root::<RenderIntrinsicWidth>(&harness, &app).size(&app),
Size::new(40.0, 10.0)
);
harness.set_child(
&mut app,
IntrinsicWidth::new()
.step_width(30.0)
.child(sized(40.0, 10.0))
.into_widget(),
);
harness.pump(&mut app);
assert_eq!(
render_object_under_root::<RenderIntrinsicWidth>(&harness, &app).size(&app),
Size::new(60.0, 10.0),
"the width is snapped up to a multiple of the step"
);
harness.set_child(
&mut app,
IntrinsicWidth::new()
.step_width(0.0)
.child(sized(40.0, 10.0))
.into_widget(),
);
harness.pump(&mut app);
assert_eq!(
render_object_under_root::<RenderIntrinsicWidth>(&harness, &app).step_width(&app),
None,
"Dart's `_stepWidth` reads a zero step as no step"
);
}
#[test]
fn intrinsic_height_sizes_to_the_child_s_max_intrinsic_height() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
IntrinsicHeight::new()
.child(sized(40.0, 10.0))
.into_widget(),
);
harness.pump(&mut app);
assert_eq!(
render_object_under_root::<RenderIntrinsicHeight>(&harness, &app).size(&app),
Size::new(40.0, 10.0)
);
}
#[test]
fn baseline_shifts_its_child_down_to_the_requested_baseline() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
Baseline::new(30.0, TextBaseline::Alphabetic)
.child(sized(10.0, 10.0))
.into_widget(),
);
harness.pump(&mut app);
let baseline = render_object_under_root::<RenderBaseline>(&harness, &app);
assert_eq!(
baseline.size(&app),
Size::new(10.0, 30.0),
"a child with no baseline reports its bottom"
);
assert_eq!(child_offset(baseline, &app), Offset::new(0.0, 20.0));
}
#[test]
fn the_clip_widgets_reach_their_render_objects_and_a_shape_clip_resolves_its_direction() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(
&mut app,
ClipOval::new().child(SizedBox::shrink()).into_widget(),
);
harness.pump(&mut app);
assert!(
render_object_under_root::<RenderClipOval>(&harness, &app).clip_behavior(&app)
== Clip::AntiAlias
);
harness.set_child(
&mut app,
ClipRSuperellipse::new()
.border_radius(waft_painting::BorderRadiusGeometry::circular(8.0))
.child(SizedBox::shrink())
.into_widget(),
);
harness.pump(&mut app);
let superellipse = render_object_under_root::<RenderClipRSuperellipse>(&harness, &app);
assert_eq!(
superellipse.border_radius(&app),
waft_painting::BorderRadiusGeometry::circular(8.0)
);
let shape: Box<dyn ShapeBorder> = Box::new(waft_painting::RoundedRectangleBorder::new(
waft_painting::BorderSide::NONE,
waft_painting::BorderRadiusGeometry::circular(4.0),
));
harness.set_child(
&mut app,
Directionality::new(
TextDirection::Rtl,
ClipPath::shape(shape)
.clip_behavior(Clip::HardEdge)
.child(SizedBox::shrink()),
)
.into_widget(),
);
harness.pump(&mut app);
let clip_path = harness
.render_root(&app)
.child(&app)
.expect("the clip under the directionality")
.as_object()
.downcast::<RenderClipPath>(&app)
.expect("a RenderClipPath");
assert_eq!(clip_path.clip_behavior(&app), Clip::HardEdge);
let clipper = clip_path.clipper(&app).expect("the shape clipper");
let clipper = clipper
.as_any()
.downcast_ref::<ShapeBorderClipper>()
.expect("a ShapeBorderClipper");
assert_eq!(clipper.text_direction, Some(TextDirection::Rtl));
}
}