use std::any::TypeId;
use accesskit::{Node, Role};
use include_doc_path::include_doc_path;
use tracing::{Span, trace_span};
use crate::core::{
AccessCtx, ChildrenIds, LayoutCtx, MeasureCtx, NewWidget, NoAction, PaintCtx, PropertiesRef,
RegisterCtx, UpdateCtx, Widget, WidgetId, WidgetMut, WidgetPod,
};
use crate::imaging::Painter;
use crate::kurbo::{Axis, Point, Size};
use crate::layout::{LayoutSize, LenReq, Length};
use crate::properties::{BorderWidth, Padding};
#[doc = concat!(
",
")",
)]
pub struct SizedBox {
child: Option<WidgetPod<dyn Widget>>,
width: Option<Length>,
height: Option<Length>,
}
impl SizedBox {
pub fn new(child: NewWidget<impl Widget + ?Sized>) -> Self {
Self {
child: Some(child.erased().to_pod()),
width: None,
height: None,
}
}
#[doc(alias = "null")]
pub fn empty() -> Self {
Self {
child: None,
width: None,
height: None,
}
}
pub fn width(mut self, width: Length) -> Self {
self.width = Some(width);
self
}
pub fn height(mut self, height: Length) -> Self {
self.height = Some(height);
self
}
pub fn size(mut self, width: Length, height: Length) -> Self {
self.width = Some(width);
self.height = Some(height);
self
}
pub fn raw_width(mut self, width: Option<Length>) -> Self {
self.width = width;
self
}
pub fn raw_height(mut self, height: Option<Length>) -> Self {
self.height = height;
self
}
}
impl SizedBox {
pub const fn length(&self, axis: Axis) -> Option<Length> {
match axis {
Axis::Horizontal => self.width,
Axis::Vertical => self.height,
}
}
}
impl SizedBox {
pub fn set_child(this: &mut WidgetMut<'_, Self>, child: NewWidget<impl Widget + ?Sized>) {
if let Some(child) = this.widget.child.take() {
this.ctx.remove_child(child);
}
this.widget.child = Some(child.erased().to_pod());
this.ctx.children_changed();
}
pub fn remove_child(this: &mut WidgetMut<'_, Self>) {
if let Some(child) = this.widget.child.take() {
this.ctx.remove_child(child);
}
}
pub fn set_width(this: &mut WidgetMut<'_, Self>, width: Length) {
this.widget.width = Some(width);
this.ctx.request_layout();
}
pub fn set_height(this: &mut WidgetMut<'_, Self>, height: Length) {
this.widget.height = Some(height);
this.ctx.request_layout();
}
pub fn set_size(this: &mut WidgetMut<'_, Self>, width: Length, height: Length) {
this.widget.width = Some(width);
this.widget.height = Some(height);
this.ctx.request_layout();
}
pub fn unset_width(this: &mut WidgetMut<'_, Self>) {
this.widget.width = None;
this.ctx.request_layout();
}
pub fn unset_height(this: &mut WidgetMut<'_, Self>) {
this.widget.height = None;
this.ctx.request_layout();
}
pub fn set_raw_width(this: &mut WidgetMut<'_, Self>, width: Option<Length>) {
this.widget.width = width;
this.ctx.request_layout();
}
pub fn set_raw_height(this: &mut WidgetMut<'_, Self>, height: Option<Length>) {
this.widget.height = height;
this.ctx.request_layout();
}
pub fn child_mut<'t>(this: &'t mut WidgetMut<'_, Self>) -> Option<WidgetMut<'t, dyn Widget>> {
let child = this.widget.child.as_mut()?;
Some(this.ctx.get_mut(child))
}
}
impl Widget for SizedBox {
type Action = NoAction;
fn register_children(&mut self, ctx: &mut RegisterCtx<'_>) {
if let Some(ref mut child) = self.child {
ctx.register_child(child);
}
}
fn property_changed(&mut self, _ctx: &mut UpdateCtx<'_>, _property_type: TypeId) {}
fn measure(
&mut self,
ctx: &mut MeasureCtx<'_>,
props: &PropertiesRef<'_>,
axis: Axis,
len_req: LenReq,
cross_length: Option<Length>,
) -> Length {
let cache = ctx.property_cache();
let border = props.get::<BorderWidth>(cache);
let padding = props.get::<Padding>(cache);
let border_length = border.length(axis);
let padding_length = padding.length(axis);
if let Some(length) = self.length(axis) {
return length
.saturating_sub(border_length)
.saturating_sub(padding_length);
}
if let Some(child) = self.child.as_mut() {
let cross = axis.cross();
let cross_length = cross_length.or_else(|| {
let length = match cross {
Axis::Horizontal => self.width,
Axis::Vertical => self.height,
};
length.map(|length| {
length
.saturating_sub(border.length(cross))
.saturating_sub(padding.length(cross))
})
});
let auto_length = len_req.into();
let context_size = LayoutSize::maybe(cross, cross_length);
ctx.compute_length(child, auto_length, context_size, axis, cross_length)
} else {
Length::ZERO
}
}
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, _props: &PropertiesRef<'_>, size: Size) {
let Some(child) = self.child.as_mut() else {
ctx.clear_baselines();
return;
};
ctx.run_layout(child, size);
ctx.place_child(child, Point::ORIGIN);
ctx.derive_baselines(child);
}
fn paint(
&mut self,
_ctx: &mut PaintCtx<'_>,
_props: &PropertiesRef<'_>,
_painter: &mut Painter<'_>,
) {
}
fn accessibility_role(&self) -> Role {
Role::GenericContainer
}
fn accessibility(
&mut self,
_ctx: &mut AccessCtx<'_>,
_props: &PropertiesRef<'_>,
_node: &mut Node,
) {
}
fn children_ids(&self) -> ChildrenIds {
if let Some(child) = &self.child {
ChildrenIds::from_slice(&[child.id()])
} else {
ChildrenIds::from_slice(&[])
}
}
fn make_trace_span(&self, id: WidgetId) -> Span {
trace_span!("SizedBox", id = id.trace())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::PropertySet;
use crate::layout::{AsUnit, UnitPoint};
use crate::palette;
use crate::properties::types::Gradient;
use crate::properties::{Background, BorderColor, CornerRadius};
use crate::testing::{TestHarness, assert_failing_render_snapshot, assert_render_snapshot};
use crate::theme::test_property_set;
use crate::widgets::Label;
#[test]
fn empty_box() {
let mut box_props = PropertySet::new();
box_props.insert(BorderColor::new(palette::css::BLUE));
box_props.insert(BorderWidth::all(5.px()));
box_props.insert(CornerRadius::all(5.px()));
let widget = SizedBox::empty()
.width(20.px())
.height(20.px())
.prepare()
.with_props(box_props);
let mut harness = TestHarness::create_with_size(test_property_set(), widget, (100, 100));
assert_render_snapshot!(harness, "sized_box_empty_box");
}
#[test]
fn label_box_no_size() {
let mut box_props = PropertySet::new();
box_props.insert(BorderColor::new(palette::css::BLUE));
box_props.insert(BorderWidth::all(5.px()));
box_props.insert(CornerRadius::all(5.px()));
let widget = SizedBox::new(Label::new("hello").prepare())
.prepare()
.with_props(box_props);
let mut harness = TestHarness::create_with_size(test_property_set(), widget, (100, 100));
assert_render_snapshot!(harness, "sized_box_label_box_no_size");
}
#[test]
fn label_box_with_size() {
let mut box_props = PropertySet::new();
box_props.insert(BorderColor::new(palette::css::BLUE));
box_props.insert(BorderWidth::all(5.px()));
box_props.insert(CornerRadius::all(5.px()));
let widget = SizedBox::new(Label::new("hello").prepare())
.width(20.px())
.height(20.px())
.prepare()
.with_props(box_props);
let mut harness = TestHarness::create_with_size(test_property_set(), widget, (100, 100));
assert_render_snapshot!(harness, "sized_box_label_box_with_size");
}
#[test]
fn label_box_with_padding() {
let mut box_props = PropertySet::new();
box_props.insert(BorderColor::new(palette::css::BLUE));
box_props.insert(BorderWidth::all(5.px()));
box_props.insert(CornerRadius::all(5.px()));
box_props.insert(Padding::from_vh(15.px(), 10.px()));
let widget = SizedBox::new(Label::new("hello").prepare())
.prepare()
.with_props(box_props);
let mut harness = TestHarness::create_with_size(test_property_set(), widget, (100, 100));
assert_render_snapshot!(harness, "sized_box_label_box_with_padding");
}
#[test]
fn label_box_with_solid_background() {
let mut box_props = PropertySet::new();
box_props.insert(Background::Color(palette::css::PLUM));
let widget = SizedBox::new(Label::new("hello").prepare())
.width(20.px())
.height(20.px())
.prepare()
.with_props(box_props);
let mut harness = TestHarness::create_with_size(test_property_set(), widget, (100, 100));
assert_render_snapshot!(harness, "sized_box_label_box_with_solid_background");
}
#[test]
fn empty_box_with_gradient_background() {
let mut box_props = PropertySet::new();
let gradient = Gradient::new_linear(2.0).with_stops([
palette::css::WHITE,
palette::css::BLACK,
palette::css::RED,
palette::css::GREEN,
palette::css::WHITE,
]);
box_props.insert(Background::Gradient(gradient));
box_props.insert(BorderColor::new(palette::css::LIGHT_SKY_BLUE));
box_props.insert(BorderWidth::all(5.px()));
box_props.insert(CornerRadius::all(10.px()));
let widget = SizedBox::empty()
.width(20.px())
.height(20.px())
.prepare()
.with_props(box_props);
let mut harness = TestHarness::create_with_size(test_property_set(), widget, (100, 100));
assert_render_snapshot!(harness, "sized_box_empty_box_with_gradient_background");
}
#[test]
fn radial_gradient_background() {
let mut box_props = PropertySet::new();
let gradient = Gradient::new_radial(UnitPoint::CENTER).with_stops([
palette::css::WHITE,
palette::css::BLACK,
palette::css::RED,
palette::css::GREEN,
palette::css::WHITE,
]);
box_props.insert(Background::Gradient(gradient));
box_props.insert(BorderColor::new(palette::css::LIGHT_SKY_BLUE));
box_props.insert(BorderWidth::all(5.px()));
box_props.insert(CornerRadius::all(10.px()));
let widget = SizedBox::empty()
.width(20.px())
.height(20.px())
.prepare()
.with_props(box_props);
let mut harness = TestHarness::create_with_size(test_property_set(), widget, (100, 100));
assert_render_snapshot!(harness, "sized_box_radial_gradient_background");
}
#[test]
fn sweep_gradient_background() {
let mut box_props = PropertySet::new();
let gradient = Gradient::new_full_sweep(UnitPoint::CENTER, 0.).with_stops([
palette::css::WHITE,
palette::css::BLACK,
palette::css::RED,
palette::css::GREEN,
palette::css::WHITE,
]);
box_props.insert(Background::Gradient(gradient));
box_props.insert(BorderColor::new(palette::css::LIGHT_SKY_BLUE));
box_props.insert(BorderWidth::all(5.px()));
box_props.insert(CornerRadius::all(10.px()));
let widget = SizedBox::empty()
.width(20.px())
.height(20.px())
.prepare()
.with_props(box_props);
let mut harness = TestHarness::create_with_size(test_property_set(), widget, (100, 100));
assert_render_snapshot!(harness, "sized_box_sweep_gradient_background");
}
#[test]
fn label_box_with_padding_and_background() {
let mut box_props = PropertySet::new();
box_props.insert(Background::Color(palette::css::PLUM));
box_props.insert(BorderColor::new(palette::css::LIGHT_SKY_BLUE));
box_props.insert(BorderWidth::all(5.px()));
box_props.insert(Padding::all(25.px()));
let widget = SizedBox::new(Label::new("hello").prepare())
.width(20.px())
.height(20.px())
.prepare()
.with_props(box_props);
let mut harness = TestHarness::create_with_size(test_property_set(), widget, (100, 100));
assert_render_snapshot!(harness, "sized_box_label_box_with_background_and_padding");
}
#[test]
fn invalid_screenshot() {
let mut box_props = PropertySet::new();
box_props.insert(BorderColor::new(palette::css::BLUE));
box_props.insert(BorderWidth::all(5.px()));
box_props.insert(CornerRadius::all(5.px()));
box_props.insert(BorderWidth::all(5.2.px()));
let widget = SizedBox::empty()
.width(20.px())
.height(20.px())
.prepare()
.with_props(box_props);
let mut harness = TestHarness::create_with_size(test_property_set(), widget, (100, 100));
assert_failing_render_snapshot!(harness, "sized_box_empty_box");
}
}