Commit 670a27d7 authored by Tasso Evangelista's avatar Tasso Evangelista Committed by GitHub

Merge pull request #895 from RocketChat/fix/menubar

[FIX] Menus
parents 1547d1c5 7b36e879
......@@ -22,7 +22,9 @@
"release-mas-dev": "build --publish never --mac mas-dev --c.mac.provisioningProfile=Development.provisionprofile",
"lint": "eslint src",
"pretest": "gulp build-unit-tests --env=test",
"test": "electron-mocha app/specs.js.autogenerated --renderer --require source-map-support/register",
"test-main": "electron-mocha --require source-map-support/register app/main.specs.js",
"test-renderer": "electron-mocha --require source-map-support/register --renderer app/renderer.specs.js",
"test": "npm-run-all test-main test-renderer",
"coverage": "npm test -- -R scripts/istanbul-reporter",
"pree2e": "gulp build-e2e-tests --env=test",
"e2e": "mocha app/e2e.js.autogenerated --require source-map-support/register"
......@@ -55,10 +57,12 @@
"istanbul": "^0.4.5",
"minimist": "^1.2.0",
"mocha": "^5.2.0",
"npm-run-all": "^4.1.3",
"rollup": "^0.66.1",
"rollup-plugin-istanbul": "^2.0.1",
"rollup-plugin-json": "^3.1.0",
"run-sequence": "^2.2.1",
"sinon": "^6.3.4",
"spectron": "^4.0.0"
},
"devEngines": {
......
......@@ -14,7 +14,5 @@ Bugsnag.metaData = {
Bugsnag.appVersion = app.getVersion();
app.setAppUserModelId('chat.rocket');
window.$ = window.jQuery = require('./vendor/jquery-3.1.1');
start();
......@@ -3,25 +3,25 @@ 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';
import { addServer, createMainWindow, getMainWindow } from './background/mainWindow';
import menus from './background/menus';
import './background/screenshare';
import i18n from './i18n/index.js';
import env from './env';
export { default as showAboutDialog } from './background/aboutDialog';
export { default as remoteServers } from './background/servers';
export { default as certificate } from './background/certificate';
export { certificate, menus };
process.env.GOOGLE_API_KEY = 'AIzaSyADqUh_c1Qhji3Cp1NE43YrcpuPkmhXD-c';
const isMacOS = process.platform === 'darwin';
const unsetDefaultApplicationMenu = () => {
if (!isMacOS) {
if (process.platform !== 'darwin') {
Menu.setApplicationMenu(null);
return;
}
......@@ -29,7 +29,7 @@ const unsetDefaultApplicationMenu = () => {
const emptyMenuTemplate = [{
submenu: [
{
label: i18n.__('Quit_App', app.getName()),
label: i18n.__('&Quit %s', app.getName()),
accelerator: 'CommandOrControl+Q',
click() {
app.quit();
......@@ -85,15 +85,24 @@ app.on('open-url', (event, url) => {
addServers([url]);
});
app.on('window-all-closed', () => {
app.quit();
});
if (!app.isDefaultProtocolClient('rocketchat')) {
app.setAsDefaultProtocolClient('rocketchat');
}
app.setAppUserModelId('chat.rocket');
if (process.platform === 'linux') {
app.disableHardwareAcceleration();
}
app.on('ready', () => {
unsetDefaultApplicationMenu();
setUserDataPath();
migrateOlderVersionUserData();
if (!app.isDefaultProtocolClient('rocketchat')) {
app.setAsDefaultProtocolClient('rocketchat');
}
createMainWindow();
getMainWindow().then((mainWindow) => certificate.initWindow(mainWindow));
......@@ -101,30 +110,6 @@ app.on('ready', () => {
autoUpdate();
});
app.on('window-all-closed', () => {
app.quit();
});
ipcMain.on('getSystemIdleTime', (event) => {
event.returnValue = idle.getIdleTime();
});
ipcMain.on('show-about-dialog', () => {
getMainWindow().then((mainWindow) => {
const win = new BrowserWindow({
title: i18n.__('About', 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 { app, Menu } from 'electron';
import { EventEmitter } from 'events';
import { getMainWindow } from './mainWindow';
import i18n from '../i18n/index.js';
const createTemplate = ({
appName,
servers = [],
currentServerUrl = null,
showTrayIcon = true,
showFullScreen = false,
showMenuBar = true,
showServerList = true,
showWindowOnUnreadChanged = false,
}, events) => ([
{
label: process.platform === 'darwin' ? appName : i18n.__('&File'),
submenu: [
...(process.platform === 'darwin' ? [
{
label: i18n.__('About %s', appName),
click: () => events.emit('about'),
},
{
type: 'separator',
},
{
submenu: [],
role: 'services',
},
{
type: 'separator',
},
{
accelerator: 'Command+H',
role: 'hide',
},
{
accelerator: 'Command+Alt+H',
role: 'hideothers',
},
{
role: 'unhide',
},
{
type: 'separator',
},
] : []),
// {
// label: i18n.__('Preferences'),
// accelerator: 'CommandOrControl+,',
// click: () => events.emit('preferences'),
// },
...(process.platform !== 'darwin' ? [
{
label: i18n.__('Add &new server'),
accelerator: 'CommandOrControl+N',
click: () => events.emit('add-new-server'),
},
] : []),
{
type: 'separator',
},
{
id: 'quit',
label: i18n.__('&Quit %s', appName),
accelerator: 'CommandOrControl+Q',
click: () => events.emit('quit'),
},
],
},
{
label: i18n.__('&Edit'),
submenu: [
{
label: i18n.__('&Undo'),
accelerator: 'CommandOrControl+Z',
role: 'undo',
},
{
label: i18n.__('&Redo'),
accelerator: process.platform === 'win32' ? 'Control+Y' : 'CommandOrControl+Shift+Z',
role: 'redo',
},
{
type: 'separator',
},
{
label: i18n.__('Cu&t'),
accelerator: 'CommandOrControl+X',
role: 'cut',
},
{
label: i18n.__('&Copy'),
accelerator: 'CommandOrControl+C',
role: 'copy',
},
{
label: i18n.__('&Paste'),
accelerator: 'CommandOrControl+V',
role: 'paste',
},
{
label: i18n.__('Select &all'),
accelerator: 'CommandOrControl+A',
role: 'selectall',
},
],
},
{
label: i18n.__('&View'),
submenu: [
{
label: i18n.__('&Reload'),
accelerator: 'CommandOrControl+R',
click: () => events.emit('reload-server'),
},
{
label: i18n.__('Reload ignoring cache'),
click: () => events.emit('reload-server', { ignoringCache: true }),
},
{
label: i18n.__('Clear trusted certificates'),
click: () => events.emit('reload-server', { ignoringCache: true, clearCertificates: true }),
},
{
label: i18n.__('Open &DevTools'),
accelerator: process.platform === 'darwin' ? 'Command+Alt+I' : 'Ctrl+Shift+I',
click: () => events.emit('open-devtools-for-server'),
},
{
type: 'separator',
},
{
label: i18n.__('&Back'),
accelerator: process.platform === 'darwin' ? 'Command+Left' : 'Alt+Left',
click: () => events.emit('go-back'),
},
{
label: i18n.__('&Forward'),
accelerator: process.platform === 'darwin' ? 'Command+Right' : 'Alt+Right',
click: () => events.emit('go-forward'),
},
{
type: 'separator',
},
{
label: i18n.__('Tray icon'),
type: 'checkbox',
checked: showTrayIcon,
click: () => events.emit('toggle', 'showTrayIcon'),
},
...(process.platform === 'darwin' ? [
{
label: i18n.__('Full screen'),
type: 'checkbox',
checked: showFullScreen,
accelerator: 'Control+Command+F',
click: () => events.emit('toggle', 'showFullScreen'),
},
] : []),
...(process.platform !== 'darwin' ? [
{
label: i18n.__('Menu bar'),
type: 'checkbox',
checked: showMenuBar,
click: () => events.emit('toggle', 'showMenuBar'),
},
] : []),
{
label: i18n.__('Server list'),
type: 'checkbox',
checked: showServerList,
click: () => events.emit('toggle', 'showServerList'),
},
{
type: 'separator',
},
{
label: i18n.__('Reset zoom'),
accelerator: 'CommandOrControl+0',
role: 'resetzoom',
},
{
label: i18n.__('Zoom in'),
accelerator: 'CommandOrControl+Plus',
role: 'zoomin',
},
{
label: i18n.__('Zoom out'),
accelerator: 'CommandOrControl+-',
role: 'zoomout',
},
],
},
{
label: i18n.__('&Window'),
id: 'window',
role: 'window',
submenu: [
...(process.platform === 'darwin' ? [
{
label: i18n.__('Add &new server'),
accelerator: 'CommandOrControl+N',
click: () => events.emit('add-new-server'),
},
{
type: 'separator',
},
] : []),
...servers.map((host, i) => ({
label: host.title.replace(/&/g, '&&'),
type: currentServerUrl ? 'radio' : 'normal',
checked: currentServerUrl === host.url,
accelerator: `CommandOrControl+${ i + 1 }`,
id: host.url,
click: () => events.emit('select-server', host),
})),
{
type: 'separator',
},
{
label: i18n.__('&Reload'),
accelerator: 'CommandOrControl+Shift+R',
click: () => events.emit('reload-app'),
},
{
label: i18n.__('Toggle &DevTools'),
click: () => events.emit('toggle-devtools'),
},
{
type: 'separator',
},
{
label: i18n.__('Show on unread messages'),
type: 'checkbox',
checked: showWindowOnUnreadChanged,
click: () => events.emit('toggle', 'showWindowOnUnreadChanged'),
},
{
type: 'separator',
},
{
label: i18n.__('Minimize'),
accelerator: 'CommandOrControl+M',
role: 'minimize',
},
{
label: i18n.__('Close'),
accelerator: 'CommandOrControl+W',
role: 'close',
},
],
},
{
label: i18n.__('&Help'),
role: 'help',
submenu: [
{
label: i18n.__('Documentation'),
click: () => events.emit('open-url', 'https://rocket.chat/docs'),
},
{
type: 'separator',
},
{
label: i18n.__('Report issue'),
click: () => events.emit('open-url', 'https://github.com/RocketChat/Rocket.Chat/issues'),
},
{
label: i18n.__('Reset app data'),
click: () => events.emit('reset-app-data'),
},
{
type: 'separator',
},
{
label: i18n.__('Learn more'),
click: () => events.emit('open-url', 'https://rocket.chat'),
},
{
id: 'about',
label: i18n.__('About %s', appName),
click: () => events.emit('about'),
},
],
},
]);
class Menus extends EventEmitter {
constructor() {
super();
this.state = {};
this.on('update', this.update.bind(this));
}
setState(partialState) {
this.state = {
...this.state,
...partialState,
};
this.update();
}
getItem(id) {
return Menu.getApplicationMenu().getMenuItemById(id);
}
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.state;
const mainWindow = await getMainWindow();
mainWindow.setAutoHideMenuBar(!showMenuBar);
mainWindow.setMenuBarVisibility(!!showMenuBar);
}
}
}
export default new Menus();
/* eslint-env node, mocha */
import { Menu } from 'electron';
import { expect } from 'chai';
import sinon from 'sinon';
import menus from './menus';
describe('menus', () => {
let menu;
beforeEach(async() => {
await menus.update();
menu = Menu.getApplicationMenu();
});
it('should be the application menu', () => {
expect(menu.items).to.not.be.empty;
});
it('should update on set state', () => {
sinon.spy(menus, 'update');
menus.setState({});
expect(menus.update.calledOnce).to.be.true;
});
const itShouldHaveAnItem = (id, which) => it(`should have an item "${ id }"`, () => {
const item = menus.getItem(id);
expect(item).to.not.be.null;
which && which(item);
});
itShouldHaveAnItem('quit', (item) => {
const spy = sinon.spy();
menus.on('quit', spy);
item.click();
expect(spy.called).to.be.true;
});
itShouldHaveAnItem('about', (item) => {
const spy = sinon.spy();
menus.on('about', spy);
item.click();
expect(spy.called).to.be.true;
});
});
{
"About": "Über %s",
"About %s": "Über %s",
"Add": "Hinzufügen",
"Add_host_to_servers": "Möchten Sie \"%s\" zu Ihrer Serverliste hinzufügen?",
"Add_new_server": "Neuen Server hinzufügen",
"Add new server": "Neuen Server hinzufügen",
"Add &new server": "&Neuen server hinzufügen",
"Add_Server": "Server hinzufügen",
"Application_Reload": "Programm - Neustart",
"Application_Toggle_DevTools": "Programm - DevTools ein-/ausblenden",
"Auth_needed_try": "Auth benötigt, probieren Sie %s",
"Bring_All_to_Front": "Alle nach vorne bringen",
"Cancel": "Abbrechen",
......@@ -16,26 +15,27 @@
"Check_for_Updates": "Auf Aktualisierungen prüfen",
"Check_for_Updates_on_Start": "Beim Start auf Aktualisierungen prüfen",
"Clear": "Löschen",
"Clear_Trusted_Certificates": "Vertraute Zertifikate löschen",
"Clear trusted certificates": "Vertraute zertifikate löschen",
"Close": "Schließen",
"Connect": "Verbinden",
"Copy": "Kopieren",
"&Copy": "&Kopieren",
"Copyright": "Copyright %s",
"Current_Server_Reload": "Aktuellen Server - neu laden",
"Current_Server_Toggle_DevTools": "Aktueller Server - DevTools ein-/ausblenden",
"Current_Version": "Aktuelle Version:",
"Cut": "Ausschneiden",
"Cu&t": "&Ausschneiden",
"Development": "Entwicklung",
"Edit": "Bearbeiten",
"Documentation": "Dokumentation",
"&Edit": "&Bearbeiten",
"Enter_your_server_URL": "Server URL eingeben",
"Help": "Hilfe",
"Help_Name": "%s Hilfe",
"&File": "&Datei",
"Full screen": "Vollbildmodus",
"&Help": "&Hilfe",
"Hide": "Ausblenden",
"History": "Verlauf",
"Host_not_validated": "Der Host \"%s\" konnte nicht validiert werden, er wurde also nicht hinzugefügt.",
"Invalid_Host": "Ungültiger Host",
"Invalid_url": "Ungültige url",
"Learn_More": "Mehr erfahren",
"Learn more": "Mehr erfahren",
"Menu bar": "MenuBar",
"Minimize": "Minimieren",
"More_spelling_suggestions": "Mehr Rechtschreibvorschläge",
"Open_Link": "Link öffnen",
......@@ -43,30 +43,30 @@
"No": "Nein",
"No_suggestions": "Keine Vorschläge",
"No_valid_server_found": "Kein gültiger Server unter dieser URL gefunden",
"Open_DevTools": "DevTools öffnen",
"Original_Zoom": "Originalgröße",
"Paste": "Einfügen",
"Open DevTools": "DevTools öffnen",
"Open &DevTools": "&DevTools öffnen",
"&Paste": "&Einfügen",
"Quit": "Beenden",
"Quit_App": "%s beenden",
"Redo": "Wiederholen",
"&Quit %s": "%s &beenden",
"&Redo": "Wieder&holen",
"Reload": "Neu laden",
"&Reload": "&Neu laden",
"Reload_server": "Server neu laden",
"Remove_server": "Server entfernen",
"Report_Issue": "Problem melden",
"Reset_App_Data": "Appdaten löschen",
"Report issue": "Problem melden",
"Reset app data": "Appdaten löschen",
"Reset zoom": "Originalgröße",
"Select_a_screen_to_share": "Wählen Sie den zu teilenden Bildschirm aus",
"Select_All": "Alle auswählen",
"Select &all": "Alle auswählen",
"Server_Failed_to_Load": "Server konnte nicht geladen werden",
"Server list": "Server liste",
"Share_Your_Screen": "Bildschirm teilen",
"Show": "Anzeigen",
"Spelling_languages": "Sprachen für die Rechtschreibprüfung",
"Timeout_trying_to_connect": "Zeitüberschreitung beim Verbindungsaufbau",
"Toggle_DevTools": "DevTools ein-/ausblenden",
"Toggle_Menu_Bar": "MenuBar ein-/ausblenden",
"Toggle_Server_List": "Server Liste ein-/ausblenden",
"Toggle_Tray_Icon": "Symbole in Menüleiste ein-/ausblenden",
"Toggle_Full_Screen": "Vollbildmodus",
"Undo": "Widerrufen",
"Toggle &DevTools": "&DevTools ein-/ausblenden",
"Tray icon": "Symbole in menüleiste",
"&Undo": "Wider&rufen",
"Update_Available": "Aktualisierung verfügbar",
"Update_Available_message": "Eine neue Version der Rocket.Chat Desktop App ist verfügbar!",
"Update_Available_New": "Neue Aktualisierung verfügbar",
......@@ -87,9 +87,9 @@
"Update_skip_version": "Diese Version überspringen",
"Validating": "Validieren...",
"Version": "Version",
"View": "Darstellung",
"Window": "Fenster",
"&View": "Darstellung",
"&Window": "&Fenster",
"Yes": "Ja",
"Zoom_In": "Vergrößern",
"Zoom_Out": "Verkleinern"
"Zoom in": "Vergrößern",
"Zoom out": "Verkleinern"
}
{
"About": "About %s",
"About %s": "About %s",
"Add": "Add",
"Add_host_to_servers": "Do you want to add \"%s\" to your list of servers?",
"Add_new_server": "Add New Server",
"Add new server": "Add new server",
"Add &new server": "Add &new server",
"Add_Server": "Add Server",
"Application_Reload": "Application - Reload",
"Application_Toggle_DevTools": "Application - Toggle DevTools",
"Auth_needed_try": "Auth needed, try %s",
"Back": "Back",
"&Back": "&Back",
"Bring_All_to_Front": "Bring All to Front",
"Browse_for_language": "Browse for language...",
"Cancel": "Cancel",
......@@ -18,30 +17,31 @@
"Check_for_Updates": "Check for Updates",
"Check_for_Updates_on_Start": "Check for Updates on Start",
"Clear": "Clear",
"Clear_Trusted_Certificates": "Clear Trusted Certificates",
"Clear trusted certificates": "Clear trusted certificates",
"Close": "Close",
"Connect": "Connect",
"Copy": "Copy",
"&Copy": "&Copy",
"Copyright": "Copyright %s",
"Current_Server_Reload": "Current Server - Reload",
"Current_Server_Toggle_DevTools": "Current Server - Toggle DevTools",
"Current_Version": "Current Version:",
"Cut": "Cut",
"Cu&t": "Cu&t",
"Development": "Development",
"Dictionary_install_error": "Error installing dictionary",
"Edit": "Edit",
"Documentation": "Documentation",
"&Edit": "&Edit",
"Enter_your_server_URL": "Enter your server URL",
"Error": "Error",
"Forward": "Forward",
"Help": "Help",
"Help_Name": "%s Help",
"&File": "&File",
"&Forward": "&Forward",
"Full screen": "Full screen",
"&Help": "&Help",
"Hide": "Hide",
"History": "History",
"Host_not_validated": "The host \"%s\" could not be validated, so was not added.",
"Houston_we_have_a_problem": "Houston, we have a problem",
"Invalid_Host": "Invalid Host",
"Invalid_url": "Invalid url",
"Learn_More": "Learn More",
"Learn more": "Learn more",
"Menu bar": "Menu bar",
"Minimize": "Minimize",
"More_spelling_suggestions": "More spelling suggestions",
"New_Version": "New Version:",
......@@ -49,32 +49,34 @@
"No_suggestions": "No suggestions",
"No updates are available.": "No updates are available.",
"No_valid_server_found": "No valid server found at the URL",
"Open_DevTools": "Open DevTools",
"Open DevTools": "Open DevTools",
"Open &DevTools": "Open &DevTools",
"Open_Language_Dictionary": "Open Language Dictionary",
"Open_Link": "Open link",
"Original_Zoom": "Original Zoom",
"Paste": "Paste",
"&Paste": "&Paste",
"Quit": "Quit",
"Quit_App": "Quit %s",
"Redo": "Redo",
"&Quit %s": "&Quit %s",
"&Redo": "&Redo",
"Reload": "Reload",
"&Reload": "&Reload",
"Reload ignoring cache": "Reload ignoring cache",
"Reload_server": "Reload s