gaoshuaixing %!s(int64=4) %!d(string=hai) anos
pai
achega
4cc652f823

+ 1 - 0
app/const/storageKey.js

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

+ 15 - 1
electron/apis/example.js

@@ -4,7 +4,8 @@ const path = require('path');
 const {
   app,
   webContents,
-  shell
+  shell,
+  globalShortcut
 } = require('electron');
 
 exports.getPath = function () {
@@ -27,6 +28,19 @@ exports.executeJS = function (str) {
   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) {
   //filepath = path.resolve(filepath);
   filepath = filepath.replace("resources", "");

+ 49 - 0
electron/lib/shortcut.js

@@ -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;

+ 21 - 0
electron/lib/storage.js

@@ -83,4 +83,25 @@ exports.getStorageDir = function () {
   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;

+ 4 - 2
electron/lib/tray.js

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

+ 12 - 0
electron/preferences.js

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

+ 4 - 12
main.js

@@ -4,8 +4,8 @@ const eggLauncher = require('./electron/lib/lanucher')
 const setup = require('./electron/setup')
 const electronConfig = require('./electron/config')
 const storage = require('./electron/lib/storage')
-const is = require('electron-is')
 const setTray = require('./electron/lib/tray')
+const preferences = require('./electron/preferences')
 
 // main window
 global.MAIN_WINDOW = null
@@ -76,21 +76,13 @@ async function createWindow () {
 
   // loding page
   MAIN_WINDOW.loadURL(path.join('file://', __dirname, '/asset/loading.html'))
-  
-  // tray 
-  setTray();
+
+  // options register
+  preferences()
 
   // egg server
   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
 }