diff --git a/src/main/browsers/main.js b/src/main/browsers/main.js index 9df3eaf..a2c9d39 100644 --- a/src/main/browsers/main.js +++ b/src/main/browsers/main.js @@ -1,15 +1,16 @@ -const { BrowserWindow, protocol } = require("electron") +const { BrowserWindow, protocol } = require("electron"); module.exports = () => { - let win + let win; let init = (opts) => { - createWindow(opts) - } + createWindow(opts); + }; let createWindow = (opts) => { - const winURL = process.env.NODE_ENV === 'development' - ? `http://localhost:9080` - : `file://${__dirname}/index.html` + const winURL = + process.env.NODE_ENV === "development" + ? `http://localhost:9080` + : `file://${__dirname}/index.html`; win = new BrowserWindow({ height: 60, @@ -17,7 +18,7 @@ module.exports = () => { resizable: true, width: 800, frame: false, - title: '拉比克', + title: "拉比克", show: false, skipTaskbar: true, webPreferences: { @@ -26,31 +27,35 @@ module.exports = () => { backgroundThrottling: false, contextIsolation: false, webviewTag: true, - nodeIntegration: true // 在网页中集成Node + nodeIntegration: true, // 在网页中集成Node + }, + }); + + win.loadURL(winURL); + + protocol.interceptFileProtocol( + "image", + (req, callback) => { + const url = req.url.substr(8); + callback(decodeURI(url)); + }, + (error) => { + if (error) { + console.error("Failed to register protocol"); + } } - }) + ); - win.loadURL(winURL) - - protocol.interceptFileProtocol('image', (req, callback) => { - const url = req.url.substr(8) - callback(decodeURI(url)) - }, (error) => { - if (error) { - console.error('Failed to register protocol') - } - }) - - win.once('ready-to-show', () => win.show()) + win.once("ready-to-show", () => win.show()); win.on("closed", () => { - win = undefined - }) - } + win = undefined; + }); + }; - let getWindow = () => win + let getWindow = () => win; return { - init: init, - getWindow: getWindow, - } -} + init, + getWindow, + }; +}; diff --git a/src/main/browsers/picker.js b/src/main/browsers/picker.js index 0950c4a..209cc45 100644 --- a/src/main/browsers/picker.js +++ b/src/main/browsers/picker.js @@ -1,13 +1,13 @@ -const { BrowserWindow, nativeImage } = require("electron") +const { BrowserWindow, nativeImage } = require("electron"); module.exports = () => { - let win + let win; let init = (x, y) => { if (win === null || win === undefined) { - createWindow() + createWindow(); } - } + }; let createWindow = () => { win = new BrowserWindow({ @@ -25,18 +25,18 @@ module.exports = () => { contextIsolation: false, devTools: false, }, - }) + }); - win.loadURL(`file://${__static}/plugins/picker/index.html`) + win.loadURL(`file://${__static}/plugins/picker/index.html`); win.on("closed", () => { - win = undefined - }) - } + win = undefined; + }); + }; - let getWindow = () => win + let getWindow = () => win; return { - init: init, - getWindow: getWindow, - } -} + init, + getWindow, + }; +}; diff --git a/src/main/browsers/separate.js b/src/main/browsers/separate.js index 87ff803..c1de595 100644 --- a/src/main/browsers/separate.js +++ b/src/main/browsers/separate.js @@ -1,22 +1,23 @@ -const { BrowserWindow } = require("electron") +const { BrowserWindow } = require("electron"); module.exports = () => { - let win + let win; let init = (opts) => { - createWindow(opts) - } + createWindow(opts); + }; let createWindow = (opts) => { - const winURL = process.env.NODE_ENV === 'development' - ? `http://localhost:9080/#/plugin` - : `${__dirname}/index.html` + const winURL = + process.env.NODE_ENV === "development" + ? `http://localhost:9080/#/plugin` + : `${__dirname}/index.html`; win = new BrowserWindow({ height: 600, useContentSize: true, width: 800, - titleBarStyle: 'hiddenInset', - title: '拉比克', + titleBarStyle: "hiddenInset", + title: "拉比克", show: false, webPreferences: { webSecurity: false, @@ -24,26 +25,30 @@ module.exports = () => { backgroundThrottling: false, contextIsolation: false, webviewTag: true, - nodeIntegration: true // 在网页中集成Node - } - }) - process.env.NODE_ENV === 'development' ? win.loadURL(winURL) : win.loadFile(winURL, { - hash: `#/plugin`, - }) + nodeIntegration: true, // 在网页中集成Node + }, + }); + process.env.NODE_ENV === "development" + ? win.loadURL(winURL) + : win.loadFile(winURL, { + hash: `#/plugin`, + }); - win.webContents.executeJavaScript(`window.setPluginInfo(${opts})`).then(() => { - win.show() - }) + win.webContents + .executeJavaScript(`window.setPluginInfo(${opts})`) + .then(() => { + win.show(); + }); win.on("closed", () => { - win = undefined - }) - } + win = undefined; + }); + }; - let getWindow = () => win + let getWindow = () => win; return { - init: init, - getWindow: getWindow, - } -} + init, + getWindow, + }; +}; diff --git a/src/main/browsers/superPanel.js b/src/main/browsers/superPanel.js index 967c6bd..1a6d0de 100644 --- a/src/main/browsers/superPanel.js +++ b/src/main/browsers/superPanel.js @@ -6,14 +6,14 @@ module.exports = () => { let init = (mainWindow) => { if (win === null || win === undefined) { createWindow(); - ipcMain.on('superPanel-hidden', () => { + ipcMain.on("superPanel-hidden", () => { win.hide(); }); - ipcMain.on('superPanel-setSize', (e, height) => { + ipcMain.on("superPanel-setSize", (e, height) => { win.setSize(250, height); }); - ipcMain.on('superPanel-openPlugin', (e, args) => { - mainWindow.webContents.send('superPanel-openPlugin', args); + ipcMain.on("superPanel-openPlugin", (e, args) => { + mainWindow.webContents.send("superPanel-openPlugin", args); }); } }; @@ -39,7 +39,7 @@ module.exports = () => { win = undefined; }); // 打包后,失焦隐藏 - win.on('blur', () => { + win.on("blur", () => { win.hide(); }); }; @@ -47,7 +47,7 @@ module.exports = () => { let getWindow = () => win; return { - init: init, - getWindow: getWindow, + init, + getWindow, }; }; diff --git a/src/main/common/config.js b/src/main/common/config.js index d3d91ea..0ff3c4e 100644 --- a/src/main/common/config.js +++ b/src/main/common/config.js @@ -1,109 +1,73 @@ -import path from "path" -import fs from 'fs' -import { getlocalDataFile } from "./utils" -import os from 'os' +import path from "path"; +import fs from "fs"; +import { getLocalDataFile } from "./utils"; +import os from "os"; -const configPath = path.join(getlocalDataFile(), './rubick-config.json') +const configPath = path.join(getLocalDataFile(), "./rubick-config.json"); + +const defaultConfigForAnyPlatform = { + version: 3, + perf: { + shortCut: { + showAndHidden: "Option+R", + separate: "Ctrl+D", + quit: "Shift+Escape", + }, + common: { + start: true, + space: true, + }, + local: { + search: true, + }, + }, + superPanel: { + baiduAPI: { + key: "", + appid: "", + }, + mouseDownTime: 500, + }, + global: [], +}; let defaultConfig = { Darwin: { - version: 3, - perf: { - shortCut: { - showAndHidden: 'Option+R', - separate: 'Ctrl+D', - quit: 'Shift+Escape' - }, - common: { - start: true, - space: true, - }, - local: { - search: true, - } - }, - superPanel: { - baiduAPI: { - key: '', - appid: '', - }, - mouseDownTime: 500 - }, - global: [] + ...defaultConfigForAnyPlatform, }, Windows_NT: { - version: 3, - perf: { - shortCut: { - showAndHidden: 'Option+R', - separate: 'Ctrl+D', - quit: 'Shift+Escape' - }, - common: { - start: true, - space: true, - }, - local: { - search: true, - } - }, - superPanel: { - baiduAPI: { - key: '', - appid: '', - }, - mouseDownTime: 500 - }, - global: [] + ...defaultConfigForAnyPlatform, }, Linux: { - version: 3, - perf: { - shortCut: { - showAndHidden: 'Option+R', - separate: 'Ctrl+D', - quit: 'Shift+Escape' - }, - common: { - start: true, - space: true, - }, - local: { - search: true, - } - }, - superPanel: { - baiduAPI: { - key: '', - appid: '', - }, - mouseDownTime: 500 - }, - global: [] - } -} + ...defaultConfigForAnyPlatform, + }, +}; global.opConfig = { config: null, get() { const platform = os.type(); try { if (!opConfig.config) { - opConfig.config = JSON.parse(fs.readFileSync(configPath) || JSON.stringify(defaultConfig[platform])) + opConfig.config = JSON.parse( + fs.readFileSync(configPath) || JSON.stringify(defaultConfig[platform]) + ); } // 重置 - if (!opConfig.config.version || opConfig.config.version < defaultConfig[platform].version) { - opConfig.config = defaultConfig[platform] - fs.writeFileSync(configPath, JSON.stringify(opConfig.config)) + if ( + !opConfig.config.version || + opConfig.config.version < defaultConfig[platform].version + ) { + opConfig.config = defaultConfig[platform]; + fs.writeFileSync(configPath, JSON.stringify(opConfig.config)); } - return opConfig.config + return opConfig.config; } catch (e) { - opConfig.config = defaultConfig[platform] - return opConfig.config + opConfig.config = defaultConfig[platform]; + return opConfig.config; } }, set(key, value) { opConfig.config[key] = value; - fs.writeFileSync(configPath, JSON.stringify(opConfig.config)) - } -} - + fs.writeFileSync(configPath, JSON.stringify(opConfig.config)); + }, +}; diff --git a/src/main/common/utils.js b/src/main/common/utils.js index ce85484..75df6bd 100644 --- a/src/main/common/utils.js +++ b/src/main/common/utils.js @@ -1,6 +1,6 @@ import fs from "fs"; -export const getlocalDataFile = () => { +export const getLocalDataFile = () => { let localDataFile = process.env.HOME; if (!localDataFile) { localDataFile = process.env.LOCALAPPDATA; @@ -14,13 +14,13 @@ export function saveData(path, value) { export function getData(path, defaultValue) { try { - return JSON.parse(fs.readFileSync(path, 'utf8')); + return JSON.parse(fs.readFileSync(path, "utf8")); } catch (e) { return defaultValue || undefined; } } -export function throttle (func, wait, options) { +export function throttle(func, wait, options) { let context, args, result; let timeout = null; let previous = 0; @@ -54,21 +54,19 @@ export function throttle (func, wait, options) { } export const commonConst = { - linux: function () { - return process.platform === 'linux' + linux: function() { + return process.platform === "linux"; }, - macOS () { - return process.platform === 'darwin'; + macOS() { + return process.platform === "darwin"; }, - windows () { - return process.platform === 'win32' + windows() { + return process.platform === "win32"; }, - production: function () { - return process.env.NODE_ENV !== 'development'; + production: function() { + return process.env.NODE_ENV !== "development"; }, - dev: function () { - return process.env.NODE_ENV === 'development'; + dev: function() { + return process.env.NODE_ENV === "development"; }, -} - - +}; diff --git a/src/renderer/App.vue b/src/renderer/App.vue index aab267a..8920358 100644 --- a/src/renderer/App.vue +++ b/src/renderer/App.vue @@ -2,7 +2,7 @@