Skip to content

Commit

Permalink
Add screen.getMousePosition() (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
bingenito authored Jul 19, 2018
1 parent 0eff39e commit c5b7b91
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/Default/default.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Container, WebContainerBase } from "../container";
import { ContainerWindow, PersistedWindowLayout, PersistedWindow, Rectangle } from "../window";
import { ScreenManager, Display } from "../screen";
import { ScreenManager, Display, Point } from "../screen";
import { NotificationOptions } from "../notification";
import { ObjectTransform, PropertyMap } from "../propertymapping";
import { Guid } from "../guid";
Expand Down Expand Up @@ -386,4 +386,10 @@ class DefaultDisplayManager implements ScreenManager {
this.getPrimaryDisplay().then(display => resolve([ display ]));
});
}

public getMousePosition(): Promise<Point> {
return new Promise<Point>((resolve, reject) => {
resolve({ x: (<any>this.window.event).screenX, y: (<any>this.window.event).screenY });
});
}
}
8 changes: 7 additions & 1 deletion src/Electron/electron.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as ContainerRegistry from "../registry";
import { ContainerWindow, PersistedWindowLayout, PersistedWindow, Rectangle } from "../window";
import { ScreenManager, Display } from "../screen";
import { ScreenManager, Display, Point } from "../screen";
import { Container, WebContainerBase } from "../container";
import { ObjectTransform, PropertyMap } from "../propertymapping";
import { NotificationOptions, ContainerNotification } from "../notification";
Expand Down Expand Up @@ -646,4 +646,10 @@ class ElectronDisplayManager implements ScreenManager {
resolve(this.electron.screen.getAllDisplays().map(this.createDisplay));
});
}

public getMousePosition(): Promise<Point> {
return new Promise<Point>((resolve, reject) => {
resolve(this.electron.screen.getCursorScreenPoint());
});
}
}
10 changes: 9 additions & 1 deletion src/OpenFin/openfin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as ContainerRegistry from "../registry";
import { ContainerWindow, PersistedWindowLayout, PersistedWindow, Rectangle } from "../window";
import { Container, WebContainerBase } from "../container";
import { ScreenManager, Display } from "../screen";
import { ScreenManager, Display, Point } from "../screen";
import { ObjectTransform, PropertyMap } from "../propertymapping";
import { NotificationOptions, ContainerNotification } from "../notification";
import { TrayIconDetails } from "../tray";
Expand Down Expand Up @@ -661,4 +661,12 @@ class OpenFinDisplayManager implements ScreenManager {
}, reject);
});
}

public getMousePosition(): Promise<Point> {
return new Promise<Point>((resolve, reject) => {
this.desktop.System.getMousePosition(position => {
resolve({ x: position.left, y: position.top });
}, reject);
});
}
}
12 changes: 12 additions & 0 deletions src/screen.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Rectangle } from "./window";

/** Represents a position in coordinates */
export class Point {
public readonly x: number;
public readonly y: number;
}

/** Retrieve information about screen size, display, etc. */
export interface ScreenManager {
/**
Expand All @@ -13,6 +19,12 @@ export interface ScreenManager {
* @returns {Promise<Display[]>} An array of all displays
*/
getAllDisplays(): Promise<Display[]>;

/**
* Retrieves the current absolute position of the mouse pointer.
* @returns {Point} The current coordinates
*/
getMousePosition(): Promise<Point>;
}

/** Details of a display connected to the system. */
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/Default/default.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ describe("DefaultDisplayManager", () => {
window = {};
Object.defineProperty(window, "devicePixelRatio", { value: 1 });
Object.defineProperty(window, "screen", { value: {availLeft: 2, availTop: 3, availWidth: 4, availHeight: 5, width: 6, height: 7} });
Object.defineProperty(window, "event", { value: { screenX: 1, screenY: 2 }});
container = new DefaultContainer(window);
});

Expand Down Expand Up @@ -506,4 +507,10 @@ describe("DefaultDisplayManager", () => {
expect(displays[0].id).toBe("Current");
}).then(done);
});

it ("getMousePosition", (done) => {
container.screen.getMousePosition().then(point => {
expect(point).toEqual({ x: 1, y: 2});
}).then(done);
});
});
9 changes: 8 additions & 1 deletion tests/unit/Electron/electron.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -830,8 +830,9 @@ describe("ElectronDisplayManager", () => {

beforeEach(() => {
electron = jasmine.createSpyObj("electron", ["ipc"]);
screen = jasmine.createSpyObj("screen", ["getPrimaryDisplay", "getAllDisplays"]);
screen = jasmine.createSpyObj("screen", ["getPrimaryDisplay", "getAllDisplays", "getCursorScreenPoint"]);
Object.defineProperty(electron, "screen", { value: screen });
screen.getCursorScreenPoint.and.returnValue({ x: 1, y: 2 });
screen.getPrimaryDisplay.and.returnValue(
{
id: "primary",
Expand Down Expand Up @@ -890,4 +891,10 @@ describe("ElectronDisplayManager", () => {
expect(displays[1].id).toBe("secondary");
}).then(done);
});

it ("getMousePosition", (done) => {
container.screen.getMousePosition().then(point => {
expect(point).toEqual({ x: 1, y: 2});
}).then(done);
});
});
9 changes: 8 additions & 1 deletion tests/unit/OpenFin/openfin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,10 +712,11 @@ describe("OpenFinDisplayManager", () => {
beforeEach(() => {
desktop = jasmine.createSpyObj("desktop", ["Application", "System", "InterApplicationBus"]);
app = jasmine.createSpyObj("application", ["getCurrent", "addEventListener"]);
system = jasmine.createSpyObj("system", ["getMonitorInfo"]);
system = jasmine.createSpyObj("system", ["getMonitorInfo", "getMousePosition"]);
Object.defineProperty(desktop, "Application", { value: app });
Object.defineProperty(desktop, "System", { value: system });
Object.defineProperty(desktop, "InterApplicationBus", { value: new MockInterApplicationBus() });
system.getMousePosition.and.callFake(callback => callback({ left: 1, top: 2 }));
system.getMonitorInfo.and.callFake(callback => callback(
{
primaryMonitor: {
Expand Down Expand Up @@ -772,4 +773,10 @@ describe("OpenFinDisplayManager", () => {
expect(displays[1].id).toBe("name2");
}).then(done);
});

it ("getMousePosition", (done) => {
container.screen.getMousePosition().then(point => {
expect(point).toEqual({ x: 1, y: 2});
}).then(done);
});
});

0 comments on commit c5b7b91

Please sign in to comment.