mgui_core/render_object/
positioned.rs1use super::*;
2
3pub struct PositionedRenderObject {
11 pub offset: Point,
12 pub signal: Option<Signal<Point>>,
13 child: Option<SharedRO>,
14 cached_size: Size,
15 ro_subscriber_id: Option<crate::reactive::SubscriberId>,
17}
18
19impl PositionedRenderObject {
20 pub fn new(offset: Point, child: impl RenderObject + 'static) -> Self {
21 Self {
22 offset,
23 signal: None,
24 child: Some(make_ro_shared(Box::new(child))),
25 cached_size: Size::ZERO,
26 ro_subscriber_id: None,
27 }
28 }
29
30 pub fn new_dynamic(offset: Point, signal: Signal<Point>, child: impl RenderObject + 'static) -> Self {
32 Self {
33 offset,
34 signal: Some(signal),
35 child: Some(make_ro_shared(Box::new(child))),
36 cached_size: Size::ZERO,
37 ro_subscriber_id: None,
38 }
39 }
40
41 pub fn set_ro_subscriber_id(&mut self, id: crate::reactive::SubscriberId) {
45 self.ro_subscriber_id = Some(id);
46 }
47
48 fn current_offset(&self) -> Point {
51 match (&self.signal, self.ro_subscriber_id) {
52 (Some(sig), Some(sub_id)) => {
53 crate::reactive::enter_subscriber(sub_id);
54 let p = sig.get();
55 crate::reactive::exit_subscriber();
56 p
57 }
58 _ => self.offset,
59 }
60 }
61}
62
63impl RenderObject for PositionedRenderObject {
64 fn layout(&mut self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size {
65 let child_size =
67 if let Some(child) = &mut self.child { child.layout_cached(constraints, ctx) } else { Size::ZERO };
68 self.cached_size = constraints.constrain(child_size);
69 self.cached_size
70 }
71
72 fn content_size(&self) -> Size {
73 self.cached_size
74 }
75
76 fn paint(&self, offset: Point, painter: &mut PaintList) {
77 if let Some(child) = &self.child {
78 let off = self.current_offset();
79 child.borrow().paint(Point::new(offset.x + off.x, offset.y + off.y), painter);
80 }
81 }
82
83 fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
84 if let Some(child) = &self.child {
85 let off = self.current_offset();
86 return child.borrow().hit_test(position, Point::new(offset.x + off.x, offset.y + off.y));
87 }
88 None
89 }
90
91 fn propagate_element_ids(&mut self, child_ids: &[ElementId], stateful_ros: &mut [SharedRO]) {
92 if let Some(child) = &mut self.child {
93 if let Some(&cid) = child_ids.first() {
94 if let Some(new_ro) = stateful_ros.first_mut() {
96 *child = new_ro.clone();
97 }
98 child.borrow_mut().set_element_id(cid);
99 }
100 }
101 }
102
103 fn child_slots(&self) -> Vec<(Point, SharedRO)> {
105 let off = self.current_offset();
106 self.child.as_ref().map(|c| vec![(off, c.clone())]).unwrap_or_default()
107 }
108
109 fn for_each_child(&self, f: &mut dyn FnMut(Point, &SharedRO)) {
110 if let Some(c) = &self.child {
111 f(self.current_offset(), c);
112 }
113 }
114
115 fn push_child(&mut self, offset: Point, child: SharedRO) {
120 let _ = (offset, child);
123 }
124
125 fn children_count(&self) -> usize {
126 if self.child.is_some() {
127 1
128 } else {
129 0
130 }
131 }
132
133 fn set_subscriber_id(&mut self, id: crate::reactive::SubscriberId) {
137 self.set_ro_subscriber_id(id);
138 }
139
140 fn apply_state(&mut self, state: &mut dyn std::any::Any) -> StateApply {
147 let s = match state.downcast_ref::<crate::widget::animated::AnimatedOffsetState>() {
148 Some(s) => s,
149 None => return StateApply::Unsupported,
150 };
151 let next = s.presented_offset();
152 if (next.x - self.offset.x).abs() < f32::EPSILON && (next.y - self.offset.y).abs() < f32::EPSILON {
153 return StateApply::Unchanged;
154 }
155 self.offset = next;
156 StateApply::Updated
157 }
158}