Commit 649d4be3 authored by Tasso Evangelista's avatar Tasso Evangelista

Isolate events from menu bar structure

parent 3219cbf8
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');
export default () => {
menus.on('quit', () => app.quit());
menus.on('about', () => ipcRenderer.send('show-about-dialog'));
menus.on('open-url', (url) => shell.openExternal(url));
menus.on('add-new-server', () => {
getCurrentWindow().show();
servers.clearActive();
webview.showLanding();
});
menus.on('select-server', ({ url }) => {
getCurrentWindow().show();
servers.setActive(url);
});
menus.on('reload-server', ({ ignoringCache = false, clearCertificates = false } = {}) => {
if (clearCertificates) {
certificate.clear();
}
const activeWebview = webview.getActive();
if (!activeWebview) {
return;
}
if (ignoringCache) {
activeWebview.reloadIgnoringCache();
return;
}
activeWebview.reload();
});
menus.on('open-devtools-for-server', () => {
const activeWebview = webview.getActive();
if (activeWebview) {
activeWebview.openDevTools();
}
});
menus.on('go-back', () => webview.goBack());
menus.on('go-forward', () => webview.goForward());
menus.on('reload-app', () => {
const mainWindow = getCurrentWindow();
if (mainWindow.destroyTray) {
mainWindow.destroyTray();
}
mainWindow.removeAllListeners();
mainWindow.reload();
});
menus.on('toggle-devtools', () => getCurrentWindow().toggleDevTools());
menus.on('reset-app-data', () => servers.resetAppData());
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.on('toggle', (property) => {
switch (property) {
case 'showTrayIcon': {
tray.toggle();
break;
}
case 'showFullScreen': {
const mainWindow = getCurrentWindow();
mainWindow.setFullScreen(!mainWindow.isFullScreen());
break;
}
case 'showWindowOnUnreadChanged': {
const previousValue = localStorage.getItem('showWindowOnUnreadChanged') === 'true';
const newValue = !previousValue;
localStorage.setItem('showWindowOnUnreadChanged', JSON.stringify(newValue));
break;
}
case 'showMenuBar': {
const previousValue = localStorage.getItem('autohideMenu') !== 'true';
const newValue = !previousValue;
localStorage.setItem('autohideMenu', JSON.stringify(!newValue));
break;
}
case 'showServerList': {
sidebar.toggle();
break;
}
}
updatePreferences();
});
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');
};
servers.on('loaded', updateServers);
servers.on('active-cleared', updateServers);
servers.on('active-setted', updateServers);
servers.on('host-added', updateServers);
servers.on('host-removed', updateServers);
servers.on('title-setted', updateServers);
sidebar.on('hosts-sorted', updateServers);
sidebar.on('badge-setted', function() {
const badge = sidebar.getGlobalBadge();
tray.showTrayAlert(badge);
});
webview.on('ipc-message-unread-changed', (hostUrl, [count]) => {
if (typeof count === 'number' && localStorage.getItem('showWindowOnUnreadChanged') === 'true') {
const mainWindow = remote.getCurrentWindow();
if (!mainWindow.isFocused()) {
mainWindow.once('focus', () => mainWindow.flashFrame(false));
mainWindow.showInactive();
mainWindow.flashFrame(true);
}
}
});
ipcRenderer.on('render-taskbar-icon', (event, messageCount) => {
// Create a canvas from unread messages
function createOverlayIcon(messageCount) {
const canvas = document.createElement('canvas');
canvas.height = 128;
canvas.width = 128;
const ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.fillStyle = 'red';
ctx.arc(64, 64, 64, 0, 2 * Math.PI);
ctx.fill();
ctx.fillStyle = '#ffffff';
ctx.textAlign = 'center';
canvas.style.letterSpacing = '-4px';
ctx.font = 'bold 92px sans-serif';
ctx.fillText(String(Math.min(99, messageCount)), 64, 98);
return canvas;
}
ipcRenderer.send('update-taskbar-icon', createOverlayIcon(messageCount).toDataURL(), String(messageCount));
});
servers.restoreActive();
updatePreferences();
updateServers();
};
This diff is collapsed.
/* globals $ */
import { remote, ipcRenderer } from 'electron';
import { ipcRenderer } from 'electron';
import i18n from '../i18n/index.js';
import attachEvents from './events';
import servers from './servers';
import sidebar from './sidebar';
import webview from './webview';
import tray from './tray';
import './menus';
sidebar.on('badge-setted', function() {
const badge = sidebar.getGlobalBadge();
tray.showTrayAlert(badge);
});
export const start = function() {
const defaultInstance = 'https://open.rocket.chat';
......@@ -28,6 +22,7 @@ export const start = function() {
if (!navigator.onLine) {
offline();
}
window.addEventListener('online', online);
window.addEventListener('offline', offline);
// end connection check
......@@ -37,9 +32,9 @@ export const start = function() {
const button = form.querySelector('[type="submit"]');
const invalidUrl = form.querySelector('#invalidUrl');
window.addEventListener('load', function() {
hostField.focus();
});
window.addEventListener('load', () => hostField.focus());
window.addEventListener('focus', () => webview.focusActive());
function validateHost() {
return new Promise(function(resolve, reject) {
......@@ -118,30 +113,6 @@ export const start = function() {
validateHost().then(function() {}, function() {});
});
ipcRenderer.on('render-taskbar-icon', (event, messageCount) => {
// Create a canvas from unread messages
function createOverlayIcon(messageCount) {
const canvas = document.createElement('canvas');
canvas.height = 128;
canvas.width = 128;
const ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.fillStyle = 'red';
ctx.arc(64, 64, 64, 0, 2 * Math.PI);
ctx.fill();
ctx.fillStyle = '#ffffff';
ctx.textAlign = 'center';
canvas.style.letterSpacing = '-4px';
ctx.font = 'bold 92px sans-serif';
ctx.fillText(String(Math.min(99, messageCount)), 64, 98);
return canvas;
}
ipcRenderer.send('update-taskbar-icon', createOverlayIcon(messageCount).toDataURL(), String(messageCount));
});
const submit = function() {
validateHost().then(function() {
const input = form.querySelector('[name="host"]');
......@@ -182,20 +153,5 @@ export const start = function() {
webview.showLanding();
});
servers.restoreActive();
attachEvents();
};
window.addEventListener('focus', function() {
webview.focusActive();
});
webview.on('ipc-message-unread-changed', (hostUrl, [count]) => {
if (typeof count === 'number' && localStorage.getItem('showWindowOnUnreadChanged') === 'true') {
const mainWindow = remote.getCurrentWindow();
if (!mainWindow.isFocused()) {
mainWindow.once('focus', () => mainWindow.flashFrame(false));
mainWindow.showInactive();
mainWindow.flashFrame(true);
}
}
});
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