Commit 5b91adb6 authored by Tasso Evangelista's avatar Tasso Evangelista

Segregate state, actions, and structure of menus

parent 1539e060
......@@ -11,16 +11,115 @@ const { certificate } = remote.require('./background');
const isMac = process.platform === 'darwin';
const isWindows = process.platform === 'win32';
// i18n.__ = (x) => x;
const getMainWindow = getCurrentWindow;
const createMenuTemplate = () => ([
const actions = {
quit() {
app.quit();
},
about() {
ipcRenderer.send('show-about-dialog');
},
addNewServer() {
getMainWindow().show();
servers.clearActive();
webview.showLanding();
},
selectServer: (host) => () => {
getMainWindow().show();
servers.setActive(host.url);
},
server: {
reload() {
const activeWebview = webview.getActive();
if (activeWebview) {
activeWebview.reload();
}
},
reloadIgnoringCache() {
const activeWebview = webview.getActive();
if (activeWebview) {
activeWebview.reload();
}
},
clearCertificatesAndReload() {
certificate.clear();
const activeWebview = webview.getActive();
if (activeWebview) {
activeWebview.reload();
}
},
openDevTools() {
const activeWebview = webview.getActive();
if (activeWebview) {
activeWebview.openDevTools();
}
},
goBack() {
webview.goBack();
},
goForward() {
webview.goForward();
},
},
toggleTrayIcon() {
tray.toggle();
},
toggleFullScreen() {
const mainWindow = getMainWindow();
mainWindow.setFullScreen(!mainWindow.isFullScreen());
},
toggleMenuBar() {
const current = localStorage.getItem('autohideMenu') === 'true';
getMainWindow().setAutoHideMenuBar(!current);
localStorage.setItem('autohideMenu', JSON.stringify(!current));
},
toggleServerList() {
sidebar.toggle();
},
app: {
reload() {
const mainWindow = getMainWindow();
console.log({ mainWindow });
if (mainWindow.destroyTray) {
mainWindow.destroyTray();
}
mainWindow.reload();
},
toggleDevTools() {
getMainWindow().toggleDevTools();
},
resetData() {
servers.resetAppData();
},
},
};
const createMenuTemplate = (state) => ([
{
label: isMac ? app.getName() : i18n.__('&File'),
submenu: [
...(isMac ? [
{
label: i18n.__('About %s', app.getName()),
click: () => ipcRenderer.send('show-about-dialog'),
click: actions.about,
},
{
type: 'separator',
......@@ -50,16 +149,12 @@ const createMenuTemplate = () => ([
// {
// label: i18n.__('Preferences'),
// accelerator: 'CommandOrControl+,',
// click: () => alert('Not implemented yet.'),
// click: actions.preferences,
// },
{
label: i18n.__('Add &new server'),
accelerator: 'CommandOrControl+N',
click() {
getCurrentWindow().show();
servers.clearActive();
webview.showLanding();
},
click: actions.addNewServer,
},
{
type: 'separator',
......@@ -67,7 +162,7 @@ const createMenuTemplate = () => ([
{
label: i18n.__('&Quit %s', app.getName()),
accelerator: 'CommandOrControl+Q',
click: () => app.quit(),
click: actions.quit,
},
],
},
......@@ -115,41 +210,20 @@ const createMenuTemplate = () => ([
{
label: i18n.__('&Reload'),
accelerator: 'CommandOrControl+R',
click() {
const activeWebview = webview.getActive();
if (activeWebview) {
activeWebview.reload();
}
},
click: actions.server.reload,
},
{
label: i18n.__('Reload ignoring cache'),
click() {
const activeWebview = webview.getActive();
if (activeWebview) {
activeWebview.reloadIgnoringCache();
}
},
click: actions.server.reloadIgnoringCache,
},
{
label: i18n.__('Clear trusted certificates'),
click: () => {
certificate.clear();
const activeWebview = webview.getActive();
if (activeWebview) {
activeWebview.reloadIgnoringCache();
}
},
click: actions.server.clearCertificatesAndReload,
},
{
label: i18n.__('Open &DevTools'),
accelerator: isMac ? 'Command+Alt+I' : 'Ctrl+Shift+I',
click() {
const activeWebview = webview.getActive();
if (activeWebview) {
activeWebview.openDevTools();
}
},
click: actions.server.openDevTools,
},
{
type: 'separator',
......@@ -157,12 +231,12 @@ const createMenuTemplate = () => ([
{
label: i18n.__('&Back'),
accelerator: isMac ? 'Command+Left' : 'Alt+Left',
click: () => webview.goBack(),
click: actions.server.goBack,
},
{
label: i18n.__('&Forward'),
accelerator: isMac ? 'Command+Right' : 'Alt+Right',
click: () => webview.goForward(),
click: actions.server.goForward,
},
{
type: 'separator',
......@@ -170,38 +244,31 @@ const createMenuTemplate = () => ([
{
label: i18n.__('Tray icon'),
type: 'checkbox',
checked: localStorage.getItem('hideTray') !== 'true',
click: () => tray.toggle(),
checked: state.trayIcon,
click: actions.toggleTrayIcon,
},
...(isMac ? [
{
label: i18n.__('Full screen'),
type: 'checkbox',
checked: getCurrentWindow().isFullScreen(),
checked: state.fullScreen,
accelerator: 'Control+Command+F',
click() {
const mainWindow = getCurrentWindow();
mainWindow.setFullScreen(!mainWindow.isFullScreen());
},
click: actions.toggleFullScreen,
},
] : []),
...(!isMac ? [
{
label: i18n.__('Menu bar'),
type: 'checkbox',
checked: localStorage.getItem('autohideMenu') === 'true',
click() {
const current = localStorage.getItem('autohideMenu') === 'true';
getCurrentWindow().setAutoHideMenuBar(!current);
localStorage.setItem('autohideMenu', JSON.stringify(!current));
},
checked: state.menuBar,
click: actions.toggleMenuBar,
},
] : []),
{
label: i18n.__('Server list'),
type: 'checkbox',
checked: localStorage.getItem('sidebar-closed') !== 'true',
click: () => sidebar.toggle(),
checked: state.serverList,
click: actions.toggleServerList,
},
{
type: 'separator',
......@@ -228,39 +295,25 @@ const createMenuTemplate = () => ([
id: 'window',
role: 'window',
submenu: [
...(Object.values(servers.hosts)
.sort((a, b) => (sidebar ? (sidebar.sortOrder.indexOf(a.url) - sidebar.sortOrder.indexOf(b.url)) : 0))
.map((host, i) => ({
label: host.title.replace(/&/g, '&&'),
type: 'radio',
checked: servers.active && servers.active.url === host.url,
accelerator: `CommandOrControl+${ i + 1 }`,
id: host.url,
click() {
getCurrentWindow().show();
servers.setActive(host.url);
},
}))
),
...state.servers.map((host, i) => ({
label: host.title.replace(/&/g, '&&'),
type: state.currentServerUrl ? 'radio' : 'normal',
checked: state.currentServerUrl === host.url,
accelerator: `CommandOrControl+${ i + 1 }`,
id: host.url,
click: actions.selectServer(host),
})),
{
type: 'separator',
},
{
label: i18n.__('&Reload'),
accelerator: 'CommandOrControl+Shift+R',
click() {
const mainWindow = getCurrentWindow();
if (mainWindow.destroyTray) {
mainWindow.destroyTray();
}
mainWindow.reload();
},
click: actions.app.reload,
},
{
label: i18n.__('Toggle &DevTools'),
click() {
getCurrentWindow().toggleDevTools();
},
click: actions.app.toggleDevTools,
},
{
type: 'separator',
......@@ -294,7 +347,7 @@ const createMenuTemplate = () => ([
},
{
label: i18n.__('Reset app data'),
click: () => servers.resetAppData(),
click: actions.app.resetData,
},
{
type: 'separator',
......@@ -305,23 +358,41 @@ const createMenuTemplate = () => ([
},
{
label: i18n.__('About %s', app.getName()),
click: () => ipcRenderer.send('show-about-dialog'),
click: actions.about,
},
],
},
]);
function updateMenus() {
const menu = Menu.buildFromTemplate(createMenuTemplate());
const state = {};
state.trayIcon = localStorage.getItem('hideTray') !== 'true';
state.fullScreen = getMainWindow().isFullScreen();
state.menuBar = localStorage.getItem('autohideMenu') !== 'true';
state.serverList = localStorage.getItem('sidebar-closed') !== 'true';
state.servers = Object.values(servers.hosts)
.sort((a, b) => (sidebar ? (sidebar.sortOrder.indexOf(a.url) - sidebar.sortOrder.indexOf(b.url)) : 0))
.map(({ title, url }) => ({ title, url }));
state.currentServerUrl = servers.active;
const menu = Menu.buildFromTemplate(createMenuTemplate(state));
Menu.setApplicationMenu(menu);
if (!isMac && localStorage.getItem('autohideMenu') === 'true') {
getCurrentWindow().setAutoHideMenuBar(true);
getMainWindow().setAutoHideMenuBar(true);
}
}
servers.on('loaded', updateMenus);
servers.on('active-cleared', updateMenus);
servers.on('active-setted', updateMenus);
servers.on('host-added', updateMenus);
servers.on('host-removed', updateMenus);
servers.on('title-setted', updateMenus);
sidebar.on('hosts-sorted', updateMenus);
sidebar.on('hide', updateMenus);
sidebar.on('show', updateMenus);
updateMenus();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment