use crate::id::ElementId;
use crate::paint::PaintList;
use crate::reactive::Signal;
use crate::{Color, Point, Rect, Size};
use std::any::Any;
use std::cell::{Cell, Ref, RefCell, RefMut};
use std::rc::Rc;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum StateApply {
Unsupported,
Unchanged,
Updated,
}
struct RoCell {
ro: RefCell<Box<dyn RenderObject>>,
layout_dirty: Cell<bool>,
layout_cache: Cell<Option<(BoxConstraints, Size)>>,
}
#[derive(Clone)]
pub struct SharedRO(Rc<RoCell>);
impl SharedRO {
pub fn borrow(&self) -> Ref<'_, Box<dyn RenderObject>> {
self.0.ro.borrow()
}
pub fn borrow_mut(&self) -> RefMut<'_, Box<dyn RenderObject>> {
self.0.ro.borrow_mut()
}
pub fn ptr_eq(&self, other: &SharedRO) -> bool {
Rc::ptr_eq(&self.0, &other.0)
}
pub fn mark_layout_dirty(&self) {
self.0.layout_dirty.set(true);
}
pub fn is_layout_dirty(&self) -> bool {
self.0.layout_dirty.get()
}
pub fn layout_cached(&self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size {
if !self.0.layout_dirty.get() {
if let Some((cached_constraints, size)) = self.0.layout_cache.get() {
if cached_constraints == constraints {
return size;
}
}
}
let size = self.0.ro.borrow_mut().layout(constraints, ctx);
self.0.layout_dirty.set(false);
self.0.layout_cache.set(Some((constraints, size)));
size
}
}
pub fn make_ro_shared(ro: Box<dyn RenderObject>) -> SharedRO {
SharedRO(Rc::new(RoCell { ro: RefCell::new(ro), layout_dirty: Cell::new(true), layout_cache: Cell::new(None) }))
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BoxConstraints {
pub min_width: f32,
pub max_width: f32,
pub min_height: f32,
pub max_height: f32,
}
impl BoxConstraints {
pub const UNBOUNDED: Self =
Self { min_width: 0.0, max_width: f32::INFINITY, min_height: 0.0, max_height: f32::INFINITY };
pub fn tight(size: Size) -> Self {
Self { min_width: size.width, max_width: size.width, min_height: size.height, max_height: size.height }
}
pub fn loose(size: Size) -> Self {
Self { min_width: 0.0, max_width: size.width, min_height: 0.0, max_height: size.height }
}
pub fn constrain(&self, size: Size) -> Size {
Size::new(size.width.clamp(self.min_width, self.max_width), size.height.clamp(self.min_height, self.max_height))
}
pub fn inset_uniform(&self, inset: f32) -> Self {
Self {
min_width: 0.0,
max_width: (self.max_width - inset * 2.0).max(0.0),
min_height: 0.0,
max_height: (self.max_height - inset * 2.0).max(0.0),
}
}
pub fn is_tight(&self) -> bool {
self.min_width == self.max_width && self.min_height == self.max_height
}
pub fn is_bounded_width(&self) -> bool {
self.max_width.is_finite()
}
pub fn is_bounded_height(&self) -> bool {
self.max_height.is_finite()
}
pub fn tighten(&self, width: f32, height: f32) -> Self {
let w = width.clamp(self.min_width, self.max_width);
let h = height.clamp(self.min_height, self.max_height);
Self::tight(Size::new(w, h))
}
pub fn loosen(&self) -> Self {
Self { min_width: 0.0, max_width: self.max_width, min_height: 0.0, max_height: self.max_height }
}
}
impl Default for BoxConstraints {
fn default() -> Self {
Self::UNBOUNDED
}
}
#[derive(Default)]
pub struct LayoutContext;
#[deny(missing_docs)]
pub trait RenderObject {
fn layout(&mut self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size;
fn paint(&self, offset: Point, painter: &mut PaintList);
fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
let _ = (position, offset);
None
}
fn set_element_id(&mut self, _id: ElementId) {}
fn propagate_element_ids(&mut self, _child_ids: &[ElementId], _stateful_ros: &mut [SharedRO]) {}
fn content_size(&self) -> Size {
Size::ZERO
}
fn content_size_opt(&self) -> Option<Size> {
let size = self.content_size();
(size.width.is_finite() && size.height.is_finite() && size.width > 0.0 && size.height > 0.0).then_some(size)
}
fn layer_size_hint(&self) -> Option<Size> {
self.content_size_opt().or_else(|| {
self.layer_layout_size()
.filter(|s| s.width.is_finite() && s.height.is_finite() && s.width > 0.0 && s.height > 0.0)
})
}
fn layer_layout_size(&self) -> Option<Size> {
None
}
fn layer_relayout_constraints(&self, layout_size: Size) -> BoxConstraints {
BoxConstraints::loose(layout_size)
}
fn paint_as_layer_root(&self, offset: Point, painter: &mut PaintList) {
self.paint(offset, painter);
}
fn push_child(&mut self, _offset: Point, _child: SharedRO) {
}
fn children_count(&self) -> usize {
0
}
fn layer_effect(&self) -> crate::paint::LayerEffect {
crate::paint::LayerEffect::None
}
fn element_id(&self) -> Option<ElementId> {
None
}
fn set_subscriber_id(&mut self, _id: crate::reactive::SubscriberId) {}
fn child_slots(&self) -> Vec<(Point, SharedRO)> {
Vec::new()
}
fn for_each_child(&self, f: &mut dyn FnMut(Point, &SharedRO)) {
for (offset, child) in self.child_slots() {
f(offset, &child);
}
}
fn apply_state(&mut self, _state: &mut dyn Any) -> StateApply {
StateApply::Unsupported
}
}
impl<T: RenderObject + ?Sized> RenderObject for Box<T> {
fn layout(&mut self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size {
(**self).layout(constraints, ctx)
}
fn paint(&self, offset: Point, painter: &mut PaintList) {
(**self).paint(offset, painter);
}
fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
(**self).hit_test(position, offset)
}
fn set_element_id(&mut self, id: ElementId) {
(**self).set_element_id(id);
}
fn propagate_element_ids(&mut self, child_ids: &[ElementId], stateful_ros: &mut [SharedRO]) {
(**self).propagate_element_ids(child_ids, stateful_ros);
}
fn content_size(&self) -> Size {
(**self).content_size()
}
fn content_size_opt(&self) -> Option<Size> {
(**self).content_size_opt()
}
fn layer_size_hint(&self) -> Option<Size> {
(**self).layer_size_hint()
}
fn layer_layout_size(&self) -> Option<Size> {
(**self).layer_layout_size()
}
fn layer_relayout_constraints(&self, layout_size: Size) -> BoxConstraints {
(**self).layer_relayout_constraints(layout_size)
}
fn paint_as_layer_root(&self, offset: Point, painter: &mut PaintList) {
(**self).paint_as_layer_root(offset, painter)
}
fn layer_effect(&self) -> crate::paint::LayerEffect {
(**self).layer_effect()
}
fn set_subscriber_id(&mut self, id: crate::reactive::SubscriberId) {
(**self).set_subscriber_id(id);
}
fn element_id(&self) -> Option<ElementId> {
(**self).element_id()
}
fn child_slots(&self) -> Vec<(Point, SharedRO)> {
(**self).child_slots()
}
fn for_each_child(&self, f: &mut dyn FnMut(Point, &SharedRO)) {
(**self).for_each_child(f)
}
fn apply_state(&mut self, state: &mut dyn Any) -> StateApply {
(**self).apply_state(state)
}
}
pub fn point_in_rrect(p: Point, rect_origin: Point, size: Size, radius: f32) -> bool {
if size.width <= 0.0 || size.height <= 0.0 {
return false;
}
let r = radius.max(0.0);
let left = rect_origin.x;
let top = rect_origin.y;
let right = left + size.width;
let bottom = top + size.height;
if p.x < left || p.x > right || p.y < top || p.y > bottom {
return false;
}
let r = r.min(size.width * 0.5).min(size.height * 0.5);
if r <= 0.0 {
return true; }
let c_tl = Point::new(left + r, top + r);
let c_tr = Point::new(right - r, top + r);
let c_bl = Point::new(left + r, bottom - r);
let c_br = Point::new(right - r, bottom - r);
if p.x < c_tl.x && p.y < c_tl.y {
return dist_sq(p, c_tl) <= r * r;
}
if p.x > c_tr.x && p.y < c_tr.y {
return dist_sq(p, c_tr) <= r * r;
}
if p.x < c_bl.x && p.y > c_bl.y {
return dist_sq(p, c_bl) <= r * r;
}
if p.x > c_br.x && p.y > c_br.y {
return dist_sq(p, c_br) <= r * r;
}
true
}
fn dist_sq(a: Point, b: Point) -> f32 {
let dx = a.x - b.x;
let dy = a.y - b.y;
dx * dx + dy * dy
}
pub fn inverse_apply_transform(world_p: Point, t: crate::Transform2D, cx: f32, cy: f32) -> Point {
let px = world_p.x - cx - t.translate.x;
let py = world_p.y - cy - t.translate.y;
let c = (-t.rotation).cos();
let sn = (-t.rotation).sin();
let rx = px * c - py * sn;
let ry = px * sn + py * c;
let sx = if t.scale_x.abs() < 1e-6 { 1.0 } else { t.scale_x };
let sy = if t.scale_y.abs() < 1e-6 { 1.0 } else { t.scale_y };
Point::new(rx / sx + cx, ry / sy + cy)
}
struct ChildSlot {
child: SharedRO,
cached_size: Size,
}
pub mod border;
pub mod clip_rrect;
pub mod column;
pub mod layer_proxy;
pub mod margin;
pub mod opacity;
pub mod padding;
pub mod positioned;
pub mod proxy;
pub mod rrect;
pub mod stack;
pub mod transform;
pub use border::{Border, BorderRenderObject};
pub use clip_rrect::ClipRRectRenderObject;
pub use column::ColumnRenderObject;
pub use layer_proxy::LayerProxyRenderObject;
pub use margin::MarginRenderObject;
pub use opacity::OpacityRenderObject;
pub use padding::EdgeInsets;
pub use padding::PaddingRenderObject;
pub use positioned::PositionedRenderObject;
pub use proxy::ProxyRenderObject;
pub use rrect::RRectRenderObject;
pub use stack::StackRenderObject;
pub use transform::TransformRenderObject;