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 = std::mem::take(&mut self.state.borrow_mut().pending_show);
41 for id in pending {
42 let Some(window) = self.ns_window(id) else { continue };
43 let modal = self.state.borrow().nodes.get(&id).and_then(|n| n.modal);
44 let owner = modal.and_then(|(owner, _)| owner).and_then(|owner| self.ns_window(owner));
45 match (modal.map(|(_, modality)| modality), owner) {
46 (Some(Modality::Window), Some(owner)) => owner.beginSheet_completionHandler(&window, None),
47 (Some(_), owner) => {
48 match owner {
49 Some(owner) => centre_on(&window, &owner),
50 None => window.center(),
51 }
52 self.run_modal(id, window.clone());
53 }
54 (None, _) => {
55 window.center();
56 window.makeKeyAndOrderFront(None);
57 }
58 }
59 if let Some(Widget::Window { _delegate, .. }) = self.state.borrow().nodes.get(&id).map(|n| &n.widget) {
61 _delegate.apply_full_screen(&window);
62 }
63 }
64 }
65
66 fn run_modal(&self, id: NodeId, window: Retained<NSWindow>) {
74 let state = self.state.clone();
75 let block = RcBlock::new(move || {
76 if !state.borrow().nodes.contains_key(&id) {
78 return;
79 }
80 let mtm = state.borrow().mtm;
81 NSApplication::sharedApplication(mtm).runModalForWindow(&window);
82 });
83 if let Some(run_loop) = CFRunLoop::main() {
84 unsafe { run_loop.perform_block(kCFRunLoopCommonModes.map(|m| &**m), Some(&block)) };
85 run_loop.wake_up();
86 }
87 }
88
89 pub fn ns_window(&self, id: NodeId) -> Option<Retained<NSWindow>> {
91 match &self.state.borrow().nodes.get(&id)?.widget {
92 Widget::Window { window, .. } => Some(window.clone()),
93 _ => None,
94 }
95 }
96
97 pub(crate) fn window_node(&self, ns_window: &NSWindow) -> Option<NodeId> {
99 self.state.borrow().nodes.iter().find_map(|(id, node)| match &node.widget {
100 Widget::Window { window, .. } if std::ptr::eq(&**window, ns_window) => Some(*id),
101 _ => None,
102 })
103 }
104}
105
106fn centre_on(window: &NSWindow, owner: &NSWindow) {
108 let (own, frame) = (owner.frame(), window.frame());
109 window.setFrameOrigin(NSPoint::new(
110 (own.origin.x + (own.size.width - frame.size.width) / 2.0).round(),
111 (own.origin.y + (own.size.height - frame.size.height) / 2.0).round(),
112 ));
113}
114
115pub(super) fn post_empty_event(app: &NSApplication) {
117 let event = NSEvent::otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2(
118 NSEventType::ApplicationDefined,
119 NSPoint::new(0.0, 0.0),
120 NSEventModifierFlags::empty(),
121 0.0,
122 0,
123 None,
124 0,
125 0,
126 0,
127 );
128 if let Some(event) = event {
129 app.postEvent_atStart(&event, false);
130 }
131}
132
133pub(super) fn in_full_screen(window: &NSWindow) -> bool {
135 window.styleMask().contains(NSWindowStyleMask::FullScreen)
136}