@windowkit/appkit 0.8.0 → 0.9.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 CHANGED
@@ -498,6 +498,48 @@ native.beginDrag(win, {
498
498
  way.
499
499
 
500
500
 
501
+ ## Screen colour sampling (the eyedropper)
502
+
503
+ The dropper button on a colour picker asks for one pixel of the screen, which is
504
+ precisely the thing an application cannot draw for itself. `NSColorSampler` (10.15+)
505
+ is macOS's answer: the system shows its own loupe **out of process**, the user
506
+ magnifies and clicks, and the app is told the one colour they picked. Nothing here
507
+ reads the screen, so this needs no Screen Recording grant — and the user gets the
508
+ magnifier every other Mac colour picker shows them.
509
+
510
+ ```js
511
+ const { screenColor, native, app } = require('@windowkit/appkit');
512
+
513
+ app.run(); // the answer arrives on the main thread
514
+
515
+ const color = await screenColor.sample();
516
+ // { r, g, b } — sRGB, 0–1 floats — or null if the user dismissed the sampler
517
+ if (color) paint(`#${[color.r, color.g, color.b].map((c) => Math.round(c * 255).toString(16).padStart(2, '0')).join('')}`);
518
+
519
+ // the same thing, unwrapped: cb(err, color)
520
+ native.sampleScreenColor((err, color) => { /* color, or null on a cancel */ });
521
+ ```
522
+
523
+ - **A cancel is an ordinary outcome, not an error.** Escape (or a dismissal any other
524
+ way) answers `null`, the same shape every rung of react-x11's eyedropper ladder
525
+ answers a cancel with; an `Error` is reserved for a colour that could not be read at
526
+ all.
527
+ - **sRGB, 0–1 floats**, the colour space every colour crosses this bridge in and the
528
+ shape of the `org.freedesktop.portal.Screenshot.PickColor` triple this stands in for.
529
+ The sampler reads the pixel in the display's own space — wide-gamut on most Macs
530
+ now — and ColorSync gamut-maps on the way, so a Display P3 red arrives as
531
+ `{ r: 1, g: 0, b: 0 }` rather than as components outside the range.
532
+ - **One at a time.** A second `sample()` while a loupe is up joins that session rather
533
+ than stacking a second one — AppKit's own rule, its header says a show "begins or
534
+ attaches to an existing color sampling session" — and both callers get the same
535
+ answer, each once.
536
+ - **Nothing dismisses it from code.** AppKit offers no such call, so the session ends
537
+ when the user picks a colour or presses Escape; there is no `cancelPanel` counterpart
538
+ here. Until then the pending sample holds the event loop open like pending I/O, and
539
+ the app has to be pumping (`app.run()`) to hear the answer — it is delivered on the
540
+ main thread, like the location grant.
541
+
542
+
501
543
  ## Privacy authorizations (TCC)
502
544
 
503
545
  macOS decides per process whether an app may use the camera, microphone,
package/binding.gyp CHANGED
@@ -7,7 +7,8 @@
7
7
  "src/backend.mm",
8
8
  "src/permissions.mm",
9
9
  "src/notifications.mm",
10
- "src/calendars.mm"
10
+ "src/calendars.mm",
11
+ "src/screencolor.mm"
11
12
  ],
12
13
  "include_dirs": [
13
14
  "<!@(node -p \"require('node-addon-api').include\")"
package/index.js CHANGED
@@ -331,6 +331,28 @@ const calendars = {
331
331
  reset: () => new Promise((resolve) => native.resetCalendarStore(resolve)),
332
332
  };
333
333
 
334
+ // One colour off the screen — the eyedropper — through NSColorSampler, the
335
+ // system's own sampler: the loupe is drawn out of process, so this needs no
336
+ // Screen Recording grant and the user gets the magnifier every other Mac
337
+ // colour picker shows. Mechanism only: what the colour means past this — the
338
+ // hex a component paints with, whether a cancel resolves null or rejects —
339
+ // stays in the renderer.
340
+ //
341
+ // Nothing dismisses the sampler from code (AppKit offers no such call): the
342
+ // session ends when the user picks a colour or presses Escape, and until
343
+ // then the pending sample holds the event loop open. The answer arrives on
344
+ // the main thread, so the app has to be pumping (app.run()) to hear it.
345
+ const screenColor = {
346
+ // -> { r, g, b } in sRGB, 0–1 floats (the Screenshot portal's (ddd) shape),
347
+ // or null when the user dismissed the sampler without picking: a cancel
348
+ // is an ordinary outcome, not an error. A sample asked for while one is
349
+ // already showing joins that session rather than stacking a second
350
+ // loupe, and both get the same answer.
351
+ sample: () =>
352
+ new Promise((resolve, reject) =>
353
+ native.sampleScreenColor((err, color) => (err ? reject(err) : resolve(color)))),
354
+ };
355
+
334
356
  const accessibility = {
335
357
  // System Settings › Accessibility › Display, as NSWorkspace reports it:
336
358
  // { reduceMotion, reduceTransparency, increaseContrast, differentiateWithoutColor, invertColors }.
@@ -342,5 +364,5 @@ const accessibility = {
342
364
  module.exports = {
343
365
  app, Window, Layer, TextLayer, GradientLayer, ShapeLayer,
344
366
  transaction, withoutAnimations, text, controls, permissions, notifications, calendars,
345
- accessibility, native,
367
+ screenColor, accessibility, native,
346
368
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@windowkit/appkit",
3
- "version": "0.8.0",
3
+ "version": "0.9.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/accessibility-display.js && node test/ignores-mouse-events.js && node test/surface-blit.js && node test/eventkit-permissions.js && node test/calendars.js && node test/calendar-writes.js"
46
+ "test": "node test/app-lifecycle.js && node test/animation.js && node test/accessibility-display.js && node test/ignores-mouse-events.js && node test/surface-blit.js && node test/eventkit-permissions.js && node test/calendars.js && node test/calendar-writes.js && node test/screencolor.js"
47
47
  },
48
48
  "dependencies": {
49
49
  "node-addon-api": "^8.3.0"
Binary file
package/src/addon.mm CHANGED
@@ -1286,6 +1286,9 @@ void InitNotifications(Napi::Env env, Napi::Object exports);
1286
1286
  // src/calendars.mm — the user's calendars and the occurrences in a range
1287
1287
  // through EventKit, and the store's change notification as a backend event.
1288
1288
  void InitCalendars(Napi::Env env, Napi::Object exports);
1289
+ // src/screencolor.mm — the eyedropper: one colour off the screen through
1290
+ // NSColorSampler, the system's own out-of-process sampler.
1291
+ void InitScreenColor(Napi::Env env, Napi::Object exports);
1289
1292
 
1290
1293
  static Napi::Object Init(Napi::Env env, Napi::Object exports) {
1291
1294
  #define FN(js, fn) exports.Set(js, Napi::Function::New(env, fn))
@@ -1329,6 +1332,7 @@ static Napi::Object Init(Napi::Env env, Napi::Object exports) {
1329
1332
  InitPermissions(env, exports);
1330
1333
  InitNotifications(env, exports);
1331
1334
  InitCalendars(env, exports);
1335
+ InitScreenColor(env, exports);
1332
1336
  return exports;
1333
1337
  }
1334
1338
 
@@ -0,0 +1,146 @@
1
+ // @windowkit/appkit screencolor.mm — one colour off the screen, through
2
+ // NSColorSampler: the eyedropper a colour picker's dropper button calls.
3
+ //
4
+ // The system draws the loupe out of process and hands back the one colour the
5
+ // user clicked, so nothing here reads the screen and the app needs no Screen
6
+ // Recording grant of its own. That is why this is the rung that belongs on
7
+ // top of react-x11's ladder rather than a fallback under the desktop
8
+ // portal's PickColor (windowkit/appkit#46): it is the same shape — the
9
+ // desktop samples, the app is told the answer — and the rung below it, a
10
+ // pointer grab and a 1×1 GetImage on the root window, has neither a pointer
11
+ // to grab nor a root window to read on Cocoa.
12
+ //
13
+ // One verb, mechanism only:
14
+ //
15
+ // sampleScreenColor(cb) cb(null, { r, g, b }) — sRGB, 0–1 floats, the
16
+ // portal's (ddd) shape — or cb(null, null) when the
17
+ // user dismissed the sampler without picking. A
18
+ // cancel is an ordinary outcome, not an error.
19
+ //
20
+ // What the colour means past that — the hex string a component paints with,
21
+ // whether a cancel resolves null or rejects — stays in the renderer.
22
+ //
23
+ // One at a time, the way NSColorSampler is: a show "begins or attaches to an
24
+ // existing color sampling session", its header says, so a second call while a
25
+ // loupe is up must not stack a second one. The bridge keeps that promise
26
+ // itself rather than leaving it to the framework — one session, and every
27
+ // caller waiting on it answered once with the same colour — because a
28
+ // callback that never fired would hold node's loop open forever.
29
+ //
30
+ // The answer is asynchronous, always: AppKit calls the handler on the main
31
+ // thread when the session ends, which is during the pump (the location grant
32
+ // in permissions.mm arrives the same way), and it crosses to JS through a
33
+ // thread-safe function, so a sampler still up holds the event loop open like
34
+ // pending I/O. Nothing dismisses it from code — AppKit offers no such call —
35
+ // so the session ends when the user picks a colour or presses Escape.
36
+
37
+ #include <napi.h>
38
+ #import <Cocoa/Cocoa.h>
39
+
40
+ #include <string>
41
+ #include <vector>
42
+
43
+ // src/backend.mm — NSApplication set up exactly once, whichever verb comes
44
+ // first. The sampler is AppKit UI like a panel, so it wants an app.
45
+ void BEnsureApp();
46
+
47
+ // What one finished session owes every caller waiting on it.
48
+ struct ColorAnswer {
49
+ bool cancelled = false; // dismissed without picking
50
+ double r = 0, g = 0, b = 0; // sRGB, 0–1
51
+ std::string error; // set instead when the colour could not be read
52
+ };
53
+
54
+ // The callers of the session that is up, in the order they asked; empty when
55
+ // none is. Touched on the JS thread only: the verb runs there, and so does
56
+ // AppKit's handler — node's main thread is the process main thread, and the
57
+ // handler arrives on it inside the pump.
58
+ static std::vector<Napi::ThreadSafeFunction> gWaiting;
59
+
60
+ // cb(err) | cb(null, null) | cb(null, { r, g, b }) on the JS thread, then the
61
+ // thread-safe function goes — which is what held the loop open meanwhile.
62
+ static void Deliver(Napi::ThreadSafeFunction tsfn, const ColorAnswer& answer) {
63
+ ColorAnswer* a = new ColorAnswer(answer);
64
+ napi_status st = tsfn.BlockingCall(
65
+ a, [](Napi::Env env, Napi::Function cb, ColorAnswer* a) {
66
+ if (!a->error.empty()) {
67
+ cb.Call({Napi::Error::New(env, a->error).Value()});
68
+ } else if (a->cancelled) {
69
+ cb.Call({env.Null(), env.Null()});
70
+ } else {
71
+ Napi::Object c = Napi::Object::New(env);
72
+ c.Set("r", Napi::Number::New(env, a->r));
73
+ c.Set("g", Napi::Number::New(env, a->g));
74
+ c.Set("b", Napi::Number::New(env, a->b));
75
+ cb.Call({env.Null(), c});
76
+ }
77
+ delete a;
78
+ });
79
+ if (st != napi_ok) delete a;
80
+ tsfn.Release();
81
+ }
82
+
83
+ // The one place a session answers. The waiting list comes off first, so a
84
+ // sampleScreenColor called from inside a callback opens a fresh session
85
+ // rather than joining the one that has just ended (the file panels' rule).
86
+ static void FinishSession(NSColor* picked) {
87
+ ColorAnswer a;
88
+ if (!picked) {
89
+ a.cancelled = true;
90
+ } else {
91
+ // Whatever space the sampler read the pixel in — a display's, wide-gamut
92
+ // on most Macs now — in the one every colour crosses this bridge in (the
93
+ // 0.5.1 rule). ColorSync gamut-maps on the way, so a Display P3 red
94
+ // arrives as sRGB (1, 0, 0) rather than as components outside 0–1.
95
+ NSColor* srgb = [picked colorUsingColorSpace:NSColorSpace.sRGBColorSpace];
96
+ if (srgb) {
97
+ a.r = srgb.redComponent;
98
+ a.g = srgb.greenComponent;
99
+ a.b = srgb.blueComponent;
100
+ } else {
101
+ // Nothing the sampler can hand back should land here (a pattern colour
102
+ // is the shape with no sRGB form), but a colour that cannot be read is
103
+ // not a cancel, so it is said rather than answered as one.
104
+ a.error =
105
+ "sampleScreenColor: the sampler answered a colour with no sRGB form";
106
+ }
107
+ }
108
+ std::vector<Napi::ThreadSafeFunction> waiting;
109
+ waiting.swap(gWaiting);
110
+ for (Napi::ThreadSafeFunction& tsfn : waiting) Deliver(tsfn, a);
111
+ }
112
+
113
+ // sampleScreenColor(cb) — show the system sampler and answer once,
114
+ // asynchronously: cb(null, { r, g, b }) in sRGB, or cb(null, null) when the
115
+ // user dismissed it. A second call while a sampler is up joins that session
116
+ // instead of showing another, and both callbacks get the same answer.
117
+ static Napi::Value SampleScreenColor(const Napi::CallbackInfo& info) {
118
+ Napi::Env env = info.Env();
119
+ if (info.Length() < 1 || !info[0].IsFunction()) {
120
+ Napi::TypeError::New(env, "sampleScreenColor: expected a callback (cb)")
121
+ .ThrowAsJavaScriptException();
122
+ return env.Undefined();
123
+ }
124
+ @autoreleasepool {
125
+ BEnsureApp();
126
+ bool sessionUp = !gWaiting.empty();
127
+ gWaiting.push_back(Napi::ThreadSafeFunction::New(
128
+ env, info[0].As<Napi::Function>(), "appkit:sampleScreenColor", 0, 1));
129
+ if (sessionUp) return env.Undefined();
130
+ // Nothing here keeps the sampler alive: AppKit retains it for as long as
131
+ // the session lasts, which its header promises in as many words.
132
+ [[NSColorSampler new] showSamplerWithSelectionHandler:^(NSColor* c) {
133
+ FinishSession(c);
134
+ }];
135
+ }
136
+ return env.Undefined();
137
+ }
138
+
139
+ // ---------------------------------------------------------------------------
140
+ // registration (called from addon.mm's Init)
141
+ // ---------------------------------------------------------------------------
142
+
143
+ void InitScreenColor(Napi::Env env, Napi::Object exports) {
144
+ exports.Set("sampleScreenColor",
145
+ Napi::Function::New(env, SampleScreenColor));
146
+ }