use std::f32::consts::FRAC_PI_2;
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::{Color, Point, Size, Transform2D};
pub struct RotatedBoxRenderObject {
pub quarter_turns: i32,
child: Option<SharedRO>,
cached_size: Size,
child_size: Size,
pub transform: Transform2D,
}
impl RotatedBoxRenderObject {
pub fn new(quarter_turns: i32, child: Option<Box<dyn RenderObject>>) -> Self {
Self {
quarter_turns,
child: child.map(make_ro_shared),
cached_size: Size::ZERO,
child_size: Size::ZERO,
transform: Transform2D::IDENTITY,
}
}
}
impl RenderObject for RotatedBoxRenderObject {
fn layout(&mut self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size {
let odd = self.quarter_turns.rem_euclid(2) == 1;
let child_constraints = if odd {
BoxConstraints {
min_width: constraints.min_height,
max_width: constraints.max_height,
min_height: constraints.min_width,
max_height: constraints.max_width,
}
} else {
constraints
};
self.child_size =
if let Some(child) = &mut self.child { child.layout_cached(child_constraints, ctx) } else { Size::ZERO };
let candidate = if odd { Size::new(self.child_size.height, self.child_size.width) } else { self.child_size };
let bounded = constraints.max_width.is_finite() || constraints.max_height.is_finite();
if bounded {
self.cached_size = constraints.constrain(candidate);
} else if self.cached_size.width <= 0.0 || self.cached_size.height <= 0.0 {
self.cached_size = candidate;
}
let rotation = (self.quarter_turns.rem_euclid(4) as f32) * FRAC_PI_2;
self.transform = Transform2D {
rotation,
scale_x: 1.0,
scale_y: 1.0,
translate: Point::new(
(self.cached_size.width - self.child_size.width) * 0.5,
(self.cached_size.height - self.child_size.height) * 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_effect(&self) -> LayerEffect {
LayerEffect::Transform(self.transform)
}
}
#[derive(Debug)]
pub struct RotatedBoxWidget {
pub quarter_turns: i32,
pub child: Option<WidgetNode>,
}
impl RotatedBoxWidget {
pub fn new(quarter_turns: i32, child: impl Widget) -> Self {
Self { quarter_turns, child: Some(WidgetNode::new(child)) }
}
pub fn quarter_turns(mut self, turns: i32) -> Self {
self.quarter_turns = turns;
self
}
}
impl Widget for RotatedBoxWidget {
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(RotatedBoxRenderObject::new(self.quarter_turns, Some(child_ro)))
}
fn children(&self) -> Vec<WidgetNode> {
match &self.child {
Some(c) => vec![c.clone()],
None => vec![],
}
}
}