From 6f882d030c49056d89f1324b11cf72faf7bb5a01 Mon Sep 17 00:00:00 2001 From: yj <1336058017@qq.com> Date: Wed, 18 Sep 2024 15:38:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=BC=A9=E6=94=BE=E5=80=8D?= =?UTF-8?q?=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.js | 12 +++++++++--- preload.js | 4 ++++ src/App.tsx | 31 ++++++++++++++++--------------- src/page/Login/index.tsx | 8 +++++--- src/render.d.ts | 1 + 5 files changed, 35 insertions(+), 21 deletions(-) diff --git a/main.js b/main.js index ae5fb5e..5281124 100644 --- a/main.js +++ b/main.js @@ -142,9 +142,9 @@ app.on('ready', () => { // 监听f12打开控制台 mainWindow.webContents.on('before-input-event', (event, input) => { // if (env === 'development') { - if (input.key === 'F12') { - mainWindow.webContents.openDevTools() - } + if (input.key === 'F12') { + mainWindow.webContents.openDevTools() + } // } }); // 监听移动 @@ -242,6 +242,12 @@ app.on('ready', () => { } } }); + // 获取桌面大小 + ipcMain.handle('getWindowSize', (event, config) => { + const primaryDisplay = screen.getPrimaryDisplay() + const { width, height } = primaryDisplay.workAreaSize + return { width, height } + }); // 设置桌面应用基础属性 ipcMain.handle('setMainWindowSize', (event, config) => { // 设置最小窗口尺寸 diff --git a/preload.js b/preload.js index e430d77..0e9d0b1 100644 --- a/preload.js +++ b/preload.js @@ -5,6 +5,10 @@ window.electron = { setMainWindowSize: (config) => { ipcRenderer.invoke('setMainWindowSize', { ...config }) }, + // 获取窗口大小 + getWindowSize: (config) => { + return ipcRenderer.invoke('getWindowSize') + }, // 设置窗口状态 setViewStatus: (status) => { ipcRenderer.invoke('setViewStatus', status) diff --git a/src/App.tsx b/src/App.tsx index fd71f73..00d10a5 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -197,21 +197,22 @@ const App: React.FC = () => { }) } const toSrc = (path: string): void => { - switch (path) { - case '/login': - storage.removeItem('user') - storage.setItem('userLogin', false) - navigate('/login') - break; - case '/home': - window.electron.setMainWindowSize({ - width: 1200, - height: 800, - }) - navigate('/home') - break; - - } + window.electron.getWindowSize().then((res: any) => { + switch (path) { + case '/login': + storage.removeItem('user') + storage.setItem('userLogin', false) + navigate('/login') + break; + case '/home': + window.electron.setMainWindowSize({ + width: Math.ceil(res.width / 1.5), + height: Math.ceil(res.height / 1.3), + }) + navigate('/home') + break; + } + }) }; const leaveChannel = async (bool?: boolean): Promise => { if (location.hash.indexOf('/meeting') === 1) { diff --git a/src/page/Login/index.tsx b/src/page/Login/index.tsx index f46fdf5..6c38c2f 100644 --- a/src/page/Login/index.tsx +++ b/src/page/Login/index.tsx @@ -142,9 +142,11 @@ const Login: React.FC = () => { storage.setItem('user', JSON.stringify(res.data)) storage.setItem('userLogin', true) try { - window.electron.setMainWindowSize({ - width: 1200, - height: 800, + window.electron.getWindowSize().then((res: any) => { + window.electron.setMainWindowSize({ + width: Math.ceil(res.width / 1.5), + height: Math.ceil(res.height / 1.3), + }) }) } catch { diff --git a/src/render.d.ts b/src/render.d.ts index cd72d54..2146c05 100644 --- a/src/render.d.ts +++ b/src/render.d.ts @@ -1,6 +1,7 @@ // electron-env.d.ts export interface IElectronAPI { setMainWindowSize: (config: any) => void; + getWindowSize: () => any; setViewStatus: (status: 'quit' | 'maximize' | 'minimize' | 'unmaximize' | 'hide') => void; getIsMaximized: () => Promise; setWriteText: (text: string) => void;