Skip to content

Commit

Permalink
Merge pull request #261 from laradumps/feature/shortcuts
Browse files Browse the repository at this point in the history
Change Global Shortcuts to LocalShortcuts
  • Loading branch information
luanfreitasdev authored Oct 25, 2023
2 parents 9c11cfe + 815ff1c commit 4953103
Show file tree
Hide file tree
Showing 11 changed files with 881 additions and 565 deletions.
62 changes: 29 additions & 33 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "laradumps",
"version": "2.3.3",
"version": "2.4.0",
"private": false,
"description": "LaraDumps a friendly app designed to boost your PHP coding and debugging experience. https://github.com/laradumps/app",
"author": "Luan Freitas <[email protected]>",
Expand All @@ -24,64 +24,60 @@
"@tailwindcss/forms": "^0.5.6",
"@tailwindcss/line-clamp": "^0.4.4",
"@types/highlight.js": "10.1.0",
"@types/node": "20.6.2",
"@typescript-eslint/eslint-plugin": "^6.7.0",
"@typescript-eslint/parser": "^6.7.0",
"@types/node": "20.8.9",
"@typescript-eslint/eslint-plugin": "^6.9.0",
"@typescript-eslint/parser": "^6.9.0",
"anser": "2.1.1",
"autoprefixer": "^10.4.15",
"axios": "^1.5.0",
"autoprefixer": "^10.4.16",
"axios": "^1.5.1",
"body-parser": "^1.20.2",
"core-js": "^3.32.2",
"core-js": "^3.33.1",
"cors": "^2.8.5",
"crypto-js": "^4.1.1",
"dotenv": "^16.3.1",
"edit-dotenv": "^1.0.4",
"electron-context-menu": "^3.6.1",
"electron-dl": "^3.5.0",
"electron-dl": "^3.5.1",
"electron-json-storage": "^4.6.0",
"electron-localshortcut": "^3.2.1",
"electron-updater": "6.1.4",
"electron-window-state": "^5.0.3",
"eslint-config-prettier": "9.0.0",
"express": "^4.18.2",
"highlight.js": "^11.8.0",
"highlight.js": "^11.9.0",
"hotkeys-js": "^3.12.0",
"humanize-duration": "^3.29.0",
"humanize-duration": "^3.30.0",
"moment": "^2.29.4",
"moment-timezone": "^0.5.43",
"pinia": "^2.1.6",
"pinia": "^2.1.7",
"postcss-import": "^15.1.0",
"sql-formatter": "^12.2.4",
"strip-ansi": "7.1.0",
"tree-kill": "^1.2.2",
"typescript": "*",
"vue": "3.3.4",
"vue-i18n": "9.4.1",
"vue-json-pretty": "^2.2.4",
"vuex": "4.1.0",
"which": "^4.0.0",
"ws": "^8.14.1"
"vue": "3.3.7",
"vue-i18n": "9.5.0",
"vue-json-pretty": "^2.2.4"
},
"devDependencies": {
"@types/crypto-js": "^4.1.2",
"@types/crypto-js": "^4.1.3",
"@types/electron": "^1.6.10",
"@types/electron-json-storage": "^4.5.0",
"@types/which": "^3.0.0",
"@vitejs/plugin-vue": "4.3.4",
"@vue/compiler-sfc": "3.3.4",
"electron": "26.2.4",
"@types/electron-json-storage": "^4.5.2",
"@types/which": "^3.0.1",
"@vitejs/plugin-vue": "4.4.0",
"@vue/compiler-sfc": "3.3.7",
"electron": "27.0.2",
"electron-builder": "24.6.4",
"esbuild": "0.19.3",
"eslint": "^8.49.0",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-n": "^16.1.0",
"eslint-plugin-prettier": "5.0.0",
"esbuild": "0.19.5",
"eslint": "^8.52.0",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-n": "^16.2.0",
"eslint-plugin-prettier": "5.0.1",
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-vue": "^9.17.0",
"eslint-plugin-vue": "^9.18.0",
"postcss": "8.4.31",
"prettier": "3.0.3",
"tailwindcss": "3.3.3",
"tailwindcss": "3.3.5",
"ts-standard": "^12.0.2",
"vite": "4.4.9"
"vite": "4.5.0"
},
"ts-standard": {
"project": "./tsconfig.json"
Expand Down
6 changes: 3 additions & 3 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import storage from "electron-json-storage";
import os from "os";
import { initSavedDumps } from "./saved-dumps";
import { initCoffeeWindow } from "./coffee";
import { configureGlobalShortcut, registerShortcuts } from "./global-shortcut";
import { configureLocalShortcut, registerShortcuts } from "./shortcut";
import { configureEnvironment } from "./environment";
import { autoUpdater, UpdateFileInfo, UpdateInfo } from "electron-updater";
import * as url from "url";
Expand Down Expand Up @@ -64,7 +64,7 @@ function createWindow(): BrowserWindow {
width: isDev ? 1080 : 670,
height: 660,
resizable: true,
alwaysOnTop: true,
alwaysOnTop: false,
center: true,
webPreferences: {
contextIsolation: false,
Expand Down Expand Up @@ -288,7 +288,7 @@ app.whenReady().then(async (): Promise<void> => {

await autoUpdater.checkForUpdates();

configureGlobalShortcut(mainWindow);
configureLocalShortcut(mainWindow);
configureEnvironment(mainWindow);

if (isMac) {
Expand Down
93 changes: 46 additions & 47 deletions src/main/global-shortcut.ts → src/main/shortcut.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import storage from "electron-json-storage";
import os from "os";
import { BrowserWindow, globalShortcut, ipcMain } from "electron";
import { BrowserWindow, ipcMain } from "electron";

// eslint-disable-next-line @typescript-eslint/no-var-requires
const electronLocalShortcut = require("electron-localshortcut");

interface ShortcutData {
alias: string;
keys: string;
}

/**
* Registers a global shortcut for clearing all data.
* Registers a local shortcut for clearing all data.
* @param mainWindow - The BrowserWindow instance.
*/
const registerGlobalShortCutForClearAll = (mainWindow: BrowserWindow): void => {
const registerLocalShortCutForClearAll = (mainWindow: BrowserWindow): void => {
storage.setDataPath(os.tmpdir());

// @ts-ignore
Expand All @@ -24,10 +27,10 @@ const registerGlobalShortCutForClearAll = (mainWindow: BrowserWindow): void => {

if (data?.keys) {
try {
globalShortcut.register(data.keys, (): void => {
electronLocalShortcut.register(data.keys, (): void => {
// eslint-disable-next-line no-console
console.log("executing clearAll!");
mainWindow.webContents.send("app:global-shortcut-execute::clearAll");
mainWindow.webContents.send("app:local-shortcut-execute::clearAll");
});
} catch (error) {
// eslint-disable-next-line no-console
Expand All @@ -38,10 +41,10 @@ const registerGlobalShortCutForClearAll = (mainWindow: BrowserWindow): void => {
};

/**
* Registers a global shortcut for dark mode.
* Registers a local shortcut for dark mode.
* @param mainWindow - The BrowserWindow instance.
*/
const registerGlobalShortCutForDarkMode = (mainWindow: BrowserWindow): void => {
const registerLocalShortCutForDarkMode = (mainWindow: BrowserWindow): void => {
storage.setDataPath(os.tmpdir());

// @ts-ignore
Expand All @@ -51,21 +54,21 @@ const registerGlobalShortCutForDarkMode = (mainWindow: BrowserWindow): void => {

if (data.keys.toString() !== "") {
// @ts-ignore
globalShortcut.register(data.keys, (): void => {
electronLocalShortcut.register(data.keys, (): void => {
// eslint-disable-next-line no-console
console.log("executing darkMode!");

mainWindow.webContents.send("app:global-shortcut-execute::darkMode");
mainWindow.webContents.send("app:local-shortcut-execute::darkMode");
});
}
});
};

/**
* Registers a global shortcut for always on top.
* Registers a local shortcut for always on top.
* @param mainWindow - The BrowserWindow instance.
*/
const registerGlobalShortCutForAlwaysOnTop = (mainWindow: BrowserWindow): void => {
const registerLocalShortCutForAlwaysOnTop = (mainWindow: BrowserWindow): void => {
storage.setDataPath(os.tmpdir());

// @ts-ignore
Expand All @@ -75,21 +78,21 @@ const registerGlobalShortCutForAlwaysOnTop = (mainWindow: BrowserWindow): void =

if (data.keys.toString() !== "") {
// @ts-ignore
globalShortcut.register(data.keys, (): void => {
electronLocalShortcut.register(data.keys, (): void => {
// eslint-disable-next-line no-console
console.log("executing alwaysOnTop!");

mainWindow.webContents.send("app:global-shortcut-execute::alwaysOnTop");
mainWindow.webContents.send("app:local-shortcut-execute::alwaysOnTop");
});
}
});
};

/**
* Registers a global shortcut for global search.
* Registers a local shortcut for global search.
* @param mainWindow - The BrowserWindow instance.
*/
const registerGlobalShortCutForSearch = (mainWindow: BrowserWindow): void => {
const registerLocalShortCutForSearch = (mainWindow: BrowserWindow): void => {
storage.setDataPath(os.tmpdir());

// @ts-ignore
Expand All @@ -99,21 +102,21 @@ const registerGlobalShortCutForSearch = (mainWindow: BrowserWindow): void => {

if (data.keys.toString() !== "") {
// @ts-ignore
globalShortcut.register(data.keys, (): void => {
electronLocalShortcut.register(data.keys, (): void => {
// eslint-disable-next-line no-console
console.log("executing globalSearch!");

mainWindow.webContents.send("app:global-shortcut-execute::globalSearch");
mainWindow.webContents.send("app:local-shortcut-execute::globalSearch");
});
}
});
};

/**
* Registers a global shortcut for toggle menu.
* Registers a local shortcut for toggle menu.
* @param mainWindow - The BrowserWindow instance.
*/
const registerGlobalShortCutForToggleMenu = (mainWindow: BrowserWindow): void => {
const registerLocalShortCutForToggleMenu = (mainWindow: BrowserWindow): void => {
storage.setDataPath(os.tmpdir());

// @ts-ignore
Expand All @@ -123,11 +126,11 @@ const registerGlobalShortCutForToggleMenu = (mainWindow: BrowserWindow): void =>

if (data.keys.toString() !== "") {
// @ts-ignore
globalShortcut.register(data.keys, () => {
electronLocalShortcut.register(data.keys, () => {
// eslint-disable-next-line no-console
console.log("executing toggleMenu!");

mainWindow.webContents.send("app:global-shortcut-execute::toggleMenu");
mainWindow.webContents.send("app:local-shortcut-execute::toggleMenu");
});
}
});
Expand All @@ -149,19 +152,19 @@ function registerShortcuts(mainWindow: BrowserWindow, alias = null): void {

switch (data.alias) {
case "clearAll":
registerGlobalShortCutForClearAll(mainWindow);
registerLocalShortCutForClearAll(mainWindow);
break;
case "darkMode":
registerGlobalShortCutForDarkMode(mainWindow);
registerLocalShortCutForDarkMode(mainWindow);
break;
case "alwaysOnTop":
registerGlobalShortCutForAlwaysOnTop(mainWindow);
registerLocalShortCutForAlwaysOnTop(mainWindow);
break;
case "globalSearch":
registerGlobalShortCutForSearch(mainWindow);
registerLocalShortCutForSearch(mainWindow);
break;
case "toggleMenu":
registerGlobalShortCutForToggleMenu(mainWindow);
registerLocalShortCutForToggleMenu(mainWindow);
break;
}
});
Expand All @@ -174,39 +177,31 @@ function registerShortcuts(mainWindow: BrowserWindow, alias = null): void {

switch (alias) {
case "clearAll":
registerGlobalShortCutForClearAll(mainWindow);
registerLocalShortCutForClearAll(mainWindow);
break;
case "darkMode":
registerGlobalShortCutForDarkMode(mainWindow);
registerLocalShortCutForDarkMode(mainWindow);
break;
case "alwaysOnTop":
registerGlobalShortCutForAlwaysOnTop(mainWindow);
registerLocalShortCutForAlwaysOnTop(mainWindow);
break;
}
}

/**
* Configures global shortcuts.
* Configures local shortcuts.
* @param mainWindow - The BrowserWindow instance.
*/
function configureGlobalShortcut(mainWindow: BrowserWindow): void {
function configureLocalShortcut(mainWindow: BrowserWindow): void {
/**
* Event listener for the "will-quit" event of the app.
* Unregisters all global shortcuts when the app is about to quit.
*/

storage.setDataPath(os.tmpdir());

/**
* Event listener for the "global-shortcut:reset" event of ipcMain.
* Unregisters all global shortcuts.
*/
ipcMain.on("global-shortcut:unregisterAll", (): void => {
globalShortcut.unregisterAll();
});

ipcMain.on("global-shortcut:registerAll", () => {
console.log("global-shortcut:registerAll");
ipcMain.on("local-shortcut:registerAll", () => {
console.log("local-shortcut:registerAll");

storage.keys((error: Error | null, keys: string[]): void => {
let shortcuts: Awaited<ShortcutData | null>[] = [];
Expand Down Expand Up @@ -253,12 +248,12 @@ function configureGlobalShortcut(mainWindow: BrowserWindow): void {
});

/**
* Event listener for the "global-shortcut:set" event of ipcMain.
* Event listener for the "local-shortcut:set" event of ipcMain.
* Sets the specified shortcut data in storage and registers the shortcuts.
* @param event - The Electron.IpcMainEvent instance.
* @param data - The shortcut data.
*/
ipcMain.on("global-shortcut:set", (event: Electron.IpcMainEvent, data): void => {
ipcMain.on("local-shortcut:set", (event: Electron.IpcMainEvent, data): void => {
storage.set(data.shortcut, data, (error): void => {
// eslint-disable-next-line no-console
if (error) console.log(error);
Expand All @@ -268,17 +263,20 @@ function configureGlobalShortcut(mainWindow: BrowserWindow): void {
});

/**
* Event listener for the "global-shortcut:get" event of ipcMain.
* Event listener for the "local-shortcut:get" event of ipcMain.
* Retrieves all stored shortcuts from storage and sends them to the mainWindow.
*/
ipcMain.on("global-shortcut:get", (): void => {
ipcMain.on("local-shortcut:get", (): void => {

storage.keys((error: Error | null, keys: string[]): void => {
if (error) {
// eslint-disable-next-line no-console
console.error(error);
return;
}

mainWindow.webContents.send("app:local-shortcut::count", keys.length);

let shortcuts: Awaited<ShortcutData | null>[] = [];

const getShortcutData = (key: string): Promise<ShortcutData | null> => {
Expand All @@ -303,7 +301,8 @@ function configureGlobalShortcut(mainWindow: BrowserWindow): void {
const promises: Promise<ShortcutData | null>[] = keys.map((key: string) => getShortcutData(key));
try {
shortcuts = await Promise.all(promises);
mainWindow.webContents.send("app:global-shortcut::list", shortcuts);

mainWindow.webContents.send("app:local-shortcut::list", shortcuts);
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
Expand All @@ -315,4 +314,4 @@ function configureGlobalShortcut(mainWindow: BrowserWindow): void {
});
}

export { registerShortcuts, configureGlobalShortcut };
export { registerShortcuts, configureLocalShortcut };
Loading

0 comments on commit 4953103

Please sign in to comment.