use accesskit::{ActionData, Node, Role};
use include_doc_path::include_doc_path;
use tracing::{Span, trace_span};
use crate::core::keyboard::{Key, NamedKey};
use crate::core::{
AccessCtx, AccessEvent, ChildrenIds, CursorIcon, EventCtx, FromDynWidget, LayoutCtx,
MeasureCtx, NewWidget, NoAction, PaintCtx, PointerButtonEvent, PointerEvent, PointerUpdate,
PropertiesMut, PropertiesRef, QueryCtx, RegisterCtx, TextEvent, Update, UpdateCtx, Widget,
WidgetId, WidgetMut, WidgetPod,
};
use crate::imaging::Painter;
use crate::kurbo::{Axis, Join, Line, Point, Size, Stroke};
use crate::layout::{AsUnit, LayoutSize, LenReq, Length};
use crate::peniko::Color;
use crate::theme;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum SplitPoint {
Fraction(f64),
FromStart(Length),
FromEnd(Length),
}
#[doc = concat!(
",
")",
)]
pub struct Split<ChildA, ChildB>
where
ChildA: Widget + ?Sized,
ChildB: Widget + ?Sized,
{
split_axis: Axis,
split_point_chosen: SplitPoint,
split_point_effective: f64,
min_lengths: (Length, Length),
bar_thickness: Length,
min_bar_area: Length,
solid: bool,
draggable: bool,
click_offset: f64,
child1: WidgetPod<ChildA>,
child2: WidgetPod<ChildB>,
}
impl<ChildA: Widget + ?Sized, ChildB: Widget + ?Sized> Split<ChildA, ChildB> {
pub fn new(child1: NewWidget<ChildA>, child2: NewWidget<ChildB>) -> Self {
Self {
split_axis: Axis::Horizontal,
split_point_chosen: SplitPoint::Fraction(0.5),
split_point_effective: 0.5,
min_lengths: (Length::ZERO, Length::ZERO),
bar_thickness: 6.px(),
min_bar_area: 6.px(),
solid: false,
draggable: true,
click_offset: 0.0,
child1: child1.to_pod(),
child2: child2.to_pod(),
}
}
pub fn split_axis(mut self, split_axis: Axis) -> Self {
self.split_axis = split_axis;
self
}
pub fn split_fraction(mut self, split_point: f64) -> Self {
self.split_point_chosen = SplitPoint::Fraction(split_point.clamp(0.0, 1.0));
self
}
pub fn split_point(mut self, split_point: SplitPoint) -> Self {
self.split_point_chosen = match split_point {
SplitPoint::Fraction(frac) => SplitPoint::Fraction(frac.clamp(0.0, 1.0)),
other => other,
};
self
}
pub fn split_point_from_start(mut self, split_point: Length) -> Self {
self.split_point_chosen = SplitPoint::FromStart(split_point);
self
}
pub fn split_point_from_end(mut self, split_point: Length) -> Self {
self.split_point_chosen = SplitPoint::FromEnd(split_point);
self
}
pub fn min_lengths(mut self, first: Length, second: Length) -> Self {
self.min_lengths = (first, second);
self
}
pub fn bar_thickness(mut self, bar_thickness: Length) -> Self {
self.bar_thickness = bar_thickness;
self
}
pub fn min_bar_area(mut self, min_bar_area: Length) -> Self {
self.min_bar_area = min_bar_area;
self
}
pub fn draggable(mut self, draggable: bool) -> Self {
self.draggable = draggable;
self
}
pub fn solid_bar(mut self, solid: bool) -> Self {
self.solid = solid;
self
}
}
impl<ChildA: Widget + ?Sized, ChildB: Widget + ?Sized> Split<ChildA, ChildB> {
#[inline]
fn bar_area(&self) -> f64 {
self.bar_thickness.max(self.min_bar_area).get()
}
fn bar_center(&self, length: f64) -> f64 {
let (edge1, edge2) = self.bar_edges(length);
(edge1 + edge2) * 0.5
}
fn bar_edges(&self, length: f64) -> (f64, f64) {
let bar_thickness = self.bar_thickness.get();
let reduced_length = length - bar_thickness;
let edge = reduced_length * self.split_point_effective;
(edge, edge + bar_thickness)
}
fn bar_area_edges(&self, length: f64) -> (f64, f64) {
let (edge1, edge2) = self.bar_edges(length);
let (space1, space2) = (edge1.max(0.), (length - edge2).max(0.));
let padding = self.bar_area() - self.bar_thickness.get();
let pad1 = (0.5 * padding).min(space1);
let pad2 = (padding - pad1).min(space2);
let pad1 = (padding - pad2).min(space1);
(edge1 - pad1, edge2 + pad2)
}
fn bar_area_hit_test(&self, length: f64, pos: f64) -> bool {
let (edge1, edge2) = self.bar_area_edges(length);
pos >= edge1 && pos <= edge2
}
fn split_side_limits(&self, length: f64) -> (f64, f64) {
let (min_child1, min_child2) = self.min_lengths;
let mut min_limit = min_child1.get();
let mut max_limit = (length - min_child2.get()).max(0.0);
if min_limit > max_limit {
min_limit = 0.5 * (min_limit + max_limit);
max_limit = min_limit;
}
(min_limit, max_limit)
}
fn calc_effective_split_point(&self, length: f64) -> f64 {
let (min_limit, max_limit) = self.split_side_limits(length);
if length <= f64::EPSILON {
0.5
} else {
let child1_len = match self.split_point_chosen {
SplitPoint::Fraction(frac) => length * frac,
SplitPoint::FromStart(len) => len.get(),
SplitPoint::FromEnd(len) => length - len.get(),
};
(child1_len / length).clamp(min_limit / length, max_limit / length)
}
}
fn set_chosen_from_child1_len(&mut self, length: f64, child1_len: f64) {
let (min_limit, max_limit) = self.split_side_limits(length);
let child1_len = child1_len.clamp(min_limit, max_limit);
match self.split_point_chosen {
SplitPoint::Fraction(_) => {
self.split_point_chosen = SplitPoint::Fraction(if length <= f64::EPSILON {
0.5
} else {
child1_len / length
});
}
SplitPoint::FromStart(_) => {
self.split_point_chosen = SplitPoint::FromStart(Length::px(child1_len));
}
SplitPoint::FromEnd(_) => {
let child2_len = (length - child1_len).max(0.0);
self.split_point_chosen = SplitPoint::FromEnd(Length::px(child2_len));
}
}
}
fn update_split_point_from_bar_center(&mut self, total_length: f64, bar_center: f64) {
let bar_thickness = self.bar_thickness.get();
let split_space = (total_length - bar_thickness).max(0.0);
let child1_len = bar_center - bar_thickness * 0.5;
self.set_chosen_from_child1_len(split_space, child1_len);
}
fn bar_color(&self, ctx: &PaintCtx<'_>) -> Color {
if !self.draggable || ctx.is_disabled() {
return theme::ZYNC_700;
}
if ctx.is_active() || ctx.is_hovered() || ctx.is_focus_target() {
theme::ZYNC_600
} else {
theme::ZYNC_500
}
}
fn paint_focus_bar(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut Painter<'_>) {
let length = ctx.content_box().size().get_coord(self.split_axis);
let (edge1, edge2) = self.bar_edges(length);
let mut rect = ctx.border_box();
rect.set_coords(self.split_axis, edge1, edge2);
let rect = rect.inset(2.0);
let focus_color = theme::FOCUS_COLOR.with_alpha(if ctx.is_active() { 1.0 } else { 0.5 });
let focus_stroke = Stroke::new(1.0).with_join(Join::Miter);
scene.stroke(rect, &focus_stroke, focus_color).draw();
}
fn paint_solid_bar(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut Painter<'_>, color: Color) {
let length = ctx.content_box().size().get_coord(self.split_axis);
let (edge1, edge2) = self.bar_edges(length);
let mut rect = ctx.border_box();
rect.set_coords(self.split_axis, edge1, edge2);
scene.fill(rect, color).draw();
}
fn paint_stroked_bar(&mut self, ctx: &mut PaintCtx<'_>, scene: &mut Painter<'_>, color: Color) {
let length = ctx.content_box().size().get_coord(self.split_axis);
let line_width = self.bar_thickness.get() / 3.0;
let line_midpoint = line_width / 2.0;
let (edge1, edge2) = self.bar_edges(length);
let edge1_line_pos = edge1 + line_midpoint;
let edge2_line_pos = edge2 - line_midpoint;
let border_box = ctx.border_box();
let (cross1, cross2) = border_box.get_coords(self.split_axis.cross());
let line1_p1 = self.split_axis.pack_point(edge1_line_pos, cross1);
let line1_p2 = self.split_axis.pack_point(edge1_line_pos, cross2);
let line2_p1 = self.split_axis.pack_point(edge2_line_pos, cross1);
let line2_p2 = self.split_axis.pack_point(edge2_line_pos, cross2);
let (line1, line2) = (Line::new(line1_p1, line1_p2), Line::new(line2_p1, line2_p2));
let style = Stroke::new(line_width);
scene.stroke(line1, &style, color).draw();
scene.stroke(line2, &style, color).draw();
}
}
impl<ChildA, ChildB> Split<ChildA, ChildB>
where
ChildA: Widget + FromDynWidget + ?Sized,
ChildB: Widget + FromDynWidget + ?Sized,
{
pub fn set_child1(this: &mut WidgetMut<'_, Self>, child: NewWidget<ChildA>) {
this.ctx
.remove_child(std::mem::replace(&mut this.widget.child1, child.to_pod()));
}
pub fn set_child2(this: &mut WidgetMut<'_, Self>, child: NewWidget<ChildB>) {
this.ctx
.remove_child(std::mem::replace(&mut this.widget.child2, child.to_pod()));
}
pub fn child1_mut<'t>(this: &'t mut WidgetMut<'_, Self>) -> WidgetMut<'t, ChildA> {
this.ctx.get_mut(&mut this.widget.child1)
}
pub fn child2_mut<'t>(this: &'t mut WidgetMut<'_, Self>) -> WidgetMut<'t, ChildB> {
this.ctx.get_mut(&mut this.widget.child2)
}
pub fn set_split_axis(this: &mut WidgetMut<'_, Self>, split_axis: Axis) {
this.widget.split_axis = split_axis;
this.ctx.request_layout();
}
pub fn set_split_point(this: &mut WidgetMut<'_, Self>, split_point: SplitPoint) {
this.widget.split_point_chosen = match split_point {
SplitPoint::Fraction(frac) => SplitPoint::Fraction(frac.clamp(0.0, 1.0)),
other => other,
};
this.ctx.request_layout();
}
pub fn set_min_lengths(this: &mut WidgetMut<'_, Self>, first: Length, second: Length) {
this.widget.min_lengths = (first, second);
this.ctx.request_layout();
}
pub fn set_bar_thickness(this: &mut WidgetMut<'_, Self>, bar_thickness: Length) {
this.widget.bar_thickness = bar_thickness;
this.ctx.request_layout();
}
pub fn set_min_bar_area(this: &mut WidgetMut<'_, Self>, min_bar_area: Length) {
this.widget.min_bar_area = min_bar_area;
this.ctx.request_layout();
}
pub fn set_draggable(this: &mut WidgetMut<'_, Self>, draggable: bool) {
this.widget.draggable = draggable;
this.ctx.request_paint_only();
}
pub fn set_bar_solid(this: &mut WidgetMut<'_, Self>, solid: bool) {
this.widget.solid = solid;
this.ctx.request_paint_only();
}
}
impl<ChildA, ChildB> Widget for Split<ChildA, ChildB>
where
ChildA: Widget + ?Sized,
ChildB: Widget + ?Sized,
{
type Action = NoAction;
fn accepts_focus(&self) -> bool {
true
}
fn on_pointer_event(
&mut self,
ctx: &mut EventCtx<'_>,
_props: &mut PropertiesMut<'_>,
event: &PointerEvent,
) {
if self.draggable {
match event {
PointerEvent::Down(PointerButtonEvent { state, .. }) => {
let pos = ctx
.local_position(state.position)
.get_coord(self.split_axis);
let length = ctx.content_box().size().get_coord(self.split_axis);
if self.bar_area_hit_test(length, pos) {
ctx.set_handled();
ctx.capture_pointer();
ctx.request_focus();
self.click_offset = pos - self.bar_center(length);
}
}
PointerEvent::Move(PointerUpdate { current, .. }) if ctx.is_active() => {
let pos = ctx
.local_position(current.position)
.get_coord(self.split_axis);
let length = ctx.content_box().size().get_coord(self.split_axis);
let effective_center = pos - self.click_offset;
self.update_split_point_from_bar_center(length, effective_center);
ctx.request_layout();
}
PointerEvent::Up(..) | PointerEvent::Cancel(..) => {
self.click_offset = 0.0;
}
_ => {}
}
}
}
fn on_text_event(
&mut self,
ctx: &mut EventCtx<'_>,
_props: &mut PropertiesMut<'_>,
event: &TextEvent,
) {
if ctx.is_disabled() || !ctx.is_focus_target() || !self.draggable {
return;
}
let TextEvent::Keyboard(key_event) = event else {
return;
};
if !key_event.state.is_down() {
return;
}
let length = ctx.content_box().size().get_coord(self.split_axis);
let bar_thickness = self.bar_thickness.get();
let split_space = (length - bar_thickness).max(0.0);
if split_space <= f64::EPSILON {
return;
}
let step = (split_space / 100.0).max(1.0);
let big_step = step * 10.0;
let delta = if key_event.modifiers.shift() {
big_step
} else {
step
};
let mut child1_len = split_space * self.split_point_effective;
match key_event.key {
Key::Named(NamedKey::ArrowLeft) if self.split_axis == Axis::Horizontal => {
child1_len -= delta;
}
Key::Named(NamedKey::ArrowRight) if self.split_axis == Axis::Horizontal => {
child1_len += delta;
}
Key::Named(NamedKey::ArrowUp) if self.split_axis == Axis::Vertical => {
child1_len -= delta;
}
Key::Named(NamedKey::ArrowDown) if self.split_axis == Axis::Vertical => {
child1_len += delta;
}
Key::Named(NamedKey::Home) => {
child1_len = self.split_side_limits(split_space).0;
}
Key::Named(NamedKey::End) => {
child1_len = self.split_side_limits(split_space).1;
}
_ => return,
}
self.set_chosen_from_child1_len(split_space, child1_len);
ctx.request_layout();
}
fn on_access_event(
&mut self,
ctx: &mut EventCtx<'_>,
_props: &mut PropertiesMut<'_>,
event: &AccessEvent,
) {
if ctx.is_disabled() || !self.draggable {
return;
}
let length = ctx.content_box().size().get_coord(self.split_axis);
let bar_thickness = self.bar_thickness.get();
let split_space = (length - bar_thickness).max(0.0);
if split_space <= f64::EPSILON {
return;
}
let step = (split_space / 100.0).max(1.0);
let mut child1_len = split_space * self.split_point_effective;
match event.action {
accesskit::Action::Increment => child1_len += step,
accesskit::Action::Decrement => child1_len -= step,
accesskit::Action::SetValue => match &event.data {
Some(ActionData::NumericValue(value)) => child1_len = *value,
Some(ActionData::Value(value)) => {
if let Ok(value) = value.parse() {
child1_len = value;
}
}
_ => return,
},
_ => return,
}
self.set_chosen_from_child1_len(split_space, child1_len);
ctx.request_layout();
}
fn register_children(&mut self, ctx: &mut RegisterCtx<'_>) {
ctx.register_child(&mut self.child1);
ctx.register_child(&mut self.child2);
}
fn update(&mut self, ctx: &mut UpdateCtx<'_>, _props: &mut PropertiesMut<'_>, event: &Update) {
match event {
Update::FocusChanged(_)
| Update::HoveredChanged(_)
| Update::ActiveChanged(_)
| Update::DisabledChanged(_) => {
ctx.request_paint_only();
}
_ => {}
}
}
fn measure(
&mut self,
ctx: &mut MeasureCtx<'_>,
_props: &PropertiesRef<'_>,
axis: Axis,
len_req: LenReq,
cross_length: Option<Length>,
) -> Length {
if let LenReq::FitContent(space) = len_req {
if axis == self.split_axis {
return space.max(self.bar_thickness);
}
return space;
}
let auto_length = len_req.into();
let cross = axis.cross();
let (child1_cross_space, child2_cross_space) = cross_length
.map(|cross_length| {
if cross == self.split_axis {
let cross_space = cross_length.saturating_sub(self.bar_thickness);
let split_point = self.calc_effective_split_point(cross_space.get());
let child1_cross_space = (cross_space.get() * split_point).px();
(
child1_cross_space,
cross_space.saturating_sub(child1_cross_space),
)
} else {
(cross_length, cross_length)
}
})
.unzip();
let child1_context_size = LayoutSize::maybe(cross, child1_cross_space);
let child2_context_size = LayoutSize::maybe(cross, child2_cross_space);
let child1_length = ctx.compute_length(
&mut self.child1,
auto_length,
child1_context_size,
axis,
child1_cross_space,
);
let child2_length = ctx.compute_length(
&mut self.child2,
auto_length,
child2_context_size,
axis,
child2_cross_space,
);
if axis == self.split_axis {
child1_length
.saturating_add(child2_length)
.saturating_add(self.bar_thickness)
} else {
child1_length.max(child2_length)
}
}
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, _props: &PropertiesRef<'_>, size: Size) {
let bar_thickness = self.bar_thickness.get();
let split_space = (size.get_coord(self.split_axis) - bar_thickness).max(0.);
let cross_space = size.get_coord(self.split_axis.cross());
self.split_point_effective = self.calc_effective_split_point(split_space);
let child1_split_space = (split_space * self.split_point_effective).max(0.);
let child2_split_space = (split_space - child1_split_space).max(0.);
let child1_size = self.split_axis.pack_size(child1_split_space, cross_space);
let child2_size = self.split_axis.pack_size(child2_split_space, cross_space);
ctx.run_layout(&mut self.child1, child1_size);
ctx.run_layout(&mut self.child2, child2_size);
let child1_origin = Point::ORIGIN;
let child2_origin = self
.split_axis
.pack_point(child1_split_space + bar_thickness, 0.);
ctx.place_child(&mut self.child1, child1_origin);
ctx.place_child(&mut self.child2, child2_origin);
}
fn paint(
&mut self,
ctx: &mut PaintCtx<'_>,
_props: &PropertiesRef<'_>,
painter: &mut Painter<'_>,
) {
let bar_color = self.bar_color(ctx);
if self.solid {
self.paint_solid_bar(ctx, painter, bar_color);
} else {
self.paint_stroked_bar(ctx, painter, bar_color);
}
if ctx.is_focus_target() && self.draggable && !ctx.is_disabled() {
self.paint_focus_bar(ctx, painter);
}
}
fn get_cursor(&self, ctx: &QueryCtx<'_>, pos: Point) -> CursorIcon {
let length = ctx.content_box().size().get_coord(self.split_axis);
let local_pos = ctx.to_local(pos).get_coord(self.split_axis);
let is_bar_area_hovered = self.bar_area_hit_test(length, local_pos);
if self.draggable && (ctx.is_active() || is_bar_area_hovered) {
match self.split_axis {
Axis::Horizontal => CursorIcon::EwResize,
Axis::Vertical => CursorIcon::NsResize,
}
} else {
CursorIcon::Default
}
}
fn accessibility_role(&self) -> Role {
Role::Splitter
}
fn accessibility(
&mut self,
ctx: &mut AccessCtx<'_>,
_props: &PropertiesRef<'_>,
node: &mut Node,
) {
let length = ctx.content_box().size().get_coord(self.split_axis);
let bar_thickness = self.bar_thickness.get();
let split_space = (length - bar_thickness).max(0.0);
let (min_limit, max_limit) = self.split_side_limits(split_space);
let child1_len = split_space * self.split_point_effective;
node.set_orientation(match self.split_axis {
Axis::Horizontal => accesskit::Orientation::Horizontal,
Axis::Vertical => accesskit::Orientation::Vertical,
});
node.set_value(child1_len.to_string());
node.set_numeric_value(child1_len);
node.set_min_numeric_value(min_limit);
node.set_max_numeric_value(max_limit);
node.set_numeric_value_step((split_space / 100.0).max(1.0));
if self.draggable && !ctx.is_disabled() {
node.add_action(accesskit::Action::SetValue);
node.add_action(accesskit::Action::Increment);
node.add_action(accesskit::Action::Decrement);
}
}
fn children_ids(&self) -> ChildrenIds {
ChildrenIds::from_slice(&[self.child1.id(), self.child2.id()])
}
fn make_trace_span(&self, id: WidgetId) -> Span {
trace_span!("Split", id = id.trace())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::{TextEvent, WindowEvent};
use crate::dpi::PhysicalSize;
use crate::properties::Padding;
use crate::testing::{TestHarness, assert_render_snapshot};
use crate::theme::test_property_set;
use crate::widgets::Label;
#[test]
fn columns() {
#[rustfmt::skip]
let widget = Split::new(
Label::new("Hello").prepare(),
Label::new("World").prepare(),
).split_axis(Axis::Horizontal).draggable(false).prepare();
let mut harness = TestHarness::create_with_size(test_property_set(), widget, (150, 150));
assert_render_snapshot!(harness, "split_columns");
}
#[test]
fn rows() {
#[rustfmt::skip]
let widget = Split::new(
Label::new("Hello").prepare(),
Label::new("World").prepare(),
).split_axis(Axis::Vertical).draggable(false).prepare();
let mut harness = TestHarness::create_with_size(test_property_set(), widget, (150, 150));
assert_render_snapshot!(harness, "split_rows");
}
#[test]
fn edit_splitter() {
let image_1 = {
let widget = Split::new(Label::new("Hello").prepare(), Label::new("World").prepare())
.split_fraction(0.3)
.min_lengths(40.px(), 10.px())
.bar_thickness(12.px())
.draggable(true)
.solid_bar(true)
.prepare();
let mut harness =
TestHarness::create_with_size(test_property_set(), widget, (100, 100));
harness.render()
};
let image_2 = {
let widget =
Split::new(Label::new("Hello").prepare(), Label::new("World").prepare()).prepare();
let mut harness =
TestHarness::create_with_size(test_property_set(), widget, (100, 100));
harness.edit_root_widget(|mut splitter| {
Split::set_split_point(&mut splitter, SplitPoint::Fraction(0.3));
Split::set_min_lengths(&mut splitter, 40.px(), 10.px());
Split::set_bar_thickness(&mut splitter, 12.px());
Split::set_draggable(&mut splitter, true);
Split::set_bar_solid(&mut splitter, true);
});
harness.render()
};
assert!(image_1 == image_2);
}
#[test]
fn drag_moves_split_point() {
let widget =
Split::new(Label::new("Hello").prepare(), Label::new("World").prepare()).prepare();
let mut harness = TestHarness::create_with_size(test_property_set(), widget, (150, 100));
let child1_initial_width = {
let root = harness.root_widget();
root.children()[0].ctx().border_box().size().width
};
harness.mouse_move(Point::new(75.0, 10.0));
harness.mouse_button_press(None);
harness.mouse_move(Point::new(105.0, 10.0));
harness.mouse_button_release(None);
let (child1_width, child2_width) = {
let root = harness.root_widget();
let children = root.children();
(
children[0].ctx().border_box().size().width,
children[1].ctx().border_box().size().width,
)
};
assert!(child1_width > child1_initial_width);
assert!((child1_width - 102.0).abs() < 0.01);
assert!((child2_width - 42.0).abs() < 0.01);
}
#[test]
fn keyboard_moves_split_point() {
let widget =
Split::new(Label::new("Hello").prepare(), Label::new("World").prepare()).prepare();
let mut harness = TestHarness::create_with_size(test_property_set(), widget, (150, 100));
let root_tag = harness.root_tag();
harness.focus_on(root_tag);
let child1_initial_width = {
let root = harness.root_widget();
root.children()[0].ctx().border_box().size().width
};
harness.process_text_event(TextEvent::key_down(Key::Named(NamedKey::ArrowRight)));
let child1_width = {
let root = harness.root_widget();
root.children()[0].ctx().border_box().size().width
};
assert!(child1_width > child1_initial_width);
}
#[test]
fn from_start_keeps_pixel_size_on_resize() {
let widget = Split::new(Label::new("Hello").prepare(), Label::new("World").prepare())
.split_point(SplitPoint::FromStart(50.px()))
.prepare();
let mut harness = TestHarness::create_with_size(test_property_set(), widget, (200, 100));
let child1_width = {
let root = harness.root_widget();
root.children()[0].ctx().border_box().size().width
};
assert!((child1_width - 50.0).abs() < 0.01);
harness.process_window_event(WindowEvent::Resize(PhysicalSize::new(300, 100)));
let child1_width = {
let root = harness.root_widget();
root.children()[0].ctx().border_box().size().width
};
assert!((child1_width - 50.0).abs() < 0.01);
}
#[test]
fn from_end_keeps_pixel_size_on_resize() {
let widget = Split::new(Label::new("Hello").prepare(), Label::new("World").prepare())
.split_point(SplitPoint::FromEnd(50.px()))
.prepare();
let mut harness = TestHarness::create_with_size(test_property_set(), widget, (200, 100));
let child2_width = {
let root = harness.root_widget();
root.children()[1].ctx().border_box().size().width
};
assert!((child2_width - 50.0).abs() < 0.01);
harness.process_window_event(WindowEvent::Resize(PhysicalSize::new(300, 100)));
let child2_width = {
let root = harness.root_widget();
root.children()[1].ctx().border_box().size().width
};
assert!((child2_width - 50.0).abs() < 0.01);
}
#[test]
fn fraction_clamps_when_set() {
let widget = Split::new(
Label::new("Hello").prepare().with_props(Padding::ZERO),
Label::new("World").prepare().with_props(Padding::ZERO),
)
.prepare();
let mut harness = TestHarness::create_with_size(test_property_set(), widget, (150, 100));
harness.edit_root_widget(|mut split| {
Split::set_split_point(&mut split, SplitPoint::Fraction(2.0));
});
let child1_width = {
let root = harness.root_widget();
root.children()[0].ctx().border_box().size().width
};
assert!((child1_width - 144.0).abs() < 0.01);
harness.edit_root_widget(|mut split| {
Split::set_split_point(&mut split, SplitPoint::Fraction(-1.0));
});
let child1_width = {
let root = harness.root_widget();
root.children()[0].ctx().border_box().size().width
};
assert!((child1_width - 0.0).abs() < 0.01);
}
}