Skip to content

Commit 7a03b9d

Browse files
authored
Feature/373/macos doubleclick (#389)
* (#373) Extend mouse provider interface with generich click and doubleClick methods * (#373) Updated implementation to match updated interface * (#373) Provide public API for click and doubleClick
1 parent 7f924f1 commit 7a03b9d

File tree

3 files changed

+60
-15
lines changed

3 files changed

+60
-15
lines changed

lib/mouse.class.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,34 @@ export class MouseClass {
221221
}
222222
});
223223
}
224+
225+
/**
226+
* {@link click} clicks a mouse button
227+
* @param btn The {@link Button} to click
228+
*/
229+
public async click(btn: Button): Promise<MouseClass> {
230+
return new Promise<MouseClass>(async (resolve, reject) => {
231+
try {
232+
await this.providerRegistry.getMouse().click(btn);
233+
resolve(this);
234+
} catch (e) {
235+
reject(e);
236+
}
237+
});
238+
}
239+
240+
/**
241+
* {@link doubleClick} performs a double click on a mouse button
242+
* @param btn The {@link Button} to click
243+
*/
244+
public async doubleClick(btn: Button): Promise<MouseClass> {
245+
return new Promise<MouseClass>(async (resolve, reject) => {
246+
try {
247+
await this.providerRegistry.getMouse().doubleClick(btn);
248+
resolve(this);
249+
} catch (e) {
250+
reject(e);
251+
}
252+
});
253+
}
224254
}

lib/provider/mouse-provider.interface.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ export interface MouseProviderInterface {
2626
*/
2727
currentMousePosition(): Promise<Point>;
2828

29+
/**
30+
* click should allow to perform a single click via OS event
31+
*
32+
* @param btn The {@link Button} to click
33+
*/
34+
click(btn: Button): Promise<void>;
35+
36+
/**
37+
* doubleClick should allow to perform a double click via OS event
38+
*
39+
* @param btn The {@link Button} to click
40+
*/
41+
doubleClick(btn: Button): Promise<void>;
42+
2943
/**
3044
* leftClick should allow to perform a left click via OS event
3145
*/

lib/provider/native/libnut-mouse.class.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import libnut = require("@nut-tree/libnut");
2-
import { Button } from "../../button.enum";
3-
import { Point } from "../../point.class";
4-
import { MouseProviderInterface } from "../mouse-provider.interface";
2+
import {Button} from "../../button.enum";
3+
import {Point} from "../../point.class";
4+
import {MouseProviderInterface} from "../mouse-provider.interface";
55

66
export default class MouseAction implements MouseProviderInterface {
77
public static buttonLookup(btn: Button): any {
@@ -41,37 +41,38 @@ export default class MouseAction implements MouseProviderInterface {
4141
}));
4242
}
4343

44-
public leftClick(): Promise<void> {
44+
public click(btn: Button): Promise<void> {
4545
return new Promise<void>(((resolve, reject) => {
4646
try {
47-
libnut.mouseClick(MouseAction.buttonLookup(Button.LEFT));
47+
libnut.mouseClick(MouseAction.buttonLookup(btn));
4848
resolve();
4949
} catch (e) {
5050
reject(e);
5151
}
5252
}));
5353
}
5454

55-
public rightClick(): Promise<void> {
55+
public doubleClick(btn: Button): Promise<void> {
5656
return new Promise<void>(((resolve, reject) => {
5757
try {
58-
libnut.mouseClick(MouseAction.buttonLookup(Button.RIGHT));
58+
libnut.mouseClick(MouseAction.buttonLookup(btn), true);
5959
resolve();
6060
} catch (e) {
6161
reject(e);
6262
}
6363
}));
6464
}
6565

66+
public leftClick(): Promise<void> {
67+
return this.click(Button.LEFT);
68+
}
69+
70+
public rightClick(): Promise<void> {
71+
return this.click(Button.RIGHT);
72+
}
73+
6674
public middleClick(): Promise<void> {
67-
return new Promise<void>(((resolve, reject) => {
68-
try {
69-
libnut.mouseClick(MouseAction.buttonLookup(Button.MIDDLE));
70-
resolve();
71-
} catch (e) {
72-
reject(e);
73-
}
74-
}));
75+
return this.click(Button.MIDDLE);
7576
}
7677

7778
public pressButton(btn: Button): Promise<void> {

0 commit comments

Comments
 (0)