Electron dialog.showMessageBox方法中的“永不再问”功能

时间:2017-06-29 10:27:46

标签: javascript checkbox callback dialog electron

我有一个可以正常运行的dialog.showMessageBox,并按照我的要求去做。

electron.dialog.showMessageBox({
            type: 'info',
            buttons: ['Yes', 'No'],
            message: 'Are you sure?',
        }, resp => {
            if (resp === 0) {
                // User selected 'Yes'
                foo.bar();
            }
        });

但每次调用此函数都很烦人,我想插入一个“永远不要再问我”这样的内容。

electron.dialog.showMessageBox({
            type: 'info',
            buttons: ['Yes', 'No'],
            message: 'Are you sure?',
            checkboxLabel: 'Never ask me again',
            checkboxChecked: false
        }, resp => {
            if (resp === 0) {
                // User selected 'Yes'
                foo.bar();
            }
        });

Dialog box

文档说布尔checkboxChecked可以在回调函数中使用,但是我想知道如何将它设置为全局,以便不再询问问题。

2 个答案:

答案 0 :(得分:1)

我建议使用名为electron-settings的lib。 Link here. 这样,您的代码看起来就像那样:

const settings = require('electron-settings'); 
electron.dialog.showMessageBox({
    type: 'info',
    buttons: ['Yes', 'No'],
    message: 'Are you sure?',
    checkboxLabel: 'Never ask me again',
    checkboxChecked: false
    }, 
    resp => {
        if (resp === 0) {
            // User selected 'Yes'
            settings.set('never_ask_again_answer', true); 
            foo.bar();
        }
    });

    // get the answer later
    let answer = settings.get('never_ask_again_answer')

编辑:我认为重新启动时它不起作用,因为您只是在开始时再次将其设置为false。很少有解决方法:

const settings = require('electron-settings');

if(!settings.has('neverAskMeAgain')){ // this if might do it
    settings.set('neverAskMeAgain', {
        state: false
    });
}

if (!settings.get('neverAskMeAgain.state')) {
    electron.dialog.showMessageBox({
        type: 'info',
        buttons: ['Yes', 'No'],
        message: 'Are you sure?',
        checkboxLabel: 'Never ask me again',
        checkboxChecked: false
    }, (resp, checkboxChecked) => {
        if (resp === 0) {
            foo.bar();
            settings.set('neverAskMeAgain.state', checkboxChecked);
        }
    });
} else if (settings.get('neverAskMeAgain.state')) {
    foo.bar();
}

答案 1 :(得分:1)

好的,这就是我所做的:

const settings = require('electron-settings');

settings.set('neverAskMeAgain', {
    state: false
});

        if (!settings.get('neverAskMeAgain.state')) {
            electron.dialog.showMessageBox({
                type: 'info',
                buttons: ['Yes', 'No'],
                message: 'Are you sure?',
                checkboxLabel: 'Never ask me again',
                checkboxChecked: false
            }, (resp, checkboxChecked) => {
                if (resp === 0) {
                    foo.bar();
                    settings.set('neverAskMeAgain.state', checkboxChecked);
                }
            });
        } else if (settings.get('neverAskMeAgain.state')) {
            foo.bar();
        }
    } else {
        foo.bar();
    }

在运行npm install && npm start时,它就像应用实例中的魅力一样,但当我退出并重新启动它时,会再次显示对话框...