Skip to content

Commit ddeab54

Browse files
committed
(#87) Screen API docs
1 parent 2d2a0c1 commit ddeab54

File tree

1 file changed

+63
-10
lines changed

1 file changed

+63
-10
lines changed

Diff for: lib/screen.class.ts

+63-10
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,68 @@ import { timeout } from "./util/poll-action.function";
1111

1212
export type FindHookCallback = (target: MatchResult) => Promise<void>;
1313

14+
/**
15+
* {@link Screen} class provides methods to access screen content of a systems main display
16+
*/
1417
export class Screen {
18+
19+
/**
20+
* Config object for {@link Screen} class
21+
*/
1522
public config = {
23+
/**
24+
* Configures the required matching percentage for template images to be declared as a match
25+
*/
1626
confidence: 0.99,
27+
28+
/**
29+
* Configures the path from which template images are loaded from
30+
*/
1731
resourceDirectory: cwd(),
1832
};
1933

34+
/**
35+
* {@link Screen} class constructor
36+
* @param vision {@link VisionAdapter} instance which bundles access to screen and / or computer vision related APIs
37+
* @param findHooks A {@link Map} of {@link FindHookCallback} methods assigned to a template image filename
38+
*/
2039
constructor(
2140
private vision: VisionAdapter,
2241
private findHooks: Map<string, FindHookCallback[]> = new Map<string, FindHookCallback[]>()) {
2342
}
2443

44+
/**
45+
* {@link width} returns the main screen width
46+
* This refers to the hardware resolution.
47+
* Screens with higher pixel density (e.g. retina displays in MacBooks) might have a higher width in in actual pixels
48+
*/
2549
public width() {
2650
return this.vision.screenWidth();
2751
}
2852

53+
/**
54+
* {@link height} returns the main screen height
55+
* This refers to the hardware resolution.
56+
* Screens with higher pixel density (e.g. retina displays in MacBooks) might have a higher height in in actual pixels
57+
*/
2958
public height() {
3059
return this.vision.screenHeight();
3160
}
3261

62+
/**
63+
* {@link find} will search for a template image on a systems main screen
64+
* @param templateImageFilename Filename of the template image, relative to {@link Screen.config.resourceDirectory}
65+
* @param params {@link LocationParameters} which are used to fine tune search region and / or match confidence
66+
*/
3367
public async find(
34-
pathToNeedle: string,
68+
templateImageFilename: string,
3569
params?: LocationParameters,
3670
): Promise<Region> {
3771
const minMatch = (params && params.confidence) || this.config.confidence;
3872
const searchRegion =
3973
(params && params.searchRegion) || await this.vision.screenSize();
4074

41-
const fullPathToNeedle = normalize(join(this.config.resourceDirectory, pathToNeedle));
75+
const fullPathToNeedle = normalize(join(this.config.resourceDirectory, templateImageFilename));
4276

4377
const screenImage = await this.vision.grabScreen();
4478

@@ -53,39 +87,58 @@ export class Screen {
5387
try {
5488
const matchResult = await this.vision.findOnScreenRegion(matchRequest);
5589
if (matchResult.confidence >= minMatch) {
56-
const possibleHooks = this.findHooks.get(pathToNeedle) || [];
90+
const possibleHooks = this.findHooks.get(templateImageFilename) || [];
5791
for (const hook of possibleHooks) {
5892
await hook(matchResult);
5993
}
6094
resolve(matchResult.location);
6195
} else {
6296
reject(
63-
`No match for ${pathToNeedle}. Required: ${minMatch}, given: ${
97+
`No match for ${templateImageFilename}. Required: ${minMatch}, given: ${
6498
matchResult.confidence
6599
}`,
66100
);
67101
}
68102
} catch (e) {
69103
reject(
70-
`Searching for ${pathToNeedle} failed. Reason: '${e}'`,
104+
`Searching for ${templateImageFilename} failed. Reason: '${e}'`,
71105
);
72106
}
73107
});
74108
}
75109

110+
/**
111+
* {@link waitFor} searches for a template image for a specified duration
112+
* @param templateImageFilename Filename of the template image, relative to {@link Screen.config.resourceDirectory}
113+
* @param timeoutMs Timeout in milliseconds after which {@link waitFor} fails
114+
* @param params {@link LocationParameters} which are used to fine tune search region and / or match confidence
115+
*/
76116
public async waitFor(
77-
pathToNeedle: string,
117+
templateImageFilename: string,
78118
timeoutMs: number = 5000,
79119
params?: LocationParameters,
80120
): Promise<Region> {
81-
return timeout(500, timeoutMs, () => this.find(pathToNeedle, params));
121+
return timeout(500, timeoutMs, () => this.find(templateImageFilename, params));
82122
}
83123

84-
public on(pathToNeedle: string, callback: FindHookCallback) {
85-
const existingHooks = this.findHooks.get(pathToNeedle) || [];
86-
this.findHooks.set(pathToNeedle, [...existingHooks, callback]);
124+
/**
125+
* {@link on} registeres a callback which is triggered once a certain template image is found
126+
* @param templateImageFilename Template image to trigger the callback on
127+
* @param callback The {@link FindHookCallback} function to trigger
128+
*/
129+
public on(templateImageFilename: string, callback: FindHookCallback) {
130+
const existingHooks = this.findHooks.get(templateImageFilename) || [];
131+
this.findHooks.set(templateImageFilename, [...existingHooks, callback]);
87132
}
88133

134+
/**
135+
* {@link capture} captures a screenshot of a systems main display
136+
* @param fileName Basename for the generated screenshot
137+
* @param fileFormat The {@link FileType} for the generated screenshot
138+
* @param filePath The output path for the generated screenshot (Default: {@link cwd})
139+
* @param fileNamePrefix Filename prefix for the generated screenshot (Default: empty)
140+
* @param fileNamePostfix Filename postfix for the generated screenshot (Default: empty)
141+
*/
89142
public async capture(
90143
fileName: string,
91144
fileFormat: FileType = FileType.PNG,

0 commit comments

Comments
 (0)