Feat:编辑窗口支持记住上一次的大小

This commit is contained in:
街角小林 2024-09-25 18:56:05 +08:00
parent 23b9c50ca0
commit 6da7cf8284
4 changed files with 78 additions and 8 deletions

View File

@ -7,7 +7,9 @@ import {
removeFileInRecent,
replaceFileInRecent,
getRecent,
saveFileListToRecent
saveFileListToRecent,
saveEditWindowSize,
getEditWindowSize
} from './storage'
import { v4 as uuid } from 'uuid'
@ -21,11 +23,10 @@ export const bindFileHandleEvent = ({ mainWindow }) => {
// 新建编辑页面
const openIds = []
const openIdToWin = {}
const windowSize = getEditWindowSize() || {}
const createEditWindow = async (event, id) => {
openIds.push(id)
const win = new BrowserWindow({
width: 1200,
height: 800,
const options = {
frame: false,
titleBarStyle: 'hiddenInset',
webPreferences: {
@ -35,7 +36,18 @@ export const bindFileHandleEvent = ({ mainWindow }) => {
contextIsolation: true,
preload: path.join(__dirname, 'preload.js')
}
})
}
if (windowSize.width && windowSize.height) {
options.width = windowSize.width
options.height = windowSize.height
} else {
options.width = 1200
options.height = 800
}
const win = new BrowserWindow(options)
if (windowSize.maximize) {
win.maximize()
}
openIdToWin[id] = win
win.on('closed', () => {
// 从openIds数组中删除
@ -49,12 +61,32 @@ export const bindFileHandleEvent = ({ mainWindow }) => {
// 从idToFilePath中删除
delete idToFilePath[id]
})
win.on('resize', () => {
const size = win.getSize()
windowSize.width = size[0]
windowSize.height = size[1]
saveEditWindowSize({
...windowSize
})
})
win.on('maximize', () => {
windowSize.maximize = true
saveEditWindowSize({
...windowSize
})
})
win.on('unmaximize', () => {
windowSize.maximize = false
saveEditWindowSize({
...windowSize
})
})
if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode
win.loadURL(
process.env.WEBPACK_DEV_SERVER_URL + '/#/workbenche/edit/' + id
)
// if (!process.env.IS_TEST) win.webContents.openDevTools()
if (!process.env.IS_TEST) win.webContents.openDevTools()
} else {
// Load the index.html when not in development
win.loadURL('app://./index.html/#/workbenche/edit/' + id)
@ -62,6 +94,14 @@ export const bindFileHandleEvent = ({ mainWindow }) => {
}
ipcMain.on('create', createEditWindow)
// 获取窗口是否处于最大化
ipcMain.handle('getIsMaximize', (event, id) => {
if (openIdToWin[id]) {
return openIdToWin[id].isMaximized()
}
return false
})
// 保存文件
const idToFilePath = {}
ipcMain.handle('save', async (event, id, data, fileName = '未命名') => {
@ -241,7 +281,7 @@ export const bindFileHandleEvent = ({ mainWindow }) => {
const exist = fs.existsSync(file)
if (!exist) {
removeFileInRecent(file).then(() => {
notifyMainWindowRefreshRecentFileList()
notifyMainWindowRefreshRecentFileList()
})
return '文件不存在'
}

View File

@ -31,5 +31,6 @@ contextBridge.exposeInMainWorld('electronAPI', {
openPath: (path, relativePath) =>
ipcRenderer.invoke('openPath', path, relativePath),
saveClientConfig: config => ipcRenderer.invoke('saveClientConfig', config),
getClientConfig: () => ipcRenderer.invoke('getClientConfig')
getClientConfig: () => ipcRenderer.invoke('getClientConfig'),
getIsMaximize: id => ipcRenderer.invoke('getIsMaximize', id)
})

View File

@ -2,6 +2,7 @@ import storage from 'electron-json-storage'
export const RECENT_FILE_LIST = 'recentFileList'
export const CLIENT_CONFIG = 'client_config'
export const EDIT_WINDOW_SIZE = 'edit_window_size'
// 保存到最近文件
export const saveToRecent = file => {
@ -127,3 +128,22 @@ export const getClientConfig = () => {
const res = storage.getSync(CLIENT_CONFIG)
return res
}
// 保存编辑窗口大小信息
export const saveEditWindowSize = config => {
return new Promise((resolve, reject) => {
storage.set(EDIT_WINDOW_SIZE, config, err => {
if (err) {
reject(err)
} else {
resolve()
}
})
})
}
// 获取编辑窗口大小信息
export const getEditWindowSize = () => {
const res = storage.getSync(EDIT_WINDOW_SIZE)
return res
}

View File

@ -17,6 +17,15 @@ export default {
isMaximize: false
}
},
async created() {
try {
this.isMaximize = await window.electronAPI.getIsMaximize(
this.$route.params.id
)
} catch (error) {
console.log(error)
}
},
methods: {
minimize() {
window.electronAPI.minimize()