Skip to main content

uzor_core/widgets/toast/
input.rs

1//! Toast input adapter - Contract/Connector for toast interaction handling
2
3use crate::types::Rect;
4
5/// Fade animation phase for toast
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum FadePhase {
8    /// Fading in (opacity 0.0 -> 1.0)
9    FadeIn,
10    /// Full display (opacity 1.0)
11    Display,
12    /// Fading out (opacity 1.0 -> 0.0)
13    FadeOut,
14}
15
16/// Input handler adapter for toast interactions
17pub trait ToastInputHandler {
18    fn calculate_stack_offset(
19        &self,
20        toast_index: usize,
21        toast_height: f64,
22        spacing: f64,
23    ) -> f64 {
24        toast_index as f64 * (toast_height + spacing)
25    }
26
27    fn is_mouse_over(&self, mouse_pos: (f64, f64), toast_rect: &Rect) -> bool {
28        let (mx, my) = mouse_pos;
29        mx >= toast_rect.x
30            && mx <= toast_rect.x + toast_rect.width
31            && my >= toast_rect.y
32            && my <= toast_rect.y + toast_rect.height
33    }
34
35    fn is_dismiss_click(&self, mouse_pos: (f64, f64), close_button_rect: &Rect) -> bool {
36        self.is_mouse_over(mouse_pos, close_button_rect)
37    }
38
39    fn update_opacity_fade(
40        &self,
41        phase: FadePhase,
42        elapsed_ms: u32,
43        fade_duration_ms: u32,
44    ) -> f64 {
45        match phase {
46            FadePhase::FadeIn => {
47                if elapsed_ms >= fade_duration_ms {
48                    1.0
49                } else {
50                    elapsed_ms as f64 / fade_duration_ms as f64
51                }
52            }
53            FadePhase::Display => 1.0,
54            FadePhase::FadeOut => {
55                if elapsed_ms >= fade_duration_ms {
56                    0.0
57                } else {
58                    1.0 - (elapsed_ms as f64 / fade_duration_ms as f64)
59                }
60            }
61        }
62    }
63
64    fn calculate_position(
65        &self,
66        screen_size: (f64, f64),
67        toast_size: (f64, f64),
68        offset: (f64, f64),
69        vertical_offset: f64,
70    ) -> (f64, f64) {
71        let (screen_width, _screen_height) = screen_size;
72        let (toast_width, _toast_height) = toast_size;
73        let (offset_x, offset_y) = offset;
74
75        let x = screen_width - toast_width - offset_x;
76        let y = offset_y + vertical_offset;
77
78        (x, y)
79    }
80}
81
82/// Default implementation of ToastInputHandler
83#[derive(Clone, Copy, Debug, Default)]
84pub struct DefaultToastInputHandler;
85
86impl ToastInputHandler for DefaultToastInputHandler {}