'use strict';

const app = require('app');
const BrowserWindow = require('browser-window');
const Menu = require('menu');
const GlobalShortcut = require('global-shortcut');

require('crash-reporter').start();

// prevent window being GC'd
let win = null;

function toggleWindow() {
	if (win.isVisible() && win.isFocused()) {
		// minimize the window if it is is focused on
		win.minimize();
	}
	else if (win.isVisible() && !win.isFocused()) {
		// brint window in front of others if it is not minized,
		// in behind of the other windows
		win.focus();
	} else if (win.isMinimized()) {
		// restore window from dock
		win.restore();
	}
}

function registerShorcuts() {
	GlobalShortcut.register('CommandOrControl+?', toggleWindow);
}

function unregisterShortcuts() {
	GlobalShortcut.unregisterAll();
}

app.on('window-all-closed', () => {
	if (process.platform !== 'darwin') {
		app.quit();
	}
});

app.on('ready', () => {
	var template = [{
		label: 'Devdocs',
    submenu: [{
		  label: 'Hide Electron',
		  accelerator: 'Esc',
		  selector: 'hide:'
		}]
	}];

	var menu = Menu.buildFromTemplate(template);
  Menu.setApplicationMenu(menu);

	registerShorcuts();

	win = new BrowserWindow({
		width: 800,
		height: 600,
		resizable: true,
		center: true,
		show: true,
		'skip-taskbar': true
	});

	win.loadUrl('http://devdocs.io');

	win.on('closed', () => {
		// deref the window
		// for multiple windows store them in an array
		win = null;

		unregisterShortcuts();
	});

	win.on('restore', () => {
		win.focus();
	});
});

@@ -165,16 +165,47 @@
 rtcut');
+%0Aconst appName = app.getName();
 %0A%0Arequir
@@ -867,47 +867,8 @@
 %3E %7B%0A
-%09if (process.platform !== 'darwin') %7B%0A%09
 %09app
@@ -876,19 +876,16 @@
 quit();%0A
-%09%7D%0A
 %7D);%0A%0Aapp
@@ -937,17 +937,15 @@
 el: 
-'Devdocs'
+appName
 ,%0A  
@@ -958,16 +958,98 @@
 enu: %5B%7B%0A
+%09%09%09label: %60About $%7BappName%7D%60,%0A%09%09%09role: 'about'%0A%09%09%7D, %7B%0A%09%09%09type: 'separator'%0A%09%09%7D, %7B%0A
 %09%09  labe
@@ -1060,17 +1060,8 @@
 Hide
- Electron
 ',%0A%09
@@ -1105,16 +1105,100 @@
 'hide:'%0A
+%09%09%7D, %7B%0A%09%09%09label: 'Quit',%0A%09%09%09accelerator: 'Cmd+Q',%0A%09%09%09click() %7B%0A%09%09%09%09app.quit();%0A%09%09%09%7D%0A
 %09%09%7D%5D%0A%09%7D%5D

Add quit option