在没有菜单栏的 NW.js 中打开新的 Window

Open new Window in NW.js without menubar

您好,我有一个使用 NW.JS 和 angularjs 的应用程序。但问题是,我想打开一个新的 window(从应用程序中加载一个新页面)而不继承 window 主菜单的菜单栏。这在 nwjs 中可能吗?此外,每次我单击触发新 window 打开的主 window 菜单栏中的菜单项时,它总是会打开新 window。我怎样才能防止这种情况发生?我的意思是,当我已经打开新的 window 时,应用程序应该避免再次打开它,除非它已经关闭。

在我的 index.html

var menu = new nw.Menu({ type: 'menubar' });

var submenu = new nw.Menu();
submenu.append(new nw.MenuItem({
    label: 'Exit',
    click: function(){
        nw.App.quit();
    }
}));

menu.append(new nw.MenuItem({
    label: 'File',
    submenu: submenu
}));
menu.append(new nw.MenuItem({
    label: 'New Page',
    click: function(){
        nw.Window.open('index.html#/new_page')
    }
}));

nw.Window.get().menu = menu;

当打开新的 window 时,您使用的是相同的 html 文件,其中包含创建菜单栏的代码,因此显然也将为弹出窗口创建菜单栏 window,您需要为弹出 window.

使用不同的 html 文件

下面是完整版本的代码,如果它不适合你,请告诉我

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" /> 
    <title>Demo</title>

    <script type="text/javascript">
        //var win = nw.Window.get();
        //win.showDevTools();

        var menu = new nw.Menu({ type: 'menubar' });

        var submenu = new nw.Menu();
        submenu.append(new nw.MenuItem({
            label: 'Exit',
            click: function(){
                nw.App.quit();
            }
        }));

        menu.append(new nw.MenuItem({
            label: 'File',
            submenu: submenu
        }));
        menu.append(new nw.MenuItem({
            label: 'New Page',
            click: function(){
                console.log('open new page');
                var parentWin = window;

                if(parentWin.localStorage.getItem('child_open')) {
                    console.log('child window is already open');
                    return;
                }

                nw.Window.open('home.html#new_page', {}, function(win) {
                    parentWin.localStorage.setItem('child_open', true);

                    win.on('closed', function() {
                        parentWin.localStorage.removeItem('child_open');
                    });
                });
            }
        }));

        nw.Window.get().menu = menu;
    </script>
</head>
<body>
</body>
</html>

home.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" /> 
    <title>Inner Window</title>
</head>
<body>
    <br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br />
    <br /><br /><br /><br /><br /><br />
    <p id="new_page"> New page section </p>
</body>
</html>