1use crate::prelude::*;
2use std::any::TypeId;
3
4pub(crate) struct ModalModel {
5 pub tooltip_visible: Signal<(bool, bool)>,
6 pub menu_visible: Signal<bool>,
7}
8
9pub enum ModalEvent {
11 ShowTooltip,
13 HideTooltip,
15 ShowMenu,
17 HideMenu,
19}
20
21impl Model for ModalModel {
22 fn event(&mut self, _cx: &mut EventContext, event: &mut Event) {
23 event.map(|modal_event, _| match modal_event {
24 ModalEvent::ShowTooltip => {
25 self.tooltip_visible.set((true, true));
26 }
27
28 ModalEvent::HideTooltip => {
29 self.tooltip_visible.set((false, true));
30 }
31
32 ModalEvent::ShowMenu => {
33 self.menu_visible.set(true);
34 }
35
36 ModalEvent::HideMenu => {
37 self.menu_visible.set(false);
38 }
39 });
40
41 event.map(|window_event, _| match window_event {
42 WindowEvent::MouseDown(MouseButton::Right) => {
43 self.menu_visible.set(true);
44 }
45 WindowEvent::MouseOver => {
46 if !self.tooltip_visible.get().0 {
47 self.tooltip_visible.set((true, true));
48 }
49 }
50 WindowEvent::MouseOut => self.tooltip_visible.set((false, true)),
51 WindowEvent::FocusIn => {
52 if !self.tooltip_visible.get().0 {
53 self.tooltip_visible.set((true, false));
54 }
55 }
56 WindowEvent::FocusOut => self.tooltip_visible.set((false, false)),
57 WindowEvent::FocusVisibility(vis) if !(*vis) => {
58 self.tooltip_visible.set((false, false))
59 }
60 WindowEvent::KeyDown(code, _) if *code == Code::Escape => {
61 self.tooltip_visible.set((false, false));
62 self.menu_visible.set(false);
63 }
64 WindowEvent::PressDown { mouse: _ } => self.tooltip_visible.set((false, true)),
65 _ => {}
66 });
67 }
68}
69
70pub(crate) struct ActionsModel {
71 pub(crate) on_press: Option<Box<dyn Fn(&mut EventContext) + Send + Sync>>,
72 pub(crate) on_press_down: Option<Box<dyn Fn(&mut EventContext) + Send + Sync>>,
73 pub(crate) on_double_click: Option<Box<dyn Fn(&mut EventContext, MouseButton) + Send + Sync>>,
74 pub(crate) on_hover: Option<Box<dyn Fn(&mut EventContext) + Send + Sync>>,
75 pub(crate) on_hover_out: Option<Box<dyn Fn(&mut EventContext) + Send + Sync>>,
76 pub(crate) on_over: Option<Box<dyn Fn(&mut EventContext) + Send + Sync>>,
77 pub(crate) on_over_out: Option<Box<dyn Fn(&mut EventContext) + Send + Sync>>,
78 pub(crate) on_mouse_move: Option<Box<dyn Fn(&mut EventContext, f32, f32) + Send + Sync>>,
79 pub(crate) on_mouse_down: Option<Box<dyn Fn(&mut EventContext, MouseButton) + Send + Sync>>,
80 pub(crate) on_mouse_up: Option<Box<dyn Fn(&mut EventContext, MouseButton) + Send + Sync>>,
81 pub(crate) on_focus_in: Option<Box<dyn Fn(&mut EventContext) + Send + Sync>>,
82 pub(crate) on_focus_out: Option<Box<dyn Fn(&mut EventContext) + Send + Sync>>,
83 pub(crate) on_geo_changed: Option<Box<dyn Fn(&mut EventContext, GeoChanged) + Send + Sync>>,
84}
85
86impl ActionsModel {
87 pub(crate) fn new() -> Self {
88 Self {
89 on_press: None,
90 on_press_down: None,
91 on_double_click: None,
92 on_hover: None,
93 on_hover_out: None,
94 on_over: None,
95 on_over_out: None,
96 on_mouse_move: None,
97 on_mouse_down: None,
98 on_mouse_up: None,
99 on_focus_in: None,
100 on_focus_out: None,
101 on_geo_changed: None,
102 }
103 }
104}
105
106impl Model for ActionsModel {
107 fn event(&mut self, cx: &mut EventContext, event: &mut Event) {
108 event.take(|actions_event, _| match actions_event {
109 ActionsEvent::OnPress(on_press) => {
110 self.on_press = Some(on_press);
111 }
112
113 ActionsEvent::OnPressDown(on_press_down) => {
114 self.on_press_down = Some(on_press_down);
115 }
116
117 ActionsEvent::OnDoubleClick(on_double_click) => {
118 self.on_double_click = Some(on_double_click);
119 }
120
121 ActionsEvent::OnHover(on_hover) => {
122 self.on_hover = Some(on_hover);
123 }
124
125 ActionsEvent::OnHoverOut(on_hover_out) => {
126 self.on_hover_out = Some(on_hover_out);
127 }
128
129 ActionsEvent::OnOver(on_over) => {
130 self.on_over = Some(on_over);
131 }
132
133 ActionsEvent::OnOverOut(on_over_out) => {
134 self.on_over_out = Some(on_over_out);
135 }
136
137 ActionsEvent::OnMouseMove(on_move) => {
138 self.on_mouse_move = Some(on_move);
139 }
140
141 ActionsEvent::OnMouseDown(on_mouse_down) => {
142 self.on_mouse_down = Some(on_mouse_down);
143 }
144
145 ActionsEvent::OnMouseUp(on_mouse_up) => {
146 self.on_mouse_up = Some(on_mouse_up);
147 }
148
149 ActionsEvent::OnFocusIn(on_focus_in) => {
150 self.on_focus_in = Some(on_focus_in);
151 }
152
153 ActionsEvent::OnFocusOut(on_focus_out) => {
154 self.on_focus_out = Some(on_focus_out);
155 }
156
157 ActionsEvent::OnGeoChanged(on_geo_changed) => {
158 self.on_geo_changed = Some(on_geo_changed);
159 cx.cache.set_bounds(cx.current, BoundingBox::default());
160 cx.needs_relayout();
161 }
162 });
163
164 event.map(|window_event, meta| match window_event {
165 WindowEvent::Press { mouse } => {
166 let over = if *mouse { cx.hovered() } else { cx.focused() };
167 if cx.current() != over && !over.is_descendant_of(cx.tree, cx.current()) {
168 return;
169 }
170
171 if !cx.is_disabled() && cx.current == meta.target {
172 let focusable = cx
181 .style
182 .abilities
183 .get(cx.current())
184 .is_some_and(|abilities| abilities.contains(Abilities::FOCUSABLE));
185 if focusable {
186 cx.focus();
187 }
188 if let Some(action) = &self.on_press {
189 (action)(cx);
190 }
191 }
192 }
193
194 WindowEvent::PressDown { mouse } => {
195 let over = if *mouse { cx.hovered() } else { cx.focused() };
196 if cx.current() != over && !over.is_descendant_of(cx.tree, cx.current()) {
197 return;
198 }
199 if !cx.is_disabled()
200 && cx.current == meta.target
201 && let Some(action) = &self.on_press_down
202 {
203 (action)(cx);
204 }
205 }
206
207 WindowEvent::MouseDoubleClick(button) => {
208 if meta.target == cx.current
209 && !cx.is_disabled()
210 && let Some(action) = &self.on_double_click
211 {
212 (action)(cx, *button);
213 }
214 }
215
216 WindowEvent::MouseEnter => {
217 if meta.target == cx.current()
218 && let Some(action) = &self.on_hover
219 {
220 (action)(cx);
221 }
222 }
223
224 WindowEvent::MouseLeave => {
225 if meta.target == cx.current()
226 && let Some(action) = &self.on_hover_out
227 {
228 (action)(cx);
229 }
230 }
231
232 WindowEvent::MouseOver => {
233 if let Some(action) = &self.on_over {
234 (action)(cx);
235 }
236 }
237
238 WindowEvent::MouseOut => {
239 if let Some(action) = &self.on_over_out {
241 (action)(cx);
242 }
243 }
245
246 WindowEvent::MouseMove(x, y) => {
247 if let Some(action) = &self.on_mouse_move {
248 (action)(cx, *x, *y);
249 }
250 }
251
252 WindowEvent::MouseDown(mouse_button) => {
253 if let Some(action) = &self.on_mouse_down {
254 (action)(cx, *mouse_button);
255 }
256 }
257
258 WindowEvent::MouseUp(mouse_button) => {
259 if let Some(action) = &self.on_mouse_up {
260 (action)(cx, *mouse_button);
261 }
262 }
263
264 WindowEvent::FocusIn => {
265 if let Some(action) = &self.on_focus_in {
266 (action)(cx);
267 }
268 }
269
270 WindowEvent::FocusOut => {
271 if let Some(action) = &self.on_focus_out {
272 (action)(cx);
273 }
274 }
275
276 WindowEvent::GeometryChanged(geo) => {
277 if meta.target == cx.current()
278 && let Some(action) = &self.on_geo_changed
279 {
280 (action)(cx, *geo);
281 }
282 }
283
284 _ => {}
285 });
286 }
287}
288
289pub(crate) enum ActionsEvent {
290 OnPress(Box<dyn Fn(&mut EventContext) + Send + Sync>),
291 OnPressDown(Box<dyn Fn(&mut EventContext) + Send + Sync>),
292 OnDoubleClick(Box<dyn Fn(&mut EventContext, MouseButton) + Send + Sync>),
293 OnHover(Box<dyn Fn(&mut EventContext) + Send + Sync>),
294 OnHoverOut(Box<dyn Fn(&mut EventContext) + Send + Sync>),
295 OnOver(Box<dyn Fn(&mut EventContext) + Send + Sync>),
296 OnOverOut(Box<dyn Fn(&mut EventContext) + Send + Sync>),
297 OnMouseMove(Box<dyn Fn(&mut EventContext, f32, f32) + Send + Sync>),
298 OnMouseDown(Box<dyn Fn(&mut EventContext, MouseButton) + Send + Sync>),
299 OnMouseUp(Box<dyn Fn(&mut EventContext, MouseButton) + Send + Sync>),
300 OnFocusIn(Box<dyn Fn(&mut EventContext) + Send + Sync>),
301 OnFocusOut(Box<dyn Fn(&mut EventContext) + Send + Sync>),
302 OnGeoChanged(Box<dyn Fn(&mut EventContext, GeoChanged) + Send + Sync>),
303}
304
305pub trait ActionModifiers<V> {
307 fn on_press<F>(self, action: F) -> Self
318 where
319 F: 'static + Fn(&mut EventContext) + Send + Sync;
320
321 fn on_press_down<F>(self, action: F) -> Self
332 where
333 F: 'static + Fn(&mut EventContext) + Send + Sync;
334
335 fn on_double_click<F>(self, action: F) -> Self
344 where
345 F: 'static + Fn(&mut EventContext, MouseButton) + Send + Sync;
346
347 fn on_hover<F>(self, action: F) -> Self
357 where
358 F: 'static + Fn(&mut EventContext) + Send + Sync;
359
360 fn on_hover_out<F>(self, action: F) -> Self
370 where
371 F: 'static + Fn(&mut EventContext) + Send + Sync;
372
373 fn on_over<F>(self, action: F) -> Self
383 where
384 F: 'static + Fn(&mut EventContext) + Send + Sync;
385
386 fn on_over_out<F>(self, action: F) -> Self
396 where
397 F: 'static + Fn(&mut EventContext) + Send + Sync;
398
399 fn on_mouse_move<F>(self, action: F) -> Self
408 where
409 F: 'static + Fn(&mut EventContext, f32, f32) + Send + Sync;
410
411 fn on_mouse_down<F>(self, action: F) -> Self
421 where
422 F: 'static + Fn(&mut EventContext, MouseButton) + Send + Sync;
423
424 fn on_mouse_up<F>(self, action: F) -> Self
434 where
435 F: 'static + Fn(&mut EventContext, MouseButton) + Send + Sync;
436
437 fn on_focus_in<F>(self, action: F) -> Self
446 where
447 F: 'static + Fn(&mut EventContext) + Send + Sync;
448
449 fn on_focus_out<F>(self, action: F) -> Self
458 where
459 F: 'static + Fn(&mut EventContext) + Send + Sync;
460
461 fn on_geo_changed<F>(self, action: F) -> Self
470 where
471 F: 'static + Fn(&mut EventContext, GeoChanged) + Send + Sync;
472
473 fn tooltip<C: Fn(&mut Context) -> Handle<'_, Tooltip> + 'static>(self, content: C) -> Self;
475
476 fn menu<C: Fn(&mut Context) -> Handle<'_, Popover> + 'static>(self, content: C) -> Self;
478}
479
480fn build_action_model(cx: &mut Context, entity: Entity) {
482 if cx.models.get(&entity).and_then(|models| models.get(&TypeId::of::<ActionsModel>())).is_none()
483 {
484 cx.with_current(entity, |cx| {
485 ActionsModel::new().build(cx);
486 });
487 }
488}
489
490fn build_modal_model(cx: &mut Context, entity: Entity) {
491 if cx.models.get(&entity).and_then(|models| models.get(&TypeId::of::<ModalModel>())).is_none() {
492 cx.with_current(entity, |cx| {
493 ModalModel {
494 tooltip_visible: Signal::new((false, true)),
495 menu_visible: Signal::new(false),
496 }
497 .build(cx);
498 });
499 }
500}
501
502impl<V: View> ActionModifiers<V> for Handle<'_, V> {
503 fn tooltip<C: Fn(&mut Context) -> Handle<'_, Tooltip> + 'static>(self, content: C) -> Self {
504 let entity = self.entity();
505
506 build_modal_model(self.cx, entity);
507
508 self.cx.with_current(entity, move |cx| {
509 let tooltip_visible = cx.data::<ModalModel>().tooltip_visible;
510 Binding::new(cx, tooltip_visible, move |cx| {
511 let tooltip_visible = tooltip_visible.get();
512 let tooltip_delay = cx.environment().tooltip_delay;
513 if tooltip_visible.0 {
514 (content)(cx)
515 .on_build(|cx| {
516 if tooltip_visible.1 {
517 cx.play_animation(
518 "tooltip_fade",
519 Duration::from_millis(100),
520 tooltip_delay,
521 )
522 }
523 })
524 .id(format!("{entity}"));
525 }
526 });
527 });
528
529 self.described_by(format!("{entity}"))
530 }
531
532 fn menu<C: Fn(&mut Context) -> Handle<'_, Popover> + 'static>(self, content: C) -> Self {
533 let entity = self.entity();
534
535 build_modal_model(self.cx, entity);
536
537 self.cx.with_current(entity, |cx| {
538 let menu_visible = cx.data::<ModalModel>().menu_visible;
539 Binding::new(cx, menu_visible, move |cx| {
540 let is_visible = menu_visible.get();
541 if is_visible {
542 (content)(cx).on_build(|cx| {
543 cx.add_listener(move |_: &mut Popover, cx, event| {
544 event.map(|window_event, _: &mut crate::events::EventMeta| {
545 match window_event {
546 WindowEvent::MouseDown(_) => {
547 if !cx.hovered.is_descendant_of(cx.tree, cx.current) {
550 cx.emit(ModalEvent::HideMenu);
551 }
552 }
554
555 _ => {}
556 }
557 });
558 });
559 });
560 }
561 });
562 });
563
564 self
565 }
566
567 fn on_press<F>(mut self, action: F) -> Self
568 where
569 F: 'static + Fn(&mut EventContext) + Send + Sync,
570 {
571 self = self.hoverable(true);
572
573 if let Some(view) = self
574 .cx
575 .views
576 .get_mut(&self.entity)
577 .and_then(|view_handler| view_handler.downcast_mut::<Button>())
578 {
579 view.action = Some(Box::new(action));
580 return self;
581 }
582
583 build_action_model(self.cx, self.entity);
584
585 self.cx.emit_custom(
586 Event::new(ActionsEvent::OnPress(Box::new(action)))
587 .target(self.entity)
588 .origin(self.entity),
589 );
590
591 self
592 }
593
594 fn on_press_down<F>(self, action: F) -> Self
595 where
596 F: 'static + Fn(&mut EventContext) + Send + Sync,
597 {
598 build_action_model(self.cx, self.entity);
599
600 self.cx.emit_custom(
601 Event::new(ActionsEvent::OnPressDown(Box::new(action)))
602 .target(self.entity)
603 .origin(self.entity),
604 );
605
606 self
607 }
608
609 fn on_double_click<F>(self, action: F) -> Self
610 where
611 F: 'static + Fn(&mut EventContext, MouseButton) + Send + Sync,
612 {
613 build_action_model(self.cx, self.entity);
614
615 self.cx.emit_custom(
616 Event::new(ActionsEvent::OnDoubleClick(Box::new(action)))
617 .target(self.entity)
618 .origin(self.entity),
619 );
620
621 self
622 }
623
624 fn on_hover<F>(self, action: F) -> Self
625 where
626 F: 'static + Fn(&mut EventContext) + Send + Sync,
627 {
628 build_action_model(self.cx, self.entity);
629
630 self.cx.emit_custom(
631 Event::new(ActionsEvent::OnHover(Box::new(action)))
632 .target(self.entity)
633 .origin(self.entity),
634 );
635
636 self
637 }
638
639 fn on_hover_out<F>(self, action: F) -> Self
640 where
641 F: 'static + Fn(&mut EventContext) + Send + Sync,
642 {
643 build_action_model(self.cx, self.entity);
644
645 self.cx.emit_custom(
646 Event::new(ActionsEvent::OnHoverOut(Box::new(action)))
647 .target(self.entity)
648 .origin(self.entity),
649 );
650
651 self
652 }
653
654 fn on_over<F>(self, action: F) -> Self
655 where
656 F: 'static + Fn(&mut EventContext) + Send + Sync,
657 {
658 build_action_model(self.cx, self.entity);
659
660 self.cx.emit_custom(
661 Event::new(ActionsEvent::OnOver(Box::new(action)))
662 .target(self.entity)
663 .origin(self.entity),
664 );
665
666 self
667 }
668
669 fn on_over_out<F>(self, action: F) -> Self
670 where
671 F: 'static + Fn(&mut EventContext) + Send + Sync,
672 {
673 build_action_model(self.cx, self.entity);
674
675 self.cx.emit_custom(
676 Event::new(ActionsEvent::OnOverOut(Box::new(action)))
677 .target(self.entity)
678 .origin(self.entity),
679 );
680
681 self
682 }
683
684 fn on_mouse_move<F>(self, action: F) -> Self
685 where
686 F: 'static + Fn(&mut EventContext, f32, f32) + Send + Sync,
687 {
688 build_action_model(self.cx, self.entity);
689
690 self.cx.emit_custom(
691 Event::new(ActionsEvent::OnMouseMove(Box::new(action)))
692 .target(self.entity)
693 .origin(self.entity),
694 );
695
696 self
697 }
698
699 fn on_mouse_down<F>(self, action: F) -> Self
700 where
701 F: 'static + Fn(&mut EventContext, MouseButton) + Send + Sync,
702 {
703 build_action_model(self.cx, self.entity);
704
705 self.cx.emit_custom(
706 Event::new(ActionsEvent::OnMouseDown(Box::new(action)))
707 .target(self.entity)
708 .origin(self.entity),
709 );
710
711 self
712 }
713
714 fn on_mouse_up<F>(self, action: F) -> Self
715 where
716 F: 'static + Fn(&mut EventContext, MouseButton) + Send + Sync,
717 {
718 build_action_model(self.cx, self.entity);
719
720 self.cx.emit_custom(
721 Event::new(ActionsEvent::OnMouseUp(Box::new(action)))
722 .target(self.entity)
723 .origin(self.entity),
724 );
725
726 self
727 }
728
729 fn on_focus_in<F>(self, action: F) -> Self
730 where
731 F: 'static + Fn(&mut EventContext) + Send + Sync,
732 {
733 build_action_model(self.cx, self.entity);
734
735 self.cx.emit_custom(
736 Event::new(ActionsEvent::OnFocusIn(Box::new(action)))
737 .target(self.entity)
738 .origin(self.entity),
739 );
740
741 self
742 }
743
744 fn on_focus_out<F>(self, action: F) -> Self
745 where
746 F: 'static + Fn(&mut EventContext) + Send + Sync,
747 {
748 build_action_model(self.cx, self.entity);
749
750 self.cx.emit_custom(
751 Event::new(ActionsEvent::OnFocusOut(Box::new(action)))
752 .target(self.entity)
753 .origin(self.entity),
754 );
755
756 self
757 }
758
759 fn on_geo_changed<F>(self, action: F) -> Self
760 where
761 F: 'static + Fn(&mut EventContext, GeoChanged) + Send + Sync,
762 {
763 build_action_model(self.cx, self.entity);
764
765 self.cx.emit_custom(
766 Event::new(ActionsEvent::OnGeoChanged(Box::new(action)))
767 .target(self.entity)
768 .origin(self.entity),
769 );
770
771 self
772 }
773}