uicore-ts 1.1.88 → 1.1.102

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.
@@ -93,6 +93,7 @@ const _UIView = class extends import_UIObject.UIObject {
93
93
  this._isCBEditorTemporaryMovable = import_UIObject.NO;
94
94
  this._intrinsicSizesCache = {};
95
95
  this.usesVirtualLayoutingForIntrinsicSizing = import_UIObject.YES;
96
+ this._contentInsets = { top: 0, left: 0, bottom: 0, right: 0 };
96
97
  this.controlEvent = _UIView.controlEvent;
97
98
  _UIView._UIViewIndex = _UIView.nextIndex;
98
99
  this._UIViewIndex = _UIView._UIViewIndex;
@@ -104,24 +105,93 @@ const _UIView = class extends import_UIObject.UIObject {
104
105
  this._loadUIEvents();
105
106
  this.setNeedsLayout();
106
107
  }
108
+ static get isVirtualLayouting() {
109
+ return this._virtualLayoutingDepth > 0;
110
+ }
111
+ get isVirtualLayouting() {
112
+ return _UIView._virtualLayoutingDepth > 0;
113
+ }
114
+ startVirtualLayout() {
115
+ _UIView._virtualLayoutingDepth = _UIView._virtualLayoutingDepth + 1;
116
+ }
117
+ finishVirtualLayout() {
118
+ if (_UIView._virtualLayoutingDepth === 0) {
119
+ throw new Error("Unbalanced finishVirtualLayout()");
120
+ }
121
+ _UIView._virtualLayoutingDepth = _UIView._virtualLayoutingDepth - 1;
122
+ }
107
123
  static get nextIndex() {
108
124
  return _UIView._UIViewIndex + 1;
109
125
  }
110
- static get pageHeight() {
126
+ static _initializePageDimensionsCacheIfNeeded() {
127
+ if (this._resizeObserverInitialized) {
128
+ return;
129
+ }
130
+ this._resizeObserverInitialized = true;
131
+ window.addEventListener("resize", () => {
132
+ this._pageDimensionsCacheValid = false;
133
+ }, { passive: true });
134
+ const bodyObserver = new ResizeObserver(() => {
135
+ this._pageDimensionsCacheValid = false;
136
+ });
137
+ if (document.body) {
138
+ bodyObserver.observe(document.body);
139
+ } else {
140
+ document.addEventListener("DOMContentLoaded", () => {
141
+ bodyObserver.observe(document.body);
142
+ }, { once: true });
143
+ }
144
+ const mutationObserver = new MutationObserver(() => {
145
+ this._pageDimensionsCacheValid = false;
146
+ });
147
+ const observeMutations = () => {
148
+ mutationObserver.observe(document.body, {
149
+ childList: true,
150
+ subtree: true,
151
+ attributes: true,
152
+ attributeFilter: ["style", "class"]
153
+ });
154
+ };
155
+ if (document.body) {
156
+ observeMutations();
157
+ } else {
158
+ document.addEventListener("DOMContentLoaded", observeMutations, { once: true });
159
+ }
160
+ }
161
+ static _updatePageDimensionsCacheIfNeeded() {
162
+ if (this._pageDimensionsCacheValid && this._cachedPageWidth !== void 0 && this._cachedPageHeight !== void 0) {
163
+ return;
164
+ }
111
165
  const body = document.body;
112
166
  const html = document.documentElement;
113
- return Math.max(
167
+ this._cachedPageWidth = Math.max(
168
+ body.scrollWidth,
169
+ body.offsetWidth,
170
+ html.clientWidth,
171
+ html.scrollWidth,
172
+ html.offsetWidth
173
+ );
174
+ this._cachedPageHeight = Math.max(
114
175
  body.scrollHeight,
115
176
  body.offsetHeight,
116
177
  html.clientHeight,
117
178
  html.scrollHeight,
118
179
  html.offsetHeight
119
180
  );
181
+ this._pageDimensionsCacheValid = true;
120
182
  }
121
183
  static get pageWidth() {
122
- const body = document.body;
123
- const html = document.documentElement;
124
- return Math.max(body.scrollWidth, body.offsetWidth, html.clientWidth, html.scrollWidth, html.offsetWidth);
184
+ this._initializePageDimensionsCacheIfNeeded();
185
+ this._updatePageDimensionsCacheIfNeeded();
186
+ return this._cachedPageWidth;
187
+ }
188
+ static get pageHeight() {
189
+ this._initializePageDimensionsCacheIfNeeded();
190
+ this._updatePageDimensionsCacheIfNeeded();
191
+ return this._cachedPageHeight;
192
+ }
193
+ static invalidatePageDimensionsCache() {
194
+ this._pageDimensionsCacheValid = false;
125
195
  }
126
196
  centerInContainer() {
127
197
  this.style.left = "50%";
@@ -470,7 +540,7 @@ const _UIView = class extends import_UIObject.UIObject {
470
540
  get frame() {
471
541
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
472
542
  let result;
473
- if (!_UIView.isVirtualLayouting) {
543
+ if (!this.isVirtualLayouting) {
474
544
  result = (_a = this._frame) == null ? void 0 : _a.copy();
475
545
  } else {
476
546
  result = (_b = this._frameForVirtualLayouting) == null ? void 0 : _b.copy();
@@ -496,7 +566,7 @@ const _UIView = class extends import_UIObject.UIObject {
496
566
  if (zIndex != void 0) {
497
567
  rectangle.zIndex = zIndex;
498
568
  }
499
- if (!_UIView.isVirtualLayouting) {
569
+ if (!this.isVirtualLayouting) {
500
570
  this._frame = rectangle;
501
571
  } else {
502
572
  this._frameForVirtualLayouting = rectangle;
@@ -509,7 +579,7 @@ const _UIView = class extends import_UIObject.UIObject {
509
579
  if (this.isInternalScaling) {
510
580
  rectangle.scale(1 / this.scale);
511
581
  }
512
- if (!_UIView.isVirtualLayouting) {
582
+ if (!this.isVirtualLayouting) {
513
583
  _UIView._setAbsoluteSizeAndPosition(
514
584
  this.viewHTMLElement,
515
585
  rectangle.topLeft.x,
@@ -526,32 +596,35 @@ const _UIView = class extends import_UIObject.UIObject {
526
596
  }
527
597
  }
528
598
  get bounds() {
529
- var _a, _b, _c, _d, _e;
599
+ var _a, _b, _c, _d, _e, _f;
530
600
  let _frame;
531
- if (!_UIView.isVirtualLayouting) {
601
+ if (!this.isVirtualLayouting) {
532
602
  _frame = this._frame;
533
603
  } else {
534
604
  _frame = this._frameForVirtualLayouting;
535
605
  }
536
606
  let result;
537
607
  if ((0, import_UIObject.IS_NOT)(_frame)) {
538
- result = new import_UIRectangle.UIRectangle(
608
+ result = (_e = this._frameCache) != null ? _e : new import_UIRectangle.UIRectangle(
539
609
  0,
540
610
  0,
541
611
  (_b = (_a = this._resizeObserverEntry) == null ? void 0 : _a.contentRect.height) != null ? _b : this.viewHTMLElement.offsetHeight,
542
612
  (_d = (_c = this._resizeObserverEntry) == null ? void 0 : _c.contentRect.width) != null ? _d : this.viewHTMLElement.offsetWidth
543
613
  );
614
+ this._frameCache = result;
544
615
  } else {
545
616
  let frame;
546
- if (!_UIView.isVirtualLayouting) {
617
+ if (!this.isVirtualLayouting) {
547
618
  frame = this.frame;
548
619
  } else {
549
- frame = (_e = this._frameForVirtualLayouting) != null ? _e : this.frame;
620
+ frame = (_f = this._frameForVirtualLayouting) != null ? _f : this.frame;
550
621
  }
551
622
  result = frame.copy();
552
623
  result.x = 0;
553
624
  result.y = 0;
554
625
  }
626
+ result.minHeight = 0;
627
+ result.minWidth = 0;
555
628
  return result;
556
629
  }
557
630
  set bounds(rectangle) {
@@ -564,6 +637,7 @@ const _UIView = class extends import_UIObject.UIObject {
564
637
  }
565
638
  didResize(entry) {
566
639
  this._resizeObserverEntry = entry;
640
+ this._frameCache = void 0;
567
641
  this.setNeedsLayout();
568
642
  this.boundsDidChange(new import_UIRectangle.UIRectangle(0, 0, entry.contentRect.height, entry.contentRect.width));
569
643
  }
@@ -891,19 +965,42 @@ const _UIView = class extends import_UIObject.UIObject {
891
965
  _UIView.runFunctionBeforeNextFrame(_UIView.layoutViewsIfNeeded);
892
966
  }
893
967
  static layoutViewsIfNeeded() {
894
- for (var i = 0; i < _UIView._viewsToLayout.length; i++) {
895
- const view = _UIView._viewsToLayout[i];
896
- view.layoutIfNeeded();
968
+ const maxIterations = 10;
969
+ let iteration = 0;
970
+ const layoutCounts = /* @__PURE__ */ new Map();
971
+ while (_UIView._viewsToLayout.length > 0 && iteration < maxIterations) {
972
+ const viewsToProcess = _UIView._viewsToLayout;
973
+ _UIView._viewsToLayout = [];
974
+ const viewDepthMap = /* @__PURE__ */ new Map();
975
+ for (let i = 0; i < viewsToProcess.length; i++) {
976
+ const view = viewsToProcess[i];
977
+ const layoutCount = layoutCounts.get(view) || 0;
978
+ if (layoutCount >= 5) {
979
+ console.warn("View layout cycle detected:", view);
980
+ continue;
981
+ }
982
+ if (!viewDepthMap.has(view)) {
983
+ viewDepthMap.set(view, view.withAllSuperviews.length);
984
+ }
985
+ }
986
+ const sortedViews = Array.from(viewDepthMap.keys()).sort((a, b) => {
987
+ return viewDepthMap.get(a) - viewDepthMap.get(b);
988
+ });
989
+ for (let i = 0; i < sortedViews.length; i++) {
990
+ const view = sortedViews[i];
991
+ view.layoutIfNeeded();
992
+ layoutCounts.set(view, (layoutCounts.get(view) || 0) + 1);
993
+ }
994
+ iteration++;
897
995
  }
898
- _UIView._viewsToLayout = [];
899
996
  }
900
997
  setNeedsLayout() {
901
- if (this._shouldLayout) {
998
+ if (this._shouldLayout && _UIView._viewsToLayout.contains(this)) {
902
999
  return;
903
1000
  }
904
1001
  this._shouldLayout = import_UIObject.YES;
905
1002
  _UIView._viewsToLayout.push(this);
906
- this._intrinsicSizesCache = {};
1003
+ this.clearIntrinsicSizeCache();
907
1004
  if (_UIView._viewsToLayout.length == 1) {
908
1005
  _UIView.scheduleLayoutViewsIfNeeded();
909
1006
  }
@@ -915,7 +1012,9 @@ const _UIView = class extends import_UIObject.UIObject {
915
1012
  if (!this._shouldLayout) {
916
1013
  return;
917
1014
  }
918
- this._shouldLayout = import_UIObject.NO;
1015
+ if (!this.isVirtualLayouting) {
1016
+ this._shouldLayout = import_UIObject.NO;
1017
+ }
919
1018
  try {
920
1019
  this.layoutSubviews();
921
1020
  } catch (exception) {
@@ -925,7 +1024,9 @@ const _UIView = class extends import_UIObject.UIObject {
925
1024
  layoutSubviews() {
926
1025
  var _a, _b;
927
1026
  this.willLayoutSubviews();
928
- this._shouldLayout = import_UIObject.NO;
1027
+ if (!this.isVirtualLayouting) {
1028
+ this._shouldLayout = import_UIObject.NO;
1029
+ }
929
1030
  if (this.constraints.length) {
930
1031
  this._updateLayoutFunction = _UIView.performAutoLayout(this.viewHTMLElement, null, this.constraints);
931
1032
  }
@@ -2140,9 +2241,55 @@ const _UIView = class extends import_UIObject.UIObject {
2140
2241
  rectangleFromView(rectangle, view) {
2141
2242
  return view.rectangleInView(rectangle, this);
2142
2243
  }
2244
+ get contentBounds() {
2245
+ const bounds = this.bounds;
2246
+ const insets = this._contentInsets;
2247
+ return new import_UIRectangle.UIRectangle(
2248
+ insets.left,
2249
+ insets.top,
2250
+ bounds.height - insets.top - insets.bottom,
2251
+ bounds.width - insets.left - insets.right
2252
+ );
2253
+ }
2254
+ contentBoundsWithInset(inset) {
2255
+ this._contentInsets = { top: inset, left: inset, bottom: inset, right: inset };
2256
+ const bounds = this.bounds;
2257
+ return new import_UIRectangle.UIRectangle(
2258
+ inset,
2259
+ inset,
2260
+ bounds.height - inset * 2,
2261
+ bounds.width - inset * 2
2262
+ );
2263
+ }
2264
+ contentBoundsWithInsets(left, right, bottom, top) {
2265
+ this._contentInsets = { left, right, bottom, top };
2266
+ const bounds = this.bounds;
2267
+ return new import_UIRectangle.UIRectangle(
2268
+ left,
2269
+ top,
2270
+ bounds.height - top - bottom,
2271
+ bounds.width - left - right
2272
+ );
2273
+ }
2274
+ _getIntrinsicSizeCacheKey(constrainingHeight, constrainingWidth) {
2275
+ return `h_${constrainingHeight}__w_${constrainingWidth}`;
2276
+ }
2277
+ _getCachedIntrinsicSize(cacheKey) {
2278
+ return this._intrinsicSizesCache[cacheKey];
2279
+ }
2280
+ _setCachedIntrinsicSize(cacheKey, size) {
2281
+ this._intrinsicSizesCache[cacheKey] = size.copy();
2282
+ }
2283
+ clearIntrinsicSizeCache() {
2284
+ var _a;
2285
+ this._intrinsicSizesCache = {};
2286
+ if ((_a = this.superview) == null ? void 0 : _a.usesVirtualLayoutingForIntrinsicSizing) {
2287
+ this.superview.clearIntrinsicSizeCache();
2288
+ }
2289
+ }
2143
2290
  intrinsicContentSizeWithConstraints(constrainingHeight = 0, constrainingWidth = 0) {
2144
- const cacheKey = "h_" + constrainingHeight + "__w_" + constrainingWidth;
2145
- const cachedResult = this._intrinsicSizesCache[cacheKey];
2291
+ const cacheKey = this._getIntrinsicSizeCacheKey(constrainingHeight, constrainingWidth);
2292
+ const cachedResult = this._getCachedIntrinsicSize(cacheKey);
2146
2293
  if (cachedResult) {
2147
2294
  return cachedResult;
2148
2295
  }
@@ -2192,85 +2339,89 @@ const _UIView = class extends import_UIObject.UIObject {
2192
2339
  }
2193
2340
  result.height = resultHeight;
2194
2341
  result.width = resultWidth;
2195
- this._intrinsicSizesCache[cacheKey] = result.copy();
2342
+ this._setCachedIntrinsicSize(cacheKey, result);
2196
2343
  return result;
2197
2344
  }
2345
+ _intrinsicFrameFromSubviewFrames() {
2346
+ var _a, _b, _c, _d;
2347
+ const framePoints = [];
2348
+ this.subviews.forEach((subview) => {
2349
+ subview.layoutIfNeeded();
2350
+ framePoints.push(subview.frame.min);
2351
+ framePoints.push(subview.frame.max);
2352
+ });
2353
+ const resultFrame = import_UIRectangle.UIRectangle.boundingBoxForPoints(framePoints).rectangleWithInsets(
2354
+ -((_a = this._contentInsets) == null ? void 0 : _a.left),
2355
+ -((_b = this._contentInsets) == null ? void 0 : _b.right),
2356
+ -((_c = this._contentInsets) == null ? void 0 : _c.bottom),
2357
+ -((_d = this._contentInsets) == null ? void 0 : _d.top)
2358
+ );
2359
+ return resultFrame;
2360
+ }
2198
2361
  intrinsicContentWidth(constrainingHeight = 0) {
2362
+ const cacheKey = this._getIntrinsicSizeCacheKey(constrainingHeight, 0);
2363
+ const cached = this._getCachedIntrinsicSize(cacheKey);
2364
+ if (cached) {
2365
+ return cached.width;
2366
+ }
2199
2367
  if (this.usesVirtualLayoutingForIntrinsicSizing) {
2200
- _UIView.isVirtualLayouting = import_UIObject.YES;
2201
- this.frame = this.frame.rectangleWithHeight(constrainingHeight);
2202
- this.layoutIfNeeded();
2203
- const framePoints = [];
2204
- this.forEachViewInSubtree((view) => {
2205
- if (view == this) {
2206
- return;
2207
- }
2208
- let frame = view.frame;
2209
- if (view.superview != this) {
2210
- frame = view.rectangleInView(
2211
- frame,
2212
- this,
2213
- import_UIObject.YES
2214
- );
2215
- }
2216
- framePoints.push(frame.min);
2217
- framePoints.push(frame.max);
2218
- });
2219
- const resultFrame = import_UIRectangle.UIRectangle.boundingBoxForPoints(framePoints);
2220
- _UIView.isVirtualLayouting = import_UIObject.NO;
2368
+ this.startVirtualLayout();
2369
+ let resultFrame;
2370
+ try {
2371
+ this.frame = this.frame.rectangleWithHeight(constrainingHeight);
2372
+ this.layoutIfNeeded();
2373
+ resultFrame = this._intrinsicFrameFromSubviewFrames();
2374
+ } finally {
2375
+ this.finishVirtualLayout();
2376
+ }
2377
+ this._setCachedIntrinsicSize(cacheKey, new import_UIRectangle.UIRectangle(0, 0, resultFrame.height, resultFrame.width));
2221
2378
  return resultFrame.width;
2222
2379
  }
2223
- return this.intrinsicContentSizeWithConstraints(constrainingHeight).width;
2380
+ const size = this.intrinsicContentSizeWithConstraints(constrainingHeight, 0);
2381
+ return size.width;
2224
2382
  }
2225
2383
  intrinsicContentHeight(constrainingWidth = 0) {
2384
+ const cacheKey = this._getIntrinsicSizeCacheKey(0, constrainingWidth);
2385
+ const cached = this._getCachedIntrinsicSize(cacheKey);
2386
+ if (cached) {
2387
+ return cached.height;
2388
+ }
2226
2389
  if (this.usesVirtualLayoutingForIntrinsicSizing) {
2227
- _UIView.isVirtualLayouting = import_UIObject.YES;
2228
- this.frame = this.frame.rectangleWithWidth(constrainingWidth);
2229
- this.layoutIfNeeded();
2230
- const framePoints = [];
2231
- this.forEachViewInSubtree((view) => {
2232
- if (view == this) {
2233
- return;
2234
- }
2235
- let frame = view.frame;
2236
- if (view.superview != this) {
2237
- frame = view.rectangleInView(
2238
- frame,
2239
- this,
2240
- import_UIObject.YES
2241
- );
2242
- }
2243
- framePoints.push(frame.min);
2244
- framePoints.push(frame.max);
2245
- });
2246
- const resultFrame = import_UIRectangle.UIRectangle.boundingBoxForPoints(framePoints);
2247
- _UIView.isVirtualLayouting = import_UIObject.NO;
2390
+ this.startVirtualLayout();
2391
+ let resultFrame;
2392
+ try {
2393
+ this.frame = this.frame.rectangleWithWidth(constrainingWidth);
2394
+ this.layoutIfNeeded();
2395
+ resultFrame = this._intrinsicFrameFromSubviewFrames();
2396
+ } finally {
2397
+ this.finishVirtualLayout();
2398
+ }
2399
+ this._setCachedIntrinsicSize(cacheKey, new import_UIRectangle.UIRectangle(0, 0, resultFrame.height, resultFrame.width));
2248
2400
  return resultFrame.height;
2249
2401
  }
2250
- return this.intrinsicContentSizeWithConstraints(void 0, constrainingWidth).height;
2402
+ const size = this.intrinsicContentSizeWithConstraints(0, constrainingWidth);
2403
+ if (isNaN(size.height)) {
2404
+ console.error("NaN in intrinsicContentHeight", this);
2405
+ var asd = 1;
2406
+ }
2407
+ return size.height;
2251
2408
  }
2252
2409
  intrinsicContentSize() {
2410
+ const cacheKey = this._getIntrinsicSizeCacheKey(0, 0);
2411
+ const cached = this._getCachedIntrinsicSize(cacheKey);
2412
+ if (cached) {
2413
+ return cached;
2414
+ }
2253
2415
  if (this.usesVirtualLayoutingForIntrinsicSizing) {
2254
- _UIView.isVirtualLayouting = import_UIObject.YES;
2255
- this.layoutIfNeeded();
2256
- const framePoints = [];
2257
- this.forEachViewInSubtree((view) => {
2258
- if (view == this) {
2259
- return;
2260
- }
2261
- let frame = view.frame;
2262
- if (view.superview != this) {
2263
- frame = view.rectangleInView(
2264
- frame,
2265
- this,
2266
- import_UIObject.YES
2267
- );
2268
- }
2269
- framePoints.push(frame.min);
2270
- framePoints.push(frame.max);
2271
- });
2272
- const resultFrame = import_UIRectangle.UIRectangle.boundingBoxForPoints(framePoints);
2273
- _UIView.isVirtualLayouting = import_UIObject.NO;
2416
+ this.startVirtualLayout();
2417
+ let resultFrame;
2418
+ try {
2419
+ this.layoutIfNeeded();
2420
+ resultFrame = this._intrinsicFrameFromSubviewFrames();
2421
+ } finally {
2422
+ this.finishVirtualLayout();
2423
+ }
2424
+ this._setCachedIntrinsicSize(cacheKey, resultFrame);
2274
2425
  return resultFrame;
2275
2426
  }
2276
2427
  return import_UIObject.nil;
@@ -2291,7 +2442,9 @@ UIView.resizeObserver = new ResizeObserver((entries, observer) => {
2291
2442
  UIView._onWindowTouchMove = import_UIObject.nil;
2292
2443
  UIView._onWindowMouseMove = import_UIObject.nil;
2293
2444
  UIView._onWindowMouseup = import_UIObject.nil;
2294
- UIView.isVirtualLayouting = import_UIObject.NO;
2445
+ UIView._virtualLayoutingDepth = 0;
2446
+ UIView._pageDimensionsCacheValid = false;
2447
+ UIView._resizeObserverInitialized = false;
2295
2448
  UIView._transformAttribute = ("transform" in document.documentElement.style ? "transform" : void 0) || ("-webkit-transform" in document.documentElement.style ? "-webkit-transform" : "undefined") || ("-moz-transform" in document.documentElement.style ? "-moz-transform" : "undefined") || ("-ms-transform" in document.documentElement.style ? "-ms-transform" : "undefined") || ("-o-transform" in document.documentElement.style ? "-o-transform" : "undefined");
2296
2449
  UIView.constraintAttribute = {
2297
2450
  "left": AutoLayout.Attribute.LEFT,