Skip to main content

mgui_core/layout/
positioned.rs

1//! Positioned —— 在父容器内按 offset 摆放的单 child 容器。
2//!
3//! 阶段 5-G 扩展:`signal` 为 `Some` 时位置由外部 `Signal<Point>` 驱动。
4//! - ElementTree mount 时让该 element 订阅 signal
5//! - `Signal::set(...)` → subscriber dirty → drain_signal_dirty → layer dirty → paint 重跑
6//!
7//! 这是"外部状态驱动子元素位置"的标准通道,适合手势拖动等高频改位置但
8//! 不想 rebuild 整树的场景。
9
10use crate::reactive::Signal;
11use crate::render_object::{PositionedRenderObject, RRectRenderObject, RenderObject};
12use crate::widget::{Widget, WidgetNode};
13use crate::{Color, Point, Size};
14
15/// Positioned(在父容器内按 offset 摆放)
16#[derive(Debug)]
17pub struct PositionedWidget {
18    pub offset: Point,
19    pub signal: Option<Signal<Point>>,
20    pub child: Option<WidgetNode>,
21}
22
23impl PositionedWidget {
24    pub fn new(offset: Point, child: impl Widget) -> Self {
25        Self { offset, signal: None, child: Some(WidgetNode::new(child)) }
26    }
27
28    /// 阶段 5-G:动态位置。**Signal 所有权转给 widget**;调用方需 `signal.clone()`
29    /// 保留一份用于外部修改(`Signal::set` / `Signal::update`)。
30    pub fn dynamic(signal: Signal<Point>, child: impl Widget) -> Self {
31        let initial = signal.peek();
32        Self { offset: initial, signal: Some(signal), child: Some(WidgetNode::new(child)) }
33    }
34}
35
36impl Widget for PositionedWidget {
37    fn as_any(&self) -> &dyn std::any::Any {
38        self
39    }
40
41    /// 阶段 5-G:ElementTree 用此钩子建立 signal → element 订阅关系
42    fn dynamic_signal(&self) -> Option<Signal<Point>> {
43        self.signal.clone()
44    }
45
46    fn create_render_object(&self) -> Box<dyn RenderObject> {
47        let child_ro: Box<dyn RenderObject> = match &self.child {
48            Some(c) => c.0.create_render_object(),
49            None => Box::new(RRectRenderObject::new(Size::ZERO, Color::TRANSPARENT, 0.0)),
50        };
51        match &self.signal {
52            Some(sig) => Box::new(PositionedRenderObject::new_dynamic(self.offset, sig.clone(), child_ro)),
53            None => Box::new(PositionedRenderObject::new(self.offset, child_ro)),
54        }
55    }
56
57    fn children(&self) -> Vec<WidgetNode> {
58        match &self.child {
59            Some(c) => vec![c.clone()],
60            None => vec![],
61        }
62    }
63
64    /// Element 树 mount/update 走此方法累计 child offset(决定 boundary 子节点的
65    /// layer offset 与 layout_offset);缺省 (0,0) 会让子层效果锚点错位。
66    fn children_with_positions(&self) -> Vec<(Point, WidgetNode)> {
67        let pos = match &self.signal {
68            Some(sig) => sig.peek(),
69            None => self.offset,
70        };
71        match &self.child {
72            Some(c) => vec![(pos, c.clone())],
73            None => vec![],
74        }
75    }
76}
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81    use crate::RRectWidget;
82
83    #[test]
84    fn children_with_positions_reports_offset() {
85        let w =
86            PositionedWidget::new(Point::new(12.0, 8.0), RRectWidget::new(Size::new(10.0, 10.0), Color::WHITE, 0.0));
87        let slots = w.children_with_positions();
88        assert_eq!(slots.len(), 1);
89        assert_eq!(slots[0].0, Point::new(12.0, 8.0));
90    }
91}