mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-06-07 16:50:10 +08:00
⚡ Perf(custom): reduce idle memory usage by 60 percent
This commit is contained in:
@@ -109,8 +109,7 @@ export function createMenu() {
|
||||
export function createContextMenu() {
|
||||
const ClipboardWatcher = clipboardPoll
|
||||
const isListeningClipboard = picgo.getConfig<boolean | undefined>(configPaths.settings.isListeningClipboard) || false
|
||||
const isMiniWindowVisible =
|
||||
windowManager.has(IWindowList.MINI_WINDOW) && windowManager.get(IWindowList.MINI_WINDOW)!.isVisible()
|
||||
const isMiniWindowVisible = windowManager.get(IWindowList.MINI_WINDOW)?.isVisible() || false
|
||||
|
||||
const startWatchClipboard = () => {
|
||||
picgo.saveConfig({ [configPaths.settings.isListeningClipboard]: true })
|
||||
@@ -236,11 +235,9 @@ export function createTray(tooltip: string) {
|
||||
// click事件在Mac和Windows上可以触发(在Ubuntu上无法触发,Unity不支持)
|
||||
if (process.platform === 'darwin' || process.platform === 'win32') {
|
||||
tray.on('right-click', () => {
|
||||
if (windowManager.has(IWindowList.TRAY_WINDOW)) {
|
||||
windowManager.get(IWindowList.TRAY_WINDOW)!.hide()
|
||||
}
|
||||
windowManager.get(IWindowList.TRAY_WINDOW)?.hide()
|
||||
createContextMenu()
|
||||
tray!.popUpContextMenu(contextMenu!)
|
||||
tray?.popUpContextMenu(contextMenu!)
|
||||
})
|
||||
|
||||
tray.on('click', (_, bounds) => {
|
||||
@@ -277,33 +274,29 @@ export function createTray(tooltip: string) {
|
||||
})
|
||||
}
|
||||
}
|
||||
windowManager.get(IWindowList.TRAY_WINDOW)!.webContents.send('clipboardFiles', obj)
|
||||
windowManager.get(IWindowList.TRAY_WINDOW)?.webContents.send('clipboardFiles', obj)
|
||||
}, 0)
|
||||
} else {
|
||||
if (windowManager.has(IWindowList.TRAY_WINDOW)) {
|
||||
windowManager.get(IWindowList.TRAY_WINDOW)!.hide()
|
||||
}
|
||||
const settingWindow = windowManager.get(IWindowList.SETTING_WINDOW)
|
||||
windowManager.get(IWindowList.TRAY_WINDOW)?.hide()
|
||||
const autoCloseMiniWindow =
|
||||
picgo.getConfig<boolean | undefined>(configPaths.settings.autoCloseMiniWindow) || false
|
||||
settingWindow!.show()
|
||||
settingWindow!.focus()
|
||||
if (windowManager.has(IWindowList.MINI_WINDOW) && autoCloseMiniWindow) {
|
||||
windowManager.get(IWindowList.MINI_WINDOW)!.hide()
|
||||
if (autoCloseMiniWindow) {
|
||||
windowManager.get(IWindowList.MINI_WINDOW)?.close()
|
||||
}
|
||||
windowManager.create(IWindowList.SETTING_WINDOW)
|
||||
}
|
||||
})
|
||||
|
||||
tray.on('drag-enter', () => {
|
||||
if (nativeTheme.shouldUseDarkColors) {
|
||||
tray!.setImage(uploadDarkPng)
|
||||
tray?.setImage(uploadDarkPng)
|
||||
} else {
|
||||
tray!.setImage(uploadPng)
|
||||
tray?.setImage(uploadPng)
|
||||
}
|
||||
})
|
||||
|
||||
tray.on('drag-end', () => {
|
||||
tray!.setImage(getTrayIcon())
|
||||
tray?.setImage(getTrayIcon())
|
||||
})
|
||||
|
||||
// drop-files only be supported in macOS
|
||||
@@ -313,8 +306,8 @@ export function createTray(tooltip: string) {
|
||||
const allConfig = picgo.getConfig<any>() || {}
|
||||
const pasteStyle = allConfig.settings?.pasteStyle || IPasteStyle.MARKDOWN
|
||||
const rawInput = cloneDeep(files)
|
||||
const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)!
|
||||
const res = await uploader.setWebContents(trayWindow.webContents).uploadReturnCtx(files)
|
||||
const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)
|
||||
const res = await uploader.setWebContents(trayWindow?.webContents).uploadReturnCtx(files)
|
||||
const imgs = res[0] ? res[0] : false
|
||||
const backImgs = res[1] ? res[1] : false
|
||||
const deleteLocalFile = allConfig.settings?.deleteLocalFile || false
|
||||
@@ -344,33 +337,35 @@ export function createTray(tooltip: string) {
|
||||
await GalleryDB.getInstance().insert(imgs[i])
|
||||
}
|
||||
handleCopyUrl(pasteText.join('\n'))
|
||||
trayWindow.webContents.send('dragFiles', imgs)
|
||||
trayWindow?.webContents.send('dragFiles', imgs)
|
||||
}
|
||||
if (backImgs !== false) {
|
||||
for (const backImg of backImgs) {
|
||||
await GalleryDB.getInstance().insert(backImg)
|
||||
}
|
||||
trayWindow.webContents.send('dragFiles', backImgs)
|
||||
trayWindow?.webContents.send('dragFiles', backImgs)
|
||||
}
|
||||
})
|
||||
}
|
||||
// toggleWindow()
|
||||
} else if (process.platform === 'linux') {
|
||||
// click事件在Ubuntu上无法触发,Unity不支持(在Mac和Windows上可以触发)
|
||||
// 需要使用 setContextMenu 设置菜单
|
||||
createContextMenu()
|
||||
tray!.setContextMenu(contextMenu)
|
||||
tray?.setContextMenu(contextMenu)
|
||||
}
|
||||
}
|
||||
|
||||
const toggleWindow = (bounds: IBounds) => {
|
||||
const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)!
|
||||
if (trayWindow.isVisible()) {
|
||||
let trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)
|
||||
if (!trayWindow) {
|
||||
trayWindow = windowManager.create(IWindowList.TRAY_WINDOW)
|
||||
}
|
||||
if (trayWindow?.isVisible()) {
|
||||
trayWindow.hide()
|
||||
} else {
|
||||
trayWindow.setPosition(bounds.x - 98 + 11, bounds.y, false)
|
||||
trayWindow.webContents.send('updateFiles')
|
||||
trayWindow.show()
|
||||
trayWindow.focus()
|
||||
trayWindow?.setPosition(bounds.x - 98 + 11, bounds.y, false)
|
||||
trayWindow?.webContents.send('updateFiles')
|
||||
trayWindow?.show()
|
||||
trayWindow?.focus()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,13 +3,14 @@ import path from 'node:path'
|
||||
import { themesDir } from '@core/datastore/dirs'
|
||||
import * as fsWalk from '@nodelib/fs.walk'
|
||||
import AdmZip from 'adm-zip'
|
||||
import windowManager from 'apis/app/window/windowManager'
|
||||
import axios from 'axios'
|
||||
import fs from 'fs-extra'
|
||||
|
||||
import { randomStringGenerator } from '@/manage/utils/common'
|
||||
import { IWindowList } from '~/utils/enum'
|
||||
|
||||
import windowManager from '../window/windowManager'
|
||||
|
||||
let insertedCSSKeyMain: string | undefined
|
||||
|
||||
export async function resolveThemes(): Promise<{ key: string; label: string }[]> {
|
||||
@@ -75,12 +76,11 @@ export async function readTheme(theme: string): Promise<string> {
|
||||
export async function applyTheme(theme: string): Promise<void> {
|
||||
theme = path.basename(theme)
|
||||
const css = await readTheme(theme)
|
||||
if (windowManager.has(IWindowList.SETTING_WINDOW)) {
|
||||
try {
|
||||
await windowManager.get(IWindowList.SETTING_WINDOW)?.webContents.removeInsertedCSS(insertedCSSKeyMain || '')
|
||||
insertedCSSKeyMain = await windowManager.get(IWindowList.SETTING_WINDOW)?.webContents.insertCSS(css)
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
try {
|
||||
const window = windowManager.get(IWindowList.SETTING_WINDOW)
|
||||
await window?.webContents.removeInsertedCSS(insertedCSSKeyMain || '')
|
||||
insertedCSSKeyMain = await window?.webContents.insertCSS(css)
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,9 +17,9 @@ const handleClipboardUploadingReturnCtx = async (img?: IUploadOption): Promise<(
|
||||
const useBuiltinClipboard = useBuiltinClipboardConfig === undefined ? true : !!useBuiltinClipboardConfig
|
||||
const win = windowManager.getAvailableWindow()
|
||||
if (useBuiltinClipboard) {
|
||||
return await uploader.setWebContents(win!.webContents).uploadWithBuildInClipboardReturnCtx(img)
|
||||
return await uploader.setWebContents(win?.webContents).uploadWithBuildInClipboardReturnCtx(img)
|
||||
}
|
||||
return await uploader.setWebContents(win!.webContents).uploadReturnCtx(img)
|
||||
return await uploader.setWebContents(win?.webContents).uploadReturnCtx(img)
|
||||
}
|
||||
|
||||
export const uploadClipboardFiles = async (): Promise<IStringKeyMap> => {
|
||||
@@ -31,7 +31,6 @@ export const uploadClipboardFiles = async (): Promise<IStringKeyMap> => {
|
||||
const allConfig = picgo.getConfig<any>() || {}
|
||||
if (img !== false) {
|
||||
if (img.length > 0) {
|
||||
const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)
|
||||
const pasteStyle = allConfig.settings?.pasteStyle || IPasteStyle.MARKDOWN
|
||||
const [pastedText, shortUrl] = await pasteTemplate(pasteStyle, img[0], allConfig.settings?.customLink)
|
||||
img[0].shortUrl = shortUrl
|
||||
@@ -52,17 +51,15 @@ export const uploadClipboardFiles = async (): Promise<IStringKeyMap> => {
|
||||
}
|
||||
const inserted = await GalleryDB.getInstance().insert(img[0])
|
||||
// trayWindow just be created in mac/windows, not in linux
|
||||
const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)
|
||||
trayWindow?.webContents?.send('clipboardFiles', [])
|
||||
trayWindow?.webContents?.send('uploadFiles')
|
||||
if (windowManager.has(IWindowList.SETTING_WINDOW)) {
|
||||
windowManager.get(IWindowList.SETTING_WINDOW)!.webContents?.send('updateGallery')
|
||||
}
|
||||
const settingWindow = windowManager.get(IWindowList.SETTING_WINDOW)
|
||||
settingWindow?.webContents?.send('updateGallery')
|
||||
if (backImg !== false) {
|
||||
await GalleryDB.getInstance().insert(backImg[0])
|
||||
trayWindow?.webContents?.send('uploadFiles')
|
||||
if (windowManager.has(IWindowList.SETTING_WINDOW)) {
|
||||
windowManager.get(IWindowList.SETTING_WINDOW)!.webContents?.send('updateGallery')
|
||||
}
|
||||
settingWindow?.webContents?.send('updateGallery')
|
||||
}
|
||||
return {
|
||||
url: handleUrlEncodeWithSetting(inserted.imgUrl as string),
|
||||
@@ -88,7 +85,7 @@ export const uploadClipboardFiles = async (): Promise<IStringKeyMap> => {
|
||||
}
|
||||
|
||||
export const uploadChoosedFiles = async (
|
||||
webContents: WebContents,
|
||||
webContents: WebContents | undefined,
|
||||
files: IFileWithPath[],
|
||||
): Promise<IStringKeyMap[]> => {
|
||||
const input = files.map(item => item.path)
|
||||
@@ -150,18 +147,16 @@ export const uploadChoosedFiles = async (
|
||||
}
|
||||
handleCopyUrl(pasteText.join('\n'))
|
||||
// trayWindow just be created in mac/windows, not in linux
|
||||
windowManager.get(IWindowList.TRAY_WINDOW)?.webContents?.send('uploadFiles')
|
||||
if (windowManager.has(IWindowList.SETTING_WINDOW)) {
|
||||
windowManager.get(IWindowList.SETTING_WINDOW)!.webContents?.send('updateGallery')
|
||||
}
|
||||
const trayWindow = windowManager.get(IWindowList.TRAY_WINDOW)
|
||||
trayWindow?.webContents?.send('uploadFiles')
|
||||
const settingWindow = windowManager.get(IWindowList.SETTING_WINDOW)
|
||||
settingWindow?.webContents?.send('updateGallery')
|
||||
if (backImgs !== false) {
|
||||
for (const backImg of backImgs) {
|
||||
await GalleryDB.getInstance().insert(backImg)
|
||||
}
|
||||
windowManager.get(IWindowList.TRAY_WINDOW)?.webContents?.send('uploadFiles')
|
||||
if (windowManager.has(IWindowList.SETTING_WINDOW)) {
|
||||
windowManager.get(IWindowList.SETTING_WINDOW)!.webContents?.send('updateGallery')
|
||||
}
|
||||
trayWindow?.webContents?.send('uploadFiles')
|
||||
settingWindow?.webContents?.send('updateGallery')
|
||||
}
|
||||
return result
|
||||
} else {
|
||||
|
||||
@@ -17,22 +17,22 @@ import { configPaths } from '~/utils/configPaths'
|
||||
import { ICOREBuildInEvent, IWindowList } from '~/utils/enum'
|
||||
import { CLIPBOARD_IMAGE_FOLDER } from '~/utils/static'
|
||||
|
||||
const waitForRename = (window: BrowserWindow, id: number): Promise<string | null> => {
|
||||
const waitForRename = (window: BrowserWindow | undefined, id: number | undefined): Promise<string | null> => {
|
||||
return new Promise(resolve => {
|
||||
ipcMain.once(`${RENAME_FILE_NAME}${id}`, (_: IpcMainEvent, newName: string) => {
|
||||
resolve(newName)
|
||||
window.close()
|
||||
window?.close()
|
||||
})
|
||||
window.on('close', () => {
|
||||
window?.on('close', () => {
|
||||
resolve(null)
|
||||
ipcMain.removeAllListeners(`${RENAME_FILE_NAME}${id}`)
|
||||
windowManager.deleteById(window.id)
|
||||
windowManager.deleteById(window?.id)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
class Uploader {
|
||||
private webContents: WebContents | null = null
|
||||
private webContents: WebContents | undefined = undefined
|
||||
|
||||
constructor() {
|
||||
this.init()
|
||||
@@ -77,10 +77,10 @@ class Uploader {
|
||||
? `${dayjs().add(index, 'ms').format('YYYYMMDDHHmmssSSS')}${item.extname}`
|
||||
: item.fileName
|
||||
if (rename) {
|
||||
const window = windowManager.create(IWindowList.RENAME_WINDOW)!
|
||||
const window = windowManager.create(IWindowList.RENAME_WINDOW)
|
||||
ipcMain.on(GET_RENAME_FILE_NAME, (evt, _) => {
|
||||
try {
|
||||
if (evt.sender.id === window.webContents.id) {
|
||||
if (evt.sender.id === window?.webContents.id) {
|
||||
logger.info('rename window ready, wait for rename...')
|
||||
window.webContents.send(RENAME_FILE_NAME, fileName, item.fileName, window.webContents.id)
|
||||
}
|
||||
@@ -88,7 +88,7 @@ class Uploader {
|
||||
logger.error(e)
|
||||
}
|
||||
})
|
||||
name = await waitForRename(window, window.webContents.id)
|
||||
name = await waitForRename(window, window?.webContents.id)
|
||||
}
|
||||
item.fileName = name || fileName
|
||||
}),
|
||||
@@ -98,7 +98,7 @@ class Uploader {
|
||||
})
|
||||
}
|
||||
|
||||
setWebContents(webContents: WebContents) {
|
||||
setWebContents(webContents: WebContents | undefined) {
|
||||
this.webContents = webContents
|
||||
return this
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ import { configPaths } from '~/utils/configPaths'
|
||||
import { IWindowList } from '~/utils/enum'
|
||||
|
||||
import logo from '../../../../../resources/logo.png?asset&asarUnpack'
|
||||
import { applyTheme } from '../theme'
|
||||
|
||||
const windowList = new Map<string, IWindowListItem>()
|
||||
|
||||
@@ -182,7 +181,10 @@ windowList.set(IWindowList.TRAY_WINDOW, {
|
||||
window.loadFile(path.join(dirname, '../renderer/index.html'))
|
||||
}
|
||||
window.on('blur', () => {
|
||||
window.hide()
|
||||
window.close()
|
||||
})
|
||||
window.on('closed', () => {
|
||||
window = null as unknown as Electron.BrowserWindow
|
||||
})
|
||||
},
|
||||
})
|
||||
@@ -191,7 +193,7 @@ windowList.set(IWindowList.SETTING_WINDOW, {
|
||||
isValid: true,
|
||||
multiple: false,
|
||||
options: () => settingWindowOptions,
|
||||
callback(window, windowManager) {
|
||||
callback(window) {
|
||||
if (!app.isPackaged && process.env.ELECTRON_RENDERER_URL) {
|
||||
window.loadURL(`${process.env.ELECTRON_RENDERER_URL}#main-page/upload`)
|
||||
} else {
|
||||
@@ -201,18 +203,13 @@ windowList.set(IWindowList.SETTING_WINDOW, {
|
||||
}
|
||||
window.on('closed', () => {
|
||||
bus.emit(TOGGLE_SHORTKEY_MODIFIED_MODE, false)
|
||||
if (process.platform === 'linux') {
|
||||
process.nextTick(() => {
|
||||
app.quit()
|
||||
})
|
||||
}
|
||||
window = null as unknown as Electron.BrowserWindow
|
||||
})
|
||||
window.on('ready-to-show', () => {
|
||||
const customTheme = picgo.getConfig<string>(configPaths.settings.theme) || 'default.css'
|
||||
applyTheme(customTheme)
|
||||
window.once('ready-to-show', async () => {
|
||||
window.show()
|
||||
window.focus()
|
||||
})
|
||||
bus.emit(CREATE_APP_MENU)
|
||||
windowManager.create(IWindowList.MINI_WINDOW)
|
||||
},
|
||||
})
|
||||
|
||||
@@ -220,7 +217,8 @@ windowList.set(IWindowList.MINI_WINDOW, {
|
||||
isValid: process.platform !== 'darwin',
|
||||
multiple: false,
|
||||
options: () => miniWindowOptions,
|
||||
callback(window) {
|
||||
callback(window, windowManager) {
|
||||
const id = window.id
|
||||
if (!app.isPackaged && process.env.ELECTRON_RENDERER_URL) {
|
||||
window.loadURL(`${process.env.ELECTRON_RENDERER_URL}#mini-page`)
|
||||
} else {
|
||||
@@ -228,6 +226,10 @@ windowList.set(IWindowList.MINI_WINDOW, {
|
||||
hash: 'mini-page',
|
||||
})
|
||||
}
|
||||
window.on('closed', () => {
|
||||
windowManager.deleteById(id)
|
||||
window = null as unknown as Electron.BrowserWindow
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
@@ -250,6 +252,9 @@ windowList.set(IWindowList.RENAME_WINDOW, {
|
||||
const positionY = Math.floor(y + height / 2 - (height > 400 ? 88 : 0))
|
||||
window.setPosition(positionX, positionY, false)
|
||||
}
|
||||
window.on('closed', () => {
|
||||
window = null as unknown as Electron.BrowserWindow
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
@@ -272,6 +277,13 @@ windowList.set(IWindowList.TOOLBOX_WINDOW, {
|
||||
const positionY = Math.floor(y + height / 2 - (height > 400 ? 225 : 0))
|
||||
window.setPosition(positionX, positionY, false)
|
||||
}
|
||||
window.once('ready-to-show', () => {
|
||||
window.show()
|
||||
window.focus()
|
||||
})
|
||||
window.on('closed', () => {
|
||||
window = null as unknown as Electron.BrowserWindow
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
@@ -322,6 +334,9 @@ windowList.set(IWindowList.UPDATE_WINDOW, {
|
||||
const positionY = Math.floor(y + height / 2 - 300)
|
||||
window.setPosition(positionX, positionY, false)
|
||||
}
|
||||
window.on('closed', () => {
|
||||
window = null as unknown as Electron.BrowserWindow
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
@@ -9,10 +9,15 @@ class WindowManager implements IWindowManager {
|
||||
|
||||
create(name: string) {
|
||||
const windowConfig: IWindowListItem = windowList.get(name)!
|
||||
if (!windowConfig.isValid) return null
|
||||
if (!windowConfig.isValid) return undefined
|
||||
|
||||
if (!windowConfig.multiple) {
|
||||
if (this.has(name)) return this.#windowMap.get(name)!
|
||||
const existingWin = this.#windowMap.get(name)
|
||||
if (existingWin) {
|
||||
if (existingWin.isMinimized()) existingWin.restore()
|
||||
existingWin.focus()
|
||||
return existingWin
|
||||
}
|
||||
}
|
||||
|
||||
const window = new BrowserWindow(windowConfig.options())
|
||||
@@ -30,17 +35,15 @@ class WindowManager implements IWindowManager {
|
||||
}
|
||||
|
||||
get(name: string) {
|
||||
if (this.has(name)) {
|
||||
return this.#windowMap.get(name)!
|
||||
}
|
||||
return this.create(name)
|
||||
return this.#windowMap.get(name) || undefined
|
||||
}
|
||||
|
||||
has(name: string) {
|
||||
return this.#windowMap.has(name)
|
||||
}
|
||||
|
||||
deleteById = (id: number) => {
|
||||
deleteById = (id: number | undefined) => {
|
||||
if (id === undefined) return
|
||||
const name = this.#windowIdMap.get(id)
|
||||
if (name) {
|
||||
this.#windowMap.delete(name)
|
||||
@@ -59,8 +62,7 @@ class WindowManager implements IWindowManager {
|
||||
|
||||
const trayWindow = this.#windowMap.get(IWindowList.TRAY_WINDOW)
|
||||
if (trayWindow) return trayWindow
|
||||
|
||||
return this.create(IWindowList.SETTING_WINDOW)!
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user