Skip to content

Commit

Permalink
Bumped dependency versions, extended window provider with new capabil…
Browse files Browse the repository at this point in the history
…ities for focus/resize/move window (#540)
  • Loading branch information
s1hofmann authored Oct 17, 2023
1 parent 1901c4b commit 6b68c4a
Show file tree
Hide file tree
Showing 8 changed files with 5,688 additions and 4,552 deletions.
5 changes: 3 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export { centerOf, randomPointIn } from "./lib/location.function";
export { OptionalSearchParameters } from "./lib/optionalsearchparameters.class";
export { EasingFunction, linear } from "./lib/mouse-movement.function";
export { Point } from "./lib/point.class";
export { Size } from "./lib/size.class";
export { Region } from "./lib/region.class";
export { Window } from "./lib/window.class";
export { FileType } from "./lib/file-type.enum";
Expand All @@ -63,7 +64,7 @@ const assert = new AssertClass(screen);

const { straightTo, up, down, left, right } = createMovementApi(
providerRegistry,
lineHelper
lineHelper,
);
const { getWindows, getActiveWindow } = createWindowApi(providerRegistry);

Expand All @@ -74,7 +75,7 @@ const imageResource = (fileName: string) =>
loadImageResource(
providerRegistry,
screen.config.resourceDirectory,
fileName
fileName,
);

const singleWord = (word: string): WordQuery => {
Expand Down
43 changes: 41 additions & 2 deletions lib/provider/native/libnut-window.class.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import libnut = require("@nut-tree/libnut");
import { Region } from "../../region.class";
import { WindowProviderInterface } from "../window-provider.interface";
import { Size } from "../../size.class";
import { Point } from "../../point.class";

export default class WindowAction implements WindowProviderInterface {
public getWindows(): Promise<number[]> {
Expand Down Expand Up @@ -32,8 +34,8 @@ export default class WindowAction implements WindowProviderInterface {
windowRect.x,
windowRect.y,
windowRect.width,
windowRect.height
)
windowRect.height,
),
);
} catch (e) {
reject(e);
Expand All @@ -50,4 +52,41 @@ export default class WindowAction implements WindowProviderInterface {
}
});
}

focusWindow(windowHandle: number): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
try {
resolve(libnut.focusWindow(windowHandle));
} catch (e) {
reject(e);
}
});
}

moveWindow(windowHandle: number, newOrigin: Point): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
try {
resolve(
libnut.moveWindow(windowHandle, { x: newOrigin.x, y: newOrigin.y }),
);
} catch (e) {
reject(e);
}
});
}

resizeWindow(windowHandle: number, newSize: Size): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
try {
resolve(
libnut.resizeWindow(windowHandle, {
width: newSize.width,
height: newSize.height,
}),
);
} catch (e) {
reject(e);
}
});
}
}
17 changes: 17 additions & 0 deletions lib/provider/window-provider.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Region } from "../region.class";
import { Point } from "../point.class";
import { Size } from "../size.class";

/**
* A WindowActionProvider should provide access to a system's window system
Expand Down Expand Up @@ -34,4 +36,19 @@ export interface WindowProviderInterface {
* @returns The {@link Region} occupied by the window addressed via its window handle
*/
getWindowRegion(windowHandle: number): Promise<Region>;

/**
* {@link focusWindow} Focuses the window addressed via its window handle
*/
focusWindow(windowHandle: number): Promise<boolean>;

/**
* {@link moveWindow} Moves the window addressed via its window handle to a new origin given as {@link Point}
*/
moveWindow(windowHandle: number, newOrigin: Point): Promise<boolean>;

/**
* {@link resizeWindow} Resizes the window addressed via its window handle to a new {@link Size}
*/
resizeWindow(windowHandle: number, newSize: Size): Promise<boolean>;
}
68 changes: 68 additions & 0 deletions lib/size.class.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { isSize, Size } from "./size.class";

describe("Size", () => {
it("should calculate the correct area of a Size", () => {
const size = new Size(100, 100);
const expected = size.width * size.height;

expect(size.area()).toEqual(expected);
});

it("should return a proper string representation", () => {
const size = new Size(100, 100);
const expected = "(100x100)";

expect(size.toString()).toEqual(expected);
});

describe("isSize typeguard", () => {
it("should identify a Size object", () => {
// GIVEN
const s = new Size(100, 100);

// WHEN
const result = isSize(s);

// THEN
expect(result).toBeTruthy();
});

it("should rule out non-size objects", () => {
// GIVEN
const r = "foo";

// WHEN
const result = isSize(r);

// THEN
expect(result).toBeFalsy();
});

it("should rule out possible object with missing properties", () => {
// GIVEN
const r = {
width: 100,
};

// WHEN
const result = isSize(r);

// THEN
expect(result).toBeFalsy();
});

it("should rule out possible object with wrong property type", () => {
// GIVEN
const r = {
width: "foo",
height: 200,
};

// WHEN
const result = isSize(r);

// THEN
expect(result).toBeFalsy();
});
});
});
33 changes: 33 additions & 0 deletions lib/size.class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export class Size {
constructor(
public width: number,
public height: number,
) {}

public area() {
return this.width * this.height;
}

public toString() {
return `(${this.width}x${this.height})`;
}
}

const testSize = new Size(100, 100);
const sizeKeys = Object.keys(testSize);
export function isSize(possibleSize: any): possibleSize is Size {
if (typeof possibleSize !== "object") {
return false;
}
for (const key of sizeKeys) {
if (!(key in possibleSize)) {
return false;
}
const possibleSizeKeyType = typeof possibleSize[key];
const sizeKeyType = typeof testSize[key as keyof typeof testSize];
if (possibleSizeKeyType !== sizeKeyType) {
return false;
}
}
return true;
}
20 changes: 19 additions & 1 deletion lib/window.class.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Region } from "./region.class";
import { ProviderRegistry } from "./provider/provider-registry.class";
import { Point } from "./point.class";
import { Size } from "./size.class";

export class Window {
constructor(
private providerRegistry: ProviderRegistry,
private windowHandle: number
private windowHandle: number,
) {}

get title(): Promise<string> {
Expand All @@ -14,4 +16,20 @@ export class Window {
get region(): Promise<Region> {
return this.providerRegistry.getWindow().getWindowRegion(this.windowHandle);
}

async move(newOrigin: Point) {
await this.providerRegistry
.getWindow()
.moveWindow(this.windowHandle, newOrigin);
}

async resize(newSize: Size) {
await this.providerRegistry
.getWindow()
.resizeWindow(this.windowHandle, newSize);
}

async focus() {
await this.providerRegistry.getWindow().focusWindow(this.windowHandle);
}
}
Loading

0 comments on commit 6b68c4a

Please sign in to comment.