@@ -11,34 +11,68 @@ import { timeout } from "./util/poll-action.function";
11
11
12
12
export type FindHookCallback = ( target : MatchResult ) => Promise < void > ;
13
13
14
+ /**
15
+ * {@link Screen } class provides methods to access screen content of a systems main display
16
+ */
14
17
export class Screen {
18
+
19
+ /**
20
+ * Config object for {@link Screen} class
21
+ */
15
22
public config = {
23
+ /**
24
+ * Configures the required matching percentage for template images to be declared as a match
25
+ */
16
26
confidence : 0.99 ,
27
+
28
+ /**
29
+ * Configures the path from which template images are loaded from
30
+ */
17
31
resourceDirectory : cwd ( ) ,
18
32
} ;
19
33
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
+ */
20
39
constructor (
21
40
private vision : VisionAdapter ,
22
41
private findHooks : Map < string , FindHookCallback [ ] > = new Map < string , FindHookCallback [ ] > ( ) ) {
23
42
}
24
43
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
+ */
25
49
public width ( ) {
26
50
return this . vision . screenWidth ( ) ;
27
51
}
28
52
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
+ */
29
58
public height ( ) {
30
59
return this . vision . screenHeight ( ) ;
31
60
}
32
61
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
+ */
33
67
public async find (
34
- pathToNeedle : string ,
68
+ templateImageFilename : string ,
35
69
params ?: LocationParameters ,
36
70
) : Promise < Region > {
37
71
const minMatch = ( params && params . confidence ) || this . config . confidence ;
38
72
const searchRegion =
39
73
( params && params . searchRegion ) || await this . vision . screenSize ( ) ;
40
74
41
- const fullPathToNeedle = normalize ( join ( this . config . resourceDirectory , pathToNeedle ) ) ;
75
+ const fullPathToNeedle = normalize ( join ( this . config . resourceDirectory , templateImageFilename ) ) ;
42
76
43
77
const screenImage = await this . vision . grabScreen ( ) ;
44
78
@@ -53,39 +87,58 @@ export class Screen {
53
87
try {
54
88
const matchResult = await this . vision . findOnScreenRegion ( matchRequest ) ;
55
89
if ( matchResult . confidence >= minMatch ) {
56
- const possibleHooks = this . findHooks . get ( pathToNeedle ) || [ ] ;
90
+ const possibleHooks = this . findHooks . get ( templateImageFilename ) || [ ] ;
57
91
for ( const hook of possibleHooks ) {
58
92
await hook ( matchResult ) ;
59
93
}
60
94
resolve ( matchResult . location ) ;
61
95
} else {
62
96
reject (
63
- `No match for ${ pathToNeedle } . Required: ${ minMatch } , given: ${
97
+ `No match for ${ templateImageFilename } . Required: ${ minMatch } , given: ${
64
98
matchResult . confidence
65
99
} `,
66
100
) ;
67
101
}
68
102
} catch ( e ) {
69
103
reject (
70
- `Searching for ${ pathToNeedle } failed. Reason: '${ e } '` ,
104
+ `Searching for ${ templateImageFilename } failed. Reason: '${ e } '` ,
71
105
) ;
72
106
}
73
107
} ) ;
74
108
}
75
109
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
+ */
76
116
public async waitFor (
77
- pathToNeedle : string ,
117
+ templateImageFilename : string ,
78
118
timeoutMs : number = 5000 ,
79
119
params ?: LocationParameters ,
80
120
) : Promise < Region > {
81
- return timeout ( 500 , timeoutMs , ( ) => this . find ( pathToNeedle , params ) ) ;
121
+ return timeout ( 500 , timeoutMs , ( ) => this . find ( templateImageFilename , params ) ) ;
82
122
}
83
123
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 ] ) ;
87
132
}
88
133
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
+ */
89
142
public async capture (
90
143
fileName : string ,
91
144
fileFormat : FileType = FileType . PNG ,
0 commit comments