mitsuami_core/ui/
queries.rs1use crate::backend::NativeState;
4use crate::geometry::{Insets, Point, Rect, Size};
5use crate::services::reply_future;
6use crate::widget::{NodeId, Prop, WidgetKind};
7
8use super::{Inner, Node, NodeInfo, Ui, in_chrome};
9
10impl Ui {
11 pub fn windows(&self) -> Vec<NodeId> {
12 self.inner.borrow().windows.clone()
13 }
14
15 pub fn exists(&self, id: NodeId) -> bool {
16 self.inner.borrow().nodes.contains_key(&id)
17 }
18
19 pub fn kind(&self, id: NodeId) -> Option<WidgetKind> {
20 self.inner.borrow().nodes.get(&id).map(|n| n.kind)
21 }
22
23 pub fn props(&self, id: NodeId) -> Vec<Prop> {
24 self.inner.borrow().nodes.get(&id).map(|n| n.props.clone()).unwrap_or_default()
25 }
26
27 pub fn parent(&self, id: NodeId) -> Option<NodeId> {
28 self.inner.borrow().nodes.get(&id).and_then(|n| n.parent)
29 }
30
31 pub fn children(&self, id: NodeId) -> Vec<NodeId> {
32 self.inner.borrow().nodes.get(&id).map(|n| n.children.clone()).unwrap_or_default()
33 }
34
35 pub fn native_children(&self, id: NodeId) -> Vec<NodeId> {
36 self.inner.borrow().nodes.get(&id).map(|n| n.native_children.clone()).unwrap_or_default()
37 }
38
39 pub fn frame(&self, id: NodeId) -> Option<Rect> {
42 let inner = self.inner.borrow();
43 inner.nodes.contains_key(&id).then(|| inner.placed_frame(id))
44 }
45
46 pub fn window_frame(&self, id: NodeId) -> Option<Rect> {
48 let inner = self.inner.borrow();
49 let node = inner.nodes.get(&id)?;
50 if node.kind == WidgetKind::Window {
51 return Some(node.frame);
52 }
53 Some(inner.placed_frame(id).offset(inner.window_origin(id)?))
54 }
55
56 pub fn viewport(&self, id: NodeId) -> Option<Rect> {
59 let insets = self.inner.borrow().nodes.get(&id)?.viewport_insets;
60 Some(self.window_frame(id)?.inset(insets))
61 }
62
63 pub fn viewport_insets(&self, id: NodeId) -> Option<Insets> {
66 let inner = self.inner.borrow();
67 let node = inner.nodes.get(&id)?;
68 (node.kind == WidgetKind::ScrollView).then_some(node.viewport_insets)
69 }
70
71 pub fn visible_rect(&self, id: NodeId) -> Option<Rect> {
75 let window = self.window_of(id)?;
76 let size = self.window_size(window)?;
77 let mut visible = self.window_frame(id)?;
78 let mut clip = Rect::new(0.0, 0.0, size.width, size.height);
79 let mut current = Some(id);
80 while let Some(node) = current {
81 match self.kind(node)? {
82 kind if in_chrome(kind) => clip = self.window_frame(node)?,
83 kind if kind.scrolls() && node != id => visible = visible.intersection(&self.viewport(node)?)?,
84 _ => {}
85 }
86 current = self.inner.borrow().nodes.get(&node)?.native_parent;
87 }
88 visible.intersection(&clip)
89 }
90
91 pub fn window_of(&self, id: NodeId) -> Option<NodeId> {
93 self.inner.borrow().window_of(id)
94 }
95
96 pub fn window_size(&self, window: NodeId) -> Option<Size> {
97 self.inner.borrow().nodes.get(&window).map(|n| n.window_size)
98 }
99
100 pub fn native_state(&self, id: NodeId) -> Option<NativeState> {
101 self.inner.borrow().backend.native_state(id)
102 }
103
104 pub fn capture(
106 &self,
107 id: NodeId,
108 ) -> impl std::future::Future<Output = Result<crate::backend::Image, crate::backend::CaptureError>> + use<> {
109 let (reply, image) = reply_future();
110 self.inner.borrow_mut().backend.capture(id, reply);
111 image
112 }
113
114 pub fn inspect(&self, id: NodeId) -> Option<NodeInfo> {
116 let inner = self.inner.borrow();
117 let origin = inner.window_origin(id)?;
118 Some(inner.inspect(id, origin))
119 }
120}
121
122impl Inner {
123 pub(super) fn window_of(&self, id: NodeId) -> Option<NodeId> {
124 let mut current = Some(id);
125 while let Some(node_id) = current {
126 let node = self.nodes.get(&node_id)?;
127 if node.kind == WidgetKind::Window {
128 return Some(node_id);
129 }
130 current = node.parent;
131 }
132 None
133 }
134
135 fn content_origin(&self, id: NodeId) -> Point {
139 let node = &self.nodes[&id];
140 if node.kind == WidgetKind::Window {
141 return Point::ZERO;
142 }
143 let parent = node.native_parent.map_or(Point::ZERO, |p| self.content_origin(p));
144 Inner::child_origin(node, self.placed_frame(id).offset(parent))
145 }
146
147 fn window_origin(&self, id: NodeId) -> Option<Point> {
149 Some(self.nodes.get(&id)?.native_parent.map_or(Point::ZERO, |p| self.content_origin(p)))
150 }
151
152 pub(super) fn placed_frame(&self, id: NodeId) -> Rect {
159 let node = &self.nodes[&id];
160 let placed_natively = in_chrome(node.kind)
161 || node.native_parent.is_some_and(|p| {
162 matches!(
163 self.nodes[&p].kind,
164 WidgetKind::List | WidgetKind::Table | WidgetKind::Tree | WidgetKind::Tabs
165 )
166 });
167 if !placed_natively {
168 return node.frame;
169 }
170 if self.is_hidden_page(id) {
171 return Rect::ZERO;
172 }
173 let native = self.backend.native_state(id).map(|s| s.frame);
174 if in_chrome(node.kind) && native.is_some_and(|f| f.size.is_empty()) {
178 return Rect::ZERO;
179 }
180 if node.kind == WidgetKind::Sidebar {
181 return native.unwrap_or(Rect::ZERO);
182 }
183 Rect { origin: native.map_or(node.frame.origin, |f| f.origin), size: node.frame.size }
184 }
185
186 pub(super) fn child_origin(node: &Node, frame: Rect) -> Point {
187 match node.kind {
188 WidgetKind::Window => Point::ZERO,
189 _ => {
190 let insets = node.viewport_insets;
191 Point::new(
192 frame.origin.x + insets.left - node.scroll_offset.x,
193 frame.origin.y + insets.top - node.scroll_offset.y,
194 )
195 }
196 }
197 }
198
199 fn inspect(&self, id: NodeId, parent_origin: Point) -> NodeInfo {
200 let node = &self.nodes[&id];
201 let frame =
202 if node.kind == WidgetKind::Window { node.frame } else { self.placed_frame(id).offset(parent_origin) };
203 let origin = Inner::child_origin(node, frame);
204 NodeInfo {
205 id,
206 kind: node.kind,
207 props: node.props.clone(),
208 test_id: node.test_id.clone(),
209 frame,
210 children: node.native_children.iter().map(|c| self.inspect(*c, origin)).collect(),
211 }
212 }
213}