@windowkit/appkit 0.15.1 → 0.16.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
@@ -412,7 +412,7 @@ and returns a `CGImage` for `layer.contents`:
412
412
 
413
413
  ```js
414
414
  const img = ca.controls.render({
415
- kind: 'push', // 'push' | 'checkbox' | 'radio' | 'popup' | 'slider' | 'switch'
415
+ kind: 'push', // 'push' | 'flexiblePush' | 'checkbox' | 'radio' | 'popup' | 'slider' | 'switch'
416
416
  title: 'Click me',
417
417
  pressed: false, // drive this from your own mouse events
418
418
  state: 1, // on/off for checkbox/radio/switch
@@ -436,6 +436,12 @@ Two render paths inside `drawControl`:
436
436
  at all, so these render a real unparented `NSControl` via
437
437
  `displayRectIgnoringOpacity:inContext:`.
438
438
 
439
+ A `push` bezel is one height: drawn into a taller frame, AppKit draws its own 22pt bezel
440
+ centred in it. `flexiblePush` is `NSBezelStyleFlexiblePush`, the same button stretched to
441
+ its frame — the bezel AppKit gives a title that wraps, and what WebKit and Gecko draw a
442
+ push button taller than a push button with. At the push's height the two are the same
443
+ pixels, except that the default button's accent fill is flat in the flexible bezel.
444
+
439
445
  The demo re-renders a control's image on each state change; a real renderer would cache
440
446
  per `(kind, size, state, appearance)` and nine-slice-stretch bezels with
441
447
  `layer.contentsCenter`. Menus/popovers are deliberately *not* painted — their
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@windowkit/appkit",
3
- "version": "0.15.1",
3
+ "version": "0.16.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/text-features.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/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/accessibility-display.js && node test/ignores-mouse-events.js && node test/surface-blit.js && node test/control-bezels.js && node test/text-features.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/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
package/src/addon.mm CHANGED
@@ -1401,7 +1401,7 @@ static NSView* DummyDrawView() {
1401
1401
 
1402
1402
  // drawControl({kind, title, state, pressed, enabled, isDefault, value,
1403
1403
  // controlSize, appearance, width, height, scale})
1404
- // kind: 'push' | 'checkbox' | 'radio' | 'popup' | 'slider'
1404
+ // kind: 'push' | 'flexiblePush' | 'checkbox' | 'radio' | 'popup' | 'slider'
1405
1405
  // -> { image: External<CGImage>, width, height, scale } (points)
1406
1406
  // width/height default to the cell's natural cellSize (slider must pass them).
1407
1407
  static Napi::Value DrawControl(const Napi::CallbackInfo& info) {
@@ -1418,7 +1418,7 @@ static Napi::Value DrawControl(const Napi::CallbackInfo& info) {
1418
1418
 
1419
1419
  NSCell* cell = nil;
1420
1420
  if ([kind isEqualToString:@"checkbox"] || [kind isEqualToString:@"radio"] ||
1421
- [kind isEqualToString:@"push"]) {
1421
+ [kind isEqualToString:@"push"] || [kind isEqualToString:@"flexiblePush"]) {
1422
1422
  NSButtonCell* c = [[NSButtonCell alloc] initTextCell:title];
1423
1423
  if ([kind isEqualToString:@"checkbox"]) {
1424
1424
  c.buttonType = NSButtonTypeSwitch;
@@ -1426,7 +1426,9 @@ static Napi::Value DrawControl(const Napi::CallbackInfo& info) {
1426
1426
  c.buttonType = NSButtonTypeRadio;
1427
1427
  } else {
1428
1428
  c.buttonType = NSButtonTypeMomentaryPushIn;
1429
- c.bezelStyle = NSBezelStylePush;
1429
+ // one height, or stretched to the frame (src/backend.mm, BuildBezel)
1430
+ c.bezelStyle = [kind isEqualToString:@"flexiblePush"] ? NSBezelStyleFlexiblePush
1431
+ : NSBezelStylePush;
1430
1432
  if (BoolOr(o, "isDefault", false)) c.keyEquivalent = @"\r"; // accent fill
1431
1433
  }
1432
1434
  c.state = state == 1 ? NSControlStateValueOn : NSControlStateValueOff;
package/src/backend.mm CHANGED
@@ -3479,7 +3479,7 @@ static bool ParseBezelSpec(Napi::Env env, Napi::Value v, BezelSpec* s) {
3479
3479
  }
3480
3480
  Napi::Object o = v.As<Napi::Object>();
3481
3481
  s->kind = BStrOr(o, "kind", @"push");
3482
- if (![@[ @"push", @"checkbox", @"radio", @"popup", @"slider", @"switch" ]
3482
+ if (![@[ @"push", @"flexiblePush", @"checkbox", @"radio", @"popup", @"slider", @"switch" ]
3483
3483
  containsObject:s->kind]) {
3484
3484
  Napi::Error::New(env, "unknown control kind").ThrowAsJavaScriptException();
3485
3485
  return false;
@@ -3500,7 +3500,7 @@ static BezelControl BuildBezel(const BezelSpec& s) {
3500
3500
  BezelControl out;
3501
3501
  NSString* kind = s.kind;
3502
3502
  if ([kind isEqualToString:@"checkbox"] || [kind isEqualToString:@"radio"] ||
3503
- [kind isEqualToString:@"push"]) {
3503
+ [kind isEqualToString:@"push"] || [kind isEqualToString:@"flexiblePush"]) {
3504
3504
  NSButtonCell* c = [[NSButtonCell alloc] initTextCell:s.title];
3505
3505
  if ([kind isEqualToString:@"checkbox"]) {
3506
3506
  c.buttonType = NSButtonTypeSwitch;
@@ -3508,7 +3508,12 @@ static BezelControl BuildBezel(const BezelSpec& s) {
3508
3508
  c.buttonType = NSButtonTypeRadio;
3509
3509
  } else {
3510
3510
  c.buttonType = NSButtonTypeMomentaryPushIn;
3511
- c.bezelStyle = NSBezelStylePush;
3511
+ // A push bezel is one height, whatever frame it is drawn into: asked
3512
+ // for a taller one it draws its own, centred. The flexible push is the
3513
+ // same button stretched to the frame — the bezel AppKit gives a title
3514
+ // that wraps, and the one WebKit and Gecko draw a tall button with.
3515
+ c.bezelStyle = [kind isEqualToString:@"flexiblePush"] ? NSBezelStyleFlexiblePush
3516
+ : NSBezelStylePush;
3512
3517
  // the Return key equivalent is what makes AppKit fill it with the
3513
3518
  // user's accent — the "default button" look
3514
3519
  if (s.isDefault) c.keyEquivalent = @"\r";
@@ -5127,6 +5132,41 @@ static double HangingWhitespaceWidth(CTLineRef line, NSString* text,
5127
5132
  return width;
5128
5133
  }
5129
5134
 
5135
+ // Where a line that CoreText broke inside a word should end instead.
5136
+ // CoreText breaks a word wider than the line between two of its clusters,
5137
+ // as ntk does; but where not even the word's first cluster fits it gives
5138
+ // the line that one cluster anyway, so a word in a box narrower than its
5139
+ // first letter came out a letter a line — and a width of 1, which is how a
5140
+ // layout is asked for its narrowest (a table column's minimum, the longest
5141
+ // word), measured the widest letter here and the longest word on X11. ntk
5142
+ // lets such a word overflow whole, and so does CSS: the line runs on to the
5143
+ // next place a line may break. Only a line of one cluster is looked at, so
5144
+ // ordinary wrapping costs a comparison.
5145
+ static long UnfitClusterRunsOn(CTTypesetterRef ts, NSString* text,
5146
+ CFStringTokenizerRef* breaks, long start,
5147
+ long count, long total, double width) {
5148
+ if (count > 16) return count;
5149
+ if ((long)CTTypesetterSuggestClusterBreak(ts, start, 0) != count) {
5150
+ return count;
5151
+ }
5152
+ CTLineRef probe = CTTypesetterCreateLine(ts, CFRangeMake(start, count));
5153
+ double w = CTLineGetTypographicBounds(probe, nullptr, nullptr, nullptr) -
5154
+ HangingWhitespaceWidth(probe, text, start, start + count);
5155
+ CFRelease(probe);
5156
+ if (w <= width) return count;
5157
+ if (!*breaks) {
5158
+ *breaks = CFStringTokenizerCreate(
5159
+ nullptr, (__bridge CFStringRef)text, CFRangeMake(0, total),
5160
+ kCFStringTokenizerUnitLineBreak, nullptr);
5161
+ }
5162
+ if (!*breaks) return count;
5163
+ CFStringTokenizerGoToTokenAtIndex(*breaks, start);
5164
+ CFRange word = CFStringTokenizerGetCurrentTokenRange(*breaks);
5165
+ if (word.location == kCFNotFound) return count;
5166
+ long end = std::min(total, (long)(word.location + word.length));
5167
+ return std::max(count, end - start);
5168
+ }
5169
+
5130
5170
  // createLayout({ spans: [{text, font (handle), color:[r,g,b,a]}],
5131
5171
  // maxWidth?, align: 0 left | 0.5 center | 1 right,
5132
5172
  // lineHeight?, maxLines?, ellipsis?, rtl?,
@@ -5189,10 +5229,15 @@ static Napi::Value CreateLayout(const Napi::CallbackInfo& info) {
5189
5229
  long start = 0;
5190
5230
  long lineIndex = 0;
5191
5231
  double breakWidth = bounded ? maxWidth : 1e9;
5232
+ CFStringTokenizerRef breaks = nullptr;
5192
5233
  while (start < total) {
5193
5234
  long count =
5194
5235
  (long)CTTypesetterSuggestLineBreak(ts, start, breakWidth);
5195
5236
  if (count <= 0) count = 1;
5237
+ if (bounded && start + count < total) {
5238
+ count = UnfitClusterRunsOn(ts, as.string, &breaks, start, count, total,
5239
+ breakWidth);
5240
+ }
5196
5241
  bool lastAllowed = maxLines > 0 && lineIndex == maxLines - 1;
5197
5242
  bool more = start + count < total;
5198
5243
  CTLineRef line = nullptr;
@@ -5293,6 +5338,7 @@ static Napi::Value CreateLayout(const Napi::CallbackInfo& info) {
5293
5338
  start = lineEnd;
5294
5339
  if (maxLines > 0 && lineIndex >= maxLines) break;
5295
5340
  }
5341
+ if (breaks) CFRelease(breaks);
5296
5342
  layout->height = y;
5297
5343
  }
5298
5344