@windowkit/appkit 0.6.0 → 0.8.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/src/backend.mm CHANGED
@@ -950,6 +950,8 @@ static Napi::Value SetBackendEventCallback(const Napi::CallbackInfo& info) {
950
950
 
951
951
  // notifications.mm: responses that arrived before a listener was installed
952
952
  void CALNotificationsReplayHeld(Napi::Env env);
953
+ // calendars.mm: an EventKit store change from before there was a listener
954
+ void CALCalendarsReplayHeld(Napi::Env env);
953
955
 
954
956
  static Napi::Value Pump2(const Napi::CallbackInfo& info) {
955
957
  Napi::Env env = info.Env();
@@ -957,9 +959,10 @@ static Napi::Value Pump2(const Napi::CallbackInfo& info) {
957
959
  // what came before the callback did, ahead of this tick's input so the
958
960
  // renderer hears of it first: the launch's URL (or a Dock click), then a
959
961
  // notification acted on before the callback existed (one that launched
960
- // the app, say)
962
+ // the app, say), then a calendar store that changed under it
961
963
  FlushPendingAppEvents(env);
962
964
  CALNotificationsReplayHeld(env);
965
+ CALCalendarsReplayHeld(env);
963
966
  @autoreleasepool {
964
967
  while (true) {
965
968
  NSEvent* e = [NSApp nextEventMatchingMask:NSEventMaskAny
@@ -1262,6 +1265,98 @@ static Napi::Value CopySurfaceRegion(const Napi::CallbackInfo& info) {
1262
1265
  return env.Undefined();
1263
1266
  }
1264
1267
 
1268
+ // blitSurface(src, sx, sy, w, h, dst, dx, dy, clip?) -> [x, y, w, h] | null
1269
+ // A row memcpy of a rect of one surface into another, at surfaces of any
1270
+ // two sizes. copySurfaceRegion is the same-size special case a swapchain
1271
+ // wants; this is the general one, for a caller compositing an offscreen
1272
+ // surface into a window at a translate — a terminal's grid, an element's
1273
+ // retained scene — where drawSurface builds a CGImage of the whole source
1274
+ // and blends it. On an M1 Pro, a 2000x1620 grid into a window-sized
1275
+ // surface: 1.2ms that way, 0.42ms this way.
1276
+ //
1277
+ // Straight copy, no blending, alpha included: this is the `copy` op, and a
1278
+ // caller reaches it by setting that blend mode and drawing at 1:1 under a
1279
+ // translate-only transform. Coordinates are device pixels with a top-left
1280
+ // origin — the convention createSurface's CTM gives user space, and the one
1281
+ // copySurfaceRegion's rects already use. Neither the destination's CTM nor
1282
+ // its clip is visible to a memcpy, so `clip`, when given, is [x, y, w, h] in
1283
+ // the DESTINATION's pixels and the caller passes the clip it is drawing
1284
+ // under; a damage region of several rects is several calls. The rect copied
1285
+ // is the destination rect intersected with that clip and with both surfaces'
1286
+ // bounds, the source origin moving with it — returned, or null when the
1287
+ // intersection is empty and nothing moved.
1288
+ //
1289
+ // When either surface is IOSurface-backed the caller owes the usual
1290
+ // surfaceLock bracketing. Two handles onto one bitmap are refused:
1291
+ // overlapping memcpy rows have no defined result, and the check is on the
1292
+ // backing store, not the handle, so the two ends of a shared IOSurface do
1293
+ // not slip through as different handles.
1294
+ static Napi::Value BlitSurface(const Napi::CallbackInfo& info) {
1295
+ Napi::Env env = info.Env();
1296
+ CALSurface* src = SurfaceFrom(info[0]);
1297
+ if (!src) return env.Null();
1298
+ CALSurface* dst = SurfaceFrom(info[5]);
1299
+ if (!dst) return env.Null();
1300
+
1301
+ const uint8_t* sbase = (const uint8_t*)CGBitmapContextGetData(src->ctx);
1302
+ uint8_t* dbase = (uint8_t*)CGBitmapContextGetData(dst->ctx);
1303
+ if (!sbase || !dbase) return env.Null();
1304
+ if (sbase == dbase) {
1305
+ Napi::Error::New(env, "blitSurface: source and destination are one bitmap")
1306
+ .ThrowAsJavaScriptException();
1307
+ return env.Null();
1308
+ }
1309
+
1310
+ long sx = (long)info[1].As<Napi::Number>().Int64Value();
1311
+ long sy = (long)info[2].As<Napi::Number>().Int64Value();
1312
+ long w = (long)info[3].As<Napi::Number>().Int64Value();
1313
+ long h = (long)info[4].As<Napi::Number>().Int64Value();
1314
+ long dx = (long)info[6].As<Napi::Number>().Int64Value();
1315
+ long dy = (long)info[7].As<Napi::Number>().Int64Value();
1316
+
1317
+ // the destination rect trimmed to the clip, the source origin moving by
1318
+ // whatever each edge takes off
1319
+ if (info.Length() > 8 && !info[8].IsNull() && !info[8].IsUndefined()) {
1320
+ if (!info[8].IsArray() || info[8].As<Napi::Array>().Length() < 4) {
1321
+ Napi::TypeError::New(env, "blitSurface: clip must be [x, y, w, h]")
1322
+ .ThrowAsJavaScriptException();
1323
+ return env.Null();
1324
+ }
1325
+ Napi::Array clip = info[8].As<Napi::Array>();
1326
+ long cx = (long)clip.Get((uint32_t)0).As<Napi::Number>().Int64Value();
1327
+ long cy = (long)clip.Get((uint32_t)1).As<Napi::Number>().Int64Value();
1328
+ long cw = (long)clip.Get((uint32_t)2).As<Napi::Number>().Int64Value();
1329
+ long ch = (long)clip.Get((uint32_t)3).As<Napi::Number>().Int64Value();
1330
+ if (dx < cx) { long over = cx - dx; sx += over; dx += over; w -= over; }
1331
+ if (dy < cy) { long over = cy - dy; sy += over; dy += over; h -= over; }
1332
+ if (dx + w > cx + cw) w = cx + cw - dx;
1333
+ if (dy + h > cy + ch) h = cy + ch - dy;
1334
+ }
1335
+ // and to both surfaces
1336
+ if (sx < 0) { dx -= sx; w += sx; sx = 0; }
1337
+ if (sy < 0) { dy -= sy; h += sy; sy = 0; }
1338
+ if (dx < 0) { sx -= dx; w += dx; dx = 0; }
1339
+ if (dy < 0) { sy -= dy; h += dy; dy = 0; }
1340
+ if (sx + w > (long)src->width) w = (long)src->width - sx;
1341
+ if (sy + h > (long)src->height) h = (long)src->height - sy;
1342
+ if (dx + w > (long)dst->width) w = (long)dst->width - dx;
1343
+ if (dy + h > (long)dst->height) h = (long)dst->height - dy;
1344
+ if (w <= 0 || h <= 0) return env.Null();
1345
+
1346
+ size_t srow = CGBitmapContextGetBytesPerRow(src->ctx);
1347
+ size_t drow = CGBitmapContextGetBytesPerRow(dst->ctx);
1348
+ for (long r = 0; r < h; r++) {
1349
+ memcpy(dbase + (size_t)(dy + r) * drow + (size_t)dx * 4,
1350
+ sbase + (size_t)(sy + r) * srow + (size_t)sx * 4, (size_t)w * 4);
1351
+ }
1352
+ Napi::Array out = Napi::Array::New(env, 4);
1353
+ out.Set((uint32_t)0, (double)dx);
1354
+ out.Set((uint32_t)1, (double)dy);
1355
+ out.Set((uint32_t)2, (double)w);
1356
+ out.Set((uint32_t)3, (double)h);
1357
+ return out;
1358
+ }
1359
+
1265
1360
  static Napi::Value SurfaceSize(const Napi::CallbackInfo& info) {
1266
1361
  CALSurface* s = SurfaceFrom(info[0]);
1267
1362
  if (!s) return info.Env().Undefined();
@@ -2724,6 +2819,56 @@ static Napi::Value CtxSetLineCap(const Napi::CallbackInfo& info) {
2724
2819
  : kCGLineCapButt);
2725
2820
  return info.Env().Undefined();
2726
2821
  }
2822
+ // ctxSetBlendMode(surface, mode) -> boolean — canvas's
2823
+ // globalCompositeOperation, in CoreGraphics' spelling. Every one of the
2824
+ // canvas names has an exact CGBlendMode: the Porter-Duff dozen a 2d context
2825
+ // composites with, and the separable and non-separable blend modes below
2826
+ // them. `copy` is the one the compositing paths care about — it is what
2827
+ // makes a paint a replacement rather than a blend, and what lets a
2828
+ // translate-only 1:1 drawSurface take blitSurface's memcpy instead.
2829
+ //
2830
+ // A name the list does not have leaves the context's blend mode alone and
2831
+ // answers false, which is canvas's rule for an unknown value (it is
2832
+ // ignored, not reset) and lets a caller keep its own property in step. The
2833
+ // mode is gstate, so ctxSave/ctxRestore bracket it like any other.
2834
+ static Napi::Value CtxSetBlendMode(const Napi::CallbackInfo& info) {
2835
+ Napi::Env env = info.Env();
2836
+ std::string mode = info[1].As<Napi::String>().Utf8Value();
2837
+ CGBlendMode blend;
2838
+ if (mode == "source-over") blend = kCGBlendModeNormal;
2839
+ else if (mode == "copy") blend = kCGBlendModeCopy;
2840
+ else if (mode == "source-in") blend = kCGBlendModeSourceIn;
2841
+ else if (mode == "source-out") blend = kCGBlendModeSourceOut;
2842
+ else if (mode == "source-atop") blend = kCGBlendModeSourceAtop;
2843
+ else if (mode == "destination-over") blend = kCGBlendModeDestinationOver;
2844
+ else if (mode == "destination-in") blend = kCGBlendModeDestinationIn;
2845
+ else if (mode == "destination-out") blend = kCGBlendModeDestinationOut;
2846
+ else if (mode == "destination-atop") blend = kCGBlendModeDestinationAtop;
2847
+ else if (mode == "xor") blend = kCGBlendModeXOR;
2848
+ else if (mode == "lighter") blend = kCGBlendModePlusLighter;
2849
+ else if (mode == "multiply") blend = kCGBlendModeMultiply;
2850
+ else if (mode == "screen") blend = kCGBlendModeScreen;
2851
+ else if (mode == "overlay") blend = kCGBlendModeOverlay;
2852
+ else if (mode == "darken") blend = kCGBlendModeDarken;
2853
+ else if (mode == "lighten") blend = kCGBlendModeLighten;
2854
+ else if (mode == "color-dodge") blend = kCGBlendModeColorDodge;
2855
+ else if (mode == "color-burn") blend = kCGBlendModeColorBurn;
2856
+ else if (mode == "hard-light") blend = kCGBlendModeHardLight;
2857
+ else if (mode == "soft-light") blend = kCGBlendModeSoftLight;
2858
+ else if (mode == "difference") blend = kCGBlendModeDifference;
2859
+ else if (mode == "exclusion") blend = kCGBlendModeExclusion;
2860
+ else if (mode == "hue") blend = kCGBlendModeHue;
2861
+ else if (mode == "saturation") blend = kCGBlendModeSaturation;
2862
+ else if (mode == "color") blend = kCGBlendModeColor;
2863
+ else if (mode == "luminosity") blend = kCGBlendModeLuminosity;
2864
+ // not canvas's, but CoreGraphics' own and the CSS spelling of two of them
2865
+ else if (mode == "clear") blend = kCGBlendModeClear;
2866
+ else if (mode == "plus-lighter") blend = kCGBlendModePlusLighter;
2867
+ else if (mode == "plus-darker") blend = kCGBlendModePlusDarker;
2868
+ else return Napi::Boolean::New(env, false);
2869
+ CGContextSetBlendMode(CtxOf(info[0]), blend);
2870
+ return Napi::Boolean::New(env, true);
2871
+ }
2727
2872
  static Napi::Value CtxSetLineJoin(const Napi::CallbackInfo& info) {
2728
2873
  std::string join = info[1].As<Napi::String>().Utf8Value();
2729
2874
  CGContextSetLineJoin(CtxOf(info[0]),
@@ -5032,6 +5177,7 @@ void InitBackend(Napi::Env env, Napi::Object exports) {
5032
5177
  BFN("surfaceLock", SurfaceLock);
5033
5178
  BFN("surfaceUnlock", SurfaceUnlock);
5034
5179
  BFN("copySurfaceRegion", CopySurfaceRegion);
5180
+ BFN("blitSurface", BlitSurface);
5035
5181
  BFN("surfaceSize", SurfaceSize);
5036
5182
  BFN("ctxSave", CtxSave);
5037
5183
  BFN("ctxRestore", CtxRestore);
@@ -5055,6 +5201,7 @@ void InitBackend(Napi::Env env, Napi::Object exports) {
5055
5201
  BFN("ctxSetGlobalAlpha", CtxSetGlobalAlpha);
5056
5202
  BFN("ctxSetLineCap", CtxSetLineCap);
5057
5203
  BFN("ctxSetLineJoin", CtxSetLineJoin);
5204
+ BFN("ctxSetBlendMode", CtxSetBlendMode);
5058
5205
  BFN("ctxSetLineDash", CtxSetLineDash);
5059
5206
  BFN("ctxFill", CtxFill);
5060
5207
  BFN("ctxStroke", CtxStroke);