use crate::draw::{inset_rect, mix_color};
use std::{cell::RefCell, ops::Range, rc::Rc, sync::Arc};
use sui_core::{
DragEvent, DragEventKind, Event, InvalidationKind, InvalidationRequest, InvalidationTarget,
KeyState, Path, Point, PointerButton, PointerEvent, PointerEventKind, PointerKind, Rect,
ScrollDelta, SemanticsAction, SemanticsActionRequest, SemanticsNode, SemanticsRole,
SemanticsValue, Size, Vector, WakeEvent, WidgetId, WindowEvent,
};
use sui_layout::{
Alignment, Axis, Constraints, FlexAlignContent, FlexItem, FlexJustify, FlexMeasurePhase,
FlexStyle, FlexWrap, IntrinsicSize, Padding as Insets, arrange_flex, flex_layout,
flex_layout_with_probes,
};
use sui_reactive::Observable;
use sui_runtime::{
ArrangeCtx, Command, EventCtx, EventPhase, FrameClock, LayerOptions, MeasureCtx,
PaintBoundaryMode, PaintCtx, REACTIVE_CHANGED, SemanticsCtx, SingleChild, Widget,
WidgetChildren, WidgetPod, WidgetPodMutVisitor, WidgetPodVisitor,
};
use sui_scene::{Brush, LayerCompositionMode};
use crate::DefaultTheme;
use crate::frame::{physical_pixels, snap_to_pixels, stroke_border};
pub struct Padding {
insets: Insets,
fill_child_width: bool,
fill_child_height: bool,
child: SingleChild,
}
impl Padding {
pub fn new<W>(insets: Insets, child: W) -> Self
where
W: Widget + 'static,
{
Self {
insets,
fill_child_width: false,
fill_child_height: false,
child: SingleChild::new(child),
}
}
pub fn all<W>(value: f32, child: W) -> Self
where
W: Widget + 'static,
{
Self::new(Insets::all(value), child)
}
pub fn horizontal<W>(value: f32, child: W) -> Self
where
W: Widget + 'static,
{
let value = value.max(0.0);
Self::new(
Insets {
left: value,
top: 0.0,
right: value,
bottom: 0.0,
},
child,
)
}
pub fn vertical<W>(value: f32, child: W) -> Self
where
W: Widget + 'static,
{
let value = value.max(0.0);
Self::new(
Insets {
left: 0.0,
top: value,
right: 0.0,
bottom: value,
},
child,
)
}
pub fn symmetric<W>(horizontal: f32, vertical: f32, child: W) -> Self
where
W: Widget + 'static,
{
Self::new(
Insets {
left: horizontal.max(0.0),
top: vertical.max(0.0),
right: horizontal.max(0.0),
bottom: vertical.max(0.0),
},
child,
)
}
pub fn insets(&self) -> Insets {
self.insets
}
pub fn fill_child_width(mut self, fill_child_width: bool) -> Self {
self.fill_child_width = fill_child_width;
self
}
pub fn fill_child_height(mut self, fill_child_height: bool) -> Self {
self.fill_child_height = fill_child_height;
self
}
pub fn fill_child(mut self) -> Self {
self.fill_child_width = true;
self.fill_child_height = true;
self
}
pub fn child_pod(&self) -> &WidgetPod {
self.child.child()
}
pub fn child_mut(&mut self) -> &mut WidgetPod {
self.child.child_mut()
}
}
impl Widget for Padding {
fn supports_output_reuse(&self) -> bool {
true
}
fn measure_axis(&mut self, ctx: &mut MeasureCtx, constraints: Constraints, axis: Axis) -> f32 {
let (min, max, padding) = match axis {
Axis::Horizontal => (
constraints.min.width,
constraints.max.width,
self.insets.left + self.insets.right,
),
Axis::Vertical => (
constraints.min.height,
constraints.max.height,
self.insets.top + self.insets.bottom,
),
};
if min == max {
return min;
}
let extent =
self.child
.measure_axis(ctx, inset_constraints(constraints, self.insets), axis);
(extent + padding).clamp(min, max)
}
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
let child_constraints = inset_constraints(constraints, self.insets);
let child_size = self.child.measure(ctx, child_constraints);
constraints.clamp(expand_size(child_size, self.insets))
}
fn measure_size(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
let child_constraints = inset_constraints(constraints, self.insets);
let child_size = self.child.measure_size(ctx, child_constraints);
constraints.clamp(expand_size(child_size, self.insets))
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
let content = inset_rect(bounds, self.insets);
let measured = self.child.child().measured_size();
let child_size = Size::new(
if self.fill_child_width {
content.width()
} else {
measured.width.min(content.width())
},
if self.fill_child_height {
content.height()
} else {
measured.height.min(content.height())
},
);
self.child
.arrange(ctx, Rect::from_origin_size(content.origin, child_size));
}
fn paint(&self, ctx: &mut PaintCtx) {
self.child.paint(ctx);
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
self.child.semantics(ctx);
}
fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
self.child.visit_children(visitor);
}
fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
self.child.visit_children_mut(visitor);
}
}
pub struct Align {
horizontal: Alignment,
vertical: Alignment,
child: SingleChild,
}
impl Align {
pub fn new<W>(horizontal: Alignment, vertical: Alignment, child: W) -> Self
where
W: Widget + 'static,
{
Self {
horizontal,
vertical,
child: SingleChild::new(child),
}
}
pub fn center<W>(child: W) -> Self
where
W: Widget + 'static,
{
Self::new(Alignment::Center, Alignment::Center, child)
}
pub fn child_pod(&self) -> &WidgetPod {
self.child.child()
}
pub fn child_mut(&mut self) -> &mut WidgetPod {
self.child.child_mut()
}
}
impl Widget for Align {
fn supports_output_reuse(&self) -> bool {
true
}
fn measure_axis(&mut self, ctx: &mut MeasureCtx, constraints: Constraints, axis: Axis) -> f32 {
let (min, max, alignment) = match axis {
Axis::Horizontal => (
constraints.min.width,
constraints.max.width,
self.horizontal,
),
Axis::Vertical => (
constraints.min.height,
constraints.max.height,
self.vertical,
),
};
if min == max || (alignment == Alignment::Stretch && max.is_finite()) {
return max;
}
let child_constraints =
aligned_child_constraints(constraints, self.horizontal, self.vertical);
self.child
.measure_axis(ctx, child_constraints, axis)
.clamp(min, max)
}
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
let child_constraints =
aligned_child_constraints(constraints, self.horizontal, self.vertical);
let child_size = self.child.measure(ctx, child_constraints);
constraints.clamp(Size::new(
stretched_dimension(self.horizontal, constraints.max.width, child_size.width),
stretched_dimension(self.vertical, constraints.max.height, child_size.height),
))
}
fn measure_size(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
let child_constraints =
aligned_child_constraints(constraints, self.horizontal, self.vertical);
let child_size = self.child.measure_size(ctx, child_constraints);
constraints.clamp(Size::new(
stretched_dimension(self.horizontal, constraints.max.width, child_size.width),
stretched_dimension(self.vertical, constraints.max.height, child_size.height),
))
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
let child_size = self.child.child().measured_size();
self.child.arrange(
ctx,
Rect::from_origin_size(
Point::new(
bounds.x() + aligned_offset(self.horizontal, bounds.width() - child_size.width),
bounds.y() + aligned_offset(self.vertical, bounds.height() - child_size.height),
),
child_size,
),
);
}
fn paint(&self, ctx: &mut PaintCtx) {
self.child.paint(ctx);
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
self.child.semantics(ctx);
}
fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
self.child.visit_children(visitor);
}
fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
self.child.visit_children_mut(visitor);
}
}
pub struct Background {
brush: Brush,
brush_reader: Option<Box<dyn Fn() -> Brush>>,
child: SingleChild,
}
impl Background {
pub fn new<W>(brush: impl Into<Brush>, child: W) -> Self
where
W: Widget + 'static,
{
Self {
brush: brush.into(),
brush_reader: None,
child: SingleChild::new(child),
}
}
pub fn brush_when<F, B>(mut self, brush: F) -> Self
where
F: Fn() -> B + 'static,
B: Into<Brush>,
{
self.brush_reader = Some(Box::new(move || brush().into()));
self
}
pub fn child_pod(&self) -> &WidgetPod {
self.child.child()
}
pub fn child_mut(&mut self) -> &mut WidgetPod {
self.child.child_mut()
}
}
impl Widget for Background {
fn supports_output_reuse(&self) -> bool {
self.brush_reader.is_none()
}
fn measure_axis(&mut self, ctx: &mut MeasureCtx, constraints: Constraints, axis: Axis) -> f32 {
self.child.measure_axis(ctx, constraints, axis)
}
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
self.child.measure(ctx, constraints)
}
fn measure_size(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
self.child.measure_size(ctx, constraints)
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
self.child.arrange(ctx, bounds);
}
fn paint(&self, ctx: &mut PaintCtx) {
let brush = self
.brush_reader
.as_ref()
.map(|reader| reader())
.unwrap_or_else(|| self.brush.clone());
ctx.fill_bounds(brush);
self.child.paint(ctx);
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
self.child.semantics(ctx);
}
fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
self.child.visit_children(visitor);
}
fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
self.child.visit_children_mut(visitor);
}
}
pub struct SemanticRegion {
name: String,
name_reader: Option<Box<dyn Fn() -> String>>,
description: Option<String>,
description_reader: Option<Box<dyn Fn() -> String>>,
role: SemanticsRole,
child: SingleChild,
}
impl SemanticRegion {
pub fn new<W>(name: impl Into<String>, child: W) -> Self
where
W: Widget + 'static,
{
Self {
name: name.into(),
name_reader: None,
description: None,
description_reader: None,
role: SemanticsRole::GenericContainer,
child: SingleChild::new(child),
}
}
pub fn name_when<F>(mut self, name: F) -> Self
where
F: Fn() -> String + 'static,
{
self.name_reader = Some(Box::new(name));
self
}
pub fn description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self.description_reader = None;
self
}
pub fn description_when<F>(mut self, description: F) -> Self
where
F: Fn() -> String + 'static,
{
self.description_reader = Some(Box::new(description));
self
}
pub fn role(mut self, role: SemanticsRole) -> Self {
self.role = role;
self
}
pub fn child_pod(&self) -> &WidgetPod {
self.child.child()
}
pub fn child_mut(&mut self) -> &mut WidgetPod {
self.child.child_mut()
}
fn name_text(&self) -> String {
self.name_reader
.as_ref()
.map(|reader| reader())
.unwrap_or_else(|| self.name.clone())
}
fn description_text(&self) -> Option<String> {
self.description_reader
.as_ref()
.map(|reader| reader())
.or_else(|| self.description.clone())
}
}
impl Widget for SemanticRegion {
fn supports_output_reuse(&self) -> bool {
true
}
fn measure_axis(&mut self, ctx: &mut MeasureCtx, constraints: Constraints, axis: Axis) -> f32 {
self.child.measure_axis(ctx, constraints, axis)
}
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
self.child.measure(ctx, constraints)
}
fn measure_size(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
self.child.measure_size(ctx, constraints)
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
self.child.arrange(ctx, bounds);
}
fn paint(&self, ctx: &mut PaintCtx) {
self.child.paint(ctx);
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
let mut node = SemanticsNode::new(ctx.widget_id(), self.role.clone(), ctx.bounds());
node.name = Some(self.name_text());
node.description = self.description_text();
ctx.push(node);
self.child.semantics(ctx);
}
fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
self.child.visit_children(visitor);
}
fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
self.child.visit_children_mut(visitor);
}
}
pub struct SizedBox {
width: Option<f32>,
height: Option<f32>,
child: Option<SingleChild>,
}
impl SizedBox {
pub fn new() -> Self {
Self {
width: None,
height: None,
child: None,
}
}
pub fn width(mut self, width: f32) -> Self {
self.width = Some(width.max(0.0));
self
}
pub fn height(mut self, height: f32) -> Self {
self.height = Some(height.max(0.0));
self
}
pub fn size(mut self, size: Size) -> Self {
self.width = Some(size.width.max(0.0));
self.height = Some(size.height.max(0.0));
self
}
pub fn child<W>(mut self, child: W) -> Self
where
W: Widget + 'static,
{
self.child = Some(SingleChild::new(child));
self
}
pub fn child_pod(&self) -> Option<&WidgetPod> {
self.child.as_ref().map(SingleChild::child)
}
pub fn child_mut(&mut self) -> Option<&mut WidgetPod> {
self.child.as_mut().map(SingleChild::child_mut)
}
}
impl Default for SizedBox {
fn default() -> Self {
Self::new()
}
}
impl Widget for SizedBox {
fn supports_output_reuse(&self) -> bool {
true
}
fn measure_axis(&mut self, ctx: &mut MeasureCtx, constraints: Constraints, axis: Axis) -> f32 {
let (min, max, explicit) = match axis {
Axis::Horizontal => (constraints.min.width, constraints.max.width, self.width),
Axis::Vertical => (constraints.min.height, constraints.max.height, self.height),
};
if let Some(value) = explicit {
return value.clamp(min, max);
}
if min == max {
return min;
}
let child_constraints = sized_box_constraints(constraints, self.width, self.height);
self.child
.as_mut()
.map_or(0.0, |child| {
child.measure_axis(ctx, child_constraints, axis)
})
.clamp(min, max)
}
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
let child_constraints = sized_box_constraints(constraints, self.width, self.height);
let child_size = if let Some(child) = &mut self.child {
child.measure(ctx, child_constraints)
} else {
Size::ZERO
};
constraints.clamp(Size::new(
self.width.unwrap_or(child_size.width),
self.height.unwrap_or(child_size.height),
))
}
fn measure_size(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
let child_constraints = sized_box_constraints(constraints, self.width, self.height);
let child_size = if let Some(child) = &mut self.child {
child.measure_size(ctx, child_constraints)
} else {
Size::ZERO
};
constraints.clamp(Size::new(
self.width.unwrap_or(child_size.width),
self.height.unwrap_or(child_size.height),
))
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
if let Some(child) = &mut self.child {
let measured = child.child().measured_size();
let child_size = Size::new(
measured.width.min(bounds.width()),
measured.height.min(bounds.height()),
);
child.arrange(ctx, Rect::from_origin_size(bounds.origin, child_size));
}
}
fn paint(&self, ctx: &mut PaintCtx) {
if let Some(child) = &self.child {
child.paint(ctx);
}
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
if let Some(child) = &self.child {
child.semantics(ctx);
}
}
fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
if let Some(child) = &self.child {
child.visit_children(visitor);
}
}
fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
if let Some(child) = &mut self.child {
child.visit_children_mut(visitor);
}
}
}
pub struct Stack {
axis: Axis,
spacing: f32,
alignment: Alignment,
children: WidgetChildren,
}
impl Stack {
pub fn new(axis: Axis) -> Self {
Self {
axis,
spacing: 0.0,
alignment: Alignment::Start,
children: WidgetChildren::new(),
}
}
pub fn horizontal() -> Self {
Self::new(Axis::Horizontal)
}
pub fn vertical() -> Self {
Self::new(Axis::Vertical)
}
pub fn gap(mut self, gap: f32) -> Self {
self.spacing = gap.max(0.0);
self
}
pub fn alignment(mut self, alignment: Alignment) -> Self {
self.alignment = alignment;
self
}
pub fn with_child<W>(mut self, child: W) -> Self
where
W: Widget + 'static,
{
self.children.push(child);
self
}
pub fn push<W>(&mut self, child: W)
where
W: Widget + 'static,
{
self.children.push(child);
}
pub fn children(&self) -> &[WidgetPod] {
self.children.as_slice()
}
pub fn children_mut(&mut self) -> &mut [WidgetPod] {
self.children.as_mut_slice()
}
}
impl Stack {
fn measure_children(
&mut self,
ctx: &mut MeasureCtx,
constraints: Constraints,
size_only: bool,
) -> Size {
let max_main = axis_main(self.axis, constraints.max);
let max_cross = axis_cross(self.axis, constraints.max);
let stretch_cross = self.alignment == Alignment::Stretch && max_cross.is_finite();
let mut main_extent = 0.0;
let mut cross_extent: f32 = 0.0;
for (index, child) in self.children.as_mut_slice().iter_mut().enumerate() {
let spacing_before = if index == 0 { 0.0 } else { self.spacing };
let remaining_main = (max_main - main_extent - spacing_before).max(0.0);
let child_constraints =
stack_child_constraints(self.axis, remaining_main, max_cross, stretch_cross);
let child_size = if size_only {
child.probe_measure(ctx, child_constraints)
} else {
child.measure(ctx, child_constraints)
};
main_extent += spacing_before + axis_main(self.axis, child_size);
cross_extent = cross_extent.max(axis_cross(self.axis, child_size));
}
constraints.clamp(axis_size(self.axis, main_extent, cross_extent))
}
}
impl Widget for Stack {
fn supports_output_reuse(&self) -> bool {
true
}
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
self.measure_children(ctx, constraints, false)
}
fn measure_size(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
self.measure_children(ctx, constraints, true)
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
let cross_available = axis_cross(self.axis, bounds.size);
let mut main_offset = 0.0;
for (index, child) in self.children.as_mut_slice().iter_mut().enumerate() {
if index > 0 {
main_offset += self.spacing;
}
let child_size = stack_arranged_child_size(
self.axis,
self.alignment,
child.measured_size(),
cross_available,
);
let cross_offset = aligned_offset(
self.alignment,
cross_available - axis_cross(self.axis, child_size),
);
child.arrange(
ctx,
Rect::from_origin_size(
Point::new(bounds.x(), bounds.y())
+ axis_point(self.axis, main_offset, cross_offset).to_vector(),
child_size,
),
);
main_offset += axis_main(self.axis, child_size);
}
}
fn paint(&self, ctx: &mut PaintCtx) {
self.children.paint(ctx);
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
self.children.semantics(ctx);
}
fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
self.children.visit_children(visitor);
}
fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
self.children.visit_children_mut(visitor);
}
}
pub struct TrailingSlotRow {
body: SingleChild,
trailing: SingleChild,
trailing_width: f32,
trailing_height: f32,
gap: f32,
}
impl TrailingSlotRow {
pub fn new<B, T>(body: B, trailing: T) -> Self
where
B: Widget + 'static,
T: Widget + 'static,
{
Self {
body: SingleChild::new(body),
trailing: SingleChild::new(trailing),
trailing_width: 0.0,
trailing_height: 0.0,
gap: 0.0,
}
}
pub fn trailing_width(mut self, width: f32) -> Self {
self.trailing_width = width.max(0.0);
self
}
pub fn trailing_height(mut self, height: f32) -> Self {
self.trailing_height = height.max(0.0);
self
}
pub fn gap(mut self, gap: f32) -> Self {
self.gap = gap.max(0.0);
self
}
pub fn body(&self) -> &WidgetPod {
self.body.child()
}
pub fn body_mut(&mut self) -> &mut WidgetPod {
self.body.child_mut()
}
pub fn trailing(&self) -> &WidgetPod {
self.trailing.child()
}
pub fn trailing_mut(&mut self) -> &mut WidgetPod {
self.trailing.child_mut()
}
fn layout_rects(&self, bounds: Rect) -> (Rect, Rect) {
let trailing_height = self
.trailing_height
.min(bounds.width())
.min(bounds.height())
.max(0.0);
let trailing_width = self.trailing_width.min(bounds.width()).max(0.0);
let gap = if trailing_width > 0.0 && bounds.width() > trailing_width {
self.gap.min(bounds.width() - trailing_width).max(0.0)
} else {
0.0
};
let body_width = (bounds.width() - trailing_width - gap).max(0.0);
let body = Rect::new(bounds.x(), bounds.y(), body_width, bounds.height());
let trailing = Rect::new(
bounds.x() + body_width + gap,
bounds.y() + ((bounds.height() - trailing_height) * 0.5).max(0.0),
trailing_width,
trailing_height,
);
(body, trailing)
}
}
impl Widget for TrailingSlotRow {
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
let width = if constraints.max.width.is_finite() {
constraints.max.width
} else {
constraints.min.width.max(self.trailing_width)
};
if !constraints.max.height.is_finite() {
let provisional_height = constraints.min.height.max(self.trailing_height);
let bounds = Rect::new(0.0, 0.0, width, provisional_height);
let (body, trailing) = self.layout_rects(bounds);
let body_size = self.body.measure(
ctx,
Constraints::new(
Size::new(body.width(), 0.0),
Size::new(body.width(), f32::INFINITY),
),
);
self.trailing.measure(
ctx,
Constraints::tight(Size::new(trailing.width(), self.trailing_height)),
);
let height = body_size
.height
.max(self.trailing_height)
.max(constraints.min.height);
return constraints.clamp(Size::new(width, height));
}
let height = constraints.max.height.max(constraints.min.height);
let bounds = Rect::new(0.0, 0.0, width, height);
let (body, trailing) = self.layout_rects(bounds);
self.body.measure(
ctx,
Constraints::tight(Size::new(body.width(), body.height())),
);
self.trailing.measure(
ctx,
Constraints::tight(Size::new(trailing.width(), trailing.height())),
);
constraints.clamp(Size::new(width, height))
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
let (body, trailing) = self.layout_rects(bounds);
self.body.arrange(ctx, body);
self.trailing.arrange(ctx, trailing);
}
fn paint(&self, ctx: &mut PaintCtx) {
self.body.paint(ctx);
self.trailing.paint(ctx);
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
self.body.semantics(ctx);
self.trailing.semantics(ctx);
}
fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
self.body.visit_children(visitor);
self.trailing.visit_children(visitor);
}
fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
self.body.visit_children_mut(visitor);
self.trailing.visit_children_mut(visitor);
}
}
pub struct Dock {
top: Option<SingleChild>,
bottom: Option<SingleChild>,
body: SingleChild,
top_height: f32,
bottom_height: f32,
fallback_width: f32,
fallback_body_height: f32,
}
impl Dock {
pub fn new<W>(body: W) -> Self
where
W: Widget + 'static,
{
Self {
top: None,
bottom: None,
body: SingleChild::new(body),
top_height: 0.0,
bottom_height: 0.0,
fallback_width: 320.0,
fallback_body_height: 240.0,
}
}
pub fn top<W>(mut self, height: f32, widget: W) -> Self
where
W: Widget + 'static,
{
self.top = Some(SingleChild::new(widget));
self.top_height = height.max(0.0);
self
}
pub fn bottom<W>(mut self, height: f32, widget: W) -> Self
where
W: Widget + 'static,
{
self.bottom = Some(SingleChild::new(widget));
self.bottom_height = height.max(0.0);
self
}
pub fn fallback_width(mut self, width: f32) -> Self {
self.fallback_width = width.max(0.0);
self
}
pub fn fallback_body_height(mut self, height: f32) -> Self {
self.fallback_body_height = height.max(0.0);
self
}
pub fn top_child(&self) -> Option<&WidgetPod> {
self.top.as_ref().map(SingleChild::child)
}
pub fn top_child_mut(&mut self) -> Option<&mut WidgetPod> {
self.top.as_mut().map(SingleChild::child_mut)
}
pub fn body(&self) -> &WidgetPod {
self.body.child()
}
pub fn body_mut(&mut self) -> &mut WidgetPod {
self.body.child_mut()
}
pub fn bottom_child(&self) -> Option<&WidgetPod> {
self.bottom.as_ref().map(SingleChild::child)
}
pub fn bottom_child_mut(&mut self) -> Option<&mut WidgetPod> {
self.bottom.as_mut().map(SingleChild::child_mut)
}
fn width_for_measure(&self, constraints: Constraints) -> f32 {
if constraints.max.width.is_finite() {
constraints.max.width
} else {
constraints.min.width.max(self.fallback_width)
}
}
}
impl Widget for Dock {
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
let width = self.width_for_measure(constraints);
let top_h = self.top_height.max(0.0);
let bottom_h = self.bottom_height.max(0.0);
let top_size = if let Some(top) = &mut self.top {
top.measure(ctx, Constraints::tight(Size::new(width, top_h)))
} else {
Size::ZERO
};
let bottom_size = if let Some(bottom) = &mut self.bottom {
bottom.measure(ctx, Constraints::tight(Size::new(width, bottom_h)))
} else {
Size::ZERO
};
let available_body_h = if constraints.max.height.is_finite() {
(constraints.max.height - top_h - bottom_h).max(0.0)
} else {
self.fallback_body_height
};
let body_size = self.body.measure(
ctx,
Constraints::new(
Size::new(width, 0.0),
Size::new(width, available_body_h.max(0.0)),
),
);
let height = if constraints.max.height.is_finite() {
constraints.max.height
} else {
top_h + body_size.height + bottom_h
};
let width = width
.max(top_size.width)
.max(body_size.width)
.max(bottom_size.width);
constraints.clamp(Size::new(width, height))
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
let top_h = self.top_height.min(bounds.height()).max(0.0);
let bottom_h = self
.bottom_height
.min((bounds.height() - top_h).max(0.0))
.max(0.0);
let body_h = (bounds.height() - top_h - bottom_h).max(0.0);
if let Some(top) = &mut self.top {
top.arrange(
ctx,
Rect::new(bounds.x(), bounds.y(), bounds.width(), top_h),
);
}
self.body.arrange(
ctx,
Rect::new(bounds.x(), bounds.y() + top_h, bounds.width(), body_h),
);
if let Some(bottom) = &mut self.bottom {
bottom.arrange(
ctx,
Rect::new(
bounds.x(),
bounds.max_y() - bottom_h,
bounds.width(),
bottom_h,
),
);
}
}
fn paint(&self, ctx: &mut PaintCtx) {
self.body.paint(ctx);
if let Some(top) = &self.top {
top.paint(ctx);
}
if let Some(bottom) = &self.bottom {
bottom.paint(ctx);
}
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
if let Some(top) = &self.top {
top.semantics(ctx);
}
self.body.semantics(ctx);
if let Some(bottom) = &self.bottom {
bottom.semantics(ctx);
}
}
fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
if let Some(top) = &self.top {
top.visit_children(visitor);
}
self.body.visit_children(visitor);
if let Some(bottom) = &self.bottom {
bottom.visit_children(visitor);
}
}
fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
if let Some(top) = &mut self.top {
top.visit_children_mut(visitor);
}
self.body.visit_children_mut(visitor);
if let Some(bottom) = &mut self.bottom {
bottom.visit_children_mut(visitor);
}
}
}
pub struct MeasuredBottomDock {
body: SingleChild,
bottom: SingleChild,
fallback_size: Size,
}
impl MeasuredBottomDock {
pub fn new<B, T>(body: B, bottom: T) -> Self
where
B: Widget + 'static,
T: Widget + 'static,
{
Self {
body: SingleChild::new(body),
bottom: SingleChild::new(bottom),
fallback_size: Size::new(640.0, 640.0),
}
}
pub fn fallback_size(mut self, size: Size) -> Self {
self.fallback_size = Size::new(size.width.max(0.0), size.height.max(0.0));
self
}
pub fn body(&self) -> &WidgetPod {
self.body.child()
}
pub fn body_mut(&mut self) -> &mut WidgetPod {
self.body.child_mut()
}
pub fn bottom(&self) -> &WidgetPod {
self.bottom.child()
}
pub fn bottom_mut(&mut self) -> &mut WidgetPod {
self.bottom.child_mut()
}
}
impl Widget for MeasuredBottomDock {
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
let width = if constraints.max.width.is_finite() {
constraints.max.width
} else {
constraints.min.width.max(self.fallback_size.width)
};
let height = if constraints.max.height.is_finite() {
constraints.max.height
} else {
constraints.min.height.max(self.fallback_size.height)
};
let bottom_size = self.bottom.measure(
ctx,
Constraints::new(Size::new(width, 0.0), Size::new(width, height)),
);
let bottom_h = bottom_size.height.min(height).max(0.0);
let body_h = (height - bottom_h).max(0.0);
self.body
.measure(ctx, Constraints::tight(Size::new(width, body_h)));
constraints.clamp(Size::new(width, body_h + bottom_h))
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
let bottom_h = self
.bottom
.child()
.measured_size()
.height
.min(bounds.height())
.max(0.0);
let body_h = (bounds.height() - bottom_h).max(0.0);
self.body.arrange(
ctx,
Rect::new(bounds.x(), bounds.y(), bounds.width(), body_h),
);
self.bottom.arrange(
ctx,
Rect::new(
bounds.x(),
bounds.max_y() - bottom_h,
bounds.width(),
bottom_h,
),
);
}
fn paint(&self, ctx: &mut PaintCtx) {
self.body.paint(ctx);
self.bottom.paint(ctx);
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
self.body.semantics(ctx);
self.bottom.semantics(ctx);
}
fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
self.body.visit_children(visitor);
self.bottom.visit_children(visitor);
}
fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
self.body.visit_children_mut(visitor);
self.bottom.visit_children_mut(visitor);
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum FixedPane {
First,
Second,
}
pub struct FixedPaneSplit {
axis: Axis,
fixed_pane: FixedPane,
fixed_extent: f32,
divider_extent: f32,
fallback_flexible_extent: f32,
first: SingleChild,
divider: SingleChild,
second: SingleChild,
}
impl FixedPaneSplit {
pub fn new<F, D, S>(axis: Axis, first: F, divider: D, second: S) -> Self
where
F: Widget + 'static,
D: Widget + 'static,
S: Widget + 'static,
{
Self {
axis,
fixed_pane: FixedPane::First,
fixed_extent: 0.0,
divider_extent: 1.0,
fallback_flexible_extent: 320.0,
first: SingleChild::new(first),
divider: SingleChild::new(divider),
second: SingleChild::new(second),
}
}
pub fn horizontal<F, D, S>(first: F, divider: D, second: S) -> Self
where
F: Widget + 'static,
D: Widget + 'static,
S: Widget + 'static,
{
Self::new(Axis::Horizontal, first, divider, second)
}
pub fn vertical<F, D, S>(first: F, divider: D, second: S) -> Self
where
F: Widget + 'static,
D: Widget + 'static,
S: Widget + 'static,
{
Self::new(Axis::Vertical, first, divider, second)
}
pub fn fixed_first(mut self, extent: f32) -> Self {
self.fixed_pane = FixedPane::First;
self.fixed_extent = extent.max(0.0);
self
}
pub fn fixed_second(mut self, extent: f32) -> Self {
self.fixed_pane = FixedPane::Second;
self.fixed_extent = extent.max(0.0);
self
}
pub fn divider_extent(mut self, extent: f32) -> Self {
self.divider_extent = extent.max(0.0);
self
}
pub fn fallback_flexible_extent(mut self, extent: f32) -> Self {
self.fallback_flexible_extent = extent.max(0.0);
self
}
pub fn first(&self) -> &WidgetPod {
self.first.child()
}
pub fn first_mut(&mut self) -> &mut WidgetPod {
self.first.child_mut()
}
pub fn divider(&self) -> &WidgetPod {
self.divider.child()
}
pub fn divider_mut(&mut self) -> &mut WidgetPod {
self.divider.child_mut()
}
pub fn second(&self) -> &WidgetPod {
self.second.child()
}
pub fn second_mut(&mut self) -> &mut WidgetPod {
self.second.child_mut()
}
fn split_extents(&self, total_main: f32) -> (f32, f32, f32) {
let divider = self.divider_extent.min(total_main).max(0.0);
let available = (total_main - divider).max(0.0);
match self.fixed_pane {
FixedPane::First => {
let first = self.fixed_extent.min(available).max(0.0);
(first, divider, (available - first).max(0.0))
}
FixedPane::Second => {
let second = self.fixed_extent.min(available).max(0.0);
((available - second).max(0.0), divider, second)
}
}
}
fn child_constraints(&self, main: f32, cross: Option<f32>) -> Constraints {
Constraints::new(
axis_size(self.axis, main, cross.unwrap_or(0.0)),
axis_size(self.axis, main, cross.unwrap_or(f32::INFINITY)),
)
}
fn child_rect(&self, bounds: Rect, main_offset: f32, main: f32) -> Rect {
Rect::from_origin_size(
bounds.origin + axis_point(self.axis, main_offset, 0.0).to_vector(),
axis_size(self.axis, main, axis_cross(self.axis, bounds.size)),
)
}
}
impl Widget for FixedPaneSplit {
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
let total_main = if axis_main(self.axis, constraints.max).is_finite() {
axis_main(self.axis, constraints.max)
} else {
self.fixed_extent + self.divider_extent + self.fallback_flexible_extent
};
let cross_max = axis_cross(self.axis, constraints.max);
let tight_cross = cross_max.is_finite().then_some(cross_max);
let (first_main, divider_main, second_main) = self.split_extents(total_main);
let first_size = self
.first
.measure(ctx, self.child_constraints(first_main, tight_cross));
let divider_size = self
.divider
.measure(ctx, self.child_constraints(divider_main, tight_cross));
let second_size = self
.second
.measure(ctx, self.child_constraints(second_main, tight_cross));
let cross = tight_cross.unwrap_or_else(|| {
axis_cross(self.axis, first_size)
.max(axis_cross(self.axis, divider_size))
.max(axis_cross(self.axis, second_size))
.max(axis_cross(self.axis, constraints.min))
});
constraints.clamp(axis_size(self.axis, total_main, cross))
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
let (first_main, divider_main, second_main) =
self.split_extents(axis_main(self.axis, bounds.size));
self.first
.arrange(ctx, self.child_rect(bounds, 0.0, first_main));
self.divider
.arrange(ctx, self.child_rect(bounds, first_main, divider_main));
self.second.arrange(
ctx,
self.child_rect(bounds, first_main + divider_main, second_main),
);
}
fn paint(&self, ctx: &mut PaintCtx) {
self.first.paint(ctx);
self.divider.paint(ctx);
self.second.paint(ctx);
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
self.first.semantics(ctx);
self.divider.semantics(ctx);
self.second.semantics(ctx);
}
fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
self.first.visit_children(visitor);
self.divider.visit_children(visitor);
self.second.visit_children(visitor);
}
fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
self.first.visit_children_mut(visitor);
self.divider.visit_children_mut(visitor);
self.second.visit_children_mut(visitor);
}
}
pub struct Flex {
style: FlexStyle,
items: Vec<FlexItem>,
children: WidgetChildren,
}
impl Flex {
pub fn new(axis: Axis) -> Self {
Self {
style: FlexStyle::new(axis),
items: Vec::new(),
children: WidgetChildren::new(),
}
}
pub fn horizontal() -> Self {
Self::new(Axis::Horizontal)
}
pub fn vertical() -> Self {
Self::new(Axis::Vertical)
}
pub fn with_style(mut self, style: FlexStyle) -> Self {
self.style = style;
self
}
pub fn wrap(mut self, wrap: FlexWrap) -> Self {
self.style = self.style.wrap(wrap);
self
}
pub fn gap(mut self, gap: f32) -> Self {
self.style = self.style.gap(gap);
self
}
pub fn main_gap(mut self, gap: f32) -> Self {
self.style = self.style.main_gap(gap);
self
}
pub fn cross_gap(mut self, gap: f32) -> Self {
self.style = self.style.cross_gap(gap);
self
}
pub fn justify(mut self, justify: FlexJustify) -> Self {
self.style = self.style.justify(justify);
self
}
pub fn align_items(mut self, alignment: Alignment) -> Self {
self.style = self.style.align_items(alignment);
self
}
pub fn align_content(mut self, alignment: FlexAlignContent) -> Self {
self.style = self.style.align_content(alignment);
self
}
pub fn with_child<W>(mut self, child: W) -> Self
where
W: Widget + 'static,
{
self.children.push(child);
self.items.push(FlexItem::new());
self
}
pub fn with_item<W>(mut self, child: W, item: FlexItem) -> Self
where
W: Widget + 'static,
{
self.children.push(child);
self.items.push(item);
self
}
pub fn spacer(mut self) -> Self {
self.push_spacer();
self
}
pub fn push<W>(&mut self, child: W)
where
W: Widget + 'static,
{
self.children.push(child);
self.items.push(FlexItem::new());
}
pub fn push_item<W>(&mut self, child: W, item: FlexItem)
where
W: Widget + 'static,
{
self.children.push(child);
self.items.push(item);
}
pub fn push_spacer(&mut self) {
self.children.push(SizedBox::new());
self.items.push(FlexItem::fill());
}
pub const fn style(&self) -> FlexStyle {
self.style
}
pub fn flex_items(&self) -> &[FlexItem] {
&self.items
}
pub fn flex_items_mut(&mut self) -> &mut [FlexItem] {
&mut self.items
}
pub fn flex_item(&self, index: usize) -> Option<FlexItem> {
self.items.get(index).copied()
}
pub fn set_flex_item(&mut self, index: usize, item: FlexItem) -> bool {
let Some(slot) = self.items.get_mut(index) else {
return false;
};
*slot = item;
true
}
pub fn children(&self) -> &[WidgetPod] {
self.children.as_slice()
}
pub fn children_mut(&mut self) -> &mut [WidgetPod] {
self.children.as_mut_slice()
}
}
impl Flex {
fn measure_flex(
&mut self,
ctx: &mut MeasureCtx,
constraints: Constraints,
commit: bool,
) -> Size {
let axis = self.style.axis;
flex_layout_with_probes(self.style, &self.items, constraints, |index, c, phase| {
let child = &mut self.children.as_mut_slice()[index];
match phase {
FlexMeasurePhase::Probe => {
let main = child.probe_measure_axis(ctx, c, axis);
match axis {
Axis::Horizontal => Size::new(main, 0.0),
Axis::Vertical => Size::new(0.0, main),
}
}
FlexMeasurePhase::Commit if commit => child.measure(ctx, c),
FlexMeasurePhase::Commit => child.probe_measure(ctx, c),
}
})
.size
}
}
impl Widget for Flex {
fn supports_output_reuse(&self) -> bool {
true
}
fn measure_axis(&mut self, ctx: &mut MeasureCtx, constraints: Constraints, axis: Axis) -> f32 {
let extent = |size: Size| match axis {
Axis::Horizontal => size.width,
Axis::Vertical => size.height,
};
if extent(constraints.min) == extent(constraints.max) {
return extent(constraints.min);
}
if axis != self.style.axis {
return extent(self.measure_size(ctx, constraints));
}
extent(
flex_layout(self.style, &self.items, constraints, |index, c| {
let main = self.children.as_mut_slice()[index].probe_measure_axis(ctx, c, axis);
match axis {
Axis::Horizontal => Size::new(main, 0.0),
Axis::Vertical => Size::new(0.0, main),
}
})
.size,
)
}
fn measure_size(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
self.measure_flex(ctx, constraints, false)
}
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
self.measure_flex(ctx, constraints, true)
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
let measured_sizes = self
.children
.as_slice()
.iter()
.map(WidgetPod::measured_size)
.collect::<Vec<_>>();
let layout = arrange_flex(self.style, &self.items, bounds.size, &measured_sizes);
for (index, item_layout) in layout.items.iter().enumerate() {
self.children.arrange_child(
index,
ctx,
item_layout.rect.translate(bounds.origin.to_vector()),
);
}
}
fn paint(&self, ctx: &mut PaintCtx) {
self.children.paint(ctx);
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
self.children.semantics(ctx);
}
fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
self.children.visit_children(visitor);
}
fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
self.children.visit_children_mut(visitor);
}
}
pub struct SwitchView {
selected: usize,
selected_reader: Option<Box<dyn Fn() -> usize>>,
selected_source: Option<Arc<dyn Observable<usize>>>,
children: WidgetChildren,
}
impl SwitchView {
pub fn new() -> Self {
Self {
selected: 0,
selected_reader: None,
selected_source: None,
children: WidgetChildren::new(),
}
}
pub fn selected(mut self, selected: usize) -> Self {
self.selected = selected;
self.selected_reader = None;
self.selected_source = None;
self
}
pub fn selected_when<F>(mut self, selected: F) -> Self
where
F: Fn() -> usize + 'static,
{
self.selected_reader = Some(Box::new(selected));
self.selected_source = None;
self
}
pub fn selected_from<O>(mut self, selected: O) -> Self
where
O: Observable<usize> + 'static,
{
self.selected = selected.get();
self.selected_reader = None;
self.selected_source = Some(Arc::new(selected));
self
}
pub fn with_child<W>(mut self, child: W) -> Self
where
W: Widget + 'static,
{
self.children.push(child);
self
}
pub fn push<W>(&mut self, child: W)
where
W: Widget + 'static,
{
self.children.push(child);
}
pub fn selected_index(&self) -> Option<usize> {
self.active_index()
}
pub fn children(&self) -> &[WidgetPod] {
self.children.as_slice()
}
pub fn children_mut(&mut self) -> &mut [WidgetPod] {
self.children.as_mut_slice()
}
fn active_index(&self) -> Option<usize> {
let selected = self
.selected_source
.as_ref()
.map(|source| source.get())
.or_else(|| self.selected_reader.as_ref().map(|reader| reader()))
.unwrap_or(self.selected);
(selected < self.children.as_slice().len()).then_some(selected)
}
}
impl Default for SwitchView {
fn default() -> Self {
Self::new()
}
}
impl Widget for SwitchView {
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
if let Some(source) = &self.selected_source {
self.selected = ctx.observe(source.as_ref());
}
let Some(index) = self.active_index() else {
return constraints.clamp(Size::ZERO);
};
self.children.as_mut_slice()[index].measure(ctx, constraints)
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
if let Some(index) = self.active_index() {
self.children.as_mut_slice()[index].arrange(ctx, bounds);
}
}
fn paint(&self, ctx: &mut PaintCtx) {
if let Some(index) = self.active_index() {
self.children.as_slice()[index].paint(ctx);
}
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
if let Some(index) = self.active_index() {
self.children.as_slice()[index].semantics(ctx);
}
}
fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
if let Some(index) = self.active_index() {
visitor.visit(&self.children.as_slice()[index]);
}
}
fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
if let Some(index) = self.active_index() {
visitor.visit(&mut self.children.as_mut_slice()[index]);
}
}
}
pub struct RebuildOnChange<K: PartialEq + Clone> {
key_fn: Option<Box<dyn Fn() -> K>>,
key_source: Option<Arc<dyn Observable<K>>>,
build: Box<dyn Fn(&K) -> WidgetPod>,
last_key: K,
child: WidgetPod,
}
impl<K: PartialEq + Clone> RebuildOnChange<K> {
pub fn key_when<KF, BF>(key_fn: KF, build: BF) -> Self
where
KF: Fn() -> K + 'static,
BF: Fn(&K) -> WidgetPod + 'static,
{
let last_key = key_fn();
let child = build(&last_key);
Self {
key_fn: Some(Box::new(key_fn)),
key_source: None,
build: Box::new(build),
last_key,
child,
}
}
pub fn key_from<O, BF>(key_source: O, build: BF) -> Self
where
O: Observable<K> + 'static,
BF: Fn(&K) -> WidgetPod + 'static,
K: 'static,
{
let last_key = key_source.get();
let child = build(&last_key);
Self {
key_fn: None,
key_source: Some(Arc::new(key_source)),
build: Box::new(build),
last_key,
child,
}
}
fn current_key(&self) -> K {
self.key_source
.as_ref()
.map(|source| source.get())
.or_else(|| self.key_fn.as_ref().map(|key_fn| key_fn()))
.expect("RebuildOnChange requires a key reader or observable")
}
pub fn refresh(&mut self) -> bool {
let key = self.current_key();
if key != self.last_key {
self.child = (self.build)(&key);
self.last_key = key;
true
} else {
false
}
}
pub fn child_pod(&self) -> &WidgetPod {
&self.child
}
pub fn child_mut(&mut self) -> &mut WidgetPod {
&mut self.child
}
}
impl<K: PartialEq + Clone + 'static> Widget for RebuildOnChange<K> {
fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
if let Some(source) = &self.key_source {
let _ = ctx.observe(source.as_ref(), InvalidationKind::Measure);
}
let pointer_up = matches!(
event,
Event::Pointer(pointer) if matches!(pointer.kind, PointerEventKind::Up)
);
let redraw = matches!(event, Event::Window(WindowEvent::RedrawRequested));
if (pointer_up || redraw) && self.refresh() {
ctx.record_rebuild(
std::any::type_name::<Self>(),
"caller-supplied structural key changed",
);
ctx.request_measure();
ctx.request_arrange();
ctx.request_paint();
ctx.request_semantics();
}
}
fn command(&mut self, ctx: &mut EventCtx, command: &Command<'_>) {
if command.is(REACTIVE_CHANGED) && self.refresh() {
ctx.record_rebuild(
std::any::type_name::<Self>(),
"observable structural key changed",
);
ctx.request_measure();
ctx.request_arrange();
ctx.request_paint();
ctx.request_semantics();
ctx.set_handled();
}
}
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
if let Some(source) = &self.key_source {
let _ = ctx.observe(source.as_ref());
}
if self.refresh() {
ctx.record_rebuild(
std::any::type_name::<Self>(),
"caller-supplied structural key changed during measure",
);
}
self.child.measure(ctx, constraints)
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
self.child.arrange(ctx, bounds);
}
fn paint(&self, ctx: &mut PaintCtx) {
self.child.paint(ctx);
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
self.child.semantics(ctx);
}
fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
visitor.visit(&self.child);
}
fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
visitor.visit(&mut self.child);
}
}
pub struct RebuildOnConstraints<K: PartialEq + Clone> {
key_fn: Box<dyn Fn(Constraints) -> K>,
build: Box<dyn Fn(&K) -> WidgetPod>,
last_key: K,
child: WidgetPod,
}
impl<K: PartialEq + Clone> RebuildOnConstraints<K> {
pub fn new<KF, BF>(initial_key: K, key_fn: KF, build: BF) -> Self
where
KF: Fn(Constraints) -> K + 'static,
BF: Fn(&K) -> WidgetPod + 'static,
{
let child = build(&initial_key);
Self {
key_fn: Box::new(key_fn),
build: Box::new(build),
last_key: initial_key,
child,
}
}
pub fn refresh_for_constraints(&mut self, constraints: Constraints) -> bool {
let key = (self.key_fn)(constraints);
if key != self.last_key {
self.child = (self.build)(&key);
self.last_key = key;
true
} else {
false
}
}
pub fn child_pod(&self) -> &WidgetPod {
&self.child
}
pub fn child_mut(&mut self) -> &mut WidgetPod {
&mut self.child
}
}
impl<K: PartialEq + Clone + 'static> Widget for RebuildOnConstraints<K> {
fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
let pointer_up = matches!(
event,
Event::Pointer(pointer) if matches!(pointer.kind, PointerEventKind::Up)
);
let redraw = matches!(event, Event::Window(WindowEvent::RedrawRequested));
let bounds = ctx.bounds();
if (pointer_up || redraw)
&& self.refresh_for_constraints(Constraints::tight(Size::new(
bounds.width(),
bounds.height(),
)))
{
ctx.record_rebuild(
std::any::type_name::<Self>(),
"constraint-derived structural key changed",
);
ctx.request_measure();
ctx.request_arrange();
ctx.request_paint();
ctx.request_semantics();
}
}
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
if self.refresh_for_constraints(constraints) {
ctx.record_rebuild(
std::any::type_name::<Self>(),
"constraint-derived structural key changed during measure",
);
}
self.child.measure(ctx, constraints)
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
self.child.arrange(ctx, bounds);
}
fn paint(&self, ctx: &mut PaintCtx) {
self.child.paint(ctx);
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
self.child.semantics(ctx);
}
fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
visitor.visit(&self.child);
}
fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
visitor.visit(&mut self.child);
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScrollAxes {
None,
Vertical,
Horizontal,
Both,
}
impl ScrollAxes {
const fn allows_horizontal(self) -> bool {
matches!(self, Self::Horizontal | Self::Both)
}
const fn allows_vertical(self) -> bool {
matches!(self, Self::Vertical | Self::Both)
}
}
pub(crate) const TOUCH_SCROLL_DRAG_THRESHOLD: f32 = 4.0;
const DRAG_AUTO_SCROLL_EDGE: f32 = 40.0;
const DRAG_AUTO_SCROLL_SPEED: f32 = 1_200.0;
#[derive(Debug, Clone, Copy)]
struct DragAutoScroll {
enabled: bool,
velocity: Vector,
}
impl Default for DragAutoScroll {
fn default() -> Self {
Self {
enabled: true,
velocity: Vector::ZERO,
}
}
}
impl DragAutoScroll {
fn is_scrolling(&self) -> bool {
self.velocity != Vector::ZERO
}
fn stop(&mut self) {
self.velocity = Vector::ZERO;
}
fn follow(&mut self, ctx: &mut EventCtx, drag: &DragEvent, viewport: Rect, axes: (bool, bool)) {
if !self.enabled {
return;
}
let was_scrolling = self.is_scrolling();
match drag.kind {
DragEventKind::Enter | DragEventKind::Over => {
let speed = |scrolls: bool, position: f32, start: f32, end: f32| {
if scrolls {
drag_edge_speed(position, start, end)
} else {
0.0
}
};
self.velocity = Vector::new(
speed(axes.0, drag.position.x, viewport.x(), viewport.max_x()),
speed(axes.1, drag.position.y, viewport.y(), viewport.max_y()),
);
}
DragEventKind::Leave if drag.target == Some(ctx.widget_id()) => self.stop(),
DragEventKind::Drop | DragEventKind::End => self.stop(),
DragEventKind::Leave => {}
}
if self.is_scrolling() && !was_scrolling {
ctx.request_animation_frame();
}
}
fn step(&self, delta: f64) -> Vector {
let seconds = delta.clamp(0.0, 1.0 / 20.0) as f32;
Vector::new(self.velocity.x * seconds, self.velocity.y * seconds)
}
}
fn drag_edge_speed(position: f32, start: f32, end: f32) -> f32 {
let zone = DRAG_AUTO_SCROLL_EDGE.min((end - start) / 4.0);
if zone <= 0.0 {
return 0.0;
}
let ramp = |distance: f32| {
let closeness = 1.0 - (distance / zone).clamp(0.0, 1.0);
closeness * closeness * DRAG_AUTO_SCROLL_SPEED
};
if position < start + zone {
-ramp(position - start)
} else if position > end - zone {
ramp(end - position)
} else {
0.0
}
}
const TOUCH_VELOCITY_WINDOW: f64 = 0.1;
const TOUCH_VELOCITY_STALE: f64 = 0.05;
const TOUCH_VELOCITY_SAMPLES: usize = 8;
const TOUCH_FLING_FRICTION: f32 = 2.0;
const TOUCH_FLING_MIN_SPEED: f32 = 60.0;
const TOUCH_FLING_STOP_SPEED: f32 = 12.0;
const TOUCH_FLING_MAX_SPEED: f32 = 8_000.0;
#[derive(Debug, Clone, Copy)]
struct TouchVelocity {
samples: [(f64, Point); TOUCH_VELOCITY_SAMPLES],
len: usize,
next: usize,
}
impl TouchVelocity {
fn new(time: f64, position: Point) -> Self {
let mut velocity = Self {
samples: [(time, position); TOUCH_VELOCITY_SAMPLES],
len: 0,
next: 0,
};
velocity.push(time, position);
velocity
}
fn push(&mut self, time: f64, position: Point) {
self.samples[self.next] = (time, position);
self.next = (self.next + 1) % TOUCH_VELOCITY_SAMPLES;
self.len = (self.len + 1).min(TOUCH_VELOCITY_SAMPLES);
}
fn sample(&self, back: usize) -> (f64, Point) {
self.samples[(self.next + TOUCH_VELOCITY_SAMPLES - back) % TOUCH_VELOCITY_SAMPLES]
}
fn release(&self, time: f64) -> Vector {
if self.len < 2 {
return Vector::ZERO;
}
let newest = self.sample(1);
if time - newest.0 > TOUCH_VELOCITY_STALE {
return Vector::ZERO;
}
let mut oldest = newest;
for back in 2..=self.len {
let sample = self.sample(back);
if newest.0 - sample.0 > TOUCH_VELOCITY_WINDOW {
break;
}
oldest = sample;
}
let span = newest.0 - oldest.0;
if span <= f64::EPSILON {
return Vector::ZERO;
}
let distance = newest.1 - oldest.1;
let span = span as f32;
Vector::new(distance.x / span, distance.y / span)
}
}
#[derive(Debug, Clone, Copy, Default)]
struct TouchFling {
velocity: Vector,
}
impl TouchFling {
fn is_active(&self) -> bool {
self.velocity != Vector::ZERO
}
fn stop(&mut self) -> bool {
let was_active = self.is_active();
self.velocity = Vector::ZERO;
was_active
}
fn start(&mut self, ctx: &mut EventCtx, velocity: Vector) {
let speed = velocity.x.hypot(velocity.y);
if !speed.is_finite() || speed < TOUCH_FLING_MIN_SPEED {
self.stop();
return;
}
let scale = (TOUCH_FLING_MAX_SPEED / speed).min(1.0);
self.velocity = Vector::new(velocity.x * scale, velocity.y * scale);
ctx.request_animation_frame();
}
fn step(&mut self, delta: f64) -> Vector {
let seconds = delta.clamp(0.0, 1.0 / 20.0) as f32;
let decay = (-TOUCH_FLING_FRICTION * seconds).exp();
let travel = (1.0 - decay) / TOUCH_FLING_FRICTION;
let step = Vector::new(self.velocity.x * travel, self.velocity.y * travel);
self.velocity = Vector::new(self.velocity.x * decay, self.velocity.y * decay);
if self.velocity.x.hypot(self.velocity.y) < TOUCH_FLING_STOP_SPEED {
self.stop();
}
step
}
fn block(&mut self, step: Vector, moved: Vector) {
if step.x != 0.0 && moved.x.abs() <= f32::EPSILON {
self.velocity.x = 0.0;
}
if step.y != 0.0 && moved.y.abs() <= f32::EPSILON {
self.velocity.y = 0.0;
}
}
}
type OffsetChangeCallback = Rc<dyn Fn(Vector)>;
type OffsetChangeCallbackWithCtx = Rc<dyn Fn(&mut EventCtx, Vector)>;
#[derive(Default)]
struct OffsetChange {
callback: Option<OffsetChangeCallback>,
callback_with_ctx: Option<OffsetChangeCallbackWithCtx>,
notified: Vector,
}
impl OffsetChange {
fn is_watched(&self) -> bool {
self.callback.is_some() || self.callback_with_ctx.is_some()
}
fn notify(&mut self, ctx: &mut EventCtx, offset: Vector) {
if offset == self.notified {
return;
}
self.notified = offset;
if let Some(callback) = self.callback.clone() {
callback(offset);
}
if let Some(callback) = self.callback_with_ctx.clone() {
callback(ctx, offset);
}
}
fn defer<C>(&self, ctx: &mut C, offset: Vector)
where
C: ScrollInvalidationCtx,
{
if offset != self.notified && self.is_watched() {
ctx.request_animation_frame();
}
}
}
#[derive(Debug, Clone, Copy)]
struct TouchScrollGesture {
pointer_id: u64,
start_position: Point,
dragging: bool,
velocity: TouchVelocity,
}
impl TouchScrollGesture {
fn new(pointer: &PointerEvent, time: f64) -> Self {
Self {
pointer_id: pointer.pointer_id,
start_position: pointer.position,
dragging: false,
velocity: TouchVelocity::new(time, pointer.position),
}
}
fn release_velocity(&self, time: f64, axes: ScrollAxes) -> Vector {
let finger = self.velocity.release(time);
Vector::new(
if axes.allows_horizontal() {
-finger.x
} else {
0.0
},
if axes.allows_vertical() {
-finger.y
} else {
0.0
},
)
}
fn matches(self, pointer: &PointerEvent) -> bool {
self.pointer_id == pointer.pointer_id
}
fn passed_threshold(self, pointer: &PointerEvent, axes: ScrollAxes) -> bool {
let distance = pointer.position - self.start_position;
let distance_squared = match axes {
ScrollAxes::None => 0.0,
ScrollAxes::Vertical => distance.y * distance.y,
ScrollAxes::Horizontal => distance.x * distance.x,
ScrollAxes::Both => distance.x * distance.x + distance.y * distance.y,
};
distance_squared >= TOUCH_SCROLL_DRAG_THRESHOLD * TOUCH_SCROLL_DRAG_THRESHOLD
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub enum ContentExtent {
#[default]
Natural,
Viewport,
AtLeastViewport,
AtLeast(f32),
MinContent,
}
impl ContentExtent {
fn resolve(self, viewport: f32, minimum_content: f32) -> (f32, f32) {
let viewport = if viewport.is_finite() {
viewport.max(0.0)
} else {
0.0
};
match self {
Self::Natural => (0.0, f32::INFINITY),
Self::Viewport => (0.0, viewport),
Self::AtLeastViewport => (viewport, f32::INFINITY),
Self::AtLeast(extent) => (
if extent.is_finite() {
extent.max(0.0)
} else {
0.0
},
f32::INFINITY,
),
Self::MinContent => {
let extent = if minimum_content.is_finite() {
minimum_content.max(0.0)
} else {
0.0
};
(extent, extent)
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Overflow {
Visible,
Clip,
Scroll,
Auto,
}
impl Overflow {
const fn is_scrollable(self) -> bool {
matches!(self, Self::Scroll | Self::Auto)
}
const fn clips_paint(self) -> bool {
!matches!(self, Self::Visible)
}
}
#[derive(Debug, Clone)]
pub struct ScrollState {
inner: Rc<RefCell<ScrollStateInner>>,
}
#[derive(Debug, Clone)]
struct ScrollStateInner {
axes: ScrollAxes,
viewport: Size,
content_size: Size,
offset: Vector,
pending_virtual_item: Option<usize>,
virtual_item_offsets: Vec<f32>,
scroll_view_id: Option<WidgetId>,
scroll_content_id: Option<WidgetId>,
scroll_bar_ids: Vec<WidgetId>,
}
impl ScrollState {
pub fn new() -> Self {
Self::default()
}
pub fn viewport_size(&self) -> Size {
self.inner.borrow().viewport
}
pub fn content_size(&self) -> Size {
self.inner.borrow().content_size
}
pub fn current_offset(&self) -> Vector {
let inner = self.inner.borrow();
clamp_shared_offset(inner.axes, inner.viewport, inner.content_size, inner.offset)
}
pub fn max_offset(&self) -> Vector {
let inner = self.inner.borrow();
shared_max_offset(inner.viewport, inner.content_size)
}
pub(crate) fn bind_scroll_view(&self, scroll_view_id: WidgetId, scroll_content_id: WidgetId) {
let mut inner = self.inner.borrow_mut();
inner.scroll_view_id = Some(scroll_view_id);
inner.scroll_content_id = Some(scroll_content_id);
}
fn bind_scroll_bar(&self, scroll_bar_id: WidgetId) {
let mut inner = self.inner.borrow_mut();
if !inner.scroll_bar_ids.contains(&scroll_bar_id) {
inner.scroll_bar_ids.push(scroll_bar_id);
}
}
pub(crate) fn sync_metrics(
&self,
axes: ScrollAxes,
viewport: Size,
content_size: Size,
) -> bool {
let mut inner = self.inner.borrow_mut();
let next_offset = clamp_shared_offset(axes, viewport, content_size, inner.offset);
let changed = inner.axes != axes
|| inner.viewport != viewport
|| inner.content_size != content_size
|| inner.offset != next_offset;
inner.axes = axes;
inner.viewport = viewport;
inner.content_size = content_size;
inner.offset = next_offset;
changed
}
pub(crate) fn sync_unmeasured_axes(&self, axes: ScrollAxes) {
let mut inner = self.inner.borrow_mut();
if inner.viewport == Size::ZERO && inner.content_size == Size::ZERO {
inner.axes = axes;
inner.offset = axis_limited_offset(axes, inner.offset);
}
}
pub fn set_offset(&self, offset: Vector) -> bool {
let mut inner = self.inner.borrow_mut();
let next_offset = if inner.viewport == Size::ZERO && inner.content_size == Size::ZERO {
axis_limited_offset(inner.axes, offset)
} else {
clamp_shared_offset(inner.axes, inner.viewport, inner.content_size, offset)
};
if inner.offset == next_offset {
return false;
}
inner.offset = next_offset;
true
}
pub fn scroll_to_item(&self, index: usize) -> bool {
let mut inner = self.inner.borrow_mut();
if inner.pending_virtual_item == Some(index) {
return false;
}
inner.pending_virtual_item = Some(index);
true
}
pub fn scroll_to_item_with_ctx(&self, index: usize, ctx: &mut EventCtx) -> bool {
if !self.scroll_to_item(index) {
return false;
}
let subscribers = self.subscribers();
if let Some(scroll_view_id) = subscribers.scroll_view_id {
for kind in [
InvalidationKind::Measure,
InvalidationKind::Paint,
InvalidationKind::HitTest,
InvalidationKind::Semantics,
] {
ctx.request(InvalidationRequest::new(
InvalidationTarget::Widget(scroll_view_id),
kind,
));
}
}
for scroll_bar_id in subscribers.scroll_bar_ids {
request_scroll_bar_refresh(ctx, scroll_bar_id);
}
true
}
pub fn virtual_item_at(&self, y: f32) -> Option<usize> {
let inner = self.inner.borrow();
let offsets = &inner.virtual_item_offsets;
if offsets.is_empty() {
return None;
}
Some(
offsets
.partition_point(|top| *top <= y)
.saturating_sub(1)
.min(offsets.len() - 1),
)
}
pub fn first_visible_item(&self) -> Option<usize> {
self.virtual_item_at(self.current_offset().y)
}
pub fn virtual_item_offset(&self, index: usize) -> Option<f32> {
self.inner.borrow().virtual_item_offsets.get(index).copied()
}
pub(crate) fn sync_virtual_item_offsets(&self, offsets: &[f32]) {
let mut inner = self.inner.borrow_mut();
if inner.virtual_item_offsets != offsets {
inner.virtual_item_offsets.clear();
inner.virtual_item_offsets.extend_from_slice(offsets);
}
}
pub(crate) fn take_pending_virtual_item(&self) -> Option<usize> {
self.inner.borrow_mut().pending_virtual_item.take()
}
fn pending_virtual_item(&self) -> Option<usize> {
self.inner.borrow().pending_virtual_item
}
pub(crate) fn subscribers(&self) -> ScrollStateSubscribers {
let inner = self.inner.borrow();
ScrollStateSubscribers {
scroll_view_id: inner.scroll_view_id,
scroll_content_id: inner.scroll_content_id,
scroll_bar_ids: inner.scroll_bar_ids.clone(),
}
}
}
impl Default for ScrollState {
fn default() -> Self {
Self {
inner: Rc::new(RefCell::new(ScrollStateInner {
axes: ScrollAxes::Vertical,
viewport: Size::ZERO,
content_size: Size::ZERO,
offset: Vector::ZERO,
pending_virtual_item: None,
virtual_item_offsets: Vec::new(),
scroll_view_id: None,
scroll_content_id: None,
scroll_bar_ids: Vec::new(),
})),
}
}
}
#[derive(Debug, Clone)]
pub(crate) struct ScrollStateSubscribers {
pub(crate) scroll_view_id: Option<WidgetId>,
pub(crate) scroll_content_id: Option<WidgetId>,
pub(crate) scroll_bar_ids: Vec<WidgetId>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ScrollBarAxis {
Vertical,
Horizontal,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ScrollBarAppearance {
Gutter,
Overlay,
}
#[derive(Debug, Clone, Copy)]
struct ScrollBarMetrics {
track: Rect,
thumb: Rect,
max_scroll: f32,
}
type AnimatedScalar = crate::Progress;
fn set_animation_target(
animation: &mut AnimatedScalar,
target: f32,
duration: f64,
easing: crate::Easing,
ctx: &mut EventCtx,
) -> bool {
animation.animate(target, crate::AnimationSpec::tween(duration, easing), ctx)
}
fn set_hover_animation_target(
animation: &mut AnimatedScalar,
target: f32,
theme: &DefaultTheme,
ctx: &mut EventCtx,
) -> bool {
set_animation_target(
animation,
target,
theme.motion.hover_duration(),
theme.motion.hover_easing(),
ctx,
)
}
fn set_press_animation_target(
animation: &mut AnimatedScalar,
target: f32,
theme: &DefaultTheme,
ctx: &mut EventCtx,
) -> bool {
set_animation_target(
animation,
target,
theme.motion.press_duration(),
theme.motion.press_easing(),
ctx,
)
}
fn set_focus_animation_target(
animation: &mut AnimatedScalar,
target: f32,
theme: &DefaultTheme,
ctx: &mut EventCtx,
) -> bool {
set_animation_target(
animation,
target,
theme.motion.focus_duration(),
theme.motion.focus_easing(),
ctx,
)
}
pub struct ScrollBar {
theme: Box<DefaultTheme>,
theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
state: ScrollState,
axis: ScrollBarAxis,
name: Option<String>,
width: Option<f32>,
hovered: bool,
dragging: bool,
hover_animation: AnimatedScalar,
drag_animation: AnimatedScalar,
focus_animation: AnimatedScalar,
pointer_id: Option<u64>,
drag_thumb_offset: f32,
appearance: ScrollBarAppearance,
focusable: bool,
}
impl ScrollBar {
pub fn vertical(state: ScrollState) -> Self {
Self {
theme: Box::new(DefaultTheme::default()),
theme_reader: None,
state,
axis: ScrollBarAxis::Vertical,
name: None,
width: None,
hovered: false,
dragging: false,
hover_animation: AnimatedScalar::new(0.0),
drag_animation: AnimatedScalar::new(0.0),
focus_animation: AnimatedScalar::new(0.0),
pointer_id: None,
drag_thumb_offset: 0.0,
appearance: ScrollBarAppearance::Gutter,
focusable: true,
}
}
pub fn horizontal(state: ScrollState) -> Self {
Self {
axis: ScrollBarAxis::Horizontal,
..Self::vertical(state)
}
}
pub fn theme(mut self, theme: DefaultTheme) -> Self {
self.theme = Box::new(theme);
self.theme_reader = None;
self
}
pub fn theme_when<F>(mut self, theme: F) -> Self
where
F: Fn() -> DefaultTheme + 'static,
{
self.theme_reader = Some(Box::new(theme));
self
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn width(mut self, width: f32) -> Self {
self.width = Some(width.max(8.0));
self
}
fn overlay(mut self) -> Self {
self.appearance = ScrollBarAppearance::Overlay;
self.focusable = false;
self
}
fn resolved_width(&self) -> f32 {
let theme = self.resolved_theme();
self.width.unwrap_or(theme.metrics.scroll_bar_thickness)
}
fn resolved_track_width(&self, clock: &impl FrameClock) -> f32 {
let expanded = self.resolved_width();
match self.appearance {
ScrollBarAppearance::Gutter => expanded,
ScrollBarAppearance::Overlay => {
let collapsed = OVERLAY_SCROLL_BAR_IDLE_THICKNESS.min(expanded);
collapsed + (expanded - collapsed) * self.hover_animation.get(clock).clamp(0.0, 1.0)
}
}
}
fn resolved_min_thumb_length(&self) -> f32 {
self.resolved_theme().metrics.scroll_bar_min_thumb_length
}
fn resolved_theme(&self) -> DefaultTheme {
self.theme_reader
.as_ref()
.map(|reader| reader())
.unwrap_or(*self.theme)
}
fn track_rect(&self, bounds: Rect, clock: &impl FrameClock) -> Rect {
let width = self.resolved_track_width(clock);
match self.axis {
ScrollBarAxis::Vertical => {
let x = match self.appearance {
ScrollBarAppearance::Gutter => {
bounds.x() + ((bounds.width() - width) * 0.5).max(0.0)
}
ScrollBarAppearance::Overlay => (bounds.max_x() - width).max(bounds.x()),
};
Rect::new(x, bounds.y(), width.min(bounds.width()), bounds.height())
}
ScrollBarAxis::Horizontal => {
let y = match self.appearance {
ScrollBarAppearance::Gutter => {
bounds.y() + ((bounds.height() - width) * 0.5).max(0.0)
}
ScrollBarAppearance::Overlay => (bounds.max_y() - width).max(bounds.y()),
};
Rect::new(bounds.x(), y, bounds.width(), width.min(bounds.height()))
}
}
}
fn metrics(&self, bounds: Rect, clock: &impl FrameClock) -> Option<ScrollBarMetrics> {
let viewport = self.state.viewport_size();
let content = self.state.content_size();
let viewport_extent = self.axis_size(viewport);
let content_extent = self.axis_size(content);
let bounds_extent = self.axis_rect_length(bounds);
if viewport_extent <= 0.0 || content_extent <= viewport_extent || bounds_extent <= 0.0 {
return None;
}
let track = self.track_rect(bounds, clock);
let track_extent = self.axis_rect_length(track);
let ratio = (viewport_extent / content_extent).clamp(0.08, 1.0);
let thumb_extent = (track_extent * ratio)
.max(self.resolved_min_thumb_length())
.min(track_extent);
let max_scroll = (content_extent - viewport_extent).max(0.0);
let travel = (track_extent - thumb_extent).max(0.0);
let offset = self
.axis_offset(self.state.current_offset())
.clamp(0.0, max_scroll.max(0.0));
let thumb_start = self.axis_rect_start(track)
+ if travel <= f32::EPSILON || max_scroll <= f32::EPSILON {
0.0
} else {
travel * (offset / max_scroll)
};
let thumb = match self.axis {
ScrollBarAxis::Vertical => {
Rect::new(track.x(), thumb_start, track.width(), thumb_extent)
}
ScrollBarAxis::Horizontal => {
Rect::new(thumb_start, track.y(), thumb_extent, track.height())
}
};
Some(ScrollBarMetrics {
track,
thumb,
max_scroll,
})
}
fn axis_size(&self, size: Size) -> f32 {
match self.axis {
ScrollBarAxis::Vertical => size.height,
ScrollBarAxis::Horizontal => size.width,
}
}
fn axis_offset(&self, offset: Vector) -> f32 {
match self.axis {
ScrollBarAxis::Vertical => offset.y,
ScrollBarAxis::Horizontal => offset.x,
}
}
fn axis_rect_start(&self, rect: Rect) -> f32 {
match self.axis {
ScrollBarAxis::Vertical => rect.y(),
ScrollBarAxis::Horizontal => rect.x(),
}
}
fn axis_rect_length(&self, rect: Rect) -> f32 {
match self.axis {
ScrollBarAxis::Vertical => rect.height(),
ScrollBarAxis::Horizontal => rect.width(),
}
}
fn axis_rect_max(&self, rect: Rect) -> f32 {
match self.axis {
ScrollBarAxis::Vertical => rect.max_y(),
ScrollBarAxis::Horizontal => rect.max_x(),
}
}
fn pointer_position(&self, position: Point) -> f32 {
match self.axis {
ScrollBarAxis::Vertical => position.y,
ScrollBarAxis::Horizontal => position.x,
}
}
fn set_hovered(&mut self, hovered: bool, ctx: &mut EventCtx) {
if self.hovered != hovered {
let theme = self.resolved_theme();
self.hovered = hovered;
set_hover_animation_target(
&mut self.hover_animation,
hovered as u8 as f32,
&theme,
ctx,
);
ctx.request_paint();
ctx.request_semantics();
}
}
fn set_dragging(&mut self, dragging: bool, ctx: &mut EventCtx) {
if self.dragging != dragging {
let theme = self.resolved_theme();
self.dragging = dragging;
set_press_animation_target(
&mut self.drag_animation,
dragging as u8 as f32,
&theme,
ctx,
);
ctx.request_paint();
ctx.request_semantics();
}
}
fn request_dependents<C>(&self, ctx: &mut C, source_widget_id: WidgetId)
where
C: ScrollInvalidationCtx,
{
let subscribers = self.state.subscribers();
if let Some(scroll_view_id) = subscribers.scroll_view_id
&& scroll_view_id != source_widget_id
{
request_scroll_view_refresh(ctx, scroll_view_id, subscribers.scroll_content_id);
}
for scroll_bar_id in subscribers.scroll_bar_ids {
if scroll_bar_id != source_widget_id {
request_scroll_bar_refresh(ctx, scroll_bar_id);
}
}
}
fn set_axis_offset<C>(
&mut self,
ctx: &mut C,
source_widget_id: WidgetId,
axis_offset: f32,
) -> bool
where
C: ScrollInvalidationCtx,
{
let current = self.state.current_offset();
let next = match self.axis {
ScrollBarAxis::Vertical => Vector::new(current.x, axis_offset),
ScrollBarAxis::Horizontal => Vector::new(axis_offset, current.y),
};
if !self.state.set_offset(next) {
return false;
}
self.request_dependents(ctx, source_widget_id);
true
}
fn set_from_pointer_position<C>(
&mut self,
ctx: &mut C,
source_widget_id: WidgetId,
metrics: ScrollBarMetrics,
pointer_position: f32,
drag_anchor: f32,
) -> bool
where
C: ScrollInvalidationCtx,
{
let thumb_extent = self.axis_rect_length(metrics.thumb);
let travel = (self.axis_rect_length(metrics.track) - thumb_extent).max(0.0);
let thumb_start = (pointer_position - drag_anchor).clamp(
self.axis_rect_start(metrics.track),
self.axis_rect_max(metrics.track) - thumb_extent,
);
let fraction = if travel <= f32::EPSILON {
0.0
} else {
(thumb_start - self.axis_rect_start(metrics.track)) / travel
};
self.set_axis_offset(ctx, source_widget_id, metrics.max_scroll * fraction)
}
fn page_step(&self) -> f32 {
let viewport = self.state.viewport_size();
(self.axis_size(viewport) * 0.85).max(40.0)
}
}
impl Widget for ScrollBar {
fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
let metrics = self.metrics(ctx.bounds(), ctx);
match event {
Event::Pointer(pointer) if pointer.kind == PointerEventKind::Move => {
self.set_hovered(
pointer_supports_hover(pointer.pointer_kind)
&& ctx.bounds().contains(pointer.position),
ctx,
);
if self.dragging
&& self.pointer_id == Some(pointer.pointer_id)
&& let Some(metrics) = metrics
&& self.set_from_pointer_position(
ctx,
ctx.widget_id(),
metrics,
self.pointer_position(pointer.position),
self.drag_thumb_offset,
)
{
ctx.request_paint();
ctx.request_semantics();
ctx.set_handled();
}
}
Event::Pointer(pointer) if pointer.kind == PointerEventKind::Enter => {
self.set_hovered(pointer_supports_hover(pointer.pointer_kind), ctx);
}
Event::Pointer(pointer) if pointer.kind == PointerEventKind::Leave => {
if !self.dragging || self.pointer_id != Some(pointer.pointer_id) {
self.set_hovered(false, ctx);
}
}
Event::Pointer(pointer)
if pointer.kind == PointerEventKind::Down
&& pointer.button == Some(PointerButton::Primary)
&& ctx.bounds().contains(pointer.position) =>
{
let Some(metrics) = metrics else {
return;
};
self.set_hovered(pointer_supports_hover(pointer.pointer_kind), ctx);
self.set_dragging(true, ctx);
self.pointer_id = Some(pointer.pointer_id);
self.drag_thumb_offset = if metrics.thumb.contains(pointer.position) {
self.pointer_position(pointer.position) - self.axis_rect_start(metrics.thumb)
} else {
self.axis_rect_length(metrics.thumb) * 0.5
};
let _ = self.set_from_pointer_position(
ctx,
ctx.widget_id(),
metrics,
self.pointer_position(pointer.position),
self.drag_thumb_offset,
);
ctx.request_pointer_capture(pointer.pointer_id);
if self.focusable {
ctx.request_focus();
}
ctx.request_paint();
ctx.request_semantics();
ctx.set_handled();
}
Event::Pointer(pointer)
if pointer.kind == PointerEventKind::Up
&& pointer.button == Some(PointerButton::Primary)
&& self.pointer_id == Some(pointer.pointer_id) =>
{
if self.dragging
&& let Some(metrics) = metrics
{
let _ = self.set_from_pointer_position(
ctx,
ctx.widget_id(),
metrics,
self.pointer_position(pointer.position),
self.drag_thumb_offset,
);
}
self.set_dragging(false, ctx);
self.pointer_id = None;
self.set_hovered(
pointer_supports_hover(pointer.pointer_kind)
&& ctx.bounds().contains(pointer.position),
ctx,
);
ctx.release_pointer_capture(pointer.pointer_id);
ctx.set_handled();
}
Event::Pointer(pointer)
if pointer.kind == PointerEventKind::Cancel
&& self.pointer_id == Some(pointer.pointer_id) =>
{
self.set_dragging(false, ctx);
self.pointer_id = None;
self.set_hovered(false, ctx);
ctx.release_pointer_capture(pointer.pointer_id);
ctx.set_handled();
}
Event::Semantics(semantics) if semantics.target == ctx.widget_id() => {
let current = f64::from(self.axis_offset(self.state.current_offset()));
let next = match &semantics.action {
SemanticsActionRequest::Increment => Some(current + 40.0),
SemanticsActionRequest::Decrement => Some(current - 40.0),
SemanticsActionRequest::SetValue(SemanticsValue::Number(value)) => Some(*value),
SemanticsActionRequest::SetValue(SemanticsValue::Range { value, .. }) => {
Some(*value)
}
_ => None,
};
let Some(next) = next.filter(|value| value.is_finite()) else {
return;
};
let next = next as f32;
if !next.is_finite() {
return;
}
if self.set_axis_offset(ctx, ctx.widget_id(), next) {
ctx.request_paint();
ctx.request_semantics();
}
ctx.set_handled();
}
Event::Keyboard(key) if ctx.is_focused() && key.state == KeyState::Pressed => {
let max_scroll = self.axis_offset(self.state.max_offset());
if max_scroll <= f32::EPSILON {
return;
}
let current = self.axis_offset(self.state.current_offset());
let next = match key.key.as_str() {
"ArrowUp" if self.axis == ScrollBarAxis::Vertical => Some(current - 40.0),
"ArrowDown" if self.axis == ScrollBarAxis::Vertical => Some(current + 40.0),
"ArrowLeft" if self.axis == ScrollBarAxis::Horizontal => Some(current - 40.0),
"ArrowRight" if self.axis == ScrollBarAxis::Horizontal => Some(current + 40.0),
"PageUp" => Some(current - self.page_step()),
"PageDown" => Some(current + self.page_step()),
"Home" => Some(0.0),
"End" => Some(max_scroll),
_ => None,
};
if let Some(next) = next {
if self.set_axis_offset(ctx, ctx.widget_id(), next) {
ctx.request_paint();
ctx.request_semantics();
}
ctx.set_handled();
}
}
_ => {}
}
}
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
self.state.bind_scroll_bar(ctx.widget_id());
let width = self.resolved_width();
let desired = match self.axis {
ScrollBarAxis::Vertical => Size::new(
width,
if constraints.max.height.is_finite() {
constraints.max.height.max(0.0)
} else {
40.0
}
.max(40.0),
),
ScrollBarAxis::Horizontal => Size::new(
if constraints.max.width.is_finite() {
constraints.max.width.max(0.0)
} else {
40.0
}
.max(40.0),
width,
),
};
constraints.clamp(desired)
}
fn paint(&self, ctx: &mut PaintCtx) {
let Some(metrics) = self.metrics(ctx.bounds(), ctx) else {
return;
};
let theme = self.resolved_theme();
let palette = theme.palette;
let track = metrics.track;
let thumb = metrics.thumb;
let track_radius = (track.width() * 0.5).min(track.height() * 0.5);
let thumb_radius = (thumb.width() * 0.5).min(thumb.height() * 0.5);
let interaction = self
.hover_animation
.get(ctx)
.max(self.drag_animation.get(ctx))
.max(self.focus_animation.get(ctx));
let track_alpha = match self.appearance {
ScrollBarAppearance::Gutter => 0.7,
ScrollBarAppearance::Overlay => 0.08 + 0.32 * interaction,
};
let thumb_alpha = match self.appearance {
ScrollBarAppearance::Gutter => 0.95,
ScrollBarAppearance::Overlay => 0.68 + 0.27 * interaction,
};
let border_alpha = match self.appearance {
ScrollBarAppearance::Gutter => 0.9,
ScrollBarAppearance::Overlay => 0.42 + 0.36 * interaction,
};
ctx.fill(
Path::rounded_rect(track, track_radius),
palette.control_active.with_alpha(track_alpha),
);
let thumb = snap_to_pixels(ctx, thumb);
ctx.fill(
Path::rounded_rect(thumb, thumb_radius),
mix_color(
mix_color(
palette.placeholder,
palette.border_strong,
self.hover_animation
.get(ctx)
.max(self.focus_animation.get(ctx)),
),
palette.text_muted,
self.drag_animation.get(ctx),
)
.clamped()
.with_alpha(thumb_alpha),
);
stroke_border(
ctx,
thumb,
thumb_radius,
physical_pixels(ctx, theme.metrics.border_width.max(1.0)),
mix_color(
palette.border.with_alpha(border_alpha),
palette.focus_ring,
self.focus_animation.get(ctx),
)
.clamped(),
);
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
let max_scroll = self.axis_offset(self.state.max_offset()).max(0.0);
let current = self
.axis_offset(self.state.current_offset())
.clamp(0.0, max_scroll.max(0.0));
let mut node = SemanticsNode::new(ctx.widget_id(), SemanticsRole::Slider, ctx.bounds());
node.name = self.name.clone();
node.value = Some(SemanticsValue::Range {
value: f64::from(current),
min: 0.0,
max: f64::from(max_scroll),
});
node.state.disabled = max_scroll <= f32::EPSILON;
node.state.focused = ctx.is_focused();
node.state.hovered = self.hovered;
node.actions = if self.focusable {
vec![
SemanticsAction::Focus,
SemanticsAction::Increment,
SemanticsAction::Decrement,
SemanticsAction::SetValue,
]
} else {
vec![
SemanticsAction::Increment,
SemanticsAction::Decrement,
SemanticsAction::SetValue,
]
};
ctx.push(node);
}
fn accepts_focus(&self) -> bool {
self.focusable
}
fn focus_changed(&mut self, ctx: &mut EventCtx, focused: bool) {
let theme = self.resolved_theme();
set_focus_animation_target(&mut self.focus_animation, focused as u8 as f32, &theme, ctx);
ctx.request_paint();
ctx.request_semantics();
}
}
const OVERLAY_SCROLL_BAR_IDLE_THICKNESS: f32 = 4.0;
fn pointer_supports_hover(pointer_kind: PointerKind) -> bool {
matches!(pointer_kind, PointerKind::Mouse | PointerKind::Pen)
}
pub(crate) fn scroll_bar_gutter(
enabled: bool,
axes: ScrollAxes,
outer: Size,
content: Size,
thickness: f32,
) -> Size {
let mut gutter = Size::ZERO;
if enabled {
for _ in 0..3 {
sui_runtime::record_scrollbar_gutter_iteration();
let viewport = scroll_viewport_size(outer, gutter);
let next = Size::new(
if axes.allows_vertical() && content.height > viewport.height + f32::EPSILON {
thickness.max(0.0).min(outer.width.max(0.0))
} else {
gutter.width
},
if axes.allows_horizontal() && content.width > viewport.width + f32::EPSILON {
thickness.max(0.0).min(outer.height.max(0.0))
} else {
gutter.height
},
);
if next == gutter {
break;
}
gutter = next;
}
}
gutter
}
pub(crate) fn scroll_viewport_size(outer: Size, gutter: Size) -> Size {
Size::new(
(outer.width - gutter.width).max(0.0),
(outer.height - gutter.height).max(0.0),
)
}
pub(crate) struct ScrollBars {
corner: SingleChild,
vertical: Option<SingleChild>,
horizontal: Option<SingleChild>,
show_vertical: bool,
show_horizontal: bool,
}
impl ScrollBars {
pub(crate) fn new(
state: ScrollState,
theme: Rc<RefCell<DefaultTheme>>,
name: Option<&str>,
axes: ScrollAxes,
) -> Self {
let vertical = axes.allows_vertical().then(|| {
let vertical_theme = Rc::clone(&theme);
let vertical_name = name
.map(|name| format!("{name} vertical scroll bar"))
.unwrap_or_else(|| "Vertical scroll bar".to_string());
SingleChild::new_with_paint_boundary(
ScrollBar::vertical(state.clone())
.theme_when(move || *vertical_theme.borrow())
.name(vertical_name)
.overlay(),
)
});
let horizontal = axes.allows_horizontal().then(|| {
let horizontal_theme = Rc::clone(&theme);
let horizontal_name = name
.map(|name| format!("{name} horizontal scroll bar"))
.unwrap_or_else(|| "Horizontal scroll bar".to_string());
SingleChild::new_with_paint_boundary(
ScrollBar::horizontal(state)
.theme_when(move || *horizontal_theme.borrow())
.name(horizontal_name)
.overlay(),
)
});
Self {
corner: SingleChild::new(SizedBox::new()),
vertical,
horizontal,
show_vertical: false,
show_horizontal: false,
}
}
pub(crate) fn set_visibility(&mut self, axes: ScrollAxes, max_offset: Vector) {
let show_vertical =
self.vertical.is_some() && axes.allows_vertical() && max_offset.y > f32::EPSILON;
let show_horizontal =
self.horizontal.is_some() && axes.allows_horizontal() && max_offset.x > f32::EPSILON;
self.show_vertical = show_vertical;
self.show_horizontal = show_horizontal;
}
pub(crate) fn measure(&mut self, ctx: &mut MeasureCtx, viewport: Size, thickness: f32) {
self.corner
.measure(ctx, Constraints::tight(Size::new(thickness, thickness)));
if let Some(vertical) = &mut self.vertical {
vertical.measure(
ctx,
Constraints::tight(Size::new(thickness, viewport.height.max(0.0))),
);
}
if let Some(horizontal) = &mut self.horizontal {
horizontal.measure(
ctx,
Constraints::tight(Size::new(viewport.width.max(0.0), thickness)),
);
}
}
pub(crate) fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect, thickness: f32) {
let gutter = Size::new(
if self.show_vertical {
thickness.min(bounds.width()).max(0.0)
} else {
0.0
},
if self.show_horizontal {
thickness.min(bounds.height()).max(0.0)
} else {
0.0
},
);
let viewport = scroll_viewport_size(bounds.size, gutter);
if let Some(vertical) = &mut self.vertical {
vertical.arrange(
ctx,
Rect::new(
bounds.x() + viewport.width,
bounds.y(),
gutter.width,
if self.show_vertical {
viewport.height
} else {
0.0
},
),
);
}
if let Some(horizontal) = &mut self.horizontal {
horizontal.arrange(
ctx,
Rect::new(
bounds.x(),
bounds.y() + viewport.height,
if self.show_horizontal {
viewport.width
} else {
0.0
},
gutter.height,
),
);
}
self.corner.arrange(
ctx,
Rect::new(
bounds.x() + viewport.width,
bounds.y() + viewport.height,
gutter.width,
gutter.height,
),
);
}
pub(crate) fn paint(&self, ctx: &mut PaintCtx) {
if let Some(vertical) = &self.vertical {
vertical.paint(ctx);
}
if let Some(horizontal) = &self.horizontal {
horizontal.paint(ctx);
}
}
pub(crate) fn semantics(&self, ctx: &mut SemanticsCtx) {
if self.show_vertical
&& let Some(vertical) = &self.vertical
{
vertical.semantics(ctx);
}
if self.show_horizontal
&& let Some(horizontal) = &self.horizontal
{
horizontal.semantics(ctx);
}
}
pub(crate) fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
self.corner.visit_children(visitor);
if let Some(vertical) = &self.vertical {
vertical.visit_children(visitor);
}
if let Some(horizontal) = &self.horizontal {
horizontal.visit_children(visitor);
}
}
pub(crate) fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
self.corner.visit_children_mut(visitor);
if let Some(vertical) = &mut self.vertical {
vertical.visit_children_mut(visitor);
}
if let Some(horizontal) = &mut self.horizontal {
horizontal.visit_children_mut(visitor);
}
}
}
pub struct ScrollView {
theme: Box<DefaultTheme>,
theme_reader: Option<Rc<dyn Fn() -> DefaultTheme>>,
scroll_bar_theme: Rc<RefCell<DefaultTheme>>,
name: Option<String>,
state: ScrollState,
overflow_x: Overflow,
overflow_y: Overflow,
content_width: ContentExtent,
content_height: ContentExtent,
shrink_height: bool,
gutter: Size,
offset: Vector,
content_size: Size,
focus_animation: AnimatedScalar,
retain_content: bool,
scroll_bars: bool,
bars: Option<ScrollBars>,
touch_scroll: Option<TouchScrollGesture>,
touch_fling: TouchFling,
offset_change: OffsetChange,
drag_auto_scroll: DragAutoScroll,
child: SingleChild,
}
impl ScrollView {
pub fn new<W>(child: W) -> Self
where
W: Widget + 'static,
{
let theme = DefaultTheme::default();
Self {
theme: Box::new(theme),
theme_reader: None,
scroll_bar_theme: Rc::new(RefCell::new(theme)),
name: None,
state: ScrollState::new(),
overflow_x: Overflow::Clip,
overflow_y: Overflow::Auto,
content_width: ContentExtent::Natural,
content_height: ContentExtent::Natural,
shrink_height: false,
gutter: Size::ZERO,
offset: Vector::ZERO,
content_size: Size::ZERO,
focus_animation: AnimatedScalar::new(0.0),
retain_content: false,
scroll_bars: true,
bars: None,
touch_scroll: None,
touch_fling: TouchFling::default(),
offset_change: OffsetChange::default(),
drag_auto_scroll: DragAutoScroll::default(),
child: SingleChild::new(child),
}
}
pub fn vertical<W>(child: W) -> Self
where
W: Widget + 'static,
{
Self::new(child)
}
pub fn horizontal<W>(child: W) -> Self
where
W: Widget + 'static,
{
Self::new(child).axes(ScrollAxes::Horizontal)
}
pub fn both<W>(child: W) -> Self
where
W: Widget + 'static,
{
Self::new(child).axes(ScrollAxes::Both)
}
pub fn axes(mut self, axes: ScrollAxes) -> Self {
self.overflow_x = if axes.allows_horizontal() {
Overflow::Auto
} else {
Overflow::Clip
};
self.overflow_y = if axes.allows_vertical() {
Overflow::Auto
} else {
Overflow::Clip
};
self.content_width = ContentExtent::Natural;
self.content_height = ContentExtent::Natural;
self.state.sync_unmeasured_axes(self.scroll_axes());
self
}
pub fn overflow(mut self, overflow: Overflow) -> Self {
self.overflow_x = overflow;
self.overflow_y = overflow;
self.content_width = if overflow.is_scrollable() {
ContentExtent::Viewport
} else {
ContentExtent::Natural
};
self.state.sync_unmeasured_axes(self.scroll_axes());
self
}
pub fn overflow_x(mut self, overflow: Overflow) -> Self {
self.overflow_x = overflow;
self.content_width = if overflow.is_scrollable() {
ContentExtent::Viewport
} else {
ContentExtent::Natural
};
self.state.sync_unmeasured_axes(self.scroll_axes());
self
}
pub fn overflow_y(mut self, overflow: Overflow) -> Self {
self.overflow_y = overflow;
self.state.sync_unmeasured_axes(self.scroll_axes());
self
}
pub const fn content_width(mut self, extent: ContentExtent) -> Self {
self.content_width = extent;
self
}
pub const fn content_height(mut self, extent: ContentExtent) -> Self {
self.content_height = extent;
self
}
pub const fn shrink_height(mut self, enabled: bool) -> Self {
self.shrink_height = enabled;
self
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn theme(mut self, theme: DefaultTheme) -> Self {
self.theme = Box::new(theme);
self.theme_reader = None;
*self.scroll_bar_theme.borrow_mut() = theme;
self
}
pub fn theme_when<F>(mut self, theme: F) -> Self
where
F: Fn() -> DefaultTheme + 'static,
{
self.theme_reader = Some(Rc::new(theme));
self
}
pub fn state(mut self, state: ScrollState) -> Self {
self.state = state;
self.state.sync_unmeasured_axes(self.scroll_axes());
self.bars = None;
self
}
pub fn overlay_scroll_bars(self, enabled: bool) -> Self {
self.scroll_bars(enabled)
}
pub fn auto_scroll_on_drag(mut self, enabled: bool) -> Self {
self.drag_auto_scroll.enabled = enabled;
self
}
pub fn on_offset_change<F>(mut self, callback: F) -> Self
where
F: Fn(Vector) + 'static,
{
self.offset_change.callback = Some(Rc::new(callback));
self
}
pub fn on_offset_change_with_ctx<F>(mut self, callback: F) -> Self
where
F: Fn(&mut EventCtx, Vector) + 'static,
{
self.offset_change.callback_with_ctx = Some(Rc::new(callback));
self
}
pub fn scroll_bars(mut self, enabled: bool) -> Self {
self.scroll_bars = enabled;
if !enabled {
self.bars = None;
}
self
}
pub const fn viewport_size_hint(mut self, enabled: bool) -> Self {
self.content_width = if enabled {
ContentExtent::Viewport
} else {
ContentExtent::Natural
};
self.content_height = if enabled {
ContentExtent::Viewport
} else {
ContentExtent::Natural
};
self
}
pub const fn viewport_width_hint(mut self, enabled: bool) -> Self {
self.content_width = if enabled {
ContentExtent::Viewport
} else {
ContentExtent::Natural
};
self
}
pub const fn viewport_height_hint(mut self, enabled: bool) -> Self {
self.content_height = if enabled {
ContentExtent::Viewport
} else {
ContentExtent::Natural
};
self
}
pub fn retain_content_layer(mut self) -> Self {
self.retain_content = true;
let child = self.child;
self.child = child.with_paint_boundary();
self
}
pub const fn current_offset(&self) -> Vector {
self.offset
}
pub fn set_offset(&mut self, offset: Vector) {
self.state.sync_unmeasured_axes(self.scroll_axes());
let _ = self.state.set_offset(offset);
self.offset = self.state.current_offset();
}
pub fn child_pod(&self) -> &WidgetPod {
self.child.child()
}
pub fn child_mut(&mut self) -> &mut WidgetPod {
self.child.child_mut()
}
pub fn replace_child<W>(&mut self, child: W)
where
W: Widget + 'static,
{
self.child = if self.retain_content {
SingleChild::new_with_paint_boundary(child)
} else {
SingleChild::new(child)
};
}
fn clamp_offset(&self, viewport: Size, offset: Vector) -> Vector {
let max_x = (self.content_size.width - viewport.width).max(0.0);
let max_y = (self.content_size.height - viewport.height).max(0.0);
Vector::new(
if self.overflow_x.is_scrollable() {
offset.x.clamp(0.0, max_x)
} else {
0.0
},
if self.overflow_y.is_scrollable() {
offset.y.clamp(0.0, max_y)
} else {
0.0
},
)
}
fn scroll_axes(&self) -> ScrollAxes {
match (
self.overflow_x.is_scrollable(),
self.overflow_y.is_scrollable(),
) {
(true, true) => ScrollAxes::Both,
(true, false) => ScrollAxes::Horizontal,
(false, true) => ScrollAxes::Vertical,
(false, false) => ScrollAxes::None,
}
}
fn resolved_theme(&self) -> DefaultTheme {
self.theme_reader
.as_ref()
.map(|reader| reader())
.unwrap_or(*self.theme)
}
fn sync_scroll_bar_theme(&self) -> DefaultTheme {
let theme = self.resolved_theme();
*self.scroll_bar_theme.borrow_mut() = theme;
theme
}
fn ensure_scroll_bars(&mut self) {
let axes = self.scroll_axes();
if self.scroll_bars && axes != ScrollAxes::None && self.bars.is_none() {
self.bars = Some(ScrollBars::new(
self.state.clone(),
Rc::clone(&self.scroll_bar_theme),
self.name.as_deref(),
axes,
));
}
}
fn touch_delta(&self, pointer: &PointerEvent) -> Vector {
let delta = Vector::new(-pointer.delta.x, -pointer.delta.y);
match self.scroll_axes() {
ScrollAxes::None => Vector::ZERO,
ScrollAxes::Vertical => Vector::new(0.0, delta.y),
ScrollAxes::Horizontal => Vector::new(delta.x, 0.0),
ScrollAxes::Both => delta,
}
}
fn has_touch_overflow(&self) -> bool {
let max_offset = self.state.max_offset();
(self.scroll_axes().allows_horizontal() && max_offset.x > f32::EPSILON)
|| (self.scroll_axes().allows_vertical() && max_offset.y > f32::EPSILON)
}
fn handle_touch_pointer(&mut self, ctx: &mut EventCtx, pointer: &PointerEvent, viewport: Size) {
if pointer.pointer_kind != PointerKind::Touch {
if pointer.kind == PointerEventKind::Down && ctx.bounds().contains(pointer.position) {
self.touch_fling.stop();
}
return;
}
match pointer.kind {
PointerEventKind::Down
if pointer.is_primary
&& pointer.button == Some(PointerButton::Primary)
&& self.viewport_rect(ctx.bounds()).contains(pointer.position)
&& self.has_touch_overflow() =>
{
self.touch_scroll = Some(TouchScrollGesture::new(pointer, ctx.current_time()));
if self.touch_fling.stop() {
ctx.set_handled();
}
}
PointerEventKind::Move => {
let Some(gesture) = self.touch_scroll else {
return;
};
if !gesture.matches(pointer) || ctx.phase() == EventPhase::Capture {
return;
}
if let Some(gesture) = &mut self.touch_scroll {
gesture.velocity.push(ctx.current_time(), pointer.position);
}
if !gesture.dragging && !gesture.passed_threshold(pointer, self.scroll_axes()) {
return;
}
let delta = self.touch_delta(pointer);
let relevant_delta = delta.x.abs().max(delta.y.abs());
if relevant_delta <= f32::EPSILON {
if gesture.dragging {
ctx.set_handled();
}
return;
}
if self.scroll_by(viewport, delta, ctx) {
if !gesture.dragging {
if let Some(gesture) = &mut self.touch_scroll {
gesture.dragging = true;
}
ctx.request_pointer_capture(pointer.pointer_id);
}
ctx.set_handled();
} else if !gesture.dragging {
if let Some(gesture) = &mut self.touch_scroll {
gesture.dragging = true;
}
ctx.request_pointer_capture(pointer.pointer_id);
}
}
PointerEventKind::Up | PointerEventKind::Cancel => {
let Some(gesture) = self.touch_scroll else {
return;
};
if !gesture.matches(pointer) {
return;
}
self.touch_scroll = None;
if gesture.dragging && ctx.phase() != EventPhase::Capture {
ctx.release_pointer_capture(pointer.pointer_id);
ctx.set_handled();
if pointer.kind == PointerEventKind::Up {
let velocity =
gesture.release_velocity(ctx.current_time(), self.scroll_axes());
self.touch_fling.start(ctx, velocity);
}
}
}
_ => {}
}
}
fn should_clip_paint(&self) -> bool {
self.overflow_x.clips_paint() || self.overflow_y.clips_paint()
}
fn viewport_rect(&self, bounds: Rect) -> Rect {
Rect::from_origin_size(
bounds.origin,
scroll_viewport_size(bounds.size, self.gutter),
)
}
fn clip_rect(&self, bounds: Rect) -> Rect {
let bounds = self.viewport_rect(bounds);
let large = 1_000_000.0;
let x = if self.overflow_x.clips_paint() {
bounds.x()
} else {
-large
};
let width = if self.overflow_x.clips_paint() {
bounds.width()
} else {
large * 2.0
};
let y = if self.overflow_y.clips_paint() {
bounds.y()
} else {
-large
};
let height = if self.overflow_y.clips_paint() {
bounds.height()
} else {
large * 2.0
};
Rect::new(x, y, width, height)
}
fn scroll_by(&mut self, viewport: Size, delta: Vector, ctx: &mut EventCtx) -> bool {
let next = self.clamp_offset(viewport, self.offset + delta);
if next != self.offset {
self.offset = next;
self.publish_state(ctx, viewport);
ctx.request_arrange();
if !self.retain_content {
ctx.request_paint();
}
ctx.request(InvalidationRequest::new(
InvalidationTarget::Widget(self.child.child().id()),
InvalidationKind::Transform,
));
ctx.request_semantics();
ctx.refresh_drag_targets();
self.offset_change.notify(ctx, self.offset);
true
} else {
false
}
}
fn sync_state<C>(&mut self, ctx: &mut C, viewport: Size)
where
C: ScrollInvalidationCtx + ScrollWidgetCtx,
{
let state = &self.state;
state.bind_scroll_view(ctx.widget_id(), self.child.child().id());
if state.sync_metrics(self.scroll_axes(), viewport, self.content_size) {
for scroll_bar_id in state.subscribers().scroll_bar_ids {
request_scroll_bar_refresh(ctx, scroll_bar_id);
}
}
self.offset = self.clamp_offset(viewport, state.current_offset());
if state.set_offset(self.offset) {
for scroll_bar_id in state.subscribers().scroll_bar_ids {
request_scroll_bar_refresh(ctx, scroll_bar_id);
}
}
self.offset_change.defer(ctx, self.offset);
}
fn publish_state(&self, ctx: &mut EventCtx, viewport: Size) {
let state = &self.state;
state.bind_scroll_view(ctx.widget_id(), self.child.child().id());
let _ = state.sync_metrics(self.scroll_axes(), viewport, self.content_size);
if state.set_offset(self.offset) {
for scroll_bar_id in state.subscribers().scroll_bar_ids {
if scroll_bar_id != ctx.widget_id() {
request_scroll_bar_refresh(ctx, scroll_bar_id);
}
}
}
}
}
pub struct VirtualScrollView {
theme: Box<DefaultTheme>,
theme_reader: Option<Rc<dyn Fn() -> DefaultTheme>>,
scroll_bar_theme: Rc<RefCell<DefaultTheme>>,
name: Option<String>,
padding: Insets,
spacing: f32,
state: ScrollState,
offset_y: f32,
last_arranged_offset_y: f32,
content_height: f32,
item_offsets: Vec<f32>,
visible_range: Range<usize>,
focus_animation: AnimatedScalar,
scroll_bars: bool,
bars: Option<ScrollBars>,
touch_scroll: Option<TouchScrollGesture>,
touch_fling: TouchFling,
offset_change: OffsetChange,
drag_auto_scroll: DragAutoScroll,
children: WidgetChildren,
}
impl VirtualScrollView {
pub fn new() -> Self {
let theme = DefaultTheme::default();
Self {
theme: Box::new(theme),
theme_reader: None,
scroll_bar_theme: Rc::new(RefCell::new(theme)),
name: None,
padding: Insets::ZERO,
spacing: 0.0,
state: ScrollState::new(),
offset_y: 0.0,
last_arranged_offset_y: 0.0,
content_height: 0.0,
item_offsets: Vec::new(),
visible_range: 0..0,
focus_animation: AnimatedScalar::new(0.0),
scroll_bars: true,
bars: None,
touch_scroll: None,
touch_fling: TouchFling::default(),
offset_change: OffsetChange::default(),
drag_auto_scroll: DragAutoScroll::default(),
children: WidgetChildren::new(),
}
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn theme(mut self, theme: DefaultTheme) -> Self {
self.theme = Box::new(theme);
self.theme_reader = None;
*self.scroll_bar_theme.borrow_mut() = theme;
self
}
pub fn theme_when<F>(mut self, theme: F) -> Self
where
F: Fn() -> DefaultTheme + 'static,
{
self.theme_reader = Some(Rc::new(theme));
self
}
pub fn padding(mut self, padding: Insets) -> Self {
self.padding = padding;
self
}
pub fn gap(mut self, gap: f32) -> Self {
self.spacing = gap.max(0.0);
self
}
pub fn state(mut self, state: ScrollState) -> Self {
self.state = state;
self.state.sync_unmeasured_axes(ScrollAxes::Vertical);
self.bars = None;
self
}
pub fn overlay_scroll_bars(self, enabled: bool) -> Self {
self.scroll_bars(enabled)
}
pub fn auto_scroll_on_drag(mut self, enabled: bool) -> Self {
self.drag_auto_scroll.enabled = enabled;
self
}
pub fn on_offset_change<F>(mut self, callback: F) -> Self
where
F: Fn(Vector) + 'static,
{
self.offset_change.callback = Some(Rc::new(callback));
self
}
pub fn on_offset_change_with_ctx<F>(mut self, callback: F) -> Self
where
F: Fn(&mut EventCtx, Vector) + 'static,
{
self.offset_change.callback_with_ctx = Some(Rc::new(callback));
self
}
pub fn scroll_bars(mut self, enabled: bool) -> Self {
self.scroll_bars = enabled;
if !enabled {
self.bars = None;
}
self
}
pub fn with_child<W>(mut self, child: W) -> Self
where
W: Widget + 'static,
{
self.children.push(child);
self
}
pub fn push<W>(&mut self, child: W)
where
W: Widget + 'static,
{
self.children.push(child);
}
pub const fn current_offset(&self) -> Vector {
Vector::new(0.0, self.offset_y)
}
pub fn set_offset(&mut self, offset: Vector) {
self.offset_y = offset.y.max(0.0);
let _ = self.state.set_offset(Vector::new(0.0, self.offset_y));
self.offset_y = self.state.current_offset().y;
}
fn resolved_theme(&self) -> DefaultTheme {
self.theme_reader
.as_ref()
.map(|reader| reader())
.unwrap_or(*self.theme)
}
fn sync_scroll_bar_theme(&self) -> DefaultTheme {
let theme = self.resolved_theme();
*self.scroll_bar_theme.borrow_mut() = theme;
theme
}
fn ensure_scroll_bars(&mut self) {
if self.scroll_bars && self.bars.is_none() {
self.bars = Some(ScrollBars::new(
self.state.clone(),
Rc::clone(&self.scroll_bar_theme),
self.name.as_deref(),
ScrollAxes::Vertical,
));
}
}
fn has_touch_overflow(&self) -> bool {
self.state.max_offset().y > f32::EPSILON
}
fn handle_touch_pointer(&mut self, ctx: &mut EventCtx, pointer: &PointerEvent, viewport: Rect) {
if pointer.pointer_kind != PointerKind::Touch {
if pointer.kind == PointerEventKind::Down && ctx.bounds().contains(pointer.position) {
self.touch_fling.stop();
}
return;
}
match pointer.kind {
PointerEventKind::Down
if pointer.is_primary
&& pointer.button == Some(PointerButton::Primary)
&& ctx.bounds().contains(pointer.position)
&& self.has_touch_overflow() =>
{
self.touch_scroll = Some(TouchScrollGesture::new(pointer, ctx.current_time()));
if self.touch_fling.stop() {
ctx.set_handled();
}
}
PointerEventKind::Move => {
let Some(gesture) = self.touch_scroll else {
return;
};
if !gesture.matches(pointer) || ctx.phase() == EventPhase::Capture {
return;
}
if let Some(gesture) = &mut self.touch_scroll {
gesture.velocity.push(ctx.current_time(), pointer.position);
}
if !gesture.dragging && !gesture.passed_threshold(pointer, ScrollAxes::Vertical) {
return;
}
let delta_y = -pointer.delta.y;
if delta_y.abs() <= f32::EPSILON {
if gesture.dragging {
ctx.set_handled();
}
return;
}
if self.scroll_by(viewport, delta_y, ctx) {
if !gesture.dragging {
if let Some(gesture) = &mut self.touch_scroll {
gesture.dragging = true;
}
ctx.request_pointer_capture(pointer.pointer_id);
}
ctx.set_handled();
} else if !gesture.dragging {
if let Some(gesture) = &mut self.touch_scroll {
gesture.dragging = true;
}
ctx.request_pointer_capture(pointer.pointer_id);
}
}
PointerEventKind::Up | PointerEventKind::Cancel => {
let Some(gesture) = self.touch_scroll else {
return;
};
if !gesture.matches(pointer) {
return;
}
self.touch_scroll = None;
if gesture.dragging && ctx.phase() != EventPhase::Capture {
ctx.release_pointer_capture(pointer.pointer_id);
ctx.set_handled();
if pointer.kind == PointerEventKind::Up {
let velocity =
gesture.release_velocity(ctx.current_time(), ScrollAxes::Vertical);
self.touch_fling.start(ctx, velocity);
}
}
}
_ => {}
}
}
fn viewport_rect(&self, bounds: Rect) -> Rect {
let viewport = inset_rect(bounds, self.padding);
let gutter = scroll_bar_gutter(
self.scroll_bars,
ScrollAxes::Vertical,
viewport.size,
Size::new(0.0, self.content_height),
self.resolved_theme().metrics.scroll_bar_thickness,
);
Rect::from_origin_size(viewport.origin, scroll_viewport_size(viewport.size, gutter))
}
fn clamp_offset(&self, viewport_height: f32, offset_y: f32) -> f32 {
let max_scroll = (self.content_height - viewport_height).max(0.0);
offset_y.clamp(0.0, max_scroll)
}
fn visible_range_for_offset(&self, viewport_height: f32, offset_y: f32) -> Range<usize> {
let overdraw = viewport_height * 0.75;
let visible_top = (offset_y - overdraw).max(0.0);
let visible_bottom = offset_y + viewport_height + overdraw;
let child_count = self.children.len();
let mut start = 0;
while start < child_count {
let row_top = self.item_offsets[start];
let row_bottom = row_top + self.children.as_slice()[start].measured_size().height;
if row_bottom >= visible_top {
break;
}
start += 1;
}
let mut end = start;
while end < child_count {
if self.item_offsets[end] > visible_bottom {
break;
}
end += 1;
}
start..end
}
fn exposed_viewport_strip(&self, viewport: Rect, previous_offset_y: f32) -> Option<Rect> {
let delta_y = self.offset_y - previous_offset_y;
if delta_y.abs() <= f32::EPSILON || viewport.is_empty() {
return None;
}
let strip_height = delta_y.abs().min(viewport.height());
if strip_height <= 0.0 {
return None;
}
if delta_y > 0.0 {
Some(Rect::new(
viewport.x(),
viewport.max_y() - strip_height,
viewport.width(),
strip_height,
))
} else {
Some(Rect::new(
viewport.x(),
viewport.y(),
viewport.width(),
strip_height,
))
}
}
fn scroll_by(&mut self, viewport: Rect, delta_y: f32, ctx: &mut EventCtx) -> bool {
let previous_offset_y = self.offset_y;
let next = self.clamp_offset(viewport.height(), previous_offset_y + delta_y);
if (next - previous_offset_y).abs() > f32::EPSILON {
self.offset_y = next;
self.publish_state(ctx, viewport.size);
ctx.request_arrange();
if let Some(exposed_strip) = self.exposed_viewport_strip(viewport, previous_offset_y) {
ctx.request_paint_rect(exposed_strip);
}
for child in self.visible_children() {
ctx.request(InvalidationRequest::new(
InvalidationTarget::Widget(child.id()),
InvalidationKind::Transform,
));
}
ctx.request_semantics();
ctx.refresh_drag_targets();
self.offset_change
.notify(ctx, Vector::new(0.0, self.offset_y));
true
} else {
false
}
}
fn sync_state<C>(&mut self, ctx: &mut C, viewport: Size)
where
C: ScrollInvalidationCtx + ScrollWidgetCtx,
{
let state = &self.state;
state.bind_scroll_view(ctx.widget_id(), ctx.widget_id());
state.sync_virtual_item_offsets(&self.item_offsets);
let content_size = Size::new(viewport.width, self.content_height);
let mut state_changed = state.sync_metrics(ScrollAxes::Vertical, viewport, content_size);
if let Some(index) = state.take_pending_virtual_item()
&& let Some(item_offset) = self.item_offsets.get(index).copied()
{
let target_offset = self.clamp_offset(viewport.height, item_offset);
state_changed |= state.set_offset(Vector::new(0.0, target_offset));
}
self.offset_y = self.clamp_offset(viewport.height, state.current_offset().y);
state_changed |= state.set_offset(Vector::new(0.0, self.offset_y));
if state_changed {
for scroll_bar_id in state.subscribers().scroll_bar_ids {
request_scroll_bar_refresh(ctx, scroll_bar_id);
}
}
self.offset_change
.defer(ctx, Vector::new(0.0, self.offset_y));
}
fn publish_state(&self, ctx: &mut EventCtx, viewport: Size) {
let state = &self.state;
state.bind_scroll_view(ctx.widget_id(), ctx.widget_id());
let content_size = Size::new(viewport.width, self.content_height);
let _ = state.sync_metrics(ScrollAxes::Vertical, viewport, content_size);
if state.set_offset(Vector::new(0.0, self.offset_y)) {
for scroll_bar_id in state.subscribers().scroll_bar_ids {
if scroll_bar_id != ctx.widget_id() {
request_scroll_bar_refresh(ctx, scroll_bar_id);
}
}
}
}
fn update_visible_range(&mut self, viewport_height: f32) {
self.visible_range = self.visible_range_for_offset(viewport_height, self.offset_y);
}
fn visible_children(&self) -> &[WidgetPod] {
&self.children.as_slice()[self.visible_range.clone()]
}
fn pending_visible_range(&self) -> Option<Range<usize>> {
let index = self.state.pending_virtual_item()?;
let offset_y = self.item_offsets.get(index).copied()?;
Some(self.visible_range_for_offset(self.state.viewport_size().height, offset_y))
}
}
impl Default for VirtualScrollView {
fn default() -> Self {
Self::new()
}
}
impl Widget for ScrollView {
fn supports_output_reuse(&self) -> bool {
self.theme_reader.is_none()
}
fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
self.sync_scroll_bar_theme();
let viewport = self.viewport_rect(ctx.bounds()).size;
if let Event::Pointer(pointer) = event {
self.handle_touch_pointer(ctx, pointer, viewport);
}
if matches!(event, Event::Wake(WakeEvent::AnimationFrame { .. })) {
self.offset_change.notify(ctx, self.offset);
}
match event {
Event::Pointer(pointer)
if pointer.kind == sui_core::PointerEventKind::Scroll
&& ctx.phase() != EventPhase::Capture
&& ctx.bounds().contains(pointer.position) =>
{
let delta = pointer
.scroll_delta
.map(scroll_delta_to_offset)
.unwrap_or(pointer.delta);
self.touch_fling.stop();
if self.scroll_by(viewport, Vector::new(-delta.x, -delta.y), ctx) {
ctx.set_handled();
}
}
Event::Drag(drag) if ctx.phase() != EventPhase::Capture => {
let axes = (
self.overflow_x.is_scrollable(),
self.overflow_y.is_scrollable(),
);
let visible = self.viewport_rect(ctx.bounds());
self.drag_auto_scroll.follow(ctx, drag, visible, axes);
}
Event::Wake(WakeEvent::AnimationFrame { delta, .. })
if self.drag_auto_scroll.is_scrolling() =>
{
let step = self.drag_auto_scroll.step(*delta);
if step == Vector::ZERO || self.scroll_by(viewport, step, ctx) {
ctx.request_animation_frame();
} else {
self.drag_auto_scroll.stop();
}
}
Event::Wake(WakeEvent::AnimationFrame { delta, .. })
if self.touch_fling.is_active() =>
{
let step = self.touch_fling.step(*delta);
let before = self.offset;
self.scroll_by(viewport, step, ctx);
self.touch_fling.block(step, self.offset - before);
if self.touch_fling.is_active() {
ctx.request_animation_frame();
}
}
Event::Keyboard(key)
if ctx.phase() != EventPhase::Capture
&& ctx.is_focused()
&& key.state == sui_core::KeyState::Pressed =>
{
self.touch_fling.stop();
let line = 40.0;
let page = (viewport.height * 0.85).max(line);
let delta = match key.key.as_str() {
"ArrowUp" => Some(Vector::new(0.0, -line)),
"ArrowDown" => Some(Vector::new(0.0, line)),
"ArrowLeft" => Some(Vector::new(-line, 0.0)),
"ArrowRight" => Some(Vector::new(line, 0.0)),
"PageUp" => Some(Vector::new(0.0, -page)),
"PageDown" => Some(Vector::new(0.0, page)),
"Home" => Some(Vector::new(-self.offset.x, -self.offset.y)),
"End" => Some(Vector::new(
self.content_size.width - viewport.width - self.offset.x,
self.content_size.height - viewport.height - self.offset.y,
)),
_ => None,
};
if let Some(delta) = delta
&& self.scroll_by(viewport, delta, ctx)
{
ctx.set_handled();
}
}
Event::Pointer(pointer)
if pointer.kind == sui_core::PointerEventKind::Down
&& ctx.phase() != EventPhase::Capture
&& ctx.bounds().contains(pointer.position) =>
{
ctx.request_focus();
}
_ => {}
}
}
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
let theme = self.sync_scroll_bar_theme();
self.ensure_scroll_bars();
let previous_gutter = self.gutter;
self.gutter = Size::ZERO;
let mut outer = constraints.min;
for _ in 0..3 {
let available = scroll_viewport_size(constraints.max, self.gutter);
let hint = Size::new(
if available.width.is_finite() {
available.width
} else {
constraints.min.width
},
if available.height.is_finite() {
available.height
} else {
constraints.min.height
},
);
let width_intrinsic = if self.overflow_x.is_scrollable()
&& self.content_width == ContentExtent::MinContent
{
self.child
.child_mut()
.intrinsic_size(ctx, Axis::Horizontal, hint.height)
} else {
IntrinsicSize::default()
};
let height_intrinsic = if self.overflow_y.is_scrollable()
&& self.content_height == ContentExtent::MinContent
{
self.child
.child_mut()
.intrinsic_size(ctx, Axis::Vertical, hint.width)
} else {
IntrinsicSize::default()
};
let mut child_constraints = Constraints::new(Size::ZERO, available);
if self.overflow_x.is_scrollable() {
(child_constraints.min.width, child_constraints.max.width) = self
.content_width
.resolve(hint.width, width_intrinsic.minimum);
} else if available.width.is_finite() {
child_constraints.min.width = available.width;
}
if self.overflow_y.is_scrollable() {
(child_constraints.min.height, child_constraints.max.height) = self
.content_height
.resolve(hint.height, height_intrinsic.minimum);
} else if available.height.is_finite() {
child_constraints.min.height = available.height;
}
self.content_size = self.child.measure(ctx, child_constraints);
outer = constraints.clamp(Size::new(
if constraints.max.width.is_finite() {
constraints.max.width
} else {
self.content_size.width + self.gutter.width
},
if self.shrink_height || !constraints.max.height.is_finite() {
self.content_size.height + self.gutter.height
} else {
constraints.max.height
},
));
let next = scroll_bar_gutter(
self.scroll_bars,
self.scroll_axes(),
outer,
self.content_size,
theme.metrics.scroll_bar_thickness,
);
if next == self.gutter {
break;
}
self.gutter = next;
}
if self.gutter != previous_gutter {
ctx.request_paint();
}
self.sync_state(ctx, scroll_viewport_size(outer, self.gutter));
let axes = self.scroll_axes();
let max_offset = self.state.max_offset();
if let Some(bars) = &mut self.bars {
bars.set_visibility(axes, max_offset);
bars.measure(ctx, outer, theme.metrics.scroll_bar_thickness);
}
outer
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
let theme = self.sync_scroll_bar_theme();
let previous_offset = self.offset;
self.gutter = scroll_bar_gutter(
self.scroll_bars,
self.scroll_axes(),
bounds.size,
self.content_size,
theme.metrics.scroll_bar_thickness,
);
let viewport = self.viewport_rect(bounds);
self.sync_state(ctx, viewport.size);
if self.offset != previous_offset && !self.retain_content {
ctx.request_paint();
}
let measured = self.child.child().measured_size();
let child_size = Size::new(
if self.overflow_x == Overflow::Clip {
viewport.width()
} else {
measured.width
},
if self.overflow_y == Overflow::Clip {
viewport.height()
} else {
measured.height
},
);
self.child.arrange(
ctx,
Rect::from_origin_size(
Point::new(bounds.x() - self.offset.x, bounds.y() - self.offset.y),
child_size,
),
);
let axes = self.scroll_axes();
let max_offset = self.state.max_offset();
if let Some(bars) = &mut self.bars {
bars.set_visibility(axes, max_offset);
bars.arrange(ctx, bounds, theme.metrics.scroll_bar_thickness);
}
}
fn paint(&self, ctx: &mut PaintCtx) {
self.sync_scroll_bar_theme();
if self.should_clip_paint() {
ctx.push_clip_rect(self.clip_rect(ctx.bounds()));
self.child.paint(ctx);
ctx.pop_clip();
} else {
self.child.paint(ctx);
}
if let Some(bars) = &self.bars {
ctx.push_clip_rect(ctx.bounds());
bars.paint(ctx);
ctx.pop_clip();
}
}
fn layer_options(&self) -> LayerOptions {
LayerOptions {
paint_boundary: PaintBoundaryMode::Explicit,
composition_mode: if self.scroll_axes() == ScrollAxes::None {
LayerCompositionMode::Normal
} else {
LayerCompositionMode::Scroll
},
}
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
let mut node = SemanticsNode::new(ctx.widget_id(), SemanticsRole::ScrollView, ctx.bounds());
node.name = self.name.clone();
node.actions = vec![SemanticsAction::Focus];
node.state.focused = ctx.is_focused();
ctx.push(node);
self.child.semantics(ctx);
if let Some(bars) = &self.bars {
bars.semantics(ctx);
}
}
fn accepts_focus(&self) -> bool {
true
}
fn focus_changed(&mut self, ctx: &mut EventCtx, focused: bool) {
let theme = self.sync_scroll_bar_theme();
set_focus_animation_target(&mut self.focus_animation, focused as u8 as f32, &theme, ctx);
ctx.request_paint();
ctx.request_semantics();
}
fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
self.child.visit_children(visitor);
if let Some(bars) = &self.bars {
bars.visit_children(visitor);
}
}
fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
self.child.visit_children_mut(visitor);
if let Some(bars) = &mut self.bars {
bars.visit_children_mut(visitor);
}
}
}
impl Widget for VirtualScrollView {
fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
self.sync_scroll_bar_theme();
let viewport = self.viewport_rect(ctx.bounds());
if let Event::Pointer(pointer) = event {
self.handle_touch_pointer(ctx, pointer, viewport);
}
if matches!(event, Event::Wake(WakeEvent::AnimationFrame { .. })) {
self.offset_change
.notify(ctx, Vector::new(0.0, self.offset_y));
}
match event {
Event::Pointer(pointer)
if pointer.kind == sui_core::PointerEventKind::Scroll
&& ctx.phase() != EventPhase::Capture
&& ctx.bounds().contains(pointer.position) =>
{
let delta = pointer
.scroll_delta
.map(scroll_delta_to_offset)
.unwrap_or(pointer.delta);
self.touch_fling.stop();
if self.scroll_by(viewport, -delta.y, ctx) {
ctx.set_handled();
}
}
Event::Drag(drag) if ctx.phase() != EventPhase::Capture => {
self.drag_auto_scroll
.follow(ctx, drag, viewport, (false, true));
}
Event::Wake(WakeEvent::AnimationFrame { delta, .. })
if self.drag_auto_scroll.is_scrolling() =>
{
let step = self.drag_auto_scroll.step(*delta).y;
if step == 0.0 || self.scroll_by(viewport, step, ctx) {
ctx.request_animation_frame();
} else {
self.drag_auto_scroll.stop();
}
}
Event::Wake(WakeEvent::AnimationFrame { delta, .. })
if self.touch_fling.is_active() =>
{
let step = self.touch_fling.step(*delta);
let before = self.offset_y;
self.scroll_by(viewport, step.y, ctx);
self.touch_fling
.block(step, Vector::new(0.0, self.offset_y - before));
if self.touch_fling.is_active() {
ctx.request_animation_frame();
}
}
Event::Keyboard(key)
if ctx.phase() != EventPhase::Capture
&& ctx.is_focused()
&& key.state == sui_core::KeyState::Pressed =>
{
self.touch_fling.stop();
let line = 40.0;
let page = (viewport.height() * 0.85).max(line);
let delta = match key.key.as_str() {
"ArrowUp" => Some(-line),
"ArrowDown" => Some(line),
"PageUp" => Some(-page),
"PageDown" => Some(page),
"Home" => Some(-self.offset_y),
"End" => Some(self.content_height - viewport.height() - self.offset_y),
_ => None,
};
if let Some(delta) = delta
&& self.scroll_by(viewport, delta, ctx)
{
ctx.set_handled();
}
}
Event::Pointer(pointer)
if pointer.kind == sui_core::PointerEventKind::Down
&& ctx.phase() != EventPhase::Capture
&& ctx.bounds().contains(pointer.position) =>
{
ctx.request_focus();
}
_ => {}
}
}
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
let theme = self.sync_scroll_bar_theme();
self.ensure_scroll_bars();
let previous_content_height = self.content_height;
let previous_item_offsets = std::mem::take(&mut self.item_offsets);
let previous_visible_range = self.visible_range.clone();
let mut gutter = 0.0;
let mut size = constraints.min;
for _ in 0..3 {
let available_width = if constraints.max.width.is_finite() {
(constraints.max.width - self.padding.left - self.padding.right - gutter).max(0.0)
} else {
f32::INFINITY
};
let child_constraints = Constraints::new(
Size::new(
if available_width.is_finite() {
available_width
} else {
0.0
},
0.0,
),
Size::new(available_width, f32::INFINITY),
);
self.item_offsets.clear();
self.item_offsets.reserve(self.children.len());
let mut content_width: f32 = 0.0;
let mut content_height = 0.0;
for child in self.children.as_mut_slice() {
let child_size = child.measure(ctx, child_constraints);
self.item_offsets.push(content_height);
content_width = content_width.max(child_size.width);
content_height += child_size.height + self.spacing;
}
if !self.item_offsets.is_empty() {
content_height -= self.spacing;
}
self.content_height = content_height;
size = constraints.clamp(Size::new(
if constraints.max.width.is_finite() {
constraints.max.width
} else {
content_width + self.padding.left + self.padding.right + gutter
},
if constraints.max.height.is_finite() {
constraints.max.height
} else {
content_height + self.padding.top + self.padding.bottom
},
));
let inner = inset_rect(Rect::from_origin_size(Point::ZERO, size), self.padding);
let next = scroll_bar_gutter(
self.scroll_bars,
ScrollAxes::Vertical,
inner.size,
Size::new(0.0, self.content_height),
theme.metrics.scroll_bar_thickness,
)
.width;
if next == gutter {
break;
}
gutter = next;
}
let viewport = self.viewport_rect(Rect::from_origin_size(Point::ZERO, size));
self.sync_state(ctx, viewport.size);
self.update_visible_range(viewport.height());
let max_offset = self.state.max_offset();
if let Some(bars) = &mut self.bars {
bars.set_visibility(ScrollAxes::Vertical, max_offset);
bars.measure(ctx, size, theme.metrics.scroll_bar_thickness);
}
if previous_content_height != self.content_height
|| previous_item_offsets != self.item_offsets
|| previous_visible_range != self.visible_range
{
ctx.request_paint();
}
size
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
let theme = self.sync_scroll_bar_theme();
let viewport = self.viewport_rect(bounds);
let previous_offset_y = self.offset_y;
self.sync_state(ctx, viewport.size);
if (self.offset_y - previous_offset_y).abs() > f32::EPSILON {
ctx.request_paint();
}
let previous_visible_range = self.visible_range.clone();
let previous_arranged_offset_y = self.last_arranged_offset_y;
self.update_visible_range(viewport.height());
if self.visible_range != previous_visible_range
&& (self.offset_y - previous_arranged_offset_y).abs() <= f32::EPSILON
{
ctx.request_paint();
}
self.last_arranged_offset_y = self.offset_y;
let viewport_width = viewport.width();
let visible_range = self.visible_range.clone();
let offset_y = self.offset_y;
let item_offsets = self.item_offsets[visible_range.clone()].to_vec();
for (relative_index, child) in self.children.as_mut_slice()[visible_range]
.iter_mut()
.enumerate()
{
let child_size = child.measured_size();
child.arrange(
ctx,
Rect::from_origin_size(
Point::new(
viewport.x(),
viewport.y() + item_offsets[relative_index] - offset_y,
),
Size::new(
if viewport_width.is_finite() {
viewport_width
} else {
child_size.width
},
child_size.height,
),
),
);
}
let max_offset = self.state.max_offset();
if let Some(bars) = &mut self.bars {
bars.set_visibility(ScrollAxes::Vertical, max_offset);
bars.arrange(ctx, bounds, theme.metrics.scroll_bar_thickness);
}
}
fn paint(&self, ctx: &mut PaintCtx) {
self.sync_scroll_bar_theme();
let viewport = self.viewport_rect(ctx.bounds());
ctx.push_clip_rect(viewport);
for child in self.visible_children() {
child.paint(ctx);
}
ctx.pop_clip();
if let Some(bars) = &self.bars {
ctx.push_clip_rect(ctx.bounds());
bars.paint(ctx);
ctx.pop_clip();
}
}
fn layer_options(&self) -> LayerOptions {
LayerOptions {
paint_boundary: PaintBoundaryMode::Explicit,
composition_mode: LayerCompositionMode::Scroll,
}
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
let mut node = SemanticsNode::new(ctx.widget_id(), SemanticsRole::ScrollView, ctx.bounds());
node.name = self.name.clone();
node.actions = vec![SemanticsAction::Focus];
node.state.focused = ctx.is_focused();
ctx.push(node);
for child in self.visible_children() {
child.semantics(ctx);
}
if let Some(bars) = &self.bars {
bars.semantics(ctx);
}
}
fn accepts_focus(&self) -> bool {
true
}
fn focus_changed(&mut self, ctx: &mut EventCtx, focused: bool) {
let theme = self.sync_scroll_bar_theme();
set_focus_animation_target(&mut self.focus_animation, focused as u8 as f32, &theme, ctx);
ctx.request_paint();
ctx.request_semantics();
}
fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
let pending_range = self.pending_visible_range();
for (index, child) in self.children.as_slice().iter().enumerate() {
if self.visible_range.contains(&index)
|| pending_range
.as_ref()
.is_some_and(|range| range.contains(&index))
{
visitor.visit(child);
}
}
if let Some(bars) = &self.bars {
bars.visit_children(visitor);
}
}
fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
let visible_range = self.visible_range.clone();
let pending_range = self.pending_visible_range();
for (index, child) in self.children.as_mut_slice().iter_mut().enumerate() {
if visible_range.contains(&index)
|| pending_range
.as_ref()
.is_some_and(|range| range.contains(&index))
{
visitor.visit(child);
}
}
if let Some(bars) = &mut self.bars {
bars.visit_children_mut(visitor);
}
}
}
fn scroll_delta_to_offset(delta: ScrollDelta) -> Vector {
match delta {
ScrollDelta::Lines(delta) => Vector::new(delta.x * 40.0, delta.y * 40.0),
ScrollDelta::Pixels(delta) => delta,
}
}
fn axis_limited_offset(axes: ScrollAxes, offset: Vector) -> Vector {
Vector::new(
if axes.allows_horizontal() {
offset.x.max(0.0)
} else {
0.0
},
if axes.allows_vertical() {
offset.y.max(0.0)
} else {
0.0
},
)
}
fn shared_max_offset(viewport: Size, content_size: Size) -> Vector {
Vector::new(
(content_size.width - viewport.width).max(0.0),
(content_size.height - viewport.height).max(0.0),
)
}
fn clamp_shared_offset(
axes: ScrollAxes,
viewport: Size,
content_size: Size,
offset: Vector,
) -> Vector {
let max = shared_max_offset(viewport, content_size);
Vector::new(
if axes.allows_horizontal() {
offset.x.clamp(0.0, max.x)
} else {
0.0
},
if axes.allows_vertical() {
offset.y.clamp(0.0, max.y)
} else {
0.0
},
)
}
pub(crate) trait ScrollInvalidationCtx {
fn push_invalidation(&mut self, request: InvalidationRequest);
fn request_animation_frame(&mut self);
}
pub(crate) trait ScrollWidgetCtx {
fn widget_id(&self) -> WidgetId;
}
impl ScrollInvalidationCtx for EventCtx {
fn push_invalidation(&mut self, request: InvalidationRequest) {
self.request(request);
}
fn request_animation_frame(&mut self) {
EventCtx::request_animation_frame(self);
}
}
impl ScrollInvalidationCtx for MeasureCtx {
fn push_invalidation(&mut self, request: InvalidationRequest) {
self.request(request);
}
fn request_animation_frame(&mut self) {
MeasureCtx::request_animation_frame(self);
}
}
impl ScrollInvalidationCtx for ArrangeCtx {
fn push_invalidation(&mut self, request: InvalidationRequest) {
self.request(request);
}
fn request_animation_frame(&mut self) {
ArrangeCtx::request_animation_frame(self);
}
}
impl ScrollWidgetCtx for EventCtx {
fn widget_id(&self) -> WidgetId {
self.widget_id()
}
}
impl ScrollWidgetCtx for MeasureCtx {
fn widget_id(&self) -> WidgetId {
self.widget_id()
}
}
impl ScrollWidgetCtx for ArrangeCtx {
fn widget_id(&self) -> WidgetId {
self.widget_id()
}
}
pub(crate) fn request_scroll_bar_refresh<C>(ctx: &mut C, widget_id: WidgetId)
where
C: ScrollInvalidationCtx,
{
for kind in [
InvalidationKind::Paint,
InvalidationKind::HitTest,
InvalidationKind::Semantics,
] {
ctx.push_invalidation(InvalidationRequest::new(
InvalidationTarget::Widget(widget_id),
kind,
));
}
}
fn request_scroll_view_refresh<C>(ctx: &mut C, widget_id: WidgetId, content_id: Option<WidgetId>)
where
C: ScrollInvalidationCtx,
{
for kind in [InvalidationKind::Arrange, InvalidationKind::Semantics] {
ctx.push_invalidation(InvalidationRequest::new(
InvalidationTarget::Widget(widget_id),
kind,
));
}
if let Some(content_id) = content_id {
ctx.push_invalidation(InvalidationRequest::new(
InvalidationTarget::Widget(content_id),
InvalidationKind::Transform,
));
}
}
fn inset_constraints(constraints: Constraints, insets: Insets) -> Constraints {
let horizontal = insets.left + insets.right;
let vertical = insets.top + insets.bottom;
Constraints::new(
Size::new(
(constraints.min.width - horizontal).max(0.0),
(constraints.min.height - vertical).max(0.0),
),
Size::new(
(constraints.max.width - horizontal).max(0.0),
(constraints.max.height - vertical).max(0.0),
),
)
}
fn expand_size(size: Size, insets: Insets) -> Size {
Size::new(
size.width + insets.left + insets.right,
size.height + insets.top + insets.bottom,
)
}
fn aligned_child_constraints(
constraints: Constraints,
horizontal: Alignment,
vertical: Alignment,
) -> Constraints {
Constraints::new(
Size::new(
if horizontal == Alignment::Stretch && constraints.max.width.is_finite() {
constraints.max.width
} else {
0.0
},
if vertical == Alignment::Stretch && constraints.max.height.is_finite() {
constraints.max.height
} else {
0.0
},
),
constraints.max,
)
}
fn stretched_dimension(alignment: Alignment, max: f32, fallback: f32) -> f32 {
if alignment == Alignment::Stretch && max.is_finite() {
max
} else {
fallback
}
}
fn aligned_offset(alignment: Alignment, remaining: f32) -> f32 {
let remaining = remaining.max(0.0);
match alignment {
Alignment::Start | Alignment::Stretch => 0.0,
Alignment::Center => remaining * 0.5,
Alignment::End => remaining,
}
}
fn sized_box_constraints(
constraints: Constraints,
width: Option<f32>,
height: Option<f32>,
) -> Constraints {
Constraints::new(
Size::new(width.unwrap_or(0.0), height.unwrap_or(0.0)),
Size::new(
width.unwrap_or(constraints.max.width),
height.unwrap_or(constraints.max.height),
),
)
}
fn axis_main(axis: Axis, size: Size) -> f32 {
match axis {
Axis::Horizontal => size.width,
Axis::Vertical => size.height,
}
}
fn axis_cross(axis: Axis, size: Size) -> f32 {
match axis {
Axis::Horizontal => size.height,
Axis::Vertical => size.width,
}
}
fn axis_size(axis: Axis, main: f32, cross: f32) -> Size {
match axis {
Axis::Horizontal => Size::new(main, cross),
Axis::Vertical => Size::new(cross, main),
}
}
fn stack_arranged_child_size(
axis: Axis,
alignment: Alignment,
measured: Size,
cross_available: f32,
) -> Size {
let main = axis_main(axis, measured);
let measured_cross = axis_cross(axis, measured);
let cross = if alignment == Alignment::Stretch {
cross_available
} else {
measured_cross.min(cross_available)
};
axis_size(axis, main, cross)
}
fn axis_point(axis: Axis, main: f32, cross: f32) -> Point {
match axis {
Axis::Horizontal => Point::new(main, cross),
Axis::Vertical => Point::new(cross, main),
}
}
fn stack_child_constraints(
axis: Axis,
remaining_main: f32,
max_cross: f32,
stretch_cross: bool,
) -> Constraints {
let min_cross = if stretch_cross { max_cross } else { 0.0 };
match axis {
Axis::Horizontal => Constraints::new(
Size::new(0.0, min_cross),
Size::new(remaining_main, max_cross),
),
Axis::Vertical => Constraints::new(
Size::new(min_cross, 0.0),
Size::new(max_cross, remaining_main),
),
}
}
#[cfg(test)]
mod tests {
use std::{
cell::{Cell, RefCell},
rc::Rc,
};
use super::{
Align, Background, ContentExtent, Dock, FixedPaneSplit, Flex, MeasuredBottomDock, Overflow,
Padding, RebuildOnChange, RebuildOnConstraints, ScrollAxes, ScrollBar, ScrollState,
ScrollView, SemanticRegion, SizedBox, Stack, SwitchView, TrailingSlotRow,
VirtualScrollView,
};
use crate::{DefaultTheme, SplitView};
use sui_core::{
Color, Event, InvalidationKind, InvalidationRequest, InvalidationTarget, Point,
PointerButton, PointerButtons, PointerEvent, PointerEventKind, PointerKind, Rect,
ScrollDelta, SemanticsAction, SemanticsActionRequest, SemanticsNode, SemanticsRole,
SemanticsValue, Size, Vector, WidgetId, WindowEvent,
};
use sui_layout::{Alignment, Axis, Constraints, FlexItem, FlexWrap, Padding as Insets};
use sui_reactive::Signal;
use sui_runtime::{
Application, ArrangeCtx, EventCtx, EventPhase, LayerOptions, MeasureCtx, PaintBoundaryMode,
PaintCtx, RenderOutput, Runtime, SemanticsCtx, SingleChild, Widget, WidgetGraphSnapshot,
WidgetNodeSnapshot, WidgetPod, WidgetPodMutVisitor, WidgetPodVisitor, WindowBuilder,
};
use sui_scene::{Brush, LayerCompositionMode, SceneCommand, SceneLayerDescriptor};
#[test]
fn flex_reads_and_replaces_each_childs_flex_item() {
let mut flex = Flex::horizontal()
.with_child(crate::Label::new("Fixed"))
.with_item(crate::Label::new("Fill"), FlexItem::flex(1.0));
assert_eq!(flex.flex_items().len(), 2);
assert_eq!(flex.flex_item(1), Some(FlexItem::flex(1.0)));
assert!(flex.set_flex_item(0, FlexItem::fixed(40.0)));
assert_eq!(flex.flex_item(0), Some(FlexItem::fixed(40.0)));
assert!(!flex.set_flex_item(2, FlexItem::new()));
assert_eq!(flex.flex_item(2), None);
}
#[test]
fn retained_output_matches_full_callbacks_across_text_dpi_and_style_changes() {
use crate::{Button, Grid, Label};
use sui_layout::GridTrack;
use sui_render_wgpu::WgpuRenderer;
struct Gate {
child: SingleChild,
reuse: bool,
}
impl Widget for Gate {
fn supports_output_reuse(&self) -> bool {
self.reuse
}
fn measure(&mut self, c: &mut MeasureCtx, k: Constraints) -> Size {
self.child.measure(c, k)
}
fn arrange(&mut self, c: &mut ArrangeCtx, r: Rect) {
self.child.arrange(c, r);
}
fn paint(&self, c: &mut PaintCtx) {
self.child.paint(c);
}
fn semantics(&self, c: &mut SemanticsCtx) {
self.child.semantics(c);
}
fn visit_children(&self, v: &mut dyn WidgetPodVisitor) {
self.child.visit_children(v);
}
fn visit_children_mut(&mut self, v: &mut dyn WidgetPodMutVisitor) {
self.child.visit_children_mut(v);
}
}
let text = Signal::new("Initial text".to_string());
let mut runtimes = [Runtime::new(), Runtime::new()];
let mut renderers = [WgpuRenderer::new(), WgpuRenderer::new()];
let windows: Vec<_> = runtimes
.iter_mut()
.enumerate()
.map(|(i, runtime)| {
let mut grid =
Grid::new([GridTrack::Fraction(1.0), GridTrack::Fraction(1.0)]).gap(3.0);
for index in 0..12 {
let label = if index == 0 {
Label::new("").text_from(text.clone())
} else {
Label::new("Stable text")
};
grid.push(
Stack::horizontal()
.with_child(label)
.with_child(Button::new(format!("Button {index}"))),
);
}
runtime
.add_window(
WindowBuilder::new()
.initial_size(Size::new(640.0, 320.0))
.root(Gate {
child: SingleChild::new(grid),
reuse: i == 0,
}),
)
.unwrap()
})
.collect();
for revision in 0..4 {
text.set(
match revision {
1 => "A longer line for measurement",
3 => "Another revision",
_ => "Short",
}
.to_string(),
);
let mut captures = Vec::new();
let mut semantic_names = Vec::new();
for ((runtime, renderer), window) in
runtimes.iter_mut().zip(&mut renderers).zip(&windows)
{
if revision == 2 {
runtime
.handle_event(
*window,
Event::Window(sui_core::WindowEvent::ScaleFactorChanged {
scale_factor: 1.25,
raw_dpi: None,
suggested_size: Some(Size::new(640.0, 320.0)),
}),
)
.unwrap();
}
if revision == 3 {
sui_runtime::set_window_render_options(
*window,
sui_runtime::WindowRenderOptions::new(false, 1.0)
.with_optical_vertical_text_alignment_enabled(false),
);
}
let output = runtime.render(*window).unwrap();
semantic_names.push(
output
.semantics
.iter()
.map(|n| n.name.clone())
.collect::<Vec<_>>(),
);
renderer.render(&output.frame).unwrap();
captures.push(renderer.capture_rgba(*window).unwrap());
}
assert_eq!(semantic_names[0], semantic_names[1]);
assert_eq!(
captures[0].pixels(),
captures[1].pixels(),
"revision {revision}"
);
}
}
#[test]
fn axis_flex_matches_full_probe_geometry_after_text_and_viewport_changes() {
use crate::Label;
use sui_layout::{FlexMeasurePhase, flex_layout, flex_layout_with_probes};
struct ProbeMode {
flex: Flex,
legacy: bool,
}
impl Widget for ProbeMode {
fn measure_axis(&mut self, ctx: &mut MeasureCtx, c: Constraints, axis: Axis) -> f32 {
if !self.legacy {
return self.flex.measure_axis(ctx, c, axis);
}
let size = self.measure_size(ctx, c);
match axis {
Axis::Horizontal => size.width,
Axis::Vertical => size.height,
}
}
fn measure_size(&mut self, ctx: &mut MeasureCtx, c: Constraints) -> Size {
if !self.legacy {
return self.flex.measure_size(ctx, c);
}
flex_layout(self.flex.style, &self.flex.items, c, |i, c| {
self.flex.children.as_mut_slice()[i].probe_measure(ctx, c)
})
.size
}
fn measure(&mut self, ctx: &mut MeasureCtx, c: Constraints) -> Size {
if !self.legacy {
return self.flex.measure(ctx, c);
}
flex_layout_with_probes(self.flex.style, &self.flex.items, c, |i, c, phase| {
let child = &mut self.flex.children.as_mut_slice()[i];
match phase {
FlexMeasurePhase::Probe => child.probe_measure(ctx, c),
FlexMeasurePhase::Commit => child.measure(ctx, c),
}
})
.size
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
self.flex.arrange(ctx, bounds);
}
fn visit_children(&self, v: &mut dyn WidgetPodVisitor) {
self.flex.visit_children(v);
}
fn visit_children_mut(&mut self, v: &mut dyn WidgetPodMutVisitor) {
self.flex.visit_children_mut(v);
}
}
fn tree(legacy: bool, depth: usize, text: Signal<String>) -> ProbeMode {
let flex = if depth == 0 {
Flex::vertical()
.gap(2.0)
.with_child(Label::new("").text_from(text))
.with_child(Label::new("A second wrapping label with several words"))
} else {
Flex::horizontal()
.gap(2.0)
.with_child(Label::new(format!("Depth {depth}")))
.with_child(Padding::all(1.0, tree(legacy, depth - 1, text)))
};
ProbeMode { flex, legacy }
}
let text = Signal::new("Wrapping text with variable width".to_string());
let mut runtimes = [Runtime::new(), Runtime::new()];
let windows: Vec<_> = runtimes
.iter_mut()
.enumerate()
.map(|(index, runtime)| {
runtime
.add_window(
WindowBuilder::new()
.initial_size(Size::new(640.0, 320.0))
.root(tree(index == 0, 5, text.clone())),
)
.unwrap()
})
.collect();
for (width, content) in [
(640.0, "Initial contents"),
(347.25, "A much longer revision wrapping at a new width"),
(720.5, "Short"),
] {
text.set(content.to_string());
let bounds: Vec<Vec<_>> = runtimes
.iter_mut()
.zip(&windows)
.map(|(runtime, window)| {
runtime
.handle_event(
*window,
Event::Window(sui_core::WindowEvent::Resized(Size::new(width, 320.0))),
)
.unwrap();
runtime.render(*window).unwrap();
runtime
.widget_graph(*window)
.unwrap()
.nodes
.iter()
.map(|n| n.bounds)
.collect()
})
.collect();
assert_eq!(bounds[0], bounds[1]);
}
}
#[test]
fn projected_flex_queries_match_full_probes_with_wrap_and_cross_dependent_children() {
use std::sync::Arc;
use sui_layout::{FlexStyle, FlexWrap, flex_layout};
struct Responsive(usize);
impl Widget for Responsive {
fn measure_size(&mut self, _: &mut MeasureCtx, c: Constraints) -> Size {
c.clamp(Size::new(
(45.0 + self.0 as f32 * 7.0).min(c.max.height * 2.0),
7.0 * (120.0 / c.max.width.max(1.0)).ceil(),
))
}
}
for axis in [Axis::Horizontal, Axis::Vertical] {
for wrap in [FlexWrap::NoWrap, FlexWrap::Wrap] {
for width in [31.25, 117.5, 480.0] {
for alignment in [Alignment::Start, Alignment::Stretch] {
let style = FlexStyle::new(axis)
.wrap(wrap)
.gap(3.5)
.align_items(alignment);
let items = [
FlexItem::new(),
FlexItem::flex(1.0).min_width(8.0).max_height(45.0),
FlexItem::new().basis_fraction(0.3),
FlexItem::fixed(23.0),
];
let c = Constraints::new(Size::new(4.0, 9.0), Size::new(width, 180.0));
let mut ctx = MeasureCtx::with_layout(
sui_core::WindowId::new(1),
WidgetId::new(1),
Rect::ZERO,
sui_layout::LayoutContext::new(
sui_core::DpiInfo::default(),
Arc::new(sui_text::TextSystem::new()),
Arc::new(sui_text::FontRegistry::new()),
Arc::new(sui_scene::ImageRegistry::new()),
),
);
let expected = flex_layout(style, &items, c, |index, c| {
Responsive(index).measure_size(&mut ctx, c)
})
.size;
let mut flex = Flex::new(axis);
flex.style = style;
for (index, item) in items.into_iter().enumerate() {
flex.push_item(Responsive(index), item);
}
assert_eq!(flex.measure_size(&mut ctx, c), expected);
assert_eq!(
flex.measure_axis(&mut ctx, c, Axis::Horizontal),
expected.width
);
assert_eq!(
flex.measure_axis(&mut ctx, c, Axis::Vertical),
expected.height
);
}
}
}
}
}
#[test]
fn nested_flex_probes_commit_leaf_layout_only_after_constraints_are_resolved() {
struct Leaf(Rc<Cell<u32>>, Rc<Cell<f32>>);
impl Widget for Leaf {
fn measure_size(&mut self, _: &mut MeasureCtx, c: Constraints) -> Size {
c.clamp(Size::new(30.0, 12.0))
}
fn measure(&mut self, _: &mut MeasureCtx, c: Constraints) -> Size {
self.0.set(self.0.get() + 1);
self.1.set(c.max.width);
c.clamp(Size::new(30.0, 12.0))
}
}
struct Preview {
child: WidgetPod,
commits: Rc<Cell<u32>>,
}
impl Widget for Preview {
fn measure(&mut self, ctx: &mut MeasureCtx, c: Constraints) -> Size {
let before = self.commits.get();
let provisional = self.child.probe_measure(ctx, c);
assert_eq!(
self.commits.get(),
before,
"provisional Flex traversal committed a leaf"
);
let size = self.child.measure(ctx, c);
assert_eq!(size, provisional);
assert_eq!(
self.commits.get(),
before + 1,
"final traversal should commit the leaf once"
);
size
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, r: Rect) {
self.child.arrange(ctx, r);
}
fn visit_children(&self, v: &mut dyn WidgetPodVisitor) {
v.visit(&self.child);
}
fn visit_children_mut(&mut self, v: &mut dyn WidgetPodMutVisitor) {
v.visit(&mut self.child);
}
}
struct SizeForwarder(WidgetPod);
impl Widget for SizeForwarder {
fn measure_size(&mut self, ctx: &mut MeasureCtx, c: Constraints) -> Size {
self.0.probe_measure(ctx, c)
}
fn measure(&mut self, ctx: &mut MeasureCtx, c: Constraints) -> Size {
self.0.measure(ctx, c)
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, r: Rect) {
self.0.arrange(ctx, r);
}
fn visit_children(&self, v: &mut dyn WidgetPodVisitor) {
v.visit(&self.0);
}
fn visit_children_mut(&mut self, v: &mut dyn WidgetPodMutVisitor) {
v.visit(&mut self.0);
}
}
let commits = Rc::new(Cell::new(0));
let width = Rc::new(Cell::new(0.0));
let mut child = WidgetPod::new(Leaf(commits.clone(), width.clone()));
for _ in 0..6 {
child = WidgetPod::new(
Flex::horizontal().with_child(Padding::all(1.0, SizeForwarder(child))),
);
}
let mut runtime = Runtime::new();
let window = runtime
.add_window(WindowBuilder::new().root(Preview { child, commits }))
.unwrap();
runtime.render(window).unwrap();
assert!(width.get().is_finite() && width.get() > 0.0);
}
struct FixedBox {
size: Size,
color: Color,
}
impl FixedBox {
fn new(size: Size, color: Color) -> Self {
Self { size, color }
}
}
impl Widget for FixedBox {
fn measure(&mut self, _ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
constraints.clamp(self.size)
}
fn paint(&self, ctx: &mut PaintCtx) {
ctx.fill_bounds(self.color);
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
ctx.push(SemanticsNode::new(
ctx.widget_id(),
SemanticsRole::GenericContainer,
ctx.bounds(),
));
}
}
#[test]
fn rebuild_on_change_rebuilds_child_only_when_key_changes() {
let key = Rc::new(Cell::new(1usize));
let builds = Rc::new(RefCell::new(Vec::new()));
let key_reader = Rc::clone(&key);
let build_log = Rc::clone(&builds);
let mut host = RebuildOnChange::key_when(
move || key_reader.get(),
move |value| {
build_log.borrow_mut().push(*value);
WidgetPod::new(FixedBox::new(
Size::new(*value as f32, 10.0),
Color::srgba(0.0, 0.0, 0.0, 1.0),
))
},
);
assert_eq!(*builds.borrow(), vec![1]);
assert!(!host.refresh());
assert_eq!(*builds.borrow(), vec![1]);
key.set(2);
assert!(host.refresh());
assert_eq!(*builds.borrow(), vec![1, 2]);
}
#[test]
fn rebuild_on_change_reports_structural_reason() -> sui_core::Result<()> {
let key = Rc::new(Cell::new(1usize));
let key_reader = Rc::clone(&key);
let (mut runtime, window_id) = build_runtime(RebuildOnChange::key_when(
move || key_reader.get(),
|value| {
WidgetPod::new(FixedBox::new(
Size::new(*value as f32 * 20.0, 20.0),
Color::BLACK,
))
},
));
runtime.render(window_id)?;
key.set(2);
runtime.handle_event(window_id, Event::Window(WindowEvent::RedrawRequested))?;
let output = runtime.render(window_id)?;
assert!(output.diagnostics.widget_rebuilds.iter().any(|sample| {
sample
.reason
.contains("caller-supplied structural key changed")
}));
Ok(())
}
#[test]
fn rebuild_on_change_observable_rebuilds_without_redraw_polling() -> sui_core::Result<()> {
let key = Signal::named("structural_mode", 1usize);
let (mut runtime, window_id) =
build_runtime(RebuildOnChange::key_from(key.clone(), |value| {
WidgetPod::new(FixedBox::new(
Size::new(*value as f32 * 20.0, 20.0),
Color::BLACK,
))
}));
let output = runtime.render(window_id)?;
assert_eq!(output.frame.viewport, Size::new(20.0, 20.0));
assert!(key.set(2));
let output = runtime.render(window_id)?;
assert_eq!(output.frame.viewport, Size::new(40.0, 20.0));
assert!(
output
.diagnostics
.widget_rebuilds
.iter()
.any(|sample| { sample.reason.contains("observable structural key changed") })
);
Ok(())
}
#[test]
fn rebuild_on_constraints_rebuilds_child_when_breakpoint_changes() {
let builds = Rc::new(RefCell::new(Vec::new()));
let build_log = Rc::clone(&builds);
let mut host = RebuildOnConstraints::new(
false,
|constraints| constraints.max.width >= 420.0,
move |wide| {
build_log.borrow_mut().push(*wide);
let width = if *wide { 24.0 } else { 12.0 };
WidgetPod::new(FixedBox::new(
Size::new(width, 10.0),
Color::srgba(0.0, 0.0, 0.0, 1.0),
))
},
);
assert_eq!(*builds.borrow(), vec![false]);
let constraints = Constraints::tight(Size::new(520.0, 80.0));
assert!(host.refresh_for_constraints(constraints));
assert_eq!(*builds.borrow(), vec![false, true]);
let constraints = Constraints::tight(Size::new(560.0, 80.0));
assert!(!host.refresh_for_constraints(constraints));
assert_eq!(*builds.borrow(), vec![false, true]);
}
struct OverflowingBox {
size: Size,
color: Color,
}
impl OverflowingBox {
fn new(size: Size, color: Color) -> Self {
Self { size, color }
}
}
impl Widget for OverflowingBox {
fn measure(&mut self, _ctx: &mut MeasureCtx, _constraints: Constraints) -> Size {
self.size
}
fn paint(&self, ctx: &mut PaintCtx) {
ctx.fill_bounds(self.color);
}
}
struct PaintCounterBox {
size: Size,
color: Color,
counts: Rc<RefCell<Vec<usize>>>,
index: usize,
}
impl PaintCounterBox {
fn new(size: Size, color: Color, counts: Rc<RefCell<Vec<usize>>>, index: usize) -> Self {
Self {
size,
color,
counts,
index,
}
}
}
impl Widget for PaintCounterBox {
fn measure(&mut self, _ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
constraints.clamp(self.size)
}
fn paint(&self, ctx: &mut PaintCtx) {
self.counts.borrow_mut()[self.index] += 1;
ctx.fill_bounds(self.color);
}
}
struct LayeredScrollPaintBox {
size: Size,
color: Color,
paints: Rc<RefCell<usize>>,
}
impl Widget for LayeredScrollPaintBox {
fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
if ctx.phase() == EventPhase::Target
&& matches!(event, Event::Pointer(pointer) if pointer.kind == PointerEventKind::Scroll)
{
ctx.request_paint();
}
}
fn measure(&mut self, _ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
constraints.clamp(self.size)
}
fn paint(&self, ctx: &mut PaintCtx) {
*self.paints.borrow_mut() += 1;
ctx.fill_bounds(self.color);
}
fn layer_options(&self) -> LayerOptions {
LayerOptions {
paint_boundary: PaintBoundaryMode::Explicit,
composition_mode: LayerCompositionMode::Normal,
}
}
}
struct HitTestBox {
size: Size,
presses: Rc<RefCell<Vec<usize>>>,
index: usize,
}
impl HitTestBox {
fn new(size: Size, presses: Rc<RefCell<Vec<usize>>>, index: usize) -> Self {
Self {
size,
presses,
index,
}
}
}
impl Widget for HitTestBox {
fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
if ctx.phase() == EventPhase::Target
&& matches!(
event,
Event::Pointer(pointer)
if pointer.kind == PointerEventKind::Down
&& ctx.bounds().contains(pointer.position)
)
{
self.presses.borrow_mut()[self.index] += 1;
ctx.set_handled();
}
}
fn measure(&mut self, _ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
constraints.clamp(self.size)
}
}
struct ConstraintProbe {
size: Size,
seen: Rc<RefCell<Vec<Constraints>>>,
}
impl ConstraintProbe {
fn new(size: Size, seen: Rc<RefCell<Vec<Constraints>>>) -> Self {
Self { size, seen }
}
}
impl Widget for ConstraintProbe {
fn measure(&mut self, _ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
self.seen.borrow_mut().push(constraints);
constraints.clamp(self.size)
}
}
struct ExpandingLayerBox {
collapsed_size: Size,
expanded_size: Size,
color: Color,
counts: Rc<RefCell<Vec<usize>>>,
index: usize,
expanded: bool,
}
impl ExpandingLayerBox {
fn new(
collapsed_size: Size,
expanded_size: Size,
color: Color,
counts: Rc<RefCell<Vec<usize>>>,
index: usize,
) -> Self {
Self {
collapsed_size,
expanded_size,
color,
counts,
index,
expanded: false,
}
}
}
impl Widget for ExpandingLayerBox {
fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
if let Event::Pointer(pointer) = event
&& pointer.kind == PointerEventKind::Down
&& ctx.bounds().contains(pointer.position)
&& !self.expanded
{
self.expanded = true;
ctx.request_measure();
ctx.request_paint();
ctx.set_handled();
}
}
fn measure(&mut self, _ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
constraints.clamp(if self.expanded {
self.expanded_size
} else {
self.collapsed_size
})
}
fn paint(&self, ctx: &mut PaintCtx) {
self.counts.borrow_mut()[self.index] += 1;
ctx.fill_bounds(self.color);
}
fn layer_options(&self) -> LayerOptions {
LayerOptions {
paint_boundary: PaintBoundaryMode::Explicit,
composition_mode: LayerCompositionMode::Normal,
}
}
}
struct ScrollViewNoPaint {
axes: ScrollAxes,
name: Option<String>,
offset: Vector,
content_size: Size,
child: SingleChild,
}
impl ScrollViewNoPaint {
fn vertical<W>(child: W) -> Self
where
W: Widget + 'static,
{
Self {
axes: ScrollAxes::Vertical,
name: None,
offset: Vector::ZERO,
content_size: Size::ZERO,
child: SingleChild::new(child),
}
}
fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
fn clamp_offset(&self, viewport: Size, offset: Vector) -> Vector {
let max_x = (self.content_size.width - viewport.width).max(0.0);
let max_y = (self.content_size.height - viewport.height).max(0.0);
Vector::new(
if self.axes.allows_horizontal() {
offset.x.clamp(0.0, max_x)
} else {
0.0
},
if self.axes.allows_vertical() {
offset.y.clamp(0.0, max_y)
} else {
0.0
},
)
}
}
impl Widget for ScrollViewNoPaint {
fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
let viewport = ctx.bounds().size;
match event {
Event::Pointer(pointer)
if pointer.kind == sui_core::PointerEventKind::Scroll
&& ctx.phase() != EventPhase::Capture
&& ctx.bounds().contains(pointer.position) =>
{
let delta = pointer
.scroll_delta
.map(super::scroll_delta_to_offset)
.unwrap_or(pointer.delta);
let next =
self.clamp_offset(viewport, self.offset + Vector::new(-delta.x, -delta.y));
if next != self.offset {
self.offset = next;
ctx.request_arrange();
ctx.request(InvalidationRequest::new(
InvalidationTarget::Widget(self.child.child().id()),
InvalidationKind::Transform,
));
ctx.request_semantics();
ctx.set_handled();
}
}
_ => {}
}
}
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
let mut child_constraints = constraints.loosen();
if self.axes.allows_horizontal() {
child_constraints.max.width = f32::INFINITY;
} else if constraints.max.width.is_finite() {
child_constraints.min.width = constraints.max.width;
child_constraints.max.width = constraints.max.width;
}
if self.axes.allows_vertical() {
child_constraints.max.height = f32::INFINITY;
} else if constraints.max.height.is_finite() {
child_constraints.min.height = constraints.max.height;
child_constraints.max.height = constraints.max.height;
}
let child_size = self.child.measure(ctx, child_constraints);
self.content_size = child_size;
let viewport = constraints.clamp(Size::new(
if constraints.max.width.is_finite() {
constraints.max.width
} else {
child_size.width
},
if constraints.max.height.is_finite() {
constraints.max.height
} else {
child_size.height
},
));
self.offset = self.clamp_offset(viewport, self.offset);
viewport
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
self.child.arrange(
ctx,
Rect::from_origin_size(
Point::new(bounds.x() - self.offset.x, bounds.y() - self.offset.y),
self.child.child().measured_size(),
),
);
}
fn paint(&self, ctx: &mut PaintCtx) {
ctx.push_clip_rect(ctx.bounds());
self.child.paint(ctx);
ctx.pop_clip();
}
fn layer_options(&self) -> LayerOptions {
LayerOptions {
paint_boundary: PaintBoundaryMode::Explicit,
composition_mode: LayerCompositionMode::Scroll,
}
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
let mut node =
SemanticsNode::new(ctx.widget_id(), SemanticsRole::ScrollView, ctx.bounds());
node.name = self.name.clone();
node.actions = vec![sui_core::SemanticsAction::Focus];
node.state.focused = ctx.is_focused();
ctx.push(node);
self.child.semantics(ctx);
}
fn accepts_focus(&self) -> bool {
true
}
fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
self.child.visit_children(visitor);
}
fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
self.child.visit_children_mut(visitor);
}
}
struct ScrollBarHost {
spacing: f32,
content: SingleChild,
scroll_bar: SingleChild,
}
impl ScrollBarHost {
fn new<W, S>(content: W, scroll_bar: S) -> Self
where
W: Widget + 'static,
S: Widget + 'static,
{
Self {
spacing: 0.0,
content: SingleChild::new(content),
scroll_bar: SingleChild::new(scroll_bar),
}
}
}
impl Widget for ScrollBarHost {
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
let scroll_bar_size = self.scroll_bar.measure(
ctx,
Constraints::new(Size::ZERO, Size::new(f32::INFINITY, constraints.max.height)),
);
let content_constraints = Constraints::new(
Size::new(
(constraints.min.width - scroll_bar_size.width - self.spacing).max(0.0),
constraints.min.height,
),
Size::new(
(constraints.max.width - scroll_bar_size.width - self.spacing).max(0.0),
constraints.max.height,
),
);
let content_size = self.content.measure(ctx, content_constraints);
constraints.clamp(Size::new(
content_size.width + scroll_bar_size.width + self.spacing,
content_size.height.max(scroll_bar_size.height),
))
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
let scroll_bar_size = self.scroll_bar.child().measured_size();
let content_width = (bounds.width() - scroll_bar_size.width - self.spacing).max(0.0);
self.content.arrange(
ctx,
Rect::new(bounds.x(), bounds.y(), content_width, bounds.height()),
);
self.scroll_bar.arrange(
ctx,
Rect::new(
bounds.max_x() - scroll_bar_size.width,
bounds.y(),
scroll_bar_size.width,
bounds.height(),
),
);
}
fn paint(&self, ctx: &mut PaintCtx) {
self.content.paint(ctx);
self.scroll_bar.paint(ctx);
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
self.content.semantics(ctx);
self.scroll_bar.semantics(ctx);
}
fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
self.content.visit_children(visitor);
self.scroll_bar.visit_children(visitor);
}
fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
self.content.visit_children_mut(visitor);
self.scroll_bar.visit_children_mut(visitor);
}
}
fn render_root<W>(root: W) -> (RenderOutput, WidgetGraphSnapshot)
where
W: Widget + 'static,
{
let mut runtime = Application::new()
.window(WindowBuilder::new().title("Containers").root(root))
.build()
.unwrap();
let window_id = runtime.window_ids()[0];
let output = runtime.render(window_id).unwrap();
let graph = runtime.widget_graph(window_id).unwrap();
(output, graph)
}
fn layer_descriptor_for(
output: &RenderOutput,
owner: WidgetId,
) -> Option<SceneLayerDescriptor> {
let mut descriptor = None;
output.frame.scene.visit_layers(&mut |layer| {
if layer.widget_id() == owner {
descriptor = Some(layer.descriptor.clone());
}
});
descriptor
}
fn build_runtime<W>(root: W) -> (Runtime, sui_core::WindowId)
where
W: Widget + 'static,
{
let runtime = Application::new()
.window(WindowBuilder::new().title("Containers").root(root))
.build()
.unwrap();
let window_id = runtime.window_ids()[0];
(runtime, window_id)
}
fn touch_pointer(
kind: PointerEventKind,
pointer_id: u64,
position: Point,
delta: Vector,
) -> PointerEvent {
let mut pointer = PointerEvent::new(kind, position);
pointer.pointer_id = pointer_id;
pointer.pointer_kind = PointerKind::Touch;
pointer.delta = delta;
pointer.is_primary = true;
match kind {
PointerEventKind::Down => {
pointer.button = Some(PointerButton::Primary);
pointer.buttons = PointerButtons::new(1);
}
PointerEventKind::Move => {
pointer.buttons = PointerButtons::new(1);
}
PointerEventKind::Up => {
pointer.button = Some(PointerButton::Primary);
}
_ => {}
}
pointer
}
fn drag_vertical_scroll_bar(
runtime: &mut Runtime,
window_id: sui_core::WindowId,
bounds: Rect,
pointer_id: u64,
delta_y: f32,
) {
let start = Point::new(bounds.x() + bounds.width() * 0.5, bounds.y() + 8.0);
let end = Point::new(start.x, start.y + delta_y);
let mut down = PointerEvent::new(PointerEventKind::Down, start);
down.pointer_id = pointer_id;
down.button = Some(PointerButton::Primary);
down.buttons = PointerButtons::new(1);
runtime
.handle_event(window_id, Event::Pointer(down))
.unwrap();
let mut moved = PointerEvent::new(PointerEventKind::Move, end);
moved.pointer_id = pointer_id;
moved.delta = Vector::new(0.0, delta_y);
moved.buttons = PointerButtons::new(1);
runtime
.handle_event(window_id, Event::Pointer(moved))
.unwrap();
runtime
.handle_event(window_id, Event::Window(WindowEvent::RedrawRequested))
.unwrap();
}
fn scroll_view_content_with_height(
graph: &WidgetGraphSnapshot,
content_height: f32,
) -> &WidgetNodeSnapshot {
graph
.nodes
.iter()
.find(|node| {
(node.measured_size.height - content_height).abs() <= 0.5
&& node
.parent
.and_then(|parent_id| {
graph.nodes.iter().find(|parent| parent.id == parent_id)
})
.is_some_and(|parent| parent.paint_boundary == PaintBoundaryMode::Explicit)
})
.expect("scroll content present")
}
fn parent_node<'a>(
graph: &'a WidgetGraphSnapshot,
node: &WidgetNodeSnapshot,
) -> &'a WidgetNodeSnapshot {
let parent_id = node.parent.expect("node has parent");
graph
.nodes
.iter()
.find(|parent| parent.id == parent_id)
.expect("parent node present")
}
fn solid_fill_colors(output: &RenderOutput) -> Vec<Color> {
let mut colors = Vec::new();
output
.frame
.scene
.visit_commands(&mut |command| match command {
SceneCommand::FillRect {
brush: Brush::Solid(color),
..
}
| SceneCommand::FillPath {
brush: Brush::Solid(color),
..
} => colors.push(*color),
_ => {}
});
colors
}
fn solid_fill_path_bounds(output: &RenderOutput) -> Vec<Rect> {
let mut bounds = Vec::new();
output.frame.scene.visit_commands(&mut |command| {
if let SceneCommand::FillPath {
path,
brush: Brush::Solid(_),
} = command
{
bounds.push(path.bounds());
}
});
bounds
}
fn solid_stroke_colors(output: &RenderOutput) -> Vec<Color> {
let mut colors = Vec::new();
output
.frame
.scene
.visit_commands(&mut |command| match command {
SceneCommand::StrokeRect {
brush: Brush::Solid(color),
..
}
| SceneCommand::StrokePath {
brush: Brush::Solid(color),
..
} => colors.push(*color),
_ => {}
});
colors
}
fn contains_approx_color(colors: &[Color], expected: Color) -> bool {
const CHANNEL_TOLERANCE: f32 = 1.0 / 255.0;
colors.iter().any(|color| {
color.space == expected.space
&& (color.red - expected.red).abs() <= CHANNEL_TOLERANCE
&& (color.green - expected.green).abs() <= CHANNEL_TOLERANCE
&& (color.blue - expected.blue).abs() <= CHANNEL_TOLERANCE
&& (color.alpha - expected.alpha).abs() <= CHANNEL_TOLERANCE
})
}
fn handle_ready_events(runtime: &mut Runtime) -> usize {
let ready = runtime.drain_ready_events();
let count = ready.len();
for (ready_window, event) in ready {
runtime
.handle_event(ready_window, event)
.expect("ready event should be handled");
}
count
}
#[test]
fn switch_view_only_exposes_selected_child_semantics() {
let (output, _) = render_root(
SwitchView::new()
.selected(1)
.with_child(crate::Label::new("Brush options"))
.with_child(crate::Label::new("Pan options")),
);
assert!(output.semantics.iter().any(|node| {
node.role == SemanticsRole::Text && node.name.as_deref() == Some("Pan options")
}));
assert!(!output.semantics.iter().any(|node| {
node.role == SemanticsRole::Text && node.name.as_deref() == Some("Brush options")
}));
}
#[test]
fn switch_view_selected_when_updates_active_child() -> sui_core::Result<()> {
let selected = Rc::new(RefCell::new(0_usize));
let selected_reader = Rc::clone(&selected);
let (mut runtime, window_id) = build_runtime(
SwitchView::new()
.selected_when(move || *selected_reader.borrow())
.with_child(
SizedBox::new()
.size(Size::new(80.0, 24.0))
.child(crate::Label::new("Brush options")),
)
.with_child(
SizedBox::new()
.size(Size::new(120.0, 36.0))
.child(crate::Label::new("Fill options")),
),
);
let output = runtime.render(window_id)?;
assert_eq!(output.frame.viewport, Size::new(80.0, 24.0));
assert!(output.semantics.iter().any(|node| {
node.role == SemanticsRole::Text && node.name.as_deref() == Some("Brush options")
}));
*selected.borrow_mut() = 1;
runtime.handle_event(
window_id,
Event::Window(sui_core::WindowEvent::Resized(Size::new(120.0, 36.0))),
)?;
let output = runtime.render(window_id)?;
assert_eq!(output.frame.viewport, Size::new(120.0, 36.0));
assert!(output.semantics.iter().any(|node| {
node.role == SemanticsRole::Text && node.name.as_deref() == Some("Fill options")
}));
assert!(!output.semantics.iter().any(|node| {
node.role == SemanticsRole::Text && node.name.as_deref() == Some("Brush options")
}));
Ok(())
}
#[test]
fn switch_view_observable_selection_relayouts_retained_child() -> sui_core::Result<()> {
let selected = Signal::named("active_panel", 0_usize);
let (mut runtime, window_id) = build_runtime(
SwitchView::new()
.selected_from(selected.clone())
.with_child(
SizedBox::new()
.size(Size::new(80.0, 24.0))
.child(crate::Label::new("Brush options")),
)
.with_child(
SizedBox::new()
.size(Size::new(120.0, 36.0))
.child(crate::Label::new("Fill options")),
),
);
let output = runtime.render(window_id)?;
assert_eq!(output.frame.viewport, Size::new(80.0, 24.0));
assert!(selected.set(1));
let output = runtime.render(window_id)?;
assert_eq!(output.frame.viewport, Size::new(120.0, 36.0));
assert!(output.semantics.iter().any(|node| {
node.role == SemanticsRole::Text && node.name.as_deref() == Some("Fill options")
}));
assert!(
output
.diagnostics
.reactive_invalidations
.iter()
.any(|sample| sample.source_name == "active_panel")
);
Ok(())
}
#[test]
fn padding_expands_child_size_and_offsets_bounds() {
let (output, graph) = render_root(Padding::new(
Insets {
left: 8.0,
top: 6.0,
right: 4.0,
bottom: 2.0,
},
FixedBox::new(Size::new(40.0, 20.0), Color::rgba(0.2, 0.3, 0.4, 1.0)),
));
assert_eq!(output.frame.viewport, Size::new(52.0, 28.0));
assert_eq!(graph.nodes[1].bounds, Rect::new(8.0, 6.0, 40.0, 20.0));
}
#[test]
fn padding_horizontal_constructor_offsets_x_only() {
let (output, graph) = render_root(Padding::horizontal(
9.0,
FixedBox::new(Size::new(40.0, 20.0), Color::rgba(0.2, 0.3, 0.4, 1.0)),
));
assert_eq!(output.frame.viewport, Size::new(58.0, 20.0));
assert_eq!(graph.nodes[1].bounds, Rect::new(9.0, 0.0, 40.0, 20.0));
}
#[test]
fn padding_can_stretch_child_to_arranged_content_height() {
let (_output, graph) = render_root(
SizedBox::new().size(Size::new(100.0, 80.0)).child(
Padding::new(
Insets {
left: 8.0,
top: 5.0,
right: 12.0,
bottom: 7.0,
},
FixedBox::new(Size::new(20.0, 10.0), Color::rgba(0.2, 0.3, 0.4, 1.0)),
)
.fill_child_height(true),
),
);
assert_eq!(graph.nodes[2].bounds, Rect::new(8.0, 5.0, 80.0, 68.0));
}
#[test]
fn padding_fill_flags_turn_off_with_false() {
let child = || FixedBox::new(Size::new(20.0, 10.0), Color::rgba(0.2, 0.3, 0.4, 1.0));
let padding = Padding::all(4.0, child())
.fill_child()
.fill_child_height(false);
assert!(padding.fill_child_width);
assert!(!padding.fill_child_height);
let padding = Padding::all(4.0, child())
.fill_child_width(true)
.fill_child_width(false);
assert!(!padding.fill_child_width);
}
#[test]
fn align_centers_child_within_available_space() {
let (output, graph) = render_root(SizedBox::new().size(Size::new(100.0, 60.0)).child(
Align::center(FixedBox::new(
Size::new(20.0, 10.0),
Color::rgba(0.4, 0.3, 0.2, 1.0),
)),
));
assert_eq!(output.frame.viewport, Size::new(100.0, 60.0));
assert_eq!(graph.nodes[2].bounds, Rect::new(40.0, 25.0, 20.0, 10.0));
}
#[test]
fn sized_box_applies_explicit_dimensions_to_child() {
let (output, graph) = render_root(SizedBox::new().width(40.0).height(24.0).child(
FixedBox::new(Size::new(12.0, 8.0), Color::rgba(0.1, 0.7, 0.2, 1.0)),
));
assert_eq!(output.frame.viewport, Size::new(40.0, 24.0));
assert_eq!(graph.nodes[1].bounds, Rect::new(0.0, 0.0, 40.0, 24.0));
}
#[test]
fn trailing_slot_row_places_fixed_trailing_slot() {
let (output, graph) = render_root(
SizedBox::new().size(Size::new(200.0, 80.0)).child(
TrailingSlotRow::new(
FixedBox::new(Size::new(120.0, 64.0), Color::rgba(0.1, 0.7, 0.2, 1.0)),
FixedBox::new(Size::new(40.0, 40.0), Color::rgba(0.7, 0.2, 0.2, 1.0)),
)
.trailing_width(50.0)
.trailing_height(40.0)
.gap(8.0),
),
);
assert_eq!(output.frame.viewport, Size::new(200.0, 80.0));
assert_eq!(graph.nodes[2].bounds, Rect::new(0.0, 0.0, 142.0, 80.0));
assert_eq!(graph.nodes[3].bounds, Rect::new(150.0, 20.0, 50.0, 40.0));
}
#[test]
fn dock_places_fixed_top_bottom_and_fills_body() {
let (output, graph) = render_root(
SizedBox::new().size(Size::new(200.0, 120.0)).child(
Dock::new(FixedBox::new(
Size::new(50.0, 50.0),
Color::rgba(0.1, 0.7, 0.2, 1.0),
))
.top(
24.0,
FixedBox::new(Size::new(200.0, 24.0), Color::rgba(0.2, 0.2, 0.8, 1.0)),
)
.bottom(
18.0,
FixedBox::new(Size::new(200.0, 18.0), Color::rgba(0.8, 0.2, 0.2, 1.0)),
),
),
);
assert_eq!(output.frame.viewport, Size::new(200.0, 120.0));
assert_eq!(graph.nodes[2].bounds, Rect::new(0.0, 0.0, 200.0, 24.0));
assert_eq!(graph.nodes[3].bounds, Rect::new(0.0, 24.0, 200.0, 78.0));
assert_eq!(graph.nodes[4].bounds, Rect::new(0.0, 102.0, 200.0, 18.0));
}
#[test]
fn measured_bottom_dock_places_natural_bottom_at_bottom_edge() {
let (output, graph) = render_root(SizedBox::new().size(Size::new(200.0, 120.0)).child(
MeasuredBottomDock::new(
FixedBox::new(Size::new(50.0, 50.0), Color::rgba(0.1, 0.7, 0.2, 1.0)),
FixedBox::new(Size::new(60.0, 26.0), Color::rgba(0.8, 0.2, 0.2, 1.0)),
),
));
assert_eq!(output.frame.viewport, Size::new(200.0, 120.0));
assert_eq!(graph.nodes[2].bounds, Rect::new(0.0, 0.0, 200.0, 94.0));
assert_eq!(graph.nodes[3].bounds, Rect::new(0.0, 94.0, 200.0, 26.0));
}
#[test]
fn fixed_pane_split_preserves_fixed_first_and_stretches_cross_axis() {
let (output, graph) = render_root(
SizedBox::new().size(Size::new(200.0, 80.0)).child(
FixedPaneSplit::horizontal(
FixedBox::new(Size::new(80.0, 12.0), Color::rgba(0.1, 0.7, 0.2, 1.0)),
FixedBox::new(Size::new(1.0, 80.0), Color::rgba(0.7, 0.7, 0.7, 1.0)),
FixedBox::new(Size::new(120.0, 12.0), Color::rgba(0.7, 0.2, 0.2, 1.0)),
)
.fixed_first(64.0)
.divider_extent(1.0),
),
);
assert_eq!(output.frame.viewport, Size::new(200.0, 80.0));
assert_eq!(graph.nodes[2].bounds, Rect::new(0.0, 0.0, 64.0, 80.0));
assert_eq!(graph.nodes[3].bounds, Rect::new(64.0, 0.0, 1.0, 80.0));
assert_eq!(graph.nodes[4].bounds, Rect::new(65.0, 0.0, 135.0, 80.0));
}
#[test]
fn fixed_pane_split_preserves_fixed_second_and_shrinks_to_fit() {
let (output, graph) = render_root(
SizedBox::new().size(Size::new(90.0, 40.0)).child(
FixedPaneSplit::horizontal(
FixedBox::new(Size::new(80.0, 12.0), Color::rgba(0.1, 0.7, 0.2, 1.0)),
FixedBox::new(Size::new(1.0, 40.0), Color::rgba(0.7, 0.7, 0.7, 1.0)),
FixedBox::new(Size::new(120.0, 12.0), Color::rgba(0.7, 0.2, 0.2, 1.0)),
)
.fixed_second(120.0)
.divider_extent(1.0),
),
);
assert_eq!(output.frame.viewport, Size::new(90.0, 40.0));
assert_eq!(graph.nodes[2].bounds, Rect::new(0.0, 0.0, 0.0, 40.0));
assert_eq!(graph.nodes[3].bounds, Rect::new(0.0, 0.0, 1.0, 40.0));
assert_eq!(graph.nodes[4].bounds, Rect::new(1.0, 0.0, 89.0, 40.0));
}
#[test]
fn stack_positions_children_with_spacing_and_alignment() {
let (output, graph) = render_root(
SizedBox::new().size(Size::new(100.0, 40.0)).child(
Stack::horizontal()
.gap(5.0)
.alignment(Alignment::Center)
.with_child(FixedBox::new(
Size::new(30.0, 10.0),
Color::rgba(0.6, 0.2, 0.2, 1.0),
))
.with_child(FixedBox::new(
Size::new(20.0, 20.0),
Color::rgba(0.2, 0.2, 0.6, 1.0),
)),
),
);
assert_eq!(output.frame.viewport, Size::new(100.0, 40.0));
assert_eq!(graph.nodes[2].bounds, Rect::new(0.0, 15.0, 30.0, 10.0));
assert_eq!(graph.nodes[3].bounds, Rect::new(35.0, 10.0, 20.0, 20.0));
}
#[test]
fn flex_grows_child_to_fill_available_main_axis_space() {
let (output, graph) = render_root(
SizedBox::new().size(Size::new(100.0, 20.0)).child(
Flex::horizontal()
.with_child(FixedBox::new(
Size::new(20.0, 10.0),
Color::rgba(0.7, 0.2, 0.2, 1.0),
))
.with_item(
FixedBox::new(Size::new(10.0, 10.0), Color::rgba(0.2, 0.2, 0.7, 1.0)),
FlexItem::new().grow(1.0),
),
),
);
assert_eq!(output.frame.viewport, Size::new(100.0, 20.0));
assert_eq!(graph.nodes[2].bounds, Rect::new(0.0, 0.0, 20.0, 10.0));
assert_eq!(graph.nodes[3].bounds, Rect::new(20.0, 0.0, 80.0, 10.0));
}
#[test]
fn flex_remeasures_wrapping_label_at_resolved_width() {
const TEXT: &str =
"Provider verification status and usage limits are shared across the cluster.";
let (output, _) = render_root(
SizedBox::new()
.width(240.0)
.child(Flex::horizontal().with_item(crate::Label::new(TEXT), FlexItem::flex(1.0))),
);
let label = output
.semantics
.iter()
.find(|node| node.role == SemanticsRole::Text && node.name.as_deref() == Some(TEXT))
.expect("flex label semantics present");
assert!((label.bounds.width() - 240.0).abs() < 0.01);
let mut shaped_width = None;
let mut line_count = None;
output.frame.scene.visit_commands(&mut |command| {
if let SceneCommand::DrawShapedText(run) = command
&& let Some(layout) = run.resolve(output.frame.text_layout_registry.as_ref())
&& layout.text() == TEXT
{
shaped_width = Some(layout.box_size().width);
line_count = Some(layout.lines().len());
}
});
assert!(shaped_width.is_some_and(|width| width >= 239.0));
assert!(line_count.is_some_and(|lines| (2..=4).contains(&lines)));
assert!(output.frame.viewport.height < 100.0);
}
#[test]
fn flex_spacer_pushes_following_children_to_remaining_edge() {
let (_, graph) = render_root(
SizedBox::new().size(Size::new(100.0, 10.0)).child(
Flex::horizontal()
.with_child(FixedBox::new(
Size::new(10.0, 10.0),
Color::rgba(0.7, 0.2, 0.2, 1.0),
))
.spacer()
.with_child(FixedBox::new(
Size::new(10.0, 10.0),
Color::rgba(0.2, 0.2, 0.7, 1.0),
)),
),
);
assert_eq!(graph.nodes[2].bounds, Rect::new(0.0, 0.0, 10.0, 10.0));
assert_eq!(graph.nodes[4].bounds, Rect::new(90.0, 0.0, 10.0, 10.0));
}
#[test]
fn flex_wraps_children_and_applies_cross_gap() {
let (output, graph) = render_root(
SizedBox::new().size(Size::new(70.0, 25.0)).child(
Flex::horizontal()
.wrap(FlexWrap::Wrap)
.main_gap(5.0)
.cross_gap(5.0)
.with_child(FixedBox::new(
Size::new(30.0, 10.0),
Color::rgba(0.7, 0.2, 0.2, 1.0),
))
.with_child(FixedBox::new(
Size::new(30.0, 10.0),
Color::rgba(0.2, 0.7, 0.2, 1.0),
))
.with_child(FixedBox::new(
Size::new(30.0, 10.0),
Color::rgba(0.2, 0.2, 0.7, 1.0),
)),
),
);
assert_eq!(output.frame.viewport, Size::new(70.0, 25.0));
assert_eq!(graph.nodes[2].bounds, Rect::new(0.0, 0.0, 30.0, 10.0));
assert_eq!(graph.nodes[3].bounds, Rect::new(35.0, 0.0, 30.0, 10.0));
assert_eq!(graph.nodes[4].bounds, Rect::new(0.0, 15.0, 30.0, 10.0));
}
#[test]
fn flex_stretches_children_on_cross_axis() {
let (_, graph) = render_root(
SizedBox::new().size(Size::new(60.0, 20.0)).child(
Flex::horizontal()
.align_items(Alignment::Stretch)
.with_child(FixedBox::new(
Size::new(20.0, 8.0),
Color::rgba(0.4, 0.3, 0.2, 1.0),
)),
),
);
assert_eq!(graph.nodes[2].bounds, Rect::new(0.0, 0.0, 20.0, 20.0));
}
#[test]
fn background_paints_before_child_content() {
let (output, _) = render_root(Background::new(
Brush::Solid(Color::rgba(0.1, 0.1, 0.1, 1.0)),
FixedBox::new(Size::new(16.0, 12.0), Color::rgba(0.9, 0.2, 0.1, 1.0)),
));
assert_eq!(output.frame.scene.commands().len(), 2);
assert_eq!(
output.frame.scene.commands()[0],
SceneCommand::FillRect {
rect: Rect::new(0.0, 0.0, 16.0, 12.0),
brush: Brush::Solid(Color::rgba(0.1, 0.1, 0.1, 1.0)),
}
);
assert_eq!(
output.frame.scene.commands()[1],
SceneCommand::FillRect {
rect: Rect::new(0.0, 0.0, 16.0, 12.0),
brush: Brush::Solid(Color::rgba(0.9, 0.2, 0.1, 1.0)),
}
);
}
#[test]
fn semantic_region_wraps_child_with_accessible_summary() {
let description = Rc::new(RefCell::new("4 active tasks".to_string()));
let description_reader = Rc::clone(&description);
let (output, graph) = render_root(
SemanticRegion::new(
"File task strip",
FixedBox::new(Size::new(96.0, 28.0), Color::rgba(0.2, 0.4, 0.6, 1.0)),
)
.role(SemanticsRole::Text)
.description_when(move || description_reader.borrow().clone()),
);
let region = output
.semantics
.iter()
.find(|node| {
node.role == SemanticsRole::Text && node.name.as_deref() == Some("File task strip")
})
.expect("semantic region node should exist");
assert_eq!(region.description.as_deref(), Some("4 active tasks"));
assert_eq!(graph.nodes[0].bounds, Rect::new(0.0, 0.0, 96.0, 28.0));
assert_eq!(graph.nodes[1].bounds, Rect::new(0.0, 0.0, 96.0, 28.0));
}
#[test]
fn stack_vertical_axis_is_available() {
let (output, graph) = render_root(
Stack::new(Axis::Vertical)
.gap(4.0)
.with_child(FixedBox::new(
Size::new(18.0, 10.0),
Color::rgba(0.5, 0.5, 0.1, 1.0),
))
.with_child(FixedBox::new(
Size::new(12.0, 8.0),
Color::rgba(0.1, 0.5, 0.5, 1.0),
)),
);
assert_eq!(output.frame.viewport, Size::new(18.0, 22.0));
assert_eq!(graph.nodes[1].bounds, Rect::new(0.0, 0.0, 18.0, 10.0));
assert_eq!(graph.nodes[2].bounds, Rect::new(0.0, 14.0, 12.0, 8.0));
}
#[test]
fn nested_containers_preserve_global_child_bounds() {
let (output, graph) = render_root(Padding::all(
24.0,
Background::new(
Brush::Solid(Color::rgba(0.1, 0.1, 0.1, 1.0)),
Padding::all(
18.0,
Stack::vertical()
.gap(10.0)
.with_child(FixedBox::new(
Size::new(50.0, 12.0),
Color::rgba(0.7, 0.2, 0.2, 1.0),
))
.with_child(FixedBox::new(
Size::new(30.0, 8.0),
Color::rgba(0.2, 0.7, 0.2, 1.0),
)),
),
),
));
assert_eq!(output.frame.viewport, Size::new(134.0, 114.0));
assert_eq!(graph.nodes[1].bounds, Rect::new(24.0, 24.0, 86.0, 66.0));
assert_eq!(graph.nodes[2].bounds, Rect::new(24.0, 24.0, 86.0, 66.0));
assert_eq!(graph.nodes[3].bounds, Rect::new(42.0, 42.0, 50.0, 30.0));
assert_eq!(graph.nodes[4].bounds, Rect::new(42.0, 42.0, 50.0, 12.0));
assert_eq!(graph.nodes[5].bounds, Rect::new(42.0, 64.0, 30.0, 8.0));
}
#[test]
fn scroll_view_updates_child_bounds_after_scroll_input() {
let (mut runtime, window_id) = build_runtime(
SizedBox::new()
.size(Size::new(80.0, 40.0))
.child(ScrollView::vertical(FixedBox::new(
Size::new(80.0, 120.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
))),
);
let _ = runtime.render(window_id).unwrap();
let mut scroll = PointerEvent::new(PointerEventKind::Scroll, Point::new(20.0, 20.0));
scroll.scroll_delta = Some(ScrollDelta::Pixels(Vector::new(0.0, -32.0)));
runtime
.handle_event(window_id, Event::Pointer(scroll))
.unwrap();
let output = runtime.render(window_id).unwrap();
let graph = runtime.widget_graph(window_id).unwrap();
assert_eq!(graph.nodes[1].bounds, Rect::new(0.0, 0.0, 80.0, 40.0));
let content = graph
.nodes
.iter()
.find(|node| {
node.bounds.width() == 80.0 - DefaultTheme::default().metrics.scroll_bar_thickness
&& node.bounds.height() == 120.0
})
.expect("scroll content present");
assert_eq!(
content.bounds,
Rect::new(
0.0,
-32.0,
80.0 - DefaultTheme::default().metrics.scroll_bar_thickness,
120.0
)
);
let semantic_content = output
.semantics
.iter()
.find(|node| node.id == content.id)
.expect("scroll content semantics present");
assert_eq!(semantic_content.bounds, content.bounds);
let scroll_view = parent_node(&graph, content);
let scroll_scene = output
.frame
.scene
.layer_scene(scroll_view.id)
.expect("scroll viewport layer present");
assert!(scroll_scene.commands().iter().any(|command| {
matches!(
command,
SceneCommand::PushClip { rect }
if *rect == Rect::new(0.0, 0.0, 80.0 - DefaultTheme::default().metrics.scroll_bar_thickness, 40.0)
)
}));
}
#[test]
fn scroll_view_translates_retained_content_without_repainting() {
let counts = Rc::new(RefCell::new(vec![0usize; 2]));
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(80.0, 60.0)).child(
ScrollView::vertical(
Stack::vertical()
.with_child(PaintCounterBox::new(
Size::new(80.0, 60.0),
Color::rgba(0.8, 0.2, 0.2, 1.0),
Rc::clone(&counts),
0,
))
.with_child(PaintCounterBox::new(
Size::new(80.0, 60.0),
Color::rgba(0.2, 0.6, 0.8, 1.0),
Rc::clone(&counts),
1,
)),
)
.retain_content_layer(),
),
);
let _ = runtime.render(window_id).unwrap();
assert_eq!(*counts.borrow(), vec![1, 1]);
let graph = runtime.widget_graph(window_id).unwrap();
let content_id = scroll_view_content_with_height(&graph, 120.0).id;
let mut scroll = PointerEvent::new(PointerEventKind::Scroll, Point::new(20.0, 20.0));
scroll.scroll_delta = Some(ScrollDelta::Pixels(Vector::new(0.0, -32.0)));
runtime
.handle_event(window_id, Event::Pointer(scroll))
.unwrap();
let output = runtime.render(window_id).unwrap();
assert_eq!(*counts.borrow(), vec![1, 1]);
assert!(output.frame.layer_updates.iter().any(|update| {
update.owner == content_id && update.kind == sui_scene::SceneLayerUpdateKind::Transform
}));
assert!(!output.frame.layer_updates.iter().any(|update| {
update.owner == content_id && update.kind == sui_scene::SceneLayerUpdateKind::Content
}));
}
#[test]
fn scroll_view_keeps_retained_content_far_from_the_origin() {
const ROWS: usize = 200;
let counts = Rc::new(RefCell::new(vec![0usize; ROWS]));
let mut stack = Stack::vertical();
for index in 0..ROWS {
stack.push(PaintCounterBox::new(
Size::new(80.0, 21.792),
Color::rgba(0.8, 0.2, 0.2, 1.0),
Rc::clone(&counts),
index,
));
}
let (mut runtime, window_id) = build_runtime(
SizedBox::new()
.size(Size::new(80.0, 60.0))
.child(ScrollView::vertical(stack).retain_content_layer()),
);
let _ = runtime.render(window_id).unwrap();
let initial = counts.borrow().clone();
for _ in 0..80 {
let mut scroll = PointerEvent::new(PointerEventKind::Scroll, Point::new(20.0, 20.0));
scroll.scroll_delta = Some(ScrollDelta::Pixels(Vector::new(0.0, -48.0)));
runtime
.handle_event(window_id, Event::Pointer(scroll))
.unwrap();
let _ = runtime.render(window_id).unwrap();
}
assert_eq!(*counts.borrow(), initial);
}
#[test]
fn scroll_view_repaints_content_by_default_after_scroll_input() {
let counts = Rc::new(RefCell::new(vec![0usize; 2]));
let (mut runtime, window_id) = build_runtime(
SizedBox::new()
.size(Size::new(80.0, 60.0))
.child(ScrollView::vertical(
Stack::vertical()
.with_child(PaintCounterBox::new(
Size::new(80.0, 60.0),
Color::rgba(0.8, 0.2, 0.2, 1.0),
Rc::clone(&counts),
0,
))
.with_child(PaintCounterBox::new(
Size::new(80.0, 60.0),
Color::rgba(0.2, 0.6, 0.8, 1.0),
Rc::clone(&counts),
1,
)),
)),
);
let _ = runtime.render(window_id).unwrap();
let mut scroll = PointerEvent::new(PointerEventKind::Scroll, Point::new(20.0, 20.0));
scroll.scroll_delta = Some(ScrollDelta::Pixels(Vector::new(0.0, -32.0)));
runtime
.handle_event(window_id, Event::Pointer(scroll))
.unwrap();
let output = runtime.render(window_id).unwrap();
assert_eq!(*counts.borrow(), vec![2, 2]);
assert!(
output
.frame
.layer_updates
.iter()
.any(|update| { update.kind == sui_scene::SceneLayerUpdateKind::Content })
);
}
#[test]
fn retained_scroll_repaints_ancestor_when_nested_layer_is_dirty() {
let nested_paints = Rc::new(RefCell::new(0usize));
let sibling_paints = Rc::new(RefCell::new(vec![0usize]));
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(80.0, 60.0)).child(
ScrollView::vertical(
Stack::vertical()
.with_child(LayeredScrollPaintBox {
size: Size::new(80.0, 60.0),
color: Color::rgba(0.8, 0.2, 0.2, 1.0),
paints: Rc::clone(&nested_paints),
})
.with_child(PaintCounterBox::new(
Size::new(80.0, 60.0),
Color::rgba(0.2, 0.6, 0.8, 1.0),
Rc::clone(&sibling_paints),
0,
)),
)
.retain_content_layer(),
),
);
let _ = runtime.render(window_id).unwrap();
let initial_graph = runtime.widget_graph(window_id).unwrap();
let content = scroll_view_content_with_height(&initial_graph, 120.0);
let content_id = content.id;
let nested_id = content.children[0];
let mut scroll = PointerEvent::new(PointerEventKind::Scroll, Point::new(20.0, 20.0));
scroll.scroll_delta = Some(ScrollDelta::Pixels(Vector::new(0.0, -32.0)));
runtime
.handle_event(window_id, Event::Pointer(scroll))
.unwrap();
let output = runtime.render(window_id).unwrap();
let graph = runtime.widget_graph(window_id).unwrap();
let nested = graph
.nodes
.iter()
.find(|node| node.id == nested_id)
.expect("nested retained layer remains in the graph");
let descriptor = layer_descriptor_for(&output, nested_id)
.expect("nested retained layer remains in the scene");
assert_eq!(*nested_paints.borrow(), 2);
assert_eq!(*sibling_paints.borrow(), vec![2]);
assert_eq!(descriptor.bounds, nested.bounds);
assert!(output.frame.layer_updates.iter().any(|update| {
update.owner == content_id && update.kind == sui_scene::SceneLayerUpdateKind::Content
}));
assert!(!output.frame.layer_updates.iter().any(|update| {
update.owner == content_id && update.kind == sui_scene::SceneLayerUpdateKind::Transform
}));
}
#[test]
fn scroll_view_hit_testing_tracks_retained_content_translation() {
let presses = Rc::new(RefCell::new(vec![0usize; 2]));
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(80.0, 40.0)).child(
ScrollView::vertical(
Stack::vertical()
.with_child(HitTestBox::new(
Size::new(80.0, 40.0),
Rc::clone(&presses),
0,
))
.with_child(HitTestBox::new(
Size::new(80.0, 40.0),
Rc::clone(&presses),
1,
)),
)
.retain_content_layer(),
),
);
let _ = runtime.render(window_id).unwrap();
let mut scroll = PointerEvent::new(PointerEventKind::Scroll, Point::new(20.0, 20.0));
scroll.scroll_delta = Some(ScrollDelta::Pixels(Vector::new(0.0, -40.0)));
runtime
.handle_event(window_id, Event::Pointer(scroll))
.unwrap();
let _ = runtime.render(window_id).unwrap();
let pointer = PointerEvent::new(PointerEventKind::Down, Point::new(20.0, 20.0));
runtime
.handle_event(window_id, Event::Pointer(pointer))
.unwrap();
assert_eq!(*presses.borrow(), vec![0, 1]);
}
#[test]
fn scroll_view_uses_scroll_layer_metadata() {
let (output, _) = render_root(SizedBox::new().size(Size::new(80.0, 40.0)).child(
ScrollView::vertical(FixedBox::new(
Size::new(80.0, 120.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
)),
));
let scroll_id = output
.semantics
.iter()
.find(|node| node.role == SemanticsRole::ScrollView)
.expect("scroll view semantics present")
.id;
let descriptor =
layer_descriptor_for(&output, scroll_id).expect("scroll view layer present");
assert_eq!(descriptor.composition_mode, LayerCompositionMode::Scroll);
}
#[test]
fn scrollbar_gutters_resolve_cross_axis_overflow_and_own_the_corner() {
let thickness = DefaultTheme::default().metrics.scroll_bar_thickness;
for content in [Size::new(96.0, 120.0), Size::new(120.0, 96.0)] {
let state = ScrollState::new();
let presses = Rc::new(RefCell::new(vec![0]));
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(100.0, 100.0)).child(
ScrollView::both(HitTestBox::new(content, Rc::clone(&presses), 0))
.state(state.clone())
.name("Canvas"),
),
);
let output = runtime.render(window_id).unwrap();
let viewport = 100.0 - thickness;
assert_eq!(state.viewport_size(), Size::new(viewport, viewport));
assert_eq!(
state.max_offset(),
Vector::new(content.width - viewport, content.height - viewport)
);
let bounds = |name| {
output
.semantics
.iter()
.find(|node| node.name.as_deref() == Some(name))
.unwrap()
.bounds
};
assert_eq!(
bounds("Canvas vertical scroll bar"),
Rect::new(viewport, 0.0, thickness, viewport)
);
assert_eq!(
bounds("Canvas horizontal scroll bar"),
Rect::new(0.0, viewport, viewport, thickness)
);
for (point, expected) in [
(Point::new(viewport + 1.0, viewport + 1.0), 0),
(Point::new(10.0, 10.0), 1),
] {
let mut down = PointerEvent::new(PointerEventKind::Down, point);
down.button = Some(PointerButton::Primary);
runtime
.handle_event(window_id, Event::Pointer(down))
.unwrap();
assert_eq!(
presses.borrow()[0],
expected,
"gutter corner must not activate content"
);
}
}
}
#[test]
fn scrollbar_gutter_reflows_content_and_releases_space_after_shrinking() {
struct ResponsiveContent {
short: Signal<bool>,
}
impl Widget for ResponsiveContent {
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
let height = if ctx.observe(&self.short) {
20.0
} else if constraints.max.width < 100.0 {
240.0
} else {
120.0
};
constraints.clamp(Size::new(constraints.max.width, height))
}
}
let short = Signal::new(false);
let state = ScrollState::new();
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(100.0, 80.0)).child(
ScrollView::vertical(ResponsiveContent {
short: short.clone(),
})
.state(state.clone()),
),
);
runtime.render(window_id).unwrap();
assert_eq!(
state.viewport_size().width,
100.0 - DefaultTheme::default().metrics.scroll_bar_thickness
);
assert_eq!(
state.content_size().height,
240.0,
"content must reflow inside the gutter"
);
state.set_offset(Vector::new(0.0, 160.0));
short.set(true);
let output = runtime.render(window_id).unwrap();
assert_eq!(state.viewport_size(), Size::new(100.0, 80.0));
assert_eq!(state.current_offset(), Vector::ZERO);
assert!(
output
.semantics
.iter()
.all(|node| node.role != SemanticsRole::Slider)
);
}
#[test]
fn hidden_scrollbars_keep_the_full_scroll_viewport() {
let state = ScrollState::new();
let (output, _) = render_root(
SizedBox::new().size(Size::new(100.0, 80.0)).child(
ScrollView::both(FixedBox::new(Size::new(240.0, 240.0), Color::WHITE))
.state(state.clone())
.scroll_bars(false),
),
);
assert_eq!(state.viewport_size(), Size::new(100.0, 80.0));
assert_eq!(state.max_offset(), Vector::new(140.0, 160.0));
assert!(
output
.semantics
.iter()
.all(|node| node.role != SemanticsRole::Slider)
);
}
#[test]
fn scroll_view_reserves_scroll_bar_space_only_when_content_overflows() {
let theme = DefaultTheme::default();
let (overflowing, graph) = render_root(
SizedBox::new().size(Size::new(80.0, 40.0)).child(
ScrollView::vertical(OverflowingBox::new(
Size::new(80.0, 120.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
))
.name("Results")
.theme(theme),
),
);
let scroll_view = overflowing
.semantics
.iter()
.find(|node| node.role == SemanticsRole::ScrollView)
.expect("scroll view semantics present");
let scroll_bar = overflowing
.semantics
.iter()
.find(|node| {
node.role == SemanticsRole::Slider
&& node.name.as_deref() == Some("Results vertical scroll bar")
})
.expect("overflowing content should expose a scroll bar");
assert_eq!(
scroll_bar.bounds.width(),
theme.metrics.scroll_bar_thickness
);
assert!(!scroll_bar.actions.contains(&SemanticsAction::Focus));
assert!(scroll_bar.actions.contains(&SemanticsAction::SetValue));
assert!(scroll_view.bounds.contains(scroll_bar.bounds.origin));
assert!(scroll_bar.bounds.max_x() <= scroll_view.bounds.max_x());
assert!(scroll_bar.bounds.max_y() <= scroll_view.bounds.max_y());
let content = graph
.nodes
.iter()
.find(|node| {
node.bounds.height() == 120.0
&& node.bounds.width() == 80.0 - theme.metrics.scroll_bar_thickness
})
.expect("scroll content leaves space for the bar");
assert_eq!(content.bounds.max_x(), scroll_bar.bounds.x());
assert_eq!(
content.bounds.width() + scroll_bar.bounds.width(),
scroll_view.bounds.width()
);
let (fitting, _) = render_root(
SizedBox::new().size(Size::new(80.0, 40.0)).child(
ScrollView::vertical(FixedBox::new(
Size::new(80.0, 40.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
))
.name("Results"),
),
);
assert!(
fitting
.semantics
.iter()
.all(|node| node.role != SemanticsRole::Slider),
"fitting content should not expose scroll-bar chrome"
);
}
#[test]
fn embedded_overlay_scroll_bar_drags_the_shared_view_state() {
let state = ScrollState::new();
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(80.0, 60.0)).child(
ScrollView::vertical(OverflowingBox::new(
Size::new(80.0, 180.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
))
.name("Results")
.state(state.clone()),
),
);
let output = runtime.render(window_id).unwrap();
let scroll_bar = output
.semantics
.iter()
.find(|node| node.name.as_deref() == Some("Results vertical scroll bar"))
.expect("embedded scroll bar present");
let point = Point::new(
scroll_bar.bounds.x() + scroll_bar.bounds.width() * 0.5,
scroll_bar.bounds.max_y() - 1.0,
);
let mut down = PointerEvent::new(PointerEventKind::Down, point);
down.pointer_id = 41;
down.button = Some(PointerButton::Primary);
down.buttons = PointerButtons::new(1);
runtime
.handle_event(window_id, Event::Pointer(down))
.unwrap();
assert!(state.current_offset().y > 0.0);
let _ = runtime.render(window_id).unwrap();
let graph = runtime.widget_graph(window_id).unwrap();
let content = graph
.nodes
.iter()
.find(|node| {
node.bounds.size
== Size::new(
80.0 - DefaultTheme::default().metrics.scroll_bar_thickness,
180.0,
)
})
.expect("scroll content present");
assert_eq!(content.bounds.y(), -state.current_offset().y);
}
#[test]
fn overlay_scroll_bar_drag_repaints_non_retained_content_immediately() {
let state = ScrollState::new();
let counts = Rc::new(RefCell::new(vec![0usize]));
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(80.0, 60.0)).child(
ScrollView::vertical(PaintCounterBox::new(
Size::new(80.0, 180.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
Rc::clone(&counts),
0,
))
.name("Results")
.state(state.clone()),
),
);
let output = runtime.render(window_id).unwrap();
assert_eq!(*counts.borrow(), vec![1]);
let scroll_bar = output
.semantics
.iter()
.find(|node| node.name.as_deref() == Some("Results vertical scroll bar"))
.expect("embedded scroll bar present");
drag_vertical_scroll_bar(&mut runtime, window_id, scroll_bar.bounds, 42, 20.0);
assert!(state.current_offset().y > 0.0);
let _ = runtime.render(window_id).unwrap();
assert_eq!(
*counts.borrow(),
vec![2],
"the first frame after a scroll-bar drag must repaint moved flattened content"
);
}
#[test]
fn overlay_scroll_bar_drag_keeps_retained_content_transform_only() {
let state = ScrollState::new();
let counts = Rc::new(RefCell::new(vec![0usize]));
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(80.0, 60.0)).child(
ScrollView::vertical(PaintCounterBox::new(
Size::new(80.0, 180.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
Rc::clone(&counts),
0,
))
.name("Retained results")
.state(state.clone())
.retain_content_layer(),
),
);
let output = runtime.render(window_id).unwrap();
assert_eq!(*counts.borrow(), vec![1]);
let scroll_bar = output
.semantics
.iter()
.find(|node| node.name.as_deref() == Some("Retained results vertical scroll bar"))
.expect("embedded scroll bar present");
drag_vertical_scroll_bar(&mut runtime, window_id, scroll_bar.bounds, 43, 20.0);
assert!(state.current_offset().y > 0.0);
let output = runtime.render(window_id).unwrap();
assert_eq!(
*counts.borrow(),
vec![1],
"retained content should move without rebuilding its paint commands"
);
assert!(
output
.frame
.layer_updates
.iter()
.any(|update| { update.kind == sui_scene::SceneLayerUpdateKind::Transform })
);
}
#[test]
fn virtual_overlay_scroll_bar_drag_repaints_visible_content_immediately() {
let state = ScrollState::new();
let counts = Rc::new(RefCell::new(vec![0usize; 4]));
let mut scroll = VirtualScrollView::new()
.name("Virtual results")
.state(state.clone());
for (index, color) in [
Color::rgba(0.8, 0.2, 0.2, 1.0),
Color::rgba(0.2, 0.8, 0.2, 1.0),
Color::rgba(0.2, 0.2, 0.8, 1.0),
Color::rgba(0.8, 0.8, 0.2, 1.0),
]
.into_iter()
.enumerate()
{
scroll.push(PaintCounterBox::new(
Size::new(80.0, 30.0),
color,
Rc::clone(&counts),
index,
));
}
let (mut runtime, window_id) =
build_runtime(SizedBox::new().size(Size::new(80.0, 80.0)).child(scroll));
let output = runtime.render(window_id).unwrap();
assert_eq!(*counts.borrow(), vec![1, 1, 1, 1]);
let scroll_bar = output
.semantics
.iter()
.find(|node| node.name.as_deref() == Some("Virtual results vertical scroll bar"))
.expect("embedded scroll bar present");
drag_vertical_scroll_bar(&mut runtime, window_id, scroll_bar.bounds, 44, 20.0);
assert!(state.current_offset().y > 0.0);
let _ = runtime.render(window_id).unwrap();
assert_eq!(
*counts.borrow(),
vec![2, 2, 2, 2],
"the first frame after a virtual scroll-bar drag must repaint visible rows"
);
}
#[test]
fn both_axis_scroll_bars_reserve_space_and_share_the_corner() {
let (output, graph) = render_root(
SizedBox::new().size(Size::new(80.0, 40.0)).child(
ScrollView::both(OverflowingBox::new(
Size::new(160.0, 120.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
))
.name("Canvas"),
),
);
let vertical = output
.semantics
.iter()
.find(|node| node.name.as_deref() == Some("Canvas vertical scroll bar"))
.expect("vertical overlay scroll bar present");
let horizontal = output
.semantics
.iter()
.find(|node| node.name.as_deref() == Some("Canvas horizontal scroll bar"))
.expect("horizontal overlay scroll bar present");
assert!(vertical.bounds.max_y() <= horizontal.bounds.y());
assert!(horizontal.bounds.max_x() <= vertical.bounds.x());
let content = graph
.nodes
.iter()
.find(|node| node.bounds.size == Size::new(160.0, 120.0))
.expect("two-axis content present");
assert_eq!(content.bounds.origin, Point::ZERO);
}
#[test]
fn virtual_scroll_view_overlays_a_synchronized_vertical_scroll_bar() {
let state = ScrollState::new();
let (output, _) = render_root(
SizedBox::new().size(Size::new(80.0, 40.0)).child(
VirtualScrollView::new()
.name("Timeline")
.state(state.clone())
.with_child(FixedBox::new(
Size::new(80.0, 40.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
))
.with_child(FixedBox::new(
Size::new(80.0, 40.0),
Color::rgba(0.7, 0.3, 0.2, 1.0),
)),
),
);
let scroll_bar = output
.semantics
.iter()
.find(|node| node.name.as_deref() == Some("Timeline vertical scroll bar"))
.expect("virtual scroll bar present");
assert_eq!(
scroll_bar.value,
Some(SemanticsValue::Range {
value: 0.0,
min: 0.0,
max: 40.0,
})
);
assert_eq!(state.max_offset(), Vector::new(0.0, 40.0));
}
#[test]
fn scroll_view_auto_overflow_uses_finite_width_and_natural_height() {
let seen = Rc::new(RefCell::new(Vec::new()));
let (output, _) = render_root(
SizedBox::new().size(Size::new(120.0, 60.0)).child(
ScrollView::both(ConstraintProbe::new(
Size::new(90.0, 180.0),
Rc::clone(&seen),
))
.overflow_x(Overflow::Auto)
.overflow_y(Overflow::Auto),
),
);
let constraints = seen
.borrow()
.last()
.copied()
.expect("probe should be measured");
assert_eq!(
constraints.max.width,
120.0 - DefaultTheme::default().metrics.scroll_bar_thickness
);
assert!(constraints.max.height.is_infinite());
assert_eq!(output.frame.viewport, Size::new(120.0, 60.0));
}
#[test]
fn scroll_view_content_extent_can_require_at_least_the_viewport() {
let seen = Rc::new(RefCell::new(Vec::new()));
let (_, _) = render_root(
SizedBox::new().size(Size::new(120.0, 60.0)).child(
ScrollView::both(ConstraintProbe::new(
Size::new(40.0, 20.0),
Rc::clone(&seen),
))
.content_width(ContentExtent::AtLeastViewport)
.content_height(ContentExtent::AtLeast(100.0)),
),
);
let constraints = seen
.borrow()
.last()
.copied()
.expect("probe should be measured");
assert_eq!(
constraints.min,
Size::new(
120.0 - DefaultTheme::default().metrics.scroll_bar_thickness,
100.0
)
);
assert!(constraints.max.width.is_infinite());
assert!(constraints.max.height.is_infinite());
}
#[test]
fn content_extent_normalizes_non_finite_explicit_values() {
assert_eq!(
ContentExtent::AtLeast(f32::NAN).resolve(80.0, 20.0),
(0.0, f32::INFINITY)
);
assert_eq!(ContentExtent::MinContent.resolve(80.0, 24.0), (24.0, 24.0));
}
#[test]
fn vertical_scroll_view_clamps_cross_axis_after_split_arrange() {
let (_, graph) = render_root(
SizedBox::new().size(Size::new(240.0, 80.0)).child(
SplitView::horizontal(
FixedBox::new(Size::new(40.0, 80.0), Color::rgba(0.1, 0.2, 0.3, 1.0)),
ScrollView::vertical(Padding::all(
8.0,
Stack::vertical()
.alignment(Alignment::Stretch)
.with_child(FixedBox::new(
Size::new(400.0, 32.0),
Color::rgba(0.4, 0.5, 0.6, 1.0),
)),
)),
)
.ratio(0.5)
.min_first(40.0)
.min_second(40.0)
.divider_thickness(8.0),
),
);
let content = graph
.nodes
.iter()
.find(|node| {
(node.bounds.x() - 132.0).abs() < 0.001
&& (node.bounds.y() - 8.0).abs() < 0.001
&& (node.bounds.height() - 32.0).abs() < 0.001
})
.expect("scroll content child should be arranged inside the narrow pane");
assert_eq!(content.bounds.width(), 100.0);
}
#[test]
fn scroll_view_visible_overflow_does_not_scroll_or_use_scroll_layer() {
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(80.0, 40.0)).child(
ScrollView::vertical(OverflowingBox::new(
Size::new(80.0, 120.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
))
.overflow(Overflow::Visible),
),
);
let output = runtime.render(window_id).unwrap();
let scroll_id = output
.semantics
.iter()
.find(|node| node.role == SemanticsRole::ScrollView)
.expect("scroll view semantics present")
.id;
let descriptor =
layer_descriptor_for(&output, scroll_id).expect("scroll view layer present");
assert_eq!(descriptor.composition_mode, LayerCompositionMode::Normal);
let mut scroll = PointerEvent::new(PointerEventKind::Scroll, Point::new(20.0, 20.0));
scroll.scroll_delta = Some(ScrollDelta::Pixels(Vector::new(0.0, -32.0)));
runtime
.handle_event(window_id, Event::Pointer(scroll))
.unwrap();
let _ = runtime.render(window_id).unwrap();
let graph = runtime.widget_graph(window_id).unwrap();
let content = graph
.nodes
.iter()
.find(|node| node.bounds.width() == 80.0 && node.bounds.height() == 120.0)
.expect("visible overflow content present");
assert_eq!(content.bounds, Rect::new(0.0, 0.0, 80.0, 120.0));
}
#[test]
fn virtual_scroll_view_uses_scroll_layer_metadata() {
let (output, _) = render_root(
SizedBox::new().size(Size::new(80.0, 40.0)).child(
VirtualScrollView::new()
.with_child(FixedBox::new(
Size::new(80.0, 40.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
))
.with_child(FixedBox::new(
Size::new(80.0, 40.0),
Color::rgba(0.7, 0.3, 0.2, 1.0),
)),
),
);
let scroll_id = output
.semantics
.iter()
.find(|node| node.role == SemanticsRole::ScrollView)
.expect("virtual scroll view semantics present")
.id;
let descriptor =
layer_descriptor_for(&output, scroll_id).expect("virtual scroll view layer present");
assert_eq!(descriptor.composition_mode, LayerCompositionMode::Scroll);
}
#[test]
fn virtual_scroll_state_scroll_to_item_aligns_item_at_viewport_top() {
let state = ScrollState::new();
assert!(state.scroll_to_item(2));
let (output, _) = render_root(
SizedBox::new().size(Size::new(80.0, 40.0)).child(
VirtualScrollView::new()
.state(state.clone())
.with_child(SemanticRegion::new(
"Item 0",
FixedBox::new(Size::new(80.0, 20.0), Color::rgba(0.8, 0.2, 0.2, 1.0)),
))
.with_child(SemanticRegion::new(
"Item 1",
FixedBox::new(Size::new(80.0, 20.0), Color::rgba(0.2, 0.8, 0.2, 1.0)),
))
.with_child(SemanticRegion::new(
"Item 2",
FixedBox::new(Size::new(80.0, 40.0), Color::rgba(0.2, 0.2, 0.8, 1.0)),
)),
),
);
let viewport = output
.semantics
.iter()
.find(|node| node.role == SemanticsRole::ScrollView)
.expect("virtual scroll view semantics present");
let target = output
.semantics
.iter()
.find(|node| node.name.as_deref() == Some("Item 2"))
.expect("target item semantics present");
assert_eq!(state.current_offset(), Vector::new(0.0, 40.0));
assert_eq!(target.bounds.y(), viewport.bounds.y());
}
#[test]
fn virtual_scroll_state_reports_items_at_content_offsets() {
let state = ScrollState::new();
assert_eq!(state.first_visible_item(), None);
assert!(state.scroll_to_item(1));
let _ = render_root(
SizedBox::new().size(Size::new(80.0, 40.0)).child(
VirtualScrollView::new()
.state(state.clone())
.gap(4.0)
.with_child(FixedBox::new(
Size::new(80.0, 20.0),
Color::rgba(0.8, 0.2, 0.2, 1.0),
))
.with_child(FixedBox::new(
Size::new(80.0, 30.0),
Color::rgba(0.2, 0.8, 0.2, 1.0),
))
.with_child(FixedBox::new(
Size::new(80.0, 40.0),
Color::rgba(0.2, 0.2, 0.8, 1.0),
)),
),
);
assert_eq!(state.virtual_item_offset(1), Some(24.0));
assert_eq!(state.virtual_item_offset(2), Some(58.0));
assert_eq!(state.virtual_item_offset(3), None);
assert_eq!(state.first_visible_item(), Some(1));
assert_eq!(state.virtual_item_at(0.0), Some(0));
assert_eq!(state.virtual_item_at(23.9), Some(0));
assert_eq!(state.virtual_item_at(24.0), Some(1));
assert_eq!(state.virtual_item_at(-10.0), Some(0));
assert_eq!(state.virtual_item_at(500.0), Some(2));
}
#[test]
fn virtual_scroll_view_paints_only_visible_children() {
let counts = Rc::new(RefCell::new(vec![0usize; 4]));
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(80.0, 40.0)).child(
VirtualScrollView::new()
.with_child(PaintCounterBox::new(
Size::new(80.0, 30.0),
Color::rgba(0.8, 0.2, 0.2, 1.0),
Rc::clone(&counts),
0,
))
.with_child(PaintCounterBox::new(
Size::new(80.0, 30.0),
Color::rgba(0.2, 0.8, 0.2, 1.0),
Rc::clone(&counts),
1,
))
.with_child(PaintCounterBox::new(
Size::new(80.0, 30.0),
Color::rgba(0.2, 0.2, 0.8, 1.0),
Rc::clone(&counts),
2,
))
.with_child(PaintCounterBox::new(
Size::new(80.0, 30.0),
Color::rgba(0.8, 0.8, 0.2, 1.0),
Rc::clone(&counts),
3,
)),
),
);
let _ = runtime.render(window_id).unwrap();
assert_eq!(*counts.borrow(), vec![1, 1, 1, 0]);
let mut scroll = PointerEvent::new(PointerEventKind::Scroll, Point::new(20.0, 20.0));
scroll.scroll_delta = Some(ScrollDelta::Pixels(Vector::new(0.0, -32.0)));
runtime
.handle_event(window_id, Event::Pointer(scroll))
.unwrap();
let _ = runtime.render(window_id).unwrap();
assert_eq!(*counts.borrow(), vec![2, 2, 2, 1]);
}
#[test]
fn scroll_view_emits_transform_updates_after_scroll_offset_changes() {
let counts = Rc::new(RefCell::new(vec![0usize; 1]));
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(80.0, 40.0)).child(
ScrollView::vertical(PaintCounterBox::new(
Size::new(80.0, 120.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
Rc::clone(&counts),
0,
))
.retain_content_layer(),
),
);
let _ = runtime.render(window_id).unwrap();
assert_eq!(*counts.borrow(), vec![1]);
let mut scroll = PointerEvent::new(PointerEventKind::Scroll, Point::new(20.0, 20.0));
scroll.scroll_delta = Some(ScrollDelta::Pixels(Vector::new(0.0, -32.0)));
runtime
.handle_event(window_id, Event::Pointer(scroll))
.unwrap();
let output = runtime.render(window_id).unwrap();
assert_eq!(*counts.borrow(), vec![1]);
assert!(
output
.frame
.layer_updates
.iter()
.any(|update| update.kind == sui_scene::SceneLayerUpdateKind::Transform)
);
}
#[test]
fn split_view_scroll_without_paint_still_updates_runtime_scene() {
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(360.0, 220.0)).child(
SplitView::horizontal(
ScrollViewNoPaint::vertical(
Stack::vertical()
.with_child(FixedBox::new(
Size::new(220.0, 120.0),
Color::rgba(0.82, 0.36, 0.18, 1.0),
))
.with_child(FixedBox::new(
Size::new(220.0, 120.0),
Color::rgba(0.18, 0.54, 0.82, 1.0),
))
.with_child(FixedBox::new(
Size::new(220.0, 120.0),
Color::rgba(0.24, 0.72, 0.36, 1.0),
)),
)
.name("NoPaint scroll"),
SizedBox::new().size(Size::new(120.0, 220.0)),
)
.ratio(0.68),
),
);
let before = runtime.render(window_id).unwrap();
let mut scroll = PointerEvent::new(PointerEventKind::Scroll, Point::new(48.0, 96.0));
scroll.scroll_delta = Some(ScrollDelta::Pixels(Vector::new(0.0, -72.0)));
runtime
.handle_event(window_id, Event::Pointer(scroll))
.unwrap();
let after = runtime.render(window_id).unwrap();
let graph = runtime.widget_graph(window_id).unwrap();
assert!(graph.nodes.iter().any(|node| node.bounds.y() < 0.0));
assert_ne!(before.frame.scene, after.frame.scene);
assert!(!after.frame.layer_updates.is_empty());
}
#[test]
fn virtual_scroll_view_repaints_exposed_strip_while_visible_range_is_unchanged() {
let counts = Rc::new(RefCell::new(vec![0usize; 4]));
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(80.0, 80.0)).child(
VirtualScrollView::new()
.with_child(PaintCounterBox::new(
Size::new(80.0, 30.0),
Color::rgba(0.8, 0.2, 0.2, 1.0),
Rc::clone(&counts),
0,
))
.with_child(PaintCounterBox::new(
Size::new(80.0, 30.0),
Color::rgba(0.2, 0.8, 0.2, 1.0),
Rc::clone(&counts),
1,
))
.with_child(PaintCounterBox::new(
Size::new(80.0, 30.0),
Color::rgba(0.2, 0.2, 0.8, 1.0),
Rc::clone(&counts),
2,
))
.with_child(PaintCounterBox::new(
Size::new(80.0, 30.0),
Color::rgba(0.8, 0.8, 0.2, 1.0),
Rc::clone(&counts),
3,
)),
),
);
let _ = runtime.render(window_id).unwrap();
assert_eq!(*counts.borrow(), vec![1, 1, 1, 1]);
let mut scroll = PointerEvent::new(PointerEventKind::Scroll, Point::new(20.0, 20.0));
scroll.scroll_delta = Some(ScrollDelta::Pixels(Vector::new(0.0, -4.0)));
runtime
.handle_event(window_id, Event::Pointer(scroll))
.unwrap();
let output = runtime.render(window_id).unwrap();
assert_eq!(*counts.borrow(), vec![2, 2, 2, 2]);
assert!(output.frame.layer_updates.iter().any(|update| {
update.kind == sui_scene::SceneLayerUpdateKind::Content
&& update.damage
== Some(Rect::new(
0.0,
76.0,
80.0 - DefaultTheme::default().metrics.scroll_bar_thickness,
4.0,
))
}));
}
#[test]
fn virtual_scroll_view_repaints_when_a_visible_layered_child_changes_height() {
let counts = Rc::new(RefCell::new(vec![0usize; 2]));
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(80.0, 80.0)).child(
VirtualScrollView::new()
.with_child(ExpandingLayerBox::new(
Size::new(80.0, 30.0),
Size::new(80.0, 60.0),
Color::rgba(0.7, 0.3, 0.2, 1.0),
Rc::clone(&counts),
0,
))
.with_child(PaintCounterBox::new(
Size::new(80.0, 30.0),
Color::rgba(0.2, 0.5, 0.8, 1.0),
Rc::clone(&counts),
1,
)),
),
);
let _ = runtime.render(window_id).unwrap();
assert_eq!(*counts.borrow(), vec![1, 1]);
let pointer = PointerEvent::new(PointerEventKind::Down, Point::new(20.0, 20.0));
runtime
.handle_event(window_id, Event::Pointer(pointer))
.unwrap();
let _ = runtime.render(window_id).unwrap();
assert_eq!(*counts.borrow(), vec![2, 2]);
}
#[test]
fn nested_scroll_views_scroll_the_inner_region_first() {
let (mut runtime, window_id) = build_runtime(
SizedBox::new()
.size(Size::new(80.0, 80.0))
.child(ScrollView::vertical(
Stack::vertical()
.gap(8.0)
.with_child(FixedBox::new(
Size::new(80.0, 32.0),
Color::rgba(0.8, 0.2, 0.2, 1.0),
))
.with_child(SizedBox::new().height(40.0).child(ScrollView::vertical(
FixedBox::new(Size::new(80.0, 120.0), Color::rgba(0.2, 0.7, 0.3, 1.0)),
)))
.with_child(FixedBox::new(
Size::new(80.0, 140.0),
Color::rgba(0.2, 0.3, 0.8, 1.0),
)),
)),
);
let _ = runtime.render(window_id).unwrap();
let mut scroll = PointerEvent::new(PointerEventKind::Scroll, Point::new(20.0, 52.0));
scroll.scroll_delta = Some(ScrollDelta::Pixels(Vector::new(0.0, -24.0)));
runtime
.handle_event(window_id, Event::Pointer(scroll))
.unwrap();
let output = runtime.render(window_id).unwrap();
let graph = runtime.widget_graph(window_id).unwrap();
let outer_content = graph
.nodes
.iter()
.find(|node| {
node.bounds.width() == 80.0 - DefaultTheme::default().metrics.scroll_bar_thickness
&& node.bounds.height() == 228.0
})
.expect("outer scroll content present");
let inner_content = graph
.nodes
.iter()
.find(|node| {
node.bounds.width()
== 80.0 - 2.0 * DefaultTheme::default().metrics.scroll_bar_thickness
&& node.bounds.height() == 120.0
})
.expect("inner scroll content present");
assert_eq!(outer_content.bounds.y(), 0.0);
assert_eq!(inner_content.bounds.y(), 16.0);
assert!(!output.frame.layer_updates.is_empty());
}
#[test]
fn nested_scroll_views_fall_back_to_parent_at_inner_limit() {
let (mut runtime, window_id) = build_runtime(
SizedBox::new()
.size(Size::new(80.0, 80.0))
.child(ScrollView::vertical(
Stack::vertical()
.gap(8.0)
.with_child(FixedBox::new(
Size::new(80.0, 32.0),
Color::rgba(0.8, 0.2, 0.2, 1.0),
))
.with_child(SizedBox::new().height(40.0).child(ScrollView::vertical(
FixedBox::new(Size::new(80.0, 120.0), Color::rgba(0.2, 0.7, 0.3, 1.0)),
)))
.with_child(FixedBox::new(
Size::new(80.0, 140.0),
Color::rgba(0.2, 0.3, 0.8, 1.0),
)),
)),
);
let _ = runtime.render(window_id).unwrap();
let mut first = PointerEvent::new(PointerEventKind::Scroll, Point::new(20.0, 52.0));
first.scroll_delta = Some(ScrollDelta::Pixels(Vector::new(0.0, -200.0)));
runtime
.handle_event(window_id, Event::Pointer(first))
.unwrap();
let _ = runtime.render(window_id).unwrap();
let mut second = PointerEvent::new(PointerEventKind::Scroll, Point::new(20.0, 52.0));
second.scroll_delta = Some(ScrollDelta::Pixels(Vector::new(0.0, -24.0)));
runtime
.handle_event(window_id, Event::Pointer(second))
.unwrap();
let _ = runtime.render(window_id).unwrap();
let graph = runtime.widget_graph(window_id).unwrap();
let outer_content = graph
.nodes
.iter()
.find(|node| {
node.bounds.width() == 80.0 - DefaultTheme::default().metrics.scroll_bar_thickness
&& node.bounds.height() == 228.0
})
.expect("outer scroll content present");
let inner_content = graph
.nodes
.iter()
.find(|node| {
node.bounds.width()
== 80.0 - 2.0 * DefaultTheme::default().metrics.scroll_bar_thickness
&& node.bounds.height() == 120.0
})
.expect("inner scroll content present");
assert_eq!(inner_content.bounds.y(), -64.0);
assert_eq!(outer_content.bounds.y(), -24.0);
}
#[test]
fn scroll_view_touch_drag_scrolls_content_after_drag_threshold() {
let state = ScrollState::new();
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(80.0, 40.0)).child(
ScrollView::vertical(OverflowingBox::new(
Size::new(80.0, 120.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
))
.state(state.clone()),
),
);
let _ = runtime.render(window_id).unwrap();
runtime
.handle_event(
window_id,
Event::Pointer(touch_pointer(
PointerEventKind::Down,
7,
Point::new(20.0, 30.0),
Vector::ZERO,
)),
)
.unwrap();
runtime
.handle_event(
window_id,
Event::Pointer(touch_pointer(
PointerEventKind::Move,
7,
Point::new(20.0, 6.0),
Vector::new(0.0, -24.0),
)),
)
.unwrap();
assert_eq!(state.current_offset(), Vector::new(0.0, 24.0));
runtime
.handle_event(
window_id,
Event::Pointer(touch_pointer(
PointerEventKind::Up,
7,
Point::new(20.0, 6.0),
Vector::ZERO,
)),
)
.unwrap();
}
#[test]
fn touch_move_below_threshold_or_from_another_pointer_does_not_scroll() {
let state = ScrollState::new();
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(80.0, 40.0)).child(
ScrollView::vertical(OverflowingBox::new(
Size::new(80.0, 120.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
))
.state(state.clone()),
),
);
let _ = runtime.render(window_id).unwrap();
runtime
.handle_event(
window_id,
Event::Pointer(touch_pointer(
PointerEventKind::Down,
1,
Point::new(20.0, 20.0),
Vector::ZERO,
)),
)
.unwrap();
runtime
.handle_event(
window_id,
Event::Pointer(touch_pointer(
PointerEventKind::Move,
1,
Point::new(20.0, 17.0),
Vector::new(0.0, -3.0),
)),
)
.unwrap();
runtime
.handle_event(
window_id,
Event::Pointer(touch_pointer(
PointerEventKind::Move,
2,
Point::new(20.0, 4.0),
Vector::new(0.0, -16.0),
)),
)
.unwrap();
assert_eq!(state.current_offset(), Vector::ZERO);
}
#[test]
fn virtual_scroll_view_supports_touch_drag_and_cancelled_gesture_recovery() {
let state = ScrollState::new();
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(80.0, 40.0)).child(
VirtualScrollView::new()
.state(state.clone())
.with_child(FixedBox::new(
Size::new(80.0, 40.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
))
.with_child(FixedBox::new(
Size::new(80.0, 40.0),
Color::rgba(0.7, 0.3, 0.2, 1.0),
))
.with_child(FixedBox::new(
Size::new(80.0, 40.0),
Color::rgba(0.3, 0.7, 0.2, 1.0),
)),
),
);
let _ = runtime.render(window_id).unwrap();
for pointer in [
touch_pointer(
PointerEventKind::Down,
3,
Point::new(20.0, 30.0),
Vector::ZERO,
),
touch_pointer(
PointerEventKind::Move,
3,
Point::new(20.0, 10.0),
Vector::new(0.0, -20.0),
),
touch_pointer(
PointerEventKind::Cancel,
3,
Point::new(20.0, 10.0),
Vector::ZERO,
),
touch_pointer(
PointerEventKind::Down,
4,
Point::new(20.0, 30.0),
Vector::ZERO,
),
touch_pointer(
PointerEventKind::Move,
4,
Point::new(20.0, 18.0),
Vector::new(0.0, -12.0),
),
] {
runtime
.handle_event(window_id, Event::Pointer(pointer))
.unwrap();
}
assert_eq!(state.current_offset(), Vector::new(0.0, 32.0));
}
#[test]
fn boundary_touch_gestures_keep_capture_for_reversal_and_accept_fresh_down() {
let state = ScrollState::new();
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(80.0, 40.0)).child(
ScrollView::vertical(OverflowingBox::new(
Size::new(80.0, 120.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
))
.state(state.clone()),
),
);
let _ = runtime.render(window_id).unwrap();
for pointer in [
touch_pointer(
PointerEventKind::Down,
20,
Point::new(20.0, 30.0),
Vector::ZERO,
),
touch_pointer(
PointerEventKind::Move,
20,
Point::new(20.0, 18.0),
Vector::new(0.0, -200.0),
),
touch_pointer(
PointerEventKind::Up,
20,
Point::new(20.0, -170.0),
Vector::ZERO,
),
touch_pointer(
PointerEventKind::Down,
21,
Point::new(20.0, 30.0),
Vector::ZERO,
),
touch_pointer(
PointerEventKind::Move,
21,
Point::new(20.0, 18.0),
Vector::new(0.0, -12.0),
),
touch_pointer(
PointerEventKind::Move,
21,
Point::new(20.0, 30.0),
Vector::new(0.0, 12.0),
),
] {
runtime
.handle_event(window_id, Event::Pointer(pointer))
.unwrap();
}
assert_eq!(
state.current_offset(),
Vector::new(0.0, 68.0),
"a captured pan should reverse away from a hard boundary"
);
for pointer in [
touch_pointer(
PointerEventKind::Down,
21,
Point::new(20.0, 18.0),
Vector::ZERO,
),
touch_pointer(
PointerEventKind::Move,
21,
Point::new(20.0, 30.0),
Vector::new(0.0, 12.0),
),
] {
runtime
.handle_event(window_id, Event::Pointer(pointer))
.unwrap();
}
assert_eq!(state.current_offset(), Vector::new(0.0, 56.0));
let virtual_state = ScrollState::new();
let (mut virtual_runtime, virtual_window) = build_runtime(
SizedBox::new().size(Size::new(80.0, 40.0)).child(
VirtualScrollView::new()
.state(virtual_state.clone())
.with_child(FixedBox::new(
Size::new(80.0, 40.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
))
.with_child(FixedBox::new(
Size::new(80.0, 40.0),
Color::rgba(0.7, 0.3, 0.2, 1.0),
))
.with_child(FixedBox::new(
Size::new(80.0, 40.0),
Color::rgba(0.3, 0.7, 0.2, 1.0),
)),
),
);
let _ = virtual_runtime.render(virtual_window).unwrap();
for pointer in [
touch_pointer(
PointerEventKind::Down,
30,
Point::new(20.0, 30.0),
Vector::ZERO,
),
touch_pointer(
PointerEventKind::Move,
30,
Point::new(20.0, 18.0),
Vector::new(0.0, -200.0),
),
touch_pointer(
PointerEventKind::Up,
30,
Point::new(20.0, -170.0),
Vector::ZERO,
),
touch_pointer(
PointerEventKind::Down,
31,
Point::new(20.0, 30.0),
Vector::ZERO,
),
touch_pointer(
PointerEventKind::Move,
31,
Point::new(20.0, 18.0),
Vector::new(0.0, -12.0),
),
touch_pointer(
PointerEventKind::Move,
31,
Point::new(20.0, 30.0),
Vector::new(0.0, 12.0),
),
] {
virtual_runtime
.handle_event(virtual_window, Event::Pointer(pointer))
.unwrap();
}
assert_eq!(virtual_state.current_offset(), Vector::new(0.0, 68.0));
for pointer in [
touch_pointer(
PointerEventKind::Down,
31,
Point::new(20.0, 18.0),
Vector::ZERO,
),
touch_pointer(
PointerEventKind::Move,
31,
Point::new(20.0, 30.0),
Vector::new(0.0, 12.0),
),
] {
virtual_runtime
.handle_event(virtual_window, Event::Pointer(pointer))
.unwrap();
}
assert_eq!(virtual_state.current_offset(), Vector::new(0.0, 56.0));
}
#[test]
fn nested_touch_scroll_hands_off_from_inner_limit_to_parent() {
let outer_state = ScrollState::new();
let inner_state = ScrollState::new();
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(80.0, 80.0)).child(
ScrollView::vertical(
Stack::vertical()
.gap(8.0)
.with_child(FixedBox::new(
Size::new(80.0, 32.0),
Color::rgba(0.8, 0.2, 0.2, 1.0),
))
.with_child(
SizedBox::new().height(40.0).child(
ScrollView::vertical(FixedBox::new(
Size::new(80.0, 120.0),
Color::rgba(0.2, 0.7, 0.3, 1.0),
))
.state(inner_state.clone()),
),
)
.with_child(FixedBox::new(
Size::new(80.0, 140.0),
Color::rgba(0.2, 0.3, 0.8, 1.0),
)),
)
.state(outer_state.clone()),
),
);
let _ = runtime.render(window_id).unwrap();
for pointer in [
touch_pointer(
PointerEventKind::Down,
9,
Point::new(20.0, 52.0),
Vector::ZERO,
),
touch_pointer(
PointerEventKind::Move,
9,
Point::new(20.0, 44.0),
Vector::new(0.0, -8.0),
),
touch_pointer(
PointerEventKind::Move,
9,
Point::new(20.0, -56.0),
Vector::new(0.0, -100.0),
),
touch_pointer(
PointerEventKind::Move,
9,
Point::new(20.0, -80.0),
Vector::new(0.0, -24.0),
),
touch_pointer(
PointerEventKind::Up,
9,
Point::new(20.0, -80.0),
Vector::ZERO,
),
] {
runtime
.handle_event(window_id, Event::Pointer(pointer))
.unwrap();
}
assert_eq!(inner_state.current_offset(), Vector::new(0.0, 80.0));
assert_eq!(outer_state.current_offset(), Vector::new(0.0, 24.0));
assert_eq!(runtime.pointer_capture_target(window_id, 9).unwrap(), None);
}
fn touch_swipe_up(
runtime: &mut Runtime,
window_id: sui_core::WindowId,
pointer_id: u64,
start: Point,
step: f32,
moves: usize,
time: f64,
) -> (f64, Point) {
runtime.tick(time);
runtime
.handle_event(
window_id,
Event::Pointer(touch_pointer(
PointerEventKind::Down,
pointer_id,
start,
Vector::ZERO,
)),
)
.unwrap();
let mut time = time;
let mut position = start;
for _ in 0..moves {
time += 1.0 / 60.0;
position = Point::new(position.x, position.y - step);
runtime.tick(time);
runtime
.handle_event(
window_id,
Event::Pointer(touch_pointer(
PointerEventKind::Move,
pointer_id,
position,
Vector::new(0.0, -step),
)),
)
.unwrap();
}
(time, position)
}
fn touch_up(
runtime: &mut Runtime,
window_id: sui_core::WindowId,
pointer_id: u64,
position: Point,
time: f64,
) {
runtime.tick(time);
runtime
.handle_event(
window_id,
Event::Pointer(touch_pointer(
PointerEventKind::Up,
pointer_id,
position,
Vector::ZERO,
)),
)
.unwrap();
}
fn run_frames(runtime: &mut Runtime, time: f64, seconds: f64) -> f64 {
let mut now = time;
while now < time + seconds {
now += 1.0 / 120.0;
runtime.tick(now);
handle_ready_events(runtime);
}
now
}
#[test]
fn touch_fling_coasts_after_release_and_slows_to_a_stop() {
let state = ScrollState::new();
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(80.0, 40.0)).child(
ScrollView::vertical(OverflowingBox::new(
Size::new(80.0, 4_000.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
))
.state(state.clone()),
),
);
let _ = runtime.render(window_id).unwrap();
let (time, position) = touch_swipe_up(
&mut runtime,
window_id,
5,
Point::new(20.0, 30.0),
10.0,
6,
1.0,
);
touch_up(&mut runtime, window_id, 5, position, time);
assert_eq!(state.current_offset(), Vector::new(0.0, 60.0));
assert!(runtime.has_pending_animation_frames(window_id).unwrap());
let time = run_frames(&mut runtime, time, 0.25);
let coasting = state.current_offset().y;
assert!(
coasting > 150.0,
"fling should keep scrolling, at {coasting}"
);
let _ = run_frames(&mut runtime, time, 5.0);
let settled = state.current_offset().y;
assert!(settled > coasting);
assert!(settled <= 60.0 + 600.0 / super::TOUCH_FLING_FRICTION + 1.0);
assert!(!runtime.has_pending_animation_frames(window_id).unwrap());
}
#[test]
fn touch_release_after_resting_does_not_fling() {
let state = ScrollState::new();
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(80.0, 40.0)).child(
ScrollView::vertical(OverflowingBox::new(
Size::new(80.0, 4_000.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
))
.state(state.clone()),
),
);
let _ = runtime.render(window_id).unwrap();
let (time, position) = touch_swipe_up(
&mut runtime,
window_id,
5,
Point::new(20.0, 30.0),
10.0,
6,
1.0,
);
touch_up(&mut runtime, window_id, 5, position, time + 0.2);
let _ = run_frames(&mut runtime, time + 0.2, 0.5);
assert_eq!(state.current_offset(), Vector::new(0.0, 60.0));
}
#[test]
fn touch_catching_a_fling_stops_it_without_pressing_content() {
let presses = Rc::new(Cell::new(0usize));
let mut content = Stack::vertical();
for index in 0..100 {
let presses = Rc::clone(&presses);
content.push(
crate::Button::new(format!("Row {index}"))
.on_press(move || presses.set(presses.get() + 1)),
);
}
let state = ScrollState::new();
let (mut runtime, window_id) = build_runtime(
SizedBox::new()
.size(Size::new(100.0, 80.0))
.child(ScrollView::vertical(content).state(state.clone())),
);
let _ = runtime.render(window_id).unwrap();
let (time, position) = touch_swipe_up(
&mut runtime,
window_id,
6,
Point::new(20.0, 70.0),
10.0,
6,
1.0,
);
touch_up(&mut runtime, window_id, 6, position, time);
let time = run_frames(&mut runtime, time, 0.1);
let _ = runtime.render(window_id).unwrap();
let caught_at = state.current_offset();
runtime.tick(time);
for kind in [PointerEventKind::Down, PointerEventKind::Up] {
runtime
.handle_event(
window_id,
Event::Pointer(touch_pointer(kind, 7, Point::new(20.0, 20.0), Vector::ZERO)),
)
.unwrap();
}
let _ = run_frames(&mut runtime, time, 0.5);
assert_eq!(state.current_offset(), caught_at);
assert_eq!(presses.get(), 0);
}
#[test]
fn virtual_scroll_view_fling_stops_at_the_end() {
let mut view = VirtualScrollView::new();
for _ in 0..3 {
view.push(FixedBox::new(
Size::new(80.0, 40.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
));
}
let state = ScrollState::new();
let (mut runtime, window_id) = build_runtime(
SizedBox::new()
.size(Size::new(80.0, 40.0))
.child(view.state(state.clone())),
);
let _ = runtime.render(window_id).unwrap();
let (time, position) = touch_swipe_up(
&mut runtime,
window_id,
8,
Point::new(20.0, 35.0),
6.0,
5,
1.0,
);
assert_eq!(state.current_offset(), Vector::new(0.0, 30.0));
touch_up(&mut runtime, window_id, 8, position, time);
let _ = run_frames(&mut runtime, time, 1.0);
assert_eq!(state.current_offset(), Vector::new(0.0, 80.0));
assert!(!runtime.has_pending_animation_frames(window_id).unwrap());
}
fn wheel(position: Point, delta_y: f32) -> Event {
let mut wheel = PointerEvent::new(PointerEventKind::Scroll, position);
wheel.delta = Vector::new(0.0, delta_y);
Event::Pointer(wheel)
}
#[test]
fn offset_change_reports_wheel_and_fling_scrolling_in_order() {
let seen = Rc::new(RefCell::new(Vec::new()));
let plain = Rc::clone(&seen);
let with_ctx = Rc::clone(&seen);
let state = ScrollState::new();
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(80.0, 40.0)).child(
ScrollView::vertical(OverflowingBox::new(
Size::new(80.0, 4_000.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
))
.state(state.clone())
.on_offset_change(move |offset| plain.borrow_mut().push(("plain", offset.y)))
.on_offset_change_with_ctx(move |_, offset| {
with_ctx.borrow_mut().push(("ctx", offset.y));
}),
),
);
let _ = runtime.render(window_id).unwrap();
runtime
.handle_event(window_id, wheel(Point::new(20.0, 20.0), -30.0))
.unwrap();
assert_eq!(*seen.borrow(), vec![("plain", 30.0), ("ctx", 30.0)]);
let (time, position) = touch_swipe_up(
&mut runtime,
window_id,
5,
Point::new(20.0, 30.0),
10.0,
6,
1.0,
);
touch_up(&mut runtime, window_id, 5, position, time);
let released = seen.borrow().len();
let _ = run_frames(&mut runtime, time, 5.0);
let seen = seen.borrow();
assert!(seen.len() > released + 10);
assert_eq!(seen.last().unwrap().1, state.current_offset().y);
assert!(seen.windows(2).all(|pair| pair[0].1 <= pair[1].1));
}
#[test]
fn offset_change_reports_layout_moves_on_the_next_frame() {
let seen = Rc::new(RefCell::new(Vec::new()));
let seen_by_view = Rc::clone(&seen);
let state = ScrollState::new();
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(80.0, 60.0)).child(
ScrollView::vertical(OverflowingBox::new(
Size::new(80.0, 180.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
))
.name("Results")
.state(state.clone())
.on_offset_change_with_ctx(move |_, offset| {
seen_by_view.borrow_mut().push(offset.y);
}),
),
);
let output = runtime.render(window_id).unwrap();
let scroll_bar = output
.semantics
.iter()
.find(|node| node.name.as_deref() == Some("Results vertical scroll bar"))
.expect("embedded scroll bar present");
drag_vertical_scroll_bar(&mut runtime, window_id, scroll_bar.bounds, 42, 20.0);
let _ = runtime.render(window_id).unwrap();
let moved_to = state.current_offset().y;
assert!(moved_to > 0.0);
assert!(seen.borrow().is_empty(), "layout has no event context");
runtime.tick(1.0);
handle_ready_events(&mut runtime);
assert_eq!(*seen.borrow(), vec![moved_to]);
runtime.tick(2.0);
handle_ready_events(&mut runtime);
assert_eq!(*seen.borrow(), vec![moved_to]);
}
#[test]
fn virtual_scroll_view_reports_offset_changes() {
let seen = Rc::new(RefCell::new(Vec::new()));
let seen_by_view = Rc::clone(&seen);
let mut view = VirtualScrollView::new().on_offset_change(move |offset| {
seen_by_view.borrow_mut().push(offset);
});
for _ in 0..3 {
view.push(FixedBox::new(
Size::new(80.0, 40.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
));
}
let (mut runtime, window_id) =
build_runtime(SizedBox::new().size(Size::new(80.0, 40.0)).child(view));
let _ = runtime.render(window_id).unwrap();
runtime
.handle_event(window_id, wheel(Point::new(20.0, 20.0), -25.0))
.unwrap();
assert_eq!(*seen.borrow(), vec![Vector::new(0.0, 25.0)]);
}
#[test]
fn touch_pan_starting_on_button_cancels_click_but_tap_still_activates() {
let pans = Rc::new(Cell::new(0usize));
let pan_count = Rc::clone(&pans);
let pan_state = ScrollState::new();
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(100.0, 48.0)).child(
ScrollView::vertical(
Stack::vertical()
.with_child(crate::Button::new("Open").on_press(move || {
pan_count.set(pan_count.get() + 1);
}))
.with_child(FixedBox::new(
Size::new(100.0, 120.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
)),
)
.state(pan_state.clone()),
),
);
let _ = runtime.render(window_id).unwrap();
for pointer in [
touch_pointer(
PointerEventKind::Down,
12,
Point::new(20.0, 20.0),
Vector::ZERO,
),
touch_pointer(
PointerEventKind::Move,
12,
Point::new(20.0, 4.0),
Vector::new(0.0, -16.0),
),
touch_pointer(
PointerEventKind::Up,
12,
Point::new(20.0, 4.0),
Vector::ZERO,
),
] {
runtime
.handle_event(window_id, Event::Pointer(pointer))
.unwrap();
}
assert_eq!(pans.get(), 0, "a pan must not activate the button");
assert_eq!(pan_state.current_offset(), Vector::new(0.0, 16.0));
let taps = Rc::new(Cell::new(0usize));
let tap_count = Rc::clone(&taps);
let (mut tap_runtime, tap_window) = build_runtime(
SizedBox::new()
.size(Size::new(100.0, 48.0))
.child(ScrollView::vertical(
Stack::vertical()
.with_child(crate::Button::new("Open").on_press(move || {
tap_count.set(tap_count.get() + 1);
}))
.with_child(FixedBox::new(
Size::new(100.0, 120.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
)),
)),
);
let _ = tap_runtime.render(tap_window).unwrap();
for pointer in [
touch_pointer(
PointerEventKind::Down,
13,
Point::new(20.0, 20.0),
Vector::ZERO,
),
touch_pointer(
PointerEventKind::Up,
13,
Point::new(20.0, 20.0),
Vector::ZERO,
),
] {
tap_runtime
.handle_event(tap_window, Event::Pointer(pointer))
.unwrap();
}
assert_eq!(taps.get(), 1, "a tap should still activate exactly once");
}
#[test]
fn touch_pan_at_scroll_boundary_cancels_underlying_button() {
let activations = Rc::new(Cell::new(0usize));
let activation_count = Rc::clone(&activations);
let (mut runtime, window_id) = build_runtime(
SizedBox::new()
.size(Size::new(100.0, 48.0))
.child(ScrollView::vertical(
Stack::vertical()
.with_child(FixedBox::new(
Size::new(100.0, 120.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
))
.with_child(crate::Button::new("Bottom").on_press(move || {
activation_count.set(activation_count.get() + 1);
})),
)),
);
let _ = runtime.render(window_id).unwrap();
let mut scroll = PointerEvent::new(PointerEventKind::Scroll, Point::new(20.0, 20.0));
scroll.scroll_delta = Some(ScrollDelta::Pixels(Vector::new(0.0, -500.0)));
runtime
.handle_event(window_id, Event::Pointer(scroll))
.unwrap();
let output = runtime.render(window_id).unwrap();
let button = output
.semantics
.iter()
.find(|node| {
node.role == SemanticsRole::Button && node.name.as_deref() == Some("Bottom")
})
.expect("bottom button visible at the scroll boundary");
let down_position = Point::new(
button.bounds.x() + button.bounds.width() * 0.5,
button.bounds.y() + button.bounds.height() * 0.5,
);
let move_position = Point::new(down_position.x, down_position.y - 12.0);
for pointer in [
touch_pointer(PointerEventKind::Down, 14, down_position, Vector::ZERO),
touch_pointer(
PointerEventKind::Move,
14,
move_position,
Vector::new(0.0, -12.0),
),
touch_pointer(PointerEventKind::Up, 14, move_position, Vector::ZERO),
] {
runtime
.handle_event(window_id, Event::Pointer(pointer))
.unwrap();
}
assert_eq!(
activations.get(),
0,
"a boundary pan must cancel the pressed control even when content cannot move"
);
}
#[test]
fn shared_scroll_state_accepts_programmatic_offsets() {
let state = ScrollState::new();
state.sync_metrics(
ScrollAxes::Vertical,
Size::new(80.0, 40.0),
Size::new(80.0, 120.0),
);
assert!(state.set_offset(Vector::new(20.0, 32.0)));
assert_eq!(state.current_offset(), Vector::new(0.0, 32.0));
assert!(!state.set_offset(Vector::new(0.0, 32.0)));
assert!(state.set_offset(Vector::new(0.0, 200.0)));
assert_eq!(state.current_offset(), Vector::new(0.0, 80.0));
}
#[test]
fn scroll_bar_updates_after_wheel_scrolling_bound_scroll_view() {
let state = ScrollState::new();
let (mut runtime, window_id) = build_runtime(
SizedBox::new()
.size(Size::new(92.0, 40.0))
.child(ScrollBarHost::new(
ScrollView::vertical(FixedBox::new(
Size::new(80.0, 120.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
))
.state(state.clone())
.scroll_bars(false)
.name("Scrollable content"),
ScrollBar::vertical(state).name("Scroll bar"),
)),
);
let _ = runtime.render(window_id).unwrap();
let mut scroll = PointerEvent::new(PointerEventKind::Scroll, Point::new(20.0, 20.0));
scroll.scroll_delta = Some(ScrollDelta::Pixels(Vector::new(0.0, -32.0)));
runtime
.handle_event(window_id, Event::Pointer(scroll))
.unwrap();
let output = runtime.render(window_id).unwrap();
let graph = runtime.widget_graph(window_id).unwrap();
let content = scroll_view_content_with_height(&graph, 120.0);
let scroll_view = parent_node(&graph, content);
assert_eq!(
content.bounds,
Rect::new(0.0, -32.0, scroll_view.bounds.width(), 120.0)
);
let scroll_bar = output
.semantics
.iter()
.find(|node| {
node.role == SemanticsRole::Slider && node.name.as_deref() == Some("Scroll bar")
})
.expect("scroll bar semantics present");
assert_eq!(
scroll_bar.value,
Some(SemanticsValue::Range {
value: 32.0,
min: 0.0,
max: 80.0,
})
);
}
#[test]
fn embedded_overlay_scroll_bar_applies_semantic_actions() {
let state = ScrollState::new();
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(80.0, 40.0)).child(
ScrollView::vertical(FixedBox::new(
Size::new(80.0, 120.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
))
.state(state.clone())
.name("Semantic content"),
),
);
let scroll_bar_id = runtime
.render(window_id)
.unwrap()
.semantics
.iter()
.find(|node| {
node.role == SemanticsRole::Slider
&& node.name.as_deref() == Some("Semantic content vertical scroll bar")
})
.expect("overlay scroll bar semantics present")
.id;
assert!(
runtime
.handle_semantics_action(
window_id,
scroll_bar_id,
SemanticsActionRequest::Increment,
)
.unwrap()
);
assert_eq!(state.current_offset(), Vector::new(0.0, 40.0));
assert!(
runtime
.handle_semantics_action(
window_id,
scroll_bar_id,
SemanticsActionRequest::SetValue(SemanticsValue::Range {
value: 72.0,
min: 0.0,
max: 80.0,
}),
)
.unwrap()
);
assert_eq!(state.current_offset(), Vector::new(0.0, 72.0));
assert!(
runtime
.handle_semantics_action(
window_id,
scroll_bar_id,
SemanticsActionRequest::Decrement,
)
.unwrap()
);
assert_eq!(state.current_offset(), Vector::new(0.0, 32.0));
assert!(
!runtime
.handle_semantics_action(
window_id,
scroll_bar_id,
SemanticsActionRequest::SetValue(SemanticsValue::Text("invalid".to_string())),
)
.unwrap()
);
}
#[test]
fn scroll_bar_defaults_follow_theme_density_and_width_override() {
let compact_state = ScrollState::new();
compact_state.sync_metrics(
ScrollAxes::Vertical,
Size::new(80.0, 40.0),
Size::new(80.0, 120.0),
);
let (compact, _) = render_root(
ScrollBar::vertical(compact_state)
.theme(DefaultTheme::compact())
.name("Compact scroll bar"),
);
let touch_state = ScrollState::new();
touch_state.sync_metrics(
ScrollAxes::Vertical,
Size::new(80.0, 40.0),
Size::new(80.0, 120.0),
);
let (touch, _) = render_root(
ScrollBar::vertical(touch_state)
.theme(DefaultTheme::touch())
.name("Touch scroll bar"),
);
assert_eq!(
compact.frame.viewport.width,
DefaultTheme::compact().metrics.scroll_bar_thickness
);
assert_eq!(
touch.frame.viewport.width,
DefaultTheme::touch().metrics.scroll_bar_thickness
);
assert!(touch.frame.viewport.width > compact.frame.viewport.width);
let override_state = ScrollState::new();
override_state.sync_metrics(
ScrollAxes::Vertical,
Size::new(80.0, 40.0),
Size::new(80.0, 120.0),
);
let (overridden, _) = render_root(
ScrollBar::vertical(override_state)
.theme(DefaultTheme::touch())
.width(9.0),
);
assert_eq!(overridden.frame.viewport.width, 9.0);
}
#[test]
fn scroll_bar_theme_when_reads_current_theme() {
let state = ScrollState::new();
state.sync_metrics(
ScrollAxes::Vertical,
Size::new(80.0, 40.0),
Size::new(80.0, 120.0),
);
let theme = Rc::new(RefCell::new(DefaultTheme::touch()));
let theme_reader = Rc::clone(&theme);
let (output, _) = render_root(
ScrollBar::vertical(state)
.theme_when(move || *theme_reader.borrow())
.name("Themed scroll bar"),
);
assert_eq!(
output.frame.viewport.width,
DefaultTheme::touch().metrics.scroll_bar_thickness
);
}
#[test]
fn overlay_scroll_bar_expands_only_for_hover_capable_pointers() {
let theme = DefaultTheme::default();
let state = ScrollState::new();
state.sync_metrics(
ScrollAxes::Vertical,
Size::new(80.0, 40.0),
Size::new(80.0, 120.0),
);
let (mut runtime, window_id) = build_runtime(
ScrollBar::vertical(state)
.theme(theme)
.name("Overlay scroll bar")
.overlay(),
);
let idle = runtime.render(window_id).expect("render should succeed");
let bounds = idle
.semantics
.iter()
.find(|node| node.name.as_deref() == Some("Overlay scroll bar"))
.expect("scroll bar semantics present")
.bounds;
let idle_width =
super::OVERLAY_SCROLL_BAR_IDLE_THICKNESS.min(theme.metrics.scroll_bar_thickness);
let idle_paths = solid_fill_path_bounds(&idle);
assert_eq!(bounds.width(), theme.metrics.scroll_bar_thickness);
assert_eq!(idle_paths.len(), 2);
assert!(
idle_paths
.iter()
.all(|path| (path.width() - idle_width).abs() <= f32::EPSILON)
);
let point = Point::new(bounds.x() + bounds.width() * 0.5, bounds.y() + 8.0);
let mut wheel = PointerEvent::new(PointerEventKind::Scroll, point);
wheel.scroll_delta = Some(ScrollDelta::Pixels(Vector::new(0.0, 24.0)));
runtime
.handle_event(window_id, Event::Pointer(wheel))
.expect("wheel event should be handled");
let after_wheel = runtime.render(window_id).expect("render should succeed");
assert!(
solid_fill_path_bounds(&after_wheel)
.iter()
.all(|path| (path.width() - idle_width).abs() <= f32::EPSILON)
);
runtime
.handle_event(
window_id,
Event::Pointer(touch_pointer(
PointerEventKind::Move,
7,
point,
Vector::ZERO,
)),
)
.expect("touch move should be handled");
let after_touch = runtime.render(window_id).expect("render should succeed");
assert!(
solid_fill_path_bounds(&after_touch)
.iter()
.all(|path| (path.width() - idle_width).abs() <= f32::EPSILON)
);
runtime
.handle_event(
window_id,
Event::Pointer(PointerEvent::new(PointerEventKind::Move, point)),
)
.expect("mouse hover should be handled");
runtime.tick(theme.motion.hover_duration());
assert_eq!(handle_ready_events(&mut runtime), 0);
let hovered = runtime.render(window_id).expect("render should succeed");
assert!(solid_fill_path_bounds(&hovered).iter().all(|path| {
(path.width() - theme.metrics.scroll_bar_thickness).abs() <= f32::EPSILON
}));
}
#[test]
fn scroll_bar_hover_and_drag_use_theme_motion() {
let theme = DefaultTheme::default();
let state = ScrollState::new();
state.sync_metrics(
ScrollAxes::Vertical,
Size::new(80.0, 40.0),
Size::new(80.0, 120.0),
);
let point = Point::new(theme.metrics.scroll_bar_thickness * 0.5, 8.0);
let expected_hover = theme.palette.border_strong.with_alpha(0.95);
let expected_drag = theme.palette.text_muted.with_alpha(0.95);
let (mut runtime, window_id) =
build_runtime(ScrollBar::vertical(state).theme(theme).name("Scroll bar"));
let _ = runtime.render(window_id).expect("render should succeed");
runtime
.handle_event(
window_id,
Event::Pointer(PointerEvent::new(PointerEventKind::Move, point)),
)
.expect("hover event should be handled");
runtime.tick(theme.motion.hover_duration() * 0.5);
assert_eq!(handle_ready_events(&mut runtime), 0);
let mid_hover = runtime.render(window_id).expect("render should succeed");
assert!(!solid_fill_colors(&mid_hover).contains(&expected_hover));
runtime.tick(theme.motion.hover_duration());
assert_eq!(handle_ready_events(&mut runtime), 0);
let settled_hover = runtime.render(window_id).expect("render should succeed");
assert!(solid_fill_colors(&settled_hover).contains(&expected_hover));
let mut down = PointerEvent::new(PointerEventKind::Down, point);
down.pointer_id = 1;
down.button = Some(PointerButton::Primary);
down.buttons = PointerButtons::new(1);
runtime
.handle_event(window_id, Event::Pointer(down))
.expect("drag event should be handled");
runtime.tick(theme.motion.hover_duration() + theme.motion.press_duration() * 0.5);
assert_eq!(handle_ready_events(&mut runtime), 0);
let mid_drag = runtime.render(window_id).expect("render should succeed");
assert!(!solid_fill_colors(&mid_drag).contains(&expected_drag));
runtime.tick(theme.motion.hover_duration() + theme.motion.press_duration());
assert_eq!(handle_ready_events(&mut runtime), 0);
let settled_drag = runtime.render(window_id).expect("render should succeed");
assert!(solid_fill_colors(&settled_drag).contains(&expected_drag));
}
#[test]
fn scroll_bar_focus_stroke_uses_theme_motion() {
let theme = DefaultTheme::default();
let state = ScrollState::new();
state.sync_metrics(
ScrollAxes::Vertical,
Size::new(80.0, 40.0),
Size::new(80.0, 120.0),
);
let point = Point::new(theme.metrics.scroll_bar_thickness * 0.5, 8.0);
let (mut runtime, window_id) =
build_runtime(ScrollBar::vertical(state).theme(theme).name("Scroll bar"));
let _ = runtime.render(window_id).expect("render should succeed");
let mut down = PointerEvent::new(PointerEventKind::Down, point);
down.pointer_id = 1;
down.button = Some(PointerButton::Primary);
down.buttons = PointerButtons::new(1);
runtime
.handle_event(window_id, Event::Pointer(down))
.expect("focus event should be handled");
let _ = runtime.render(window_id).expect("render should succeed");
runtime.tick(theme.motion.focus_duration() * 0.5);
assert_eq!(handle_ready_events(&mut runtime), 0);
let mid_focus = runtime.render(window_id).expect("render should succeed");
assert!(
!contains_approx_color(&solid_stroke_colors(&mid_focus), theme.palette.focus_ring),
"scroll bar focus stroke should not snap to the settled focus color"
);
runtime.tick(theme.motion.focus_duration() + 0.01);
assert_eq!(handle_ready_events(&mut runtime), 0);
let settled_focus = runtime.render(window_id).expect("render should succeed");
let settled_strokes = solid_stroke_colors(&settled_focus);
assert!(
contains_approx_color(&settled_strokes, theme.palette.focus_ring),
"scroll bar focus stroke should settle to the theme focus color; strokes={settled_strokes:?}"
);
}
#[test]
fn scroll_view_focus_keeps_viewport_chrome_neutral() {
let theme = DefaultTheme::default();
let point = Point::new(20.0, 20.0);
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(80.0, 40.0)).child(
ScrollView::vertical(FixedBox::new(
Size::new(80.0, 120.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
))
.theme(theme),
),
);
let _ = runtime.render(window_id).expect("render should succeed");
let mut down = PointerEvent::new(PointerEventKind::Down, point);
down.pointer_id = 1;
down.button = Some(PointerButton::Primary);
down.buttons = PointerButtons::new(1);
runtime
.handle_event(window_id, Event::Pointer(down))
.expect("focus event should be handled");
let focused = runtime.render(window_id).expect("render should succeed");
let focused_strokes = solid_stroke_colors(&focused);
assert!(
!contains_approx_color(&focused_strokes, theme.palette.focus_ring),
"scroll view focus should not paint an activation ring; strokes={focused_strokes:?}"
);
}
#[test]
fn virtual_scroll_view_focus_keeps_viewport_chrome_neutral() {
let theme = DefaultTheme::default();
let point = Point::new(20.0, 20.0);
let (mut runtime, window_id) = build_runtime(
SizedBox::new().size(Size::new(80.0, 40.0)).child(
VirtualScrollView::new()
.theme(theme)
.with_child(FixedBox::new(
Size::new(80.0, 30.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
))
.with_child(FixedBox::new(
Size::new(80.0, 30.0),
Color::rgba(0.3, 0.4, 0.8, 1.0),
))
.with_child(FixedBox::new(
Size::new(80.0, 30.0),
Color::rgba(0.4, 0.5, 0.9, 1.0),
)),
),
);
let _ = runtime.render(window_id).expect("render should succeed");
let mut down = PointerEvent::new(PointerEventKind::Down, point);
down.pointer_id = 1;
down.button = Some(PointerButton::Primary);
down.buttons = PointerButtons::new(1);
runtime
.handle_event(window_id, Event::Pointer(down))
.expect("focus event should be handled");
let focused = runtime.render(window_id).expect("render should succeed");
let focused_strokes = solid_stroke_colors(&focused);
assert!(
!contains_approx_color(&focused_strokes, theme.palette.focus_ring),
"virtual scroll view focus should not paint an activation ring; strokes={focused_strokes:?}"
);
}
#[test]
fn scroll_bar_pointer_input_moves_bound_scroll_view() {
let state = ScrollState::new();
let (mut runtime, window_id) = build_runtime(
SizedBox::new()
.size(Size::new(92.0, 40.0))
.child(ScrollBarHost::new(
ScrollView::vertical(FixedBox::new(
Size::new(80.0, 120.0),
Color::rgba(0.2, 0.3, 0.7, 1.0),
))
.state(state.clone())
.scroll_bars(false),
ScrollBar::vertical(state),
)),
);
let _ = runtime.render(window_id).unwrap();
let mut down = PointerEvent::new(PointerEventKind::Down, Point::new(86.0, 36.0));
down.button = Some(PointerButton::Primary);
runtime
.handle_event(window_id, Event::Pointer(down))
.unwrap();
let mut up = PointerEvent::new(PointerEventKind::Up, Point::new(86.0, 36.0));
up.button = Some(PointerButton::Primary);
runtime.handle_event(window_id, Event::Pointer(up)).unwrap();
let _ = runtime.render(window_id).unwrap();
let graph = runtime.widget_graph(window_id).unwrap();
let content = scroll_view_content_with_height(&graph, 120.0);
let scroll_view = parent_node(&graph, content);
assert_eq!(
content.bounds,
Rect::new(0.0, -80.0, scroll_view.bounds.width(), 120.0)
);
}
}