use crate::id::ElementId;
use crate::paint::{LayerEffect, PaintList};
use crate::render_object::{
inverse_apply_transform, make_ro_shared, BoxConstraints, LayoutContext, RRectRenderObject, RenderObject, SharedRO,
};
use crate::widget::{Widget, WidgetNode};
use crate::{BoxFit, Color, Point, Size, Transform2D};
use super::alignment::Alignment;
pub struct FittedBoxRenderObject {
pub fit: BoxFit,
pub alignment: Alignment,
child: Option<SharedRO>,
cached_size: Size,
child_size: Size,
pub transform: Transform2D,
}
impl FittedBoxRenderObject {
pub fn new(fit: BoxFit, alignment: Alignment, child: Option<Box<dyn RenderObject>>) -> Self {
Self {
fit,
alignment,
child: child.map(make_ro_shared),
cached_size: Size::ZERO,
child_size: Size::ZERO,
transform: Transform2D::IDENTITY,
}
}
}
impl RenderObject for FittedBoxRenderObject {
fn layout(&mut self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size {
self.child_size = if let Some(child) = &mut self.child {
child.layout_cached(
BoxConstraints { min_width: 0.0, max_width: f32::INFINITY, min_height: 0.0, max_height: f32::INFINITY },
ctx,
)
} else {
Size::ZERO
};
let bounded = constraints.max_width.is_finite() || constraints.max_height.is_finite();
if bounded {
self.cached_size = constraints.constrain(self.child_size);
} else if self.cached_size.width <= 0.0 || self.cached_size.height <= 0.0 {
self.cached_size = self.child_size;
}
let cw = self.child_size.width;
let ch = self.child_size.height;
let bw = self.cached_size.width;
let bh = self.cached_size.height;
let (sx, sy) = if cw > 0.0 && ch > 0.0 && bw > 0.0 && bh > 0.0 {
match self.fit {
BoxFit::Fill => (bw / cw, bh / ch),
BoxFit::Contain => {
let s = (bw / cw).min(bh / ch);
(s, s)
}
}
} else {
(1.0, 1.0)
};
let qx = (bw - cw * sx) * self.alignment.x;
let qy = (bh - ch * sy) * self.alignment.y;
self.transform = Transform2D {
rotation: 0.0,
scale_x: sx,
scale_y: sy,
translate: Point::new(qx + (sx - 1.0) * cw * 0.5, qy + (sy - 1.0) * ch * 0.5),
};
self.cached_size
}
fn paint(&self, _offset: Point, _painter: &mut PaintList) {
}
fn paint_as_layer_root(&self, offset: Point, painter: &mut PaintList) {
if let Some(c) = &self.child {
c.borrow().paint(offset, painter);
}
}
fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
let child = self.child.as_ref()?;
let s = self.child_size;
if s.width <= 0.0 || s.height <= 0.0 {
return child.borrow().hit_test(position, offset);
}
let cx = offset.x + s.width * 0.5;
let cy = offset.y + s.height * 0.5;
let local = inverse_apply_transform(position, self.transform, cx, cy);
child.borrow().hit_test(local, offset)
}
fn propagate_element_ids(&mut self, child_ids: &[ElementId], stateful_ros: &mut [SharedRO]) {
if let (Some(child), Some(&cid)) = (self.child.as_mut(), child_ids.first()) {
if let Some(new_ro) = stateful_ros.first_mut() {
*child = new_ro.clone();
}
child.borrow_mut().set_element_id(cid);
}
}
fn child_slots(&self) -> Vec<(Point, SharedRO)> {
self.child.as_ref().map(|c| vec![(Point::ZERO, c.clone())]).unwrap_or_default()
}
fn for_each_child(&self, f: &mut dyn FnMut(Point, &SharedRO)) {
if let Some(c) = &self.child {
f(Point::ZERO, c);
}
}
fn children_count(&self) -> usize {
if self.child.is_some() {
1
} else {
0
}
}
fn content_size(&self) -> Size {
self.child_size
}
fn layer_layout_size(&self) -> Option<Size> {
Some(self.cached_size)
}
fn layer_relayout_constraints(&self, layout_size: Size) -> BoxConstraints {
BoxConstraints::tight(layout_size)
}
fn layer_effect(&self) -> LayerEffect {
LayerEffect::Transform(self.transform)
}
}
#[derive(Debug)]
pub struct FittedBoxWidget {
pub fit: BoxFit,
pub alignment: Alignment,
pub child: Option<WidgetNode>,
}
impl FittedBoxWidget {
pub fn new(child: impl Widget, fit: BoxFit) -> Self {
Self { fit, alignment: Alignment::CENTER, child: Some(WidgetNode::new(child)) }
}
pub fn contain(child: impl Widget) -> Self {
Self::new(child, BoxFit::Contain)
}
pub fn fill(child: impl Widget) -> Self {
Self::new(child, BoxFit::Fill)
}
pub fn align(mut self, alignment: Alignment) -> Self {
self.alignment = alignment;
self
}
}
impl Widget for FittedBoxWidget {
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn is_repaint_boundary(&self) -> bool {
true
}
fn create_render_object(&self) -> Box<dyn RenderObject> {
let child_ro = match &self.child {
Some(c) => c.0.create_render_object(),
None => Box::new(RRectRenderObject::new(Size::ZERO, Color::TRANSPARENT, 0.0)),
};
Box::new(FittedBoxRenderObject::new(self.fit, self.alignment, Some(child_ro)))
}
fn children(&self) -> Vec<WidgetNode> {
match &self.child {
Some(c) => vec![c.clone()],
None => vec![],
}
}
}