Skip to content

Commit

Permalink
Feature/element queries (#575)
Browse files Browse the repository at this point in the history
* Extended window provider with minimize and restore methods

* Moved window interface to types folder, extended it with element query definitions

* Defined intefaces for WindowElement objects and their description

* Introduces window-element query type

* Introduced ElementInspectionProviderInterface

* Added ElementInspectionProviderInterface to provider registry

* Restructured shared code to improve exports

* Introduced windowElementDescribedBy query

* Implemented find, findAll, waitFor and hooks for windows

* Fixed broken tutorials link in readme

* Updated window tests

* Removed leftover dead code
  • Loading branch information
s1hofmann committed Apr 2, 2024
1 parent 9cf16f8 commit f9000da
Show file tree
Hide file tree
Showing 21 changed files with 628 additions and 62 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Check out this demo video to get a first impression of what nut.js is capable of

# Tutorials

Please consult the project website at [nutjs.dev](https://nutjs.dev/docs/tutorial-first_steps/prerequisites) for in-depth tutorials
Please consult the project website at [nutjs.dev](https://nutjs.dev/tutorials/first_steps#prerequisites) for in-depth tutorials

# API Docs

Expand Down
21 changes: 20 additions & 1 deletion core/nut.js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ import { LineHelper } from "./lib/util/linehelper.class";
import { createWindowApi } from "./lib/window.function";
import providerRegistry from "./lib/provider/provider-registry.class";
import { loadImageResource } from "./lib/imageResources.function";
import { ColorQuery, LineQuery, RGBA, WindowQuery, WordQuery } from "@nut-tree/shared";
import {
ColorQuery,
LineQuery,
RGBA,
WindowElementDescription,
WindowElementQuery,
WindowQuery,
WordQuery
} from "@nut-tree/shared";

export {
AssertClass,
Expand Down Expand Up @@ -91,6 +99,16 @@ const windowWithTitle = (title: string | RegExp): WindowQuery => {
};
};

const windowElementDescribedBy = (description: WindowElementDescription): WindowElementQuery => {
return {
type: "window-element",
id: `window-element-described-by-${JSON.stringify(description)}`,
by: {
description
}
};
};

const pixelWithColor = (color: RGBA): ColorQuery => {
return {
type: "color",
Expand Down Expand Up @@ -122,5 +140,6 @@ export {
singleWord,
textLine,
windowWithTitle,
windowElementDescribedBy,
pixelWithColor
};
18 changes: 17 additions & 1 deletion core/nut.js/lib/provider/provider-registry.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
ScreenProviderInterface,
TextFinderInterface,
WindowFinderInterface,
WindowProviderInterface
WindowProviderInterface,
ElementInspectionProviderInterface
} from "@nut-tree/provider-interfaces";

import ImageReaderImpl from "./io/jimp-image-reader.class";
Expand Down Expand Up @@ -45,6 +46,7 @@ class DefaultProviderRegistry implements ProviderRegistry {
private _textFinder?: TextFinderInterface;
private _windowFinder?: WindowFinderInterface;
private _colorFinder?: ColorFinderInterface;
private _windowElementInspector?: ElementInspectionProviderInterface;

hasClipboard(): boolean {
return this._clipboard != null;
Expand Down Expand Up @@ -190,6 +192,20 @@ class DefaultProviderRegistry implements ProviderRegistry {
this.getLogProvider().trace("Registered new WindowFinder provider", value);
};

getWindowElementInspector = (): ElementInspectionProviderInterface => {
if (this._windowElementInspector) {
return this._windowElementInspector;
}
const error = new Error(`No WindowElementInspector registered`);
this.getLogProvider().error(error);
throw error;
};

registerWindowElementInspector = (value: ElementInspectionProviderInterface) => {
this._windowElementInspector = value;
this.getLogProvider().trace("Registered new WindowElementInspector provider", value);
};

hasImageReader(): boolean {
return this._imageReader != null;
}
Expand Down
17 changes: 7 additions & 10 deletions core/nut.js/lib/screen.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { cwd } from "process";
import {
ColorQuery,
FileType,
FindHookCallback,
FindInput,
FindResult,
Image,
Expand All @@ -13,12 +14,13 @@ import {
isWindowQuery,
LineQuery,
MatchRequest,
MatchResult,
MatchResultCallback,
OptionalSearchParameters,
Point,
PointResultFindInput,
Region,
RegionResultFindInput,
WindowCallback,
WindowResultFindInput,
WordQuery
} from "@nut-tree/shared";
Expand All @@ -34,15 +36,6 @@ import {
isRegionResultFindInput
} from "./screen-helpers.function";

export type WindowCallback = (target: Window) => void | Promise<void>;
export type MatchResultCallback<TARGET_TYPE> = (
target: MatchResult<TARGET_TYPE>
) => void | Promise<void>;
export type FindHookCallback =
| WindowCallback
| MatchResultCallback<Point>
| MatchResultCallback<Region>;

function validateSearchRegion(
search: Region,
screen: Region,
Expand Down Expand Up @@ -333,6 +326,10 @@ export class ScreenClass {
searchInput: WindowResultFindInput | Promise<WindowResultFindInput>,
params?: OptionalSearchParameters<PROVIDER_DATA_TYPE>
): Promise<Window[]>;
public async findAll<PROVIDER_DATA_TYPE>(
searchInput: FindInput | Promise<FindInput>,
params?: OptionalSearchParameters<PROVIDER_DATA_TYPE>
): Promise<FindResult[]>;
public async findAll<PROVIDER_DATA_TYPE>(
searchInput: FindInput | Promise<FindInput>,
params?: OptionalSearchParameters<PROVIDER_DATA_TYPE>
Expand Down
Loading

0 comments on commit f9000da

Please sign in to comment.