feat: 支持快捷键设置

This commit is contained in:
muwoo
2021-06-30 21:19:41 +08:00
parent 7341edb32f
commit 4bac5ac8a2
8 changed files with 400 additions and 21 deletions

View File

@@ -7,6 +7,7 @@ import {
} from 'electron';
import Api from './api';
import robot from 'robotjs';
import './config';
const browsers = require("../browsers")();
const mouseEvents = require("osx-mouse");
@@ -20,6 +21,19 @@ let closePicker = (newColor) => {
}
};
function registerShortCut(mainWindow) {
const config = global.opConfig.get();
globalShortcut.unregisterAll();
globalShortcut.register(config.perf.shortCut.showAndHidden, () => {
mainWindow.show();
});
globalShortcut.register(config.perf.shortCut.separate, () => {
mainWindow.webContents.send('new-window');
});
}
export default function init(mainWindow) {
const mouseTrack = mouseEvents();
let down_time = 0;
@@ -31,26 +45,20 @@ export default function init(mainWindow) {
new Notification({ title: 'Rubick 通知', body: '长按了' }).show();
}
});
registerShortCut(mainWindow);
ipcMain.on('re-register', (event, arg) => {
registerShortCut(mainWindow);
});
ipcMain.on('changeWindowSize-rubick', (event, arg) => {
mainWindow.setSize(arg.width || 800, arg.height);
});
mainWindow.on('blur', () => {
// mainWindow.hide();
mainWindow.hide();
});
globalShortcut.register('Alt+R', () => {
mainWindow.show();
});
ipcMain.on('init-shortcut', (event) => {
globalShortcut.register('ctrl+d', () => {
event.sender.send('new-window');
});
})
ipcMain.on('msg-trigger', async (event, arg) => {
const window = arg.winId ? BrowserWindow.fromId(arg.winId) : mainWindow
const operators = arg.type.split('.');

46
src/main/common/config.js Normal file
View File

@@ -0,0 +1,46 @@
import path from "path";
import fs from 'fs';
import {getlocalDataFile} from "./utils";
import os from 'os';
const configPath = path.join(getlocalDataFile(), './rubick-config.json');
let defaultConfig = {
Darwin: {
perf: {
shortCut: {
showAndHidden: 'Option+R',
separate: 'Ctrl+D'
},
common: {
start: true,
space: true,
},
local: {
search: true,
}
},
}
}
global.opConfig = {
config: null,
get() {
const platform = os.type();
try {
if (!opConfig.config) {
opConfig.config = JSON.parse(fs.readFileSync(configPath) || JSON.stringify(defaultConfig[platform]));
}
return opConfig.config;
} catch (e) {
opConfig.config = defaultConfig[platform]
return opConfig.config;
}
},
set(key, value) {
console.log(opConfig.config);
opConfig.config[key] = value;
fs.writeFileSync(configPath, JSON.stringify(opConfig.config));
}
}