Migrate app to TypeScript (#1637)

* Initial setup and migrated src/common

* WIP

* WIP

* WIP

* Main module basically finished

* Renderer process migrated

* Added CI step and some fixes

* Fixed remainder of issues and added proper ESLint config

* Fixed a couple issues

* Progress!

* Some more fixes

* Fixed a test

* Fix build step

* PR feedback
This commit is contained in:
Devin Binnie
2021-06-28 09:51:23 -04:00
committed by GitHub
parent 422673a740
commit 1b3d0eac8f
115 changed files with 16246 additions and 9921 deletions

123
src/main/tray/tray.ts Normal file
View File

@@ -0,0 +1,123 @@
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import path from 'path';
import {app, nativeImage, Tray, systemPreferences, nativeTheme} from 'electron';
import {UPDATE_TRAY} from 'common/communication';
import * as WindowManager from '../windows/windowManager';
import * as AppState from '../appState';
const assetsDir = path.resolve(app.getAppPath(), 'assets');
let trayImages: Record<string, Electron.NativeImage>;
let trayIcon: Tray;
let lastStatus = 'normal';
let lastMessage = app.name;
export function refreshTrayImages(trayIconTheme: string) {
const winTheme = nativeTheme.shouldUseDarkColors ? 'dark' : 'light';
switch (process.platform) {
case 'win32':
trayImages = {
normal: nativeImage.createFromPath(path.resolve(assetsDir, `windows/tray_${winTheme}.ico`)),
unread: nativeImage.createFromPath(path.resolve(assetsDir, `windows/tray_${winTheme}_unread.ico`)),
mention: nativeImage.createFromPath(path.resolve(assetsDir, `windows/tray_${winTheme}_mention.ico`)),
};
break;
case 'darwin':
{
const osxNormal = nativeImage.createFromPath(path.resolve(assetsDir, 'osx/menuIcons/MenuIcon16Template.png'));
const osxUnread = nativeImage.createFromPath(path.resolve(assetsDir, 'osx/menuIcons/MenuIconUnread16Template.png'));
osxNormal.setTemplateImage(true);
osxUnread.setTemplateImage(true);
trayImages = {
normal: osxNormal,
unread: osxUnread,
mention: osxUnread,
};
break;
}
case 'linux':
{
if (trayIconTheme === 'dark') {
trayImages = {
normal: nativeImage.createFromPath(path.resolve(assetsDir, 'linux', 'top_bar_dark_16.png')),
unread: nativeImage.createFromPath(path.resolve(assetsDir, 'linux', 'top_bar_dark_unread_16.png')),
mention: nativeImage.createFromPath(path.resolve(assetsDir, 'linux', 'top_bar_dark_mention_16.png')),
};
} else {
//Fallback for invalid theme setting
trayImages = {
normal: nativeImage.createFromPath(path.resolve(assetsDir, 'linux', 'top_bar_light_16.png')),
unread: nativeImage.createFromPath(path.resolve(assetsDir, 'linux', 'top_bar_light_unread_16.png')),
mention: nativeImage.createFromPath(path.resolve(assetsDir, 'linux', 'top_bar_light_mention_16.png')),
};
}
break;
}
default:
trayImages = {};
}
if (trayIcon) {
setTray(lastStatus, lastMessage);
}
return trayImages;
}
export function setupTray(icontheme: string) {
refreshTrayImages(icontheme);
trayIcon = new Tray(trayImages.normal);
if (process.platform === 'darwin') {
systemPreferences.subscribeNotification('AppleInterfaceThemeChangedNotification', () => {
trayIcon.setImage(trayImages.normal);
});
}
trayIcon.setToolTip(app.name);
trayIcon.on('click', () => {
WindowManager.restoreMain();
});
trayIcon.on('right-click', () => {
trayIcon.popUpContextMenu();
});
trayIcon.on('balloon-click', () => {
WindowManager.restoreMain();
});
AppState.on(UPDATE_TRAY, (anyExpired, anyMentions, anyUnreads) => {
if (anyExpired) {
setTray('mention', 'Session Expired: Please sign in to continue receiving notifications.');
} else if (anyMentions) {
setTray('mention', 'You have been mentioned');
} else if (anyUnreads) {
setTray('unread', 'You have unread channels');
} else {
setTray('normal', app.name);
}
});
}
function setTray(status: string, message: string) {
lastStatus = status;
lastMessage = message;
trayIcon.setImage(trayImages[status]);
trayIcon.setToolTip(message);
}
export function destroyTray() {
if (trayIcon && process.platform === 'win32') {
trayIcon.destroy();
}
}
export function setTrayMenu(tMenu: Electron.Menu) {
if (trayIcon) {
trayIcon.setContextMenu(tMenu);
}
}