@@ -99,18 +99,15 @@ export class ScreenClass {
99
99
template : Image | Promise < Image > ,
100
100
params ?: OptionalSearchParameters ,
101
101
) : Promise < Region > {
102
- const minMatch = ( params && params . confidence ) || this . config . confidence ;
103
- const screenSize = await this . providerRegistry . getScreen ( ) . screenSize ( ) ;
104
- const searchRegion = ( params && params . searchRegion ) || screenSize ;
105
- const searchMultipleScales = ( params && params . searchMultipleScales )
106
-
107
- const needle = await template ;
102
+ const {
103
+ minMatch,
104
+ screenSize,
105
+ searchRegion,
106
+ screenImage,
107
+ searchMultipleScales
108
+ } = await this . getFindParameters ( params ) ;
108
109
109
- if ( ! isImage ( needle ) ) {
110
- throw Error ( `find requires an Image, but received ${ JSON . stringify ( needle ) } ` )
111
- }
112
-
113
- const screenImage = await this . providerRegistry . getScreen ( ) . grabScreenRegion ( searchRegion ) ;
110
+ const needle = await ScreenClass . getNeedle ( template ) ;
114
111
115
112
const matchRequest = new MatchRequest (
116
113
screenImage ,
@@ -155,18 +152,15 @@ export class ScreenClass {
155
152
template : FirstArgumentType < typeof ScreenClass . prototype . find > ,
156
153
params ?: OptionalSearchParameters ,
157
154
) : Promise < Region [ ] > {
158
- const minMatch = ( params && params . confidence ) || this . config . confidence ;
159
- const screenSize = await this . providerRegistry . getScreen ( ) . screenSize ( ) ;
160
- const searchRegion = ( params && params . searchRegion ) || screenSize ;
161
- const searchMultipleScales = ( params && params . searchMultipleScales )
162
-
163
- const needle = await template ;
164
-
165
- if ( ! isImage ( needle ) ) {
166
- throw Error ( `findAll requires an Image, but received ${ JSON . stringify ( needle ) } ` )
167
- }
155
+ const {
156
+ minMatch,
157
+ screenSize,
158
+ searchRegion,
159
+ screenImage,
160
+ searchMultipleScales
161
+ } = await this . getFindParameters ( params ) ;
168
162
169
- const screenImage = await this . providerRegistry . getScreen ( ) . grabScreenRegion ( searchRegion ) ;
163
+ const needle = await ScreenClass . getNeedle ( template ) ;
170
164
171
165
const matchRequest = new MatchRequest (
172
166
screenImage ,
@@ -356,4 +350,29 @@ export class ScreenClass {
356
350
await this . providerRegistry . getImageWriter ( ) . store ( { image, path : outputPath } )
357
351
return outputPath ;
358
352
}
353
+
354
+ private async getFindParameters ( params ?: OptionalSearchParameters ) {
355
+ const minMatch = params ?. confidence ?? this . config . confidence ;
356
+ const screenSize = await this . providerRegistry . getScreen ( ) . screenSize ( ) ;
357
+ const searchRegion = params ?. searchRegion ?? screenSize ;
358
+ const screenImage = await this . providerRegistry . getScreen ( ) . grabScreenRegion ( searchRegion ) ;
359
+ const searchMultipleScales = params ?. searchMultipleScales ?? true ;
360
+
361
+ return ( {
362
+ minMatch,
363
+ screenSize,
364
+ searchRegion,
365
+ screenImage,
366
+ searchMultipleScales
367
+ } ) ;
368
+ }
369
+
370
+ private static async getNeedle ( template : FirstArgumentType < typeof ScreenClass . prototype . find > ) {
371
+ const needle = await template ;
372
+
373
+ if ( ! isImage ( needle ) ) {
374
+ throw Error ( `find requires an Image, but received ${ JSON . stringify ( needle ) } ` )
375
+ }
376
+ return needle ;
377
+ }
359
378
}
0 commit comments