hyprforge_ui/widgets/
split.rs1use iced::widget::{container, mouse_area};
9use iced::{mouse, Element, Length};
10
11pub const SPLIT_HANDLE_WIDTH: f32 = 7.0;
15
16pub fn split_handle<'a, Message: Clone + 'a>(on_press: Message, on_reset: Message) -> Element<'a, Message> {
20 let strip = container(super::vertical_divider())
21 .width(Length::Fixed(SPLIT_HANDLE_WIDTH))
22 .height(Length::Fill)
23 .center_x(Length::Fixed(SPLIT_HANDLE_WIDTH));
24 mouse_area(strip)
25 .interaction(mouse::Interaction::ResizingHorizontally)
26 .on_press(on_press)
27 .on_double_click(on_reset)
28 .into()
29}
30
31pub fn split_ratio(x: f32, width: f32, min_side: f32) -> f32 {
36 if !(x.is_finite() && width.is_finite()) || width <= 2.0 * min_side {
37 return 0.5;
38 }
39 let edge = min_side / width;
40 (x / width).clamp(edge, 1.0 - edge)
41}
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46
47 #[test]
48 fn a_drag_follows_the_pointer_but_never_squeezes_a_pane_below_its_minimum() {
49 assert_eq!(split_ratio(300.0, 1000.0, 200.0), 0.3);
50 assert_eq!(split_ratio(50.0, 1000.0, 200.0), 0.2, "left kept at 200");
51 assert_eq!(split_ratio(990.0, 1000.0, 200.0), 0.8, "right kept at 200");
52 }
53
54 #[test]
55 fn an_area_too_narrow_for_two_minimums_splits_evenly() {
56 assert_eq!(split_ratio(100.0, 300.0, 200.0), 0.5);
57 assert_eq!(split_ratio(f32::NAN, 1000.0, 200.0), 0.5);
58 }
59}