From c4e63fb3f9b27a8b1e00c52d9a391b2d107dce1f Mon Sep 17 00:00:00 2001 From: Devin Binnie <52460000+devinbinnie@users.noreply.github.com> Date: Fri, 17 Dec 2021 09:10:08 -0500 Subject: [PATCH] [MM-40574] Fix for race condition where new server modal didn't show initially on Linux, resizing issues (#1865) * FIx for linux modal sizing * Fix race condition, merge'd --- src/common/communication.ts | 1 + src/main/app/config.test.js | 6 +++--- src/main/app/config.ts | 4 ++-- src/main/app/initialize.test.js | 4 +++- src/main/app/initialize.ts | 5 ++--- src/main/app/intercom.ts | 13 +++++++++++++ src/main/views/modalManager.ts | 12 +++++++++++- src/main/views/modalView.ts | 18 +++++++++++------- src/main/windows/windowManager.ts | 2 ++ 9 files changed, 48 insertions(+), 17 deletions(-) diff --git a/src/common/communication.ts b/src/common/communication.ts index e0dc0fb6..c288d3eb 100644 --- a/src/common/communication.ts +++ b/src/common/communication.ts @@ -98,6 +98,7 @@ export const GET_AVAILABLE_SPELL_CHECKER_LANGUAGES = 'get-available-spell-checke export const GET_VIEW_NAME = 'get-view-name'; export const GET_VIEW_WEBCONTENTS_ID = 'get-view-webcontents-id'; +export const RESIZE_MODAL = 'resize-modal'; export const GET_MODAL_UNCLOSEABLE = 'get-modal-uncloseable'; export const MODAL_UNCLOSEABLE = 'modal-uncloseable'; diff --git a/src/main/app/config.test.js b/src/main/app/config.test.js index 1a018282..7764af46 100644 --- a/src/main/app/config.test.js +++ b/src/main/app/config.test.js @@ -7,7 +7,7 @@ import {RELOAD_CONFIGURATION} from 'common/communication'; import Config from 'common/config'; import {handleConfigUpdate} from 'main/app/config'; -import {handleNewServerModal} from 'main/app/intercom'; +import {addNewServerModalWhenMainWindowIsShown} from 'main/app/intercom'; import WindowManager from 'main/windows/windowManager'; import AutoLauncher from 'main/AutoLauncher'; @@ -32,7 +32,7 @@ jest.mock('main/app/utils', () => ({ updateServerInfos: jest.fn(), })); jest.mock('main/app/intercom', () => ({ - handleNewServerModal: jest.fn(), + addNewServerModalWhenMainWindowIsShown: jest.fn(), })); jest.mock('main/AutoLauncher', () => ({ enable: jest.fn(), @@ -98,7 +98,7 @@ describe('main/app/config', () => { Config.registryConfigData = {}; handleConfigUpdate({teams: []}); - expect(handleNewServerModal).toHaveBeenCalled(); + expect(addNewServerModalWhenMainWindowIsShown).toHaveBeenCalled(); Object.defineProperty(process, 'platform', { value: originalPlatform, diff --git a/src/main/app/config.ts b/src/main/app/config.ts index 29b6790d..4b668ff7 100644 --- a/src/main/app/config.ts +++ b/src/main/app/config.ts @@ -14,7 +14,7 @@ import {setUnreadBadgeSetting} from 'main/badge'; import {refreshTrayImages} from 'main/tray/tray'; import WindowManager from 'main/windows/windowManager'; -import {handleNewServerModal} from './intercom'; +import {addNewServerModalWhenMainWindowIsShown} from './intercom'; import {handleUpdateMenuEvent, updateServerInfos, updateSpellCheckerLocales} from './utils'; let didCheckForAddServerModal = false; @@ -58,7 +58,7 @@ export function handleConfigUpdate(newConfig: CombinedConfig) { updateServerInfos(newConfig.teams); WindowManager.initializeCurrentServerName(); if (newConfig.teams.length === 0) { - handleNewServerModal(); + addNewServerModalWhenMainWindowIsShown(); } } diff --git a/src/main/app/initialize.test.js b/src/main/app/initialize.test.js index 0c2afbbb..27ed8099 100644 --- a/src/main/app/initialize.test.js +++ b/src/main/app/initialize.test.js @@ -95,7 +95,9 @@ jest.mock('main/app/app', () => ({})); jest.mock('main/app/config', () => ({ handleConfigUpdate: jest.fn(), })); -jest.mock('main/app/intercom', () => ({})); +jest.mock('main/app/intercom', () => ({ + addNewServerModalWhenMainWindowIsShown: jest.fn(), +})); jest.mock('main/app/utils', () => ({ clearAppCache: jest.fn(), getDeeplinkingURL: jest.fn(), diff --git a/src/main/app/initialize.ts b/src/main/app/initialize.ts index d94cdee8..6b247f9e 100644 --- a/src/main/app/initialize.ts +++ b/src/main/app/initialize.ts @@ -63,6 +63,7 @@ import { } from './app'; import {handleConfigUpdate, handleDarkModeChange} from './config'; import { + addNewServerModalWhenMainWindowIsShown, handleAppVersion, handleCloseTab, handleEditServerModal, @@ -373,9 +374,7 @@ function initializeAfterAppReady() { // only check for non-Windows, as with Windows we have to wait for GPO teams if (process.platform !== 'win32' || typeof Config.registryConfigData !== 'undefined') { if (Config.teams.length === 0) { - setTimeout(() => { - handleNewServerModal(); - }, 200); + addNewServerModalWhenMainWindowIsShown(); } } } diff --git a/src/main/app/intercom.ts b/src/main/app/intercom.ts index d68b370b..77bf75b2 100644 --- a/src/main/app/intercom.ts +++ b/src/main/app/intercom.ts @@ -76,6 +76,19 @@ export function handleOpenTab(event: IpcMainEvent, serverName: string, tabName: Config.set('teams', teams); } +export function addNewServerModalWhenMainWindowIsShown() { + const mainWindow = WindowManager.getMainWindow(); + if (mainWindow) { + if (mainWindow.isVisible()) { + handleNewServerModal(); + } else { + mainWindow.once('show', () => { + handleNewServerModal(); + }); + } + } +} + export function handleNewServerModal() { const html = getLocalURLString('newServer.html'); diff --git a/src/main/views/modalManager.ts b/src/main/views/modalManager.ts index 60100da7..0c02d8c0 100644 --- a/src/main/views/modalManager.ts +++ b/src/main/views/modalManager.ts @@ -15,9 +15,11 @@ import { EMIT_CONFIGURATION, DARK_MODE_CHANGE, GET_MODAL_UNCLOSEABLE, + RESIZE_MODAL, } from 'common/communication'; -import WindowManager from '../windows/windowManager'; +import {getAdjustedWindowBoundaries} from 'main/utils'; +import WindowManager from 'main/windows/windowManager'; import {ModalView} from './modalView'; @@ -33,6 +35,7 @@ export class ModalManager { ipcMain.handle(RETRIEVE_MODAL_INFO, this.handleInfoRequest); ipcMain.on(MODAL_RESULT, this.handleModalResult); ipcMain.on(MODAL_CANCEL, this.handleModalCancel); + ipcMain.on(RESIZE_MODAL, this.handleResizeModal); ipcMain.on(EMIT_CONFIGURATION, this.handleEmitConfiguration); } @@ -118,6 +121,13 @@ export class ModalManager { return this.modalQueue.some((modal) => modal.isActive()); } + handleResizeModal = (event: IpcMainEvent, bounds: Electron.Rectangle) => { + if (this.modalQueue.length) { + const currentModal = this.modalQueue[0]; + currentModal.view.setBounds(getAdjustedWindowBoundaries(bounds.width, bounds.height)); + } + } + focusCurrentModal = () => { if (this.isModalDisplayed()) { this.modalQueue[0].view.webContents.focus(); diff --git a/src/main/views/modalView.ts b/src/main/views/modalView.ts index 02648dac..927e8d41 100644 --- a/src/main/views/modalView.ts +++ b/src/main/views/modalView.ts @@ -64,13 +64,17 @@ export class ModalView { this.windowAttached = win || this.window; this.windowAttached.addBrowserView(this.view); - this.view.setBounds(getWindowBoundaries(this.windowAttached)); - this.view.setAutoResize({ - height: true, - width: true, - horizontal: true, - vertical: true, - }); + + // Linux sometimes doesn't have the bound initialized correctly initially, so we wait to set them + const setBoundsFunction = () => { + this.view.setBounds(getWindowBoundaries(this.windowAttached!)); + }; + if (process.platform === 'linux') { + setTimeout(setBoundsFunction, 10); + } else { + setBoundsFunction(); + } + this.status = Status.SHOWING; if (this.view.webContents.isLoading()) { this.view.webContents.once('did-finish-load', () => { diff --git a/src/main/windows/windowManager.ts b/src/main/windows/windowManager.ts index dac7f8a8..66636ec9 100644 --- a/src/main/windows/windowManager.ts +++ b/src/main/windows/windowManager.ts @@ -19,6 +19,7 @@ import { APP_LOGGED_IN, GET_VIEW_NAME, GET_VIEW_WEBCONTENTS_ID, + RESIZE_MODAL, APP_LOGGED_OUT, } from 'common/communication'; import urlUtils from 'common/utils/url'; @@ -183,6 +184,7 @@ export class WindowManager { } this.viewManager.setLoadingScreenBounds(); this.teamDropdown?.updateWindowBounds(); + ipcMain.emit(RESIZE_MODAL, null, bounds); } sendToRenderer = (channel: string, ...args: any[]) => {