UNPKG

scriptable-testlab

Version:

A lightweight, efficient tool designed to manage and update scripts for Scriptable.

194 lines 4.92 kB
import { AbsRect } from "scriptable-abstract"; import { MockPoint } from "./point"; import { MockSize } from "./size"; const _DEFAULT_STATE = { x: 0, y: 0, width: 0, height: 0, minX: 0, minY: 0, maxX: 0, maxY: 0, midX: 0, midY: 0, origin: new MockPoint(0, 0), size: new MockSize(0, 0) }; class MockRect extends AbsRect { constructor(x = 0, y = 0, width = 0, height = 0) { if (Number.isNaN(x)) throw new Error("x coordinate must be a valid number"); if (!Number.isFinite(x)) throw new Error("x coordinate must be finite"); if (Number.isNaN(y)) throw new Error("y coordinate must be a valid number"); if (!Number.isFinite(y)) throw new Error("y coordinate must be finite"); if (Number.isNaN(width)) throw new Error("width must be a valid number"); if (!Number.isFinite(width)) throw new Error("width must be finite"); if (width < 0) throw new Error("width must be non-negative"); if (Number.isNaN(height)) throw new Error("height must be a valid number"); if (!Number.isFinite(height)) throw new Error("height must be finite"); if (height < 0) throw new Error("height must be non-negative"); super({ x, y, width, height, minX: x, minY: y, maxX: x + width, maxY: y + height, midX: x + width / 2, midY: y + height / 2, origin: new MockPoint(x, y), size: new MockSize(width, height) }); } validateCoordinate(value, name) { if (Number.isNaN(value)) { throw new Error(`${name} coordinate must be a valid number`); } if (!Number.isFinite(value)) { throw new Error(`${name} coordinate must be finite`); } } validateDimension(value, name) { if (Number.isNaN(value)) { throw new Error(`${name} must be a valid number`); } if (!Number.isFinite(value)) { throw new Error(`${name} must be finite`); } if (value < 0) { throw new Error(`${name} must be non-negative`); } } get x() { return this.state.x; } set x(value) { this.validateCoordinate(value, "x"); const origin = new MockPoint(value, this.y); this.setState({ x: value, minX: value, maxX: value + this.width, midX: value + this.width / 2, origin }); } get y() { return this.state.y; } set y(value) { this.validateCoordinate(value, "y"); const origin = new MockPoint(this.x, value); this.setState({ y: value, minY: value, maxY: value + this.height, midY: value + this.height / 2, origin }); } get width() { return this.state.width; } set width(value) { this.validateDimension(value, "width"); const size = new MockSize(value, this.height); this.setState({ width: value, maxX: this.x + value, midX: this.x + value / 2, size }); } get height() { return this.state.height; } set height(value) { this.validateDimension(value, "height"); const size = new MockSize(this.width, value); this.setState({ height: value, maxY: this.y + value, midY: this.y + value / 2, size }); } get origin() { return this.state.origin; } set origin(value) { this.setState({ x: value.x, y: value.y, minX: value.x, minY: value.y, maxX: value.x + this.width, maxY: value.y + this.height, midX: value.x + this.width / 2, midY: value.y + this.height / 2, origin: value }); } get size() { return this.state.size; } set size(value) { this.setState({ width: value.width, height: value.height, maxX: this.x + value.width, maxY: this.y + value.height, midX: this.x + value.width / 2, midY: this.y + value.height / 2, size: value }); } get minX() { return this.state.minX; } get minY() { return this.state.minY; } get maxX() { return this.state.maxX; } get maxY() { return this.state.maxY; } get midX() { return this.state.midX; } get midY() { return this.state.midY; } setState(state) { const newState = { ...this.state, ...state }; if ("x" in state || "width" in state) { newState.minX = newState.x; newState.maxX = newState.x + newState.width; newState.midX = newState.x + newState.width / 2; } if ("y" in state || "height" in state) { newState.minY = newState.y; newState.maxY = newState.y + newState.height; newState.midY = newState.y + newState.height / 2; } if ("x" in state || "y" in state) { newState.origin = new MockPoint(newState.x, newState.y); } if ("width" in state || "height" in state) { newState.size = new MockSize(newState.width, newState.height); } super.setState(newState); return this; } } export { MockRect }; //# sourceMappingURL=rect.js.map