use std::sync::Arc;
use super::element_ref::{ElementRect, ElementRef};
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Visibility {
pub ratio: f32,
pub visible: bool,
pub visible_rect: ElementRect,
pub rect: ElementRect,
pub root_rect: ElementRect,
}
#[derive(Clone, Default)]
pub struct VisibilityOptions {
thresholds: Vec<f32>,
margin: (f32, f32),
root: Option<ElementRef>,
}
impl std::fmt::Debug for VisibilityOptions {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("VisibilityOptions")
.field("thresholds", &self.thresholds)
.field("margin", &self.margin)
.field("root", &self.root.is_some())
.finish()
}
}
impl VisibilityOptions {
pub fn new() -> Self {
Self::default()
}
pub fn thresholds(mut self, thresholds: impl IntoIterator<Item = f32>) -> Self {
self.thresholds = thresholds
.into_iter()
.filter(|share| share.is_finite())
.map(|share| share.clamp(0.0, 1.0))
.collect();
self.thresholds.sort_by(f32::total_cmp);
self.thresholds.dedup();
self
}
pub fn margin(self, px: f32) -> Self {
self.margins(px, px)
}
pub fn margins(mut self, across: f32, down: f32) -> Self {
self.margin = (across, down);
self
}
pub fn root(mut self, root: &ElementRef) -> Self {
self.root = Some(root.clone());
self
}
pub(crate) fn explicit_root(&self) -> Option<&ElementRef> {
self.root.as_ref()
}
pub(crate) fn grown(&self, root: Area) -> Area {
Area {
x: root.x - self.margin.0,
y: root.y - self.margin.1,
width: (root.width + self.margin.0 * 2.0).max(0.0),
height: (root.height + self.margin.1 * 2.0).max(0.0),
}
}
pub(crate) fn level(&self, visibility: &Visibility) -> usize {
if !visibility.visible {
return 0;
}
if self.thresholds.is_empty() {
return 1;
}
self
.thresholds
.iter()
.filter(|share| visibility.ratio >= **share)
.count()
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub(crate) struct Area {
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
}
impl Area {
pub(crate) fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
Self {
x,
y,
width: width.max(0.0),
height: height.max(0.0),
}
}
pub(crate) fn within(self, other: Area) -> Area {
let (x, y) = (self.x.max(other.x), self.y.max(other.y));
let right = (self.x + self.width).min(other.x + other.width);
let bottom = (self.y + self.height).min(other.y + other.height);
Area::new(x, y, right - x, bottom - y)
}
fn rect(self, from: Area) -> ElementRect {
ElementRect {
x: self.x,
y: self.y,
relative_x: self.x - from.x,
relative_y: self.y - from.y,
width: self.width,
height: self.height,
}
}
}
pub(crate) fn measure(element: Area, root: Area) -> Visibility {
let right = (element.x + element.width).min(root.x + root.width);
let bottom = (element.y + element.height).min(root.y + root.height);
let (left, top) = (element.x.max(root.x), element.y.max(root.y));
let shown = Area::new(left, top, right - left, bottom - top);
let area = element.width * element.height;
let (visible, ratio) = if area > 0.0 {
let share = shown.width * shown.height / area;
(share > 0.0, share.clamp(0.0, 1.0))
} else {
let inside = root.width > 0.0 && root.height > 0.0 && right >= left && bottom >= top;
(inside, if inside { 1.0 } else { 0.0 })
};
Visibility {
ratio,
visible,
visible_rect: if visible {
shown.rect(root)
} else {
Area::new(element.x, element.y, 0.0, 0.0).rect(root)
},
rect: element.rect(root),
root_rect: root.rect(root),
}
}
pub(crate) fn hidden() -> Visibility {
Visibility::default()
}
pub(crate) type VisibilityCallback = Arc<dyn Fn(Visibility) + Send + Sync>;
pub(crate) struct Watch {
pub id: u64,
pub options: VisibilityOptions,
pub observer: VisibilityCallback,
pub level: Option<usize>,
}
#[cfg(test)]
mod tests {
use super::*;
fn area(x: f32, y: f32, width: f32, height: f32) -> Area {
Area::new(x, y, width, height)
}
#[test]
fn the_share_is_the_part_of_the_box_inside_the_root() {
let root = area(0.0, 0.0, 100.0, 100.0);
let inside = measure(area(10.0, 10.0, 50.0, 50.0), root);
assert_eq!((inside.ratio, inside.visible), (1.0, true));
let half = measure(area(0.0, 75.0, 100.0, 50.0), root);
assert_eq!((half.ratio, half.visible), (0.5, true));
assert_eq!(
(half.visible_rect.y, half.visible_rect.height),
(75.0, 25.0),
"the part that shows"
);
let below = measure(area(0.0, 100.0, 100.0, 50.0), root);
assert_eq!(
(below.ratio, below.visible),
(0.0, false),
"touching the edge is not showing"
);
}
#[test]
fn a_box_of_no_size_shows_where_it_lies_inside_the_root() {
let root = area(0.0, 0.0, 100.0, 100.0);
assert!(
measure(area(0.0, 100.0, 100.0, 0.0), root).visible,
"at the root's edge"
);
assert_eq!(measure(area(0.0, 40.0, 100.0, 0.0), root).ratio, 1.0);
assert!(!measure(area(0.0, 140.0, 100.0, 0.0), root).visible, "under it");
}
#[test]
fn a_margin_grows_the_root_and_a_level_counts_the_thresholds_crossed() {
let options = VisibilityOptions::new().margin(50.0).thresholds([0.5, 0.0, 1.0, 0.5]);
let root = options.grown(area(0.0, 0.0, 100.0, 100.0));
assert_eq!(root, area(-50.0, -50.0, 200.0, 200.0));
assert_eq!(
options.level(&measure(area(0.0, 200.0, 100.0, 100.0), root)),
0,
"out of it"
);
assert_eq!(
options.level(&measure(area(0.0, 140.0, 100.0, 100.0), root)),
1,
"a tenth in"
);
assert_eq!(
options.level(&measure(area(0.0, 100.0, 100.0, 100.0), root)),
2,
"half in"
);
assert_eq!(
options.level(&measure(area(0.0, 20.0, 100.0, 100.0), root)),
3,
"all in"
);
let plain = VisibilityOptions::new();
assert_eq!(
plain.level(&measure(area(0.0, 99.0, 100.0, 100.0), area(0.0, 0.0, 100.0, 100.0))),
1
);
assert_eq!(
plain.level(&measure(area(0.0, 100.0, 100.0, 100.0), area(0.0, 0.0, 100.0, 100.0))),
0
);
}
}