@geastack/apple 0.2.33 → 0.2.35
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +7 -7
- package/targets/ios/main/ios_display.mm +10 -0
- package/targets/macos/README.md +178 -6
- package/targets/macos/build-macos.sh +22 -3
- package/targets/macos/main/font_registry.h +8 -1
- package/targets/macos/main/font_registry.mm +42 -17
- package/targets/macos/main/macos_content_view.h +20 -0
- package/targets/macos/main/macos_main.mm +233 -34
- package/targets/macos/main/macos_microphone.mm +134 -0
- package/targets/macos/main/macos_native_shell.h +12 -0
- package/targets/macos/main/macos_native_shell.mm +102 -3
- package/targets/macos/main/macos_renderer.h +40 -0
- package/targets/macos/main/macos_renderer.mm +681 -12
- package/targets/macos/main/macos_windows.h +46 -0
- package/targets/macos/main/macos_windows.mm +704 -0
- package/targets/macos/window-config.mjs +9 -4
- package/targets/shared/apple_audio.mm +121 -2
- package/targets/shared/apple_audio_selftest.mm +74 -2
- package/targets/watchos/Info.plist.in +18 -0
- package/targets/watchos/README.md +116 -0
- package/targets/watchos/build-watchos.sh +255 -0
- package/targets/watchos/generate-xcode-project.mjs +434 -0
- package/targets/watchos/main/watchos_app.swift +140 -0
- package/targets/watchos/main/watchos_bridge.h +48 -0
- package/targets/watchos/main/watchos_host.mm +224 -0
- package/targets/watchos/main/watchos_platform.mm +95 -0
|
@@ -0,0 +1,704 @@
|
|
|
1
|
+
// `<window>` on macOS: each `<window>` element in the tree is its own titled,
|
|
2
|
+
// resizable NSWindow in this process (macos_windows.h).
|
|
3
|
+
//
|
|
4
|
+
// The element's subtree is ordinary tree content -- the same stores, the same
|
|
5
|
+
// reactive updates, the same event dispatch -- that the engine keeps out of its
|
|
6
|
+
// parent's layout and painting (engine node_model.h `isDetachedRoot`). Here it
|
|
7
|
+
// is laid out as its own root at the window's content size and rendered into
|
|
8
|
+
// the window: a GeaContentView for ordinary content, or the native split shell
|
|
9
|
+
// (macos_native_shell.mm) when the window's content is a `<glass-split>`.
|
|
10
|
+
#import "macos_windows.h"
|
|
11
|
+
|
|
12
|
+
#import <QuartzCore/CADisplayLink.h>
|
|
13
|
+
|
|
14
|
+
#include "macos_content_view.h"
|
|
15
|
+
#include "macos_key_event.h"
|
|
16
|
+
#include "macos_native_shell.h"
|
|
17
|
+
#include "macos_renderer.h"
|
|
18
|
+
|
|
19
|
+
#include "events.h"
|
|
20
|
+
#include "host/menu_bar.h"
|
|
21
|
+
#include "ui/node.h"
|
|
22
|
+
#include "ui/node_model.h"
|
|
23
|
+
#include "ui/tree_events.h"
|
|
24
|
+
#include "ui/tree_internal.h"
|
|
25
|
+
|
|
26
|
+
#include <cmath>
|
|
27
|
+
#include <cstdint>
|
|
28
|
+
#include <cstdlib>
|
|
29
|
+
#include <cstring>
|
|
30
|
+
#include <initializer_list>
|
|
31
|
+
#include <string>
|
|
32
|
+
#include <vector>
|
|
33
|
+
|
|
34
|
+
using gea::embedded::ui::NodeHandle;
|
|
35
|
+
using gea::embedded::ui::Tree;
|
|
36
|
+
|
|
37
|
+
namespace {
|
|
38
|
+
|
|
39
|
+
NSString *attributeString(int node, const char *name)
|
|
40
|
+
{
|
|
41
|
+
Tree &tree = Tree::instance();
|
|
42
|
+
if (!tree.hasAttribute(node, name)) return nil;
|
|
43
|
+
const char *value = tree.getAttribute(node, name);
|
|
44
|
+
return [NSString stringWithUTF8String:value ? value : ""];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// JSX writes `minWidth`; markup written by hand may say `min-width`.
|
|
48
|
+
double attributeNumber(int node, std::initializer_list<const char *> names, double fallback)
|
|
49
|
+
{
|
|
50
|
+
for (const char *name : names) {
|
|
51
|
+
NSString *value = attributeString(node, name);
|
|
52
|
+
if (value.length == 0) continue;
|
|
53
|
+
const double number = value.doubleValue;
|
|
54
|
+
if (std::isfinite(number) && number > 0) return number;
|
|
55
|
+
}
|
|
56
|
+
return fallback;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
bool connectedTo(int node, int root)
|
|
60
|
+
{
|
|
61
|
+
Tree &tree = Tree::instance();
|
|
62
|
+
for (int id = node, left = tree.nodeCount(); left > 0 && id >= 0 && id < tree.nodeCount(); --left, id = tree.node(id).parent)
|
|
63
|
+
if (id == root) return true;
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
int glassSplitUnder(int node, int depth = 0)
|
|
68
|
+
{
|
|
69
|
+
Tree &tree = Tree::instance();
|
|
70
|
+
if (node < 0 || node >= tree.nodeCount() || depth > 4) return -1;
|
|
71
|
+
if (depth > 0 && std::strcmp(tree.tagName(node), "glass-split") == 0) return node;
|
|
72
|
+
if (depth > 0 && tree.isDetachedRoot(node)) return -1;
|
|
73
|
+
for (int child = tree.node(node).first_child; child >= 0; child = tree.node(child).next_sibling) {
|
|
74
|
+
const int found = glassSplitUnder(child, depth + 1);
|
|
75
|
+
if (found >= 0) return found;
|
|
76
|
+
}
|
|
77
|
+
return -1;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// A key event for the `<window>` rooted at `root`: its focused element when
|
|
81
|
+
// there is one, else the window's own first element, so `onKeyDown` on the
|
|
82
|
+
// window's content (or the window) runs -- the same aim the main window takes.
|
|
83
|
+
bool dispatchHostedKey(NSEvent *native, int root, bool up)
|
|
84
|
+
{
|
|
85
|
+
Tree &tree = Tree::instance();
|
|
86
|
+
if (root < 0 || root >= tree.nodeCount()) return false;
|
|
87
|
+
auto event = geaNativeKey(native, false, up);
|
|
88
|
+
int target = root;
|
|
89
|
+
for (int id = tree.activeInputId(); id >= 0 && id < tree.nodeCount(); id = tree.node(id).parent)
|
|
90
|
+
if (id == root) { target = tree.activeInputId(); break; }
|
|
91
|
+
if (target == root && tree.node(root).first_child >= 0) target = tree.node(root).first_child;
|
|
92
|
+
event.targetId = target;
|
|
93
|
+
event.target = gea::framework::events::EventTarget(target);
|
|
94
|
+
event.currentTargetId = root;
|
|
95
|
+
event.currentTarget = gea::framework::events::EventTarget(root);
|
|
96
|
+
gea::embedded::ui::dispatchRootKeyEvent(event, false);
|
|
97
|
+
return static_cast<bool>(event.defaultPrevented);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// The nearest node at or above `node`, inside the `<window>` `root`, that
|
|
101
|
+
// carries `name`; -1 when none does.
|
|
102
|
+
int windowAncestorWith(int node, int root, const char *name)
|
|
103
|
+
{
|
|
104
|
+
Tree &tree = Tree::instance();
|
|
105
|
+
for (int id = node, left = tree.nodeCount(); left > 0 && id >= 0 && id < tree.nodeCount(); --left, id = tree.node(id).parent) {
|
|
106
|
+
if (tree.hasAttribute(id, name)) return id;
|
|
107
|
+
if (id == root) break;
|
|
108
|
+
}
|
|
109
|
+
return -1;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// The `data-window-drag` element a mouse-down on `node` drags the window by:
|
|
113
|
+
// none when a node between them handles pointer input itself (a listener of
|
|
114
|
+
// its own or a delegated JSX onClick) or is a text field.
|
|
115
|
+
int windowDragElement(int node, int root)
|
|
116
|
+
{
|
|
117
|
+
Tree &tree = Tree::instance();
|
|
118
|
+
const int drag = windowAncestorWith(node, root, "data-window-drag");
|
|
119
|
+
if (drag < 0) return -1;
|
|
120
|
+
for (int id = node; id >= 0 && id != drag; id = tree.node(id).parent) {
|
|
121
|
+
const char *tag = tree.tagName(id);
|
|
122
|
+
if (tree.handlesPointerEvents(id)) return -1;
|
|
123
|
+
if (tag && (std::strcmp(tag, "input") == 0 || std::strcmp(tag, "textarea") == 0 || std::strcmp(tag, "button") == 0)) return -1;
|
|
124
|
+
}
|
|
125
|
+
return drag;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// The window's close button: the delegate hides it and fires `close`.
|
|
129
|
+
void closeLikeCloseButton(NSWindow *window)
|
|
130
|
+
{
|
|
131
|
+
id<NSWindowDelegate> delegate = window.delegate;
|
|
132
|
+
if ([delegate respondsToSelector:@selector(windowShouldClose:)] && ![delegate windowShouldClose:window]) return;
|
|
133
|
+
[window close];
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
bool performWindowAction(NSWindow *window, const char *action)
|
|
137
|
+
{
|
|
138
|
+
if (!action) return false;
|
|
139
|
+
if (std::strcmp(action, "close") == 0) closeLikeCloseButton(window);
|
|
140
|
+
else if (std::strcmp(action, "minimize") == 0) [window miniaturize:nil];
|
|
141
|
+
else if (std::strcmp(action, "zoom") == 0) [window zoom:nil];
|
|
142
|
+
else return false;
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// A double-click on a drag area does what the system's "double-click a
|
|
147
|
+
// window's title bar to" setting says.
|
|
148
|
+
void titleBarDoubleClick(NSWindow *window)
|
|
149
|
+
{
|
|
150
|
+
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
|
|
151
|
+
NSString *action = [defaults stringForKey:@"AppleActionOnDoubleClick"];
|
|
152
|
+
if ([action isEqualToString:@"None"]) return;
|
|
153
|
+
if ([action isEqualToString:@"Minimize"] || (!action && [defaults boolForKey:@"AppleMiniaturizeOnDoubleClick"]))
|
|
154
|
+
[window miniaturize:nil];
|
|
155
|
+
else
|
|
156
|
+
[window zoom:nil];
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
uint64_t gLastLayoutSerial = 0;
|
|
160
|
+
NSSize gLastMainSize = NSZeroSize;
|
|
161
|
+
bool gLayoutForced = true;
|
|
162
|
+
|
|
163
|
+
} // namespace
|
|
164
|
+
|
|
165
|
+
@class GeaWindowHost;
|
|
166
|
+
@interface GeaWindows ()
|
|
167
|
+
+ (void)updateActivationPolicy;
|
|
168
|
+
+ (void)auxiliaryFrameFrom:(GeaWindowHost *)host;
|
|
169
|
+
@end
|
|
170
|
+
|
|
171
|
+
// A window whose content is the native split shell has no Gea view holding the
|
|
172
|
+
// keyboard (its panes are plain container views); keys nothing else took
|
|
173
|
+
// arrive here and go to the window's tree.
|
|
174
|
+
//
|
|
175
|
+
// It also gives the app's own window chrome its behaviour: a mouse-down on a
|
|
176
|
+
// `data-window-drag` element moves the window (a double-click zooms), a click
|
|
177
|
+
// on a `data-window-action` element closes, minimizes or zooms it, and Cmd-W /
|
|
178
|
+
// Cmd-M work with the standard buttons hidden.
|
|
179
|
+
@interface GeaHostedWindow : NSWindow
|
|
180
|
+
@property(nonatomic) int geaKeyRoot;
|
|
181
|
+
@property(nonatomic) int geaPendingAction;
|
|
182
|
+
@end
|
|
183
|
+
|
|
184
|
+
@implementation GeaHostedWindow
|
|
185
|
+
- (void)sendEvent:(NSEvent *)event
|
|
186
|
+
{
|
|
187
|
+
const int root = [GeaWindows nodeForWindow:self];
|
|
188
|
+
if (event.type == NSEventTypeLeftMouseDown && root >= 0) {
|
|
189
|
+
self.geaPendingAction = -1;
|
|
190
|
+
const int node = gea::macos::nodeAtWindowPoint(self, event.locationInWindow, nullptr, nullptr);
|
|
191
|
+
if (node >= 0) {
|
|
192
|
+
const int action = windowAncestorWith(node, root, "data-window-action");
|
|
193
|
+
if (action >= 0) {
|
|
194
|
+
self.geaPendingAction = action;
|
|
195
|
+
} else if (windowDragElement(node, root) >= 0) {
|
|
196
|
+
// The event stops here, so it brings the window forward itself.
|
|
197
|
+
if (!self.isKeyWindow) [self makeKeyAndOrderFront:nil];
|
|
198
|
+
if (event.clickCount == 2) titleBarDoubleClick(self);
|
|
199
|
+
else [self performWindowDragWithEvent:event];
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
} else if (event.type == NSEventTypeLeftMouseUp && self.geaPendingAction >= 0) {
|
|
204
|
+
const int action = self.geaPendingAction;
|
|
205
|
+
self.geaPendingAction = -1;
|
|
206
|
+
[super sendEvent:event];
|
|
207
|
+
// The control acts once the click has reached the app, and only when the
|
|
208
|
+
// button comes up over the control it went down on.
|
|
209
|
+
const int node = gea::macos::nodeAtWindowPoint(self, event.locationInWindow, nullptr, nullptr);
|
|
210
|
+
if (root >= 0 && node >= 0 && windowAncestorWith(node, root, "data-window-action") == action)
|
|
211
|
+
performWindowAction(self, Tree::instance().getAttribute(action, "data-window-action"));
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
[super sendEvent:event];
|
|
215
|
+
}
|
|
216
|
+
- (BOOL)performKeyEquivalent:(NSEvent *)event
|
|
217
|
+
{
|
|
218
|
+
const NSEventModifierFlags flags = event.modifierFlags & NSEventModifierFlagDeviceIndependentFlagsMask;
|
|
219
|
+
if (flags == NSEventModifierFlagCommand) {
|
|
220
|
+
NSString *key = event.charactersIgnoringModifiers.lowercaseString;
|
|
221
|
+
if ([key isEqualToString:@"w"]) { closeLikeCloseButton(self); return YES; }
|
|
222
|
+
if ([key isEqualToString:@"m"]) { [self miniaturize:nil]; return YES; }
|
|
223
|
+
}
|
|
224
|
+
return [super performKeyEquivalent:event];
|
|
225
|
+
}
|
|
226
|
+
- (void)keyDown:(NSEvent *)event
|
|
227
|
+
{
|
|
228
|
+
if (self.geaKeyRoot >= 0 && dispatchHostedKey(event, self.geaKeyRoot, false)) return;
|
|
229
|
+
[super keyDown:event];
|
|
230
|
+
}
|
|
231
|
+
- (void)keyUp:(NSEvent *)event
|
|
232
|
+
{
|
|
233
|
+
if (self.geaKeyRoot >= 0 && dispatchHostedKey(event, self.geaKeyRoot, true)) return;
|
|
234
|
+
[super keyUp:event];
|
|
235
|
+
}
|
|
236
|
+
@end
|
|
237
|
+
|
|
238
|
+
@interface GeaWindowHost : NSObject <NSWindowDelegate> {
|
|
239
|
+
@public
|
|
240
|
+
int _node;
|
|
241
|
+
gea::framework::events::EventTarget _owner;
|
|
242
|
+
}
|
|
243
|
+
@property(nonatomic, strong) GeaHostedWindow *window;
|
|
244
|
+
@property(nonatomic, strong) GeaContentView *content;
|
|
245
|
+
@property(nonatomic, strong) GeaSplitShell *shell;
|
|
246
|
+
@property(nonatomic, strong) id displayLink;
|
|
247
|
+
@property(nonatomic, copy) NSString *title;
|
|
248
|
+
@property(nonatomic, copy) NSString *focusToken;
|
|
249
|
+
@property(nonatomic) BOOL hiddenByUser;
|
|
250
|
+
@property(nonatomic) BOOL split;
|
|
251
|
+
@property(nonatomic, copy) NSString *chrome;
|
|
252
|
+
@property(nonatomic) NSSize laidOutSize;
|
|
253
|
+
@end
|
|
254
|
+
|
|
255
|
+
static NSMutableArray<GeaWindowHost *> *gHosts = nil;
|
|
256
|
+
|
|
257
|
+
@implementation GeaWindowHost
|
|
258
|
+
|
|
259
|
+
- (instancetype)initWithNode:(int)node
|
|
260
|
+
{
|
|
261
|
+
if (!(self = [super init])) return nil;
|
|
262
|
+
_node = node;
|
|
263
|
+
_owner = gea::framework::events::EventTarget(node);
|
|
264
|
+
_laidOutSize = NSZeroSize;
|
|
265
|
+
const int width = static_cast<int>(attributeNumber(node, {"width"}, 960));
|
|
266
|
+
const int height = static_cast<int>(attributeNumber(node, {"height"}, 640));
|
|
267
|
+
const bool split = glassSplitUnder(node) >= 0;
|
|
268
|
+
NSWindowStyleMask style = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable |
|
|
269
|
+
NSWindowStyleMaskResizable;
|
|
270
|
+
// The split shell's sidebar glass and unified toolbar reach the top edge.
|
|
271
|
+
if (split) style |= NSWindowStyleMaskFullSizeContentView;
|
|
272
|
+
GeaHostedWindow *window = [[GeaHostedWindow alloc] initWithContentRect:NSMakeRect(0, 0, width, height)
|
|
273
|
+
styleMask:style
|
|
274
|
+
backing:NSBackingStoreBuffered
|
|
275
|
+
defer:NO];
|
|
276
|
+
window.geaKeyRoot = -1;
|
|
277
|
+
window.geaPendingAction = -1;
|
|
278
|
+
_split = split;
|
|
279
|
+
window.releasedWhenClosed = NO;
|
|
280
|
+
window.delegate = self;
|
|
281
|
+
window.tabbingMode = NSWindowTabbingModeDisallowed;
|
|
282
|
+
window.collectionBehavior |= NSWindowCollectionBehaviorFullScreenPrimary;
|
|
283
|
+
self.window = window;
|
|
284
|
+
[self applyAttributes];
|
|
285
|
+
|
|
286
|
+
GeaContentView *content = [[GeaContentView alloc] initWithFrame:[window.contentView bounds]];
|
|
287
|
+
content.geaHostedRoot = node;
|
|
288
|
+
content.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
|
|
289
|
+
content.wantsLayer = YES;
|
|
290
|
+
window.contentView = content;
|
|
291
|
+
self.content = content;
|
|
292
|
+
if (split) {
|
|
293
|
+
self.shell = [GeaSplitShell shellForRootNode:node window:window];
|
|
294
|
+
if (self.shell) {
|
|
295
|
+
// The split controller resizes the window to its fitting size.
|
|
296
|
+
[window setContentSize:NSMakeSize(width, height)];
|
|
297
|
+
window.geaKeyRoot = node;
|
|
298
|
+
self.content = nil;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// A window the user moved or resized reopens where they left it -- except
|
|
303
|
+
// under a scripted replay, whose screenshots need the authored size.
|
|
304
|
+
const bool replay = std::getenv("GEA_MACOS_REPLAY_PLAN") != nullptr;
|
|
305
|
+
NSString *autosave = self.title.length ? [@"gea.window." stringByAppendingString:self.title] : nil;
|
|
306
|
+
if (replay || !autosave || ![window setFrameUsingName:autosave]) [window center];
|
|
307
|
+
if (!replay && autosave) window.frameAutosaveName = autosave;
|
|
308
|
+
return self;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
- (void)applyAttributes
|
|
312
|
+
{
|
|
313
|
+
NSString *title = attributeString(_node, "title");
|
|
314
|
+
if (!title.length) {
|
|
315
|
+
title = [NSBundle.mainBundle objectForInfoDictionaryKey:@"CFBundleDisplayName"]
|
|
316
|
+
?: [NSBundle.mainBundle objectForInfoDictionaryKey:@"CFBundleName"]
|
|
317
|
+
?: NSProcessInfo.processInfo.processName;
|
|
318
|
+
}
|
|
319
|
+
if (![title isEqualToString:self.title]) {
|
|
320
|
+
self.title = title;
|
|
321
|
+
self.window.title = title;
|
|
322
|
+
}
|
|
323
|
+
NSString *chrome = [attributeString(_node, "chrome") isEqualToString:@"none"] ? @"none" : @"native";
|
|
324
|
+
if (![chrome isEqualToString:self.chrome]) {
|
|
325
|
+
self.chrome = chrome;
|
|
326
|
+
[self applyChrome:[chrome isEqualToString:@"none"]];
|
|
327
|
+
}
|
|
328
|
+
const NSSize minimum = NSMakeSize(attributeNumber(_node, {"minWidth", "min-width", "minwidth"}, 320),
|
|
329
|
+
attributeNumber(_node, {"minHeight", "min-height", "minheight"}, 240));
|
|
330
|
+
if (!NSEqualSizes(minimum, self.window.contentMinSize)) self.window.contentMinSize = minimum;
|
|
331
|
+
NSString *focus = attributeString(_node, "focus") ?: @"";
|
|
332
|
+
if (self.focusToken && ![focus isEqualToString:self.focusToken]) [self present];
|
|
333
|
+
self.focusToken = focus;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// `chrome="none"`: the content fills the whole window under a transparent,
|
|
337
|
+
// title-less title bar with the standard buttons hidden. The window keeps its
|
|
338
|
+
// shadow, rounded corners, edge resizing and key equivalents.
|
|
339
|
+
- (void)applyChrome:(BOOL)none
|
|
340
|
+
{
|
|
341
|
+
NSWindow *window = self.window;
|
|
342
|
+
const NSRect frame = window.frame;
|
|
343
|
+
NSWindowStyleMask style = window.styleMask;
|
|
344
|
+
if (none || self.split) style |= NSWindowStyleMaskFullSizeContentView;
|
|
345
|
+
else style &= ~NSWindowStyleMaskFullSizeContentView;
|
|
346
|
+
window.styleMask = style;
|
|
347
|
+
window.titlebarAppearsTransparent = none;
|
|
348
|
+
window.titleVisibility = none ? NSWindowTitleHidden : NSWindowTitleVisible;
|
|
349
|
+
for (NSWindowButton button : {NSWindowCloseButton, NSWindowMiniaturizeButton, NSWindowZoomButton})
|
|
350
|
+
[window standardWindowButton:button].hidden = none;
|
|
351
|
+
window.appearance = none ? [NSAppearance appearanceNamed:NSAppearanceNameDarkAqua] : nil;
|
|
352
|
+
// A change after opening keeps the window where it is, at its size.
|
|
353
|
+
if (window.isVisible) [window setFrame:frame display:YES];
|
|
354
|
+
gLayoutForced = true;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
- (void)present
|
|
358
|
+
{
|
|
359
|
+
self.hiddenByUser = NO;
|
|
360
|
+
[GeaWindows updateActivationPolicy];
|
|
361
|
+
[NSApp activateIgnoringOtherApps:YES];
|
|
362
|
+
[self.window makeKeyAndOrderFront:nil];
|
|
363
|
+
if (self.content && self.window.firstResponder == self.window) [self.window makeFirstResponder:self.content];
|
|
364
|
+
if (@available(macOS 14.0, *)) {
|
|
365
|
+
if (!self.displayLink) {
|
|
366
|
+
CADisplayLink *link = [self.window displayLinkWithTarget:self selector:@selector(displayLinkFired:)];
|
|
367
|
+
[link addToRunLoop:NSRunLoop.mainRunLoop forMode:NSRunLoopCommonModes];
|
|
368
|
+
self.displayLink = link;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
- (void)displayLinkFired:(id)link
|
|
374
|
+
{
|
|
375
|
+
(void)link;
|
|
376
|
+
[GeaWindows auxiliaryFrameFrom:self];
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
- (BOOL)alive
|
|
380
|
+
{
|
|
381
|
+
Tree &tree = Tree::instance();
|
|
382
|
+
return _owner.valid() && tree.isDetachedRoot(_node) && connectedTo(_node, tree.mountedRoot());
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
- (void)destroy
|
|
386
|
+
{
|
|
387
|
+
if (@available(macOS 14.0, *)) [(CADisplayLink *)self.displayLink invalidate];
|
|
388
|
+
self.displayLink = nil;
|
|
389
|
+
self.window.delegate = nil;
|
|
390
|
+
[self.window orderOut:nil];
|
|
391
|
+
[self.window close];
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
#pragma mark NSWindowDelegate
|
|
395
|
+
|
|
396
|
+
// The user closed the window. It is hidden, and `close` tells the app, which
|
|
397
|
+
// unmounts the element; one that stays mounted reappears when `focus` changes.
|
|
398
|
+
- (BOOL)windowShouldClose:(NSWindow *)sender
|
|
399
|
+
{
|
|
400
|
+
(void)sender;
|
|
401
|
+
self.hiddenByUser = YES;
|
|
402
|
+
[self.window orderOut:nil];
|
|
403
|
+
[GeaWindows updateActivationPolicy];
|
|
404
|
+
if (_owner.valid()) {
|
|
405
|
+
gea::framework::events::PointerEvent event;
|
|
406
|
+
event.type = gea::framework::events::PointerEventType::Close;
|
|
407
|
+
event.targetId = _node;
|
|
408
|
+
event.bubbles = true;
|
|
409
|
+
event.cancelable = false;
|
|
410
|
+
Tree::instance().dispatchEvent(event);
|
|
411
|
+
}
|
|
412
|
+
return NO;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
- (void)windowDidResize:(NSNotification *)notification
|
|
416
|
+
{
|
|
417
|
+
(void)notification;
|
|
418
|
+
// The display link covers a live drag; this lands the final size at once.
|
|
419
|
+
// Deferred, since a resize can come from inside a frame (the window's own
|
|
420
|
+
// creation, a scripted resize).
|
|
421
|
+
static bool scheduled = false;
|
|
422
|
+
if (scheduled) return;
|
|
423
|
+
scheduled = true;
|
|
424
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
425
|
+
scheduled = false;
|
|
426
|
+
id delegate = NSApp.delegate;
|
|
427
|
+
if ([delegate respondsToSelector:@selector(tick)]) [delegate performSelector:@selector(tick)];
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
- (void)windowDidMiniaturize:(NSNotification *)notification
|
|
432
|
+
{
|
|
433
|
+
(void)notification;
|
|
434
|
+
[GeaWindows updateActivationPolicy];
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
@end
|
|
438
|
+
|
|
439
|
+
@implementation GeaWindows
|
|
440
|
+
|
|
441
|
+
+ (NSMutableArray<GeaWindowHost *> *)hosts
|
|
442
|
+
{
|
|
443
|
+
if (!gHosts) gHosts = [NSMutableArray array];
|
|
444
|
+
return gHosts;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
+ (BOOL)hasWindows
|
|
448
|
+
{
|
|
449
|
+
return gHosts.count > 0;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
+ (BOOL)anyVisible
|
|
453
|
+
{
|
|
454
|
+
for (GeaWindowHost *host in gHosts)
|
|
455
|
+
if (host.window.isVisible) return YES;
|
|
456
|
+
return NO;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
+ (NSWindow *)mainWindow
|
|
460
|
+
{
|
|
461
|
+
id delegate = NSApp.delegate;
|
|
462
|
+
if ([delegate respondsToSelector:@selector(window)]) {
|
|
463
|
+
id window = [delegate performSelector:@selector(window)];
|
|
464
|
+
if ([window isKindOfClass:[NSWindow class]]) return window;
|
|
465
|
+
}
|
|
466
|
+
return nil;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// A menu bar app has no Dock icon or app menu; while one of its windows is
|
|
470
|
+
// open it gets both (and Cmd-Tab), and gives them up when the last one closes.
|
|
471
|
+
+ (void)updateActivationPolicy
|
|
472
|
+
{
|
|
473
|
+
if (!gea::host::MenuBar.active()) return;
|
|
474
|
+
bool open = false;
|
|
475
|
+
for (GeaWindowHost *host in gHosts)
|
|
476
|
+
if (!host.hiddenByUser && !host.window.isMiniaturized) open = true;
|
|
477
|
+
const NSApplicationActivationPolicy wanted =
|
|
478
|
+
open ? NSApplicationActivationPolicyRegular : NSApplicationActivationPolicyAccessory;
|
|
479
|
+
if (NSApp.activationPolicy != wanted) [NSApp setActivationPolicy:wanted];
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
+ (void)auxiliaryFrameFrom:(GeaWindowHost *)host
|
|
483
|
+
{
|
|
484
|
+
// The main window's display link drives frames while it is on screen; the
|
|
485
|
+
// first visible `<window>` drives them otherwise (a hidden menu bar panel).
|
|
486
|
+
NSWindow *main = [self mainWindow];
|
|
487
|
+
if (main.isVisible) return;
|
|
488
|
+
for (GeaWindowHost *candidate in gHosts) {
|
|
489
|
+
if (!candidate.window.isVisible) continue;
|
|
490
|
+
if (candidate != host) return;
|
|
491
|
+
id delegate = NSApp.delegate;
|
|
492
|
+
if ([delegate respondsToSelector:@selector(tick)]) [delegate performSelector:@selector(tick)];
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
+ (void)reconcile
|
|
498
|
+
{
|
|
499
|
+
Tree &tree = Tree::instance();
|
|
500
|
+
const int mounted = tree.mountedRoot();
|
|
501
|
+
if (mounted < 0 && gHosts.count == 0) return;
|
|
502
|
+
std::vector<int> found;
|
|
503
|
+
for (int i = 0; i < tree.nodeCount(); ++i)
|
|
504
|
+
if (tree.isDetachedRoot(i) && connectedTo(i, mounted)) found.push_back(i);
|
|
505
|
+
if (found.empty() && gHosts.count == 0) return;
|
|
506
|
+
|
|
507
|
+
NSMutableArray<GeaWindowHost *> *hosts = [self hosts];
|
|
508
|
+
bool changed = false;
|
|
509
|
+
for (GeaWindowHost *host in [hosts copy]) {
|
|
510
|
+
if ([host alive]) continue;
|
|
511
|
+
[host destroy];
|
|
512
|
+
[hosts removeObject:host];
|
|
513
|
+
changed = true;
|
|
514
|
+
}
|
|
515
|
+
for (int node : found) {
|
|
516
|
+
GeaWindowHost *existing = nil;
|
|
517
|
+
for (GeaWindowHost *host in hosts)
|
|
518
|
+
if (host->_node == node) { existing = host; break; }
|
|
519
|
+
if (existing) {
|
|
520
|
+
[existing applyAttributes];
|
|
521
|
+
continue;
|
|
522
|
+
}
|
|
523
|
+
GeaWindowHost *host = [[GeaWindowHost alloc] initWithNode:node];
|
|
524
|
+
[hosts addObject:host];
|
|
525
|
+
[host present];
|
|
526
|
+
changed = true;
|
|
527
|
+
}
|
|
528
|
+
if (changed) {
|
|
529
|
+
gLayoutForced = true;
|
|
530
|
+
[self updateActivationPolicy];
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
+ (void)layoutAndSyncMainView:(NSView *)mainView mainShell:(GeaSplitShell *)mainShell mainRoot:(int)mainRoot
|
|
535
|
+
{
|
|
536
|
+
Tree &tree = Tree::instance();
|
|
537
|
+
bool resized = gLayoutForced;
|
|
538
|
+
NSSize mainSize = mainView.bounds.size;
|
|
539
|
+
if (mainShell) {
|
|
540
|
+
if ([mainShell updatePaneFrames]) resized = true;
|
|
541
|
+
} else if (!NSEqualSizes(mainSize, gLastMainSize)) {
|
|
542
|
+
resized = true;
|
|
543
|
+
}
|
|
544
|
+
for (GeaWindowHost *host in gHosts) {
|
|
545
|
+
if (host.shell) {
|
|
546
|
+
if ([host.shell updatePaneFrames]) resized = true;
|
|
547
|
+
} else if (!NSEqualSizes(host.content.bounds.size, host.laidOutSize)) {
|
|
548
|
+
resized = true;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
// Lay out only when the tree or a viewport changed. Several roots share the
|
|
553
|
+
// engine's one "last laid-out viewport", so every window laid out is a full
|
|
554
|
+
// pass, and so is the main root after it. The refresh serial bumps for any
|
|
555
|
+
// refresh anywhere -- a canvas repainting in the main window, a class
|
|
556
|
+
// flipping there -- so windows are laid out again only when something in a
|
|
557
|
+
// `<window>` subtree changed or a viewport did; otherwise the main root keeps
|
|
558
|
+
// its record and its pass stays cheap. Windows first and the main root last
|
|
559
|
+
// leave that record on the mounted root, where the engine's own refresh
|
|
560
|
+
// expects it.
|
|
561
|
+
const int windowChanges = tree.takeDetachedRootChanges();
|
|
562
|
+
bool layoutWindows = resized || (windowChanges & Tree::kDetachedLayout);
|
|
563
|
+
// A content change inside a window that did not resize re-lays only the
|
|
564
|
+
// stable boxes around the changed nodes. That leaves the engine's viewport
|
|
565
|
+
// record on the main root, so the main root needs no pass either; only when
|
|
566
|
+
// some window cannot be laid out that way do all of them take a full pass.
|
|
567
|
+
if (layoutWindows && !resized) {
|
|
568
|
+
bool scoped = true;
|
|
569
|
+
for (GeaWindowHost *host in gHosts) {
|
|
570
|
+
if (host.shell || host->_node < 0 || host->_node >= tree.nodeCount() || host.laidOutSize.width <= 0) {
|
|
571
|
+
scoped = false;
|
|
572
|
+
break;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
if (scoped) {
|
|
576
|
+
for (GeaWindowHost *host in gHosts)
|
|
577
|
+
if (!tree.relayoutDetachedRoot(host->_node)) scoped = false;
|
|
578
|
+
}
|
|
579
|
+
if (scoped) layoutWindows = false;
|
|
580
|
+
}
|
|
581
|
+
bool laidOut = false;
|
|
582
|
+
if (layoutWindows || tree.refreshSerial() != gLastLayoutSerial) {
|
|
583
|
+
laidOut = true;
|
|
584
|
+
for (GeaWindowHost *host in gHosts) {
|
|
585
|
+
if (!layoutWindows) continue;
|
|
586
|
+
if (host.shell) {
|
|
587
|
+
[host.shell layoutPanes];
|
|
588
|
+
continue;
|
|
589
|
+
}
|
|
590
|
+
const NSSize size = host.content.bounds.size;
|
|
591
|
+
const int w = static_cast<int>(size.width), h = static_cast<int>(size.height);
|
|
592
|
+
if (w <= 0 || h <= 0 || host->_node < 0 || host->_node >= tree.nodeCount()) continue;
|
|
593
|
+
if (!NSEqualSizes(size, host.laidOutSize)) {
|
|
594
|
+
NodeHandle(host->_node).style().width(w);
|
|
595
|
+
NodeHandle(host->_node).style().height(h);
|
|
596
|
+
}
|
|
597
|
+
tree.computeLayout(host->_node, w, h);
|
|
598
|
+
host.laidOutSize = size;
|
|
599
|
+
}
|
|
600
|
+
if (mainShell) {
|
|
601
|
+
[mainShell layoutPanes];
|
|
602
|
+
} else if (mainRoot >= 0) {
|
|
603
|
+
const int w = static_cast<int>(mainSize.width), h = static_cast<int>(mainSize.height);
|
|
604
|
+
NodeHandle(mainRoot).style().width(w);
|
|
605
|
+
NodeHandle(mainRoot).style().height(h);
|
|
606
|
+
tree.computeLayout(mainRoot, w, h);
|
|
607
|
+
gLastMainSize = mainSize;
|
|
608
|
+
}
|
|
609
|
+
gLastLayoutSerial = tree.refreshSerial();
|
|
610
|
+
gLayoutForced = false;
|
|
611
|
+
}
|
|
612
|
+
// Nothing was laid out and nothing in a window changed: every view already
|
|
613
|
+
// shows the tree. Re-syncing would diff every node of every window (text
|
|
614
|
+
// attributes included) each frame for nothing -- the cost that made a long
|
|
615
|
+
// list in a `<window>` scroll at a few frames a second.
|
|
616
|
+
if (!laidOut && windowChanges == 0) return;
|
|
617
|
+
// The main window refreshing (a canvas repainting there) says nothing about
|
|
618
|
+
// the windows: they are walked only when something in them changed.
|
|
619
|
+
const bool syncWindows = layoutWindows || windowChanges != 0;
|
|
620
|
+
|
|
621
|
+
std::vector<gea::macos::SyncRoot> roots;
|
|
622
|
+
if (mainShell) [mainShell appendSyncRoots:&roots];
|
|
623
|
+
else if (mainRoot >= 0 && mainView) roots.push_back({mainView, mainRoot, 0, 0, mainRoot});
|
|
624
|
+
for (GeaWindowHost *host in gHosts) {
|
|
625
|
+
if (host.shell) {
|
|
626
|
+
[host.shell appendSyncRoots:&roots];
|
|
627
|
+
continue;
|
|
628
|
+
}
|
|
629
|
+
const int node = host->_node;
|
|
630
|
+
if (node < 0 || node >= tree.nodeCount() || host.laidOutSize.width <= 0) continue;
|
|
631
|
+
const auto &layout = tree.node(node).layout;
|
|
632
|
+
for (int child = tree.node(node).first_child; child >= 0; child = tree.node(child).next_sibling)
|
|
633
|
+
roots.push_back({host.content, child, static_cast<int>(layout.x), static_cast<int>(layout.y), node, !syncWindows});
|
|
634
|
+
}
|
|
635
|
+
gea::macos::MacosRenderer::instance().syncRoots(roots.data(), roots.size());
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
+ (void)showAll
|
|
639
|
+
{
|
|
640
|
+
for (GeaWindowHost *host in gHosts) [host present];
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
+ (NSWindow *)windowNamed:(NSString *)title
|
|
644
|
+
{
|
|
645
|
+
for (GeaWindowHost *host in gHosts)
|
|
646
|
+
if ([host.title isEqualToString:title]) return host.window;
|
|
647
|
+
NSScanner *scanner = [NSScanner scannerWithString:title];
|
|
648
|
+
NSInteger index = 0;
|
|
649
|
+
if ([scanner scanInteger:&index] && scanner.isAtEnd && index >= 0 && index < static_cast<NSInteger>(gHosts.count))
|
|
650
|
+
return gHosts[static_cast<NSUInteger>(index)].window;
|
|
651
|
+
return nil;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
+ (BOOL)performWindowActionForNode:(int)node window:(NSWindow *)window
|
|
655
|
+
{
|
|
656
|
+
const int root = [self nodeForWindow:window];
|
|
657
|
+
const int action = root >= 0 ? windowAncestorWith(node, root, "data-window-action") : -1;
|
|
658
|
+
return action >= 0 && performWindowAction(window, Tree::instance().getAttribute(action, "data-window-action"));
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
+ (int)windowDragElementForNode:(int)node window:(NSWindow *)window
|
|
662
|
+
{
|
|
663
|
+
const int root = [self nodeForWindow:window];
|
|
664
|
+
return root >= 0 ? windowDragElement(node, root) : -1;
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
+ (int)nodeForWindow:(NSWindow *)window
|
|
668
|
+
{
|
|
669
|
+
for (GeaWindowHost *host in gHosts)
|
|
670
|
+
if (host.window == window) return host->_node;
|
|
671
|
+
return -1;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
+ (NSArray<NSDictionary *> *)describe
|
|
675
|
+
{
|
|
676
|
+
NSMutableArray *windows = [NSMutableArray array];
|
|
677
|
+
for (GeaWindowHost *host in gHosts) {
|
|
678
|
+
const NSRect frame = host.window.frame;
|
|
679
|
+
const NSSize content = [host.window contentRectForFrameRect:frame].size;
|
|
680
|
+
[windows addObject:@{
|
|
681
|
+
@"title": host.title ?: @"",
|
|
682
|
+
@"windowNumber": @(host.window.windowNumber),
|
|
683
|
+
@"visible": @(host.window.isVisible),
|
|
684
|
+
@"key": @(host.window.isKeyWindow),
|
|
685
|
+
@"width": @(content.width),
|
|
686
|
+
@"height": @(content.height),
|
|
687
|
+
@"shell": @(host.shell != nil),
|
|
688
|
+
@"chrome": host.chrome ?: @"native",
|
|
689
|
+
}];
|
|
690
|
+
}
|
|
691
|
+
return windows;
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
@end
|
|
695
|
+
|
|
696
|
+
extern "C" bool gea_macos_focus_hosted_window(int nodeId)
|
|
697
|
+
{
|
|
698
|
+
for (GeaWindowHost *host in gHosts) {
|
|
699
|
+
if (host->_node != nodeId) continue;
|
|
700
|
+
[host present];
|
|
701
|
+
return true;
|
|
702
|
+
}
|
|
703
|
+
return false;
|
|
704
|
+
}
|