Skip to main content

uzor_core/widgets/slider/
input.rs

1//! Slider input adapter - Contract/Connector for slider event handling
2
3use crate::types::Rect;
4
5/// Input handler adapter for slider events
6pub trait SliderInputHandler {
7    fn hit_test(
8        &self,
9        handle_center: (f64, f64),
10        handle_radius: f64,
11        mouse_pos: (f64, f64),
12    ) -> bool {
13        let dx = mouse_pos.0 - handle_center.0;
14        let dy = mouse_pos.1 - handle_center.1;
15        (dx * dx + dy * dy) <= (handle_radius * handle_radius)
16    }
17
18    fn mouse_to_value(
19        &self,
20        mouse_x: f64,
21        track_x: f64,
22        track_width: f64,
23        min: f64,
24        max: f64,
25    ) -> f64 {
26        if track_width <= 0.0 {
27            return min;
28        }
29        let t = ((mouse_x - track_x) / track_width).clamp(0.0, 1.0);
30        min + t * (max - min)
31    }
32
33    fn scroll_to_delta(&self, scroll_delta: f64, min: f64, max: f64) -> f64 {
34        let range = max - min;
35        if range <= 0.0 {
36            return 0.0;
37        }
38        scroll_delta.signum() * range * 0.1
39    }
40
41    fn is_on_track(&self, mouse_pos: (f64, f64), track_rect: &Rect) -> bool {
42        mouse_pos.0 >= track_rect.x
43            && mouse_pos.0 <= track_rect.x + track_rect.width
44            && mouse_pos.1 >= track_rect.y
45            && mouse_pos.1 <= track_rect.y + track_rect.height
46    }
47}
48
49/// Default implementation of SliderInputHandler
50#[derive(Clone, Copy, Debug, Default)]
51pub struct DefaultSliderInputHandler;
52
53impl SliderInputHandler for DefaultSliderInputHandler {}