电子窗口宽度扩大但不缩回

问题描述 投票:0回答:1

我正在使用 Electron 开发一个应用程序,我正在发送一条用于扩展应用程序宽度的 IPC 消息和一条用于缩回应用程序宽度的 IPC 消息。有代码,我的前端正确触发了这些 ipc 消息,只是它不想回到 1024。

我的应用程序使用固定的宽度和高度,当retractAppView执行时,它只是将整个窗口向上移动1px。

// main.ts
ipcMain.on('expandAppView', () => {
    const mainWindow = BrowserWindow.getFocusedWindow()
    if (mainWindow) mainWindow.setSize(1400, 500, false)
})

ipcMain.on('retractAppView', () => {
    const mainWindow = BrowserWindow.getFocusedWindow()
    if (mainWindow) mainWindow.setSize(1024, 500, false)
})
// nav.tsx
let hasExpanded = false
const expandAppView = () => {
    if (!hasExpanded) window.electron.ipcRenderer.sendMessage('expandAppView')
    else window.electron.ipcRenderer.sendMessage('retractAppView')
    hasExpanded = !hasExpanded
}
// preload.ts
// Disable no-unused-vars, broken for spread args
/* eslint no-unused-vars: off */
import { contextBridge, ipcRenderer, IpcRendererEvent } from 'electron'

export type Channels = 'closeApp' | 'minimiseApp' | 'expandAppView' | 'retractAppView'

const electronHandler = {
    ipcRenderer: {
        sendMessage(channel: Channels, ...args: unknown[]) {
            ipcRenderer.send(channel, ...args)
        },
        on(channel: Channels, func: (...args: unknown[]) => void) {
            const subscription = (_event: IpcRendererEvent, ...args: unknown[]) =>
                func(...args)
            ipcRenderer.on(channel, subscription)

            return () => {
                ipcRenderer.removeListener(channel, subscription)
            }
        },
        once(channel: Channels, func: (...args: unknown[]) => void) {
            ipcRenderer.once(channel, (_event, ...args) => func(...args))
        },
    },
}

contextBridge.exposeInMainWorld('electron', electronHandler)

export type ElectronHandler = typeof electronHandler

我期望当

retractAppView
ipc 消息运行时,它会使窗口宽 1024px

reactjs typescript electron ipc
1个回答
0
投票

我找到了解决方案,您必须使窗口可调整大小,然后扩展/收缩窗口大小,然后使其不可调整大小

例如:

ipcMain.on('retractAppView', () => {
    const mainWindow = BrowserWindow.getFocusedWindow()
    if (!mainWindow) return

    mainWindow.resizable = true
    mainWindow.setSize(1024, 500, true)
    mainWindow.resizable = false
})
© www.soinside.com 2019 - 2024. All rights reserved.