[MM-51874] Migrate loading screen to singleton (#2655)

* Migrate loadingScreen to singleton

* REVERT ME when MainWindow singleton changes are merged

* Revert "REVERT ME when MainWindow singleton changes are merged"

This reverts commit 2de5520117b9aefb8eeb161d493de7cb275f7a5b.
This commit is contained in:
Devin Binnie
2023-04-04 11:49:40 -04:00
committed by GitHub
parent 22ec280945
commit 39150137b6
10 changed files with 204 additions and 170 deletions

View File

@@ -14,7 +14,6 @@ import {
UPDATE_TARGET_URL,
LOAD_SUCCESS,
LOAD_FAILED,
TOGGLE_LOADING_SCREEN_VISIBILITY,
LOADSCREEN_END,
SET_ACTIVE_VIEW,
OPEN_TAB,
@@ -22,7 +21,6 @@ import {
UPDATE_LAST_ACTIVE,
UPDATE_URL_VIEW_WIDTH,
MAIN_WINDOW_SHOWN,
DARK_MODE_CHANGE,
} from 'common/communication';
import Config from 'common/config';
import urlUtils, {equalUrlsIgnoringSubpath} from 'common/utils/url';
@@ -37,21 +35,16 @@ import {localizeMessage} from 'main/i18nManager';
import {ServerInfo} from 'main/server/serverInfo';
import MainWindow from 'main/windows/mainWindow';
import {getLocalURLString, getLocalPreload, getWindowBoundaries} from '../utils';
import {getLocalURLString, getLocalPreload} from '../utils';
import {MattermostView} from './MattermostView';
import modalManager from './modalManager';
import WebContentsEventManager from './webContentEvents';
import LoadingScreen from './loadingScreen';
const URL_VIEW_DURATION = 10 * SECOND;
const URL_VIEW_HEIGHT = 20;
export enum LoadingScreenState {
VISIBLE = 1,
FADING = 2,
HIDDEN = 3,
}
export class ViewManager {
lastActiveServer?: number;
viewOptions: BrowserViewConstructorOptions;
@@ -60,15 +53,12 @@ export class ViewManager {
currentView?: string;
urlView?: BrowserView;
urlViewCancel?: () => void;
loadingScreen?: BrowserView;
loadingScreenState: LoadingScreenState;
constructor() {
this.lastActiveServer = Config.lastActiveTeam;
this.viewOptions = {webPreferences: {spellcheck: Config.useSpellChecker}};
this.views = new Map(); // keep in mind that this doesn't need to hold server order, only tabs on the renderer need that.
this.closedViews = new Map();
this.loadingScreenState = LoadingScreenState.HIDDEN;
}
getServers = () => {
@@ -97,9 +87,6 @@ export class ViewManager {
if (this.closedViews.has(view.name)) {
this.closedViews.delete(view.name);
}
if (!this.loadingScreen) {
this.createLoadingScreen();
}
}
loadView = (srv: MattermostServer, serverInfo: ServerInfo, tab: Tab, url?: string) => {
@@ -242,7 +229,7 @@ export class ViewManager {
if (!newView.isErrored()) {
newView.show();
if (newView.needsLoadingScreen()) {
this.showLoadingScreen();
LoadingScreen.show();
}
}
MainWindow.get()?.webContents.send(SET_ACTIVE_VIEW, newView.tab.server.name, newView.tab.type);
@@ -290,7 +277,7 @@ export class ViewManager {
const view = this.views.get(server);
if (view && this.getCurrentView() === view) {
this.showByName(this.currentView!);
this.fadeLoadingScreen();
LoadingScreen.fade();
}
}
@@ -314,7 +301,7 @@ export class ViewManager {
failLoading = (tabName: string) => {
log.debug('viewManager.failLoading', tabName);
this.fadeLoadingScreen();
LoadingScreen.fade();
if (this.currentView === tabName) {
this.getCurrentView()?.hide();
}
@@ -417,87 +404,16 @@ export class ViewManager {
}
}
setLoadingScreenBounds = () => {
const mainWindow = MainWindow.get();
if (!mainWindow) {
return;
}
this.loadingScreen?.setBounds(getWindowBoundaries(mainWindow));
}
createLoadingScreen = () => {
const preload = getLocalPreload('desktopAPI.js');
this.loadingScreen = new BrowserView({webPreferences: {
preload,
// Workaround for this issue: https://github.com/electron/electron/issues/30993
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
transparent: true,
}});
const localURL = getLocalURLString('loadingScreen.html');
this.loadingScreen.webContents.loadURL(localURL);
}
showLoadingScreen = () => {
const mainWindow = MainWindow.get();
if (!mainWindow) {
return;
}
if (!this.loadingScreen) {
this.createLoadingScreen();
}
this.loadingScreenState = LoadingScreenState.VISIBLE;
if (this.loadingScreen?.webContents.isLoading()) {
this.loadingScreen.webContents.once('did-finish-load', () => {
this.loadingScreen!.webContents.send(TOGGLE_LOADING_SCREEN_VISIBILITY, true);
});
} else {
this.loadingScreen!.webContents.send(TOGGLE_LOADING_SCREEN_VISIBILITY, true);
}
if (mainWindow.getBrowserViews().includes(this.loadingScreen!)) {
mainWindow.setTopBrowserView(this.loadingScreen!);
} else {
mainWindow.addBrowserView(this.loadingScreen!);
}
this.setLoadingScreenBounds();
}
fadeLoadingScreen = () => {
if (this.loadingScreen && this.loadingScreenState === LoadingScreenState.VISIBLE) {
this.loadingScreenState = LoadingScreenState.FADING;
this.loadingScreen.webContents.send(TOGGLE_LOADING_SCREEN_VISIBILITY, false);
}
}
hideLoadingScreen = () => {
if (this.loadingScreen && this.loadingScreenState !== LoadingScreenState.HIDDEN) {
this.loadingScreenState = LoadingScreenState.HIDDEN;
MainWindow.get()?.removeBrowserView(this.loadingScreen);
}
}
setServerInitialized = (server: string) => {
const view = this.views.get(server);
if (view) {
view.setInitialized();
if (this.getCurrentView() === view) {
this.fadeLoadingScreen();
LoadingScreen.fade();
}
}
}
updateLoadingScreenDarkMode = (darkMode: boolean) => {
if (this.loadingScreen) {
this.loadingScreen.webContents.send(DARK_MODE_CHANGE, darkMode);
}
}
deeplinkSuccess = (viewName: string) => {
log.debug('viewManager.deeplinkSuccess', viewName);