mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-05-22 00:30:33 +08:00
🐛 Fix(custom): wait for tary windows loading before send clipboard data
ISSUES CLOSED: #481
This commit is contained in:
@@ -244,39 +244,15 @@ export function createTray(tooltip: string) {
|
|||||||
tray.on('click', (_, bounds) => {
|
tray.on('click', (_, bounds) => {
|
||||||
if (process.platform === 'darwin') {
|
if (process.platform === 'darwin') {
|
||||||
toggleWindow(bounds)
|
toggleWindow(bounds)
|
||||||
setTimeout(async () => {
|
const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)
|
||||||
const img = clipboard.readImage()
|
if (!trayWindow) return
|
||||||
const obj: ImgInfo[] = []
|
if (trayWindow.webContents.isLoading()) {
|
||||||
if (!img.isEmpty()) {
|
trayWindow.webContents.once('did-finish-load', () => {
|
||||||
// 从剪贴板来的图片默认转为png
|
sendClipboardFiles()
|
||||||
// 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 {
|
} else {
|
||||||
if (decodePath !== '') {
|
sendClipboardFiles()
|
||||||
// 带有中文的路径,无法直接被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()
|
|
||||||
obj.push({
|
|
||||||
width: img.getSize().width,
|
|
||||||
height: img.getSize().height,
|
|
||||||
imgUrl,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
windowManager.get(IWindowList.TRAY_WINDOW)?.webContents.send('clipboardFiles', obj)
|
|
||||||
}, 0)
|
|
||||||
} else {
|
} else {
|
||||||
windowManager.get(IWindowList.TRAY_WINDOW)?.hide()
|
windowManager.get(IWindowList.TRAY_WINDOW)?.hide()
|
||||||
const autoCloseMiniWindow =
|
const autoCloseMiniWindow =
|
||||||
@@ -357,17 +333,60 @@ export function createTray(tooltip: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const toggleWindow = (bounds: IBounds) => {
|
function toggleWindow(bounds: IBounds) {
|
||||||
let trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)
|
let trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)
|
||||||
if (!trayWindow) {
|
if (!trayWindow) {
|
||||||
trayWindow = windowManager.create(IWindowList.TRAY_WINDOW)
|
trayWindow = windowManager.create(IWindowList.TRAY_WINDOW)
|
||||||
}
|
}
|
||||||
if (trayWindow?.isVisible()) {
|
if (!trayWindow) return
|
||||||
|
if (trayWindow.isVisible()) {
|
||||||
trayWindow.hide()
|
trayWindow.hide()
|
||||||
} else {
|
} else {
|
||||||
trayWindow?.setPosition(bounds.x - 98 + 11, bounds.y, false)
|
trayWindow.setPosition(bounds.x - 98 + 11, bounds.y, false)
|
||||||
trayWindow?.webContents.send('updateFiles')
|
if (trayWindow.webContents.isLoading()) {
|
||||||
trayWindow?.show()
|
trayWindow.webContents.once('did-finish-load', () => {
|
||||||
trayWindow?.focus()
|
trayWindow.webContents.send('updateFiles')
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
trayWindow.webContents.send('updateFiles')
|
||||||
|
}
|
||||||
|
trayWindow.show()
|
||||||
|
trayWindow.focus()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function sendClipboardFiles() {
|
||||||
|
const img = clipboard.readImage()
|
||||||
|
console.log('Clipboard image', img.isEmpty())
|
||||||
|
const obj: ImgInfo[] = []
|
||||||
|
if (!img.isEmpty()) {
|
||||||
|
// 从剪贴板来的图片默认转为png
|
||||||
|
// https://github.com/electron/electron/issues/9035
|
||||||
|
const imgPath = clipboard.read('public.file-url')
|
||||||
|
console.log(imgPath)
|
||||||
|
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()
|
||||||
|
obj.push({
|
||||||
|
width: img.getSize().width,
|
||||||
|
height: img.getSize().height,
|
||||||
|
imgUrl,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
windowManager.get(IWindowList.TRAY_WINDOW)?.webContents.send('clipboardFiles', obj)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user