Skip to content

Commit

Permalink
fix: after the system restarts, the watching not works
Browse files Browse the repository at this point in the history
  • Loading branch information
showlotus committed Jan 15, 2025
1 parent b5456cf commit fd9bf39
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 42 deletions.
91 changes: 49 additions & 42 deletions electron/main/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,50 +50,57 @@ function getPublicDirectoryPath(files: string[]) {
return files[0].split('\\').slice(0, -1).join('\\')
}

NativeClipboard.watch(
(type: 'TEXT' | 'IMAGE' | 'FILE', data: any, source: string, app: string) => {
if (!shouldUpdateHistory) return
if (type === 'IMAGE') {
const img = clipboard.readImage()
const { width, height } = img.getSize()
data = {
url: img.toDataURL(),
width,
height,
content: ''
} as any
} else if (type === 'FILE') {
const files = clipboardFiles.readFiles()
if (files.length === 1) {
const stat = fs.lstatSync(files[0])
if (stat.isFile()) {
data = {
subType: 'file',
content: getFileName(files[0]),
path: files[0]
export function registerWatch() {
NativeClipboard.watch(
(
type: 'TEXT' | 'IMAGE' | 'FILE',
data: any,
source: string,
app: string
) => {
if (!shouldUpdateHistory) return
if (type === 'IMAGE') {
const img = clipboard.readImage()
const { width, height } = img.getSize()
data = {
url: img.toDataURL(),
width,
height,
content: ''
} as any
} else if (type === 'FILE') {
const files = clipboardFiles.readFiles()
if (files.length === 1) {
const stat = fs.lstatSync(files[0])
if (stat.isFile()) {
data = {
subType: 'file',
content: getFileName(files[0]),
path: files[0]
}
} else if (stat.isDirectory()) {
data = {
subType: 'folder',
content: getFileName(files[0]),
path: files[0]
}
}
} else if (stat.isDirectory()) {
} else {
data = {
subType: 'folder',
content: getFileName(files[0]),
path: files[0]
}
files,
path: getPublicDirectoryPath(files),
content: getFileName(getPublicDirectoryPath(files)),
subType: 'folder,file'
} as any
}
} else {
data = {
files,
path: getPublicDirectoryPath(files),
content: getFileName(getPublicDirectoryPath(files)),
subType: 'folder,file'
} as any
}
}

getWinWebContents().send('update-clipboard', {
type,
data,
source,
app
})
}
)
getWinWebContents().send('update-clipboard', {
type,
data,
source,
app
})
}
)
}
2 changes: 2 additions & 0 deletions electron/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { fileURLToPath } from 'node:url'

import {
currActiveWindowHandle,
registerWatch,
setShouldPaste,
shouldPaste,
updateCurrActiveWindowHandle
Expand Down Expand Up @@ -195,6 +196,7 @@ async function createWindow() {
app.whenReady().then(() => {
createWindow()
checkUpdate()
registerWatch()
})

app.on('window-all-closed', () => {
Expand Down

0 comments on commit fd9bf39

Please sign in to comment.