Skip to content

Commit f9000da

Browse files
authored
Feature/element queries (#575)
* 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
1 parent 9cf16f8 commit f9000da

21 files changed

+628
-62
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Check out this demo video to get a first impression of what nut.js is capable of
4848

4949
# Tutorials
5050

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

5353
# API Docs
5454

core/nut.js/index.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ import { LineHelper } from "./lib/util/linehelper.class";
88
import { createWindowApi } from "./lib/window.function";
99
import providerRegistry from "./lib/provider/provider-registry.class";
1010
import { loadImageResource } from "./lib/imageResources.function";
11-
import { ColorQuery, LineQuery, RGBA, WindowQuery, WordQuery } from "@nut-tree/shared";
11+
import {
12+
ColorQuery,
13+
LineQuery,
14+
RGBA,
15+
WindowElementDescription,
16+
WindowElementQuery,
17+
WindowQuery,
18+
WordQuery
19+
} from "@nut-tree/shared";
1220

1321
export {
1422
AssertClass,
@@ -91,6 +99,16 @@ const windowWithTitle = (title: string | RegExp): WindowQuery => {
9199
};
92100
};
93101

102+
const windowElementDescribedBy = (description: WindowElementDescription): WindowElementQuery => {
103+
return {
104+
type: "window-element",
105+
id: `window-element-described-by-${JSON.stringify(description)}`,
106+
by: {
107+
description
108+
}
109+
};
110+
};
111+
94112
const pixelWithColor = (color: RGBA): ColorQuery => {
95113
return {
96114
type: "color",
@@ -122,5 +140,6 @@ export {
122140
singleWord,
123141
textLine,
124142
windowWithTitle,
143+
windowElementDescribedBy,
125144
pixelWithColor
126145
};

core/nut.js/lib/provider/provider-registry.class.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
ScreenProviderInterface,
1313
TextFinderInterface,
1414
WindowFinderInterface,
15-
WindowProviderInterface
15+
WindowProviderInterface,
16+
ElementInspectionProviderInterface
1617
} from "@nut-tree/provider-interfaces";
1718

1819
import ImageReaderImpl from "./io/jimp-image-reader.class";
@@ -45,6 +46,7 @@ class DefaultProviderRegistry implements ProviderRegistry {
4546
private _textFinder?: TextFinderInterface;
4647
private _windowFinder?: WindowFinderInterface;
4748
private _colorFinder?: ColorFinderInterface;
49+
private _windowElementInspector?: ElementInspectionProviderInterface;
4850

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

195+
getWindowElementInspector = (): ElementInspectionProviderInterface => {
196+
if (this._windowElementInspector) {
197+
return this._windowElementInspector;
198+
}
199+
const error = new Error(`No WindowElementInspector registered`);
200+
this.getLogProvider().error(error);
201+
throw error;
202+
};
203+
204+
registerWindowElementInspector = (value: ElementInspectionProviderInterface) => {
205+
this._windowElementInspector = value;
206+
this.getLogProvider().trace("Registered new WindowElementInspector provider", value);
207+
};
208+
193209
hasImageReader(): boolean {
194210
return this._imageReader != null;
195211
}

core/nut.js/lib/screen.class.ts

+7-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { cwd } from "process";
22
import {
33
ColorQuery,
44
FileType,
5+
FindHookCallback,
56
FindInput,
67
FindResult,
78
Image,
@@ -13,12 +14,13 @@ import {
1314
isWindowQuery,
1415
LineQuery,
1516
MatchRequest,
16-
MatchResult,
17+
MatchResultCallback,
1718
OptionalSearchParameters,
1819
Point,
1920
PointResultFindInput,
2021
Region,
2122
RegionResultFindInput,
23+
WindowCallback,
2224
WindowResultFindInput,
2325
WordQuery
2426
} from "@nut-tree/shared";
@@ -34,15 +36,6 @@ import {
3436
isRegionResultFindInput
3537
} from "./screen-helpers.function";
3638

37-
export type WindowCallback = (target: Window) => void | Promise<void>;
38-
export type MatchResultCallback<TARGET_TYPE> = (
39-
target: MatchResult<TARGET_TYPE>
40-
) => void | Promise<void>;
41-
export type FindHookCallback =
42-
| WindowCallback
43-
| MatchResultCallback<Point>
44-
| MatchResultCallback<Region>;
45-
4639
function validateSearchRegion(
4740
search: Region,
4841
screen: Region,
@@ -333,6 +326,10 @@ export class ScreenClass {
333326
searchInput: WindowResultFindInput | Promise<WindowResultFindInput>,
334327
params?: OptionalSearchParameters<PROVIDER_DATA_TYPE>
335328
): Promise<Window[]>;
329+
public async findAll<PROVIDER_DATA_TYPE>(
330+
searchInput: FindInput | Promise<FindInput>,
331+
params?: OptionalSearchParameters<PROVIDER_DATA_TYPE>
332+
): Promise<FindResult[]>;
336333
public async findAll<PROVIDER_DATA_TYPE>(
337334
searchInput: FindInput | Promise<FindInput>,
338335
params?: OptionalSearchParameters<PROVIDER_DATA_TYPE>

0 commit comments

Comments
 (0)