mgui-core 0.6.0

mgui 框架核心:Element/RenderObject/Widget 三树、布局与绘制协议、帧管线与响应式原语
Documentation
//! OverflowBox — 自身填满父约束,child 用自定义约束布局且允许溢出
//!
//! 语义(对齐 Flutter `RenderOverflowBox`):
//! - 自身尺寸 = 父可用最大空间(`constraints` 的有界最大;无界时退化用 child 尺寸兜底)
//! - child 用显式 `min/max` 约束布局(未设置的轴 = `0..∞`)
//! - 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;

/// OverflowBox RenderObject
pub struct OverflowBoxRenderObject {
    pub min_width: f32,
    pub max_width: f32,
    pub min_height: f32,
    pub max_height: f32,
    pub alignment: Alignment,
    child: Option<SharedRO>,
    cached_size: Size,
    child_cached_size: Size,
    child_offset: Point,
}

impl OverflowBoxRenderObject {
    pub fn new(
        min_width: f32,
        max_width: f32,
        min_height: f32,
        max_height: f32,
        alignment: Alignment,
        child: Option<Box<dyn RenderObject>>,
    ) -> Self {
        Self {
            min_width,
            max_width,
            min_height,
            max_height,
            alignment,
            child: child.map(make_ro_shared),
            cached_size: Size::ZERO,
            child_cached_size: Size::ZERO,
            child_offset: Point::ZERO,
        }
    }
}

impl RenderObject for OverflowBoxRenderObject {
    fn layout(&mut self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size {
        // 1. child 用显式约束布局(不继承父约束,故可溢出)
        let child_constraints = BoxConstraints {
            min_width: self.min_width,
            max_width: self.max_width,
            min_height: self.min_height,
            max_height: self.max_height,
        };
        self.child_cached_size =
            if let Some(child) = &mut self.child { child.layout_cached(child_constraints, ctx) } else { Size::ZERO };
        // 2. 自身尺寸 = 父最大空间;无界时退化用 child 尺寸(避免 Size(∞,∞))
        let biggest = Size::new(constraints.max_width, constraints.max_height);
        self.cached_size = if biggest.width.is_finite() && biggest.height.is_finite() {
            biggest
        } else {
            constraints.constrain(self.child_cached_size)
        };
        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
        }
    }
}

/// OverflowBox(child 用自定义约束,允许溢出)
#[derive(Debug)]
pub struct OverflowBoxWidget {
    pub min_width: f32,
    pub max_width: f32,
    pub min_height: f32,
    pub max_height: f32,
    pub alignment: Alignment,
    pub child: Option<WidgetNode>,
}

impl OverflowBoxWidget {
    /// 默认:child 用 `0..∞` 约束(完全自由)
    pub fn new(child: impl Widget) -> Self {
        Self {
            min_width: 0.0,
            max_width: f32::INFINITY,
            min_height: 0.0,
            max_height: f32::INFINITY,
            alignment: Alignment::CENTER,
            child: Some(WidgetNode::new(child)),
        }
    }

    pub fn min_w(mut self, w: f32) -> Self {
        self.min_width = w;
        self
    }
    pub fn max_w(mut self, w: f32) -> Self {
        self.max_width = w;
        self
    }
    pub fn min_h(mut self, h: f32) -> Self {
        self.min_height = h;
        self
    }
    pub fn max_h(mut self, h: f32) -> Self {
        self.max_height = h;
        self
    }
    pub fn align(mut self, alignment: Alignment) -> Self {
        self.alignment = alignment;
        self
    }
}

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

    fn is_equivalent(&self, old: &dyn Widget) -> bool {
        old.as_any().downcast_ref::<OverflowBoxWidget>().is_some_and(|o| {
            self.min_width == o.min_width
                && self.max_width == o.max_width
                && self.min_height == o.min_height
                && self.max_height == o.max_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(OverflowBoxRenderObject::new(
            self.min_width,
            self.max_width,
            self.min_height,
            self.max_height,
            self.alignment,
            Some(child_ro),
        ))
    }

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