@windowkit/appkit 0.8.0 → 0.10.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.
@@ -0,0 +1,157 @@
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
+ #include "channel.h" // CALOnUI: the session is the UI thread's; CALCallJS
44
+
45
+ // src/backend.mm — NSApplication set up exactly once, whichever verb comes
46
+ // first. The sampler is AppKit UI like a panel, so it wants an app.
47
+ void BEnsureApp();
48
+
49
+ // What one finished session owes every caller waiting on it.
50
+ struct ColorAnswer {
51
+ bool cancelled = false; // dismissed without picking
52
+ double r = 0, g = 0, b = 0; // sRGB, 0–1
53
+ std::string error; // set instead when the colour could not be read
54
+ };
55
+
56
+ // The callers of the session that is up, in the order they asked; empty when
57
+ // none is. Touched on the UI thread only: the verb's own part runs there
58
+ // (inline in pump mode, a command from a worker), and so does AppKit's
59
+ // handler, inside the pump or the run.
60
+ static std::vector<Napi::ThreadSafeFunction> gWaiting;
61
+
62
+ // cb(err) | cb(null, null) | cb(null, { r, g, b }) on the JS thread, then the
63
+ // thread-safe function goes — which is what held the loop open meanwhile.
64
+ static void Deliver(Napi::ThreadSafeFunction tsfn, const ColorAnswer& answer) {
65
+ ColorAnswer* a = new ColorAnswer(answer);
66
+ napi_status st = tsfn.BlockingCall(
67
+ a, [](Napi::Env env, Napi::Function cb, ColorAnswer* a) {
68
+ // an environment on its way out gets no answer (channel.h)
69
+ if (!CALCanCallIntoJS(env)) {
70
+ delete a;
71
+ return;
72
+ }
73
+ if (!a->error.empty()) {
74
+ CALCallJS(env, cb, {Napi::Error::New(env, a->error).Value()});
75
+ } else if (a->cancelled) {
76
+ CALCallJS(env, cb, {env.Null(), env.Null()});
77
+ } else {
78
+ Napi::Object c = Napi::Object::New(env);
79
+ c.Set("r", Napi::Number::New(env, a->r));
80
+ c.Set("g", Napi::Number::New(env, a->g));
81
+ c.Set("b", Napi::Number::New(env, a->b));
82
+ CALCallJS(env, cb, {env.Null(), c});
83
+ }
84
+ delete a;
85
+ });
86
+ if (st != napi_ok) delete a;
87
+ tsfn.Release();
88
+ }
89
+
90
+ // The one place a session answers. The waiting list comes off first, so a
91
+ // sampleScreenColor called from inside a callback opens a fresh session
92
+ // rather than joining the one that has just ended (the file panels' rule).
93
+ static void FinishSession(NSColor* picked) {
94
+ ColorAnswer a;
95
+ if (!picked) {
96
+ a.cancelled = true;
97
+ } else {
98
+ // Whatever space the sampler read the pixel in — a display's, wide-gamut
99
+ // on most Macs now — in the one every colour crosses this bridge in (the
100
+ // 0.5.1 rule). ColorSync gamut-maps on the way, so a Display P3 red
101
+ // arrives as sRGB (1, 0, 0) rather than as components outside 0–1.
102
+ NSColor* srgb = [picked colorUsingColorSpace:NSColorSpace.sRGBColorSpace];
103
+ if (srgb) {
104
+ a.r = srgb.redComponent;
105
+ a.g = srgb.greenComponent;
106
+ a.b = srgb.blueComponent;
107
+ } else {
108
+ // Nothing the sampler can hand back should land here (a pattern colour
109
+ // is the shape with no sRGB form), but a colour that cannot be read is
110
+ // not a cancel, so it is said rather than answered as one.
111
+ a.error =
112
+ "sampleScreenColor: the sampler answered a colour with no sRGB form";
113
+ }
114
+ }
115
+ std::vector<Napi::ThreadSafeFunction> waiting;
116
+ waiting.swap(gWaiting);
117
+ for (Napi::ThreadSafeFunction& tsfn : waiting) Deliver(tsfn, a);
118
+ }
119
+
120
+ // sampleScreenColor(cb) — show the system sampler and answer once,
121
+ // asynchronously: cb(null, { r, g, b }) in sRGB, or cb(null, null) when the
122
+ // user dismissed it. A second call while a sampler is up joins that session
123
+ // instead of showing another, and both callbacks get the same answer.
124
+ static Napi::Value SampleScreenColor(const Napi::CallbackInfo& info) {
125
+ Napi::Env env = info.Env();
126
+ if (info.Length() < 1 || !info[0].IsFunction()) {
127
+ Napi::TypeError::New(env, "sampleScreenColor: expected a callback (cb)")
128
+ .ThrowAsJavaScriptException();
129
+ return env.Undefined();
130
+ }
131
+ // made in the caller's environment, which is the one answered
132
+ Napi::ThreadSafeFunction tsfn = Napi::ThreadSafeFunction::New(
133
+ env, info[0].As<Napi::Function>(), "appkit:sampleScreenColor", 0, 1);
134
+ CALOnUI(^{
135
+ @autoreleasepool {
136
+ BEnsureApp();
137
+ bool sessionUp = !gWaiting.empty();
138
+ gWaiting.push_back(tsfn);
139
+ if (sessionUp) return;
140
+ // Nothing here keeps the sampler alive: AppKit retains it for as long
141
+ // as the session lasts, which its header promises in as many words.
142
+ [[NSColorSampler new] showSamplerWithSelectionHandler:^(NSColor* c) {
143
+ FinishSession(c);
144
+ }];
145
+ }
146
+ });
147
+ return env.Undefined();
148
+ }
149
+
150
+ // ---------------------------------------------------------------------------
151
+ // registration (called from addon.mm's Init)
152
+ // ---------------------------------------------------------------------------
153
+
154
+ void InitScreenColor(Napi::Env env, Napi::Object exports) {
155
+ exports.Set("sampleScreenColor",
156
+ Napi::Function::New(env, SampleScreenColor));
157
+ }