Skip to main content

uzor_core/widgets/overlay/
input.rs

1//! Overlay input adapter - Contract/Connector for overlay event handling
2
3use std::time::Duration;
4use crate::types::Rect;
5
6/// Input handler adapter for overlay events
7pub trait OverlayInputHandler {
8    fn should_show(&self, hover_duration: Duration, delay_threshold_ms: u32) -> bool {
9        hover_duration.as_millis() >= delay_threshold_ms as u128
10    }
11
12    fn calculate_fade_opacity(&self, elapsed_ms: u32, fade_duration_ms: u32) -> f64 {
13        if elapsed_ms >= fade_duration_ms {
14            1.0
15        } else {
16            (elapsed_ms as f64) / (fade_duration_ms as f64)
17        }
18    }
19
20    fn adjust_position_to_screen(
21        &self,
22        pos: (f64, f64),
23        size: (f64, f64),
24        screen: (f64, f64)
25    ) -> (f64, f64) {
26        let (mut x, mut y) = pos;
27        let (width, height) = size;
28        let (screen_width, screen_height) = screen;
29
30        if x + width > screen_width {
31            x = screen_width - width;
32        }
33        if x < 0.0 {
34            x = 0.0;
35        }
36
37        if y + height > screen_height {
38            y = screen_height - height;
39        }
40        if y < 0.0 {
41            y = 0.0;
42        }
43
44        (x, y)
45    }
46
47    fn calculate_tooltip_size(&self, text: &str, max_width: f64, font_size: f64) -> (f64, f64) {
48        let char_width = font_size * 0.6;
49        let chars_per_line = (max_width / char_width).max(1.0) as usize;
50        let line_count = (text.len() + chars_per_line - 1) / chars_per_line.max(1);
51        let width = max_width.min(text.len() as f64 * char_width);
52        let height = line_count as f64 * font_size * 1.5;
53        (width.max(0.0), height.max(font_size))
54    }
55
56    fn is_mouse_nearby(
57        &self,
58        mouse_pos: (f64, f64),
59        overlay_rect: &Rect,
60        threshold: f64
61    ) -> bool {
62        let (mx, my) = mouse_pos;
63        mx >= overlay_rect.x - threshold
64            && mx <= overlay_rect.x + overlay_rect.width + threshold
65            && my >= overlay_rect.y - threshold
66            && my <= overlay_rect.y + overlay_rect.height + threshold
67    }
68}
69
70/// Default implementation of OverlayInputHandler
71#[derive(Clone, Copy, Debug, Default)]
72pub struct DefaultOverlayInputHandler;
73
74impl OverlayInputHandler for DefaultOverlayInputHandler {}