fret_ui/elements/
queries.rs1use fret_core::{AppWindowId, NodeId, Rect};
2
3use super::with_window_state;
4use super::{ElementContext, ElementRuntime, GlobalElementId};
5use crate::UiHost;
6
7pub fn with_element_cx<H: UiHost, R>(
8 app: &mut H,
9 window: AppWindowId,
10 bounds: Rect,
11 root_name: &str,
12 f: impl FnOnce(&mut ElementContext<'_, H>) -> R,
13) -> R {
14 app.with_global_mut_untracked(ElementRuntime::new, |runtime, app| {
15 let mut cx = ElementContext::new_for_root_name(app, runtime, window, bounds, root_name);
16 f(&mut cx)
17 })
18}
19
20pub fn root_bounds_for_element<H: UiHost>(
21 app: &mut H,
22 window: AppWindowId,
23 element: GlobalElementId,
24) -> Option<Rect> {
25 app.with_global_mut_untracked(ElementRuntime::new, |runtime, _app| {
26 let state = runtime.for_window_mut(window);
27 let root = state.node_entry(element).map(|e| e.root)?;
28 state.root_bounds(root)
29 })
30}
31
32pub fn node_for_element<H: UiHost>(
38 app: &mut H,
39 window: AppWindowId,
40 element: GlobalElementId,
41) -> Option<NodeId> {
42 with_window_state(app, window, |st| st.node_entry(element).map(|e| e.node))
43}
44
45pub fn live_node_for_element<H: UiHost>(
54 app: &mut H,
55 window: AppWindowId,
56 element: GlobalElementId,
57) -> Option<NodeId> {
58 let frame_id = app.frame_id();
59 with_window_state(app, window, |st| {
60 st.node_entry(element)
61 .and_then(|entry| (entry.last_seen_frame == frame_id).then_some(entry.node))
62 })
63}
64
65pub fn peek_node_for_element<H: UiHost>(
73 app: &mut H,
74 window: AppWindowId,
75 element: GlobalElementId,
76) -> Option<NodeId> {
77 app.with_global_mut_untracked(ElementRuntime::new, |runtime, _app| {
78 let state = runtime.for_window(window)?;
79 state.node_entry(element).map(|e| e.node)
80 })
81}
82
83pub fn element_is_live_in_current_frame<H: UiHost>(
88 app: &mut H,
89 window: AppWindowId,
90 element: GlobalElementId,
91) -> bool {
92 let frame_id = app.frame_id();
93 with_window_state(app, window, |st| {
94 st.node_entry(element)
95 .map(|e| e.last_seen_frame == frame_id)
96 .unwrap_or(false)
97 })
98}
99
100pub fn element_identity_is_live_in_current_frame<H: UiHost>(
108 app: &mut H,
109 window: AppWindowId,
110 element: GlobalElementId,
111) -> bool {
112 with_window_state(app, window, |st| {
113 st.element_identity_is_live_in_current_frame(element)
114 })
115}
116
117pub fn bounds_for_element<H: UiHost>(
123 app: &mut H,
124 window: AppWindowId,
125 element: GlobalElementId,
126) -> Option<Rect> {
127 with_window_state(app, window, |st| st.last_bounds(element))
128}
129
130pub fn current_bounds_for_element<H: UiHost>(
137 app: &mut H,
138 window: AppWindowId,
139 element: GlobalElementId,
140) -> Option<Rect> {
141 with_window_state(app, window, |st| st.current_bounds(element))
142}
143
144pub fn visual_bounds_for_element<H: UiHost>(
150 app: &mut H,
151 window: AppWindowId,
152 element: GlobalElementId,
153) -> Option<Rect> {
154 with_window_state(app, window, |st| {
155 st.last_visual_bounds(element)
156 .or_else(|| st.last_bounds(element))
157 })
158}
159
160pub fn current_visual_bounds_for_element<H: UiHost>(
163 app: &mut H,
164 window: AppWindowId,
165 element: GlobalElementId,
166) -> Option<Rect> {
167 with_window_state(app, window, |st| {
168 st.current_visual_bounds(element)
169 .or_else(|| st.current_bounds(element))
170 })
171}
172
173pub(crate) fn record_bounds_for_element<H: UiHost>(
174 app: &mut H,
175 window: AppWindowId,
176 element: GlobalElementId,
177 bounds: Rect,
178) {
179 with_window_state(app, window, |st| st.record_bounds(element, bounds));
180}
181
182pub(crate) fn record_visual_bounds_for_element<H: UiHost>(
183 app: &mut H,
184 window: AppWindowId,
185 element: GlobalElementId,
186 bounds: Rect,
187) {
188 with_window_state(app, window, |st| st.record_visual_bounds(element, bounds));
189}