Officially support the system tray on Windows

This commit is contained in:
Evan Hahn 2021-06-29 12:18:03 -05:00 committed by GitHub
parent 23acbf284e
commit af1f2ea449
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 968 additions and 194 deletions

88
main.js
View file

@ -65,12 +65,6 @@ function getMainWindow() {
return mainWindow;
}
// Tray icon and related objects
let tray = null;
const startInTray = process.argv.some(arg => arg === '--start-in-tray');
const usingTrayIcon =
startInTray || process.argv.some(arg => arg === '--use-tray-icon');
const config = require('./app/config').default;
// Very important to put before the single instance check, since it is based on the
@ -91,8 +85,13 @@ const attachments = require('./app/attachments');
const attachmentChannel = require('./app/attachment_channel');
const bounce = require('./ts/services/bounce');
const updater = require('./ts/updater/index');
const createTrayIcon = require('./app/tray_icon').default;
const dockIcon = require('./ts/dock_icon');
const { SystemTrayService } = require('./app/SystemTrayService');
const { SystemTraySettingCache } = require('./app/SystemTraySettingCache');
const {
SystemTraySetting,
shouldMinimizeToSystemTray,
parseSystemTraySetting,
} = require('./ts/types/SystemTraySetting');
const ephemeralConfig = require('./app/ephemeral_config');
const logging = require('./ts/logging/main_process_logging');
const { MainSQL } = require('./ts/sql/main');
@ -127,6 +126,10 @@ const { PowerChannel } = require('./ts/main/powerChannel');
const { maybeParseUrl, setUrlSearchParams } = require('./ts/util/url');
const sql = new MainSQL();
let systemTrayService;
const systemTraySettingCache = new SystemTraySettingCache(sql, process.argv);
const challengeHandler = new ChallengeMainHandler();
let sqlInitTimeStart = 0;
@ -174,14 +177,6 @@ function showWindow() {
} else {
mainWindow.show();
}
// toggle the visibility of the show/hide tray icon menu entries
if (tray) {
tray.updateContextMenu();
}
// show the app on the Dock in case it was hidden before
dockIcon.show();
}
if (!process.mas) {
@ -394,6 +389,10 @@ async function createWindow() {
delete windowOptions.autoHideMenuBar;
}
const startInTray =
(await systemTraySettingCache.get()) ===
SystemTraySetting.MinimizeToAndStartInSystemTray;
const visibleOnAnyScreen = _.some(screen.getAllDisplays(), display => {
if (!_.isNumber(windowOptions.x) || !_.isNumber(windowOptions.y)) {
return false;
@ -416,12 +415,15 @@ async function createWindow() {
mainWindow = new BrowserWindow(windowOptions);
mainWindowCreated = true;
setupSpellChecker(mainWindow, locale.messages);
if (!usingTrayIcon && windowConfig && windowConfig.maximized) {
if (!startInTray && windowConfig) {
mainWindow.maximize();
}
if (!usingTrayIcon && windowConfig && windowConfig.fullscreen) {
if (!startInTray && windowConfig && windowConfig.fullscreen) {
mainWindow.setFullScreen(true);
}
if (systemTrayService) {
systemTrayService.setMainWindow(mainWindow);
}
function captureAndSaveWindowStats() {
if (!mainWindow) {
@ -533,17 +535,10 @@ async function createWindow() {
// On Mac, or on other platforms when the tray icon is in use, the window
// should be only hidden, not closed, when the user clicks the close button
const usingTrayIcon = shouldMinimizeToSystemTray(
await systemTraySettingCache.get()
);
if (!windowState.shouldQuit() && (usingTrayIcon || OS.isMacOS())) {
// toggle the visibility of the show/hide tray icon menu entries
if (tray) {
tray.updateContextMenu();
}
// hide the app from the Dock on macOS if the tray icon is enabled
if (usingTrayIcon) {
dockIcon.hide();
}
return;
}
@ -560,7 +555,10 @@ async function createWindow() {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
mainWindow = undefined;
if (systemTrayService) {
systemTrayService.setMainWindow(mainWindow);
}
});
mainWindow.on('enter-full-screen', () => {
@ -580,7 +578,6 @@ async function createWindow() {
return;
}
// allow to start minimised in tray
if (!startInTray) {
console.log('showing main window');
mainWindow.show();
@ -1360,12 +1357,14 @@ app.on('ready', async () => {
ready = true;
if (usingTrayIcon) {
tray = createTrayIcon(getMainWindow, locale.messages);
}
setupMenu();
systemTrayService = new SystemTrayService({ messages: locale.messages });
systemTrayService.setMainWindow(mainWindow);
systemTrayService.setEnabled(
shouldMinimizeToSystemTray(await systemTraySettingCache.get())
);
ensureFilePermissions([
'config.json',
'sql/db.sqlite',
@ -1561,6 +1560,19 @@ ipc.on('set-menu-bar-visibility', (event, visibility) => {
}
});
ipc.on('update-system-tray-setting', (
_event,
rawSystemTraySetting /* : Readonly<unknown> */
) => {
const systemTraySetting = parseSystemTraySetting(rawSystemTraySetting);
systemTraySettingCache.set(systemTraySetting);
if (systemTrayService) {
const isEnabled = shouldMinimizeToSystemTray(systemTraySetting);
systemTrayService.setEnabled(isEnabled);
}
});
ipc.on('close-about', () => {
if (aboutWindow) {
aboutWindow.close();
@ -1583,9 +1595,9 @@ ipc.on('show-screen-share', (event, sourceName) => {
showScreenShareWindow(sourceName);
});
ipc.on('update-tray-icon', (event, unreadCount) => {
if (tray) {
tray.updateIcon(unreadCount);
ipc.on('update-tray-icon', (_event, unreadCount) => {
if (systemTrayService) {
systemTrayService.setUnreadCount(unreadCount);
}
});
@ -1645,6 +1657,8 @@ installSettingsGetter('theme-setting');
installSettingsSetter('theme-setting');
installSettingsGetter('hide-menu-bar');
installSettingsSetter('hide-menu-bar');
installSettingsGetter('system-tray-setting');
installSettingsSetter('system-tray-setting');
installSettingsGetter('notification-setting');
installSettingsSetter('notification-setting');