use js_sys::Promise;
pub use perspective_client::config::viewer::{Layout, Orientation, SplitLayout, TabLayout};
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = web_sys::HtmlElement)]
pub type RegularLayout;
#[wasm_bindgen(method, js_name = "insertPanel")]
pub fn insert_panel(this: &RegularLayout, name: &str, path: JsValue, split: JsValue)
-> Promise;
#[wasm_bindgen(method, js_name = "removePanel")]
pub fn remove_panel(this: &RegularLayout, name: &str) -> Promise;
#[wasm_bindgen(method)]
pub fn clear(this: &RegularLayout) -> Promise;
#[wasm_bindgen(method, js_name = "calculatePath")]
pub fn calculate_path(this: &RegularLayout, name: &str) -> JsValue;
#[wasm_bindgen(method, js_name = "calculateIntersect")]
pub fn calculate_intersect(this: &RegularLayout, coordinates: &JsValue) -> JsValue;
#[wasm_bindgen(method, js_name = "setOverlayState")]
pub fn set_overlay_state(
this: &RegularLayout,
coordinates: &JsValue,
target: &JsValue,
) -> Promise;
#[wasm_bindgen(method, js_name = "clearOverlayState")]
pub fn clear_overlay_state(
this: &RegularLayout,
coordinates: &JsValue,
target: &JsValue,
) -> Promise;
#[wasm_bindgen(method, js_name = "diffCoordinates")]
pub fn diff_coordinates(this: &RegularLayout, coordinates: &JsValue, target: &JsValue) -> f64;
#[wasm_bindgen(method, js_name = "realCoordinates")]
pub fn real_coordinates(this: &RegularLayout, window: &JsValue) -> JsValue;
#[wasm_bindgen(method, js_name = "restorePhysics")]
pub fn restore_physics(this: &RegularLayout, physics: &JsValue);
#[wasm_bindgen(method, js_name = "savePhysics")]
pub fn save_physics(this: &RegularLayout) -> JsValue;
#[wasm_bindgen(method, js_name = "resumeResize")]
pub fn resume_resize(this: &RegularLayout);
#[wasm_bindgen(method)]
pub fn save(this: &RegularLayout) -> JsValue;
#[wasm_bindgen(method)]
pub fn restore(this: &RegularLayout, layout: &JsValue) -> Promise;
#[wasm_bindgen(method, js_name = "restoreSync")]
pub fn restore_sync(this: &RegularLayout, layout: &JsValue);
#[wasm_bindgen(method)]
pub fn select(this: &RegularLayout, name: &str) -> Promise;
#[wasm_bindgen(method)]
pub fn maximize(this: &RegularLayout, name: &str);
#[wasm_bindgen(method)]
pub fn minimize(this: &RegularLayout);
pub type PresizeDetail;
#[wasm_bindgen(method, js_name = "calculatePresizePaths")]
pub fn calculate_presize_paths(this: &PresizeDetail) -> JsValue;
#[wasm_bindgen(method, getter)]
pub fn overlay(this: &PresizeDetail) -> JsValue;
}
impl RegularLayout {
pub const BEFORE_RESIZE_EVENT: &'static str = "regular-layout-before-resize";
pub const BEFORE_UPDATE_EVENT: &'static str = "regular-layout-before-update";
pub const FRAME_TAG_NAME: &'static str = "regular-layout-frame";
pub const SELECT_EVENT: &'static str = "regular-layout-select";
pub const TAG_NAME: &'static str = "regular-layout";
pub const UPDATE_EVENT: &'static str = "regular-layout-update";
pub fn contains_panel(&self, name: &str) -> bool {
let path = self.calculate_path(name);
!path.is_null() && !path.is_undefined()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub struct ViewWindow {
pub row_start: f64,
pub row_end: f64,
pub col_start: f64,
pub col_end: f64,
}
pub trait LayoutExt {
fn insert_split(&self, index: usize, name: &str, orientation: Orientation) -> Layout;
fn view_window(&self, name: &str) -> Option<ViewWindow>;
#[doc(hidden)]
fn view_window_in(&self, name: &str, window: ViewWindow) -> Option<ViewWindow>;
}
impl LayoutExt for Layout {
fn insert_split(&self, index: usize, name: &str, orientation: Orientation) -> Layout {
let leaf = Layout::Tab(TabLayout {
tabs: vec![name.to_owned()],
selected: None,
});
match self {
Layout::Split(split) if split.orientation == orientation => {
let index = index.min(split.children.len());
let mut children = split.children.clone();
let mut sizes = split.sizes.clone();
children.insert(index, leaf);
sizes.insert(index, 1.0 / (children.len() - 1).max(1) as f64);
let total: f64 = sizes.iter().sum();
let sizes = if total.is_finite() && total > 0.0 {
sizes.iter().map(|size| size / total).collect()
} else {
vec![1.0 / children.len() as f64; children.len()]
};
Layout::Split(SplitLayout {
children,
sizes,
orientation,
})
},
_ => Layout::Split(SplitLayout {
children: if index == 0 {
vec![leaf, self.clone()]
} else {
vec![self.clone(), leaf]
},
sizes: vec![0.5, 0.5],
orientation,
}),
}
}
fn view_window(&self, name: &str) -> Option<ViewWindow> {
self.view_window_in(name, ViewWindow {
row_start: 0.0,
row_end: 1.0,
col_start: 0.0,
col_end: 1.0,
})
}
fn view_window_in(&self, name: &str, window: ViewWindow) -> Option<ViewWindow> {
match self {
Layout::Tab(tab) => {
let selected = tab.tabs.get(tab.selected.unwrap_or(0));
(selected.map(String::as_str) == Some(name)).then_some(window)
},
Layout::Split(split) => {
let vertical = split.orientation == Orientation::Vertical;
let (start, end) = if vertical {
(window.row_start, window.row_end)
} else {
(window.col_start, window.col_end)
};
let mut pos = start;
for (child, size) in split.children.iter().zip(&split.sizes) {
let next = pos + (end - start) * size;
let child_window = if vertical {
ViewWindow {
row_start: pos,
row_end: next,
..window
}
} else {
ViewWindow {
col_start: pos,
col_end: next,
..window
}
};
if let Some(found) = child.view_window_in(name, child_window) {
return Some(found);
}
pos = next;
}
None
},
}
}
}