escrcpy/electron/exposes/search/index.js
2024-05-15 11:56:56 +08:00

78 lines
1.5 KiB
JavaScript

import remote from '@electron/remote'
import { FindInPage } from 'electron-find-in-page'
import { primaryColor } from '$renderer/configs/index.js'
export default () => {
const theme = {
isDark: false,
}
let findInPage = null
async function open({ ...args } = {}) {
await create(args)
return findInPage.openFindWindow()
}
function close() {
remote.getCurrentWebContents().stopFindInPage('clearSelection')
if (!findInPage) {
return false
}
return findInPage.closeFindWindow()
}
async function update({ isDark = false, ...args } = {}) {
if (isDark === theme.isDark) {
return findInPage
}
try {
await findInPage.destroy()
}
catch (error) {
console.warn('error', error.message)
}
findInPage = null
return create({ ...args, isDark })
}
async function create({ ...args } = {}) {
if (findInPage) {
return update(args)
}
theme.isDark = args.isDark
findInPage = new FindInPage(remote.getCurrentWebContents(), {
...args,
preload: true,
inputFocusColor: primaryColor,
...(theme.isDark
? {
boxShadowColor: '#4C4D4F',
boxBgColor: '#262727',
inputColor: '#CFD3DC',
inputBgColor: '#141414',
textColor: '#CFD3DC',
textHoverBgColor: '#4C4D4F',
}
: {}),
})
console.log('findInPage', findInPage)
return findInPage
}
return {
open,
close,
}
}