Skip to content

Commit

Permalink
chore: refactor HotkeyManager to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
zunderscore committed Dec 28, 2024
1 parent 2bc5e84 commit 5842844
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 168 deletions.
4 changes: 2 additions & 2 deletions src/backend/app-management/electron/events/when-ready.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ exports.whenReady = async () => {
setupCommonListeners();

windowManagement.updateSplashScreenStatus("Loading hotkeys...");
const hotkeyManager = require("../../../hotkeys/hotkey-manager");
hotkeyManager.refreshHotkeyCache();
const { HotkeyManager } = require("../../../hotkeys/hotkey-manager");
HotkeyManager.loadHotkeys();

windowManagement.updateSplashScreenStatus("Starting currency timer...");
const currencyManager = require("../../../currency/currency-manager");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ exports.windowsAllClosed = async () => {
await customScriptRunner.stopAllScripts();

// Unregister all shortcuts.
const hotkeyManager = require("../../../hotkeys/hotkey-manager");
hotkeyManager.unregisterAllHotkeys();
const { HotkeyManager } = require("../../../hotkeys/hotkey-manager");
HotkeyManager.unregisterAllHotkeys();

// Stop the chat moderation service
const chatModerationManager = require("../../../chat/moderation/chat-moderation-manager");
Expand Down
108 changes: 0 additions & 108 deletions src/backend/hotkeys/hotkey-manager.js

This file was deleted.

155 changes: 155 additions & 0 deletions src/backend/hotkeys/hotkey-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { globalShortcut } from "electron";
import { JsonDB } from "node-json-db";
import { v4 as uuid } from "uuid";

import logger from "../logwrapper";
import frontendCommunicator from "../common/frontend-communicator.js";
import profileManager from "../../backend/common/profile-manager.js";
import accountAccess from "../common/account-access";
import { TriggerType } from "../common/EffectType";
import effectRunner from "../common/effect-runner";
import { EffectList } from "../../types/effects";

interface FirebotHotkey {
id: string;
code: Electron.Accelerator;
active: boolean;
effects: EffectList;
}

class HotkeyManager {
hotkeys: FirebotHotkey[] = [];

constructor() {
frontendCommunicator.on("hotkeys:get-hotkeys", () => {
return this.hotkeys;
});

frontendCommunicator.on("hotkeys:add-hotkey", (hotkey: FirebotHotkey) => {
this.addHotkey(hotkey);
});

frontendCommunicator.on("hotkeys:update-hotkey", (hotkey: FirebotHotkey) => {
this.updateHotkey(hotkey);
});

frontendCommunicator.on("hotkeys:delete-hotkey", (id: string) => {
this.deleteHotkey(id);
});
}

private getHotkeyDb(): JsonDB {
return profileManager.getJsonDbInProfile("/hotkeys");
}

loadHotkeys() {
try {
const hotkeyData = this.getHotkeyDb().getData("/");

if (hotkeyData?.length) {
this.hotkeys = hotkeyData || [];
}

this.unregisterAllHotkeys();
this.registerAllHotkeys();

frontendCommunicator.send("hotkeys:hotkeys-updated", this.hotkeys);
logger.info("Loaded hotkeys");
} catch (err) {
logger.error(err);
}
}

unregisterAllHotkeys() {
globalShortcut.unregisterAll();
}

addHotkey(hotkey: FirebotHotkey) {
hotkey.id = uuid();

this.hotkeys.push(hotkey);
this.registerHotkey(hotkey.code);

this.saveHotkeys();
}

updateHotkey(hotkey: FirebotHotkey) {
const index = this.hotkeys.findIndex(h => h.id === hotkey.id);

if (index > -1) {
this.hotkeys[index] = hotkey;

this.saveHotkeys();
}
}

deleteHotkey(id: string) {
const hotkey = this.hotkeys.find(h => h.id === id);

this.hotkeys.splice(this.hotkeys.indexOf(hotkey), 1);

globalShortcut.unregister(hotkey.code);

this.saveHotkeys();
}

private saveHotkeys() {
try {
this.getHotkeyDb().push("/", this.hotkeys);
} catch (error) {
logger.error("Error saving hotkeys", error);
}

frontendCommunicator.send("hotkeys:hotkeys-updated", this.hotkeys);
}

private registerAllHotkeys() {
if (this.hotkeys == null) {
return;
}

this.hotkeys.filter(h => h.active).forEach((k) => {
this.registerHotkey(k.code);
});
}

private registerHotkey(accelerator: Electron.Accelerator) {
try {
const success = globalShortcut.register(accelerator, () => {
this.runHotkey(accelerator);
});

if (!success) {
logger.warn(`Unable to register hotkey ${accelerator} with OS. This typically means it is already taken by another application.`);
}
} catch (error) {
logger.error(`Error while registering hotkey ${accelerator} with OS`, error);
}
}

private runHotkey(code: Electron.Accelerator) {
const hotkey = this.hotkeys.find(k => k.code === code);

const effects = hotkey.effects;

if (effects == null) {
return;
}

const processEffectsRequest = {
trigger: {
type: TriggerType.HOTKEY,
metadata: {
username: accountAccess.getAccounts().streamer.username,
hotkey: hotkey
}
},
effects: effects
};
effectRunner.processEffects(processEffectsRequest);
}
}

const hotkeyManager = new HotkeyManager();

export { hotkeyManager as HotkeyManager };
9 changes: 5 additions & 4 deletions src/gui/app/controllers/hotkeys.controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"use strict";

(function() {
//This handles the Hotkeys tab

Expand All @@ -24,13 +25,13 @@
resolveObj: {
hotkey: () => hotkey
},
closeCallback: resp => {
closeCallback: (resp) => {
const action = resp.action,
hotkey = resp.hotkey;

switch (action) {
case "add":
hotkeyService.saveHotkey(hotkey);
hotkeyService.addHotkey(hotkey);
break;
case "update":
hotkeyService.updateHotkey(hotkey);
Expand All @@ -43,7 +44,7 @@
confirmLabel: "Delete",
confirmBtnType: "btn-danger"
})
.then(confirmed => {
.then((confirmed) => {
if (confirmed) {
hotkeyService.deleteHotkey(hotkey);
}
Expand All @@ -54,4 +55,4 @@
});
};
});
}());
}());
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
modalInstance: "<"
},
controller: function(
$scope,
hotkeyService,
utilityService,
ngToast
) {
const $ctrl = this;
Expand Down Expand Up @@ -95,4 +93,4 @@
};
}
});
}());
}());
Loading

0 comments on commit 5842844

Please sign in to comment.