make Rect mutable and use Readonly<Rect> instead

Although readonly is quite a joke in Typescript, this is a better
approach which allows both mutable and immutable patterns.
This commit is contained in:
Eon S. Jeon 2020-07-02 06:20:07 +09:00
parent 2d5e981171
commit 47fa847f7e
2 changed files with 19 additions and 11 deletions

View File

@ -91,7 +91,7 @@ interface IConfig {
interface IDriverWindow {
readonly fullScreen: boolean;
readonly geometry: Rect;
readonly geometry: Readonly<Rect>;
readonly id: string;
readonly maximized: boolean;
readonly shouldIgnore: boolean;
@ -106,7 +106,7 @@ interface IDriverWindow {
interface ISurface {
readonly id: string;
readonly ignore: boolean;
readonly workingArea: Rect;
readonly workingArea: Readonly<Rect>;
next(): ISurface | null;
}

View File

@ -19,10 +19,13 @@
// DEALINGS IN THE SOFTWARE.
class Rect {
public readonly height: number;
public readonly width: number;
public readonly x: number;
public readonly y: number;
constructor(
public x: number,
public y: number,
public width: number,
public height: number,
) {
}
public get maxX(): number { return this.x + this.width; }
public get maxY(): number { return this.y + this.height; }
@ -34,11 +37,8 @@ class Rect {
];
}
constructor(x: number, y: number, w: number, h: number) {
this.height = h;
this.width = w;
this.x = x;
this.y = y;
public clone(): Rect {
return new Rect(this.x, this.y, this.width, this.height);
}
public equals(other: Rect): boolean {
@ -59,6 +59,14 @@ class Rect {
);
}
public gap_mut(left: number, right: number, top: number, bottom: number): this {
this.x += left;
this.y += top;
this.width -= (left + right);
this.height -= (top + bottom);
return this;
}
public includes(other: Rect): boolean {
return (
this.x <= other.x &&