mitsuami_appkit/backend/
handle.rs1use mitsuami_core::{Command, NativeAppInfo, NativeIcon, NodeId, Size, WidgetKind};
4use objc2::Message;
5use objc2::rc::Retained;
6use objc2_app_kit::{NSApplication, NSView};
7use objc2_foundation::{NSBundle, NSDate, NSDefaultRunLoopMode, NSProcessInfo, NSRunLoop};
8
9use super::{AppKitHandle, State, Widget};
10
11impl AppKitHandle {
12 pub(crate) fn app_name(&self) -> String {
15 let name = self.state.borrow().app_name.clone();
16 name.unwrap_or_else(|| NSProcessInfo::processInfo().processName().to_string())
17 }
18
19 pub fn command_log(&self) -> Vec<Command> {
20 self.state.borrow().log.clone()
21 }
22
23 pub fn take_command_log(&self) -> Vec<Command> {
24 std::mem::take(&mut self.state.borrow_mut().log)
25 }
26
27 pub fn node_count(&self) -> usize {
29 self.state.borrow().nodes.len()
30 }
31
32 pub fn ns_view(&self, id: NodeId) -> Option<Retained<NSView>> {
34 let state = self.state.borrow();
35 let view = state.nodes.get(&id)?.widget.view();
36 Some(view.retain())
37 }
38}
39
40impl mitsuami_core::TestHooks for AppKitHandle {
41 fn name(&self) -> &'static str {
42 "appkit"
43 }
44
45 fn dragged_files(&self, node: NodeId) -> Option<Vec<std::path::PathBuf>> {
46 let state = self.state.borrow();
47 let host = state.nodes.get(&node)?;
48 match &state.nodes.get(&host.parent?)?.widget {
49 Widget::List(list) => list.dragged_files(host.row?),
50 _ => None,
51 }
52 }
53
54 fn resize_window(&self, window: NodeId, size: Size) {
55 AppKitHandle::resize_window(self, window, size);
56 }
57
58 fn close_window(&self, window: NodeId) {
60 if let Some(window) = self.ns_window(window) {
61 window.performClose(None);
62 }
63 }
64
65 fn take_command_log(&self) -> Vec<Command> {
66 AppKitHandle::take_command_log(self)
67 }
68
69 fn node_count(&self) -> usize {
70 AppKitHandle::node_count(self)
71 }
72
73 fn app_info(&self, _window: NodeId) -> NativeAppInfo {
76 let mtm = self.state.borrow().mtm;
77 let id = NSBundle::mainBundle().bundleIdentifier().map(|id| id.to_string());
78 let icon = NSApplication::sharedApplication(mtm).applicationIconImage().map(|image| {
80 let size = image.size();
81 NativeIcon::Image { width: size.width.round() as u32, height: size.height.round() as u32 }
82 });
83 NativeAppInfo { id, name: self.state.borrow().app_name.clone(), icon }
84 }
85
86 fn settle(&self) {
92 if self.state.borrow().nodes.values().any(|n| n.kind == WidgetKind::SearchInput) {
93 NSRunLoop::currentRunLoop().limitDateForMode(unsafe { NSDefaultRunLoopMode });
94 }
95 let deadline = std::time::Instant::now() + std::time::Duration::from_secs(2);
98 while super::images::thumbnails_pending() && std::time::Instant::now() < deadline {
99 let until = NSDate::dateWithTimeIntervalSinceNow(0.01);
100 NSRunLoop::currentRunLoop().runMode_beforeDate(unsafe { NSDefaultRunLoopMode }, &until);
101 }
102 let state = self.state.borrow();
103 state.layout_lists();
104 state.layout_toolbars();
105 }
106}
107
108impl State {
109 pub(super) fn layout_toolbars(&self) {
113 for node in self.nodes.values() {
114 layout_toolbar(&node.widget);
115 }
116 }
117
118 pub(super) fn layout_lists(&self) {
123 for node in self.nodes.values() {
124 layout_list(&node.widget);
125 }
126 }
127
128 pub(super) fn note_touched(&mut self, command: &Command) {
134 let ids = match command {
135 Command::Create { id, .. }
136 | Command::SetProp { id, .. }
137 | Command::SetFrame { id, .. }
138 | Command::SetWindowSize { id, .. }
139 | Command::ScrollTo { id, .. }
140 | Command::ScrollToRow { id, .. } => [Some(*id), None],
141 Command::Insert { parent, child, .. } | Command::Remove { parent, child } => [Some(*parent), Some(*child)],
142 _ => return,
143 };
144 for id in ids.into_iter().flatten() {
145 let Some(node) = self.nodes.get(&id) else { continue };
146 match &node.widget {
147 Widget::List(_) => {
148 self.touched_lists.insert(id);
149 }
150 Widget::Window { .. } => {
151 self.touched_windows.insert(id);
152 }
153 Widget::Tabs(_) | Widget::Scroll(_) => self.touched_all_lists = true,
154 _ => {}
155 }
156 let parent = node.parent.and_then(|p| Some((p, &self.nodes.get(&p)?.widget)));
157 match parent {
158 Some((list, Widget::List(_))) => {
160 self.touched_lists.insert(list);
161 }
162 Some((window, Widget::Window { .. })) => {
164 self.touched_windows.insert(window);
165 }
166 _ => {}
167 }
168 if let Some(window) = self.toolbar_item_above(id).and_then(|item| self.nodes[&item].parent) {
170 self.touched_windows.insert(window);
171 }
172 }
173 }
174
175 pub(super) fn layout_touched(&mut self) {
179 if std::mem::take(&mut self.touched_all_lists) {
180 self.touched_lists.clear();
181 self.layout_lists();
182 }
183 for id in std::mem::take(&mut self.touched_lists) {
184 if let Some(node) = self.nodes.get(&id) {
185 layout_list(&node.widget);
186 }
187 }
188 for id in std::mem::take(&mut self.touched_windows) {
189 if let Some(node) = self.nodes.get(&id) {
190 layout_toolbar(&node.widget);
191 }
192 }
193 }
194}
195
196fn layout_toolbar(widget: &Widget) {
197 if let Widget::Window { window, toolbar, split, .. } = widget
198 && (toolbar.is_some() || split.is_some())
199 {
200 window.layoutIfNeeded();
201 }
202}
203
204fn layout_list(widget: &Widget) {
205 if let Widget::List(list) = widget {
206 list.table.setNeedsLayout(true);
209 list.scroll.layoutSubtreeIfNeeded();
210 }
211}