use egui::{Pos2, Rect};
use crate::{AllowedSplits, OverlayFeel, Split};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum DropZone {
Center,
Left,
Right,
Top,
Bottom,
}
impl DropZone {
pub fn at(rect: Rect, pointer: Pos2, edge_coverage: f32, allowed: AllowedSplits) -> Self {
let edge_coverage = edge_coverage.clamp(0.0, 0.5);
if !(rect.width() > 0.0 && rect.height() > 0.0) || edge_coverage == 0.0 {
return Self::Center;
}
let x = ((pointer.x - rect.left()) / rect.width()).clamp(0.0, 1.0);
let y = ((pointer.y - rect.top()) / rect.height()).clamp(0.0, 1.0);
let left_right = matches!(allowed, AllowedSplits::All | AllowedSplits::LeftRightOnly);
let top_bottom = matches!(allowed, AllowedSplits::All | AllowedSplits::TopBottomOnly);
let mut best = (Self::Center, edge_coverage);
for (zone, distance, enabled) in [
(Self::Left, x, left_right),
(Self::Right, 1.0 - x, left_right),
(Self::Top, y, top_bottom),
(Self::Bottom, 1.0 - y, top_bottom),
] {
if enabled && distance < best.1 {
best = (zone, distance);
}
}
best.0
}
pub fn in_leaf(
leaf: Rect,
tab_bar: Option<Rect>,
pointer: Pos2,
feel: &OverlayFeel,
allowed: AllowedSplits,
) -> Self {
let Some(tab_bar) = tab_bar else {
return Self::at(leaf, pointer, feel.zone_edge_coverage, allowed);
};
let body_top =
(tab_bar.bottom() + feel.zone_tab_bar_stack_band.max(0.0)).min(leaf.bottom());
if (tab_bar.bottom()..body_top).contains(&pointer.y) {
return Self::Center;
}
let rect = if feel.zone_edges_below_tab_bar {
Rect::from_min_max(Pos2::new(leaf.left(), body_top), leaf.max)
} else {
leaf
};
Self::at(rect, pointer, feel.zone_edge_coverage, allowed)
}
pub const fn split(self) -> Option<Split> {
match self {
Self::Center => None,
Self::Left => Some(Split::Left),
Self::Right => Some(Split::Right),
Self::Top => Some(Split::Above),
Self::Bottom => Some(Split::Below),
}
}
}
#[cfg(test)]
mod tests {
use super::DropZone;
use crate::{AllowedSplits, Split};
use egui::{Rect, pos2};
const LEAF: Rect = Rect::from_min_max(pos2(100.0, 50.0), pos2(500.0, 250.0));
fn at(x: f32, y: f32) -> DropZone {
DropZone::at(LEAF, pos2(x, y), 0.25, AllowedSplits::All)
}
#[test]
fn the_middle_is_the_center() {
assert_eq!(at(300.0, 150.0), DropZone::Center);
assert_eq!(at(201.0, 150.0), DropZone::Center);
assert_eq!(at(399.0, 150.0), DropZone::Center);
assert_eq!(at(300.0, 101.0), DropZone::Center);
assert_eq!(at(300.0, 199.0), DropZone::Center);
}
#[test]
fn each_edge_has_its_band() {
assert_eq!(at(110.0, 150.0), DropZone::Left);
assert_eq!(at(199.0, 150.0), DropZone::Left);
assert_eq!(at(490.0, 150.0), DropZone::Right);
assert_eq!(at(401.0, 150.0), DropZone::Right);
assert_eq!(at(300.0, 55.0), DropZone::Top);
assert_eq!(at(300.0, 99.0), DropZone::Top);
assert_eq!(at(300.0, 245.0), DropZone::Bottom);
assert_eq!(at(300.0, 201.0), DropZone::Bottom);
}
#[test]
fn in_a_corner_the_nearer_edge_wins() {
assert_eq!(at(120.0, 70.0), DropZone::Left);
assert_eq!(at(140.0, 60.0), DropZone::Top);
assert_eq!(at(495.0, 240.0), DropZone::Right);
assert_eq!(at(460.0, 245.0), DropZone::Bottom);
}
#[test]
fn a_pointer_outside_counts_as_on_the_border() {
assert_eq!(at(20.0, 150.0), DropZone::Left);
assert_eq!(at(300.0, 900.0), DropZone::Bottom);
}
#[test]
fn only_allowed_splits_have_bands() {
let near_left_top = pos2(110.0, 55.0);
let pick = |allowed| DropZone::at(LEAF, near_left_top, 0.25, allowed);
assert_eq!(pick(AllowedSplits::LeftRightOnly), DropZone::Left);
assert_eq!(pick(AllowedSplits::TopBottomOnly), DropZone::Top);
assert_eq!(pick(AllowedSplits::None), DropZone::Center);
}
#[test]
fn the_coverage_sets_the_band_width() {
let pointer = pos2(180.0, 150.0); assert_eq!(
DropZone::at(LEAF, pointer, 0.1, AllowedSplits::All),
DropZone::Center
);
assert_eq!(
DropZone::at(LEAF, pointer, 0.3, AllowedSplits::All),
DropZone::Left
);
assert_eq!(
DropZone::at(LEAF, pos2(290.0, 150.0), 2.0, AllowedSplits::All),
DropZone::Left
);
assert_eq!(
DropZone::at(LEAF, pointer, 0.0, AllowedSplits::All),
DropZone::Center
);
}
#[test]
fn an_empty_leaf_is_all_center() {
let empty = Rect::from_min_size(pos2(10.0, 10.0), egui::Vec2::ZERO);
assert_eq!(
DropZone::at(empty, pos2(10.0, 10.0), 0.25, AllowedSplits::All),
DropZone::Center
);
}
const COMPACT: Rect = Rect::from_min_max(pos2(0.0, 0.0), pos2(400.0, 96.0));
const TAB_BAR: Rect = Rect::from_min_max(pos2(0.0, 0.0), pos2(400.0, 28.0));
fn in_compact(y: f32, feel: &crate::OverlayFeel) -> DropZone {
DropZone::in_leaf(
COMPACT,
Some(TAB_BAR),
pos2(200.0, y),
feel,
AllowedSplits::All,
)
}
#[test]
fn under_the_tab_bar_the_stack_band_joins_the_tabs() {
let feel = crate::OverlayFeel::default();
assert_eq!(feel.zone_tab_bar_stack_band, 8.0);
assert_eq!(in_compact(28.0, &feel), DropZone::Center);
assert_eq!(in_compact(35.9, &feel), DropZone::Center);
assert_eq!(in_compact(36.0, &feel), DropZone::Top);
assert_eq!(in_compact(53.9, &feel), DropZone::Top);
assert_eq!(in_compact(54.1, &feel), DropZone::Center);
assert_eq!(in_compact(78.1, &feel), DropZone::Bottom);
}
#[test]
fn the_former_geometry_is_the_whole_leaf() {
let feel = crate::OverlayFeel {
zone_edges_below_tab_bar: false,
zone_tab_bar_stack_band: 0.0,
..Default::default()
};
assert_eq!(in_compact(28.5, &feel), DropZone::Top);
assert_eq!(in_compact(29.0, &feel), DropZone::Center);
}
#[test]
fn bands_below_the_tab_bar_without_a_stack_band() {
let feel = crate::OverlayFeel {
zone_tab_bar_stack_band: 0.0,
..Default::default()
};
assert_eq!(in_compact(28.0, &feel), DropZone::Top);
assert_eq!(in_compact(48.0, &feel), DropZone::Top);
assert_eq!(in_compact(49.0, &feel), DropZone::Center);
}
#[test]
fn without_a_tab_bar_it_is_the_whole_leaf() {
let feel = crate::OverlayFeel::default();
assert_eq!(
DropZone::in_leaf(COMPACT, None, pos2(200.0, 28.5), &feel, AllowedSplits::All),
DropZone::Top
);
}
#[test]
fn zones_map_to_splits() {
assert_eq!(DropZone::Center.split(), None);
assert_eq!(DropZone::Left.split(), Some(Split::Left));
assert_eq!(DropZone::Right.split(), Some(Split::Right));
assert_eq!(DropZone::Top.split(), Some(Split::Above));
assert_eq!(DropZone::Bottom.split(), Some(Split::Below));
}
}