2017-11-27 19:21:12 -05:00
|
|
|
'use strict'
|
|
|
|
|
2019-05-22 12:13:12 -04:00
|
|
|
import {
|
|
|
|
app,
|
|
|
|
BrowserWindow,
|
|
|
|
Tray,
|
|
|
|
Menu,
|
|
|
|
Notification,
|
|
|
|
clipboard,
|
|
|
|
ipcMain,
|
|
|
|
globalShortcut,
|
|
|
|
dialog,
|
2019-12-19 06:17:21 -05:00
|
|
|
systemPreferences,
|
|
|
|
WebContents,
|
|
|
|
IpcMainEvent,
|
|
|
|
protocol
|
2019-05-22 12:13:12 -04:00
|
|
|
} from 'electron'
|
2019-12-19 06:17:21 -05:00
|
|
|
import {
|
|
|
|
createProtocol,
|
|
|
|
installVueDevtools
|
|
|
|
} from 'vue-cli-plugin-electron-builder/lib'
|
|
|
|
import db from '#/datastore'
|
2019-12-26 07:15:41 -05:00
|
|
|
import picgo from '~/main/utils/picgo'
|
|
|
|
import uploader from '~/main/utils/uploader'
|
2019-12-19 06:17:21 -05:00
|
|
|
import beforeOpen from '~/main/utils/beforeOpen'
|
|
|
|
import pasteTemplate from '#/utils/pasteTemplate'
|
|
|
|
import updateChecker from '~/main/utils/updateChecker'
|
|
|
|
import { getPicBeds } from '~/main/utils/getPicBeds'
|
|
|
|
import pkg from 'root/package.json'
|
|
|
|
import picgoCoreIPC from '~/main/utils/picgoCoreIPC'
|
2019-01-12 06:19:26 -05:00
|
|
|
import fixPath from 'fix-path'
|
2019-12-19 06:17:21 -05:00
|
|
|
import { getUploadFiles } from '~/main/utils/handleArgv'
|
|
|
|
import bus from '~/main/utils/eventBus'
|
2019-09-10 02:38:08 -04:00
|
|
|
import {
|
|
|
|
updateShortKeyFromVersion212
|
2019-12-19 06:17:21 -05:00
|
|
|
} from '~/main/migrate/shortKeyUpdateHelper'
|
2019-12-22 04:22:32 -05:00
|
|
|
import shortKeyHandler from '~/main/utils/shortKeyHandler'
|
2019-12-20 05:46:39 -05:00
|
|
|
import logger from '~/main/utils/logger'
|
|
|
|
|
2019-12-19 06:17:21 -05:00
|
|
|
const isDevelopment = process.env.NODE_ENV !== 'production'
|
2019-12-20 05:46:39 -05:00
|
|
|
protocol.registerSchemesAsPrivileged([{ scheme: 'picgo', privileges: { secure: true, standard: true } }])
|
2019-12-19 06:17:21 -05:00
|
|
|
|
2019-12-28 02:19:27 -05:00
|
|
|
beforeOpen()
|
2017-11-27 19:21:12 -05:00
|
|
|
|
2019-12-19 06:17:21 -05:00
|
|
|
let window: BrowserWindow | null
|
|
|
|
let settingWindow: BrowserWindow | null
|
|
|
|
let miniWindow: BrowserWindow | null
|
|
|
|
let tray: Tray | null
|
|
|
|
let menu: Menu | null
|
|
|
|
let contextMenu: Menu | null
|
|
|
|
const winURL = isDevelopment
|
|
|
|
? (process.env.WEBPACK_DEV_SERVER_URL as string)
|
|
|
|
: `picgo://./index.html`
|
|
|
|
const settingWinURL = isDevelopment
|
|
|
|
? `${(process.env.WEBPACK_DEV_SERVER_URL as string)}#setting/upload`
|
|
|
|
: `picgo://./index.html#setting/upload`
|
|
|
|
const miniWinURL = isDevelopment
|
|
|
|
? `${(process.env.WEBPACK_DEV_SERVER_URL as string)}#mini-page`
|
|
|
|
: `picgo://./index.html#mini-page`
|
2017-11-27 19:21:12 -05:00
|
|
|
|
2019-01-12 06:19:26 -05:00
|
|
|
// fix the $PATH in macOS
|
|
|
|
fixPath()
|
|
|
|
|
2018-12-25 03:11:10 -05:00
|
|
|
function createContextMenu () {
|
2019-12-26 07:15:41 -05:00
|
|
|
const picBeds = getPicBeds()
|
2019-12-28 03:23:16 -05:00
|
|
|
const submenu = picBeds.filter(item => item.visible).map(item => {
|
2018-06-05 08:29:16 -04:00
|
|
|
return {
|
|
|
|
label: item.name,
|
|
|
|
type: 'radio',
|
2019-09-11 07:30:08 -04:00
|
|
|
checked: db.get('picBed.current') === item.type,
|
2018-06-05 08:29:16 -04:00
|
|
|
click () {
|
2019-12-26 07:15:41 -05:00
|
|
|
picgo.saveConfig({
|
|
|
|
'picBed.current': item.type
|
|
|
|
})
|
2018-07-10 10:37:29 -04:00
|
|
|
if (settingWindow) {
|
|
|
|
settingWindow.webContents.send('syncPicBed')
|
|
|
|
}
|
2018-06-05 08:29:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2018-01-30 02:20:19 -05:00
|
|
|
contextMenu = Menu.buildFromTemplate([
|
2018-01-09 22:31:07 -05:00
|
|
|
{
|
|
|
|
label: '关于',
|
|
|
|
click () {
|
|
|
|
dialog.showMessageBox({
|
|
|
|
title: 'PicGo',
|
|
|
|
message: 'PicGo',
|
|
|
|
detail: `Version: ${pkg.version}\nAuthor: Molunerfinn\nGithub: https://github.com/Molunerfinn/PicGo`
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
2017-11-27 19:21:12 -05:00
|
|
|
{
|
2017-11-28 10:56:15 -05:00
|
|
|
label: '打开详细窗口',
|
2017-11-27 19:21:12 -05:00
|
|
|
click () {
|
|
|
|
if (settingWindow === null) {
|
|
|
|
createSettingWindow()
|
2019-12-19 06:17:21 -05:00
|
|
|
settingWindow!.show()
|
2017-11-27 19:21:12 -05:00
|
|
|
} else {
|
|
|
|
settingWindow.show()
|
|
|
|
settingWindow.focus()
|
|
|
|
}
|
2018-07-07 02:45:14 -04:00
|
|
|
if (miniWindow) {
|
|
|
|
miniWindow.hide()
|
|
|
|
}
|
2017-11-27 19:21:12 -05:00
|
|
|
}
|
2017-11-28 10:56:15 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: '选择默认图床',
|
|
|
|
type: 'submenu',
|
2019-12-19 06:17:21 -05:00
|
|
|
// @ts-ignore
|
2018-06-05 08:29:16 -04:00
|
|
|
submenu
|
2017-12-11 08:52:14 -05:00
|
|
|
},
|
2019-12-19 06:17:21 -05:00
|
|
|
// @ts-ignore
|
2018-01-10 04:12:24 -05:00
|
|
|
{
|
|
|
|
label: '打开更新助手',
|
|
|
|
type: 'checkbox',
|
2019-09-11 07:30:08 -04:00
|
|
|
checked: db.get('settings.showUpdateTip'),
|
2018-01-10 04:12:24 -05:00
|
|
|
click () {
|
2019-09-11 07:30:08 -04:00
|
|
|
const value = db.get('settings.showUpdateTip')
|
|
|
|
db.set('settings.showUpdateTip', !value)
|
2018-01-10 04:12:24 -05:00
|
|
|
}
|
|
|
|
},
|
2019-01-21 03:46:33 -05:00
|
|
|
{
|
|
|
|
label: '重启应用',
|
|
|
|
click () {
|
|
|
|
app.relaunch()
|
|
|
|
app.exit(0)
|
|
|
|
}
|
|
|
|
},
|
2019-12-19 06:17:21 -05:00
|
|
|
// @ts-ignore
|
2017-12-11 08:52:14 -05:00
|
|
|
{
|
|
|
|
role: 'quit',
|
|
|
|
label: '退出'
|
2017-11-27 19:21:12 -05:00
|
|
|
}
|
|
|
|
])
|
2018-12-25 03:11:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function createTray () {
|
|
|
|
const menubarPic = process.platform === 'darwin' ? `${__static}/menubar.png` : `${__static}/menubar-nodarwin.png`
|
|
|
|
tray = new Tray(menubarPic)
|
2017-11-27 19:21:12 -05:00
|
|
|
tray.on('right-click', () => {
|
2018-06-26 05:03:46 -04:00
|
|
|
if (window) {
|
|
|
|
window.hide()
|
|
|
|
}
|
2018-12-25 03:11:10 -05:00
|
|
|
createContextMenu()
|
2019-12-19 06:17:21 -05:00
|
|
|
tray!.popUpContextMenu(contextMenu!)
|
2017-11-27 19:21:12 -05:00
|
|
|
})
|
2018-10-20 11:24:28 -04:00
|
|
|
tray.on('click', (event, bounds) => {
|
2018-01-10 03:51:09 -05:00
|
|
|
if (process.platform === 'darwin') {
|
2018-10-20 11:24:28 -04:00
|
|
|
toggleWindow(bounds)
|
2018-01-10 03:51:09 -05:00
|
|
|
setTimeout(() => {
|
2019-12-28 03:23:16 -05:00
|
|
|
let img = clipboard.readImage()
|
|
|
|
let obj: ImgInfo[] = []
|
|
|
|
if (!img.isEmpty()) {
|
|
|
|
// 从剪贴板来的图片默认转为png
|
|
|
|
// @ts-ignore
|
|
|
|
const imgUrl = 'data:image/png;base64,' + Buffer.from(img.toPNG(), 'binary').toString('base64')
|
|
|
|
obj.push({
|
|
|
|
width: img.getSize().width,
|
|
|
|
height: img.getSize().height,
|
|
|
|
imgUrl
|
|
|
|
})
|
|
|
|
}
|
2019-12-19 06:17:21 -05:00
|
|
|
window!.webContents.send('clipboardFiles', obj)
|
2018-01-10 03:51:09 -05:00
|
|
|
}, 0)
|
|
|
|
} else {
|
2018-06-26 05:03:46 -04:00
|
|
|
if (window) {
|
|
|
|
window.hide()
|
|
|
|
}
|
2018-01-10 03:51:09 -05:00
|
|
|
if (settingWindow === null) {
|
|
|
|
createSettingWindow()
|
2019-12-19 06:17:21 -05:00
|
|
|
settingWindow!.show()
|
2018-01-10 03:51:09 -05:00
|
|
|
} else {
|
|
|
|
settingWindow.show()
|
|
|
|
settingWindow.focus()
|
|
|
|
}
|
2018-09-13 04:54:18 -04:00
|
|
|
if (miniWindow) {
|
|
|
|
miniWindow.hide()
|
|
|
|
}
|
2017-11-27 19:21:12 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
tray.on('drag-enter', () => {
|
2019-05-22 12:13:12 -04:00
|
|
|
if (systemPreferences.isDarkMode()) {
|
2019-12-19 06:17:21 -05:00
|
|
|
tray!.setImage(`${__static}/upload-dark.png`)
|
2019-05-22 12:13:12 -04:00
|
|
|
} else {
|
2019-12-19 06:17:21 -05:00
|
|
|
tray!.setImage(`${__static}/upload.png`)
|
2019-05-22 12:13:12 -04:00
|
|
|
}
|
2017-11-27 19:21:12 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
tray.on('drag-end', () => {
|
2019-12-19 06:17:21 -05:00
|
|
|
tray!.setImage(`${__static}/menubar.png`)
|
2017-11-27 19:21:12 -05:00
|
|
|
})
|
|
|
|
|
2019-12-19 06:17:21 -05:00
|
|
|
tray.on('drop-files', async (event: Event, files: string[]) => {
|
2019-09-11 07:30:08 -04:00
|
|
|
const pasteStyle = db.get('settings.pasteStyle') || 'markdown'
|
2019-12-26 07:15:41 -05:00
|
|
|
const imgs = await uploader.setWebContents(window!.webContents).upload(files)
|
2017-12-22 09:30:16 -05:00
|
|
|
if (imgs !== false) {
|
2019-12-19 06:17:21 -05:00
|
|
|
for (let i = 0; i < imgs.length; i++) {
|
2019-04-18 05:25:10 -04:00
|
|
|
clipboard.writeText(pasteTemplate(pasteStyle, imgs[i]))
|
2017-12-22 09:30:16 -05:00
|
|
|
const notification = new Notification({
|
|
|
|
title: '上传成功',
|
2019-12-19 06:17:21 -05:00
|
|
|
body: imgs[i].imgUrl!,
|
2017-12-22 09:30:16 -05:00
|
|
|
icon: files[i]
|
|
|
|
})
|
|
|
|
setTimeout(() => {
|
|
|
|
notification.show()
|
|
|
|
}, i * 100)
|
2019-09-11 07:30:08 -04:00
|
|
|
db.insert('uploaded', imgs[i])
|
2017-12-22 09:30:16 -05:00
|
|
|
}
|
2019-12-19 06:17:21 -05:00
|
|
|
window!.webContents.send('dragFiles', imgs)
|
2017-11-27 19:21:12 -05:00
|
|
|
}
|
|
|
|
})
|
2017-12-08 02:52:41 -05:00
|
|
|
// toggleWindow()
|
2017-11-27 19:21:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const createWindow = () => {
|
2018-06-26 05:03:46 -04:00
|
|
|
if (process.platform !== 'darwin' && process.platform !== 'win32') {
|
|
|
|
return
|
|
|
|
}
|
2017-11-27 19:21:12 -05:00
|
|
|
window = new BrowserWindow({
|
|
|
|
height: 350,
|
|
|
|
width: 196, // 196
|
|
|
|
show: false,
|
|
|
|
frame: false,
|
|
|
|
fullscreenable: false,
|
|
|
|
resizable: false,
|
|
|
|
transparent: true,
|
|
|
|
vibrancy: 'ultra-dark',
|
|
|
|
webPreferences: {
|
2019-05-05 10:22:03 -04:00
|
|
|
nodeIntegration: true,
|
|
|
|
nodeIntegrationInWorker: true,
|
2017-11-27 19:21:12 -05:00
|
|
|
backgroundThrottling: false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
window.loadURL(winURL)
|
|
|
|
|
|
|
|
window.on('closed', () => {
|
|
|
|
window = null
|
|
|
|
})
|
|
|
|
|
|
|
|
window.on('blur', () => {
|
2019-12-19 06:17:21 -05:00
|
|
|
window!.hide()
|
2017-11-27 19:21:12 -05:00
|
|
|
})
|
2019-04-14 03:41:48 -04:00
|
|
|
return window
|
2017-11-27 19:21:12 -05:00
|
|
|
}
|
|
|
|
|
2018-07-05 12:43:53 -04:00
|
|
|
const createMiniWidow = () => {
|
2018-07-06 11:54:36 -04:00
|
|
|
if (miniWindow) {
|
|
|
|
return false
|
|
|
|
}
|
2019-12-21 04:28:29 -05:00
|
|
|
let obj: IBrowserWindowOptions = {
|
2018-07-05 12:43:53 -04:00
|
|
|
height: 64,
|
2018-07-06 11:54:36 -04:00
|
|
|
width: 64,
|
2019-04-17 02:45:08 -04:00
|
|
|
show: process.platform === 'linux',
|
2018-07-05 12:43:53 -04:00
|
|
|
frame: false,
|
|
|
|
fullscreenable: false,
|
2019-01-21 03:46:33 -05:00
|
|
|
skipTaskbar: true,
|
2018-07-05 12:43:53 -04:00
|
|
|
resizable: false,
|
2019-04-17 02:45:08 -04:00
|
|
|
transparent: process.platform !== 'linux',
|
2018-07-06 12:01:06 -04:00
|
|
|
icon: `${__static}/logo.png`,
|
2018-07-05 12:43:53 -04:00
|
|
|
webPreferences: {
|
2019-05-05 10:22:03 -04:00
|
|
|
backgroundThrottling: false,
|
|
|
|
nodeIntegration: true,
|
|
|
|
nodeIntegrationInWorker: true
|
2018-07-05 12:43:53 -04:00
|
|
|
}
|
2018-07-10 21:39:23 -04:00
|
|
|
}
|
|
|
|
|
2019-09-11 07:30:08 -04:00
|
|
|
if (db.get('settings.miniWindowOntop')) {
|
2018-09-07 05:32:11 -04:00
|
|
|
obj.alwaysOnTop = true
|
|
|
|
}
|
|
|
|
|
2018-07-10 21:39:23 -04:00
|
|
|
miniWindow = new BrowserWindow(obj)
|
2018-07-05 12:43:53 -04:00
|
|
|
|
|
|
|
miniWindow.loadURL(miniWinURL)
|
|
|
|
|
|
|
|
miniWindow.on('closed', () => {
|
|
|
|
miniWindow = null
|
|
|
|
})
|
2019-04-14 03:41:48 -04:00
|
|
|
return miniWindow
|
2018-07-05 12:43:53 -04:00
|
|
|
}
|
|
|
|
|
2017-11-27 19:21:12 -05:00
|
|
|
const createSettingWindow = () => {
|
2019-12-21 04:28:29 -05:00
|
|
|
const options: IBrowserWindowOptions = {
|
2017-11-27 19:21:12 -05:00
|
|
|
height: 450,
|
|
|
|
width: 800,
|
2017-12-08 02:52:41 -05:00
|
|
|
show: false,
|
2017-11-27 19:21:12 -05:00
|
|
|
frame: true,
|
|
|
|
center: true,
|
|
|
|
fullscreenable: false,
|
|
|
|
resizable: false,
|
2018-01-10 03:51:09 -05:00
|
|
|
title: 'PicGo',
|
2017-11-27 19:21:12 -05:00
|
|
|
vibrancy: 'ultra-dark',
|
|
|
|
transparent: true,
|
|
|
|
titleBarStyle: 'hidden',
|
|
|
|
webPreferences: {
|
2018-09-18 06:48:55 -04:00
|
|
|
backgroundThrottling: false,
|
2019-05-05 10:22:03 -04:00
|
|
|
nodeIntegration: true,
|
|
|
|
nodeIntegrationInWorker: true,
|
2018-09-18 06:48:55 -04:00
|
|
|
webSecurity: false
|
2017-11-27 19:21:12 -05:00
|
|
|
}
|
2018-01-10 03:51:09 -05:00
|
|
|
}
|
2018-06-25 07:27:41 -04:00
|
|
|
if (process.platform !== 'darwin') {
|
2018-09-13 04:54:18 -04:00
|
|
|
options.show = false
|
2018-01-10 03:51:09 -05:00
|
|
|
options.frame = false
|
|
|
|
options.backgroundColor = '#3f3c37'
|
2018-06-25 07:27:41 -04:00
|
|
|
options.transparent = false
|
2018-07-05 12:43:53 -04:00
|
|
|
options.icon = `${__static}/logo.png`
|
2018-01-10 03:51:09 -05:00
|
|
|
}
|
|
|
|
settingWindow = new BrowserWindow(options)
|
2017-11-27 19:21:12 -05:00
|
|
|
|
2019-12-19 06:17:21 -05:00
|
|
|
settingWindow!.loadURL(settingWinURL)
|
2017-11-27 19:21:12 -05:00
|
|
|
|
2019-12-19 06:17:21 -05:00
|
|
|
settingWindow!.on('closed', () => {
|
2019-09-25 04:34:29 -04:00
|
|
|
bus.emit('toggleShortKeyModifiedMode', false)
|
2017-11-27 19:21:12 -05:00
|
|
|
settingWindow = null
|
2018-07-10 22:04:42 -04:00
|
|
|
if (process.platform === 'linux') {
|
2019-09-25 04:34:29 -04:00
|
|
|
process.nextTick(() => {
|
|
|
|
app.quit()
|
|
|
|
})
|
2018-07-10 22:04:42 -04:00
|
|
|
}
|
2017-11-27 19:21:12 -05:00
|
|
|
})
|
2018-06-26 05:03:46 -04:00
|
|
|
createMenu()
|
2018-07-05 12:43:53 -04:00
|
|
|
createMiniWidow()
|
2019-04-14 03:41:48 -04:00
|
|
|
return settingWindow
|
2017-11-27 19:21:12 -05:00
|
|
|
}
|
|
|
|
|
2017-12-15 04:37:38 -05:00
|
|
|
const createMenu = () => {
|
2018-01-09 22:31:07 -05:00
|
|
|
if (process.env.NODE_ENV !== 'development') {
|
|
|
|
const template = [{
|
|
|
|
label: 'Edit',
|
|
|
|
submenu: [
|
|
|
|
{ label: 'Undo', accelerator: 'CmdOrCtrl+Z', selector: 'undo:' },
|
|
|
|
{ label: 'Redo', accelerator: 'Shift+CmdOrCtrl+Z', selector: 'redo:' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ label: 'Cut', accelerator: 'CmdOrCtrl+X', selector: 'cut:' },
|
|
|
|
{ label: 'Copy', accelerator: 'CmdOrCtrl+C', selector: 'copy:' },
|
|
|
|
{ label: 'Paste', accelerator: 'CmdOrCtrl+V', selector: 'paste:' },
|
|
|
|
{ label: 'Select All', accelerator: 'CmdOrCtrl+A', selector: 'selectAll:' },
|
|
|
|
{
|
|
|
|
label: 'Quit',
|
|
|
|
accelerator: 'CmdOrCtrl+Q',
|
|
|
|
click () {
|
|
|
|
app.quit()
|
|
|
|
}
|
2017-12-15 04:37:38 -05:00
|
|
|
}
|
2018-01-09 22:31:07 -05:00
|
|
|
]
|
|
|
|
}]
|
2019-12-19 06:17:21 -05:00
|
|
|
// @ts-ignore
|
2018-01-09 22:31:07 -05:00
|
|
|
menu = Menu.buildFromTemplate(template)
|
|
|
|
Menu.setApplicationMenu(menu)
|
|
|
|
}
|
2017-12-15 04:37:38 -05:00
|
|
|
}
|
|
|
|
|
2019-12-21 04:28:29 -05:00
|
|
|
const toggleWindow = (bounds: IBounds) => {
|
2019-12-19 06:17:21 -05:00
|
|
|
if (window!.isVisible()) {
|
|
|
|
window!.hide()
|
2017-11-27 19:21:12 -05:00
|
|
|
} else {
|
2018-10-20 11:24:28 -04:00
|
|
|
showWindow(bounds)
|
2017-11-27 19:21:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-21 04:28:29 -05:00
|
|
|
const showWindow = (bounds: IBounds) => {
|
2019-12-19 06:17:21 -05:00
|
|
|
window!.setPosition(bounds.x - 98 + 11, bounds.y, false)
|
|
|
|
window!.webContents.send('updateFiles')
|
|
|
|
window!.show()
|
|
|
|
window!.focus()
|
2017-11-27 19:21:12 -05:00
|
|
|
}
|
|
|
|
|
2017-12-23 06:46:20 -05:00
|
|
|
const uploadClipboardFiles = async () => {
|
2018-07-07 03:06:36 -04:00
|
|
|
let win
|
2019-12-19 06:17:21 -05:00
|
|
|
if (miniWindow!.isVisible()) {
|
2018-07-07 03:06:36 -04:00
|
|
|
win = miniWindow
|
|
|
|
} else {
|
2019-04-14 03:41:48 -04:00
|
|
|
win = settingWindow || window || createSettingWindow()
|
2018-07-07 03:06:36 -04:00
|
|
|
}
|
2019-12-26 07:15:41 -05:00
|
|
|
let img = await uploader.setWebContents(win!.webContents).upload()
|
2017-12-23 06:46:20 -05:00
|
|
|
if (img !== false) {
|
2018-01-30 02:20:19 -05:00
|
|
|
if (img.length > 0) {
|
2019-09-11 07:30:08 -04:00
|
|
|
const pasteStyle = db.get('settings.pasteStyle') || 'markdown'
|
2019-04-18 05:25:10 -04:00
|
|
|
clipboard.writeText(pasteTemplate(pasteStyle, img[0]))
|
2018-01-30 02:20:19 -05:00
|
|
|
const notification = new Notification({
|
|
|
|
title: '上传成功',
|
2019-12-19 06:17:21 -05:00
|
|
|
body: img[0].imgUrl!,
|
2018-01-30 02:20:19 -05:00
|
|
|
icon: img[0].imgUrl
|
|
|
|
})
|
|
|
|
notification.show()
|
2019-09-11 07:30:08 -04:00
|
|
|
db.insert('uploaded', img[0])
|
2019-12-19 06:17:21 -05:00
|
|
|
window!.webContents.send('clipboardFiles', [])
|
|
|
|
window!.webContents.send('uploadFiles', img)
|
2018-08-07 23:21:08 -04:00
|
|
|
if (settingWindow) {
|
|
|
|
settingWindow.webContents.send('updateGallery')
|
|
|
|
}
|
2018-01-30 02:20:19 -05:00
|
|
|
} else {
|
|
|
|
const notification = new Notification({
|
|
|
|
title: '上传不成功',
|
|
|
|
body: '你剪贴板最新的一条记录不是图片哦'
|
|
|
|
})
|
|
|
|
notification.show()
|
|
|
|
}
|
2017-12-23 06:46:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-21 04:28:29 -05:00
|
|
|
const uploadChoosedFiles = async (webContents: WebContents, files: IFileWithPath[]) => {
|
2019-04-14 03:41:48 -04:00
|
|
|
const input = files.map(item => item.path)
|
2019-12-26 07:15:41 -05:00
|
|
|
const imgs = await uploader.setWebContents(webContents).upload(input)
|
2019-04-14 03:41:48 -04:00
|
|
|
if (imgs !== false) {
|
2019-09-11 07:30:08 -04:00
|
|
|
const pasteStyle = db.get('settings.pasteStyle') || 'markdown'
|
2019-04-14 03:41:48 -04:00
|
|
|
let pasteText = ''
|
2019-12-19 06:17:21 -05:00
|
|
|
for (let i = 0; i < imgs.length; i++) {
|
2019-04-18 05:25:10 -04:00
|
|
|
pasteText += pasteTemplate(pasteStyle, imgs[i]) + '\r\n'
|
2019-04-14 03:41:48 -04:00
|
|
|
const notification = new Notification({
|
|
|
|
title: '上传成功',
|
2019-12-19 06:17:21 -05:00
|
|
|
body: imgs[i].imgUrl!,
|
2019-04-14 03:41:48 -04:00
|
|
|
icon: files[i].path
|
|
|
|
})
|
|
|
|
setTimeout(() => {
|
|
|
|
notification.show()
|
|
|
|
}, i * 100)
|
2019-09-11 07:30:08 -04:00
|
|
|
db.insert('uploaded', imgs[i])
|
2019-04-14 03:41:48 -04:00
|
|
|
}
|
|
|
|
clipboard.writeText(pasteText)
|
2019-12-19 06:17:21 -05:00
|
|
|
window!.webContents.send('uploadFiles', imgs)
|
2019-04-14 03:41:48 -04:00
|
|
|
if (settingWindow) {
|
|
|
|
settingWindow.webContents.send('updateGallery')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-26 07:15:41 -05:00
|
|
|
picgoCoreIPC()
|
2018-09-18 06:48:55 -04:00
|
|
|
|
2018-12-27 22:10:28 -05:00
|
|
|
// from macOS tray
|
2019-12-19 06:17:21 -05:00
|
|
|
ipcMain.on('uploadClipboardFiles', async () => {
|
2019-12-26 07:15:41 -05:00
|
|
|
const img = await uploader.setWebContents(window!.webContents).upload()
|
2017-12-22 09:30:16 -05:00
|
|
|
if (img !== false) {
|
2019-09-11 07:30:08 -04:00
|
|
|
const pasteStyle = db.get('settings.pasteStyle') || 'markdown'
|
2019-04-18 05:25:10 -04:00
|
|
|
clipboard.writeText(pasteTemplate(pasteStyle, img[0]))
|
2017-12-22 09:30:16 -05:00
|
|
|
const notification = new Notification({
|
|
|
|
title: '上传成功',
|
2019-12-19 06:17:21 -05:00
|
|
|
body: img[0].imgUrl!,
|
2017-12-23 06:46:20 -05:00
|
|
|
// icon: file[0]
|
|
|
|
icon: img[0].imgUrl
|
2017-12-22 09:30:16 -05:00
|
|
|
})
|
|
|
|
notification.show()
|
2019-09-11 07:30:08 -04:00
|
|
|
db.insert('uploaded', img[0])
|
2019-12-19 06:17:21 -05:00
|
|
|
window!.webContents.send('clipboardFiles', [])
|
2018-08-07 23:21:08 -04:00
|
|
|
if (settingWindow) {
|
|
|
|
settingWindow.webContents.send('updateGallery')
|
|
|
|
}
|
2017-12-22 09:30:16 -05:00
|
|
|
}
|
2019-12-19 06:17:21 -05:00
|
|
|
window!.webContents.send('uploadFiles')
|
2017-11-28 10:56:15 -05:00
|
|
|
})
|
|
|
|
|
2018-01-30 02:20:19 -05:00
|
|
|
ipcMain.on('uploadClipboardFilesFromUploadPage', () => {
|
|
|
|
uploadClipboardFiles()
|
|
|
|
})
|
|
|
|
|
2019-12-21 04:28:29 -05:00
|
|
|
ipcMain.on('uploadChoosedFiles', async (evt: IpcMainEvent, files: IFileWithPath[]) => {
|
2019-04-15 22:45:09 -04:00
|
|
|
return uploadChoosedFiles(evt.sender, files)
|
2017-11-27 19:21:12 -05:00
|
|
|
})
|
|
|
|
|
2019-12-22 04:22:32 -05:00
|
|
|
ipcMain.on('updateShortKey', (evt: IpcMainEvent, item: IShortKeyConfig, oldKey: string, from: string) => {
|
|
|
|
const result = shortKeyHandler.updateShortKey(item, oldKey, from)
|
|
|
|
evt.sender.send('updateShortKeyResponse', result)
|
|
|
|
if (result) {
|
|
|
|
const notification = new Notification({
|
|
|
|
title: '操作成功',
|
|
|
|
body: '你的快捷键已经修改成功'
|
|
|
|
})
|
|
|
|
notification.show()
|
|
|
|
} else {
|
|
|
|
const notification = new Notification({
|
|
|
|
title: '操作失败',
|
|
|
|
body: '快捷键冲突,请重新设置'
|
|
|
|
})
|
|
|
|
notification.show()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
ipcMain.on('bindOrUnbindShortKey', (evt: IpcMainEvent, item: IShortKeyConfig, from: string) => {
|
|
|
|
const result = shortKeyHandler.bindOrUnbindShortKey(item, from)
|
|
|
|
if (result) {
|
|
|
|
const notification = new Notification({
|
|
|
|
title: '操作成功',
|
|
|
|
body: '你的快捷键已经修改成功'
|
|
|
|
})
|
|
|
|
notification.show()
|
|
|
|
} else {
|
|
|
|
const notification = new Notification({
|
|
|
|
title: '操作失败',
|
|
|
|
body: '快捷键冲突,请重新设置'
|
|
|
|
})
|
|
|
|
notification.show()
|
|
|
|
}
|
2018-01-30 02:20:19 -05:00
|
|
|
})
|
|
|
|
|
2019-12-19 06:17:21 -05:00
|
|
|
ipcMain.on('updateCustomLink', () => {
|
2018-03-09 01:35:41 -05:00
|
|
|
const notification = new Notification({
|
|
|
|
title: '操作成功',
|
|
|
|
body: '你的自定义链接格式已经修改成功'
|
|
|
|
})
|
|
|
|
notification.show()
|
|
|
|
})
|
|
|
|
|
2019-12-19 06:17:21 -05:00
|
|
|
ipcMain.on('autoStart', (evt: IpcMainEvent, val: boolean) => {
|
2018-05-02 07:34:24 -04:00
|
|
|
app.setLoginItemSettings({
|
|
|
|
openAtLogin: val
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-12-19 06:17:21 -05:00
|
|
|
ipcMain.on('openSettingWindow', () => {
|
2018-07-06 11:54:36 -04:00
|
|
|
if (!settingWindow) {
|
|
|
|
createSettingWindow()
|
|
|
|
} else {
|
|
|
|
settingWindow.show()
|
|
|
|
}
|
2019-12-19 06:17:21 -05:00
|
|
|
if (miniWindow) {
|
|
|
|
miniWindow.hide()
|
|
|
|
}
|
2018-07-06 11:54:36 -04:00
|
|
|
})
|
|
|
|
|
2019-12-19 06:17:21 -05:00
|
|
|
ipcMain.on('openMiniWindow', () => {
|
2018-07-06 11:54:36 -04:00
|
|
|
if (!miniWindow) {
|
|
|
|
createMiniWidow()
|
|
|
|
}
|
2019-12-19 06:17:21 -05:00
|
|
|
miniWindow!.show()
|
|
|
|
miniWindow!.focus()
|
|
|
|
settingWindow!.hide()
|
2018-07-06 11:54:36 -04:00
|
|
|
})
|
|
|
|
|
2018-07-10 10:37:29 -04:00
|
|
|
// from mini window
|
2019-12-19 06:17:21 -05:00
|
|
|
ipcMain.on('syncPicBed', () => {
|
2018-07-10 10:37:29 -04:00
|
|
|
if (settingWindow) {
|
|
|
|
settingWindow.webContents.send('syncPicBed')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-12-19 06:17:21 -05:00
|
|
|
ipcMain.on('getPicBeds', (evt: IpcMainEvent) => {
|
2019-12-26 07:15:41 -05:00
|
|
|
const picBeds = getPicBeds()
|
2018-12-23 10:15:00 -05:00
|
|
|
evt.sender.send('getPicBeds', picBeds)
|
2018-12-25 03:11:10 -05:00
|
|
|
evt.returnValue = picBeds
|
2018-12-23 10:15:00 -05:00
|
|
|
})
|
|
|
|
|
2019-12-19 06:17:21 -05:00
|
|
|
ipcMain.on('toggleShortKeyModifiedMode', (evt: IpcMainEvent, val: boolean) => {
|
2019-09-25 04:34:29 -04:00
|
|
|
bus.emit('toggleShortKeyModifiedMode', val)
|
2019-09-11 03:31:27 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
// const shortKeyHash = {
|
|
|
|
// upload: uploadClipboardFiles
|
|
|
|
// }
|
2018-01-30 02:20:19 -05:00
|
|
|
|
2018-12-25 04:28:12 -05:00
|
|
|
const gotTheLock = app.requestSingleInstanceLock()
|
|
|
|
|
|
|
|
if (!gotTheLock) {
|
|
|
|
app.quit()
|
|
|
|
} else {
|
2019-04-14 03:41:48 -04:00
|
|
|
app.on('second-instance', (event, commandLine, workingDirectory) => {
|
|
|
|
let files = getUploadFiles(commandLine, workingDirectory)
|
2019-04-14 22:28:11 -04:00
|
|
|
if (files === null || files.length > 0) { // 如果有文件列表作为参数,说明是命令行启动
|
|
|
|
if (files === null) {
|
|
|
|
uploadClipboardFiles()
|
2019-04-14 03:41:48 -04:00
|
|
|
} else {
|
2019-04-14 22:28:11 -04:00
|
|
|
let win
|
|
|
|
if (miniWindow && miniWindow.isVisible()) {
|
|
|
|
win = miniWindow
|
|
|
|
} else {
|
|
|
|
win = settingWindow || window || createSettingWindow()
|
|
|
|
}
|
|
|
|
uploadChoosedFiles(win.webContents, files)
|
2019-04-14 03:41:48 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (settingWindow) {
|
|
|
|
if (settingWindow.isMinimized()) {
|
|
|
|
settingWindow.restore()
|
|
|
|
}
|
|
|
|
settingWindow.focus()
|
|
|
|
}
|
2018-01-09 22:31:07 -05:00
|
|
|
}
|
2019-04-14 03:41:48 -04:00
|
|
|
})
|
2018-01-09 22:31:07 -05:00
|
|
|
}
|
|
|
|
|
2018-01-10 03:51:09 -05:00
|
|
|
if (process.platform === 'win32') {
|
2019-12-19 06:17:21 -05:00
|
|
|
app.setAppUserModelId('com.molunerfinn.picgo')
|
2018-01-10 03:51:09 -05:00
|
|
|
}
|
|
|
|
|
2018-06-26 05:03:46 -04:00
|
|
|
if (process.env.XDG_CURRENT_DESKTOP && process.env.XDG_CURRENT_DESKTOP.includes('Unity')) {
|
|
|
|
process.env.XDG_CURRENT_DESKTOP = 'Unity'
|
|
|
|
}
|
|
|
|
|
2019-12-19 06:17:21 -05:00
|
|
|
app.on('ready', async () => {
|
2019-12-20 05:46:39 -05:00
|
|
|
createProtocol('picgo')
|
2019-12-19 06:17:21 -05:00
|
|
|
if (isDevelopment && !process.env.IS_TEST) {
|
|
|
|
// Install Vue Devtools
|
|
|
|
try {
|
|
|
|
await installVueDevtools()
|
|
|
|
} catch (e) {
|
|
|
|
console.error('Vue Devtools failed to install:', e.toString())
|
|
|
|
}
|
|
|
|
}
|
2017-11-27 19:21:12 -05:00
|
|
|
createWindow()
|
2018-06-26 05:03:46 -04:00
|
|
|
createSettingWindow()
|
|
|
|
if (process.platform === 'darwin' || process.platform === 'win32') {
|
|
|
|
createTray()
|
|
|
|
}
|
2019-09-11 07:30:08 -04:00
|
|
|
db.set('needReload', false)
|
2017-12-23 03:38:19 -05:00
|
|
|
updateChecker()
|
2019-09-10 02:38:08 -04:00
|
|
|
initEventCenter()
|
|
|
|
// 不需要阻塞
|
|
|
|
process.nextTick(() => {
|
2019-09-11 07:30:08 -04:00
|
|
|
updateShortKeyFromVersion212(db, db.get('settings.shortKey'))
|
2019-12-22 04:22:32 -05:00
|
|
|
shortKeyHandler.init()
|
2017-12-23 06:46:20 -05:00
|
|
|
})
|
2019-09-10 02:38:08 -04:00
|
|
|
|
2019-04-14 10:07:33 -04:00
|
|
|
if (process.env.NODE_ENV !== 'development') {
|
|
|
|
let files = getUploadFiles()
|
2019-12-20 05:46:39 -05:00
|
|
|
if (files === null) {
|
|
|
|
logger.info('get clipboardFile, null')
|
|
|
|
} else {
|
|
|
|
logger.info(`get clipboardFile, ${files.toString()}`)
|
|
|
|
}
|
2019-04-14 22:28:11 -04:00
|
|
|
if (files === null || files.length > 0) { // 如果有文件列表作为参数,说明是命令行启动
|
|
|
|
if (files === null) {
|
|
|
|
uploadClipboardFiles()
|
2019-04-14 10:07:33 -04:00
|
|
|
} else {
|
2019-04-14 22:28:11 -04:00
|
|
|
let win
|
|
|
|
if (miniWindow && miniWindow.isVisible()) {
|
|
|
|
win = miniWindow
|
|
|
|
} else {
|
|
|
|
win = settingWindow || window || createSettingWindow()
|
|
|
|
}
|
|
|
|
uploadChoosedFiles(win.webContents, files)
|
2019-04-14 10:07:33 -04:00
|
|
|
}
|
2019-04-14 03:41:48 -04:00
|
|
|
}
|
|
|
|
}
|
2017-11-27 19:21:12 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
app.on('window-all-closed', () => {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
app.quit()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
app.on('activate', () => {
|
2019-12-19 06:17:21 -05:00
|
|
|
createProtocol('picgo')
|
2018-06-26 05:03:46 -04:00
|
|
|
if (window === null) {
|
2017-11-27 19:21:12 -05:00
|
|
|
createWindow()
|
|
|
|
}
|
2018-06-26 05:03:46 -04:00
|
|
|
if (settingWindow === null) {
|
|
|
|
createSettingWindow()
|
|
|
|
}
|
2017-11-27 19:21:12 -05:00
|
|
|
})
|
|
|
|
|
2017-12-23 06:46:20 -05:00
|
|
|
app.on('will-quit', () => {
|
|
|
|
globalShortcut.unregisterAll()
|
2019-09-10 02:38:08 -04:00
|
|
|
bus.removeAllListeners()
|
2017-12-23 06:46:20 -05:00
|
|
|
})
|
|
|
|
|
2018-05-02 05:17:55 -04:00
|
|
|
app.setLoginItemSettings({
|
2019-09-11 07:30:08 -04:00
|
|
|
openAtLogin: db.get('settings.autoStart') || false
|
2018-05-02 05:17:55 -04:00
|
|
|
})
|
|
|
|
|
2019-09-10 02:38:08 -04:00
|
|
|
function initEventCenter () {
|
2019-12-19 06:17:21 -05:00
|
|
|
const eventList: any = {
|
2019-12-22 04:22:32 -05:00
|
|
|
'picgo:upload': uploadClipboardFiles,
|
|
|
|
'createSettingWindow': shortKeyRequestSettingWindow,
|
|
|
|
hideMiniWindow
|
2019-09-10 02:38:08 -04:00
|
|
|
}
|
|
|
|
for (let i in eventList) {
|
|
|
|
bus.on(i, eventList[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 04:22:32 -05:00
|
|
|
function shortKeyRequestSettingWindow (command: string) {
|
|
|
|
if (!settingWindow) createSettingWindow()
|
|
|
|
bus.emit('createSettingWindowDone', command, settingWindow!.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
function hideMiniWindow () {
|
|
|
|
if (miniWindow && miniWindow.isVisible()) {
|
|
|
|
miniWindow.hide()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-19 06:17:21 -05:00
|
|
|
// Exit cleanly on request from parent process in development mode.
|
|
|
|
if (isDevelopment) {
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
process.on('message', data => {
|
|
|
|
if (data === 'graceful-exit') {
|
|
|
|
app.quit()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
process.on('SIGTERM', () => {
|
|
|
|
app.quit()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-27 19:21:12 -05:00
|
|
|
/**
|
|
|
|
* Auto Updater
|
|
|
|
*
|
|
|
|
* Uncomment the following code below and install `electron-updater` to
|
|
|
|
* support auto updating. Code Signing with a valid certificate is required.
|
|
|
|
* https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
|
|
|
|
*/
|
|
|
|
|
|
|
|
// import { autoUpdater } from 'electron-updater'
|
|
|
|
|
|
|
|
// autoUpdater.on('update-downloaded', () => {
|
|
|
|
// autoUpdater.quitAndInstall()
|
|
|
|
// })
|
|
|
|
|
|
|
|
// app.on('ready', () => {
|
|
|
|
// if (process.env.NODE_ENV === 'production') {
|
|
|
|
// autoUpdater.checkForUpdates()
|
|
|
|
// }
|
|
|
|
// })
|