mitsuami_appkit/backend/
windows.rs1use block2::RcBlock;
4use mitsuami_core::{Modality, NodeId, Size};
5use objc2::rc::Retained;
6use objc2_app_kit::{NSApplication, NSEvent, NSEventModifierFlags, NSEventType, NSWindow, NSWindowStyleMask};
7use objc2_core_foundation::{CFRunLoop, kCFRunLoopCommonModes};
8use objc2_foundation::{NSPoint, NSSize};
9
10use super::{AppKitHandle, Widget};
11
12impl AppKitHandle {
13 pub fn resize_window(&self, window: NodeId, size: Size) {
17 let state = self.state.borrow();
18 let Some(Widget::Window { window, _delegate, .. }) = state.nodes.get(&window).map(|n| &n.widget) else {
19 return;
20 };
21 if !window.styleMask().contains(NSWindowStyleMask::Resizable) {
23 return;
24 }
25 let (window, extra) = (window.clone(), _delegate.extra(window));
26 drop(state);
27 let (min, max) = (window.contentMinSize(), window.contentMaxSize());
30 window.setContentSize(NSSize::new(
31 (size.width as f64 + extra.width).min(max.width).max(min.width),
32 (size.height as f64 + extra.height).min(max.height).max(min.height),
33 ));
34 }
35
36 pub fn show_pending_windows(&self) {
40 let (pending, activate) = {
41 let mut state = self.state.borrow_mut();
42 (std::mem::take(&mut state.pending_show), std::mem::take(&mut state.activate_on_show))
43 };
44 for id in pending {
45 let Some(window) = self.ns_window(id) else { continue };
46 let modal = self.state.borrow().nodes.get(&id).and_then(|n| n.modal);
47 let owner = modal.and_then(|(owner, _)| owner).and_then(|owner| self.ns_window(owner));
48 match (modal.map(|(_, modality)| modality), owner) {
49 (Some(Modality::Window), Some(owner)) => owner.beginSheet_completionHandler(&window, None),
50 (Some(_), owner) => {
51 match owner {
52 Some(owner) => centre_on(&window, &owner),
53 None => window.center(),
54 }
55 self.run_modal(id, window.clone());
56 }
57 (None, _) => {
58 let framed = matches!(
63 self.state.borrow().nodes.get(&id).map(|n| &n.widget),
64 Some(Widget::Window { _delegate, .. }) if _delegate.framed() || _delegate.zoomed(&window)
65 );
66 if !framed {
67 window.center();
68 }
69 if activate {
73 NSApplication::sharedApplication(self.state.borrow().mtm).activate();
74 }
75 window.makeKeyAndOrderFront(None);
76 }
77 }
78 if let Some(Widget::Window { _delegate, .. }) = self.state.borrow().nodes.get(&id).map(|n| &n.widget) {
80 _delegate.apply_full_screen(&window);
81 _delegate.report_frame(&window);
82 }
83 let waiting = {
85 let mut state = self.state.borrow_mut();
86 let (waiting, kept) = std::mem::take(&mut state.on_shown).into_iter().partition(|(w, _)| *w == id);
87 state.on_shown = kept;
88 waiting
89 };
90 for (_, run) in waiting {
91 run();
92 }
93 }
94 }
95
96 pub(crate) fn activate_on_show(&self) {
99 self.state.borrow_mut().activate_on_show = true;
100 }
101
102 pub(crate) fn when_shown(&self, on: Option<NodeId>, run: impl FnOnce() + 'static) {
110 let mut state = self.state.borrow_mut();
111 match on.filter(|id| state.pending_show.contains(id)) {
112 Some(id) => state.on_shown.push((id, Box::new(run))),
113 None => {
114 drop(state);
115 run();
116 }
117 }
118 }
119
120 fn run_modal(&self, id: NodeId, window: Retained<NSWindow>) {
128 let state = self.state.clone();
129 let block = RcBlock::new(move || {
130 if !state.borrow().nodes.contains_key(&id) {
132 return;
133 }
134 let mtm = state.borrow().mtm;
135 NSApplication::sharedApplication(mtm).runModalForWindow(&window);
136 });
137 if let Some(run_loop) = CFRunLoop::main() {
138 unsafe { run_loop.perform_block(kCFRunLoopCommonModes.map(|m| &**m), Some(&block)) };
139 run_loop.wake_up();
140 }
141 }
142
143 pub fn ns_window(&self, id: NodeId) -> Option<Retained<NSWindow>> {
145 match &self.state.borrow().nodes.get(&id)?.widget {
146 Widget::Window { window, .. } => Some(window.clone()),
147 _ => None,
148 }
149 }
150
151 pub(crate) fn window_node(&self, ns_window: &NSWindow) -> Option<NodeId> {
153 self.state.borrow().nodes.iter().find_map(|(id, node)| match &node.widget {
154 Widget::Window { window, .. } if std::ptr::eq(&**window, ns_window) => Some(*id),
155 _ => None,
156 })
157 }
158}
159
160fn centre_on(window: &NSWindow, owner: &NSWindow) {
162 let (own, frame) = (owner.frame(), window.frame());
163 window.setFrameOrigin(NSPoint::new(
164 (own.origin.x + (own.size.width - frame.size.width) / 2.0).round(),
165 (own.origin.y + (own.size.height - frame.size.height) / 2.0).round(),
166 ));
167}
168
169pub(super) fn soon(run: Box<dyn FnOnce()>) {
171 let run = std::cell::Cell::new(Some(run));
172 let block = RcBlock::new(move || {
173 if let Some(run) = run.take() {
174 run();
175 }
176 });
177 if let Some(run_loop) = CFRunLoop::main() {
178 unsafe { run_loop.perform_block(kCFRunLoopCommonModes.map(|m| &**m), Some(&block)) };
179 run_loop.wake_up();
180 }
181}
182
183pub(super) fn post_empty_event(app: &NSApplication) {
185 let event = NSEvent::otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2(
186 NSEventType::ApplicationDefined,
187 NSPoint::new(0.0, 0.0),
188 NSEventModifierFlags::empty(),
189 0.0,
190 0,
191 None,
192 0,
193 0,
194 0,
195 );
196 if let Some(event) = event {
197 app.postEvent_atStart(&event, false);
198 }
199}
200
201pub(super) fn in_full_screen(window: &NSWindow) -> bool {
203 window.styleMask().contains(NSWindowStyleMask::FullScreen)
204}