【问题标题】:Electron prevent main window from closing电子防止主窗口关闭
【发布时间】:2020-11-09 04:51:02
【问题描述】:

我正在编写一个电子应用程序,如果用户打开了未保存的文件,我想在保存之前提示用户。我在网上找到了这个示例代码:

window.onbeforeunload = (e) => {
      var answer = confirm('Do you really want to close the application?');
      e.returnValue = answer;  // this will *prevent* the closing no matter what value is passed
      if(answer) { mainWindow.destroy(); }  // this will close the app
    };

如果在出现后几秒钟内按下“是”、“取消”或“X”按钮,则此代码会奇怪地工作,但是如果您让对话框在屏幕上停留片刻,然后单击按钮,则无论按下什么,应用程序都会关闭.

此代码位于 index.html 调用的我的主脚本文件中

【问题讨论】:

    标签: javascript electron


    【解决方案1】:

    真是奇怪的行为!我无法解释为什么会发生这种情况,但可以为您提供在主进程中实施的解决方法。

    您可以使用电子的dialog 模块并使用电子创建相同的确认对话框。这个按预期工作。

    main.js

    const { app, BrowserWindow, dialog } = require('electron')
    const path = require('path')
    
    app.once('ready', () => {
      let win = new BrowserWindow()
      win.loadURL(path.resolve(__dirname, 'index.html'))
      win.on('close', e => {
        let choice = dialog.showMessageBox(
          win,
          {
            type: 'question',
            buttons: ['Yes', 'No'],
            title: 'Confirm',
            message: 'Do you really want to close the application?'
          }
        )
        if (choice === 1) e.preventDefault()
      })
    })
    

    【讨论】:

      【解决方案2】:

      只有在 DevTools 窗口被激活时才有可能。

      无论如何,最好按照 pergy 的建议处理事件关闭。这是迄今为止最好的方法。

      但请注意,e.preventDefault() 在代码中无处不在。正确管理 preventDefault() 后,您需要将变量 e.defaultPrevented = false 转换为应用程序的自然行为。

      实际上,e.preventDefault() 函数似乎将变量 e.defaultPrevented 转换为 true,直到您更改其值。

      【讨论】:

        【解决方案3】:

        在我的例子中,当我不想关闭窗口时,我不得不使用一个名为 modificationEnCours 的变量,它是 true,然后如果我想要这样的话,我必须使用 false

        let mainWindow 
        let mainMenu // Menu de la fenêtre principale
        
        app.on('ready', () => {
          // Listen for app to be ready
        
          // Create the mainWindow 
          mainWindow = new BrowserWindow({
            width: 1024,
            height: 768,
            minHeight: 350,
            minWidth: 500,
            frame: true, 
            webPreferences: {
              nodeIntegration: true
            }
            })
        
          // Quit app when window is closed
          mainWindow.on('close', function(e){
            console.log('close')
            if (modificationEnCours){
              e.preventDefault()
            
              if(msgBoxVerifieSauvegarde('Question','Voulez-vous enregistrer avant de quitter ?')) {
                modificationEnCours=false
                app.quit()
              }
            } else if (process.platform !== 'darwin') {
              modificationEnCours=false
              app.quit()
              mainWindow = null
            }
          })
        
        
          // Load html in window
          mainWindow.loadFile(path.join(__dirname, 'mainWindow.html'))
        
        
          // Build menu from template
          mainMenu = Menu.buildFromTemplate(mainMenuTemplate)
          // Insert menu
          Menu.setApplicationMenu(mainMenu)
        })
        

        【讨论】:

          猜你喜欢
          • 2019-09-30
          • 1970-01-01
          • 2010-12-06
          • 2015-06-12
          • 1970-01-01
          • 2020-06-26
          • 1970-01-01
          • 1970-01-01
          • 2019-10-06
          相关资源
          最近更新 更多