@@ -84,8 +84,8 @@ export class Screen {
84
84
params ?: LocationParameters ,
85
85
) : Promise < Region > {
86
86
const minMatch = ( params && params . confidence ) || this . config . confidence ;
87
- const searchRegion =
88
- ( params && params . searchRegion ) || await this . vision . screenSize ( ) ;
87
+ const screenSize = await this . vision . screenSize ( ) ;
88
+ const searchRegion = ( params && params . searchRegion ) || screenSize ;
89
89
const searchMultipleScales = ( params && params . searchMultipleScales )
90
90
91
91
const fullPathToNeedle = normalize ( join ( this . config . resourceDirectory , templateImageFilename ) ) ;
@@ -102,6 +102,18 @@ export class Screen {
102
102
103
103
return new Promise < Region > ( async ( resolve , reject ) => {
104
104
try {
105
+ if ( searchRegion . left < 0 || searchRegion . top < 0 || searchRegion . width < 0 || searchRegion . height < 0 ) {
106
+ throw new Error ( `Negative values in search region ${ searchRegion } ` )
107
+ }
108
+ if ( isNaN ( searchRegion . left ) || isNaN ( searchRegion . top ) || isNaN ( searchRegion . width ) || isNaN ( searchRegion . height ) ) {
109
+ throw new Error ( `NaN values in search region ${ searchRegion } ` )
110
+ }
111
+ if ( searchRegion . width < 2 || searchRegion . height < 2 ) {
112
+ throw new Error ( `Search region ${ searchRegion } is not large enough. Must be at least two pixels in both width and height.` )
113
+ }
114
+ if ( searchRegion . left + searchRegion . width > screenSize . width || searchRegion . top + searchRegion . height > screenSize . height ) {
115
+ throw new Error ( `Search region ${ searchRegion } extends beyond screen boundaries (${ screenSize . width } x${ screenSize . height } )` )
116
+ }
105
117
const matchResult = await this . vision . findOnScreenRegion ( matchRequest ) ;
106
118
if ( matchResult . confidence >= minMatch ) {
107
119
const possibleHooks = this . findHooks . get ( templateImageFilename ) || [ ] ;
0 commit comments