Skip to content

Commit 2d01246

Browse files
committed
fix: allow elementId with optional x/y offsets for click/hover
1 parent 7a05300 commit 2d01246

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

lib/commands/extension.ts

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -378,12 +378,8 @@ export async function executeClick(this: NovaWindowsDriver, clickArgs: {
378378
interClickDelayMs = 100,
379379
} = clickArgs;
380380

381-
if (!!elementId && ((x !== null && x !== undefined) || (y !== null && y !== undefined))) {
382-
throw new errors.InvalidArgumentError('Either elementId or x and y must be provided.');
383-
}
384-
385-
if ((x !== null && x !== undefined) !== (y !== null && y !== undefined)) {
386-
throw new errors.InvalidArgumentError('Both x and y must be provided.');
381+
if ((x != null) !== (y != null)) {
382+
throw new errors.InvalidArgumentError('Both x and y must be provided if either is set.');
387383
}
388384

389385
let pos: [number, number];
@@ -399,7 +395,10 @@ export async function executeClick(this: NovaWindowsDriver, clickArgs: {
399395

400396
const rectJson = await this.sendPowerShellCommand(new FoundAutomationElement(elementId).buildGetElementRectCommand());
401397
const rect = JSON.parse(rectJson.replaceAll(/(?:infinity)/gi, 0x7FFFFFFF.toString())) as Rect;
402-
pos = [rect.x + (rect.width / 2), rect.y + (rect.height / 2)];
398+
pos = [
399+
rect.x + (x ?? Math.trunc(rect.width / 2)),
400+
rect.y + (y ?? Math.trunc(rect.height / 2)),
401+
];
403402
} else {
404403
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
405404
pos = [x!, y!];
@@ -466,7 +465,7 @@ export async function executeHover(this: NovaWindowsDriver, hoverArgs: {
466465
endElementId?: string,
467466
endX?: number,
468467
endY?: number,
469-
modifierKeys?: ('shift' | 'ctrl' | 'alt' | 'win') | ('shift' | 'ctrl' | 'alt' | 'win')[], // TODO: add types
468+
modifierKeys?: ('shift' | 'ctrl' | 'alt' | 'win') | ('shift' | 'ctrl' | 'alt' | 'win')[],
470469
durationMs?: number,
471470
}) {
472471
const {
@@ -478,20 +477,12 @@ export async function executeHover(this: NovaWindowsDriver, hoverArgs: {
478477
durationMs = 500,
479478
} = hoverArgs;
480479

481-
if (!!startElementId && ((startX !== null && startX !== undefined) || (startY !== null && startY !== undefined))) {
482-
throw new errors.InvalidArgumentError('Either startElementId or startX and startY must be provided.');
483-
}
484-
485-
if (!!endElementId && ((endX !== null && endX !== undefined) || (endY !== null && endY !== undefined))) {
486-
throw new errors.InvalidArgumentError('Either endElementId or endX and endY must be provided.');
480+
if ((startX != null) !== (startY != null)) {
481+
throw new errors.InvalidArgumentError('Both startX and startY must be provided if either is set.');
487482
}
488483

489-
if ((startX !== null && startX !== undefined) !== (startY !== null && startY !== undefined)) {
490-
throw new errors.InvalidArgumentError('Both startX and startY must be provided.');
491-
}
492-
493-
if ((endX !== null && endX !== undefined) !== (endY !== null && endY !== undefined)) {
494-
throw new errors.InvalidArgumentError('Both endX and endY must be provided.');
484+
if ((endX != null) !== (endY != null)) {
485+
throw new errors.InvalidArgumentError('Both endX and endY must be provided if either is set.');
495486
}
496487

497488
const processesModifierKeys = Array.isArray(modifierKeys) ? modifierKeys : [modifierKeys];
@@ -508,13 +499,15 @@ export async function executeHover(this: NovaWindowsDriver, hoverArgs: {
508499

509500
const rectJson = await this.sendPowerShellCommand(new FoundAutomationElement(startElementId).buildGetElementRectCommand());
510501
const rect = JSON.parse(rectJson.replaceAll(/(?:infinity)/gi, 0x7FFFFFFF.toString())) as Rect;
511-
startPos = [rect.x + (rect.width / 2), rect.y + (rect.height / 2)];
502+
startPos = [
503+
rect.x + (startX ?? rect.width / 2),
504+
rect.y + (startY ?? rect.height / 2)
505+
];
512506
} else {
513507
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
514508
startPos = [startX!, startY!];
515509
}
516510

517-
518511
let endPos: [number, number];
519512
if (endElementId) {
520513
if (await this.sendPowerShellCommand(/* ps1 */ `$null -eq ${new FoundAutomationElement(endElementId).toString()}`)) {
@@ -528,7 +521,10 @@ export async function executeHover(this: NovaWindowsDriver, hoverArgs: {
528521

529522
const rectJson = await this.sendPowerShellCommand(new FoundAutomationElement(endElementId).buildGetElementRectCommand());
530523
const rect = JSON.parse(rectJson.replaceAll(/(?:infinity)/gi, 0x7FFFFFFF.toString())) as Rect;
531-
endPos = [rect.x + (rect.width / 2), rect.y + (rect.height / 2)];
524+
endPos = [
525+
rect.x + (endX ?? rect.width / 2),
526+
rect.y + (endY ?? rect.height / 2)
527+
];
532528
} else {
533529
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
534530
endPos = [endX!, endY!];

0 commit comments

Comments
 (0)