use accesskit::Role;
use smallvec::{smallvec, SmallVec};
use tracing::{trace, trace_span, Span};
use vello::Scene;
use crate::contexts::AccessCtx;
use crate::paint_scene_helpers::UnitPoint;
use crate::widget::{WidgetPod, WidgetRef};
use crate::{
AccessEvent, BoxConstraints, EventCtx, LayoutCtx, LifeCycle, LifeCycleCtx, PaintCtx,
PointerEvent, Rect, Size, StatusChange, TextEvent, Widget,
};
pub struct Align {
align: UnitPoint,
child: WidgetPod<Box<dyn Widget>>,
width_factor: Option<f64>,
height_factor: Option<f64>,
}
impl Align {
pub fn new(align: UnitPoint, child: impl Widget + 'static) -> Align {
Align {
align,
child: WidgetPod::new(child).boxed(),
width_factor: None,
height_factor: None,
}
}
pub fn centered(child: impl Widget + 'static) -> Align {
Align::new(UnitPoint::CENTER, child)
}
pub fn right(child: impl Widget + 'static) -> Align {
Align::new(UnitPoint::RIGHT, child)
}
pub fn left(child: impl Widget + 'static) -> Align {
Align::new(UnitPoint::LEFT, child)
}
pub fn horizontal(align: UnitPoint, child: impl Widget + 'static) -> Align {
Align {
align,
child: WidgetPod::new(child).boxed(),
width_factor: None,
height_factor: Some(1.0),
}
}
pub fn vertical(align: UnitPoint, child: impl Widget + 'static) -> Align {
Align {
align,
child: WidgetPod::new(child).boxed(),
width_factor: Some(1.0),
height_factor: None,
}
}
}
impl Widget for Align {
fn on_pointer_event(&mut self, ctx: &mut EventCtx, event: &PointerEvent) {
self.child.on_pointer_event(ctx, event);
}
fn on_text_event(&mut self, ctx: &mut EventCtx, event: &TextEvent) {
self.child.on_text_event(ctx, event);
}
fn on_access_event(&mut self, ctx: &mut EventCtx, event: &AccessEvent) {
self.child.on_access_event(ctx, event);
}
fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle) {
self.child.lifecycle(ctx, event);
}
fn on_status_change(&mut self, _ctx: &mut LifeCycleCtx, _event: &StatusChange) {}
fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints) -> Size {
let size = self.child.layout(ctx, &bc.loosen());
log_size_warnings(size);
let mut my_size = size;
if bc.is_width_bounded() {
my_size.width = bc.max().width;
}
if bc.is_height_bounded() {
my_size.height = bc.max().height;
}
if let Some(width) = self.width_factor {
my_size.width = size.width * width;
}
if let Some(height) = self.height_factor {
my_size.height = size.height * height;
}
my_size = bc.constrain(my_size);
let extra_width = (my_size.width - size.width).max(0.);
let extra_height = (my_size.height - size.height).max(0.);
let origin = self
.align
.resolve(Rect::new(0., 0., extra_width, extra_height))
.expand();
ctx.place_child(&mut self.child, origin);
let my_insets = self.child.compute_parent_paint_insets(my_size);
ctx.set_paint_insets(my_insets);
if self.height_factor.is_some() {
let baseline_offset = self.child.baseline_offset();
if baseline_offset > 0f64 {
ctx.set_baseline_offset(baseline_offset + extra_height / 2.0);
}
}
trace!(
"Computed layout: origin={}, size={}, insets={:?}",
origin,
my_size,
my_insets
);
my_size
}
fn paint(&mut self, ctx: &mut PaintCtx, scene: &mut Scene) {
self.child.paint(ctx, scene);
}
fn accessibility_role(&self) -> Role {
Role::GenericContainer
}
fn accessibility(&mut self, ctx: &mut AccessCtx) {
self.child.accessibility(ctx);
}
fn children(&self) -> SmallVec<[WidgetRef<'_, dyn Widget>; 16]> {
smallvec![self.child.as_dyn()]
}
fn make_trace_span(&self) -> Span {
trace_span!("Align")
}
}
fn log_size_warnings(size: Size) {
if size.width.is_infinite() {
tracing::warn!("Align widget's child has an infinite width.");
}
if size.height.is_infinite() {
tracing::warn!("Align widget's child has an infinite height.");
}
}
#[cfg(test)]
mod tests {
use insta::assert_debug_snapshot;
use super::*;
use crate::assert_render_snapshot;
use crate::testing::TestHarness;
use crate::widget::Label;
#[test]
fn centered() {
let widget = Align::centered(Label::new("hello"));
let mut harness = TestHarness::create(widget);
assert_debug_snapshot!(harness.root_widget());
assert_render_snapshot!(harness, "centered");
}
#[test]
fn right() {
let widget = Align::right(Label::new("hello"));
let mut harness = TestHarness::create(widget);
assert_debug_snapshot!(harness.root_widget());
assert_render_snapshot!(harness, "right");
}
#[test]
fn left() {
let widget = Align::left(Label::new("hello"));
let mut harness = TestHarness::create(widget);
assert_debug_snapshot!(harness.root_widget());
assert_render_snapshot!(harness, "left");
}
}