From 21f5c839d1b59a39e4d376b01a5baa9f92701367 Mon Sep 17 00:00:00 2001 From: kPherox Date: Mon, 8 Apr 2019 00:55:27 +0900 Subject: [PATCH] Add utility function for create window Add F5 key event for reload --- package-lock.json | 47 ++++++++++++++++++++++++++ package.json | 1 + src/background.js | 84 ++++++++++++++++++++++++++++++++--------------- 3 files changed, 105 insertions(+), 27 deletions(-) diff --git a/package-lock.json b/package-lock.json index 023c2321..d9558ae2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4679,12 +4679,47 @@ "jimp": "^0.2.27" } }, + "electron-is-accelerator": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/electron-is-accelerator/-/electron-is-accelerator-0.1.2.tgz", + "integrity": "sha1-UJ5RDCala1Xhf4Y6SwThEYRqsns=", + "dev": true + }, "electron-is-dev": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/electron-is-dev/-/electron-is-dev-1.0.1.tgz", "integrity": "sha512-iwM3EotA9HTXqMGpQRkR/kT8OZqBbdfHTnlwcxsjSLYqY8svvsq0MuujsWCn3/vtgRmDv/PC/gKUUpoZvi5C1w==", "dev": true }, + "electron-localshortcut": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/electron-localshortcut/-/electron-localshortcut-3.1.0.tgz", + "integrity": "sha512-MgL/j5jdjW7iA0R6cI7S045B0GlKXWM1FjjujVPjlrmyXRa6yH0bGSaIAfxXAF9tpJm3pLEiQzerYHkRh9JG/A==", + "dev": true, + "requires": { + "debug": "^2.6.8", + "electron-is-accelerator": "^0.1.0", + "keyboardevent-from-electron-accelerator": "^1.1.0", + "keyboardevents-areequal": "^0.2.1" + }, + "dependencies": { + "debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "dev": true, + "requires": { + "ms": "2.0.0" + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=", + "dev": true + } + } + }, "electron-osx-sign": { "version": "0.4.11", "resolved": "https://registry.npmjs.org/electron-osx-sign/-/electron-osx-sign-0.4.11.tgz", @@ -8026,6 +8061,18 @@ "integrity": "sha1-edk9LTM2PW/dKXCzNdkUGtWR15s=", "dev": true }, + "keyboardevent-from-electron-accelerator": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/keyboardevent-from-electron-accelerator/-/keyboardevent-from-electron-accelerator-1.1.0.tgz", + "integrity": "sha512-VDC4vKWGrR3VgIKCE4CsXnvObGgP8C2idnTKEMUkuEuvDGE1GEBX9FtNdJzrD00iQlhI3xFxRaeItsUmlERVng==", + "dev": true + }, + "keyboardevents-areequal": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/keyboardevents-areequal/-/keyboardevents-areequal-0.2.2.tgz", + "integrity": "sha512-Nv+Kr33T0mEjxR500q+I6IWisOQ0lK1GGOncV0kWE6n4KFmpcu7RUX5/2B0EUtX51Cb0HjZ9VJsSY3u4cBa0kw==", + "dev": true + }, "killable": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/killable/-/killable-1.0.1.tgz", diff --git a/package.json b/package.json index c8017add..1187f843 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "electron": "^4.0.0", "electron-context-menu": "^0.11.0", "electron-icon-builder": "^1.0.0", + "electron-localshortcut": "^3.1.0", "eslint": "^5.8.0", "eslint-plugin-vue": "^5.0.0", "vue-cli-plugin-electron-builder": "^1.2.0", diff --git a/src/background.js b/src/background.js index a23a24a8..09e935a6 100644 --- a/src/background.js +++ b/src/background.js @@ -13,43 +13,73 @@ import { createProtocol, installVueDevtools } from 'vue-cli-plugin-electron-builder/lib' +import localShortcut from 'electron-localshortcut' + import { bugs } from '../package.json' import thedeskInfo from '../info.json' const isDevelopment = process.env.NODE_ENV !== 'production' -// Keep a global reference of the window object, if you don't, the window will -// be closed automatically when the JavaScript object is garbage collected. -let win +// イベントリスナや`createWindow`関数が参照するグローバル変数 +let createdAppProtocol = false + , windows = {} ContextMenu() -// Standard scheme must be registered before the app is ready +// Standard schemeはreadyの前に登録する必要がある protocol.registerStandardSchemes(['app'], { secure: true }) -function createWindow () { - // Create the browser window. - win = new BrowserWindow({ - width: 800, - height: 600, - icon: path.join(__static, 'icon.png') - }) - - win.setMenuBarVisibility(true) - - if (process.env.WEBPACK_DEV_SERVER_URL) { - // Load the url of the dev server if in development mode - win.loadURL(process.env.WEBPACK_DEV_SERVER_URL) - if (!process.env.IS_TEST) win.webContents.openDevTools() - } else { - createProtocol('app') - // Load the index.html when not in development - win.loadURL('app://./index.html') +// Windowを作る +async function createWindow(windowName, loadPath, windowOptions, singleton, lastAction, openDevTools) { + if (typeof windows[windowName] !== 'undefined') { + windows[windowName].show() + windows[windowName].focus() + return } - win.on('closed', () => { - win = null + // 引数のバリデーション + if (typeof windowOptions !== 'object') windowOptions = {} + if (typeof lastAction !== 'function') lastAction = () => {} + + let win = new BrowserWindow(windowOptions) + + // ページの表示が完了するまで非表示にする + win.hide() + win.webContents.on('did-finish-load', () => { + windows[windowName].show() }) + + win.on('closed', () => { + windows[windowName] = undefined + }) + + if (process.env.WEBPACK_DEV_SERVER_URL) { + // `electron:serve`で起動した時の読み込み + win.loadURL(process.env.WEBPACK_DEV_SERVER_URL + loadPath) + if (openDevTools) win.webContents.openDevTools() + } else { + // ビルドしたアプリでの読み込み + if (!createdAppProtocol) { + createProtocol('app') + createdAppProtocol = true + } + win.loadURL(`app://./${loadPath}`) + } + + localShortcut.register(win, 'F5', () => windows[windowName].reload()) + lastAction(win) + + windows[windowName] = win +} + +function openMainWindow() { + const winOpts = { + icon: path.join(__static, 'icon.png'), + width: 800, + height: 600, + autoHideMenuBar: true, + } + createWindow('main', 'index.html', winOpts, true, null, !process.env.IS_TEST) } // Quit when all windows are closed. @@ -64,8 +94,8 @@ app.on('window-all-closed', () => { 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 (win === null) { - createWindow() + if (typeof windows.main === 'undefined') { + openMainWindow() } }) @@ -81,7 +111,7 @@ app.on('ready', async () => { console.error('Vue Devtools failed to install:', e.toString()) } } - createWindow() + openMainWindow() }) // Exit cleanly on request from parent process in development mode.