mgui-core 0.6.0

mgui 框架核心:Element/RenderObject/Widget 三树、布局与绘制协议、帧管线与响应式原语
Documentation
//! SizedOverflowBox — 自身固定尺寸,child 在其内按 alignment 摆位且**允许溢出**
//!
//! 语义(对齐 Flutter `RenderSizedOverflowBox`):
//! - 自身尺寸 = `constraints.constrain(Size(width, height))`
//! - child 用父约束的松约束(`constraints.loosen()`)布局 → 可大于自身尺寸
//! - child 按 `alignment` 摆位,差值可为负 → 溢出(不裁剪)

use crate::id::ElementId;
use crate::paint::PaintList;
use crate::render_object::{make_ro_shared, BoxConstraints, LayoutContext, RRectRenderObject, RenderObject, SharedRO};
use crate::widget::{Widget, WidgetNode};
use crate::{Color, Point, Size};

use super::alignment::Alignment;

/// SizedOverflowBox RenderObject
pub struct SizedOverflowBoxRenderObject {
    pub width: f32,
    pub height: f32,
    pub alignment: Alignment,
    child: Option<SharedRO>,
    cached_size: Size,
    child_cached_size: Size,
    child_offset: Point,
}

impl SizedOverflowBoxRenderObject {
    pub fn new(width: f32, height: f32, alignment: Alignment, child: Option<Box<dyn RenderObject>>) -> Self {
        Self {
            width,
            height,
            alignment,
            child: child.map(make_ro_shared),
            cached_size: Size::ZERO,
            child_cached_size: Size::ZERO,
            child_offset: Point::ZERO,
        }
    }
}

impl RenderObject for SizedOverflowBoxRenderObject {
    fn layout(&mut self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size {
        self.child_cached_size =
            if let Some(child) = &mut self.child { child.layout_cached(constraints.loosen(), ctx) } else { Size::ZERO };
        self.cached_size = constraints.constrain(Size::new(self.width, self.height));
        let delta_w = self.cached_size.width - self.child_cached_size.width;
        let delta_h = self.cached_size.height - self.child_cached_size.height;
        self.child_offset = Point::new(self.alignment.x * delta_w, self.alignment.y * delta_h);
        self.cached_size
    }

    fn content_size(&self) -> Size {
        self.cached_size
    }

    fn paint(&self, offset: Point, painter: &mut PaintList) {
        if let Some(child) = &self.child {
            child.borrow().paint(Point::new(offset.x + self.child_offset.x, offset.y + self.child_offset.y), painter);
        }
    }

    fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
        self.child.as_ref().and_then(|c| {
            c.borrow().hit_test(position, Point::new(offset.x + self.child_offset.x, offset.y + self.child_offset.y))
        })
    }

    fn propagate_element_ids(&mut self, child_ids: &[ElementId], stateful_ros: &mut [SharedRO]) {
        if let Some(child) = &mut self.child {
            if let Some(&cid) = 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![(self.child_offset, c.clone())]).unwrap_or_default()
    }

    fn for_each_child(&self, f: &mut dyn FnMut(Point, &SharedRO)) {
        if let Some(c) = &self.child {
            f(self.child_offset, c);
        }
    }

    fn children_count(&self) -> usize {
        if self.child.is_some() {
            1
        } else {
            0
        }
    }
}

/// SizedOverflowBox(固定尺寸容器,child 可溢出)
#[derive(Debug)]
pub struct SizedOverflowBoxWidget {
    pub width: f32,
    pub height: f32,
    pub alignment: Alignment,
    pub child: Option<WidgetNode>,
}

impl SizedOverflowBoxWidget {
    pub fn new(width: f32, height: f32, child: impl Widget) -> Self {
        Self { width, height, alignment: Alignment::CENTER, child: Some(WidgetNode::new(child)) }
    }

    pub fn align(mut self, alignment: Alignment) -> Self {
        self.alignment = alignment;
        self
    }
}

impl Widget for SizedOverflowBoxWidget {
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn is_equivalent(&self, old: &dyn Widget) -> bool {
        old.as_any().downcast_ref::<SizedOverflowBoxWidget>().is_some_and(|o| {
            self.width == o.width
                && self.height == o.height
                && self.alignment == o.alignment
                && crate::widget::optional_child_equivalent(&self.child, &o.child)
        })
    }

    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(SizedOverflowBoxRenderObject::new(self.width, self.height, self.alignment, Some(child_ro)))
    }

    fn children(&self) -> Vec<WidgetNode> {
        match &self.child {
            Some(c) => vec![c.clone()],
            None => vec![],
        }
    }
}