UNPKG

uicore-ts

Version:

UICore is a library to build native-like user interfaces using pure Typescript. No HTML is needed at all. Components are described as TS classes and all user interactions are handled explicitly. This library is strongly inspired by the UIKit framework tha

902 lines (901 loc) 31.3 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var UIRectangle_exports = {}; __export(UIRectangle_exports, { UIRectangle: () => UIRectangle }); module.exports = __toCommonJS(UIRectangle_exports); var import_UIObject = require("./UIObject"); var import_UIPoint = require("./UIPoint"); var import_UIView = require("./UIView"); class UIRectangle extends import_UIObject.UIObject { constructor(x = 0, y = 0, height = 0, width = 0) { super(); this._isLazyCopy = import_UIObject.NO; this._isBeingUpdated = import_UIObject.NO; this._data = { min: new import_UIPoint.UIPoint(x, y), max: new import_UIPoint.UIPoint(x + width, y + height), refCount: 1 }; this._setupPointCallbacks(); if ((0, import_UIObject.IS_NIL)(height)) { this._data.max.y = height; } if ((0, import_UIObject.IS_NIL)(width)) { this._data.max.x = width; } } _setupPointCallbacks() { this._data.min.didChange = (point) => { var _a; (_a = this.rectanglePointDidChange) == null ? void 0 : _a.call(this, point); this._rectanglePointDidChange(); }; this._data.max.didChange = (point) => { var _a; (_a = this.rectanglePointDidChange) == null ? void 0 : _a.call(this, point); this._rectanglePointDidChange(); }; } materialize() { if (this._isLazyCopy || this._data.refCount > 1) { this._data.refCount--; const oldData = this._data; this._data = { min: oldData.min.copy(), max: oldData.max.copy(), minHeight: oldData.minHeight, maxHeight: oldData.maxHeight, minWidth: oldData.minWidth, maxWidth: oldData.maxWidth, refCount: 1 }; this._setupPointCallbacks(); this._isLazyCopy = import_UIObject.NO; } } lazyCopy() { const result = Object.create(UIRectangle.prototype); result._data = this._data; result._data.refCount++; result._isLazyCopy = import_UIObject.YES; result._isBeingUpdated = import_UIObject.NO; result.rectanglePointDidChange = this.rectanglePointDidChange; return result; } get min() { return this._data.min; } set min(value) { this.materialize(); this._data.min = value; this._setupPointCallbacks(); } get max() { return this._data.max; } set max(value) { this.materialize(); this._data.max = value; this._setupPointCallbacks(); } get minHeight() { return this._data.minHeight; } set minHeight(value) { this.materialize(); this._data.minHeight = value; } get maxHeight() { return this._data.maxHeight; } set maxHeight(value) { this.materialize(); this._data.maxHeight = value; } get minWidth() { return this._data.minWidth; } set minWidth(value) { this.materialize(); this._data.minWidth = value; } get maxWidth() { return this._data.maxWidth; } set maxWidth(value) { this.materialize(); this._data.maxWidth = value; } copy() { const result = new UIRectangle(this.x, this.y, this.height, this.width); result.minHeight = this.minHeight; result.minWidth = this.minWidth; result.maxHeight = this.maxHeight; result.maxWidth = this.maxWidth; return result; } isEqualTo(rectangle) { return (0, import_UIObject.IS)(rectangle) && this.min.isEqualTo(rectangle.min) && this.max.isEqualTo(rectangle.max); } static zero() { return new UIRectangle(0, 0, 0, 0); } containsPoint(point) { return this.min.x <= point.x && this.min.y <= point.y && point.x <= this.max.x && point.y <= this.max.y; } updateByAddingPoint(point) { this.materialize(); if (!point) { point = new import_UIPoint.UIPoint(0, 0); } this.beginUpdates(); const min = this.min.copy(); if (min.x === import_UIObject.nil) { min.x = this.max.x; } if (min.y === import_UIObject.nil) { min.y = this.max.y; } const max = this.max.copy(); if (max.x === import_UIObject.nil) { max.x = this.min.x; } if (max.y === import_UIObject.nil) { max.y = this.min.y; } this.min.x = Math.min(min.x, point.x); this.min.y = Math.min(min.y, point.y); this.max.x = Math.max(max.x, point.x); this.max.y = Math.max(max.y, point.y); this.finishUpdates(); } scale(scale) { this.materialize(); if ((0, import_UIObject.IS_NOT_NIL)(this.max.y)) { this.height = this.height * scale; } if ((0, import_UIObject.IS_NOT_NIL)(this.max.x)) { this.width = this.width * scale; } } get height() { if (this.max.y === import_UIObject.nil) { return import_UIObject.nil; } return this.max.y - this.min.y; } set height(height) { this.materialize(); this._data.max.y = this.min.y + height; } get width() { if (this.max.x === import_UIObject.nil) { return import_UIObject.nil; } return this.max.x - this.min.x; } set width(width) { this.materialize(); this._data.max.x = this.min.x + width; } get x() { return this.min.x; } set x(x) { this.materialize(); this.beginUpdates(); const width = this.width; this._data.min.x = x; this._data.max.x = this.min.x + width; this.finishUpdates(); } get y() { return this.min.y; } set y(y) { this.materialize(); this.beginUpdates(); const height = this.height; this._data.min.y = y; this._data.max.y = this.min.y + height; this.finishUpdates(); } get topLeft() { return this.min.copy(); } get topRight() { return new import_UIPoint.UIPoint(this.max.x, this.y); } get bottomLeft() { return new import_UIPoint.UIPoint(this.x, this.max.y); } get bottomRight() { return this.max.copy(); } get center() { return this.min.copy().add(this.min.to(this.max).scale(0.5)); } set center(center) { this.materialize(); const offset = this.center.to(center); this.offsetByPoint(offset); } offsetByPoint(offset) { this.materialize(); this.min.add(offset); this.max.add(offset); return this; } concatenateWithRectangle(rectangle) { this.updateByAddingPoint(rectangle.bottomRight); this.updateByAddingPoint(rectangle.topLeft); return this; } rectangleByConcatenatingWithRectangle(rectangle) { return this.lazyCopy().concatenateWithRectangle(rectangle); } intersectionRectangleWithRectangle(rectangle) { const result = this.lazyCopy(); result.materialize(); result.beginUpdates(); const min = result.min; if (min.x === import_UIObject.nil) { min.x = rectangle.max.x - Math.min(result.width, rectangle.width); } if (min.y === import_UIObject.nil) { min.y = rectangle.max.y - Math.min(result.height, rectangle.height); } const max = result.max; if (max.x === import_UIObject.nil) { max.x = rectangle.min.x + Math.min(result.width, rectangle.width); } if (max.y === import_UIObject.nil) { max.y = rectangle.min.y + Math.min(result.height, rectangle.height); } result.min.x = Math.max(result.min.x, rectangle.min.x); result.min.y = Math.max(result.min.y, rectangle.min.y); result.max.x = Math.min(result.max.x, rectangle.max.x); result.max.y = Math.min(result.max.y, rectangle.max.y); if (result.height < 0) { const averageY = (this.center.y + rectangle.center.y) * 0.5; result.min.y = averageY; result.max.y = averageY; } if (result.width < 0) { const averageX = (this.center.x + rectangle.center.x) * 0.5; result.min.x = averageX; result.max.x = averageX; } result.finishUpdates(); return result; } get area() { return this.height * this.width; } intersectsWithRectangle(rectangle) { return this.intersectionRectangleWithRectangle(rectangle).area != 0; } rectangleWithInsets(left, right, bottom, top) { const result = this.lazyCopy(); result.materialize(); result.min.x = this.min.x + left; result.max.x = this.max.x - right; result.min.y = this.min.y + top; result.max.y = this.max.y - bottom; return result; } rectangleWithInset(inset) { return this.rectangleWithInsets(inset, inset, inset, inset); } rectangleWithHeight(height, centeredOnPosition = import_UIObject.nil) { height = this._heightNumberFromSizeNumberOrFunctionOrView(height); if (isNaN(centeredOnPosition)) { centeredOnPosition = import_UIObject.nil; } const result = this.lazyCopy(); result.height = height; if (centeredOnPosition != import_UIObject.nil) { const change = height - this.height; result.offsetByPoint(new import_UIPoint.UIPoint(0, change * centeredOnPosition).scale(-1)); } return result; } rectangleWithWidth(width, centeredOnPosition = import_UIObject.nil) { width = this._widthNumberFromSizeNumberOrFunctionOrView(width); if (isNaN(centeredOnPosition)) { centeredOnPosition = import_UIObject.nil; } const result = this.lazyCopy(); result.width = width; if (centeredOnPosition != import_UIObject.nil) { const change = width - this.width; result.offsetByPoint(new import_UIPoint.UIPoint(change * centeredOnPosition, 0).scale(-1)); } return result; } rectangleWithHeightRelativeToWidth(heightRatio = 1, centeredOnPosition = import_UIObject.nil) { return this.rectangleWithHeight(this.width * heightRatio, centeredOnPosition); } rectangleWithWidthRelativeToHeight(widthRatio = 1, centeredOnPosition = import_UIObject.nil) { return this.rectangleWithWidth(this.height * widthRatio, centeredOnPosition); } rectangleWithX(x, centeredOnPosition = 0) { const result = this.lazyCopy(); result.x = x - result.width * centeredOnPosition; return result; } rectangleWithY(y, centeredOnPosition = 0) { const result = this.lazyCopy(); result.y = y - result.height * centeredOnPosition; return result; } rectangleByAddingX(x) { const result = this.lazyCopy(); result.x = this.x + x; return result; } rectangleByAddingY(y) { const result = this.lazyCopy(); result.y = this.y + y; return result; } rectangleByAddingWidth(widthToAdd, centeredOnPosition = 0) { const result = this.rectangleWithWidth(this.width + widthToAdd, centeredOnPosition); return result; } rectangleByAddingHeight(heightToAdd, centeredOnPosition = 0) { const result = this.rectangleWithHeight(this.height + heightToAdd, centeredOnPosition); return result; } rectangleWithRelativeValues(relativeXPosition, widthMultiplier, relativeYPosition, heightMultiplier) { const result = this.lazyCopy(); result.materialize(); const width = result.width; const height = result.height; result.width = widthMultiplier * width; result.height = heightMultiplier * height; result.center = new import_UIPoint.UIPoint( relativeXPosition * width, relativeYPosition * height ); return result; } rectangleWithMaxWidth(maxWidth, centeredOnPosition = 0) { if (this.width <= maxWidth) { return this.lazyCopy(); } return this.rectangleWithWidth(maxWidth, centeredOnPosition); } rectangleWithMaxHeight(maxHeight, centeredOnPosition = 0) { if (this.height <= maxHeight) { return this.lazyCopy(); } return this.rectangleWithHeight(maxHeight, centeredOnPosition); } rectangleWithMinWidth(minWidth, centeredOnPosition = 0) { if (this.width >= minWidth) { return this.lazyCopy(); } return this.rectangleWithWidth(minWidth, centeredOnPosition); } rectangleWithMinHeight(minHeight, centeredOnPosition = 0) { if (this.height >= minHeight) { return this.lazyCopy(); } return this.rectangleWithHeight(minHeight, centeredOnPosition); } rectangleByCenteringInRectangle(referenceRectangle, xPosition = 0.5, yPosition = 0.5) { const result = this.lazyCopy(); result.center = referenceRectangle.topLeft.pointByAddingX(xPosition * referenceRectangle.width).pointByAddingY(yPosition * referenceRectangle.height); return result; } rectanglesBySplittingWidth(weights, paddings = 0, absoluteWidths = import_UIObject.nil) { if ((0, import_UIObject.IS_NIL)(paddings)) { paddings = 1; } if (!(paddings instanceof Array)) { paddings = [paddings].arrayByRepeating(weights.length - 1); } paddings = paddings.arrayByTrimmingToLengthIfLonger(weights.length - 1); paddings = paddings.map((padding) => this._widthNumberFromSizeNumberOrFunctionOrView(padding)); if (!(absoluteWidths instanceof Array) && (0, import_UIObject.IS_NOT_NIL)(absoluteWidths)) { absoluteWidths = [absoluteWidths].arrayByRepeating(weights.length); } absoluteWidths = absoluteWidths.map( (width) => this._widthNumberFromSizeNumberOrFunctionOrView(width) ); weights = weights.map((weight) => this._widthNumberFromSizeNumberOrFunctionOrView(weight)); const result = []; const sumOfWeights = weights.reduce( (a, b, index) => { if ((0, import_UIObject.IS_NOT_NIL)(absoluteWidths[index])) { b = 0; } return a + b; }, 0 ); const sumOfPaddings = paddings.summedValue; const sumOfAbsoluteWidths = absoluteWidths.summedValue; const totalRelativeWidth = this.width - sumOfPaddings - sumOfAbsoluteWidths; let previousCellMaxX = this.x; for (let i = 0; i < weights.length; i++) { let resultWidth; if ((0, import_UIObject.IS_NOT_NIL)(absoluteWidths[i])) { resultWidth = absoluteWidths[i] || 0; } else { resultWidth = totalRelativeWidth * (weights[i] / sumOfWeights); } const rectangle = this.rectangleWithWidth(resultWidth); let padding = 0; if (paddings.length > i && paddings[i]) { padding = paddings[i]; } rectangle.x = previousCellMaxX; previousCellMaxX = rectangle.max.x + padding; result.push(rectangle); } return result; } rectanglesBySplittingHeight(weights, paddings = 0, absoluteHeights = import_UIObject.nil) { if ((0, import_UIObject.IS_NIL)(paddings)) { paddings = 1; } if (!(paddings instanceof Array)) { paddings = [paddings].arrayByRepeating(weights.length - 1); } paddings = paddings.arrayByTrimmingToLengthIfLonger(weights.length - 1); paddings = paddings.map((padding) => this._heightNumberFromSizeNumberOrFunctionOrView(padding)); if (!(absoluteHeights instanceof Array) && (0, import_UIObject.IS_NOT_NIL)(absoluteHeights)) { absoluteHeights = [absoluteHeights].arrayByRepeating(weights.length); } absoluteHeights = absoluteHeights.map( (height) => this._heightNumberFromSizeNumberOrFunctionOrView(height) ); weights = weights.map((weight) => this._heightNumberFromSizeNumberOrFunctionOrView(weight)); const result = []; const sumOfWeights = weights.reduce( (a, b, index) => { if ((0, import_UIObject.IS_NOT_NIL)(absoluteHeights[index])) { b = 0; } return a + b; }, 0 ); const sumOfPaddings = paddings.summedValue; const sumOfAbsoluteHeights = absoluteHeights.summedValue; const totalRelativeHeight = this.height - sumOfPaddings - sumOfAbsoluteHeights; let previousCellMaxY = this.y; for (let i = 0; i < weights.length; i++) { let resultHeight; if ((0, import_UIObject.IS_NOT_NIL)(absoluteHeights[i])) { resultHeight = absoluteHeights[i] || 0; } else { resultHeight = totalRelativeHeight * (weights[i] / sumOfWeights); } const rectangle = this.rectangleWithHeight(resultHeight); let padding = 0; if (paddings.length > i && paddings[i]) { padding = paddings[i]; } rectangle.y = previousCellMaxY; previousCellMaxY = rectangle.max.y + padding; result.push(rectangle); } return result; } rectanglesByEquallySplittingWidth(numberOfFrames, padding = 0) { const result = []; const totalPadding = padding * (numberOfFrames - 1); const resultWidth = (this.width - totalPadding) / numberOfFrames; for (var i = 0; i < numberOfFrames; i++) { const rectangle = this.rectangleWithWidth(resultWidth, i / (numberOfFrames - 1)); result.push(rectangle); } return result; } rectanglesByEquallySplittingHeight(numberOfFrames, padding = 0) { const result = []; const totalPadding = padding * (numberOfFrames - 1); const resultHeight = (this.height - totalPadding) / numberOfFrames; for (var i = 0; i < numberOfFrames; i++) { const rectangle = this.rectangleWithHeight(resultHeight, i / (numberOfFrames - 1)); result.push(rectangle); } return result; } distributeViewsAlongWidth(views, weights = 1, paddings, absoluteWidths) { if (!(weights instanceof Array)) { weights = [weights].arrayByRepeating(views.length); } const frames = this.rectanglesBySplittingWidth(weights, paddings, absoluteWidths); frames.forEach((frame, index) => (0, import_UIObject.FIRST_OR_NIL)(views[index]).frame = frame); return this; } distributeViewsAlongHeight(views, weights = 1, paddings, absoluteHeights) { if (!(weights instanceof Array)) { weights = [weights].arrayByRepeating(views.length); } const frames = this.rectanglesBySplittingHeight(weights, paddings, absoluteHeights); frames.forEach((frame, index) => (0, import_UIObject.FIRST_OR_NIL)(views[index]).frame = frame); return this; } distributeViewsEquallyAlongWidth(views, padding) { const frames = this.rectanglesByEquallySplittingWidth(views.length, padding); frames.forEach((frame, index) => views[index].frame = frame); return this; } distributeViewsEquallyAlongHeight(views, padding) { const frames = this.rectanglesByEquallySplittingHeight(views.length, padding); frames.forEach((frame, index) => views[index].frame = frame); return this; } _heightNumberFromSizeNumberOrFunctionOrView(height) { if (height instanceof Function) { return height(this.width); } if (height instanceof import_UIView.UIView) { return height.intrinsicContentHeight(this.width); } return height; } _widthNumberFromSizeNumberOrFunctionOrView(width) { if (width instanceof Function) { return width(this.height); } if (width instanceof import_UIView.UIView) { return width.intrinsicContentWidth(this.height); } return width; } rectangleForNextRow(padding = 0, height = this.height) { const heightNumber = this._heightNumberFromSizeNumberOrFunctionOrView(height); const result = this.rectangleWithY(this.max.y + padding); if (heightNumber != this.height) { result.height = heightNumber; } return result; } rectangleForNextColumn(padding = 0, width = this.width) { const widthNumber = this._widthNumberFromSizeNumberOrFunctionOrView(width); const result = this.rectangleWithX(this.max.x + padding); if (widthNumber != this.width) { result.width = widthNumber; } return result; } rectangleForPreviousRow(padding = 0, height = this.height) { const heightNumber = this._heightNumberFromSizeNumberOrFunctionOrView(height); const result = this.rectangleWithY(this.min.y - heightNumber - padding); if (heightNumber != this.height) { result.height = heightNumber; } return result; } rectangleForPreviousColumn(padding = 0, width = this.width) { const widthNumber = this._widthNumberFromSizeNumberOrFunctionOrView(width); const result = this.rectangleWithX(this.min.x - widthNumber - padding); if (widthNumber != this.width) { result.width = widthNumber; } return result; } framesByDistributingViewsAsColumn(views, paddings = 0, absoluteHeights = import_UIObject.nil) { const frames = []; let currentRectangle = this.lazyCopy(); if (!(paddings instanceof Array)) { paddings = [paddings].arrayByRepeating(views.length - 1); } paddings = paddings.map((padding) => this._heightNumberFromSizeNumberOrFunctionOrView(padding)); if (!(absoluteHeights instanceof Array) && (0, import_UIObject.IS_NOT_NIL)(absoluteHeights)) { absoluteHeights = [absoluteHeights].arrayByRepeating(views.length); } absoluteHeights = absoluteHeights.map( (height) => this._heightNumberFromSizeNumberOrFunctionOrView(height) ); for (let i = 0; i < views.length; i++) { const frame = currentRectangle.rectangleWithHeight(views[i]); if ((0, import_UIObject.IS_NOT_NIL)(absoluteHeights[i])) { frame.height = absoluteHeights[i]; } views[i].frame = frame; frames.push(frame); const padding = paddings[i] || 0; currentRectangle = frame.rectangleForNextRow(padding); } return frames; } framesByDistributingViewsAsRow(views, paddings = 0, absoluteWidths = import_UIObject.nil, centeredOnPosition = 0) { const frames = []; let currentRectangle = this.lazyCopy(); if (!(paddings instanceof Array)) { paddings = [paddings].arrayByRepeating(views.length - 1); } paddings = paddings.map((padding) => this._widthNumberFromSizeNumberOrFunctionOrView(padding)); if (!(absoluteWidths instanceof Array) && (0, import_UIObject.IS_NOT_NIL)(absoluteWidths)) { absoluteWidths = [absoluteWidths].arrayByRepeating(views.length); } absoluteWidths = absoluteWidths.map( (width) => this._widthNumberFromSizeNumberOrFunctionOrView(width) ); for (let i = 0; i < views.length; i++) { const frame = currentRectangle.rectangleWithWidth(views[i]); if ((0, import_UIObject.IS_NOT_NIL)(absoluteWidths[i])) { frame.width = absoluteWidths[i]; } frames.push(frame); const padding = paddings[i] || 0; currentRectangle = frame.rectangleForNextColumn(padding); } if (centeredOnPosition !== 0 && frames.length > 0) { const rowWidth = frames.lastElement.max.x - frames.firstElement.x; const offset = (this.width - rowWidth) * centeredOnPosition - (frames.firstElement.x - this.x); frames.forEach((frame) => { frame.x += offset; }); } frames.forEach((frame, index) => views[index].frame = frame); return frames; } framesByDistributingViewsAsGrid(views, paddings = 0, absoluteHeights = import_UIObject.nil) { const frames = []; let currentRowRectangle = this.lazyCopy(); if (!(paddings instanceof Array)) { paddings = [paddings].arrayByRepeating(views.length - 1); } paddings = paddings.map((padding) => this._heightNumberFromSizeNumberOrFunctionOrView(padding)); if (!(absoluteHeights instanceof Array) && (0, import_UIObject.IS_NOT_NIL)(absoluteHeights)) { absoluteHeights = [absoluteHeights].arrayByRepeating(views.length); } absoluteHeights = absoluteHeights.map( (height) => this._heightNumberFromSizeNumberOrFunctionOrView(height) ); for (let i = 0; i < views.length; i++) { const rowViews = views[i]; const rowFrames = currentRowRectangle.framesByDistributingViewsAsRow(rowViews); if ((0, import_UIObject.IS_NOT_NIL)(absoluteHeights[i])) { const heightNumber = absoluteHeights[i]; rowFrames.forEach((frame, j) => { frame.height = heightNumber; rowViews[j].frame = frame; }); } frames.push(rowFrames); const padding = paddings[i] || 0; const maxHeight = Math.max(...rowFrames.map((f) => f.height)); currentRowRectangle = currentRowRectangle.rectangleForNextRow(padding, maxHeight); } return frames; } rectangleWithIntrinsicContentSizeForView(view, centeredOnXPosition = 0, centeredOnYPosition = 0) { const intrinsicContentSize = view.intrinsicContentSize(); return this.rectangleWithHeight(intrinsicContentSize.height, centeredOnYPosition).rectangleWithWidth(intrinsicContentSize.width, centeredOnXPosition); } settingMinHeight(minHeight) { this.minHeight = minHeight; return this; } settingMinWidth(minWidth) { this.minWidth = minWidth; return this; } settingMaxHeight(maxHeight) { this.maxHeight = maxHeight; return this; } settingMaxWidth(maxWidth) { this.maxWidth = maxWidth; return this; } rectangleByEnforcingMinAndMaxSizes(centeredOnXPosition = 0, centeredOnYPosition = 0) { return this.rectangleWithHeight( [ [this.height, this.maxHeight].filter((value) => (0, import_UIObject.IS_NOT_LIKE_NULL)(value)).min(), this.minHeight ].filter((value) => (0, import_UIObject.IS_NOT_LIKE_NULL)(value)).max(), centeredOnYPosition ).rectangleWithWidth( [ [this.width, this.maxWidth].filter((value) => (0, import_UIObject.IS_NOT_LIKE_NULL)(value)).min(), this.minWidth ].filter((value) => (0, import_UIObject.IS_NOT_LIKE_NULL)(value)).max(), centeredOnXPosition ); } assignedAsFrameOfView(view, isWeakFrame = view.hasWeakFrame) { view.frame = this; view.hasWeakFrame = isWeakFrame; return this; } toString() { const result = "[" + this.class.name + "] { x: " + this.x + ", y: " + this.y + ", height: " + this.height.toFixed(2) + ", width: " + this.height.toFixed(2) + " }"; return result; } get [Symbol.toStringTag]() { return this.toString(); } IF(condition) { const conditionalBlock = new UIRectangleConditionalBlock(this, condition); return conditionalBlock.getProxy(); } ELSE_IF(condition) { return this; } ELSE() { return this; } ENDIF(performFunction) { if (performFunction) { return performFunction(this); } return this; } static boundingBoxForPoints(points) { if (points.length === 0) { return new UIRectangle(); } const first = points[0]; const result = new UIRectangle(first.x, first.y, 0, 0); for (let i = 1; i < points.length; i++) { result.updateByAddingPoint(points[i]); } return result; } static boundingBoxForRectanglesAndPoints(rectanglesAndPoints) { if (rectanglesAndPoints.length === 0) { return new UIRectangle(); } const first = rectanglesAndPoints[0]; const result = first instanceof UIRectangle ? new UIRectangle(first.x, first.y, first.height, first.width) : new UIRectangle(first.x, first.y, 0, 0); for (let i = 1; i < rectanglesAndPoints.length; i++) { const rectangleOrPoint = rectanglesAndPoints[i]; if (rectangleOrPoint instanceof UIRectangle) { result.updateByAddingPoint(rectangleOrPoint.min); result.updateByAddingPoint(rectangleOrPoint.max); } else { result.updateByAddingPoint(rectangleOrPoint); } } return result; } beginUpdates() { this._isBeingUpdated = import_UIObject.YES; } finishUpdates() { this._isBeingUpdated = import_UIObject.NO; this.didChange(); } didChange() { } _rectanglePointDidChange() { if (!this._isBeingUpdated) { this.didChange(); } } } class UIRectangleConditionalBlock { constructor(initialResult, condition) { this._stack = [{ resultBeforeIF: null, currentResult: initialResult, originalResult: initialResult, anyConditionMet: condition, currentBranchActive: condition }]; } get _top() { return this._stack[this._stack.length - 1]; } get _shouldExecute() { return this._stack.every((frame) => frame.currentBranchActive); } createProxy() { const self = this; return new Proxy({}, { get(_, prop) { if (prop === "IF") { return (condition) => { self._stack.push({ resultBeforeIF: self._top.currentResult, currentResult: self._top.currentResult, originalResult: self._top.currentResult, anyConditionMet: condition, currentBranchActive: condition }); return self.createProxy(); }; } if (prop === "TRANSFORM") { return (fn) => { if (self._shouldExecute) { self._top.currentResult = fn(self._top.currentResult); } return self.createProxy(); }; } if (prop === "ELSE_IF") { return (condition) => { const top = self._top; top.currentBranchActive = !top.anyConditionMet && condition; if (top.currentBranchActive) { top.anyConditionMet = true; top.currentResult = top.originalResult; } return self.createProxy(); }; } if (prop === "ELSE") { return () => { const top = self._top; top.currentBranchActive = !top.anyConditionMet; if (top.currentBranchActive) { top.anyConditionMet = true; top.currentResult = top.originalResult; } return self.createProxy(); }; } if (prop === "ENDIF") { let endif2 = function(performFunction) { if (self._stack.length === 1) { const top = self._top; const result = top.currentResult; if (performFunction && top.anyConditionMet) { return performFunction(result); } else { return result; } } const completedFrame = self._stack.pop(); const resolvedResult = completedFrame.anyConditionMet ? completedFrame.currentResult : completedFrame.resultBeforeIF; const finalResult = performFunction ? performFunction(resolvedResult) : resolvedResult; self._top.currentResult = finalResult; return self.createProxy(); }; var endif = endif2; return endif2; } const value = self._top.currentResult[prop]; if (typeof value === "function") { return (...args) => { if (self._shouldExecute) { self._top.currentResult = value.apply(self._top.currentResult, args); } return self.createProxy(); }; } if (self._shouldExecute) { self._top.currentResult = value; } return self.createProxy(); } }); } getProxy() { return this.createProxy(); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { UIRectangle }); //# sourceMappingURL=UIRectangle.js.map