2022-08-25 08:04:50 -04:00
|
|
|
|
import fs from 'fs-extra'
|
2020-01-10 09:34:08 -05:00
|
|
|
|
import {
|
|
|
|
|
app,
|
|
|
|
|
Menu,
|
|
|
|
|
Tray,
|
|
|
|
|
dialog,
|
|
|
|
|
clipboard,
|
|
|
|
|
systemPreferences,
|
|
|
|
|
Notification
|
|
|
|
|
} from 'electron'
|
2020-04-10 11:28:46 -04:00
|
|
|
|
import uploader from 'apis/app/uploader'
|
2021-07-26 23:58:24 -04:00
|
|
|
|
import db, { GalleryDB } from '~/main/apis/core/datastore'
|
2020-04-10 11:28:46 -04:00
|
|
|
|
import windowManager from 'apis/app/window/windowManager'
|
2022-01-08 02:44:09 -05:00
|
|
|
|
import { IWindowList } from '#/types/enum'
|
2022-01-04 10:40:28 -05:00
|
|
|
|
import pasteTemplate from '~/main/utils/pasteTemplate'
|
2020-01-10 09:34:08 -05:00
|
|
|
|
import pkg from 'root/package.json'
|
2022-08-25 08:04:50 -04:00
|
|
|
|
import { ensureFilePath, handleCopyUrl } from '~/main/utils/common'
|
2021-04-10 09:01:55 -04:00
|
|
|
|
import { privacyManager } from '~/main/utils/privacyManager'
|
2022-08-20 04:44:55 -04:00
|
|
|
|
// import { T } from '#/i18n'
|
|
|
|
|
import { T } from '~/main/i18n'
|
2022-08-07 10:34:26 -04:00
|
|
|
|
import { isMacOSVersionGreaterThanOrEqualTo } from '~/main/utils/getMacOSVersion'
|
2022-12-31 07:44:19 -05:00
|
|
|
|
import { buildPicBedListMenu } from '~/main/events/remotes/menu'
|
2020-01-10 09:34:08 -05:00
|
|
|
|
let contextMenu: Menu | null
|
|
|
|
|
let menu: Menu | null
|
|
|
|
|
let tray: Tray | null
|
2021-03-28 05:44:07 -04:00
|
|
|
|
export function createContextMenu () {
|
|
|
|
|
if (process.platform === 'darwin' || process.platform === 'win32') {
|
2022-12-31 07:44:19 -05:00
|
|
|
|
const submenu = buildPicBedListMenu()
|
2020-12-28 20:59:50 -05:00
|
|
|
|
contextMenu = Menu.buildFromTemplate([
|
|
|
|
|
{
|
2022-01-09 21:20:56 -05:00
|
|
|
|
label: T('ABOUT'),
|
2021-03-28 05:44:07 -04:00
|
|
|
|
click () {
|
2020-12-28 20:59:50 -05:00
|
|
|
|
dialog.showMessageBox({
|
2023-01-16 10:27:33 -05:00
|
|
|
|
title: 'PicList',
|
|
|
|
|
message: 'PicList',
|
|
|
|
|
detail: `Version: ${pkg.version}\nAuthor: Kuingsmile\nGithub: https://github.com/Kuingsmile/PicList`
|
2020-12-28 20:59:50 -05:00
|
|
|
|
})
|
2020-01-10 09:34:08 -05:00
|
|
|
|
}
|
2020-12-28 20:59:50 -05:00
|
|
|
|
},
|
|
|
|
|
{
|
2022-01-09 21:20:56 -05:00
|
|
|
|
label: T('OPEN_MAIN_WINDOW'),
|
2021-03-28 05:44:07 -04:00
|
|
|
|
click () {
|
2020-12-28 20:59:50 -05:00
|
|
|
|
const settingWindow = windowManager.get(IWindowList.SETTING_WINDOW)
|
|
|
|
|
settingWindow!.show()
|
|
|
|
|
settingWindow!.focus()
|
|
|
|
|
if (windowManager.has(IWindowList.MINI_WINDOW)) {
|
|
|
|
|
windowManager.get(IWindowList.MINI_WINDOW)!.hide()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
2022-01-09 21:20:56 -05:00
|
|
|
|
label: T('CHOOSE_DEFAULT_PICBED'),
|
2020-12-28 20:59:50 -05:00
|
|
|
|
type: 'submenu',
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
submenu
|
|
|
|
|
},
|
2020-01-10 09:34:08 -05:00
|
|
|
|
// @ts-ignore
|
2020-12-28 20:59:50 -05:00
|
|
|
|
{
|
2022-01-09 21:20:56 -05:00
|
|
|
|
label: T('OPEN_UPDATE_HELPER'),
|
2020-12-28 20:59:50 -05:00
|
|
|
|
type: 'checkbox',
|
|
|
|
|
checked: db.get('settings.showUpdateTip'),
|
2021-03-28 05:44:07 -04:00
|
|
|
|
click () {
|
2020-12-28 20:59:50 -05:00
|
|
|
|
const value = db.get('settings.showUpdateTip')
|
|
|
|
|
db.set('settings.showUpdateTip', !value)
|
|
|
|
|
}
|
|
|
|
|
},
|
2021-04-10 09:01:55 -04:00
|
|
|
|
{
|
2022-01-09 21:20:56 -05:00
|
|
|
|
label: T('PRIVACY_AGREEMENT'),
|
2021-04-10 09:01:55 -04:00
|
|
|
|
click () {
|
|
|
|
|
privacyManager.show(false)
|
|
|
|
|
}
|
|
|
|
|
},
|
2020-12-28 20:59:50 -05:00
|
|
|
|
{
|
2022-01-09 21:20:56 -05:00
|
|
|
|
label: T('RELOAD_APP'),
|
2021-03-28 05:44:07 -04:00
|
|
|
|
click () {
|
2020-12-28 20:59:50 -05:00
|
|
|
|
app.relaunch()
|
|
|
|
|
app.exit(0)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
{
|
|
|
|
|
role: 'quit',
|
2022-01-09 21:20:56 -05:00
|
|
|
|
label: T('QUIT')
|
2020-01-10 09:34:08 -05:00
|
|
|
|
}
|
2020-12-28 20:59:50 -05:00
|
|
|
|
])
|
2021-03-28 05:44:07 -04:00
|
|
|
|
} else if (process.platform === 'linux') {
|
2020-12-28 20:59:50 -05:00
|
|
|
|
// TODO 图床选择功能
|
|
|
|
|
// 由于在Linux难以像在Mac和Windows上那样在点击时构造ContextMenu,
|
|
|
|
|
// 暂时取消这个选单,避免引起和设置中启用的图床不一致
|
|
|
|
|
|
|
|
|
|
// TODO 重启应用功能
|
|
|
|
|
// 目前的实现无法正常工作
|
|
|
|
|
|
|
|
|
|
contextMenu = Menu.buildFromTemplate([
|
|
|
|
|
{
|
2022-01-09 21:20:56 -05:00
|
|
|
|
label: T('OPEN_MAIN_WINDOW'),
|
2021-03-28 05:44:07 -04:00
|
|
|
|
click () {
|
2020-12-28 20:59:50 -05:00
|
|
|
|
const settingWindow = windowManager.get(IWindowList.SETTING_WINDOW)
|
|
|
|
|
settingWindow!.show()
|
|
|
|
|
settingWindow!.focus()
|
|
|
|
|
if (windowManager.has(IWindowList.MINI_WINDOW)) {
|
|
|
|
|
windowManager.get(IWindowList.MINI_WINDOW)!.hide()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
{
|
2022-01-09 21:20:56 -05:00
|
|
|
|
label: T('OPEN_UPDATE_HELPER'),
|
2020-12-28 20:59:50 -05:00
|
|
|
|
type: 'checkbox',
|
|
|
|
|
checked: db.get('settings.showUpdateTip'),
|
2021-03-28 05:44:07 -04:00
|
|
|
|
click () {
|
2020-12-28 20:59:50 -05:00
|
|
|
|
const value = db.get('settings.showUpdateTip')
|
|
|
|
|
db.set('settings.showUpdateTip', !value)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
2022-01-09 21:20:56 -05:00
|
|
|
|
label: T('ABOUT'),
|
2021-03-28 05:44:07 -04:00
|
|
|
|
click () {
|
2020-12-28 20:59:50 -05:00
|
|
|
|
dialog.showMessageBox({
|
2023-01-16 10:27:33 -05:00
|
|
|
|
title: 'PicList',
|
|
|
|
|
message: 'PicList',
|
2020-12-28 20:59:50 -05:00
|
|
|
|
buttons: ['Ok'],
|
2023-01-16 10:27:33 -05:00
|
|
|
|
detail: `Version: ${pkg.version}\nAuthor: Kuingsmile\nGithub: https://github.com/Kuingsmile/PicList`
|
2021-03-28 05:44:07 -04:00
|
|
|
|
})
|
2020-12-28 20:59:50 -05:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
{
|
|
|
|
|
role: 'quit',
|
2022-01-09 21:20:56 -05:00
|
|
|
|
label: T('QUIT')
|
2020-01-10 09:34:08 -05:00
|
|
|
|
}
|
2020-12-28 20:59:50 -05:00
|
|
|
|
])
|
|
|
|
|
}
|
2020-01-10 09:34:08 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-07 10:34:26 -04:00
|
|
|
|
const getTrayIcon = () => {
|
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
|
const isMacOSGreaterThan11 = isMacOSVersionGreaterThanOrEqualTo('11')
|
2022-10-20 22:46:54 -04:00
|
|
|
|
return isMacOSGreaterThan11 ? `${__static}/menubar-newdarwinTemplate.png` : `${__static}/menubar.png`
|
2022-08-07 10:34:26 -04:00
|
|
|
|
} else {
|
|
|
|
|
return `${__static}/menubar-nodarwin.png`
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-28 05:44:07 -04:00
|
|
|
|
export function createTray () {
|
2022-08-07 10:34:26 -04:00
|
|
|
|
const menubarPic = getTrayIcon()
|
2020-01-10 09:34:08 -05:00
|
|
|
|
tray = new Tray(menubarPic)
|
2020-12-28 20:59:50 -05:00
|
|
|
|
// click事件在Mac和Windows上可以触发(在Ubuntu上无法触发,Unity不支持)
|
2021-03-28 05:44:07 -04:00
|
|
|
|
if (process.platform === 'darwin' || process.platform === 'win32') {
|
2020-12-28 20:59:50 -05:00
|
|
|
|
tray.on('right-click', () => {
|
2020-01-10 09:34:08 -05:00
|
|
|
|
if (windowManager.has(IWindowList.TRAY_WINDOW)) {
|
|
|
|
|
windowManager.get(IWindowList.TRAY_WINDOW)!.hide()
|
|
|
|
|
}
|
2020-12-28 20:59:50 -05:00
|
|
|
|
createContextMenu()
|
|
|
|
|
tray!.popUpContextMenu(contextMenu!)
|
|
|
|
|
})
|
|
|
|
|
tray.on('click', (event, bounds) => {
|
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
|
toggleWindow(bounds)
|
2022-08-25 08:04:50 -04:00
|
|
|
|
setTimeout(async () => {
|
2022-01-04 10:40:28 -05:00
|
|
|
|
const img = clipboard.readImage()
|
|
|
|
|
const obj: ImgInfo[] = []
|
2020-12-28 20:59:50 -05:00
|
|
|
|
if (!img.isEmpty()) {
|
|
|
|
|
// 从剪贴板来的图片默认转为png
|
2022-08-25 08:04:50 -04:00
|
|
|
|
// https://github.com/electron/electron/issues/9035
|
|
|
|
|
const imgPath = clipboard.read('public.file-url')
|
|
|
|
|
if (imgPath) {
|
|
|
|
|
const decodePath = ensureFilePath(imgPath)
|
|
|
|
|
if (decodePath === imgPath) {
|
|
|
|
|
obj.push({
|
|
|
|
|
imgUrl: imgPath
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
if (decodePath !== '') {
|
|
|
|
|
// 带有中文的路径,无法直接被img.src所使用,会被转义
|
|
|
|
|
const base64 = await fs.readFile(decodePath.replace('file://', ''), { encoding: 'base64' })
|
|
|
|
|
obj.push({
|
|
|
|
|
imgUrl: `data:image/png;base64,${base64}`
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
const imgUrl = img.toDataURL()
|
|
|
|
|
// console.log(imgUrl)
|
|
|
|
|
obj.push({
|
|
|
|
|
width: img.getSize().width,
|
|
|
|
|
height: img.getSize().height,
|
|
|
|
|
imgUrl
|
|
|
|
|
})
|
|
|
|
|
}
|
2020-12-28 20:59:50 -05:00
|
|
|
|
}
|
|
|
|
|
windowManager.get(IWindowList.TRAY_WINDOW)!.webContents.send('clipboardFiles', obj)
|
|
|
|
|
}, 0)
|
|
|
|
|
} else {
|
|
|
|
|
if (windowManager.has(IWindowList.TRAY_WINDOW)) {
|
|
|
|
|
windowManager.get(IWindowList.TRAY_WINDOW)!.hide()
|
|
|
|
|
}
|
|
|
|
|
const settingWindow = windowManager.get(IWindowList.SETTING_WINDOW)
|
|
|
|
|
settingWindow!.show()
|
|
|
|
|
settingWindow!.focus()
|
|
|
|
|
if (windowManager.has(IWindowList.MINI_WINDOW)) {
|
|
|
|
|
windowManager.get(IWindowList.MINI_WINDOW)!.hide()
|
|
|
|
|
}
|
2020-01-10 09:34:08 -05:00
|
|
|
|
}
|
2020-12-28 20:59:50 -05:00
|
|
|
|
})
|
2020-01-10 09:34:08 -05:00
|
|
|
|
|
2020-12-28 20:59:50 -05:00
|
|
|
|
tray.on('drag-enter', () => {
|
|
|
|
|
if (systemPreferences.isDarkMode()) {
|
|
|
|
|
tray!.setImage(`${__static}/upload-dark.png`)
|
|
|
|
|
} else {
|
|
|
|
|
tray!.setImage(`${__static}/upload.png`)
|
|
|
|
|
}
|
|
|
|
|
})
|
2020-01-10 09:34:08 -05:00
|
|
|
|
|
2020-12-28 20:59:50 -05:00
|
|
|
|
tray.on('drag-end', () => {
|
|
|
|
|
tray!.setImage(`${__static}/menubar.png`)
|
|
|
|
|
})
|
2020-01-10 09:34:08 -05:00
|
|
|
|
|
2021-04-25 11:28:08 -04:00
|
|
|
|
// drop-files only be supported in macOS
|
|
|
|
|
// so the tray window must be available
|
2020-12-28 20:59:50 -05:00
|
|
|
|
tray.on('drop-files', async (event: Event, files: string[]) => {
|
|
|
|
|
const pasteStyle = db.get('settings.pasteStyle') || 'markdown'
|
|
|
|
|
const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)!
|
|
|
|
|
const imgs = await uploader
|
|
|
|
|
.setWebContents(trayWindow.webContents)
|
|
|
|
|
.upload(files)
|
|
|
|
|
if (imgs !== false) {
|
|
|
|
|
const pasteText: string[] = []
|
|
|
|
|
for (let i = 0; i < imgs.length; i++) {
|
2021-08-01 02:50:25 -04:00
|
|
|
|
pasteText.push(pasteTemplate(pasteStyle, imgs[i], db.get('settings.customLink')))
|
2020-12-28 20:59:50 -05:00
|
|
|
|
const notification = new Notification({
|
2022-01-11 08:50:29 -05:00
|
|
|
|
title: T('UPLOAD_SUCCEED'),
|
2022-12-30 22:29:36 -05:00
|
|
|
|
body: imgs[i].imgUrl!
|
|
|
|
|
// icon: files[i]
|
2020-12-28 20:59:50 -05:00
|
|
|
|
})
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
notification.show()
|
|
|
|
|
}, i * 100)
|
2021-07-26 23:58:24 -04:00
|
|
|
|
await GalleryDB.getInstance().insert(imgs[i])
|
2020-12-28 20:59:50 -05:00
|
|
|
|
}
|
|
|
|
|
handleCopyUrl(pasteText.join('\n'))
|
|
|
|
|
trayWindow.webContents.send('dragFiles', imgs)
|
2020-01-10 09:34:08 -05:00
|
|
|
|
}
|
2020-12-28 20:59:50 -05:00
|
|
|
|
})
|
|
|
|
|
// toggleWindow()
|
2021-03-28 05:44:07 -04:00
|
|
|
|
} else if (process.platform === 'linux') {
|
2020-12-28 20:59:50 -05:00
|
|
|
|
// click事件在Ubuntu上无法触发,Unity不支持(在Mac和Windows上可以触发)
|
|
|
|
|
// 需要使用 setContextMenu 设置菜单
|
|
|
|
|
createContextMenu()
|
|
|
|
|
tray!.setContextMenu(contextMenu)
|
|
|
|
|
}
|
2020-01-10 09:34:08 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-28 05:44:07 -04:00
|
|
|
|
export function createMenu () {
|
2020-01-10 09:34:08 -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',
|
2021-03-28 05:44:07 -04:00
|
|
|
|
click () {
|
2020-01-10 09:34:08 -05:00
|
|
|
|
app.quit()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}]
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
menu = Menu.buildFromTemplate(template)
|
|
|
|
|
Menu.setApplicationMenu(menu)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const toggleWindow = (bounds: IBounds) => {
|
|
|
|
|
const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)!
|
|
|
|
|
if (trayWindow.isVisible()) {
|
|
|
|
|
trayWindow.hide()
|
|
|
|
|
} else {
|
|
|
|
|
trayWindow.setPosition(bounds.x - 98 + 11, bounds.y, false)
|
|
|
|
|
trayWindow.webContents.send('updateFiles')
|
|
|
|
|
trayWindow.show()
|
|
|
|
|
trayWindow.focus()
|
|
|
|
|
}
|
|
|
|
|
}
|