Skip to content

Commit

Permalink
🚧 WIP: shortcut system
Browse files Browse the repository at this point in the history
  • Loading branch information
Molunerfinn committed Sep 10, 2019
1 parent fe9e19a commit f21940e
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 40 deletions.
5 changes: 3 additions & 2 deletions src/datastore/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand All @@ -67,7 +67,8 @@ function getClipboardFiles () {
let files = [
'/linux.sh',
'/mac.applescript',
'/windows.ps1'
'/windows.ps1',
'/windows10.ps1'
]

return files.map(item => {
Expand Down
26 changes: 23 additions & 3 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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) { // 如果有文件列表作为参数,说明是命令行启动
Expand Down Expand Up @@ -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
*
Expand Down
25 changes: 25 additions & 0 deletions src/main/migrate/shortKeyUpdateHelper.js
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 5 additions & 0 deletions src/main/utils/eventBus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { EventEmitter } from 'events'

const bus = new EventEmitter()

export default bus
32 changes: 32 additions & 0 deletions src/main/utils/shortKeyRegister.js
Original file line number Diff line number Diff line change
@@ -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
}
52 changes: 17 additions & 35 deletions static/windows.ps1
Original file line number Diff line number Diff line change
@@ -1,44 +1,26 @@
# Adapted from https://github.com/octan3/img-clipboard-dump/blob/master/dump-clipboard-png.ps1

param($imagePath)

# https://github.com/PowerShell/PowerShell/issues/7233
# fix the output encoding bug
[console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
# Adapted from https://github.com/octan3/img-clipboard-dump/blob/master/dump-clipboard-png.ps1

Add-Type -Assembly PresentationCore
function main {
$img = [Windows.Clipboard]::GetImage()

if ($img -eq $null) {
"no image"
Exit 1
}
$img = [Windows.Clipboard]::GetImage()

if (-not $imagePath) {
"no image"
Exit 1
}

$fcb = new-object Windows.Media.Imaging.FormatConvertedBitmap($img, [Windows.Media.PixelFormats]::Rgb24, $null, 0)
$stream = [IO.File]::Open($imagePath, "OpenOrCreate")
$encoder = New-Object Windows.Media.Imaging.PngBitmapEncoder
$encoder.Frames.Add([Windows.Media.Imaging.BitmapFrame]::Create($fcb)) | out-null
$encoder.Save($stream) | out-null
$stream.Dispose() | out-null

$imagePath
if ($img -eq $null) {
"no image"
Exit 1
}

try {
# For WIN10
$file = Get-Clipboard -Format FileDropList
if ($file -ne $null) {
Convert-Path $file
Exit 1
}
} catch {
# For WIN7 WIN8 WIN10
main
if (-not $imagePath) {
"no image"
Exit 1
}

main
$fcb = new-object Windows.Media.Imaging.FormatConvertedBitmap($img, [Windows.Media.PixelFormats]::Rgb24, $null, 0)
$stream = [IO.File]::Open($imagePath, "OpenOrCreate")
$encoder = New-Object Windows.Media.Imaging.PngBitmapEncoder
$encoder.Frames.Add([Windows.Media.Imaging.BitmapFrame]::Create($fcb)) | out-null
$encoder.Save($stream) | out-null
$stream.Dispose() | out-null

$imagePath
44 changes: 44 additions & 0 deletions static/windows10.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Adapted from https://github.com/octan3/img-clipboard-dump/blob/master/dump-clipboard-png.ps1
param($imagePath)

# https://github.com/PowerShell/PowerShell/issues/7233
# fix the output encoding bug
[console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding

Add-Type -Assembly PresentationCore
function main {
$img = [Windows.Clipboard]::GetImage()

if ($img -eq $null) {
"no image"
Exit 1
}

if (-not $imagePath) {
"no image"
Exit 1
}

$fcb = new-object Windows.Media.Imaging.FormatConvertedBitmap($img, [Windows.Media.PixelFormats]::Rgb24, $null, 0)
$stream = [IO.File]::Open($imagePath, "OpenOrCreate")
$encoder = New-Object Windows.Media.Imaging.PngBitmapEncoder
$encoder.Frames.Add([Windows.Media.Imaging.BitmapFrame]::Create($fcb)) | out-null
$encoder.Save($stream) | out-null
$stream.Dispose() | out-null

$imagePath
}

try {
# For WIN10
$file = Get-Clipboard -Format FileDropList
if ($file -ne $null) {
Convert-Path $file
Exit 1
}
} catch {
# For WIN7 WIN8 WIN10
main
}

main

0 comments on commit f21940e

Please sign in to comment.