Skip to content

Commit

Permalink
🐛 Fix: macos clipboard image can't show on tray page
Browse files Browse the repository at this point in the history
ISSUES CLOSED: #961
  • Loading branch information
Molunerfinn committed Aug 25, 2022
1 parent b6b2eea commit 20e38f4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
38 changes: 29 additions & 9 deletions src/main/apis/app/system/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'fs-extra'
import {
app,
Menu,
Expand All @@ -15,7 +16,7 @@ import { IWindowList } from '#/types/enum'
import picgo from '@core/picgo'
import pasteTemplate from '~/main/utils/pasteTemplate'
import pkg from 'root/package.json'
import { handleCopyUrl } from '~/main/utils/common'
import { ensureFilePath, handleCopyUrl } from '~/main/utils/common'
import { privacyManager } from '~/main/utils/privacyManager'
// import { T } from '#/i18n'
import { T } from '~/main/i18n'
Expand Down Expand Up @@ -173,18 +174,37 @@ export function createTray () {
tray.on('click', (event, bounds) => {
if (process.platform === 'darwin') {
toggleWindow(bounds)
setTimeout(() => {
setTimeout(async () => {
const img = clipboard.readImage()
const 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
})
// 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
})
}
}
windowManager.get(IWindowList.TRAY_WINDOW)!.webContents.send('clipboardFiles', obj)
}, 0)
Expand Down
3 changes: 2 additions & 1 deletion src/main/apis/app/window/windowList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ windowList.set(IWindowList.TRAY_WINDOW, {
nodeIntegration: !!process.env.ELECTRON_NODE_INTEGRATION,
contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION,
nodeIntegrationInWorker: true,
backgroundThrottling: false
backgroundThrottling: false,
webSecurity: false
}
}
},
Expand Down
17 changes: 17 additions & 0 deletions src/main/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'fs-extra'
import db from '~/main/apis/core/datastore'
import { clipboard, Notification, dialog } from 'electron'

Expand Down Expand Up @@ -69,3 +70,19 @@ export const calcDurationRange = (duration: number) => {
// max range
return 100000
}

/**
* macOS public.file-url will get encoded file path,
* so we need to decode it
*/
export const ensureFilePath = (filePath: string, prefix = 'file://'): string => {
filePath = filePath.replace(prefix, '')
if (fs.existsSync(filePath)) {
return `${prefix}${filePath}`
}
filePath = decodeURIComponent(filePath)
if (fs.existsSync(filePath)) {
return `${prefix}${filePath}`
}
return ''
}
1 change: 0 additions & 1 deletion src/renderer/pages/TrayPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ export default class extends Vue {
})
ipcRenderer.on('uploadFiles', async () => {
this.files = (await this.$$db.get<ImgInfo>({ orderBy: 'desc', limit: 5 })).data
console.log(this.files)
this.uploadFlag = false
})
ipcRenderer.on('updateFiles', () => {
Expand Down

0 comments on commit 20e38f4

Please sign in to comment.