use egui::{Pos2, Rect, ViewportId};
use crate::layout::{Laid, Shown, ZoneName};
#[derive(Clone, Debug, Default, PartialEq)]
pub struct WorkArea {
parts: Vec<Part>,
outside: Outside,
}
#[derive(Clone, Debug, PartialEq)]
enum Part {
Space,
Zone(ZoneName),
Rect(Rect),
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum Outside {
#[default]
Cancel,
Native,
}
impl WorkArea {
pub fn window() -> Self {
Self::default()
}
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn space(mut self) -> Self {
self.parts.push(Part::Space);
self
}
#[must_use]
pub fn zone(mut self, name: impl Into<String>) -> Self {
self.parts.push(Part::Zone(ZoneName(name.into())));
self
}
#[must_use]
pub fn rect(mut self, rect: Rect) -> Self {
self.parts.push(Part::Rect(rect));
self
}
#[must_use]
pub fn outside(mut self, outside: Outside) -> Self {
self.outside = outside;
self
}
pub fn is_window(&self) -> bool {
self.parts.is_empty()
}
pub(crate) fn outside_rule(&self) -> Outside {
self.outside
}
}
#[derive(Clone, Debug, Default)]
pub(crate) struct BoundZones {
pub(crate) root: Option<Rect>,
pub(crate) open: Vec<(ZoneName, Rect)>,
}
impl BoundZones {
pub(crate) fn of(laid: &Laid) -> Self {
Self {
root: laid.zones.first().map(|zone| zone.rect),
open: laid
.zones
.iter()
.filter(|zone| matches!(zone.shown, Shown::Open | Shown::Over))
.map(|zone| (zone.name.clone(), zone.rect))
.collect(),
}
}
}
impl WorkArea {
pub(crate) fn regions(
&self,
zones: &BoundZones,
docks: impl Iterator<Item = Rect> + Clone,
) -> Option<Vec<Rect>> {
if self.is_window() {
return None;
}
let regions: Vec<Rect> = self
.parts
.iter()
.filter_map(|part| match part {
Part::Space => zones
.root
.or_else(|| docks.clone().reduce(|union, rect| union.union(rect))),
Part::Zone(name) => zones
.open
.iter()
.find(|(zone, _)| zone == name)
.map(|(_, rect)| *rect),
Part::Rect(rect) => Some(*rect),
})
.filter(|rect| rect.is_positive())
.collect();
(!regions.is_empty()).then_some(regions)
}
}
pub(crate) fn region_of(regions: &[Rect], rect: Rect, pointer: Option<Pos2>) -> Option<Rect> {
if let Some(pointer) = pointer
&& let Some(region) = regions.iter().find(|region| region.contains(pointer))
{
return Some(*region);
}
let overlap = |region: &Rect| {
let common = region.intersect(rect);
if common.is_positive() {
common.area()
} else {
0.0
}
};
regions
.iter()
.copied()
.filter(|region| overlap(region) > 0.0)
.max_by(|a, b| overlap(a).total_cmp(&overlap(b)))
.or_else(|| {
regions.iter().copied().min_by(|a, b| {
a.distance_sq_to_pos(rect.center())
.total_cmp(&b.distance_sq_to_pos(rect.center()))
})
})
}
pub(crate) fn main_window(viewport: Option<ViewportId>) -> bool {
viewport.is_none_or(|viewport| viewport == ViewportId::ROOT)
}
#[cfg(test)]
mod tests {
use egui::{pos2, vec2};
use super::*;
const LEFT: Rect = Rect::from_min_max(pos2(0.0, 0.0), pos2(100.0, 100.0));
const RIGHT: Rect = Rect::from_min_max(pos2(150.0, 0.0), pos2(250.0, 100.0));
#[test]
fn the_window_has_no_region() {
assert_eq!(
WorkArea::window().regions(&BoundZones::default(), std::iter::empty()),
None
);
assert!(WorkArea::new().is_window());
}
#[test]
fn a_zone_not_open_is_no_region_and_none_left_is_unrestricted() {
let area = WorkArea::new().zone("side");
assert_eq!(
area.regions(&BoundZones::default(), std::iter::empty()),
None
);
let zones = BoundZones {
root: None,
open: vec![(ZoneName("side".into()), LEFT)],
};
assert_eq!(area.regions(&zones, std::iter::empty()), Some(vec![LEFT]));
}
#[test]
fn the_space_without_bind_is_the_union_of_its_docks() {
let area = WorkArea::new().space();
assert_eq!(
area.regions(&BoundZones::default(), [LEFT, RIGHT].into_iter()),
Some(vec![LEFT.union(RIGHT)])
);
}
#[test]
fn a_float_moved_goes_to_the_region_under_the_pointer() {
let float = Rect::from_min_size(pos2(10.0, 10.0), vec2(50.0, 50.0));
assert_eq!(
region_of(&[LEFT, RIGHT], float, Some(pos2(200.0, 50.0))),
Some(RIGHT)
);
}
#[test]
fn over_a_gap_a_float_stays_in_its_region_without_flicker() {
let mut float = Rect::from_min_size(pos2(150.0, 10.0), vec2(50.0, 50.0));
for _ in 0..5 {
let region = region_of(&[LEFT, RIGHT], float, Some(pos2(125.0, 50.0)));
assert_eq!(region, Some(RIGHT));
float = float.translate(vec2(-10.0, 0.0));
float = float.translate(vec2((RIGHT.left() - float.left()).max(0.0), 0.0));
}
}
#[test]
fn a_float_in_no_region_goes_to_the_nearest() {
let float = Rect::from_min_size(pos2(300.0, 10.0), vec2(20.0, 20.0));
assert_eq!(region_of(&[LEFT, RIGHT], float, None), Some(RIGHT));
let straddling = Rect::from_min_max(pos2(80.0, 0.0), pos2(160.0, 50.0));
assert_eq!(region_of(&[LEFT, RIGHT], straddling, None), Some(LEFT));
}
}