Skip to content

Commit 89dcebf

Browse files
authored
refactor: adding enums for click and updating
1 parent c37cac0 commit 89dcebf

File tree

2 files changed

+22
-24
lines changed

2 files changed

+22
-24
lines changed

lib/commands/extension.ts

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
convertStringToCondition,
2525
pwsh
2626
} from '../powershell';
27-
import { Enum, Key } from '../enums';
27+
import { ClickType, Enum, Key } from '../enums';
2828

2929
const PLATFORM_COMMAND_PREFIX = 'windows:';
3030

@@ -359,7 +359,7 @@ export async function executeClick(this: NovaWindowsDriver, clickArgs: {
359359
elementId?: string,
360360
x?: number,
361361
y?: number,
362-
button?: 'left' | 'middle' | 'right' | 'back' | 'forward', // TODO: add types
362+
button?: ClickType,
363363
modifierKeys?: ('shift' | 'ctrl' | 'alt' | 'win') | ('shift' | 'ctrl' | 'alt' | 'win')[], // TODO: add types
364364
durationMs?: number,
365365
times?: number,
@@ -368,7 +368,7 @@ export async function executeClick(this: NovaWindowsDriver, clickArgs: {
368368
const {
369369
elementId,
370370
x, y,
371-
button = 'left',
371+
button = ClickType.LEFT,
372372
modifierKeys = [],
373373
durationMs = 0,
374374
times = 1,
@@ -402,26 +402,14 @@ export async function executeClick(this: NovaWindowsDriver, clickArgs: {
402402
pos = [x!, y!];
403403
}
404404

405-
let mouseButton: number;
406-
switch (button.toLowerCase()) {
407-
case 'left':
408-
mouseButton = 0;
409-
break;
410-
case 'middle':
411-
mouseButton = 1;
412-
break;
413-
case 'right':
414-
mouseButton = 2;
415-
break;
416-
case 'back':
417-
mouseButton = 3;
418-
break;
419-
case 'forward':
420-
mouseButton = 4;
421-
break;
422-
default:
423-
throw new errors.InvalidArgumentError('Property button should be one of left, middle, right, back or forward.');
424-
}
405+
const clickTypeToButtonMapping: { [key in ClickType]: number} = {
406+
[ClickType.LEFT]: 0,
407+
[ClickType.MIDDLE]: 1,
408+
[ClickType.RIGHT]: 2,
409+
[ClickType.BACK]: 3,
410+
[ClickType.FORWARD]: 4
411+
};
412+
const mouseButton: number = clickTypeToButtonMapping[button];
425413

426414
await mouseMoveAbsolute(pos[0], pos[1], 0);
427415
for (let i = 0; i < times; i++) {

lib/enums.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,14 @@ export const Key = Object.freeze({
8585
R_DELETE: '\uE05D',
8686
} as const);
8787

88-
export type Key = Enum<typeof Key>;
88+
export type Key = Enum<typeof Key>;
89+
90+
export const ClickType = Object.freeze({
91+
LEFT: 'left',
92+
MIDDLE: 'middle',
93+
RIGHT: 'right',
94+
BACK: 'back',
95+
FORWARD: 'forward'
96+
});
97+
98+
export type ClickType = Enum<typeof ClickType>;

0 commit comments

Comments
 (0)