1use std::any::{Any, TypeId};
11use std::collections::HashMap;
12use std::fmt::{self, Debug};
13use std::rc::Rc;
14use std::time::Duration;
15
16use waft_animation::{
17 AnimationBehavior, AnimationController, AnimationRef, AnyAnimation, CurvedAnimation, Curves,
18};
19use waft_embedder::{Canvas, Color, Offset, Paint, Size};
20use waft_foundation::{
21 App, ChangeNotifier, ChangeNotifierData, Handle, HandleId, Listenable, Listener,
22 OptionRetained, ValueChanged,
23};
24use waft_gestures::{TapDownDetails, TapUpDetails};
25use waft_rendering::CustomPainter;
26use waft_scheduler::TickerProviderObject;
27use waft_services::{MouseCursorRef, SystemMouseCursors};
28
29use crate::framework::{BuildContext, IntoWidget, State, WidgetRef};
30use crate::widgets::actions::{ActionRef, ActivateIntent, CallbackAction, FocusableActionDetector};
31use crate::widgets::basic::{CustomPaint, Semantics};
32use crate::widgets::focus_manager::FocusNodeRef;
33use crate::widgets::gesture_detector::GestureDetector;
34use crate::widgets::ticker_provider::TickerProviderStateMixin;
35use crate::widgets::widget_state::{WidgetState, WidgetStatePropertyRef, WidgetStates};
36use waft_foundation::Ref;
37
38const K_TOGGLE_DURATION: Duration = Duration::from_millis(200);
40
41const K_REACTION_FADE_DURATION: Duration = Duration::from_millis(50);
43
44const K_REACTION_ANIMATION_DURATION: Duration = Duration::from_millis(100);
46
47#[derive(Default)]
53pub struct ToggleableStateMixinData {
54 position_controller: Option<Ref<AnimationController>>,
55 position: Option<Ref<CurvedAnimation>>,
56 reaction_controller: Option<Ref<AnimationController>>,
57 reaction: Option<Ref<CurvedAnimation>>,
58 reaction_hover_fade: Option<Ref<CurvedAnimation>>,
59 reaction_hover_fade_controller: Option<Ref<AnimationController>>,
60 reaction_focus_fade: Option<Ref<CurvedAnimation>>,
61 reaction_focus_fade_controller: Option<Ref<AnimationController>>,
62 action_map: HashMap<TypeId, ActionRef>,
63 down_position: Option<Offset>,
64 focused: bool,
65 hovering: bool,
66}
67
68impl ToggleableStateMixinData {
69 pub fn new() -> ToggleableStateMixinData {
71 ToggleableStateMixinData::default()
72 }
73}
74
75pub trait ToggleableStateMixin: TickerProviderStateMixin + TickerProviderObject {
91 fn toggleable_data(self: Handle<Self>, app: &App) -> &ToggleableStateMixinData;
93
94 fn toggleable_data_mut(self: Handle<Self>, app: &mut App) -> &mut ToggleableStateMixinData;
96
97 fn as_toggleable_state(self: Handle<Self>) -> AnyToggleableState {
100 AnyToggleableState {
101 id: self.id(),
102 vtable: const { &ToggleableStateVTable::of::<Self>() },
103 }
104 }
105
106 fn position_controller(self: Handle<Self>, app: &App) -> Handle<AnimationController> {
114 self.toggleable_data(app)
115 .position_controller
116 .handle()
117 .expect("created in init_state")
118 }
119
120 fn position(self: Handle<Self>, app: &App) -> &Ref<CurvedAnimation> {
129 self.toggleable_data(app)
130 .position
131 .as_ref()
132 .expect("created in init_state")
133 }
134
135 fn reaction_controller(self: Handle<Self>, app: &App) -> Handle<AnimationController> {
143 self.toggleable_data(app)
144 .reaction_controller
145 .handle()
146 .expect("created in init_state")
147 }
148
149 fn reaction(self: Handle<Self>, app: &App) -> &Ref<CurvedAnimation> {
157 self.toggleable_data(app)
158 .reaction
159 .as_ref()
160 .expect("created in init_state")
161 }
162
163 fn reaction_hover_fade(self: Handle<Self>, app: &App) -> &Ref<CurvedAnimation> {
171 self.toggleable_data(app)
172 .reaction_hover_fade
173 .as_ref()
174 .expect("created in init_state")
175 }
176
177 fn reaction_focus_fade(self: Handle<Self>, app: &App) -> &Ref<CurvedAnimation> {
185 self.toggleable_data(app)
186 .reaction_focus_fade
187 .as_ref()
188 .expect("created in init_state")
189 }
190
191 fn reaction_animation_duration(self: Handle<Self>, app: &App) -> Option<Duration> {
194 let _ = app;
195 Some(K_REACTION_ANIMATION_DURATION)
196 }
197
198 fn is_interactive(self: Handle<Self>, app: &App) -> bool {
205 self.on_changed(app).is_some()
206 }
207
208 fn on_changed(self: Handle<Self>, app: &App) -> Option<ValueChanged<Option<bool>>>;
218
219 fn value(self: Handle<Self>, app: &App) -> Option<bool>;
229
230 fn tristate(self: Handle<Self>, app: &App) -> bool;
236
237 fn init_state(self: Handle<Self>, app: &mut App) {
240 let position_value = if self.value(app) == Some(false) {
241 0.0
242 } else {
243 1.0
244 };
245 let position_controller = AnimationController::new(
246 app,
247 Some(position_value),
248 Some(K_TOGGLE_DURATION),
249 None,
250 0.0,
251 1.0,
252 AnimationBehavior::Normal,
253 self,
254 );
255 let position = CurvedAnimation::new(
256 app,
257 position_controller.erase(),
258 Curves::ease_in(),
259 Some(Curves::ease_out()),
260 );
261 let reaction_duration = self
262 .reaction_animation_duration(app)
263 .unwrap_or(K_REACTION_ANIMATION_DURATION);
264 let reaction_controller = AnimationController::new(
265 app,
266 None,
267 Some(reaction_duration),
268 None,
269 0.0,
270 1.0,
271 AnimationBehavior::Normal,
272 self,
273 );
274 let reaction = CurvedAnimation::new(
275 app,
276 reaction_controller.erase(),
277 Curves::fast_out_slow_in(),
278 None,
279 );
280 let fade_value = {
281 let data = self.toggleable_data(app);
282 if data.hovering || data.focused {
283 1.0
284 } else {
285 0.0
286 }
287 };
288 let reaction_hover_fade_controller = AnimationController::new(
289 app,
290 Some(fade_value),
291 Some(K_REACTION_FADE_DURATION),
292 None,
293 0.0,
294 1.0,
295 AnimationBehavior::Normal,
296 self,
297 );
298 let reaction_hover_fade = CurvedAnimation::new(
299 app,
300 reaction_hover_fade_controller.erase(),
301 Curves::fast_out_slow_in(),
302 None,
303 );
304 let reaction_focus_fade_controller = AnimationController::new(
305 app,
306 Some(fade_value),
307 Some(K_REACTION_FADE_DURATION),
308 None,
309 0.0,
310 1.0,
311 AnimationBehavior::Normal,
312 self,
313 );
314 let reaction_focus_fade = CurvedAnimation::new(
315 app,
316 reaction_focus_fade_controller.erase(),
317 Curves::fast_out_slow_in(),
318 None,
319 );
320 let activate = CallbackAction::<ActivateIntent>::new(
321 app,
322 Rc::new(move |app: &mut App, _intent: &ActivateIntent| {
323 self.handle_tap(app);
324 None
325 }),
326 );
327 let data = self.toggleable_data_mut(app);
328 data.position_controller = Some(position_controller);
329 data.position = Some(position);
330 data.reaction_controller = Some(reaction_controller);
331 data.reaction = Some(reaction);
332 data.reaction_hover_fade_controller = Some(reaction_hover_fade_controller);
333 data.reaction_hover_fade = Some(reaction_hover_fade);
334 data.reaction_focus_fade_controller = Some(reaction_focus_fade_controller);
335 data.reaction_focus_fade = Some(reaction_focus_fade);
336 data.action_map
337 .insert(TypeId::of::<ActivateIntent>(), activate.erase());
338 }
339
340 fn animate_to_value(self: Handle<Self>, app: &mut App) {
346 let position_controller = self.position_controller(app);
347 let value = self.value(app);
348 if self.tristate(app) {
349 if value.is_none() {
350 position_controller.set_value(app, 0.0);
351 }
352 if value.unwrap_or(true) {
353 position_controller.forward(app, None);
354 } else {
355 position_controller.reverse(app, None);
356 }
357 } else if value.unwrap_or(false) {
358 position_controller.forward(app, None);
359 } else {
360 position_controller.reverse(app, None);
361 }
362 }
363
364 fn dispose(self: Handle<Self>, app: &mut App) {
367 let data = self.toggleable_data(app);
368 let pairs = [
369 (data.position_controller.handle(), data.position.handle()),
370 (data.reaction_controller.handle(), data.reaction.handle()),
371 (
372 data.reaction_hover_fade_controller.handle(),
373 data.reaction_hover_fade.handle(),
374 ),
375 (
376 data.reaction_focus_fade_controller.handle(),
377 data.reaction_focus_fade.handle(),
378 ),
379 ];
380 for (controller, animation) in pairs {
381 controller.expect("created in init_state").dispose(app);
382 animation.expect("created in init_state").dispose(app);
383 }
384 }
385
386 fn down_position(self: Handle<Self>, app: &App) -> Option<Offset> {
391 self.toggleable_data(app).down_position
392 }
393
394 fn states(self: Handle<Self>, app: &App) -> WidgetStates {
403 let mut states = WidgetStates::new();
404 if !self.is_interactive(app) {
405 states.insert(WidgetState::Disabled);
406 }
407 if self.toggleable_data(app).hovering {
408 states.insert(WidgetState::Hovered);
409 }
410 if self.toggleable_data(app).focused {
411 states.insert(WidgetState::Focused);
412 }
413 if self.value(app).unwrap_or(true) {
414 states.insert(WidgetState::Selected);
415 }
416 states
417 }
418
419 #[allow(clippy::too_many_arguments)]
445 fn build_toggleable(
446 self: Handle<Self>,
447 app: &mut App,
448 focus_node: Option<FocusNodeRef>,
449 on_focus_change: Option<ValueChanged<bool>>,
450 autofocus: bool,
451 mouse_cursor: Option<WidgetStatePropertyRef<MouseCursorRef>>,
452 size: Size,
453 painter: impl CustomPainter,
454 ) -> WidgetRef {
455 self.build_toggleable_with_child(
456 app,
457 focus_node,
458 on_focus_change,
459 autofocus,
460 mouse_cursor,
461 CustomPaint::new().size(size).painter(painter).into_widget(),
462 )
463 }
464
465 fn build_toggleable_with_child(
488 self: Handle<Self>,
489 app: &mut App,
490 focus_node: Option<FocusNodeRef>,
491 on_focus_change: Option<ValueChanged<bool>>,
492 autofocus: bool,
493 mouse_cursor: Option<WidgetStatePropertyRef<MouseCursorRef>>,
494 child: WidgetRef,
495 ) -> WidgetRef {
496 let is_interactive = self.is_interactive(app);
497 let effective_mouse_cursor = match mouse_cursor {
498 Some(mouse_cursor) => mouse_cursor.resolve(&self.states(app)),
499 None => SystemMouseCursors::BASIC.into(),
500 };
501 let mut detector = FocusableActionDetector::new(
502 self.tap_detector(app)
503 .child(Semantics::new().enabled(is_interactive).child(child)),
504 )
505 .actions(self.toggleable_data(app).action_map.clone())
506 .autofocus(autofocus)
507 .enabled(is_interactive)
508 .on_show_focus_highlight(move |app, focused| {
509 self.handle_focus_highlight_changed(app, focused)
510 })
511 .on_show_hover_highlight(move |app, hovering| self.handle_hover_changed(app, hovering))
512 .mouse_cursor(effective_mouse_cursor);
513 if let Some(focus_node) = focus_node {
514 detector = detector.focus_node(focus_node);
515 }
516 if let Some(on_focus_change) = on_focus_change {
517 detector = detector.on_focus_change(move |app, focused| on_focus_change(app, focused));
518 }
519 detector.into_widget()
520 }
521}
522
523trait ToggleableStateMixinPrivate: ToggleableStateMixin {
525 fn tap_detector(self: Handle<Self>, app: &App) -> GestureDetector {
527 if !self.is_interactive(app) {
528 return GestureDetector::new();
529 }
530 GestureDetector::new()
531 .on_tap_down(Rc::new(move |app: &mut App, details| {
532 self.handle_tap_down(app, details)
533 }))
534 .on_tap(Listener::new(move |app| self.handle_tap(app)))
535 .on_tap_up(Rc::new(move |app: &mut App, details| {
536 self.handle_tap_end(app, Some(details))
537 }))
538 .on_tap_cancel(Listener::new(move |app| self.handle_tap_end(app, None)))
539 }
540
541 fn handle_tap_down(self: Handle<Self>, app: &mut App, details: TapDownDetails) {
542 if self.is_interactive(app) {
543 self.toggleable_data_mut(app).down_position = Some(details.local_position);
544 self.set_state(app, |_| {});
545 self.reaction_controller(app).forward(app, None);
546 }
547 }
548
549 fn handle_tap(self: Handle<Self>, app: &mut App) {
550 if !self.is_interactive(app) {
551 return;
552 }
553 let on_changed = self.on_changed(app).expect("an interactive toggleable");
554 match self.value(app) {
555 Some(false) => on_changed(app, Some(true)),
556 Some(true) => {
557 let new_value = if self.tristate(app) {
558 None
559 } else {
560 Some(false)
561 };
562 on_changed(app, new_value);
563 }
564 None => on_changed(app, Some(false)),
565 }
566 }
568
569 fn handle_tap_end(self: Handle<Self>, app: &mut App, _details: Option<TapUpDetails>) {
570 if self.toggleable_data(app).down_position.is_some() {
571 self.toggleable_data_mut(app).down_position = None;
572 self.set_state(app, |_| {});
573 }
574 self.reaction_controller(app).reverse(app, None);
575 }
576
577 fn handle_focus_highlight_changed(self: Handle<Self>, app: &mut App, focused: bool) {
578 if focused != self.toggleable_data(app).focused {
579 self.toggleable_data_mut(app).focused = focused;
580 self.set_state(app, |_| {});
581 let controller = self
582 .toggleable_data(app)
583 .reaction_focus_fade_controller
584 .handle()
585 .expect("created in init_state");
586 if focused {
587 controller.forward(app, None);
588 } else {
589 controller.reverse(app, None);
590 }
591 }
592 }
593
594 fn handle_hover_changed(self: Handle<Self>, app: &mut App, hovering: bool) {
595 if hovering != self.toggleable_data(app).hovering {
596 self.toggleable_data_mut(app).hovering = hovering;
597 self.set_state(app, |_| {});
598 let controller = self
599 .toggleable_data(app)
600 .reaction_hover_fade_controller
601 .handle()
602 .expect("created in init_state");
603 if hovering {
604 controller.forward(app, None);
605 } else {
606 controller.reverse(app, None);
607 }
608 }
609 }
610}
611
612impl<S: ToggleableStateMixin> ToggleableStateMixinPrivate for S {}
613
614struct ToggleableStateVTable {
616 type_name: fn() -> &'static str,
617 context: fn(&App, HandleId) -> BuildContext,
618 mounted: fn(&App, HandleId) -> bool,
619 position_controller: fn(&App, HandleId) -> Handle<AnimationController>,
620 position: fn(&App, HandleId) -> &Ref<CurvedAnimation>,
621 reaction_controller: fn(&App, HandleId) -> Handle<AnimationController>,
622 reaction: fn(&App, HandleId) -> &Ref<CurvedAnimation>,
623 reaction_hover_fade: fn(&App, HandleId) -> &Ref<CurvedAnimation>,
624 reaction_focus_fade: fn(&App, HandleId) -> &Ref<CurvedAnimation>,
625 reaction_animation_duration: fn(&App, HandleId) -> Option<Duration>,
626 is_interactive: fn(&App, HandleId) -> bool,
627 on_changed: fn(&App, HandleId) -> Option<ValueChanged<Option<bool>>>,
628 value: fn(&App, HandleId) -> Option<bool>,
629 tristate: fn(&App, HandleId) -> bool,
630 animate_to_value: fn(&mut App, HandleId),
631 down_position: fn(&App, HandleId) -> Option<Offset>,
632 states: fn(&App, HandleId) -> WidgetStates,
633 #[allow(clippy::type_complexity)]
634 build_toggleable_with_child: fn(
635 &mut App,
636 HandleId,
637 Option<FocusNodeRef>,
638 Option<ValueChanged<bool>>,
639 bool,
640 Option<WidgetStatePropertyRef<MouseCursorRef>>,
641 WidgetRef,
642 ) -> WidgetRef,
643}
644
645fn resolve<S: 'static>(id: HandleId) -> Handle<S> {
647 Handle::from_id(id)
648}
649
650impl ToggleableStateVTable {
651 const fn of<S: ToggleableStateMixin>() -> ToggleableStateVTable {
652 ToggleableStateVTable {
653 type_name: std::any::type_name::<S>,
654 context: |app, id| <S as State>::context(resolve(id), app),
655 mounted: |app, id| <S as State>::mounted(resolve(id), app),
656 position_controller: |app, id| {
657 <S as ToggleableStateMixin>::position_controller(resolve(id), app)
658 },
659 position: |app, id| <S as ToggleableStateMixin>::position(resolve(id), app),
660 reaction_controller: |app, id| {
661 <S as ToggleableStateMixin>::reaction_controller(resolve(id), app)
662 },
663 reaction: |app, id| <S as ToggleableStateMixin>::reaction(resolve(id), app),
664 reaction_hover_fade: |app, id| {
665 <S as ToggleableStateMixin>::reaction_hover_fade(resolve(id), app)
666 },
667 reaction_focus_fade: |app, id| {
668 <S as ToggleableStateMixin>::reaction_focus_fade(resolve(id), app)
669 },
670 reaction_animation_duration: |app, id| {
671 <S as ToggleableStateMixin>::reaction_animation_duration(resolve(id), app)
672 },
673 is_interactive: |app, id| <S as ToggleableStateMixin>::is_interactive(resolve(id), app),
674 on_changed: |app, id| <S as ToggleableStateMixin>::on_changed(resolve(id), app),
675 value: |app, id| <S as ToggleableStateMixin>::value(resolve(id), app),
676 tristate: |app, id| <S as ToggleableStateMixin>::tristate(resolve(id), app),
677 animate_to_value: |app, id| {
678 <S as ToggleableStateMixin>::animate_to_value(resolve(id), app)
679 },
680 down_position: |app, id| <S as ToggleableStateMixin>::down_position(resolve(id), app),
681 states: |app, id| <S as ToggleableStateMixin>::states(resolve(id), app),
682 build_toggleable_with_child: |app,
683 id,
684 focus_node,
685 on_focus_change,
686 autofocus,
687 mouse_cursor,
688 child| {
689 <S as ToggleableStateMixin>::build_toggleable_with_child(
690 resolve(id),
691 app,
692 focus_node,
693 on_focus_change,
694 autofocus,
695 mouse_cursor,
696 child,
697 )
698 },
699 }
700 }
701}
702
703#[derive(Clone, Copy)]
709pub struct AnyToggleableState {
710 id: HandleId,
711 vtable: &'static ToggleableStateVTable,
712}
713
714impl PartialEq for AnyToggleableState {
715 fn eq(&self, other: &AnyToggleableState) -> bool {
716 self.id == other.id
717 }
718}
719
720impl Eq for AnyToggleableState {}
721
722impl Debug for AnyToggleableState {
723 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
724 write!(f, "{}({:?})", (self.vtable.type_name)(), self.id)
725 }
726}
727
728impl AnyToggleableState {
729 pub fn id(self) -> HandleId {
731 self.id
732 }
733
734 pub fn context(self, app: &App) -> BuildContext {
736 (self.vtable.context)(app, self.id)
737 }
738
739 pub fn mounted(self, app: &App) -> bool {
741 (self.vtable.mounted)(app, self.id)
742 }
743
744 pub fn position_controller(self, app: &App) -> Handle<AnimationController> {
746 (self.vtable.position_controller)(app, self.id)
747 }
748
749 pub fn position(self, app: &App) -> &Ref<CurvedAnimation> {
751 (self.vtable.position)(app, self.id)
752 }
753
754 pub fn reaction_controller(self, app: &App) -> Handle<AnimationController> {
756 (self.vtable.reaction_controller)(app, self.id)
757 }
758
759 pub fn reaction(self, app: &App) -> &Ref<CurvedAnimation> {
761 (self.vtable.reaction)(app, self.id)
762 }
763
764 pub fn reaction_hover_fade(self, app: &App) -> &Ref<CurvedAnimation> {
766 (self.vtable.reaction_hover_fade)(app, self.id)
767 }
768
769 pub fn reaction_focus_fade(self, app: &App) -> &Ref<CurvedAnimation> {
771 (self.vtable.reaction_focus_fade)(app, self.id)
772 }
773
774 pub fn reaction_animation_duration(self, app: &App) -> Option<Duration> {
776 (self.vtable.reaction_animation_duration)(app, self.id)
777 }
778
779 pub fn is_interactive(self, app: &App) -> bool {
781 (self.vtable.is_interactive)(app, self.id)
782 }
783
784 pub fn on_changed(self, app: &App) -> Option<ValueChanged<Option<bool>>> {
786 (self.vtable.on_changed)(app, self.id)
787 }
788
789 pub fn value(self, app: &App) -> Option<bool> {
791 (self.vtable.value)(app, self.id)
792 }
793
794 pub fn tristate(self, app: &App) -> bool {
796 (self.vtable.tristate)(app, self.id)
797 }
798
799 pub fn animate_to_value(self, app: &mut App) {
801 (self.vtable.animate_to_value)(app, self.id);
802 }
803
804 pub fn down_position(self, app: &App) -> Option<Offset> {
806 (self.vtable.down_position)(app, self.id)
807 }
808
809 pub fn states(self, app: &App) -> WidgetStates {
811 (self.vtable.states)(app, self.id)
812 }
813
814 #[allow(clippy::too_many_arguments)]
816 pub fn build_toggleable(
817 self,
818 app: &mut App,
819 focus_node: Option<FocusNodeRef>,
820 on_focus_change: Option<ValueChanged<bool>>,
821 autofocus: bool,
822 mouse_cursor: Option<WidgetStatePropertyRef<MouseCursorRef>>,
823 size: Size,
824 painter: impl CustomPainter,
825 ) -> WidgetRef {
826 self.build_toggleable_with_child(
827 app,
828 focus_node,
829 on_focus_change,
830 autofocus,
831 mouse_cursor,
832 CustomPaint::new().size(size).painter(painter).into_widget(),
833 )
834 }
835
836 pub fn build_toggleable_with_child(
838 self,
839 app: &mut App,
840 focus_node: Option<FocusNodeRef>,
841 on_focus_change: Option<ValueChanged<bool>>,
842 autofocus: bool,
843 mouse_cursor: Option<WidgetStatePropertyRef<MouseCursorRef>>,
844 child: WidgetRef,
845 ) -> WidgetRef {
846 (self.vtable.build_toggleable_with_child)(
847 app,
848 self.id,
849 focus_node,
850 on_focus_change,
851 autofocus,
852 mouse_cursor,
853 child,
854 )
855 }
856}
857
858#[derive(Default)]
862pub struct ToggleablePainterData {
863 position: Option<AnimationRef<f64>>,
864 reaction: Option<AnimationRef<f64>>,
865 reaction_focus_fade: Option<AnimationRef<f64>>,
866 reaction_hover_fade: Option<AnimationRef<f64>>,
867 active_color: Option<Color>,
868 inactive_color: Option<Color>,
869 inactive_reaction_color: Option<Color>,
870 reaction_color: Option<Color>,
871 hover_color: Option<Color>,
872 focus_color: Option<Color>,
873 splash_radius: Option<f64>,
874 down_position: Option<Offset>,
875 is_focused: Option<bool>,
876 is_hovered: Option<bool>,
877 is_active: Option<bool>,
878}
879
880impl ToggleablePainterData {
881 pub fn new() -> ToggleablePainterData {
883 ToggleablePainterData::default()
884 }
885}
886
887pub trait ToggleablePainter: ChangeNotifier {
903 fn toggleable_painter_data(self: Handle<Self>, app: &App) -> &ToggleablePainterData;
905
906 fn toggleable_painter_data_mut(self: Handle<Self>, app: &mut App)
908 -> &mut ToggleablePainterData;
909
910 fn position(self: Handle<Self>, app: &App) -> AnyAnimation<f64> {
914 self.toggleable_painter_data(app)
915 .position
916 .handle()
917 .expect("set before painting")
918 }
919
920 fn set_position(self: Handle<Self>, app: &mut App, value: AnimationRef<f64>) {
922 let old = self.toggleable_painter_data(app).position.handle();
923 if old == Some(value.handle()) {
924 return;
925 }
926 let notify = Listener::handle_method(self, Handle::<Self>::notify_listeners);
927 if let Some(old) = old {
928 old.remove_listener(app, ¬ify);
929 }
930 value.add_listener(app, notify);
931 self.toggleable_painter_data_mut(app).position = Some(value);
932 self.notify_listeners(app);
933 }
934
935 fn reaction(self: Handle<Self>, app: &App) -> AnyAnimation<f64> {
939 self.toggleable_painter_data(app)
940 .reaction
941 .handle()
942 .expect("set before painting")
943 }
944
945 fn set_reaction(self: Handle<Self>, app: &mut App, value: AnimationRef<f64>) {
947 let old = self.toggleable_painter_data(app).reaction.handle();
948 if old == Some(value.handle()) {
949 return;
950 }
951 let notify = Listener::handle_method(self, Handle::<Self>::notify_listeners);
952 if let Some(old) = old {
953 old.remove_listener(app, ¬ify);
954 }
955 value.add_listener(app, notify);
956 self.toggleable_painter_data_mut(app).reaction = Some(value);
957 self.notify_listeners(app);
958 }
959
960 fn reaction_focus_fade(self: Handle<Self>, app: &App) -> AnyAnimation<f64> {
964 self.toggleable_painter_data(app)
965 .reaction_focus_fade
966 .handle()
967 .expect("set before painting")
968 }
969
970 fn set_reaction_focus_fade(self: Handle<Self>, app: &mut App, value: AnimationRef<f64>) {
972 let old = self
973 .toggleable_painter_data(app)
974 .reaction_focus_fade
975 .handle();
976 if old == Some(value.handle()) {
977 return;
978 }
979 let notify = Listener::handle_method(self, Handle::<Self>::notify_listeners);
980 if let Some(old) = old {
981 old.remove_listener(app, ¬ify);
982 }
983 value.add_listener(app, notify);
984 self.toggleable_painter_data_mut(app).reaction_focus_fade = Some(value);
985 self.notify_listeners(app);
986 }
987
988 fn reaction_hover_fade(self: Handle<Self>, app: &App) -> AnyAnimation<f64> {
992 self.toggleable_painter_data(app)
993 .reaction_hover_fade
994 .handle()
995 .expect("set before painting")
996 }
997
998 fn set_reaction_hover_fade(self: Handle<Self>, app: &mut App, value: AnimationRef<f64>) {
1000 let old = self
1001 .toggleable_painter_data(app)
1002 .reaction_hover_fade
1003 .handle();
1004 if old == Some(value.handle()) {
1005 return;
1006 }
1007 let notify = Listener::handle_method(self, Handle::<Self>::notify_listeners);
1008 if let Some(old) = old {
1009 old.remove_listener(app, ¬ify);
1010 }
1011 value.add_listener(app, notify);
1012 self.toggleable_painter_data_mut(app).reaction_hover_fade = Some(value);
1013 self.notify_listeners(app);
1014 }
1015
1016 fn active_color(self: Handle<Self>, app: &App) -> Color {
1021 self.toggleable_painter_data(app)
1022 .active_color
1023 .expect("set before painting")
1024 }
1025
1026 fn set_active_color(self: Handle<Self>, app: &mut App, value: Color) {
1028 if self.toggleable_painter_data(app).active_color == Some(value) {
1029 return;
1030 }
1031 self.toggleable_painter_data_mut(app).active_color = Some(value);
1032 self.notify_listeners(app);
1033 }
1034
1035 fn inactive_color(self: Handle<Self>, app: &App) -> Color {
1040 self.toggleable_painter_data(app)
1041 .inactive_color
1042 .expect("set before painting")
1043 }
1044
1045 fn set_inactive_color(self: Handle<Self>, app: &mut App, value: Color) {
1047 if self.toggleable_painter_data(app).inactive_color == Some(value) {
1048 return;
1049 }
1050 self.toggleable_painter_data_mut(app).inactive_color = Some(value);
1051 self.notify_listeners(app);
1052 }
1053
1054 fn inactive_reaction_color(self: Handle<Self>, app: &App) -> Color {
1059 self.toggleable_painter_data(app)
1060 .inactive_reaction_color
1061 .expect("set before painting")
1062 }
1063
1064 fn set_inactive_reaction_color(self: Handle<Self>, app: &mut App, value: Color) {
1066 if self.toggleable_painter_data(app).inactive_reaction_color == Some(value) {
1067 return;
1068 }
1069 self.toggleable_painter_data_mut(app)
1070 .inactive_reaction_color = Some(value);
1071 self.notify_listeners(app);
1072 }
1073
1074 fn reaction_color(self: Handle<Self>, app: &App) -> Color {
1079 self.toggleable_painter_data(app)
1080 .reaction_color
1081 .expect("set before painting")
1082 }
1083
1084 fn set_reaction_color(self: Handle<Self>, app: &mut App, value: Color) {
1086 if self.toggleable_painter_data(app).reaction_color == Some(value) {
1087 return;
1088 }
1089 self.toggleable_painter_data_mut(app).reaction_color = Some(value);
1090 self.notify_listeners(app);
1091 }
1092
1093 fn hover_color(self: Handle<Self>, app: &App) -> Color {
1099 self.toggleable_painter_data(app)
1100 .hover_color
1101 .expect("set before painting")
1102 }
1103
1104 fn set_hover_color(self: Handle<Self>, app: &mut App, value: Color) {
1106 if self.toggleable_painter_data(app).hover_color == Some(value) {
1107 return;
1108 }
1109 self.toggleable_painter_data_mut(app).hover_color = Some(value);
1110 self.notify_listeners(app);
1111 }
1112
1113 fn focus_color(self: Handle<Self>, app: &App) -> Color {
1119 self.toggleable_painter_data(app)
1120 .focus_color
1121 .expect("set before painting")
1122 }
1123
1124 fn set_focus_color(self: Handle<Self>, app: &mut App, value: Color) {
1126 if self.toggleable_painter_data(app).focus_color == Some(value) {
1127 return;
1128 }
1129 self.toggleable_painter_data_mut(app).focus_color = Some(value);
1130 self.notify_listeners(app);
1131 }
1132
1133 fn splash_radius(self: Handle<Self>, app: &App) -> f64 {
1135 self.toggleable_painter_data(app)
1136 .splash_radius
1137 .expect("set before painting")
1138 }
1139
1140 fn set_splash_radius(self: Handle<Self>, app: &mut App, value: f64) {
1142 if self.toggleable_painter_data(app).splash_radius == Some(value) {
1143 return;
1144 }
1145 self.toggleable_painter_data_mut(app).splash_radius = Some(value);
1146 self.notify_listeners(app);
1147 }
1148
1149 fn down_position(self: Handle<Self>, app: &App) -> Option<Offset> {
1155 self.toggleable_painter_data(app).down_position
1156 }
1157
1158 fn set_down_position(self: Handle<Self>, app: &mut App, value: Option<Offset>) {
1160 if self.toggleable_painter_data(app).down_position == value {
1161 return;
1162 }
1163 self.toggleable_painter_data_mut(app).down_position = value;
1164 self.notify_listeners(app);
1165 }
1166
1167 fn is_focused(self: Handle<Self>, app: &App) -> bool {
1169 self.toggleable_painter_data(app)
1170 .is_focused
1171 .expect("set before painting")
1172 }
1173
1174 fn set_is_focused(self: Handle<Self>, app: &mut App, value: Option<bool>) {
1176 if self.toggleable_painter_data(app).is_focused == value {
1177 return;
1178 }
1179 self.toggleable_painter_data_mut(app).is_focused = value;
1180 self.notify_listeners(app);
1181 }
1182
1183 fn is_hovered(self: Handle<Self>, app: &App) -> bool {
1185 self.toggleable_painter_data(app)
1186 .is_hovered
1187 .expect("set before painting")
1188 }
1189
1190 fn set_is_hovered(self: Handle<Self>, app: &mut App, value: Option<bool>) {
1192 if self.toggleable_painter_data(app).is_hovered == value {
1193 return;
1194 }
1195 self.toggleable_painter_data_mut(app).is_hovered = value;
1196 self.notify_listeners(app);
1197 }
1198
1199 fn is_active(self: Handle<Self>, app: &App) -> bool {
1201 self.toggleable_painter_data(app)
1202 .is_active
1203 .expect("set before painting")
1204 }
1205
1206 fn set_is_active(self: Handle<Self>, app: &mut App, value: Option<bool>) {
1208 if self.toggleable_painter_data(app).is_active == value {
1209 return;
1210 }
1211 self.toggleable_painter_data_mut(app).is_active = value;
1212 self.notify_listeners(app);
1213 }
1214
1215 fn paint(self: Handle<Self>, app: &mut App, canvas: &mut Canvas, size: Size);
1217
1218 fn paint_radial_reaction(
1224 self: Handle<Self>,
1225 app: &App,
1226 canvas: &mut Canvas,
1227 offset: Offset,
1228 origin: Offset,
1229 ) {
1230 let reaction = self.reaction(app);
1231 let reaction_focus_fade = self.reaction_focus_fade(app);
1232 let reaction_hover_fade = self.reaction_hover_fade(app);
1233 if !reaction.is_dismissed(app)
1234 || !reaction_focus_fade.is_dismissed(app)
1235 || !reaction_hover_fade.is_dismissed(app)
1236 {
1237 let reaction_color = Color::lerp(
1238 Color::lerp(
1239 Color::lerp(
1240 Some(self.inactive_reaction_color(app)),
1241 Some(self.reaction_color(app)),
1242 self.position(app).value(app),
1243 ),
1244 Some(self.hover_color(app)),
1245 reaction_hover_fade.value(app),
1246 ),
1247 Some(self.focus_color(app)),
1248 reaction_focus_fade.value(app),
1249 )
1250 .expect("lerping non-null colors");
1251 let reaction_paint = Paint::from_color(reaction_color.into());
1252 let splash_radius = self.splash_radius(app);
1253 let reaction_radius = if self.is_focused(app) || self.is_hovered(app) {
1255 splash_radius
1256 } else {
1257 splash_radius * reaction.value(app)
1258 };
1259 if reaction_radius > 0.0 {
1260 canvas.draw_circle(origin + offset, reaction_radius as f32, &reaction_paint);
1261 }
1262 }
1263 }
1264
1265 fn dispose(self: Handle<Self>, app: &mut App) {
1268 let notify = Listener::handle_method(self, Handle::<Self>::notify_listeners);
1269 let data = self.toggleable_painter_data(app);
1270 let animations = [
1271 data.position.handle(),
1272 data.reaction.handle(),
1273 data.reaction_focus_fade.handle(),
1274 data.reaction_hover_fade.handle(),
1275 ];
1276 for animation in animations.into_iter().flatten() {
1277 animation.remove_listener(app, ¬ify);
1278 }
1279 ChangeNotifierData::dispose(app, self);
1280 }
1281}
1282
1283pub struct ToggleablePainterRef<P: ToggleablePainter> {
1287 painter: Ref<P>,
1288 repaint: Rc<dyn Listenable>,
1289}
1290
1291impl<P: ToggleablePainter> ToggleablePainterRef<P> {
1292 pub fn new(painter: Ref<P>) -> ToggleablePainterRef<P> {
1294 ToggleablePainterRef {
1295 repaint: Rc::new(painter.clone()),
1296 painter,
1297 }
1298 }
1299
1300 pub fn painter(&self) -> Handle<P> {
1302 self.painter.handle()
1303 }
1304}
1305
1306impl<P: ToggleablePainter> CustomPainter for ToggleablePainterRef<P> {
1307 fn repaint(&self) -> Option<&Rc<dyn Listenable>> {
1308 Some(&self.repaint)
1309 }
1310
1311 fn paint(&self, app: &mut App, canvas: &mut Canvas, size: Size) {
1312 ToggleablePainter::paint(self.painter.handle(), app, canvas, size);
1313 }
1314
1315 fn should_repaint(&self, _app: &App, _old_delegate: &dyn CustomPainter) -> bool {
1316 true
1317 }
1318
1319 fn as_any(&self) -> &dyn Any {
1320 self
1321 }
1322}
1323
1324impl<P: ToggleablePainter> Debug for ToggleablePainterRef<P> {
1325 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1326 write!(f, "{}#{:?}", std::any::type_name::<P>(), self.painter.id())
1327 }
1328}