uicore-ts 1.1.101 → 1.1.105

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/scripts/UIView.ts CHANGED
@@ -149,7 +149,8 @@ export class UIView extends UIObject {
149
149
  _UIViewIntrinsicTemporaryWidth?: string
150
150
  _UIViewIntrinsicTemporaryHeight?: string
151
151
  _enabled: boolean = YES
152
- _frame?: UIRectangle & { zIndex?: number }
152
+ _frame?: UIRectangle & { zIndex?: number };
153
+ _frameCache?: UIRectangle
153
154
  _backgroundColor: UIColor = UIColor.transparentColor
154
155
 
155
156
  _viewHTMLElement!: HTMLElement & LooseObject
@@ -292,24 +293,145 @@ export class UIView extends UIObject {
292
293
  }
293
294
 
294
295
 
295
- static get pageHeight() {
296
+ // static get pageHeight() {
297
+ // const body = document.body
298
+ // const html = document.documentElement
299
+ // return Math.max(
300
+ // body.scrollHeight,
301
+ // body.offsetHeight,
302
+ // html.clientHeight,
303
+ // html.scrollHeight,
304
+ // html.offsetHeight
305
+ // )
306
+ // }
307
+ //
308
+ // static get pageWidth() {
309
+ // const body = document.body
310
+ // const html = document.documentElement
311
+ // return Math.max(body.scrollWidth, body.offsetWidth, html.clientWidth, html.scrollWidth, html.offsetWidth)
312
+ // }
313
+
314
+ //#region Static Properties - Page Dimensions Cache
315
+
316
+ private static _cachedPageWidth: number | undefined
317
+ private static _cachedPageHeight: number | undefined
318
+ private static _pageDimensionsCacheValid = false
319
+ private static _resizeObserverInitialized = false
320
+
321
+ //#endregion
322
+
323
+ //#region Static Methods - Page Dimensions
324
+
325
+ /**
326
+ * Initialize resize observer to invalidate cache when page dimensions change.
327
+ * This is called lazily on first access.
328
+ */
329
+ private static _initializePageDimensionsCacheIfNeeded() {
330
+ if (this._resizeObserverInitialized) {
331
+ return
332
+ }
333
+
334
+ this._resizeObserverInitialized = true
335
+
336
+ // Invalidate cache on window resize
337
+ window.addEventListener('resize', () => {
338
+ this._pageDimensionsCacheValid = false
339
+ }, { passive: true })
340
+
341
+ // Observe document.body for mutations that might affect dimensions
342
+ const bodyObserver = new ResizeObserver(() => {
343
+ this._pageDimensionsCacheValid = false
344
+ })
345
+
346
+ // Start observing once body is available
347
+ if (document.body) {
348
+ bodyObserver.observe(document.body)
349
+ } else {
350
+ // Wait for DOMContentLoaded if body isn't ready yet
351
+ document.addEventListener('DOMContentLoaded', () => {
352
+ bodyObserver.observe(document.body)
353
+ }, { once: true })
354
+ }
355
+
356
+ // Also invalidate on DOM mutations that might add/remove content
357
+ const mutationObserver = new MutationObserver(() => {
358
+ this._pageDimensionsCacheValid = false
359
+ })
360
+
361
+ const observeMutations = () => {
362
+ mutationObserver.observe(document.body, {
363
+ childList: true,
364
+ subtree: true,
365
+ attributes: true,
366
+ attributeFilter: ['style', 'class']
367
+ })
368
+ }
369
+
370
+ if (document.body) {
371
+ observeMutations()
372
+ } else {
373
+ document.addEventListener('DOMContentLoaded', observeMutations, { once: true })
374
+ }
375
+ }
376
+
377
+ /**
378
+ * Compute and cache page dimensions.
379
+ * Only triggers reflow when cache is invalid.
380
+ */
381
+ private static _updatePageDimensionsCacheIfNeeded() {
382
+ if (this._pageDimensionsCacheValid &&
383
+ this._cachedPageWidth !== undefined &&
384
+ this._cachedPageHeight !== undefined) {
385
+ return
386
+ }
387
+
296
388
  const body = document.body
297
389
  const html = document.documentElement
298
- return Math.max(
390
+
391
+ // Compute both at once to minimize reflows
392
+ this._cachedPageWidth = Math.max(
393
+ body.scrollWidth,
394
+ body.offsetWidth,
395
+ html.clientWidth,
396
+ html.scrollWidth,
397
+ html.offsetWidth
398
+ )
399
+
400
+ this._cachedPageHeight = Math.max(
299
401
  body.scrollHeight,
300
402
  body.offsetHeight,
301
403
  html.clientHeight,
302
404
  html.scrollHeight,
303
405
  html.offsetHeight
304
406
  )
407
+
408
+ this._pageDimensionsCacheValid = true
305
409
  }
306
410
 
307
411
  static get pageWidth() {
308
- const body = document.body
309
- const html = document.documentElement
310
- return Math.max(body.scrollWidth, body.offsetWidth, html.clientWidth, html.scrollWidth, html.offsetWidth)
412
+ this._initializePageDimensionsCacheIfNeeded()
413
+ this._updatePageDimensionsCacheIfNeeded()
414
+ return this._cachedPageWidth!
311
415
  }
312
416
 
417
+ static get pageHeight() {
418
+ this._initializePageDimensionsCacheIfNeeded()
419
+ this._updatePageDimensionsCacheIfNeeded()
420
+ return this._cachedPageHeight!
421
+ }
422
+
423
+ /**
424
+ * Manually invalidate the page dimensions cache.
425
+ * Useful if you know dimensions changed and want to force a recalculation.
426
+ */
427
+ static invalidatePageDimensionsCache() {
428
+ this._pageDimensionsCacheValid = false
429
+ }
430
+
431
+ //#endregion
432
+
433
+
434
+
313
435
  centerInContainer() {
314
436
  this.style.left = "50%"
315
437
  this.style.top = "50%"
@@ -992,12 +1114,13 @@ export class UIView extends UIObject {
992
1114
 
993
1115
  let result: UIRectangle
994
1116
  if (IS_NOT(_frame)) {
995
- result = new UIRectangle(
1117
+ result = this._frameCache ?? new UIRectangle(
996
1118
  0,
997
1119
  0,
998
1120
  this._resizeObserverEntry?.contentRect.height ?? this.viewHTMLElement.offsetHeight,
999
1121
  this._resizeObserverEntry?.contentRect.width ?? this.viewHTMLElement.offsetWidth
1000
1122
  )
1123
+ this._frameCache = result
1001
1124
  }
1002
1125
  else {
1003
1126
  let frame: (UIRectangle & { zIndex?: number })
@@ -1035,6 +1158,7 @@ export class UIView extends UIObject {
1035
1158
  didResize(entry: ResizeObserverEntry) {
1036
1159
 
1037
1160
  this._resizeObserverEntry = entry
1161
+ this._frameCache = undefined
1038
1162
  this.setNeedsLayout()
1039
1163
 
1040
1164
  this.boundsDidChange(new UIRectangle(0, 0, entry.contentRect.height, entry.contentRect.width))