Skip to content

Commit

Permalink
Global shortcut (#126)
Browse files Browse the repository at this point in the history
Co-authored-by: swyx.io <shawnthe1@gmail.com>
  • Loading branch information
seanoliver and swyxio authored Aug 15, 2023
1 parent 4a33d79 commit e22a01a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
* `./src/main.js` using webpack. This gives us some performance wins.
*/
import path from 'path';
import { app, BrowserWindow, shell, screen, ipcMain } from 'electron';

import { app, BrowserWindow, shell, screen, ipcMain, globalShortcut } from 'electron';
import { autoUpdater } from 'electron-updater';
import log from 'electron-log';
import Store from 'electron-store';
Expand All @@ -29,21 +30,29 @@ class AppUpdater {
let mainWindow: BrowserWindow | null = null;

store.reset();

ipcMain.on('ipc-example', async (event, arg) => {
const msgTemplate = (pingPong: string) => `IPC test: ${pingPong}`;
console.log(msgTemplate(arg));
event.reply('ipc-example', msgTemplate('pong'));
});

ipcMain.on('electron-store-get', async (event, val, def) => {
event.returnValue = store.get(val, def);
});

ipcMain.on('electron-store-set', async (event, property, val) => {
store.set(property, val);
});

ipcMain.on('reload-browser', async (event, property, val) => {
mainWindow?.reload();
});

ipcMain.on('open-settings-window', () => {
createSettingsWindow();
});

if (process.env.NODE_ENV === 'production') {
const sourceMapSupport = require('source-map-support');
sourceMapSupport.install();
Expand Down Expand Up @@ -203,10 +212,32 @@ app
.whenReady()
.then(() => {
createWindow();

// Register global shortcut (CommandOrControl+Shift+G) to show the app
const ret = globalShortcut.register('CommandOrControl+Shift+G', () => {
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.focus();
mainWindow.show();
}
});

if (!ret) {
console.log('Global shortcut registration failed');
}

app.on('activate', () => {

// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) createWindow();
});
})
.catch(console.log);

app.on('will-quit', () => {
// Unregister the global shortcut
globalShortcut.unregisterAll();
});
5 changes: 5 additions & 0 deletions src/main/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import {
shell,
BrowserWindow,
MenuItemConstructorOptions,
ipcRenderer
} from 'electron';

interface DarwinMenuItemConstructorOptions extends MenuItemConstructorOptions {
selector?: string;
submenu?: DarwinMenuItemConstructorOptions[] | Menu;
}

function openSettingsWindow() {
ipcRenderer.send('open-settings-window');
}

export default class MenuBuilder {
mainWindow: BrowserWindow;

Expand Down
File renamed without changes.
8 changes: 8 additions & 0 deletions src/settings/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from 'react';
import ReactDOM from 'react-dom';
import SettingsComponent from './SettingsComponent';

ReactDOM.render(
<SettingsComponent />,
document.getElementById('root')
);

0 comments on commit e22a01a

Please sign in to comment.