use fret_core::{AppWindowId, NodeId, Rect};
use super::with_window_state;
use super::{ElementContext, ElementRuntime, GlobalElementId};
use crate::UiHost;
pub fn with_element_cx<H: UiHost, R>(
app: &mut H,
window: AppWindowId,
bounds: Rect,
root_name: &str,
f: impl FnOnce(&mut ElementContext<'_, H>) -> R,
) -> R {
app.with_global_mut_untracked(ElementRuntime::new, |runtime, app| {
let mut cx = ElementContext::new_for_root_name(app, runtime, window, bounds, root_name);
f(&mut cx)
})
}
pub fn root_bounds_for_element<H: UiHost>(
app: &mut H,
window: AppWindowId,
element: GlobalElementId,
) -> Option<Rect> {
app.with_global_mut_untracked(ElementRuntime::new, |runtime, _app| {
let state = runtime.for_window_mut(window);
let root = state.node_entry(element).map(|e| e.root)?;
state.root_bounds(root)
})
}
pub fn node_for_element<H: UiHost>(
app: &mut H,
window: AppWindowId,
element: GlobalElementId,
) -> Option<NodeId> {
with_window_state(app, window, |st| st.node_entry(element).map(|e| e.node))
}
pub fn live_node_for_element<H: UiHost>(
app: &mut H,
window: AppWindowId,
element: GlobalElementId,
) -> Option<NodeId> {
let frame_id = app.frame_id();
with_window_state(app, window, |st| {
st.node_entry(element)
.and_then(|entry| (entry.last_seen_frame == frame_id).then_some(entry.node))
})
}
pub fn peek_node_for_element<H: UiHost>(
app: &mut H,
window: AppWindowId,
element: GlobalElementId,
) -> Option<NodeId> {
app.with_global_mut_untracked(ElementRuntime::new, |runtime, _app| {
let state = runtime.for_window(window)?;
state.node_entry(element).map(|e| e.node)
})
}
pub fn element_is_live_in_current_frame<H: UiHost>(
app: &mut H,
window: AppWindowId,
element: GlobalElementId,
) -> bool {
let frame_id = app.frame_id();
with_window_state(app, window, |st| {
st.node_entry(element)
.map(|e| e.last_seen_frame == frame_id)
.unwrap_or(false)
})
}
pub fn element_identity_is_live_in_current_frame<H: UiHost>(
app: &mut H,
window: AppWindowId,
element: GlobalElementId,
) -> bool {
with_window_state(app, window, |st| {
st.element_identity_is_live_in_current_frame(element)
})
}
pub fn bounds_for_element<H: UiHost>(
app: &mut H,
window: AppWindowId,
element: GlobalElementId,
) -> Option<Rect> {
with_window_state(app, window, |st| st.last_bounds(element))
}
pub fn current_bounds_for_element<H: UiHost>(
app: &mut H,
window: AppWindowId,
element: GlobalElementId,
) -> Option<Rect> {
with_window_state(app, window, |st| st.current_bounds(element))
}
pub fn visual_bounds_for_element<H: UiHost>(
app: &mut H,
window: AppWindowId,
element: GlobalElementId,
) -> Option<Rect> {
with_window_state(app, window, |st| {
st.last_visual_bounds(element)
.or_else(|| st.last_bounds(element))
})
}
pub fn current_visual_bounds_for_element<H: UiHost>(
app: &mut H,
window: AppWindowId,
element: GlobalElementId,
) -> Option<Rect> {
with_window_state(app, window, |st| {
st.current_visual_bounds(element)
.or_else(|| st.current_bounds(element))
})
}
pub(crate) fn record_bounds_for_element<H: UiHost>(
app: &mut H,
window: AppWindowId,
element: GlobalElementId,
bounds: Rect,
) {
with_window_state(app, window, |st| st.record_bounds(element, bounds));
}
pub(crate) fn record_visual_bounds_for_element<H: UiHost>(
app: &mut H,
window: AppWindowId,
element: GlobalElementId,
bounds: Rect,
) {
with_window_state(app, window, |st| st.record_visual_bounds(element, bounds));
}