use perspective_js::utils::global;
use web_sys::*;
const VIEWPORT_PADDING: f64 = 8.0;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ModalSide {
Below,
Beside,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Bounds {
pub top: f64,
pub left: f64,
pub bottom: f64,
pub right: f64,
}
impl From<&DomRect> for Bounds {
fn from(rect: &DomRect) -> Self {
Self {
top: rect.top(),
left: rect.left(),
bottom: rect.bottom(),
right: rect.right(),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ModalPlacement {
pub top: f64,
pub left: f64,
pub max_height: Option<f64>,
pub max_width: Option<f64>,
pub flipped_vertical: bool,
}
struct Axis {
offset: f64,
limit: Option<f64>,
flipped: bool,
}
fn place_axis(start: f64, end: f64, size: f64, extent: f64) -> Axis {
let start = start.clamp(0.0, extent);
let end = end.clamp(0.0, extent);
if start + size <= extent {
Axis {
offset: start,
limit: None,
flipped: false,
}
} else if end - size >= 0.0 {
Axis {
offset: end - size,
limit: None,
flipped: true,
}
} else if extent - start >= end {
Axis {
offset: start,
limit: Some((extent - start - VIEWPORT_PADDING).max(0.0)),
flipped: false,
}
} else {
let limit = (end - VIEWPORT_PADDING).max(0.0);
Axis {
offset: end - limit,
limit: Some(limit),
flipped: true,
}
}
}
pub fn place(
anchor: &Bounds,
size: (f64, f64),
viewport: (f64, f64),
side: ModalSide,
) -> ModalPlacement {
let (horizontal, vertical) = match side {
ModalSide::Below => (
place_axis(anchor.left, anchor.right, size.0, viewport.0),
place_axis(anchor.bottom - 1.0, anchor.top + 1.0, size.1, viewport.1),
),
ModalSide::Beside => (
place_axis(anchor.right, anchor.left, size.0, viewport.0),
place_axis(anchor.top - 1.0, anchor.bottom + 1.0, size.1, viewport.1),
),
};
ModalPlacement {
top: vertical.offset,
left: horizontal.offset,
max_height: vertical.limit,
max_width: horizontal.limit,
flipped_vertical: vertical.flipped,
}
}
fn viewport_size() -> (f64, f64) {
let root = global::document().document_element().unwrap();
(root.client_width() as f64, root.client_height() as f64)
}
fn natural_size(elem: &HtmlElement) -> (f64, f64) {
let rect = elem.get_bounding_client_rect();
(rect.width(), rect.height())
}
fn chrome_size(elem: &HtmlElement) -> (f64, f64) {
let Ok(Some(style)) = global::window().get_computed_style(elem) else {
return (0.0, 0.0);
};
if style.get_property_value("box-sizing").as_deref() == Ok("border-box") {
return (0.0, 0.0);
}
let px = |name: &str| {
style
.get_property_value(name)
.ok()
.and_then(|value| value.trim_end_matches("px").parse::<f64>().ok())
.unwrap_or_default()
};
(
px("padding-left")
+ px("padding-right")
+ px("border-left-width")
+ px("border-right-width"),
px("padding-top")
+ px("padding-bottom")
+ px("border-top-width")
+ px("border-bottom-width"),
)
}
fn set_limit(style: &CssStyleDeclaration, dimension: &str, overflow: &str, limit: Option<f64>) {
match limit {
Some(limit) => {
let _ = style.set_property(dimension, &format!("{}px", limit.max(0.0)));
let _ = style.set_property(overflow, "auto");
},
None => {
let _ = style.remove_property(dimension);
let _ = style.remove_property(overflow);
},
}
}
pub fn apply_placement(elem: &HtmlElement, anchor: &Bounds, side: ModalSide) -> ModalPlacement {
let style = elem.style();
let scroll = (elem.scroll_top(), elem.scroll_left());
set_limit(&style, "max-height", "overflow-y", None);
set_limit(&style, "max-width", "overflow-x", None);
let _ = style.set_property("top", "0px");
let _ = style.set_property("left", "0px");
let viewport = viewport_size();
let size = natural_size(elem);
let chrome = chrome_size(elem);
let mut placement = place(anchor, size, viewport, side);
let max_height = placement.max_height.map(|limit| limit - chrome.1);
if max_height.is_some() {
set_limit(&style, "max-height", "overflow-y", max_height);
let width = natural_size(elem).0;
placement = place(anchor, (width, size.1), viewport, side);
}
let max_width = placement.max_width.map(|limit| limit - chrome.0);
set_limit(&style, "max-height", "overflow-y", max_height);
set_limit(&style, "max-width", "overflow-x", max_width);
let _ = style.set_property("top", &format!("{}px", placement.top));
let _ = style.set_property("left", &format!("{}px", placement.left));
elem.set_scroll_top(scroll.0);
elem.set_scroll_left(scroll.1);
placement
}