Skip to content

Reject promise if searchRegion outside screenSize #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions lib/screen.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ export class Screen {
params?: LocationParameters,
): Promise<Region> {
const minMatch = (params && params.confidence) || this.config.confidence;
const searchRegion =
(params && params.searchRegion) || await this.vision.screenSize();
const screenSize = await this.vision.screenSize();
const searchRegion = (params && params.searchRegion) || screenSize;
const searchMultipleScales = (params && params.searchMultipleScales)

const fullPathToNeedle = normalize(join(this.config.resourceDirectory, templateImageFilename));
Expand All @@ -101,6 +101,18 @@ export class Screen {

return new Promise<Region>(async (resolve, reject) => {
try {
if ( searchRegion.left < 0 || searchRegion.top < 0 || searchRegion.width < 0 || searchRegion.height < 0 ) {
throw new Error(`Negative values in search region ${searchRegion}`)
}
if ( isNaN(searchRegion.left) || isNaN(searchRegion.top) || isNaN(searchRegion.width) || isNaN(searchRegion.height) ) {
throw new Error(`NaN values in search region ${searchRegion}`)
}
if ( searchRegion.width < 2 || searchRegion.height < 2 ) {
throw new Error(`Search region ${searchRegion} is not large enough. Must be at least two pixels in both width and height.`)
}
if ( searchRegion.left + searchRegion.width > screenSize.width || searchRegion.top + searchRegion.height > screenSize.height ) {
throw new Error(`Search region ${searchRegion} extends beyond screen boundaries (${screenSize.width}x${screenSize.height})`)
}
const matchResult = await this.vision.findOnScreenRegion(matchRequest);
if (matchResult.confidence >= minMatch) {
const possibleHooks = this.findHooks.get(templateImageFilename) || [];
Expand Down