🔨 Refactor: refactor of some codes

This commit is contained in:
萌萌哒赫萝 2023-06-28 06:57:17 -07:00
parent c5e4a58f1f
commit 6680434231
2 changed files with 135 additions and 135 deletions

View File

@ -17,9 +17,7 @@ import { T } from '~/main/i18n'
const windowList = new Map<IWindowList, IWindowListItem>() const windowList = new Map<IWindowList, IWindowListItem>()
const handleWindowParams = (windowURL: string) => { const handleWindowParams = (windowURL: string) => windowURL
return windowURL
}
const getDefaultWindowSizes = (): { width: number, height: number } => { const getDefaultWindowSizes = (): { width: number, height: number } => {
const mainWindowWidth = picgo.getConfig<any>('settings.mainWindowWidth') const mainWindowWidth = picgo.getConfig<any>('settings.mainWindowWidth')
@ -33,13 +31,9 @@ const getDefaultWindowSizes = (): { width: number, height: number } => {
const defaultWindowWidth = getDefaultWindowSizes().width const defaultWindowWidth = getDefaultWindowSizes().width
const defaultWindowHeight = getDefaultWindowSizes().height const defaultWindowHeight = getDefaultWindowSizes().height
windowList.set(IWindowList.TRAY_WINDOW, { const trayWindowOptions = {
isValid: process.platform !== 'linux',
multiple: false,
options () {
return {
height: 350, height: 350,
width: 196, // 196 width: 196,
show: false, show: false,
frame: false, frame: false,
fullscreenable: false, fullscreenable: false,
@ -54,20 +48,8 @@ windowList.set(IWindowList.TRAY_WINDOW, {
webSecurity: false webSecurity: false
} }
} }
},
callback (window) {
window.loadURL(handleWindowParams(TRAY_WINDOW_URL))
window.on('blur', () => {
window.hide()
})
}
})
windowList.set(IWindowList.SETTING_WINDOW, { const settingWindowOptions = {
isValid: true,
multiple: false,
options () {
const options: IBrowserWindowOptions = {
height: defaultWindowHeight, height: defaultWindowHeight,
width: defaultWindowWidth, width: defaultWindowWidth,
show: false, show: false,
@ -86,16 +68,102 @@ windowList.set(IWindowList.SETTING_WINDOW, {
nodeIntegrationInWorker: true, nodeIntegrationInWorker: true,
webSecurity: false webSecurity: false
} }
} } as IBrowserWindowOptions
if (process.platform !== 'darwin') { if (process.platform !== 'darwin') {
options.show = false settingWindowOptions.show = false
options.frame = false settingWindowOptions.frame = false
options.backgroundColor = '#3f3c37' settingWindowOptions.backgroundColor = '#3f3c37'
options.transparent = false settingWindowOptions.transparent = false
options.icon = `${__static}/logo.png` settingWindowOptions.icon = `${__static}/logo.png`
} }
return options
}, const miniWindowOptions = {
height: 64,
width: 64,
show: process.platform === 'linux',
frame: false,
fullscreenable: false,
skipTaskbar: true,
resizable: false,
transparent: process.platform !== 'linux',
icon: `${__static}/logo.png`,
webPreferences: {
backgroundThrottling: false,
nodeIntegration: !!process.env.ELECTRON_NODE_INTEGRATION,
contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION,
nodeIntegrationInWorker: true
}
} as IBrowserWindowOptions
if (db.get('settings.miniWindowOntop')) {
miniWindowOptions.alwaysOnTop = true
}
const renameWindowOptions = {
height: 175,
width: 300,
show: true,
fullscreenable: false,
resizable: false,
vibrancy: 'ultra-dark',
webPreferences: {
nodeIntegration: !!process.env.ELECTRON_NODE_INTEGRATION,
contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION,
nodeIntegrationInWorker: true,
backgroundThrottling: false
}
} as IBrowserWindowOptions
if (process.platform !== 'darwin') {
renameWindowOptions.show = true
renameWindowOptions.backgroundColor = '#3f3c37'
renameWindowOptions.autoHideMenuBar = true
renameWindowOptions.transparent = false
}
const toolboxWindowOptions = {
height: 450,
width: 800,
show: false,
frame: true,
center: true,
fullscreenable: false,
resizable: false,
title: `PicList ${T('TOOLBOX')}`,
vibrancy: 'ultra-dark',
icon: `${__static}/logo.png`,
webPreferences: {
backgroundThrottling: false,
nodeIntegration: !!process.env.ELECTRON_NODE_INTEGRATION,
contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION,
nodeIntegrationInWorker: true,
webSecurity: false
}
} as IBrowserWindowOptions
if (process.platform !== 'darwin') {
toolboxWindowOptions.backgroundColor = '#3f3c37'
toolboxWindowOptions.autoHideMenuBar = true
toolboxWindowOptions.transparent = false
}
windowList.set(IWindowList.TRAY_WINDOW, {
isValid: process.platform !== 'linux',
multiple: false,
options: () => trayWindowOptions,
callback (window) {
window.loadURL(handleWindowParams(TRAY_WINDOW_URL))
window.on('blur', () => {
window.hide()
})
}
})
windowList.set(IWindowList.SETTING_WINDOW, {
isValid: true,
multiple: false,
options: () => settingWindowOptions,
callback (window, windowManager) { callback (window, windowManager) {
window.once('show', () => { window.once('show', () => {
remoteNoticeHandler.triggerHook(IRemoteNoticeTriggerHook.SETTING_WINDOW_OPEN) remoteNoticeHandler.triggerHook(IRemoteNoticeTriggerHook.SETTING_WINDOW_OPEN)
@ -117,30 +185,7 @@ windowList.set(IWindowList.SETTING_WINDOW, {
windowList.set(IWindowList.MINI_WINDOW, { windowList.set(IWindowList.MINI_WINDOW, {
isValid: process.platform !== 'darwin', isValid: process.platform !== 'darwin',
multiple: false, multiple: false,
options () { options: () => miniWindowOptions,
const obj: IBrowserWindowOptions = {
height: 64,
width: 64,
show: process.platform === 'linux',
frame: false,
fullscreenable: false,
skipTaskbar: true,
resizable: false,
transparent: process.platform !== 'linux',
icon: `${__static}/logo.png`,
webPreferences: {
backgroundThrottling: false,
nodeIntegration: !!process.env.ELECTRON_NODE_INTEGRATION,
contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION,
nodeIntegrationInWorker: true
}
}
if (db.get('settings.miniWindowOntop')) {
obj.alwaysOnTop = true
}
return obj
},
callback (window) { callback (window) {
window.loadURL(handleWindowParams(MINI_WINDOW_URL)) window.loadURL(handleWindowParams(MINI_WINDOW_URL))
} }
@ -149,29 +194,7 @@ windowList.set(IWindowList.MINI_WINDOW, {
windowList.set(IWindowList.RENAME_WINDOW, { windowList.set(IWindowList.RENAME_WINDOW, {
isValid: true, isValid: true,
multiple: true, multiple: true,
options () { options: () => renameWindowOptions,
const options: IBrowserWindowOptions = {
height: 175,
width: 300,
show: true,
fullscreenable: false,
resizable: false,
vibrancy: 'ultra-dark',
webPreferences: {
nodeIntegration: !!process.env.ELECTRON_NODE_INTEGRATION,
contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION,
nodeIntegrationInWorker: true,
backgroundThrottling: false
}
}
if (process.platform !== 'darwin') {
options.show = true
options.backgroundColor = '#3f3c37'
options.autoHideMenuBar = true
options.transparent = false
}
return options
},
async callback (window, windowManager) { async callback (window, windowManager) {
window.loadURL(handleWindowParams(RENAME_WINDOW_URL)) window.loadURL(handleWindowParams(RENAME_WINDOW_URL))
const currentWindow = windowManager.getAvailableWindow() const currentWindow = windowManager.getAvailableWindow()
@ -194,33 +217,7 @@ windowList.set(IWindowList.RENAME_WINDOW, {
windowList.set(IWindowList.TOOLBOX_WINDOW, { windowList.set(IWindowList.TOOLBOX_WINDOW, {
isValid: true, isValid: true,
multiple: false, multiple: false,
options () { options: () => toolboxWindowOptions,
const options: IBrowserWindowOptions = {
height: 450,
width: 800,
show: false,
frame: true,
center: true,
fullscreenable: false,
resizable: false,
title: `PicList ${T('TOOLBOX')}`,
vibrancy: 'ultra-dark',
icon: `${__static}/logo.png`,
webPreferences: {
backgroundThrottling: false,
nodeIntegration: !!process.env.ELECTRON_NODE_INTEGRATION,
contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION,
nodeIntegrationInWorker: true,
webSecurity: false
}
}
if (process.platform !== 'darwin') {
options.backgroundColor = '#3f3c37'
options.autoHideMenuBar = true
options.transparent = false
}
return options
},
async callback (window, windowManager) { async callback (window, windowManager) {
window.loadURL(TOOLBOX_WINDOW_URL) window.loadURL(TOOLBOX_WINDOW_URL)
const currentWindow = windowManager.getAvailableWindow() const currentWindow = windowManager.getAvailableWindow()

View File

@ -2,26 +2,29 @@ import fs from 'fs-extra'
import dayjs from 'dayjs' import dayjs from 'dayjs'
import util from 'util' import util from 'util'
const checkLogFileIsLarge = (logPath: string): { const MB = 1024 * 1024
const DEFAULT_LOG_FILE_SIZE_LIMIT = 10 * MB
interface CheckLogFileResult {
isLarge: boolean isLarge: boolean
logFileSize?: number logFileSize?: number
logFileSizeLimit?: number logFileSizeLimit?: number
} => { }
const checkLogFileIsLarge = (logPath: string): CheckLogFileResult => {
try { try {
if (fs.existsSync(logPath)) { if (fs.existsSync(logPath)) {
const logFileSize = fs.statSync(logPath).size const logFileSize = fs.statSync(logPath).size
const logFileSizeLimit = 10 * 1024 * 1024 // 10 MB default
return { return {
isLarge: logFileSize > logFileSizeLimit, isLarge: logFileSize > DEFAULT_LOG_FILE_SIZE_LIMIT,
logFileSize, logFileSize,
logFileSizeLimit logFileSizeLimit: DEFAULT_LOG_FILE_SIZE_LIMIT
} }
} }
return { return {
isLarge: false isLarge: false
} }
} catch (e) { } catch (e) {
// why throw error???
console.log(e) console.log(e)
return { return {
isLarge: true isLarge: true
@ -36,14 +39,14 @@ const recreateLogFile = (logPath: string): void => {
fs.createFileSync(logPath) fs.createFileSync(logPath)
} }
} catch (e) { } catch (e) {
// do nothing console.log(e)
} }
} }
/** /**
* for local log before piclist inited * for local log before piclist inited
*/ */
const getLogger = (logPath: string, logtype: string) => { const getLogger = (logPath: string, logType: string) => {
let hasUncathcedError = false let hasUncathcedError = false
try { try {
if (!fs.existsSync(logPath)) { if (!fs.existsSync(logPath)) {
@ -64,7 +67,7 @@ const getLogger = (logPath: string, logtype: string) => {
return return
} }
try { try {
let log = `${dayjs().format('YYYY-MM-DD HH:mm:ss')} [${logtype} ${type.toUpperCase()}] ` let log = `${dayjs().format('YYYY-MM-DD HH:mm:ss')} [${logType} ${type.toUpperCase()}] `
msg.forEach((item: ILogArgvTypeWithError) => { msg.forEach((item: ILogArgvTypeWithError) => {
if (typeof item === 'object' && type === 'error') { if (typeof item === 'object' && type === 'error') {
log += `\n------Error Stack Begin------\n${util.format(item.stack)}\n-------Error Stack End------- ` log += `\n------Error Stack Begin------\n${util.format(item.stack)}\n-------Error Stack End------- `