Skip to main content

mgui_core/render_object/
opacity.rs

1use super::*;
2
3/// 阶段 5:Opacity 容器 RO(整体 alpha 乘)
4pub struct OpacityRenderObject {
5    pub child: Option<SharedRO>,
6    pub element_id: Option<ElementId>,
7    pub size: Size,
8    /// 整体 alpha 乘(0.0 完全透明,1.0 不透明)
9    pub opacity: f32,
10}
11
12impl OpacityRenderObject {
13    pub fn new(child: Box<dyn RenderObject>, opacity: f32) -> Self {
14        Self {
15            child: Some(make_ro_shared(child)),
16            element_id: None,
17            size: Size::ZERO,
18            opacity: opacity.clamp(0.0, 1.0),
19        }
20    }
21    pub fn empty(opacity: f32) -> Self {
22        Self { child: None, element_id: None, size: Size::ZERO, opacity: opacity.clamp(0.0, 1.0) }
23    }
24}
25
26impl RenderObject for OpacityRenderObject {
27    fn layout(&mut self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size {
28        self.size = self.child.as_mut().map(|c| c.layout_cached(constraints, ctx)).unwrap_or(Size::ZERO);
29        self.size
30    }
31    fn paint(&self, _offset: Point, _painter: &mut PaintList) {
32        // layer 边界,paint noop
33    }
34    fn paint_as_layer_root(&self, offset: Point, painter: &mut PaintList) {
35        // 本层内容 = child 的绘制命令(见 trait 文档:非 boundary child 无自己的
36        // layer,必须画进本层,否则 cmds=0 整层不可见)
37        if let Some(c) = &self.child {
38            c.borrow().paint(offset, painter);
39        }
40    }
41    fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
42        // 阶段 5:opacity=0 完全透明 → 没有命中;否则透传到 child
43        if self.opacity <= 0.0 {
44            return None;
45        }
46        self.child.as_ref()?.borrow().hit_test(position, offset)
47    }
48    fn set_element_id(&mut self, _id: ElementId) {
49        // noop
50    }
51    /// 阶段 2-E:element_origin 遍历用 — 透传 child
52    fn child_slots(&self) -> Vec<(Point, SharedRO)> {
53        self.child.as_ref().map(|c| vec![(Point::ZERO, c.clone())]).unwrap_or_default()
54    }
55
56    fn for_each_child(&self, f: &mut dyn FnMut(Point, &SharedRO)) {
57        if let Some(c) = &self.child {
58            f(Point::ZERO, c);
59        }
60    }
61    fn propagate_element_ids(&mut self, child_ids: &[ElementId], stateful_ros: &mut [SharedRO]) {
62        if let (Some(child_ro), Some(&cid)) = (self.child.as_mut(), child_ids.first()) {
63            if let Some(new_ro) = stateful_ros.first_mut() {
64                *child_ro = new_ro.clone();
65            }
66            child_ro.borrow_mut().set_element_id(cid);
67        }
68    }
69    fn content_size(&self) -> Size {
70        self.size
71    }
72    fn layer_layout_size(&self) -> Option<Size> {
73        Some(self.size)
74    }
75    fn push_child(&mut self, _offset: Point, _child: SharedRO) {
76        // noop
77    }
78    fn children_count(&self) -> usize {
79        if self.child.is_some() {
80            1
81        } else {
82            0
83        }
84    }
85    fn layer_effect(&self) -> crate::paint::LayerEffect {
86        crate::paint::LayerEffect::Opacity(self.opacity)
87    }
88
89    /// 属性动画:从 `AnimatedOpacityState` 就地读当前呈现值写 opacity。
90    ///
91    /// state 内持有统一的 `SharedTrack`(曲线或弹簧),RO 不关心底层轨道类型。
92    /// 不重画 layer 内容、不 layout、不替换 RO —— 合成阶段 `layer_effect()` 每帧
93    /// 实时读新 opacity。幂等:值未变返回 `Unchanged`。
94    fn apply_state(&mut self, state: &mut dyn std::any::Any) -> StateApply {
95        let track_state = match state.downcast_ref::<crate::widget::animated::AnimatedOpacityState>() {
96            Some(s) => s,
97            None => return StateApply::Unsupported,
98        };
99        let next = track_state.presented_opacity().clamp(0.0, 1.0);
100        if (next - self.opacity).abs() < f32::EPSILON {
101            return StateApply::Unchanged;
102        }
103        self.opacity = next;
104        StateApply::Updated
105    }
106}