Skip to content

Commit

Permalink
Merge pull request #6 from steveruizok/shortcut
Browse files Browse the repository at this point in the history
Adds global shortcut (Command + Shift + R)
  • Loading branch information
steveruizok authored Feb 18, 2021
2 parents a160879 + 51d59b4 commit 9848d48
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 29 deletions.
55 changes: 39 additions & 16 deletions app/background.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/background.js.map

Large diffs are not rendered by default.

52 changes: 40 additions & 12 deletions main/background.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { app, screen } from "electron"
import { app, globalShortcut } from "electron"
import serve from "electron-serve"
import { createWindow } from "./helpers"

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

app.on("browser-window-focus", () => {
if (mainWindow) {
mainWindow.webContents.send("projectMsg", { eventName: "FOCUSED_WINDOW" })
}
})

app.on("browser-window-blur", () => {
if (mainWindow) {
mainWindow.webContents.send("projectMsg", { eventName: "BLURRED_WINDOW" })
}
})
// Create window

const mainWindow = createWindow("main", {
fullscreenable: false,
Expand All @@ -42,6 +32,44 @@ if (isProd) {
mainWindow.setAlwaysOnTop(true, "floating")
mainWindow.setResizable(false)

// Window events

app.on("browser-window-focus", () => {
if (mainWindow) {
mainWindow.webContents.send("projectMsg", { eventName: "FOCUSED_WINDOW" })
}
})

app.on("browser-window-blur", () => {
if (mainWindow) {
mainWindow.webContents.send("projectMsg", { eventName: "BLURRED_WINDOW" })
}
})

// Setup global shortcut

app.whenReady().then(() => {
// Register a 'CommandOrControl+X' shortcut listener.
const ret = globalShortcut.register("CommandOrControl+Shift+R", () => {
app.focus({ steal: true })
mainWindow.webContents.focus()
mainWindow.webContents.send("projectMsg", {
eventName: "ACTIVATE_SHORTCUT",
})
})

if (!ret) {
console.warn("Shortcut registration failed.")
}
})

app.on("will-quit", () => {
// Unregister all shortcuts.
globalShortcut.unregisterAll()
})

// Kickoff

if (isProd) {
await mainWindow.loadURL("app://./home.html")
} else {
Expand Down
3 changes: 3 additions & 0 deletions renderer/lib/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ const state = createState({
inactive: {
onEnter: ["clearCurrentMark", "deactivate"],
on: {
ACTIVATE_SHORTCUT: {
to: ["active", "drawing"],
},
ACTIVATED: { to: "active" },
ENTERED_CONTROLS: { to: "selecting" },
},
Expand Down
1 change: 1 addition & 0 deletions renderer/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import styled from "styled-components"
import Head from "next/head"
import GlobalStyles from "../styles/globals"
import usePointer from "hooks/usePointer"
import useWindowEvents from "hooks/useWindowEvents"
Expand Down

0 comments on commit 9848d48

Please sign in to comment.