Skip to main content

mitsuami_core/ui/
queries.rs

1//! Reading the tree: nodes, their frames and windows, and inspection.
2
3use 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    /// Frame relative to the native parent, as last sent to the backend.
40    /// A list's rows are where the platform put them, at the size sent.
41    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    /// Frame in the coordinates of the node's window.
47    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    /// Where a `ScrollView` shows its content, in window coordinates: its
57    /// frame less the room its scroll bars take, if the platform's do.
58    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    /// Where a `ScrollView`'s viewport is in its frame, as the platform
64    /// last reported it.
65    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    /// The part of a node that can be seen: its window frame, clipped by
72    /// every enclosing scroll view's viewport and by the window (or, in the toolbar,
73    /// by its toolbar item; the sidebar, by itself). `None` if nothing is.
74    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    /// The window a node is attached to, if any.
92    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    /// An offscreen screenshot of a window or node.
105    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    /// The native tree under `id`, with window-coordinate frames.
115    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    /// Window coordinates of the point `id`'s children are positioned
136    /// from: its own top-left (a scroll view's viewport's), moved by its
137    /// scroll offset.
138    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    /// Window coordinates of the origin `id`'s frame is relative to.
148    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    /// A node's frame in its native parent. A list's rows, a table's cells and a window's
153    /// toolbar items are where the platform placed them (as its
154    /// `native_state` says), at the size the core sent; toolbar items the
155    /// platform hides are empty. A window's sidebar is where the platform
156    /// placed it, at the size it gave it. So is a tab view's page, at the
157    /// size the core gave it; the pages it doesn't show are empty.
158    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!(self.nodes[&p].kind, WidgetKind::List | WidgetKind::Table | WidgetKind::Tabs)
163            });
164        if !placed_natively {
165            return node.frame;
166        }
167        if self.is_hidden_page(id) {
168            return Rect::ZERO;
169        }
170        let native = self.backend.native_state(id).map(|s| s.frame);
171        // A toolbar may hide an item that doesn't fit (in an overflow
172        // menu, on AppKit and WinUI), and a window its sidebar (collapsed,
173        // or a page of its own in a narrow window): it isn't shown at all.
174        if in_chrome(node.kind) && native.is_some_and(|f| f.size.is_empty()) {
175            return Rect::ZERO;
176        }
177        if node.kind == WidgetKind::Sidebar {
178            return native.unwrap_or(Rect::ZERO);
179        }
180        Rect { origin: native.map_or(node.frame.origin, |f| f.origin), size: node.frame.size }
181    }
182
183    pub(super) fn child_origin(node: &Node, frame: Rect) -> Point {
184        match node.kind {
185            WidgetKind::Window => Point::ZERO,
186            _ => {
187                let insets = node.viewport_insets;
188                Point::new(
189                    frame.origin.x + insets.left - node.scroll_offset.x,
190                    frame.origin.y + insets.top - node.scroll_offset.y,
191                )
192            }
193        }
194    }
195
196    fn inspect(&self, id: NodeId, parent_origin: Point) -> NodeInfo {
197        let node = &self.nodes[&id];
198        let frame =
199            if node.kind == WidgetKind::Window { node.frame } else { self.placed_frame(id).offset(parent_origin) };
200        let origin = Inner::child_origin(node, frame);
201        NodeInfo {
202            id,
203            kind: node.kind,
204            props: node.props.clone(),
205            test_id: node.test_id.clone(),
206            frame,
207            children: node.native_children.iter().map(|c| self.inspect(*c, origin)).collect(),
208        }
209    }
210}