mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-05-23 17:20:10 +08:00
🔨 Refactor: change macos clipboard watcher implementment
This commit is contained in:
48
src/main/utils/clipboardPoll.ts
Normal file
48
src/main/utils/clipboardPoll.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { clipboard, nativeImage } from 'electron'
|
||||
import { EventEmitter } from 'events'
|
||||
|
||||
class ClipboardWatcher extends EventEmitter {
|
||||
lastImage: Electron.NativeImage | null
|
||||
timer: NodeJS.Timeout | null
|
||||
|
||||
constructor () {
|
||||
super()
|
||||
this.lastImage = null
|
||||
this.timer = null
|
||||
}
|
||||
|
||||
startListening (watchDelay = 500) {
|
||||
if (this.timer) {
|
||||
clearInterval(this.timer)
|
||||
}
|
||||
|
||||
this.timer = setInterval(() => {
|
||||
const image = clipboard.readImage()
|
||||
if (image.isEmpty()) {
|
||||
return
|
||||
}
|
||||
|
||||
const dataUrl = image.toDataURL()
|
||||
const currentImage = nativeImage.createFromDataURL(dataUrl)
|
||||
|
||||
if (this.lastImage) {
|
||||
const lastDataUrl = this.lastImage.toDataURL()
|
||||
if (lastDataUrl === dataUrl) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
this.lastImage = currentImage
|
||||
this.emit('change', currentImage)
|
||||
}, watchDelay)
|
||||
}
|
||||
|
||||
stopListening () {
|
||||
if (this.timer) {
|
||||
clearInterval(this.timer)
|
||||
this.timer = null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new ClipboardWatcher()
|
||||
Reference in New Issue
Block a user