Skip to main content

uzor_core/widgets/panel/
input.rs

1//! Panel input adapter - Contract/Connector for panel event handling
2
3use crate::types::Rect;
4
5/// Input handler adapter for panel events
6pub trait PanelInputHandler {
7    fn hit_test(&self, rect: Rect, mouse_pos: (f64, f64)) -> bool {
8        let (mx, my) = mouse_pos;
9        mx >= rect.x
10            && mx <= rect.x + rect.width
11            && my >= rect.y
12            && my <= rect.y + rect.height
13    }
14
15    fn hit_test_resize_handle(
16        &self,
17        handle_rect: Rect,
18        mouse_pos: (f64, f64),
19    ) -> bool {
20        self.hit_test(handle_rect, mouse_pos)
21    }
22
23    fn hit_test_chevron(
24        &self,
25        chevron_rect: Rect,
26        mouse_pos: (f64, f64),
27    ) -> bool {
28        self.hit_test(chevron_rect, mouse_pos)
29    }
30
31    fn mouse_to_size(
32        &self,
33        mouse_pos: f64,
34        min_size: f64,
35        max_size: f64,
36    ) -> f64 {
37        mouse_pos.clamp(min_size, max_size)
38    }
39
40    fn calculate_resize_handle(
41        &self,
42        panel_rect: Rect,
43        handle_width: f64,
44        is_left: bool,
45    ) -> Rect {
46        if is_left {
47            Rect {
48                x: panel_rect.x + panel_rect.width - handle_width / 2.0,
49                y: panel_rect.y,
50                width: handle_width,
51                height: panel_rect.height,
52            }
53        } else {
54            Rect {
55                x: panel_rect.x - handle_width / 2.0,
56                y: panel_rect.y,
57                width: handle_width,
58                height: panel_rect.height,
59            }
60        }
61    }
62
63    fn is_outside_click(
64        &self,
65        mouse_pos: (f64, f64),
66        modal_rect: Rect,
67    ) -> bool {
68        !self.hit_test(modal_rect, mouse_pos)
69    }
70
71    fn calculate_floating_position(
72        &self,
73        anchor_rect: Rect,
74        offset: (f64, f64),
75    ) -> (f64, f64) {
76        (anchor_rect.x + offset.0, anchor_rect.y + offset.1)
77    }
78
79    fn calculate_chevron_rect(
80        &self,
81        panel_x: f64,
82        panel_y: f64,
83        button_height: f64,
84        panel_width: f64,
85    ) -> Rect {
86        Rect {
87            x: panel_x,
88            y: panel_y,
89            width: panel_width,
90            height: button_height,
91        }
92    }
93}
94
95/// Default implementation of PanelInputHandler
96#[derive(Clone, Copy, Debug, Default)]
97pub struct DefaultPanelInputHandler;
98
99impl PanelInputHandler for DefaultPanelInputHandler {}