Commit b65ae5c2 authored by Tasso Evangelista's avatar Tasso Evangelista

Move menus from renderer to main process

parent 649d4be3
......@@ -3,7 +3,7 @@ import querystring from 'querystring';
import url from 'url';
import jetpack from 'fs-jetpack';
import idle from '@paulcbetts/system-idle-time';
import { app, ipcMain, BrowserWindow, Menu } from 'electron';
import { app, ipcMain, Menu } from 'electron';
import autoUpdate from './background/autoUpdate';
import certificate from './background/certificate';
......@@ -13,8 +13,10 @@ import './background/screenshare';
import i18n from './i18n/index.js';
import env from './env';
export { default as remoteServers } from './background/servers';
export { default as showAboutDialog } from './background/aboutDialog';
export { default as certificate } from './background/certificate';
export { default as menus } from './background/menus';
export { default as remoteServers } from './background/servers';
process.env.GOOGLE_API_KEY = 'AIzaSyADqUh_c1Qhji3Cp1NE43YrcpuPkmhXD-c';
......@@ -111,23 +113,3 @@ app.on('ready', () => {
ipcMain.on('getSystemIdleTime', (event) => {
event.returnValue = idle.getIdleTime();
});
ipcMain.on('show-about-dialog', () => {
getMainWindow().then((mainWindow) => {
const win = new BrowserWindow({
title: i18n.__('About %s', app.getName()),
parent: mainWindow,
width: 400,
height: 300,
type: 'toolbar',
resizable: false,
maximizable: false,
minimizable: false,
center: true,
show: false,
});
win.setMenuBarVisibility(false);
win.once('ready-to-show', () => win.show());
win.loadURL(`file://${ __dirname }/public/about.html`);
});
});
import { app, BrowserWindow } from 'electron';
import { getMainWindow } from './mainWindow';
import i18n from '../i18n/index.js';
export default async() => {
const mainWindow = await getMainWindow();
const win = new BrowserWindow({
title: i18n.__('About %s', app.getName()),
parent: mainWindow,
width: 400,
height: 300,
type: 'toolbar',
resizable: false,
maximizable: false,
minimizable: false,
center: true,
show: false,
});
win.setMenuBarVisibility(false);
win.once('ready-to-show', () => win.show());
win.loadURL(`file://${ __dirname }/public/about.html`);
};
import { remote } from 'electron';
import { app, Menu } from 'electron';
import { EventEmitter } from 'events';
import { getMainWindow } from './mainWindow';
import i18n from '../i18n/index.js';
const { app, getCurrentWindow, Menu } = remote;
const createTemplate = ({
appName,
servers = [],
......@@ -274,18 +273,28 @@ const createTemplate = ({
class Menus extends EventEmitter {
constructor() {
super();
this.state = {};
this.on('update', this.update.bind(this));
}
update() {
const template = createTemplate({ appName: app.getName(), ...this }, this);
setState(partialState) {
this.state = {
...this.state,
...partialState,
};
this.update();
}
async update() {
const template = createTemplate({ appName: app.getName(), ...this.state }, this);
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
if (process.platform !== 'darwin') {
const { showMenuBar } = this;
getCurrentWindow().setAutoHideMenuBar(!showMenuBar);
getCurrentWindow().setMenuBarVisibility(!!showMenuBar);
const { showMenuBar } = this.state;
const mainWindow = await getMainWindow();
mainWindow.setAutoHideMenuBar(!showMenuBar);
mainWindow.setMenuBarVisibility(!!showMenuBar);
}
}
}
......
import { remote, ipcRenderer } from 'electron';
import menus from './menus';
import servers from './servers';
import sidebar from './sidebar';
import tray from './tray';
import webview from './webview';
const { app, getCurrentWindow, shell } = remote;
const { certificate } = remote.require('./background');
const { certificate, menus, showAboutDialog } = remote.require('./background');
export default () => {
menus.on('quit', () => app.quit());
menus.on('about', () => ipcRenderer.send('show-about-dialog'));
menus.on('about', () => showAboutDialog());
menus.on('open-url', (url) => shell.openExternal(url));
......@@ -72,12 +71,13 @@ export default () => {
const updatePreferences = () => {
const mainWindow = getCurrentWindow();
menus.showTrayIcon = localStorage.getItem('hideTray') !== 'true';
menus.showFullScreen = mainWindow.isFullScreen();
menus.showWindowOnUnreadChanged = localStorage.getItem('showWindowOnUnreadChanged') === 'true';
menus.showMenuBar = localStorage.getItem('autohideMenu') !== 'true';
menus.showServerList = localStorage.getItem('sidebar-closed') !== 'true';
menus.emit('update');
menus.setState({
showTrayIcon: localStorage.getItem('hideTray') !== 'true',
showFullScreen: mainWindow.isFullScreen(),
showWindowOnUnreadChanged: localStorage.getItem('showWindowOnUnreadChanged') === 'true',
showMenuBar: localStorage.getItem('autohideMenu') !== 'true',
showServerList: localStorage.getItem('sidebar-closed') !== 'true',
});
};
menus.on('toggle', (property) => {
......@@ -117,11 +117,12 @@ export default () => {
});
const updateServers = () => {
menus.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 }));
menus.currentServerUrl = servers.active;
menus.emit('update');
menus.setState({
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 })),
currentServerUrl: servers.active,
});
};
servers.on('loaded', updateServers);
......
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