use iced::widget::{container, mouse_area};
use iced::{mouse, Element, Length};
pub const SPLIT_HANDLE_WIDTH: f32 = 7.0;
pub fn split_handle<'a, Message: Clone + 'a>(on_press: Message, on_reset: Message) -> Element<'a, Message> {
let strip = container(super::vertical_divider())
.width(Length::Fixed(SPLIT_HANDLE_WIDTH))
.height(Length::Fill)
.center_x(Length::Fixed(SPLIT_HANDLE_WIDTH));
mouse_area(strip)
.interaction(mouse::Interaction::ResizingHorizontally)
.on_press(on_press)
.on_double_click(on_reset)
.into()
}
pub fn split_ratio(x: f32, width: f32, min_side: f32) -> f32 {
if !(x.is_finite() && width.is_finite()) || width <= 2.0 * min_side {
return 0.5;
}
let edge = min_side / width;
(x / width).clamp(edge, 1.0 - edge)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_drag_follows_the_pointer_but_never_squeezes_a_pane_below_its_minimum() {
assert_eq!(split_ratio(300.0, 1000.0, 200.0), 0.3);
assert_eq!(split_ratio(50.0, 1000.0, 200.0), 0.2, "left kept at 200");
assert_eq!(split_ratio(990.0, 1000.0, 200.0), 0.8, "right kept at 200");
}
#[test]
fn an_area_too_narrow_for_two_minimums_splits_evenly() {
assert_eq!(split_ratio(100.0, 300.0, 200.0), 0.5);
assert_eq!(split_ratio(f32::NAN, 1000.0, 200.0), 0.5);
}
}