javascript - 最小化和关闭按钮在 Electron 应用程序中不起作用

标签 javascript node.js electron window

我正在使用 Electron 框架制作一个应用程序,我尝试使用按钮来关闭和最小化窗口。我已经尝试了多种方法来做到这一点,但没有成功。

这就是我目前所拥有的:
HTML:

<body>
    <!-- Titlebar -->
    <button id="minimize-button">-</button>
    <button id="close-button">x</button>

    <!-- Script -->
    <script src="./js/minimize-close.js"></script>
</body>

JS(minimize-close.js):

const { ipcRenderer } = require('electron');

document.getElementById("minimize-button").addEventListener('click', () => {
    ipcRenderer.send('minimize-window');
});

document.getElementById("close-button").addEventListener('click', () => {
    ipcRenderer.send('close-window');
});

JS(index.js):

const { app, BrowserWindow, ipcMain } = require('electron');

function createWindow(){
    const window = new BrowserWindow({
        width: 960, height: 580,
        resizable: false, maximizable: false,
        frame: false, autoHideMenuBar: true,
        icon: './icon.ico',
        webPreferences: {
            nodeIntegration: true,
            devTools: false
        }
    });

    window.loadFile('./src/index.html');
}

// Minimize and close window
ipcMain.on('minimize-window', () => {
    window.minimize();
});

ipcMain.on('close-window', () => {
    window.close();
});

app.whenReady().then(() => {
    createWindow();

    app.on('activate', function(){
        if(BrowserWindow.getAllWindows().length === 0){
            createWindow();
        }
    });
});

app.on('window-all-closed', function(){
    if(process.platform !== 'darwin'){
        app.quit();
    }
});

最佳答案

您需要设置contextIsolation: false
我还阅读过 window 必须是全局的。如果不是在某个时刻,它将被垃圾收集器收集,因为创建它的函数已经完成。

let window;

function createWindow(){
    window = new BrowserWindow({
        width: 960, height: 580,
        resizable: false, maximizable: false,
        frame: false, autoHideMenuBar: true,
        icon: './icon.ico',
        webPreferences: {
            nodeIntegration: true,
            // devTools: false,  // commented for debugging purposes
            contextIsolation: false
        }
    });

    window.loadFile('./src/index.html');
    window.webContents.openDevTools(); // Open dev tools to see if any error arised. In this case I saw 'require is not defined' before setting contextIsolation. Remove it when going into production.
}

选项B:更“安全”

如果安全性是一个问题,并且您希望 nodeIntegrationcontextIsolation 保留其默认安全值,则需要 preload.js 方案。< br/> 如果您的应用程序加载了远程内容,则应该是这种情况。

HTML

<body>
    <!-- Titlebar -->
    <button id="minimize-button">-</button>
    <button id="close-button">x</button>
</body>

preload.js

const { ipcRenderer } = require('electron');

window.addEventListener('DOMContentLoaded', () => {
    document.getElementById("minimize-button").addEventListener('click', () => {
        ipcRenderer.send('minimize-window');
    });
    
    document.getElementById("close-button").addEventListener('click', () => {
        ipcRenderer.send('close-window');
    });
})

index.js

const path = require('path');


function createWindow(){
    window = new BrowserWindow({
        width: 960, height: 580,
        resizable: false, maximizable: false,
        frame: false, autoHideMenuBar: true,
        icon: './icon.ico',
        webPreferences: {
            // devTools: false,  // commented for debugging purposes
            preload: path.join(__dirname, 'preload.js')
        }
    });

    window.loadFile('./src/index.html');
    window.webContents.openDevTools(); // Open dev tools to see if any error arised. In this case I saw 'require is not defined' before setting contextIsolation. Remove it when going into production.
}

关于javascript - 最小化和关闭按钮在 Electron 应用程序中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68217009/

相关文章:

node.js - 表达 js 4 如何在不渲染任何 View /css 的情况下提供 json 结果

node.js - Mongoosastic 插件不更新嵌套对象

javascript - 渲染器问题中的 Electron 非上下文感知 native 模块

javascript - 未定义的 var module.exports

javascript - 尝试提交带有签名图像的 PDF 表单

javascript - 如何禁用 Bootstrap 的 typeahead 数据源?

node.js - 表达多个静态路由正则表达式解析

javascript - 使用 Electron (NightareJS) 重复单击页面上的元素

node.js - 错误TypeError : Cannot read property 'BrowserWindow' of undefined in angular version 10 and electron version 10 and ngx-electron 2. 2.0

javascript - 外部js加载之前执行的js语句