Add types for GlobalHotkey

This commit is contained in:
licui
2019-01-22 16:13:49 -05:00
parent 48f3e9de55
commit 2ef9b10032
2 changed files with 103 additions and 0 deletions

View File

@@ -78,6 +78,7 @@ declare namespace fin {
main(f: () => any): void;
Application: OpenFinApplicationStatic;
ExternalApp: OpenFinExternalApplicationStatic;
GlobalHotkey: OpenFinGlobalHotkey;
InterApplicationBus: OpenFinInterApplicationBus;
Notification: OpenFinNotificationStatic;
System: OpenFinSystem;
@@ -312,6 +313,45 @@ declare namespace fin {
errorCallback?: (reason: string, error: ErrorInfo) => void): void;
}
/**
* GlobalHotkey
* The Global Hotkey allows the registration and unregistration of given hotkeys at the OS level, meaning a Window/Application will receive the events regardless of focused state.
*/
interface OpenFinGlobalHotkey {
/**
* Registers an event listener on the specified event.
*/
addEventListener(
type: OpenFinGlobalHotkeyEventType,
listener: (event: GlobalHotkeyEvent) => void,
callback?: () => void,
errorCallback?: (reason: string, error: ErrorInfo) => void): void;
/**
* Checks if a given hotkey has been registered
*/
isRegistered(hotkey: string, callback?: (registered: boolean) => void, errorCallback?: (reason: string, error: ErrorInfo) => void): void;
/**
* Registers a global hotkey with the operating system.
*/
register(hotkey: string, listener: () => void, callback?: () => void, errorCallback?: (reason: string, error: ErrorInfo) => void): void;
/**
* Removes a previously registered event listener from the specified event.
*/
removeEventListener(
type: OpenFinGlobalHotkeyEventType,
listener: (event: GlobalHotkeyEvent) => void,
callback?: () => void,
errorCallback?: (reason: string, error: ErrorInfo) => void): void;
/**
* Unregisters a global hotkey with the operating system.
*/
unregister(hotkey: string, callback?: () => void, errorCallback?: (reason: string, error: ErrorInfo) => void): void;
/**
* Unregisters all global hotkeys for the current application.
*/
unregisterAll(callback?: () => void, errorCallback?: (reason: string, error: ErrorInfo) => void): void;
}
/**
* InterApplicationBus
* A messaging bus that allows for pub/sub messaging between different applications.
@@ -982,6 +1022,21 @@ declare namespace fin {
uuid: string;
}
interface GlobalHotkeyEvent {
topic: string;
type: OpenFinGlobalHotkeyEventType;
/**
* The Identity that has just registered the hotkey
*/
identity: {
name: string;
uuid: string;
parentFrame: string;
entityType: string;
},
hotkey: string;
}
interface DesktopIconClickedEvent {
mouse: {
/**
@@ -1211,6 +1266,9 @@ declare namespace fin {
type OpenFinExternalApplicationEventType = "connected"
| "disconnected";
type OpenFinGlobalHotkeyEventType = "registered"
| "unregistered";
type OpenFinSystemEventType = "application-closed"
| "application-crashed"
| "application-created"

View File

@@ -164,6 +164,51 @@ function test_external_application() {
}, err => console.log(err));
}
function test_global_hot_key() {
// addEventListener
fin.desktop.GlobalHotkey.addEventListener('registered', (event) => {
console.log(` window ${event.identity.name} has registered ${event.hotkey}`, event);
}, () => {
console.log('The registration was successful');
}, (reason, err) => {
console.log(`Error Message: ${err.message} Error Stack: ${err.stack}`);
});
// isRegister
const hotkey = 'CommandOrControl+X';
fin.desktop.GlobalHotkey.isRegistered(hotkey, registered => {
console.log(`hotkey ${hotkey} is registered ? ${registered}`);
}, (reason, err) => {
console.log('Error unregistering the hotkey', err);
});
// register
fin.desktop.GlobalHotkey.register(hotkey, () => {
console.log(`${hotkey} pressed`);
}, () => {
console.log('Success');
}, (reason, err) => {
console.log('Error registering the hotkey', err);
});
// removeEventListener
const previousCallback = () => { };
fin.desktop.GlobalHotkey.removeEventListener('registered', previousCallback, () => {
console.log('The unregistration was successful');
}, (reason, err) => {
console.log(`Error Message: ${err.message} Error Stack: ${err.stack}`);
});
// unregister
fin.desktop.GlobalHotkey.unregister(hotkey, () => {
console.log('Success');
}, (reason, err) => {
console.log('Error unregistering the hotkey', err);
});
// unregisterAll
fin.desktop.GlobalHotkey.unregisterAll(() => {
console.log('Success');
}, (reason, err) => {
console.log('Error unregistering all hotkeys for this application', err);
});
}
function test_inter_application_bus() {
// addSubscribeListener
fin.desktop.InterApplicationBus.addSubscribeListener((uuid, topic, name) => {