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, Rect, WidgetEvent};
25
26#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
34pub struct EventContext {
35 handled: bool,
36}
37
38impl EventContext {
39 pub fn stop(&mut self) {
40 self.handled = true;
41 }
42
43 pub fn is_handled(&self) -> bool {
44 self.handled
45 }
46}
47
48pub use overlay_host::{OverlayAnchor, OverlayAware, OverlayEvent, OverlayFocus, OverlayIntent, OverlayRef};
51
52#[derive(Clone)]
61pub struct WidgetNode(pub Rc<dyn Widget>);
62
63impl WidgetNode {
64 pub fn new<W: Widget + 'static>(widget: W) -> Self {
65 Self(Rc::new(widget))
66 }
67
68 pub fn as_any(&self) -> &dyn Any {
69 self.0.as_any()
70 }
71
72 pub fn type_id(&self) -> std::any::TypeId {
77 self.0.as_any().type_id()
78 }
79
80 pub fn is<T: 'static>(&self) -> bool {
81 self.0.as_any().is::<T>()
82 }
83
84 pub fn ptr_eq(&self, other: &WidgetNode) -> bool {
86 Rc::ptr_eq(&self.0, &other.0)
87 }
88
89 pub fn is_equivalent(&self, old: &WidgetNode) -> bool {
92 if widget_eq_disabled() {
93 return false;
94 }
95 if self.0.is_stateful() || old.0.is_stateful() {
96 return false;
97 }
98 if self.0.is_repaint_boundary() || old.0.is_repaint_boundary() {
99 return false;
100 }
101 if self.0.key() != old.0.key() {
102 return false;
103 }
104 if self.0.dynamic_signal().is_some() || old.0.dynamic_signal().is_some() {
105 return false; }
107 self.0.can_update(&*old.0) && self.0.is_equivalent(&*old.0)
108 }
109}
110
111impl std::fmt::Debug for WidgetNode {
112 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
113 f.debug_struct("WidgetNode").field("type", &self.0.type_name()).finish()
114 }
115}
116
117#[allow(unused_variables)]
123#[deny(missing_docs)]
124pub trait Widget: Any + Debug + 'static {
125 fn as_any(&self) -> &dyn Any;
127
128 fn type_name(&self) -> &'static str {
130 std::any::type_name::<Self>()
131 }
132
133 fn is_stateful(&self) -> bool {
135 false
136 }
137
138 fn is_focusable(&self) -> bool {
140 false
141 }
142
143 fn wants_ime(&self) -> bool {
145 false
146 }
147
148 fn captures_tab(&self) -> bool {
151 false
152 }
153
154 fn create_state(&self) -> Option<Box<dyn State>> {
156 None
157 }
158
159 fn create_render_object(&self) -> Box<dyn RenderObject>;
161
162 fn children(&self) -> Vec<WidgetNode> {
164 Vec::new()
165 }
166
167 fn children_with_positions(&self) -> Vec<(Point, WidgetNode)> {
169 self.children().into_iter().map(|c| (Point::ZERO, c)).collect()
170 }
171
172 fn handle_event(&self, event: &WidgetEvent, local_pos: Point) {}
174
175 fn handle_event_ctx(&self, event: &WidgetEvent, local_pos: Point, _ctx: &mut EventContext) {
177 self.handle_event(event, local_pos);
178 }
179
180 fn accepts_scroll(&self) -> bool {
182 false
183 }
184
185 fn key(&self) -> Option<u64> {
187 None
188 }
189
190 fn gesture_state(&self) -> Option<GestureState> {
192 None
193 }
194
195 fn dynamic_signal(&self) -> Option<Signal<Point>> {
197 None
198 }
199
200 fn semantics(&self) -> Option<crate::semantics::WidgetSemantics> {
202 None
203 }
204
205 fn as_inherited(&self) -> Option<&dyn crate::inherited::InheritedWidget> {
207 None
208 }
209
210 fn can_update(&self, old: &dyn Widget) -> bool {
213 self.as_any().type_id() == old.as_any().type_id()
214 }
215
216 fn is_equivalent(&self, _old: &dyn Widget) -> bool {
222 false
223 }
224
225 fn is_repaint_boundary(&self) -> bool {
227 false
228 }
229
230 fn visible_children(&self) -> Option<Vec<bool>> {
237 None
238 }
239
240 fn parent_data(&self) -> Option<crate::layout::ParentData> {
243 None
244 }
245}
246
247impl Widget for WidgetNode {
249 fn as_any(&self) -> &dyn Any {
250 self.0.as_any()
251 }
252 fn type_name(&self) -> &'static str {
253 self.0.type_name()
254 }
255 fn is_stateful(&self) -> bool {
256 self.0.is_stateful()
257 }
258 fn is_focusable(&self) -> bool {
259 self.0.is_focusable()
260 }
261 fn wants_ime(&self) -> bool {
262 self.0.wants_ime()
263 }
264 fn captures_tab(&self) -> bool {
265 self.0.captures_tab()
266 }
267 fn create_state(&self) -> Option<Box<dyn State>> {
268 self.0.create_state()
269 }
270 fn create_render_object(&self) -> Box<dyn RenderObject> {
271 self.0.create_render_object()
272 }
273 fn children(&self) -> Vec<WidgetNode> {
274 self.0.children()
275 }
276 fn children_with_positions(&self) -> Vec<(Point, WidgetNode)> {
277 self.0.children_with_positions()
278 }
279 fn handle_event(&self, event: &WidgetEvent, local_pos: Point) {
280 self.0.handle_event(event, local_pos)
281 }
282 fn handle_event_ctx(&self, event: &WidgetEvent, local_pos: Point, ctx: &mut EventContext) {
283 self.0.handle_event_ctx(event, local_pos, ctx)
284 }
285 fn accepts_scroll(&self) -> bool {
286 self.0.accepts_scroll()
287 }
288 fn key(&self) -> Option<u64> {
289 self.0.key()
290 }
291 fn gesture_state(&self) -> Option<GestureState> {
292 self.0.gesture_state()
293 }
294 fn dynamic_signal(&self) -> Option<Signal<Point>> {
295 self.0.dynamic_signal()
296 }
297 fn semantics(&self) -> Option<crate::semantics::WidgetSemantics> {
298 self.0.semantics()
299 }
300 fn as_inherited(&self) -> Option<&dyn crate::inherited::InheritedWidget> {
301 self.0.as_inherited()
302 }
303 fn can_update(&self, old: &dyn Widget) -> bool {
304 self.0.can_update(old)
305 }
306 fn is_equivalent(&self, old: &dyn Widget) -> bool {
307 self.0.is_equivalent(old)
308 }
309 fn is_repaint_boundary(&self) -> bool {
310 self.0.is_repaint_boundary()
311 }
312 fn visible_children(&self) -> Option<Vec<bool>> {
313 self.0.visible_children()
314 }
315 fn parent_data(&self) -> Option<crate::layout::ParentData> {
316 self.0.parent_data()
317 }
318}
319
320pub trait StatefulWidget: Widget {}
326
327fn state_apply_disabled() -> bool {
329 static DISABLED: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
330 *DISABLED.get_or_init(|| std::env::var_os("MGUI_DISABLE_STATE_APPLY").is_some())
331}
332
333pub fn state_apply_enabled() -> bool {
338 !state_apply_disabled()
339}
340
341fn widget_eq_disabled() -> bool {
343 static DISABLED: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
344 *DISABLED.get_or_init(|| std::env::var_os("MGUI_DISABLE_WIDGET_EQ").is_some())
345}
346
347pub fn children_equivalent(a: &[WidgetNode], b: &[WidgetNode]) -> bool {
349 a.len() == b.len() && a.iter().zip(b.iter()).all(|(x, y)| x.is_equivalent(y))
350}
351
352pub fn optional_child_equivalent(a: &Option<WidgetNode>, b: &Option<WidgetNode>) -> bool {
354 match (a, b) {
355 (Some(a), Some(b)) => a.is_equivalent(b),
356 (None, None) => true,
357 _ => false,
358 }
359}
360
361#[allow(unused_variables)]
370#[deny(missing_docs)]
371pub trait State: Any {
372 fn as_any(&self) -> &dyn Any;
374
375 fn as_any_mut(&mut self) -> &mut dyn Any;
377
378 fn widget(&self) -> &WidgetNode;
380
381 fn set_widget(&mut self, widget: WidgetNode);
383
384 fn init_state(&mut self) {}
386
387 fn did_update_widget(&mut self) {}
389
390 fn dispose(&mut self) {}
392
393 fn build(&self, ctx: &mut crate::inherited::BuildContext<'_>) -> Vec<WidgetNode> {
395 let _ = ctx;
396 self.widget().children()
397 }
398
399 fn create_render_object(&self) -> Box<dyn RenderObject> {
402 self.widget().create_render_object()
403 }
404
405 fn apply_to_ro(&mut self, ro: &mut dyn RenderObject) -> crate::render_object::StateApply {
413 if state_apply_disabled() {
414 return crate::render_object::StateApply::Unsupported;
415 }
416 ro.apply_state(self.as_any_mut())
417 }
418
419 fn semantics(&self) -> Option<crate::semantics::WidgetSemantics> {
422 None
423 }
424
425 fn handle_event(&mut self, event: &WidgetEvent, local_pos: Point) {}
427
428 fn handle_event_ctx(&mut self, event: &WidgetEvent, local_pos: Point, _ctx: &mut EventContext) {
430 self.handle_event(event, local_pos);
431 }
432
433 fn scroll_into_view(&mut self, _target: Rect) -> bool {
438 false
439 }
440
441 fn tick(&mut self, _now_ms: u64) {}
443
444 fn handle_overlay_key(&mut self, _key: crate::KeyCode, _mods: crate::KeyModifiers) -> bool {
450 false
451 }
452
453 fn is_animating(&self) -> bool {
455 false
456 }
457
458 fn next_animation_delay(&self, _now_ms: u64) -> Option<u64> {
463 None
464 }
465
466 fn take_needs_build(&mut self) -> bool {
472 false
473 }
474
475 fn gesture_state(&self) -> Option<GestureState> {
477 None
478 }
479
480 fn reject_gestures(&mut self) {}
482
483 fn handles_build_error(&self) -> bool {
485 false
486 }
487
488 fn on_build_error(&mut self, msg: &str) {
490 let _ = msg;
491 }
492
493 fn ime_cursor_area(&self) -> Option<(f32, f32, f32)> {
495 None
496 }
497
498 fn as_overlay_aware(&mut self) -> Option<&mut dyn OverlayAware> {
503 None
504 }
505}
506
507pub mod overlay_host;
511pub mod primitives;
512
513pub use primitives::{RRectWidget, StackWidget};
517pub use overlay_host::AnchorPlacement as TooltipPlacement;