🚧 WIP: shortcut system

This commit is contained in:
PiEgg
2019-09-10 14:38:08 +08:00
parent fe9e19a84d
commit f21940ef23
7 changed files with 149 additions and 40 deletions

View File

@@ -44,7 +44,7 @@ if (!db.has('settings.shortKey').value()) {
// init generate clipboard image files
let clipboardFiles = getClipboardFiles()
if (!fs.pathExistsSync(path.join(STORE_PATH, 'windows.ps1'))) {
if (!fs.pathExistsSync(path.join(STORE_PATH, 'windows10.ps1'))) {
clipboardFiles.forEach(item => {
fs.copyFileSync(item.origin, item.dest)
})
@@ -67,7 +67,8 @@ function getClipboardFiles () {
let files = [
'/linux.sh',
'/mac.applescript',
'/windows.ps1'
'/windows.ps1',
'/windows10.ps1'
]
return files.map(item => {

View File

@@ -22,6 +22,13 @@ import pkg from '../../package.json'
import picgoCoreIPC from './utils/picgoCoreIPC'
import fixPath from 'fix-path'
import { getUploadFiles } from './utils/handleArgv'
import bus from './utils/eventBus'
import {
updateShortKeyFromVersion212
} from './migrate/shortKeyUpdateHelper'
import {
initShortKeyRegister
} from './utils/shortKeyRegister'
if (process.platform === 'darwin') {
beforeOpen()
}
@@ -553,10 +560,13 @@ app.on('ready', () => {
}
db.read().set('needReload', false).write()
updateChecker()
globalShortcut.register(db.read().get('settings.shortKey.upload').value(), () => {
uploadClipboardFiles()
initEventCenter()
// 不需要阻塞
process.nextTick(() => {
updateShortKeyFromVersion212(db, db.read().get('settings.shortKey').value())
initShortKeyRegister(globalShortcut, db.read().get('settings.shortKey').value())
})
if (process.env.NODE_ENV !== 'development') {
let files = getUploadFiles()
if (files === null || files.length > 0) { // 如果有文件列表作为参数,说明是命令行启动
@@ -592,12 +602,22 @@ app.on('activate', () => {
app.on('will-quit', () => {
globalShortcut.unregisterAll()
bus.removeAllListeners()
})
app.setLoginItemSettings({
openAtLogin: db.read().get('settings.autoStart').value() || false
})
function initEventCenter () {
const eventList = {
'picgo:upload': uploadClipboardFiles
}
for (let i in eventList) {
bus.on(i, eventList[i])
}
}
/**
* Auto Updater
*

View File

@@ -0,0 +1,25 @@
// from v2.1.2
const updateShortKeyFromVersion212 = (db, shortKeyConfig) => {
let needUpgrade = false
Object.keys(shortKeyConfig).forEach(item => {
if (typeof shortKeyConfig[item] === 'string') {
needUpgrade = true
shortKeyConfig[item] = {
enable: true,
key: shortKeyConfig[item],
name: `picgo:${item}`,
lable: '快捷上传'
}
}
})
if (needUpgrade) {
db.read().set('settings.shortKey', shortKeyConfig).write()
return shortKeyConfig
} else {
return false
}
}
export {
updateShortKeyFromVersion212
}

View File

@@ -0,0 +1,5 @@
import { EventEmitter } from 'events'
const bus = new EventEmitter()
export default bus

View File

@@ -0,0 +1,32 @@
import bus from '../utils/eventBus'
/**
*
* @param {string} name
*/
const shortKeyHandler = (name) => {
if (name.includes('picgo:')) {
bus.emit(name)
} else if (name.includes('picgo-plugin-')) {
// TODO: 处理插件快捷键
}
}
// 初始化阶段的注册
const initShortKeyRegister = (globalShortcut, shortKeys) => {
let errorList = []
for (let i in shortKeys) {
try {
if (shortKeys[i].enable) {
globalShortcut.register(shortKeys[i].key, () => {
shortKeyHandler(shortKeys[i].name)
})
}
} catch (e) {
errorList.push(shortKeys[i])
}
}
}
export {
initShortKeyRegister
}