@windowkit/appkit 0.19.0 → 0.20.0
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/README.md +47 -2
- package/package.json +2 -2
- package/prebuilds/darwin-arm64/calayers.node +0 -0
- package/prebuilds/darwin-x64/calayers.node +0 -0
- package/src/backend.mm +276 -0
package/README.md
CHANGED
|
@@ -126,14 +126,15 @@ waits on the UI thread (`test/threaded-verbs.js` covers each row):
|
|
|
126
126
|
| from a worker | verbs |
|
|
127
127
|
| --- | --- |
|
|
128
128
|
| unchanged, on the calling thread | surfaces, every `ctx*` (`ctxDrawSymbol` included), layouts and fonts, `symbolSize`, `pasteboardTypeForMIME`, `pasteboardTypeInfo`, `contentTypeFor`, `colorSpace` |
|
|
129
|
-
| a command, answering nothing | `initApp`, `setActivationPolicy`, `setAppName`, `activateApp`, `showWindow`, `hideWindow`, `setWindowFrame`, `setWindowTitle`, `setWindowMinMax`, `setWindowIgnoresMouseEvents`, `invalidateWindowShadow`, `destroyWindow2`, `setCursor`, `setMainMenu`, `setDockMenu`, `setDockBadge`, `cancelUserAttention`, `setStatusItem`, `setStatusItemMenu`, `removeStatusItem`, `registerDropTypes`, `setDropResponse`, `pasteboardWriteText`, `pasteboardClear`, `cancelPanel`; the test posts `postMouseEvent`, `postKeyEvent`, `postAppleEvent`, `postAccessibilityDisplayChange` |
|
|
129
|
+
| a command, answering nothing | `initApp`, `setActivationPolicy`, `setAppName`, `activateApp`, `showWindow`, `hideWindow`, `setWindowFrame`, `setWindowTitle`, `setWindowMinMax`, `setWindowIgnoresMouseEvents`, `invalidateWindowShadow`, `destroyWindow2`, `setCursor`, `setMainMenu`, `setDockMenu`, `setDockBadge`, `cancelUserAttention`, `setStatusItem`, `setStatusItemMenu`, `removeStatusItem`, `registerDropTypes`, `setDropResponse`, `pasteboardWriteText`, `pasteboardClear`, `cancelPanel`, `cancelPopUpMenu`; the test posts `postMouseEvent`, `postKeyEvent`, `postAppleEvent`, `postAccessibilityDisplayChange` |
|
|
130
130
|
| a handle at the call | `createWindow2`, followed by `window-created { handle, windowNumber }`; every event about the window, input included, carries `handle`, so `ev.handle === win` |
|
|
131
131
|
| | `windowRootLayer`, allocated with the window |
|
|
132
132
|
| | `createStatusItem`: its clicks carry the handle, and it is held until `removeStatusItem` |
|
|
133
133
|
| | `requestUserAttention`: a bridge id |
|
|
134
134
|
| | `openPanel` / `savePanel`: the answer through the callback, as before |
|
|
135
|
+
| | `popUpMenu`: the menu opens from a callout of its own, and answers through the callback |
|
|
135
136
|
| the published copy | `getWindowFrame`, `windowIsVisible`, `windowNumber` (all `null` until the window is made), `listScreens`, `accessibilityDisplayOptions`, `activationPolicy`, `appInfo`, `pasteboardChangeCount` |
|
|
136
|
-
| a callback, the last argument | `pasteboardReadText`, `snapshotWindow`, `snapshotStatusItem`, `windowNumberAtPoint`, `mainMenuInfo`, `dockMenuInfo`, `statusItemInfo`, `activateMenuItem`, `activateDockMenuItem`, `activateStatusItemMenuItem`, `clickStatusItem`, `dragItems`, `dragItemData`, `dragItemString`, `postDragEvent`. On the main thread each still answers synchronously when no callback is given. |
|
|
137
|
+
| a callback, the last argument | `pasteboardReadText`, `snapshotWindow`, `snapshotStatusItem`, `windowNumberAtPoint`, `mainMenuInfo`, `dockMenuInfo`, `statusItemInfo`, `activateMenuItem`, `activateDockMenuItem`, `activateStatusItemMenuItem`, `popUpMenuInfo`, `activatePopUpMenuItem`, `clickStatusItem`, `dragItems`, `dragItemData`, `dragItemString`, `postDragEvent`. On the main thread each still answers synchronously when no callback is given. |
|
|
137
138
|
| events | `beginDrag`: `drag-session-began`, or `drag-session-ended` with nothing dropped. Its `provide` is a TypeError; give every value up front. |
|
|
138
139
|
|
|
139
140
|
A few verbs behave differently from a worker:
|
|
@@ -1205,6 +1206,50 @@ the item's window (pump afterwards; declined for left/right while a menu is
|
|
|
1205
1206
|
set, since that click would open it), and `snapshotStatusItem(item, file)`
|
|
1206
1207
|
writes the composited item to a PNG.
|
|
1207
1208
|
|
|
1209
|
+
## Pop-up menu (a control's menu)
|
|
1210
|
+
|
|
1211
|
+
The menu that drops from a control in a window — a `<select>`'s list, a
|
|
1212
|
+
pop-up button's — the way `NSPopUpButton`'s does: the current item placed
|
|
1213
|
+
over the control and checked, the menu at least the control's width, in the
|
|
1214
|
+
font asked for. It is run through an `NSPopUpButtonCell` that is never drawn,
|
|
1215
|
+
`performClickWithFrame:inView:`, which is what a browser does for a
|
|
1216
|
+
`<select>` on macOS, so the placement, the tracking (type-select, the scroll
|
|
1217
|
+
arrows of a long menu, VoiceOver) and the look are AppKit's own. The items
|
|
1218
|
+
are `setMainMenu`'s vocabulary.
|
|
1219
|
+
|
|
1220
|
+
```js
|
|
1221
|
+
const menu = native.popUpMenu(win, {
|
|
1222
|
+
items: [ // setMainMenu's item vocabulary
|
|
1223
|
+
{ id: 1, title: 'Default' },
|
|
1224
|
+
{ id: 2, title: 'Retro' },
|
|
1225
|
+
{ separator: true },
|
|
1226
|
+
{ id: 3, title: 'Chaos', enabled: false },
|
|
1227
|
+
],
|
|
1228
|
+
frame: [40, 60, 220, 24], // the control, in the window's content, top-left, points
|
|
1229
|
+
selected: 2, // placed over the control, checked; absent: none is
|
|
1230
|
+
fontSize: 13, // the menu's font; the menu font at its own size by default
|
|
1231
|
+
fontFamily: 'Avenir Next', // a family the system has, else the menu font at fontSize
|
|
1232
|
+
appearance: 'dark', // 'light' | 'dark'; absent: the window's
|
|
1233
|
+
rtl: false,
|
|
1234
|
+
}, (id) => {
|
|
1235
|
+
// the id chosen, or null for a menu dismissed (Escape, a click outside, cancelPopUpMenu)
|
|
1236
|
+
});
|
|
1237
|
+
native.cancelPopUpMenu(menu); // ends the tracking: the callback answers null
|
|
1238
|
+
```
|
|
1239
|
+
|
|
1240
|
+
Tracking is a modal loop. In pump mode it runs inside the call, the thread
|
|
1241
|
+
being AppKit's for the length of the gesture, and the callback has run when
|
|
1242
|
+
the call returns. From a worker the handle is answered at the call, the menu
|
|
1243
|
+
opens from a callout of its own, the answer arrives through the callback, and
|
|
1244
|
+
`cancelPopUpMenu` can end it while it is open.
|
|
1245
|
+
|
|
1246
|
+
For tests: `popUpMenuInfo(menu)` returns an open pop-up as `mainMenuInfo`'s
|
|
1247
|
+
shape plus `fontFamily`, `fontSize`, `appearance` and `rtl`, and null once
|
|
1248
|
+
it has answered; `activatePopUpMenuItem(menu, index)` chooses an item the
|
|
1249
|
+
way tracking would and ends the tracking. In pump mode, keys posted with
|
|
1250
|
+
`postKeyEvent` before the call are read by the tracking as a person's would
|
|
1251
|
+
be (`test/popup-menu.js`).
|
|
1252
|
+
|
|
1208
1253
|
## Dock & app presence
|
|
1209
1254
|
|
|
1210
1255
|
The handful of things an app shows outside its own windows — the Dock tile and the name
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@windowkit/appkit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.0",
|
|
4
4
|
"description": "Retained-mode AppKit backend for Node.js: Core Animation (CALayer) layer trees, CoreText layout and drawing, IOSurface presentation, NSMenu, and native control bezels — the macOS half of react-x11.",
|
|
5
5
|
"author": "Andrey Sidorov <andrey.sidorov@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"install": "node scripts/install.js",
|
|
44
44
|
"build": "node-gyp rebuild",
|
|
45
45
|
"demo": "node demo/demo.js",
|
|
46
|
-
"test": "node test/app-lifecycle.js && node test/animation.js && node test/layer-transform.js && node test/accessibility-display.js && node test/ignores-mouse-events.js && node test/surface-blit.js && node test/surface-faded.js && node test/surface-colorspace.js && node test/round-rect-xy.js && node test/control-bezels.js && node test/text-features.js && node test/font-from-data.js && node test/text-coverage.js && node test/text-typesetter.js && node test/text-line-height.js && node test/text-line-width.js && node test/symbols.js && node test/eventkit-permissions.js && node test/calendars.js && node test/calendar-writes.js && node test/screencolor.js && node test/notifications.js && node test/threaded.js && node test/threaded-verbs.js && node test/threaded-frames.js && node test/threaded-resize.js && node test/ending-worker.js && node test/live-resize.js && node test/activation-policy.js"
|
|
46
|
+
"test": "node test/app-lifecycle.js && node test/animation.js && node test/layer-transform.js && node test/accessibility-display.js && node test/ignores-mouse-events.js && node test/popup-menu.js && node test/surface-blit.js && node test/surface-faded.js && node test/surface-colorspace.js && node test/round-rect-xy.js && node test/control-bezels.js && node test/text-features.js && node test/font-from-data.js && node test/text-coverage.js && node test/text-typesetter.js && node test/text-line-height.js && node test/text-line-width.js && node test/symbols.js && node test/eventkit-permissions.js && node test/calendars.js && node test/calendar-writes.js && node test/screencolor.js && node test/notifications.js && node test/threaded.js && node test/threaded-verbs.js && node test/threaded-frames.js && node test/threaded-resize.js && node test/ending-worker.js && node test/live-resize.js && node test/activation-policy.js"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"node-addon-api": "^8.3.0"
|
|
Binary file
|
|
Binary file
|
package/src/backend.mm
CHANGED
|
@@ -2259,6 +2259,278 @@ static Napi::Value ActivateMenuItemFn(const Napi::CallbackInfo& info) {
|
|
|
2259
2259
|
});
|
|
2260
2260
|
}
|
|
2261
2261
|
|
|
2262
|
+
// ---------------------------------------------------------------------------
|
|
2263
|
+
// pop-up menus — a menu that drops from a control in a window, the way an
|
|
2264
|
+
// NSPopUpButton's does: the chosen item placed over the control, the menu
|
|
2265
|
+
// at least the control's width, the current item checked. What a <select>
|
|
2266
|
+
// and a context menu want, and what a drawn menu can only imitate: its
|
|
2267
|
+
// vibrancy needs private API to reproduce, and its tracking (type-select,
|
|
2268
|
+
// the scroll arrows of a long menu, VoiceOver) is AppKit's.
|
|
2269
|
+
//
|
|
2270
|
+
// Run through an NSPopUpButtonCell that is never drawn, which is what a
|
|
2271
|
+
// browser does for a <select>: performClickWithFrame:inView: places and
|
|
2272
|
+
// tracks the menu exactly as the control would, over the frame given, in
|
|
2273
|
+
// the cell's font. Tracking is a modal loop, so the answer is a callback:
|
|
2274
|
+
// in pump mode it runs before the call returns, the thread having been
|
|
2275
|
+
// AppKit's for the length of the gesture; from a worker the menu is opened
|
|
2276
|
+
// from a callout of its own (CALOnUIModal) and answers through a
|
|
2277
|
+
// threadsafe function, and cancelPopUpMenu can end it.
|
|
2278
|
+
// ---------------------------------------------------------------------------
|
|
2279
|
+
|
|
2280
|
+
static Napi::Value BCommandAnswer(Napi::Env env, bool ok);
|
|
2281
|
+
|
|
2282
|
+
// An item's action records the choice rather than emitting `menu-activate`:
|
|
2283
|
+
// a pop-up answers its caller, once, and a dismissal is an answer too.
|
|
2284
|
+
@interface CALPopUpTarget : CALMenuTarget {
|
|
2285
|
+
@public
|
|
2286
|
+
bool picked_;
|
|
2287
|
+
NSInteger chosen_;
|
|
2288
|
+
}
|
|
2289
|
+
@end
|
|
2290
|
+
@implementation CALPopUpTarget
|
|
2291
|
+
- (void)activate:(NSMenuItem*)sender {
|
|
2292
|
+
picked_ = true;
|
|
2293
|
+
chosen_ = sender.tag;
|
|
2294
|
+
}
|
|
2295
|
+
@end
|
|
2296
|
+
|
|
2297
|
+
struct PopUpSpec {
|
|
2298
|
+
std::vector<MenuItemSpec> items;
|
|
2299
|
+
double frame[4] = {0, 0, 0, 0}; // the control: x, y, width, height
|
|
2300
|
+
bool hasSelected = false;
|
|
2301
|
+
NSInteger selected = 0;
|
|
2302
|
+
double fontSize = 0; // 0: the menu font's own size
|
|
2303
|
+
NSString* fontFamily = @""; // a family the system knows, or the menu font
|
|
2304
|
+
NSString* appearance = @""; // "light" | "dark" | "": the window's
|
|
2305
|
+
bool rtl = false;
|
|
2306
|
+
};
|
|
2307
|
+
|
|
2308
|
+
static PopUpSpec ParsePopUpSpec(Napi::Object o) {
|
|
2309
|
+
PopUpSpec s;
|
|
2310
|
+
Napi::Value items = o.Get("items");
|
|
2311
|
+
if (items.IsArray()) s.items = ParseMenuItems(items.As<Napi::Array>());
|
|
2312
|
+
Napi::Value frame = o.Get("frame");
|
|
2313
|
+
if (frame.IsArray()) {
|
|
2314
|
+
Napi::Array a = frame.As<Napi::Array>();
|
|
2315
|
+
for (uint32_t i = 0; i < 4 && i < a.Length(); i++) {
|
|
2316
|
+
Napi::Value v = a.Get(i);
|
|
2317
|
+
if (v.IsNumber()) s.frame[i] = v.As<Napi::Number>().DoubleValue();
|
|
2318
|
+
}
|
|
2319
|
+
}
|
|
2320
|
+
Napi::Value sel = o.Get("selected");
|
|
2321
|
+
if (sel.IsNumber()) {
|
|
2322
|
+
s.hasSelected = true;
|
|
2323
|
+
s.selected = (NSInteger)sel.As<Napi::Number>().Int64Value();
|
|
2324
|
+
}
|
|
2325
|
+
s.fontSize = BNumOr(o, "fontSize", 0);
|
|
2326
|
+
s.fontFamily = BStrOr(o, "fontFamily", @"");
|
|
2327
|
+
s.appearance = BStrOr(o, "appearance", @"");
|
|
2328
|
+
s.rtl = BBoolOr(o, "rtl", false);
|
|
2329
|
+
return s;
|
|
2330
|
+
}
|
|
2331
|
+
|
|
2332
|
+
// The family asked for when the system has it — a face it matched under
|
|
2333
|
+
// another name is not it — and the menu font at that size otherwise.
|
|
2334
|
+
static NSFont* PopUpFont(NSString* family, double size) {
|
|
2335
|
+
CGFloat sz = size > 0 ? size : [NSFont systemFontSize];
|
|
2336
|
+
if (family.length) {
|
|
2337
|
+
NSFontDescriptor* d = [NSFontDescriptor
|
|
2338
|
+
fontDescriptorWithFontAttributes:@{NSFontFamilyAttribute : family}];
|
|
2339
|
+
NSFont* f = [NSFont fontWithDescriptor:d size:sz];
|
|
2340
|
+
if (f && [f.familyName caseInsensitiveCompare:family] == NSOrderedSame) return f;
|
|
2341
|
+
}
|
|
2342
|
+
return [NSFont menuFontOfSize:size > 0 ? size : 0];
|
|
2343
|
+
}
|
|
2344
|
+
|
|
2345
|
+
// What a pop-up owes JS, hung on its menu: pump mode's callback, or a
|
|
2346
|
+
// worker's threadsafe function.
|
|
2347
|
+
@interface CALPopUpPending : NSObject {
|
|
2348
|
+
@public
|
|
2349
|
+
napi_env env_;
|
|
2350
|
+
Napi::FunctionReference cb_;
|
|
2351
|
+
bool threaded_;
|
|
2352
|
+
CALTsfn tsfn_;
|
|
2353
|
+
}
|
|
2354
|
+
@end
|
|
2355
|
+
@implementation CALPopUpPending
|
|
2356
|
+
@end
|
|
2357
|
+
|
|
2358
|
+
static char kPopUpPendingKey;
|
|
2359
|
+
|
|
2360
|
+
// The menu as the spec describes it, and the cell that will run it.
|
|
2361
|
+
static NSPopUpButtonCell* MakePopUp(const PopUpSpec& spec, CALPopUpTarget* target) {
|
|
2362
|
+
NSMenu* menu = BuildMenu(spec.items, target);
|
|
2363
|
+
NSFont* font = PopUpFont(spec.fontFamily, spec.fontSize);
|
|
2364
|
+
menu.font = font;
|
|
2365
|
+
if ([spec.appearance isEqualToString:@"dark"]) {
|
|
2366
|
+
menu.appearance = [NSAppearance appearanceNamed:NSAppearanceNameDarkAqua];
|
|
2367
|
+
} else if ([spec.appearance isEqualToString:@"light"]) {
|
|
2368
|
+
menu.appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua];
|
|
2369
|
+
}
|
|
2370
|
+
if (spec.rtl) {
|
|
2371
|
+
menu.userInterfaceLayoutDirection = NSUserInterfaceLayoutDirectionRightToLeft;
|
|
2372
|
+
}
|
|
2373
|
+
NSPopUpButtonCell* cell = [[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:NO];
|
|
2374
|
+
cell.autoenablesItems = NO;
|
|
2375
|
+
cell.font = font;
|
|
2376
|
+
cell.menu = menu;
|
|
2377
|
+
NSInteger at = spec.hasSelected ? [menu indexOfItemWithTag:spec.selected] : -1;
|
|
2378
|
+
if (at >= 0) [cell selectItemAtIndex:at];
|
|
2379
|
+
else [cell selectItem:nil];
|
|
2380
|
+
return cell;
|
|
2381
|
+
}
|
|
2382
|
+
|
|
2383
|
+
// Track it over the control, on the UI thread, and say what was chosen.
|
|
2384
|
+
static CALValueBlock TrackPopUp(NSPopUpButtonCell* cell, CALPopUpTarget* target,
|
|
2385
|
+
NSWindow* win, const PopUpSpec& spec) {
|
|
2386
|
+
if (win && win.contentView) {
|
|
2387
|
+
NSView* view = win.contentView;
|
|
2388
|
+
double x = spec.frame[0], y = spec.frame[1];
|
|
2389
|
+
double w = spec.frame[2], h = spec.frame[3];
|
|
2390
|
+
// the frame is top-left, as every rect here is
|
|
2391
|
+
NSRect r = NSMakeRect(x, view.isFlipped ? y : view.bounds.size.height - y - h, w, h);
|
|
2392
|
+
[cell attachPopUpWithFrame:r inView:view];
|
|
2393
|
+
[cell performClickWithFrame:r inView:view];
|
|
2394
|
+
[cell dismissPopUp];
|
|
2395
|
+
}
|
|
2396
|
+
bool picked = target->picked_;
|
|
2397
|
+
double chosen = (double)target->chosen_;
|
|
2398
|
+
return ^Napi::Value(Napi::Env e) {
|
|
2399
|
+
return picked ? Napi::Value(Napi::Number::New(e, chosen)) : Napi::Value(e.Null());
|
|
2400
|
+
};
|
|
2401
|
+
}
|
|
2402
|
+
|
|
2403
|
+
// The one place a pop-up answers; the pending record comes off first.
|
|
2404
|
+
static void FinishPopUp(NSMenu* menu, CALValueBlock answer) {
|
|
2405
|
+
CALPopUpPending* p = objc_getAssociatedObject(menu, &kPopUpPendingKey);
|
|
2406
|
+
if (!p) return;
|
|
2407
|
+
objc_setAssociatedObject(menu, &kPopUpPendingKey, nil,
|
|
2408
|
+
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
|
2409
|
+
if (p->threaded_) {
|
|
2410
|
+
CALReply(p->tsfn_, answer);
|
|
2411
|
+
return;
|
|
2412
|
+
}
|
|
2413
|
+
Napi::Env env(p->env_);
|
|
2414
|
+
Napi::HandleScope scope(env);
|
|
2415
|
+
Napi::Value result = answer(env);
|
|
2416
|
+
Napi::FunctionReference cb = std::move(p->cb_);
|
|
2417
|
+
cb.Call({result});
|
|
2418
|
+
}
|
|
2419
|
+
|
|
2420
|
+
// popUpMenu(win, spec, cb) -> menu handle
|
|
2421
|
+
// spec: { items, // setMainMenu's item vocabulary
|
|
2422
|
+
// frame: [x, y, w, h], // the control, in the window's content,
|
|
2423
|
+
// // top-left, points
|
|
2424
|
+
// selected?, // the id placed over the control, checked
|
|
2425
|
+
// fontSize?, fontFamily?,// the menu's font; the menu font otherwise
|
|
2426
|
+
// appearance?: 'light' | 'dark', rtl? }
|
|
2427
|
+
// cb(id | null) — the item chosen, or null for a menu dismissed
|
|
2428
|
+
static Napi::Value PopUpMenuFn(const Napi::CallbackInfo& info) {
|
|
2429
|
+
Napi::Env env = info.Env();
|
|
2430
|
+
if (!info[0].IsExternal() || !info[1].IsObject() || !info[2].IsFunction()) {
|
|
2431
|
+
Napi::TypeError::New(env, "popUpMenu(win, spec, cb): window handle, spec object and callback required")
|
|
2432
|
+
.ThrowAsJavaScriptException();
|
|
2433
|
+
return env.Undefined();
|
|
2434
|
+
}
|
|
2435
|
+
id owner = CALHandleTarget(info[0]);
|
|
2436
|
+
PopUpSpec spec = ParsePopUpSpec(info[1].As<Napi::Object>());
|
|
2437
|
+
|
|
2438
|
+
if (pthread_main_np()) {
|
|
2439
|
+
BEnsureApp();
|
|
2440
|
+
CALPopUpTarget* target = [CALPopUpTarget new];
|
|
2441
|
+
target->source_ = "popup";
|
|
2442
|
+
NSPopUpButtonCell* cell = MakePopUp(spec, target);
|
|
2443
|
+
CALPopUpPending* p = [CALPopUpPending new];
|
|
2444
|
+
p->env_ = (napi_env)env;
|
|
2445
|
+
p->cb_ = Napi::Persistent(info[2].As<Napi::Function>());
|
|
2446
|
+
p->threaded_ = false;
|
|
2447
|
+
objc_setAssociatedObject(cell.menu, &kPopUpPendingKey, p,
|
|
2448
|
+
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
|
2449
|
+
Napi::Value handle = BWrapRetained(env, cell.menu);
|
|
2450
|
+
FinishPopUp(cell.menu, TrackPopUp(cell, target, owner ? CALResolve(owner) : nil, spec));
|
|
2451
|
+
return handle;
|
|
2452
|
+
}
|
|
2453
|
+
|
|
2454
|
+
CALHandle* h = CALNewHandle();
|
|
2455
|
+
Napi::Value handle = CALWrapHandle(env, h, false);
|
|
2456
|
+
CALTsfn tsfn = CALReplyTo(env, info[2].As<Napi::Function>(), "appkit:popUpMenu");
|
|
2457
|
+
CALOnUIModal(^{
|
|
2458
|
+
BEnsureApp();
|
|
2459
|
+
CALPopUpTarget* target = [CALPopUpTarget new];
|
|
2460
|
+
target->source_ = "popup";
|
|
2461
|
+
NSPopUpButtonCell* cell = MakePopUp(spec, target);
|
|
2462
|
+
CALPopUpPending* p = [CALPopUpPending new];
|
|
2463
|
+
p->threaded_ = true;
|
|
2464
|
+
p->tsfn_ = tsfn;
|
|
2465
|
+
objc_setAssociatedObject(cell.menu, &kPopUpPendingKey, p,
|
|
2466
|
+
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
|
2467
|
+
h->object_ = cell.menu;
|
|
2468
|
+
FinishPopUp(cell.menu, TrackPopUp(cell, target, owner ? CALResolve(owner) : nil, spec));
|
|
2469
|
+
});
|
|
2470
|
+
return handle;
|
|
2471
|
+
}
|
|
2472
|
+
|
|
2473
|
+
// cancelPopUpMenu(menu) -> bool — end the tracking: the pop-up answers null.
|
|
2474
|
+
// A pop-up that has answered already is left alone.
|
|
2475
|
+
static Napi::Value CancelPopUpMenuFn(const Napi::CallbackInfo& info) {
|
|
2476
|
+
Napi::Env env = info.Env();
|
|
2477
|
+
if (!info[0].IsExternal()) {
|
|
2478
|
+
Napi::TypeError::New(env, "cancelPopUpMenu: menu handle required")
|
|
2479
|
+
.ThrowAsJavaScriptException();
|
|
2480
|
+
return env.Undefined();
|
|
2481
|
+
}
|
|
2482
|
+
id target = CALHandleTarget(info[0]);
|
|
2483
|
+
__block bool ok = false;
|
|
2484
|
+
CALOnUI(^{
|
|
2485
|
+
NSMenu* menu = CALResolve(target);
|
|
2486
|
+
if (!menu || !objc_getAssociatedObject(menu, &kPopUpPendingKey)) return;
|
|
2487
|
+
[menu cancelTrackingWithoutAnimation];
|
|
2488
|
+
ok = true;
|
|
2489
|
+
});
|
|
2490
|
+
return BCommandAnswer(env, ok);
|
|
2491
|
+
}
|
|
2492
|
+
|
|
2493
|
+
// popUpMenuInfo(menu, cb?) — an open pop-up as data, for tests: the tree in
|
|
2494
|
+
// mainMenuInfo's shape, its font and its appearance; null once it answered.
|
|
2495
|
+
static Napi::Value PopUpMenuInfoFn(const Napi::CallbackInfo& info) {
|
|
2496
|
+
id target = CALHandleTarget(info[0]);
|
|
2497
|
+
return CALAnswer(info, "popUpMenuInfo", ^CALValueBlock {
|
|
2498
|
+
NSMenu* menu = CALResolve(target);
|
|
2499
|
+
if (!menu || !objc_getAssociatedObject(menu, &kPopUpPendingKey)) {
|
|
2500
|
+
return ^Napi::Value(Napi::Env e) { return e.Null(); };
|
|
2501
|
+
}
|
|
2502
|
+
std::shared_ptr<MenuInfoData> data = MenuInfoOf(menu);
|
|
2503
|
+
std::string family = menu.font.familyName.UTF8String ?: "";
|
|
2504
|
+
double size = menu.font.pointSize;
|
|
2505
|
+
std::string appearance = menu.appearance.name.UTF8String ?: "";
|
|
2506
|
+
bool rtl = menu.userInterfaceLayoutDirection == NSUserInterfaceLayoutDirectionRightToLeft;
|
|
2507
|
+
return ^Napi::Value(Napi::Env e) {
|
|
2508
|
+
Napi::Object o = MenuInfoValue(e, *data);
|
|
2509
|
+
o.Set("fontFamily", family);
|
|
2510
|
+
o.Set("fontSize", size);
|
|
2511
|
+
o.Set("appearance", appearance);
|
|
2512
|
+
o.Set("rtl", rtl);
|
|
2513
|
+
return o;
|
|
2514
|
+
};
|
|
2515
|
+
});
|
|
2516
|
+
}
|
|
2517
|
+
|
|
2518
|
+
// activatePopUpMenuItem(menu, index, cb?) -> bool — choose an open pop-up's
|
|
2519
|
+
// item by index, the way tracking would, and end the tracking. For tests.
|
|
2520
|
+
static Napi::Value ActivatePopUpMenuItemFn(const Napi::CallbackInfo& info) {
|
|
2521
|
+
id target = CALHandleTarget(info[0]);
|
|
2522
|
+
NSInteger index = info.Length() > 1 && info[1].IsNumber()
|
|
2523
|
+
? (NSInteger)info[1].As<Napi::Number>().Int64Value()
|
|
2524
|
+
: -1;
|
|
2525
|
+
return CALAnswer(info, "activatePopUpMenuItem", ^CALValueBlock {
|
|
2526
|
+
NSMenu* menu = CALResolve(target);
|
|
2527
|
+
bool ok = menu && objc_getAssociatedObject(menu, &kPopUpPendingKey) &&
|
|
2528
|
+
ActivateInMenu(menu, {index});
|
|
2529
|
+
if (ok) [menu cancelTrackingWithoutAnimation];
|
|
2530
|
+
return BoolAnswer(ok);
|
|
2531
|
+
});
|
|
2532
|
+
}
|
|
2533
|
+
|
|
2262
2534
|
// ---------------------------------------------------------------------------
|
|
2263
2535
|
// status items — NSStatusItem, the menu-bar extra (the "tray"). The cocoa
|
|
2264
2536
|
// counterpart of the freedesktop StatusNotifierItem: an icon or a title in
|
|
@@ -7367,6 +7639,10 @@ void InitBackend(Napi::Env env, Napi::Object exports) {
|
|
|
7367
7639
|
BFN("setDockMenu", SetDockMenuFn);
|
|
7368
7640
|
BFN("dockMenuInfo", DockMenuInfoFn);
|
|
7369
7641
|
BFN("activateDockMenuItem", ActivateDockMenuItemFn);
|
|
7642
|
+
BFN("popUpMenu", PopUpMenuFn);
|
|
7643
|
+
BFN("cancelPopUpMenu", CancelPopUpMenuFn);
|
|
7644
|
+
BFN("popUpMenuInfo", PopUpMenuInfoFn);
|
|
7645
|
+
BFN("activatePopUpMenuItem", ActivatePopUpMenuItemFn);
|
|
7370
7646
|
BFN("initApp", InitAppFn);
|
|
7371
7647
|
BFN("setActivationPolicy", SetActivationPolicyFn);
|
|
7372
7648
|
BFN("setDockBadge", SetDockBadgeFn);
|