Skip to content

Commit 9848d48

Browse files
authored
Merge pull request #6 from steveruizok/shortcut
Adds global shortcut (Command + Shift + R)
2 parents a160879 + 51d59b4 commit 9848d48

File tree

5 files changed

+84
-29
lines changed

5 files changed

+84
-29
lines changed

app/background.js

Lines changed: 39 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/background.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

main/background.js

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { app, screen } from "electron"
1+
import { app, globalShortcut } from "electron"
22
import serve from "electron-serve"
33
import { createWindow } from "./helpers"
44

@@ -13,17 +13,7 @@ if (isProd) {
1313
;(async () => {
1414
await app.whenReady()
1515

16-
app.on("browser-window-focus", () => {
17-
if (mainWindow) {
18-
mainWindow.webContents.send("projectMsg", { eventName: "FOCUSED_WINDOW" })
19-
}
20-
})
21-
22-
app.on("browser-window-blur", () => {
23-
if (mainWindow) {
24-
mainWindow.webContents.send("projectMsg", { eventName: "BLURRED_WINDOW" })
25-
}
26-
})
16+
// Create window
2717

2818
const mainWindow = createWindow("main", {
2919
fullscreenable: false,
@@ -42,6 +32,44 @@ if (isProd) {
4232
mainWindow.setAlwaysOnTop(true, "floating")
4333
mainWindow.setResizable(false)
4434

35+
// Window events
36+
37+
app.on("browser-window-focus", () => {
38+
if (mainWindow) {
39+
mainWindow.webContents.send("projectMsg", { eventName: "FOCUSED_WINDOW" })
40+
}
41+
})
42+
43+
app.on("browser-window-blur", () => {
44+
if (mainWindow) {
45+
mainWindow.webContents.send("projectMsg", { eventName: "BLURRED_WINDOW" })
46+
}
47+
})
48+
49+
// Setup global shortcut
50+
51+
app.whenReady().then(() => {
52+
// Register a 'CommandOrControl+X' shortcut listener.
53+
const ret = globalShortcut.register("CommandOrControl+Shift+R", () => {
54+
app.focus({ steal: true })
55+
mainWindow.webContents.focus()
56+
mainWindow.webContents.send("projectMsg", {
57+
eventName: "ACTIVATE_SHORTCUT",
58+
})
59+
})
60+
61+
if (!ret) {
62+
console.warn("Shortcut registration failed.")
63+
}
64+
})
65+
66+
app.on("will-quit", () => {
67+
// Unregister all shortcuts.
68+
globalShortcut.unregisterAll()
69+
})
70+
71+
// Kickoff
72+
4573
if (isProd) {
4674
await mainWindow.loadURL("app://./home.html")
4775
} else {

renderer/lib/state.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ const state = createState({
9191
inactive: {
9292
onEnter: ["clearCurrentMark", "deactivate"],
9393
on: {
94+
ACTIVATE_SHORTCUT: {
95+
to: ["active", "drawing"],
96+
},
9497
ACTIVATED: { to: "active" },
9598
ENTERED_CONTROLS: { to: "selecting" },
9699
},

renderer/pages/_app.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import styled from "styled-components"
2+
import Head from "next/head"
23
import GlobalStyles from "../styles/globals"
34
import usePointer from "hooks/usePointer"
45
import useWindowEvents from "hooks/useWindowEvents"

0 commit comments

Comments
 (0)