1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//! Windows: resizing them as the user does, showing them, and their modal loops.
use block2::RcBlock;
use mitsuami_core::{Modality, NodeId, Size};
use objc2::rc::Retained;
use objc2_app_kit::{NSApplication, NSEvent, NSEventModifierFlags, NSEventType, NSWindow, NSWindowStyleMask};
use objc2_core_foundation::{CFRunLoop, kCFRunLoopCommonModes};
use objc2_foundation::{NSPoint, NSSize};
use super::{AppKitHandle, Widget};
impl AppKitHandle {
/// Resizes a window's content like the user would, between its minimum
/// and maximum, as a drag goes (`setContentSize:` alone would go past
/// them); the window delegate reports it back as a `WindowResized` event.
pub fn resize_window(&self, window: NodeId, size: Size) {
let state = self.state.borrow();
let Some(Widget::Window { window, _delegate, .. }) = state.nodes.get(&window).map(|n| &n.widget) else {
return;
};
// The user can't resize a window without the resizable style.
if !window.styleMask().contains(NSWindowStyleMask::Resizable) {
return;
}
let (window, extra) = (window.clone(), _delegate.extra(window));
drop(state);
// Not `clamp`, which panics on a maximum below the minimum (a
// tweak's): the minimum wins, as it does in `at_least_min`.
let (min, max) = (window.contentMinSize(), window.contentMaxSize());
window.setContentSize(NSSize::new(
(size.width as f64 + extra.width).min(max.width).max(min.width),
(size.height as f64 + extra.height).min(max.height).max(min.height),
));
}
/// Orders front windows whose first layout has been applied: a sheet
/// on the window it belongs to, a window in the app's modal loop, or
/// an ordinary window.
pub fn show_pending_windows(&self) {
let (pending, activate) = {
let mut state = self.state.borrow_mut();
(std::mem::take(&mut state.pending_show), std::mem::take(&mut state.activate_on_show))
};
for id in pending {
let Some(window) = self.ns_window(id) else { continue };
let modal = self.state.borrow().nodes.get(&id).and_then(|n| n.modal);
let owner = modal.and_then(|(owner, _)| owner).and_then(|owner| self.ns_window(owner));
match (modal.map(|(_, modality)| modality), owner) {
(Some(Modality::Window), Some(owner)) => owner.beginSheet_completionHandler(&window, None),
(Some(_), owner) => {
match owner {
Some(owner) => centre_on(&window, &owner),
None => window.center(),
}
self.run_modal(id, window.clone());
}
(None, _) => {
// Where the app's frame put it, else centred: AppKit
// would put a new window in the screen's corner.
// Zoomed, it fills the screen: its restored frame was
// centred as it was sized (`set_frame`).
let framed = matches!(
self.state.borrow().nodes.get(&id).map(|n| &n.widget),
Some(Widget::Window { _delegate, .. }) if _delegate.framed() || _delegate.zoomed(&window)
);
if !framed {
window.center();
}
// Opened from the tray's menu, it comes forward with
// the app, as menu bar apps' windows do; it would
// otherwise open behind the active app's.
if activate {
NSApplication::sharedApplication(self.state.borrow().mtm).activate();
}
window.makeKeyAndOrderFront(None);
}
}
// Full screen asked for before it was shown; and where it is.
if let Some(Widget::Window { _delegate, .. }) = self.state.borrow().nodes.get(&id).map(|n| &n.widget) {
_delegate.apply_full_screen(&window);
_delegate.report_frame(&window);
}
// The dialogs asked for on it before it was shown.
let waiting = {
let mut state = self.state.borrow_mut();
let (waiting, kept) = std::mem::take(&mut state.on_shown).into_iter().partition(|(w, _)| *w == id);
state.on_shown = kept;
waiting
};
for (_, run) in waiting {
run();
}
}
}
/// The user chose a tray item: a window it opens in the next turn
/// activates the app.
pub(crate) fn activate_on_show(&self) {
self.state.borrow_mut().activate_on_show = true;
}
/// Runs `run` once the window `on` has been shown: at once if it has
/// (or there's no window), else right after its first layout puts it
/// on screen. A sheet begun on a window not yet shown orders it in
/// where AppKit put it, the screen's bottom-left corner, and the
/// window's centring then moves it under the sheet, which puts it back
/// in the corner as it ends; a dialog asked for as an app starts waits
/// for its window instead.
pub(crate) fn when_shown(&self, on: Option<NodeId>, run: impl FnOnce() + 'static) {
let mut state = self.state.borrow_mut();
match on.filter(|id| state.pending_show.contains(id)) {
Some(id) => state.on_shown.push((id, Box::new(run))),
None => {
drop(state);
run();
}
}
}
/// Runs the app's modal loop for the window, once the current run-loop
/// turn is over: it's a nested loop, which mustn't start inside a
/// tick. The UI keeps ticking in it (the app's observer runs in the
/// modal panel mode too). It ends when the window is destroyed. Queued
/// in the common modes: a dialog opened from an app-modal window is
/// asked for inside that window's loop, in the modal panel mode, where
/// a default-mode block waits until the outer loop ends.
fn run_modal(&self, id: NodeId, window: Retained<NSWindow>) {
let state = self.state.clone();
let block = RcBlock::new(move || {
// Destroyed before it got to run.
if !state.borrow().nodes.contains_key(&id) {
return;
}
let mtm = state.borrow().mtm;
NSApplication::sharedApplication(mtm).runModalForWindow(&window);
});
if let Some(run_loop) = CFRunLoop::main() {
unsafe { run_loop.perform_block(kCFRunLoopCommonModes.map(|m| &**m), Some(&block)) };
run_loop.wake_up();
}
}
/// Escape hatch: the native window of a window node.
pub fn ns_window(&self, id: NodeId) -> Option<Retained<NSWindow>> {
match &self.state.borrow().nodes.get(&id)?.widget {
Widget::Window { window, .. } => Some(window.clone()),
_ => None,
}
}
/// The window node showing this native window.
pub(crate) fn window_node(&self, ns_window: &NSWindow) -> Option<NodeId> {
self.state.borrow().nodes.iter().find_map(|(id, node)| match &node.widget {
Widget::Window { window, .. } if std::ptr::eq(&**window, ns_window) => Some(*id),
_ => None,
})
}
}
/// Centres a window on another, as dialogs open over their window.
fn centre_on(window: &NSWindow, owner: &NSWindow) {
let (own, frame) = (owner.frame(), window.frame());
window.setFrameOrigin(NSPoint::new(
(own.origin.x + (own.size.width - frame.size.width) / 2.0).round(),
(own.origin.y + (own.size.height - frame.size.height) / 2.0).round(),
));
}
/// Runs `run` in the main run loop's next turn.
pub(super) fn soon(run: Box<dyn FnOnce()>) {
let run = std::cell::Cell::new(Some(run));
let block = RcBlock::new(move || {
if let Some(run) = run.take() {
run();
}
});
if let Some(run_loop) = CFRunLoop::main() {
unsafe { run_loop.perform_block(kCFRunLoopCommonModes.map(|m| &**m), Some(&block)) };
run_loop.wake_up();
}
}
/// An empty event, which wakes the app's event loop.
pub(super) fn post_empty_event(app: &NSApplication) {
let event = NSEvent::otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2(
NSEventType::ApplicationDefined,
NSPoint::new(0.0, 0.0),
NSEventModifierFlags::empty(),
0.0,
0,
None,
0,
0,
0,
);
if let Some(event) = event {
app.postEvent_atStart(&event, false);
}
}
/// In full screen, or moving into or out of it.
pub(super) fn in_full_screen(window: &NSWindow) -> bool {
window.styleMask().contains(NSWindowStyleMask::FullScreen)
}