From c5264714c8a82bbb9270eaa8c962a21aadb56ff4 Mon Sep 17 00:00:00 2001 From: bingenito Date: Wed, 18 Jul 2018 15:28:35 -0400 Subject: [PATCH] Add screen.getMousePosition() --- src/Default/default.ts | 8 +++++++- src/Electron/electron.ts | 8 +++++++- src/OpenFin/openfin.ts | 10 +++++++++- src/screen.ts | 12 ++++++++++++ tests/unit/Default/default.spec.ts | 7 +++++++ tests/unit/Electron/electron.spec.ts | 9 ++++++++- tests/unit/OpenFin/openfin.spec.ts | 9 ++++++++- 7 files changed, 58 insertions(+), 5 deletions(-) diff --git a/src/Default/default.ts b/src/Default/default.ts index 60db4c00..303df8cf 100644 --- a/src/Default/default.ts +++ b/src/Default/default.ts @@ -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"; @@ -386,4 +386,10 @@ class DefaultDisplayManager implements ScreenManager { this.getPrimaryDisplay().then(display => resolve([ display ])); }); } + + public getMousePosition(): Promise { + return new Promise((resolve, reject) => { + resolve({ x: (this.window.event).screenX, y: (this.window.event).screenY }); + }); + } } \ No newline at end of file diff --git a/src/Electron/electron.ts b/src/Electron/electron.ts index f855583d..0c9813bc 100644 --- a/src/Electron/electron.ts +++ b/src/Electron/electron.ts @@ -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"; @@ -646,4 +646,10 @@ class ElectronDisplayManager implements ScreenManager { resolve(this.electron.screen.getAllDisplays().map(this.createDisplay)); }); } + + public getMousePosition(): Promise { + return new Promise((resolve, reject) => { + resolve(this.electron.screen.getCursorScreenPoint()); + }); + } } diff --git a/src/OpenFin/openfin.ts b/src/OpenFin/openfin.ts index 64f49c77..4a176a51 100644 --- a/src/OpenFin/openfin.ts +++ b/src/OpenFin/openfin.ts @@ -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"; @@ -661,4 +661,12 @@ class OpenFinDisplayManager implements ScreenManager { }, reject); }); } + + public getMousePosition(): Promise { + return new Promise((resolve, reject) => { + this.desktop.System.getMousePosition(position => { + resolve({ x: position.left, y: position.top }); + }, reject); + }); + } } diff --git a/src/screen.ts b/src/screen.ts index d65349c0..3e4dc6f3 100644 --- a/src/screen.ts +++ b/src/screen.ts @@ -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 { /** @@ -13,6 +19,12 @@ export interface ScreenManager { * @returns {Promise} An array of all displays */ getAllDisplays(): Promise; + + /** + * Retrieves the current absolute position of the mouse pointer. + * @returns {Point} The current coordinates + */ + getMousePosition(): Promise; } /** Details of a display connected to the system. */ diff --git a/tests/unit/Default/default.spec.ts b/tests/unit/Default/default.spec.ts index 6f4d2099..0453790c 100644 --- a/tests/unit/Default/default.spec.ts +++ b/tests/unit/Default/default.spec.ts @@ -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); }); @@ -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); + }); }); \ No newline at end of file diff --git a/tests/unit/Electron/electron.spec.ts b/tests/unit/Electron/electron.spec.ts index d7ba4fd8..d6b704ed 100644 --- a/tests/unit/Electron/electron.spec.ts +++ b/tests/unit/Electron/electron.spec.ts @@ -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", @@ -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); + }); }); \ No newline at end of file diff --git a/tests/unit/OpenFin/openfin.spec.ts b/tests/unit/OpenFin/openfin.spec.ts index d5107412..96b07899 100644 --- a/tests/unit/OpenFin/openfin.spec.ts +++ b/tests/unit/OpenFin/openfin.spec.ts @@ -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: { @@ -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); + }); }); \ No newline at end of file