Electron: アンロード前に確認ダイアログを表示するにはどうすればよいですか?

作成日 2015年07月22日  ·  3コメント  ·  ソース: electron/electron

Electronのbeforeunloadイベントは、ブラウザとは動作が異なります。 文字列を返すと、確認ダイアログに表示されません。 BrowserWindowで、 beforeunloadの使用例を示し、コメントで確認ダイアログを表示できると述べているのを見ました。

//ダイアログAPIを使用して、ユーザーがアプリケーションを閉じることを確認できるようにすることもできます

ダイアログAPIはメインプロセスでのみ使用できませんか?
通常のブラウザの動作を再現する最も簡単な方法は何ですか?

最も参考になるコメント

@shneorasaあなたが尋ねてから数ヶ月が経ちました(そしてあなたはおそらく今までに解決策を見つけました)が、将来解決策を探している人のために、 window.onbeforeloadを使用するのでwindow.onbeforeload

beforeunloadイベントが上書きされると、将来のダイアログが台無しになり、アプリが破損する可能性があります。 代わりに、私が行ったように、アプリ内に単純なブール値を格納できます。

// Inside main/index.js
import { app, BrowserWindow, dialog } from 'electron'
app.showExitPrompt = true

これで、次のようにBrowserWindowcloseイベントを中断できます。

// Inside main/index.js, where BrowserWindow is initialized
mainWindow.on('close', (e) => {
    if (app.showExitPrompt) {
        e.preventDefault() // Prevents the window from closing 
        dialog.showMessageBox({
            type: 'question',
            buttons: ['Yes', 'No'],
            title: 'Confirm',
            message: 'Unsaved data will be lost. Are you sure you want to quit?'
        }, function (response) {
            if (response === 0) { // Runs the following if 'Yes' is clicked
                app.showExitPrompt = false
                mainWindow.close()
            }
        })
    }
})

私はこのソリューションをVue.jsを使用してElectronアプリに実装しました。ダイアログの表示/非表示は、 appをインポートするのと同じくらい簡単です。

const { app } = require('electron').remote

保存されたブール値を上書きします。

app.showExitPrompt = true

全てのコメント3件

どうでも。 私はそれを考え出した。

将来の参考のために、ここに例があります:

window.onbeforeunload = function(e) {
    var remote = require('remote');
    var dialog = remote.require('dialog');
    var choice = dialog.showMessageBox(
            remote.getCurrentWindow(),
            {
                type: 'question',
                buttons: ['Yes', 'No'],
                title: 'Confirm',
                message: 'Are you sure you want to quit?'
            });

    return choice === 0;
};

やあ(:
上記の解決策は私にはうまくいきません。
それでもブラウザの「onbeforeunload」がかかるため...
何か案が?

@shneorasaあなたが尋ねてから数ヶ月が経ちました(そしてあなたはおそらく今までに解決策を見つけました)が、将来解決策を探している人のために、 window.onbeforeloadを使用するのでwindow.onbeforeload

beforeunloadイベントが上書きされると、将来のダイアログが台無しになり、アプリが破損する可能性があります。 代わりに、私が行ったように、アプリ内に単純なブール値を格納できます。

// Inside main/index.js
import { app, BrowserWindow, dialog } from 'electron'
app.showExitPrompt = true

これで、次のようにBrowserWindowcloseイベントを中断できます。

// Inside main/index.js, where BrowserWindow is initialized
mainWindow.on('close', (e) => {
    if (app.showExitPrompt) {
        e.preventDefault() // Prevents the window from closing 
        dialog.showMessageBox({
            type: 'question',
            buttons: ['Yes', 'No'],
            title: 'Confirm',
            message: 'Unsaved data will be lost. Are you sure you want to quit?'
        }, function (response) {
            if (response === 0) { // Runs the following if 'Yes' is clicked
                app.showExitPrompt = false
                mainWindow.close()
            }
        })
    }
})

私はこのソリューションをVue.jsを使用してElectronアプリに実装しました。ダイアログの表示/非表示は、 appをインポートするのと同じくらい簡単です。

const { app } = require('electron').remote

保存されたブール値を上書きします。

app.showExitPrompt = true
このページは役に立ちましたか?
0 / 5 - 0 評価