Skip to content

Commit

Permalink
fix show main window when maximized and minimized
Browse files Browse the repository at this point in the history
  • Loading branch information
timche committed Feb 12, 2022
1 parent 53c8dd4 commit dce6856
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 82 deletions.
4 changes: 2 additions & 2 deletions src/main/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from './account-views'
import { initOrUpdateAppMenu } from './menus/app'
import config, { ConfigKey } from './config'
import { sendToMainWindow, getMainWindow } from './main-window'
import { sendToMainWindow, showMainWindow } from './main-window'
import { initOrUpdateDockMenu } from './menus/dock'
import { initOrUpdateTrayMenu } from './menus/tray'
import { Account } from '../types'
Expand Down Expand Up @@ -96,7 +96,7 @@ export function getAccountsMenuItems(withAccelerator?: boolean) {
label,
click() {
selectAccount(id)
getMainWindow().show()
showMainWindow()
},
accelerator: withAccelerator ? `CommandOrControl+${index + 1}` : undefined
}))
Expand Down
36 changes: 9 additions & 27 deletions src/main/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { app } from 'electron'
import config, { ConfigKey } from './config'
import { getMainWindow } from './main-window'
import { getMainWindow, showMainWindow } from './main-window'
import { sendToSelectedAccountView } from './account-views'
import { appId } from '../constants'

Expand Down Expand Up @@ -36,47 +36,29 @@ export async function initApp() {
}

app.on('second-instance', () => {
const mainWindow = getMainWindow()

if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore()
}

mainWindow.show()
}
showMainWindow()
})

app.on('open-url', (_event, mailto) => {
sendToSelectedAccountView('gmail:compose-mail', mailto.split(':')[1])

const mainWindow = getMainWindow()

if (mainWindow) {
mainWindow.show()
}
showMainWindow()
})

app.on('activate', () => {
const mainWindow = getMainWindow()

if (mainWindow) {
mainWindow.show()
}
showMainWindow()
})

app.on('before-quit', () => {
setIsQuittingApp(true)

const mainWindow = getMainWindow()

if (mainWindow) {
config.set(ConfigKey.LastWindowState, {
bounds: mainWindow.getBounds(),
fullscreen: mainWindow.isFullScreen(),
maximized: mainWindow.isMaximized()
})
}
config.set(ConfigKey.LastWindowState, {
bounds: mainWindow.getBounds(),
fullscreen: mainWindow.isFullScreen(),
maximized: mainWindow.isMaximized()
})
})

await app.whenReady()
Expand Down
6 changes: 3 additions & 3 deletions src/main/gmail.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { app, ipcMain, Notification } from 'electron'
import { getAccountIdByViewId } from './account-views'
import { getAccount, selectAccount } from './accounts'
import { getMainWindow, sendToMainWindow } from './main-window'
import { sendToMainWindow, showMainWindow } from './main-window'
import config, { ConfigKey } from './config'
import { is } from 'electron-util'
import { updateTrayUnreadStatus } from './tray'
Expand Down Expand Up @@ -94,9 +94,9 @@ export function newMailNotification(
})

notification.on('click', () => {
accountViewWebContents.send('gmail:open-mail', messageId)
showMainWindow()
selectAccount(account.id)
getMainWindow().show()
accountViewWebContents.send('gmail:open-mail', messageId)
})

notification.show()
Expand Down
15 changes: 14 additions & 1 deletion src/main/main-window/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ export function getMainWindow() {
return mainWindow
}

export function showMainWindow() {
if (!mainWindow) {
throw new Error('Main window is uninitialized or has been destroyed')
}

if (mainWindow.isMinimized()) {
mainWindow.restore()
return
}

mainWindow.show()
}

export function sendToMainWindow(channel: string, ...args: any[]) {
if (mainWindow) {
mainWindow.webContents.send(channel, ...args)
Expand Down Expand Up @@ -154,7 +167,7 @@ export function createMainWindow(): void {

mainWindow.webContents.on('dom-ready', () => {
if (!shouldLaunchMinimized() && mainWindow) {
mainWindow.show()
showMainWindow()
}
})

Expand Down
41 changes: 14 additions & 27 deletions src/main/menus/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
isDefaultAccount,
removeAccount
} from '../accounts'
import { getMainWindow, sendToMainWindow } from '../main-window'
import { getMainWindow, sendToMainWindow, showMainWindow } from '../main-window'
import {
forEachAccountView,
getSelectedAccountView,
Expand Down Expand Up @@ -460,8 +460,7 @@ export function getAppMenu() {
accelerator: 'Command+,',
click() {
sendToSelectedAccountView('gmail:go-to', 'settings')
const mainWindow = getMainWindow()
mainWindow.show()
showMainWindow()
}
},
{
Expand Down Expand Up @@ -500,8 +499,7 @@ export function getAppMenu() {
label: 'Compose',
click() {
sendToSelectedAccountView('gmail:compose-mail')
const mainWindow = getMainWindow()
mainWindow.show()
showMainWindow()
}
},
{
Expand All @@ -524,8 +522,7 @@ export function getAppMenu() {
click() {
sendToMainWindow('add-account-request')
hideAccountViews()
const mainWindow = getMainWindow()
mainWindow.show()
showMainWindow()
}
},
{
Expand All @@ -535,8 +532,7 @@ export function getAppMenu() {
if (selectedAccount) {
sendToMainWindow('edit-account-request', selectedAccount)
hideAccountViews()
const mainWindow = getMainWindow()
mainWindow.show()
showMainWindow()
}
}
},
Expand All @@ -546,8 +542,7 @@ export function getAppMenu() {
const selectedAccount = getSelectedAccount()

if (selectedAccount) {
const mainWindow = getMainWindow()
mainWindow.show()
showMainWindow()

if (isDefaultAccount(selectedAccount.id)) {
dialog.showMessageBox({
Expand Down Expand Up @@ -753,32 +748,28 @@ export function getAppMenu() {
label: 'Inbox',
click() {
sendToSelectedAccountView('gmail:go-to', 'inbox')
const mainWindow = getMainWindow()
mainWindow.show()
showMainWindow()
}
},
{
label: 'Important',
click() {
sendToSelectedAccountView('gmail:go-to', 'imp')
const mainWindow = getMainWindow()
mainWindow.show()
showMainWindow()
}
},
{
label: 'Snoozed',
click() {
sendToSelectedAccountView('gmail:go-to', 'snoozed')
const mainWindow = getMainWindow()
mainWindow.show()
showMainWindow()
}
},
{
label: 'Starred',
click() {
sendToSelectedAccountView('gmail:go-to', 'starred')
const mainWindow = getMainWindow()
mainWindow.show()
showMainWindow()
}
},
{
Expand All @@ -788,24 +779,21 @@ export function getAppMenu() {
label: 'Drafts',
click() {
sendToSelectedAccountView('gmail:go-to', 'drafts')
const mainWindow = getMainWindow()
mainWindow.show()
showMainWindow()
}
},
{
label: 'Scheduled',
click() {
sendToSelectedAccountView('gmail:go-to', 'scheduled')
const mainWindow = getMainWindow()
mainWindow.show()
showMainWindow()
}
},
{
label: 'Sent',
click() {
sendToSelectedAccountView('gmail:go-to', 'sent')
const mainWindow = getMainWindow()
mainWindow.show()
showMainWindow()
}
},
{
Expand All @@ -815,8 +803,7 @@ export function getAppMenu() {
label: 'All Mail',
click() {
sendToSelectedAccountView('gmail:go-to', 'all')
const mainWindow = getMainWindow()
mainWindow.show()
showMainWindow()
}
}
]
Expand Down
24 changes: 11 additions & 13 deletions src/main/menus/dock.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { app, Menu } from 'electron'
import { is } from 'electron-util'
import config, { ConfigKey } from '../config'
import { getMainWindow } from '../main-window'
import { showMainWindow } from '../main-window'
import { sendToSelectedAccountView } from '../account-views'
import { getAccountsMenuItems } from '../accounts'

export function initOrUpdateDockMenu() {
const mainWindow = getMainWindow()

if (is.macos && mainWindow) {
if (is.macos) {
if (!config.get(ConfigKey.ShowDockIcon)) {
app.dock.hide()
}
Expand All @@ -22,7 +20,7 @@ export function initOrUpdateDockMenu() {
label: 'Compose',
click() {
sendToSelectedAccountView('gmail:compose-mail')
mainWindow.show()
showMainWindow()
}
},
{
Expand All @@ -32,28 +30,28 @@ export function initOrUpdateDockMenu() {
label: 'Inbox',
click() {
sendToSelectedAccountView('gmail:go-to', 'inbox')
mainWindow.show()
showMainWindow()
}
},
{
label: 'Important',
click() {
sendToSelectedAccountView('gmail:go-to', 'imp')
mainWindow.show()
showMainWindow()
}
},
{
label: 'Snoozed',
click() {
sendToSelectedAccountView('gmail:go-to', 'snoozed')
mainWindow.show()
showMainWindow()
}
},
{
label: 'Starred',
click() {
sendToSelectedAccountView('gmail:go-to', 'starred')
mainWindow.show()
showMainWindow()
}
},
{
Expand All @@ -63,21 +61,21 @@ export function initOrUpdateDockMenu() {
label: 'Drafts',
click() {
sendToSelectedAccountView('gmail:go-to', 'drafts')
mainWindow.show()
showMainWindow()
}
},
{
label: 'Scheduled',
click() {
sendToSelectedAccountView('gmail:go-to', 'scheduled')
mainWindow.show()
showMainWindow()
}
},
{
label: 'Sent',
click() {
sendToSelectedAccountView('gmail:go-to', 'sent')
mainWindow.show()
showMainWindow()
}
},
{
Expand All @@ -87,7 +85,7 @@ export function initOrUpdateDockMenu() {
label: 'All Mail',
click() {
sendToSelectedAccountView('gmail:go-to', 'all')
mainWindow.show()
showMainWindow()
}
}
])
Expand Down
4 changes: 2 additions & 2 deletions src/main/menus/tray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { shouldLaunchMinimized } from '../app'
import { getAccountsMenuItems } from '../accounts'
import { app, Menu, MenuItemConstructorOptions } from 'electron'
import { is } from 'electron-util'
import { getMainWindow } from '../main-window'
import { getMainWindow, showMainWindow } from '../main-window'
import { getTray } from '../tray'

let trayMenu: Menu | undefined
Expand Down Expand Up @@ -47,7 +47,7 @@ export function getTrayMenuTemplate() {
},
{
click: () => {
getMainWindow().show()
showMainWindow()
},
label: 'Show',
visible: shouldLaunchMinimized(),
Expand Down
7 changes: 2 additions & 5 deletions src/main/tray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as path from 'path'
import { app, nativeImage, NativeImage, Tray } from 'electron'
import { is } from 'electron-util'
import config, { ConfigKey } from './config'
import { getMainWindow } from './main-window'
import { showMainWindow } from './main-window'
import { getTrayMenu } from './menus/tray'

let tray: Tray | undefined
Expand Down Expand Up @@ -72,9 +72,6 @@ export function initTray() {
tray.setToolTip(app.name)

tray.on('click', () => {
const mainWindow = getMainWindow()
if (mainWindow) {
mainWindow.show()
}
showMainWindow()
})
}
Loading

0 comments on commit dce6856

Please sign in to comment.