1use std::any::Any;
19use std::fmt::Debug;
20use std::rc::Rc;
21
22use crate::reactive::Signal;
23use crate::render_object::RenderObject;
24use crate::{GestureState, Point, WidgetEvent};
25
26pub use overlay_host::{OverlayAnchor, OverlayAware, OverlayEvent, OverlayFocus, OverlayIntent, OverlayRef};
29
30#[derive(Clone)]
39pub struct WidgetNode(pub Rc<dyn Widget>);
40
41impl WidgetNode {
42 pub fn new<W: Widget + 'static>(widget: W) -> Self {
43 Self(Rc::new(widget))
44 }
45
46 pub fn as_any(&self) -> &dyn Any {
47 self.0.as_any()
48 }
49
50 pub fn type_id(&self) -> std::any::TypeId {
55 self.0.as_any().type_id()
56 }
57
58 pub fn is<T: 'static>(&self) -> bool {
59 self.0.as_any().is::<T>()
60 }
61
62 pub fn ptr_eq(&self, other: &WidgetNode) -> bool {
64 Rc::ptr_eq(&self.0, &other.0)
65 }
66
67 pub fn is_equivalent(&self, old: &WidgetNode) -> bool {
70 if widget_eq_disabled() {
71 return false;
72 }
73 if self.0.is_stateful() || old.0.is_stateful() {
74 return false;
75 }
76 if self.0.is_repaint_boundary() || old.0.is_repaint_boundary() {
77 return false;
78 }
79 if self.0.key() != old.0.key() {
80 return false;
81 }
82 if self.0.dynamic_signal().is_some() || old.0.dynamic_signal().is_some() {
83 return false; }
85 self.0.can_update(&*old.0) && self.0.is_equivalent(&*old.0)
86 }
87}
88
89impl std::fmt::Debug for WidgetNode {
90 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
91 f.debug_struct("WidgetNode").field("type", &self.0.type_name()).finish()
92 }
93}
94
95#[allow(unused_variables)]
101#[deny(missing_docs)]
102pub trait Widget: Any + Debug + 'static {
103 fn as_any(&self) -> &dyn Any;
105
106 fn type_name(&self) -> &'static str {
108 std::any::type_name::<Self>()
109 }
110
111 fn is_stateful(&self) -> bool {
113 false
114 }
115
116 fn is_focusable(&self) -> bool {
118 false
119 }
120
121 fn wants_ime(&self) -> bool {
123 false
124 }
125
126 fn captures_tab(&self) -> bool {
129 false
130 }
131
132 fn create_state(&self) -> Option<Box<dyn State>> {
134 None
135 }
136
137 fn create_render_object(&self) -> Box<dyn RenderObject>;
139
140 fn children(&self) -> Vec<WidgetNode> {
142 Vec::new()
143 }
144
145 fn children_with_positions(&self) -> Vec<(Point, WidgetNode)> {
147 self.children().into_iter().map(|c| (Point::ZERO, c)).collect()
148 }
149
150 fn handle_event(&self, event: &WidgetEvent, local_pos: Point) {}
152
153 fn accepts_scroll(&self) -> bool {
155 false
156 }
157
158 fn key(&self) -> Option<u64> {
160 None
161 }
162
163 fn gesture_state(&self) -> Option<GestureState> {
165 None
166 }
167
168 fn dynamic_signal(&self) -> Option<Signal<Point>> {
170 None
171 }
172
173 fn semantics(&self) -> Option<crate::semantics::WidgetSemantics> {
175 None
176 }
177
178 fn as_inherited(&self) -> Option<&dyn crate::inherited::InheritedWidget> {
180 None
181 }
182
183 fn can_update(&self, old: &dyn Widget) -> bool {
186 self.as_any().type_id() == old.as_any().type_id()
187 }
188
189 fn is_equivalent(&self, _old: &dyn Widget) -> bool {
195 false
196 }
197
198 fn is_repaint_boundary(&self) -> bool {
200 false
201 }
202
203 fn visible_children(&self) -> Option<Vec<bool>> {
210 None
211 }
212
213 fn parent_data(&self) -> Option<crate::layout::ParentData> {
216 None
217 }
218}
219
220impl Widget for WidgetNode {
222 fn as_any(&self) -> &dyn Any {
223 self.0.as_any()
224 }
225 fn type_name(&self) -> &'static str {
226 self.0.type_name()
227 }
228 fn is_stateful(&self) -> bool {
229 self.0.is_stateful()
230 }
231 fn is_focusable(&self) -> bool {
232 self.0.is_focusable()
233 }
234 fn wants_ime(&self) -> bool {
235 self.0.wants_ime()
236 }
237 fn captures_tab(&self) -> bool {
238 self.0.captures_tab()
239 }
240 fn create_state(&self) -> Option<Box<dyn State>> {
241 self.0.create_state()
242 }
243 fn create_render_object(&self) -> Box<dyn RenderObject> {
244 self.0.create_render_object()
245 }
246 fn children(&self) -> Vec<WidgetNode> {
247 self.0.children()
248 }
249 fn children_with_positions(&self) -> Vec<(Point, WidgetNode)> {
250 self.0.children_with_positions()
251 }
252 fn handle_event(&self, event: &WidgetEvent, local_pos: Point) {
253 self.0.handle_event(event, local_pos)
254 }
255 fn accepts_scroll(&self) -> bool {
256 self.0.accepts_scroll()
257 }
258 fn key(&self) -> Option<u64> {
259 self.0.key()
260 }
261 fn gesture_state(&self) -> Option<GestureState> {
262 self.0.gesture_state()
263 }
264 fn dynamic_signal(&self) -> Option<Signal<Point>> {
265 self.0.dynamic_signal()
266 }
267 fn semantics(&self) -> Option<crate::semantics::WidgetSemantics> {
268 self.0.semantics()
269 }
270 fn as_inherited(&self) -> Option<&dyn crate::inherited::InheritedWidget> {
271 self.0.as_inherited()
272 }
273 fn can_update(&self, old: &dyn Widget) -> bool {
274 self.0.can_update(old)
275 }
276 fn is_equivalent(&self, old: &dyn Widget) -> bool {
277 self.0.is_equivalent(old)
278 }
279 fn is_repaint_boundary(&self) -> bool {
280 self.0.is_repaint_boundary()
281 }
282 fn visible_children(&self) -> Option<Vec<bool>> {
283 self.0.visible_children()
284 }
285 fn parent_data(&self) -> Option<crate::layout::ParentData> {
286 self.0.parent_data()
287 }
288}
289
290pub trait StatefulWidget: Widget {}
296
297fn state_apply_disabled() -> bool {
299 static DISABLED: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
300 *DISABLED.get_or_init(|| std::env::var_os("MGUI_DISABLE_STATE_APPLY").is_some())
301}
302
303pub fn state_apply_enabled() -> bool {
308 !state_apply_disabled()
309}
310
311fn widget_eq_disabled() -> bool {
313 static DISABLED: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
314 *DISABLED.get_or_init(|| std::env::var_os("MGUI_DISABLE_WIDGET_EQ").is_some())
315}
316
317pub fn children_equivalent(a: &[WidgetNode], b: &[WidgetNode]) -> bool {
319 a.len() == b.len() && a.iter().zip(b.iter()).all(|(x, y)| x.is_equivalent(y))
320}
321
322pub fn optional_child_equivalent(a: &Option<WidgetNode>, b: &Option<WidgetNode>) -> bool {
324 match (a, b) {
325 (Some(a), Some(b)) => a.is_equivalent(b),
326 (None, None) => true,
327 _ => false,
328 }
329}
330
331#[allow(unused_variables)]
340#[deny(missing_docs)]
341pub trait State: Any {
342 fn as_any(&self) -> &dyn Any;
344
345 fn as_any_mut(&mut self) -> &mut dyn Any;
347
348 fn widget(&self) -> &WidgetNode;
350
351 fn set_widget(&mut self, widget: WidgetNode);
353
354 fn init_state(&mut self) {}
356
357 fn did_update_widget(&mut self) {}
359
360 fn dispose(&mut self) {}
362
363 fn build(&self, ctx: &mut crate::inherited::BuildContext<'_>) -> Vec<WidgetNode> {
365 let _ = ctx;
366 self.widget().children()
367 }
368
369 fn create_render_object(&self) -> Box<dyn RenderObject> {
372 self.widget().create_render_object()
373 }
374
375 fn apply_to_ro(&mut self, ro: &mut dyn RenderObject) -> crate::render_object::StateApply {
383 if state_apply_disabled() {
384 return crate::render_object::StateApply::Unsupported;
385 }
386 ro.apply_state(self.as_any_mut())
387 }
388
389 fn semantics(&self) -> Option<crate::semantics::WidgetSemantics> {
392 None
393 }
394
395 fn handle_event(&mut self, event: &WidgetEvent, local_pos: Point) {}
397
398 fn tick(&mut self, _now_ms: u64) {}
400
401 fn handle_overlay_key(&mut self, _key: crate::KeyCode, _mods: crate::KeyModifiers) -> bool {
407 false
408 }
409
410 fn is_animating(&self) -> bool {
412 false
413 }
414
415 fn next_animation_delay(&self, _now_ms: u64) -> Option<u64> {
420 None
421 }
422
423 fn take_needs_build(&mut self) -> bool {
429 false
430 }
431
432 fn gesture_state(&self) -> Option<GestureState> {
434 None
435 }
436
437 fn reject_gestures(&mut self) {}
439
440 fn handles_build_error(&self) -> bool {
442 false
443 }
444
445 fn on_build_error(&mut self, msg: &str) {
447 let _ = msg;
448 }
449
450 fn ime_cursor_area(&self) -> Option<(f32, f32, f32)> {
452 None
453 }
454
455 fn as_overlay_aware(&mut self) -> Option<&mut dyn OverlayAware> {
460 None
461 }
462}
463
464pub mod overlay_host;
468pub mod primitives;
469
470pub use primitives::{RRectWidget, StackWidget};
474pub use overlay_host::AnchorPlacement as TooltipPlacement;