Skip to content

Instantly share code, notes, and snippets.

@mrfabbri
Last active August 29, 2015 14:03
Show Gist options
  • Save mrfabbri/4b30c247ea147c0b1379 to your computer and use it in GitHub Desktop.
Save mrfabbri/4b30c247ea147c0b1379 to your computer and use it in GitHub Desktop.
<html>
<head>
<script type="text/javascript">
var nw = require('nw.gui');
var win = nw.Window.get();
var nativeMenu = new nw.Menu({ type: 'menubar' });
if ('createMacBuiltin' in nativeMenu) {
nativeMenu.createMacBuiltin('node-webkit');
nativeMenu.items[0].submenu.append(
new nw.MenuItem({
label: 'Do Something',
click: function () { alert('Doing something'); }
})
);
win.menu = nativeMenu;
}
function hideWindowOnClose() {
win.on('close', function (event) {
if (event == 'quit') { console.log('quit'); win.close(true); }
else { win.hide(); }
});
nw.App.on('reopen', function () { win.show(); });
alert('This window will now hide instead of closing.');
document.getElementById('closingBehaviour').innerText = 'HIDE';
}
function closeWindowOnClose() {
win.removeAllListeners('close');
nw.App.removeAllListeners('reopen');
alert('This window will now actually close when closing.');
document.getElementById('closingBehaviour').innerText = 'CLOSE';
}
window.onload = function () {
document.getElementById('openWindow').onclick = function (event) {
event.preventDefault();
window.open('nw://blank');
};
document.getElementById('hideWindowOnClose').onclick = function (event) {
event.preventDefault();
hideWindowOnClose();
};
document.getElementById('closeWindowOnClose').onclick = function (event) {
event.preventDefault();
closeWindowOnClose();
};
};
</script>
</head>
<body>
<h1>
A test
</h1>
<p>
On a Mac,
<a id="openWindow" href="">open another window</a>.<br>
Then close this window and click on <code>Do Something</code> in the
<code>node-webkit</code> menu.<br>
A <code>** Unknown exception behavior: 0 segmentation fault</code>
will be reported.
</p>
<p>
Prevent the error changing the closing behaviour of this window to
<a id="hideWindowOnClose" href=""> hiding instead of closing</a>.
After that, you can reopen ("unhide") this window clicking on the
node-webkit Dock icon.
</p>
<p>
<a id="closeWindowOnClose" href="">Get back</a>
to the native (error triggering) window closing behaviour.
</p>
<p>
Current window closing behaviour:
<span id='closingBehaviour'>CLOSE</span>
</p>
</body>
</html>
{
"name": "nw-test-menu",
"description": "A program to test node-webkit menu when closing main window",
"version": "0.0.1",
"main": "index.html",
"window": {
"title": "node-webkit"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment