This commit is contained in:
gaoshuaixing
2021-06-24 16:11:01 +08:00
parent fc1e83adf1
commit 4cc652f823
7 changed files with 106 additions and 15 deletions

View File

@@ -3,5 +3,6 @@
module.exports = { module.exports = {
EGG_CONFIG: 'egg_config', EGG_CONFIG: 'egg_config',
ELECTRON_IPC: 'electron_ipc', ELECTRON_IPC: 'electron_ipc',
PREFERENCES: 'preferences',
TEST_DATA: 'test_data' TEST_DATA: 'test_data'
}; };

View File

@@ -4,7 +4,8 @@ const path = require('path');
const { const {
app, app,
webContents, webContents,
shell shell,
globalShortcut
} = require('electron'); } = require('electron');
exports.getPath = function () { exports.getPath = function () {
@@ -27,6 +28,19 @@ exports.executeJS = function (str) {
return webContents.fromId(1).executeJavaScript(jscode); return webContents.fromId(1).executeJavaScript(jscode);
} }
exports.shortcut = function (shortcutStr = '') {
// if (!dir) {
// return false;
// }
// globalShortcut.register("CommandOrControl+Shift+S", () => {
// MAIN_WINDOW.show()
// })
// globalShortcut.register("CommandOrControl+Shift+H", () => {
// MAIN_WINDOW.hide()
// })
return true;
}
function getElectronPath(filepath) { function getElectronPath(filepath) {
//filepath = path.resolve(filepath); //filepath = path.resolve(filepath);
filepath = filepath.replace("resources", ""); filepath = filepath.replace("resources", "");

49
electron/lib/shortcut.js Normal file
View File

@@ -0,0 +1,49 @@
'use strict';
const { globalShortcut } = require('electron');
const storage = require('./storage');
const shortcutList = {
'CommandOrControl+Shift+s': 'showWindow',
'CommandOrControl+Shift+h': 'hideWindow',
}
exports.setup = function () {
// default
//const preferences = storage.getPreferences();
// const shortcuts = preferences.hasOwnProperty('shortcuts') ? preferences.shortcuts : {};
// storage.setShortcuts(shortcuts);
// for (let key in shortcuts) {
// const fn = this.shortcuts[key]();
// console.log(fn.toString());
// this.register(key, fn);
// }
}
exports.register = function (cmd, fn, force = true) {
const isRegistered = this.isRegistered(cmd);
console.log('[shortcut] [register] cmd:', [cmd, isRegistered]);
if (isRegistered && !force) {
return;
}
globalShortcut.register(cmd, fn)
}
exports.isRegistered = function (cmd) {
return globalShortcut.isRegistered(cmd)
}
exports.unregister = function (cmd) {
globalShortcut.unregister(cmd)
}
// function showWindow () {
// MAIN_WINDOW.show()
// }
// function hideWindow () {
// MAIN_WINDOW.hide()
// }
exports = module.exports;

View File

@@ -83,4 +83,25 @@ exports.getStorageDir = function () {
return storageDir; return storageDir;
} }
exports.getPreferences = function () {
const key = storageKey.PREFERENCES;
if (!this.instance().has(key).value()) {
this.instance().set(key, {}).write();
}
const res = this.instance()
.get(key)
.value();
return res;
};
exports.setShortcuts = function (data) {
const key = storageKey.PREFERENCES + '.shortcuts';
const res = this.instance()
.set(key, data)
.write();
return res;
};
exports = module.exports; exports = module.exports;

View File

@@ -4,7 +4,7 @@ const {app, Tray, Menu} = require('electron');
const path = require('path'); const path = require('path');
const pkg = require('../../package.json'); const pkg = require('../../package.json');
module.exports = () => { exports.setup = function () {
MAIN_WINDOW.on('close', (event) => { MAIN_WINDOW.on('close', (event) => {
if (!CAN_QUIT) { if (!CAN_QUIT) {
MAIN_WINDOW.hide(); MAIN_WINDOW.hide();
@@ -36,3 +36,5 @@ module.exports = () => {
}); });
return APP_TRAY; return APP_TRAY;
} }
exports = module.exports;

12
electron/preferences.js Normal file
View File

@@ -0,0 +1,12 @@
'use strict';
const shortcut = require('./lib/shortcut');
const tray = require('./lib/tray');
module.exports = () => {
// shortcut
shortcut.setup();
// tray
tray.setup();
}

14
main.js
View File

@@ -4,8 +4,8 @@ const eggLauncher = require('./electron/lib/lanucher')
const setup = require('./electron/setup') const setup = require('./electron/setup')
const electronConfig = require('./electron/config') const electronConfig = require('./electron/config')
const storage = require('./electron/lib/storage') const storage = require('./electron/lib/storage')
const is = require('electron-is')
const setTray = require('./electron/lib/tray') const setTray = require('./electron/lib/tray')
const preferences = require('./electron/preferences')
// main window // main window
global.MAIN_WINDOW = null global.MAIN_WINDOW = null
@@ -77,20 +77,12 @@ async function createWindow () {
// loding page // loding page
MAIN_WINDOW.loadURL(path.join('file://', __dirname, '/asset/loading.html')) MAIN_WINDOW.loadURL(path.join('file://', __dirname, '/asset/loading.html'))
// tray // options register
setTray(); preferences()
// egg server // egg server
await startServer(eggConfig) await startServer(eggConfig)
// check update
const updateConfig = electronConfig.get('autoUpdate')
if ((is.windows() && updateConfig.windows) || (is.macOS() && updateConfig.macOS)
|| (is.linux() && updateConfig.linux)) {
const autoUpdater = require('./electron/autoUpdater');
autoUpdater.checkUpdate();
}
return MAIN_WINDOW return MAIN_WINDOW
} }