Skip to content

Commit

Permalink
fix(tray): use tray only in talk window and re-setup when window changes
Browse files Browse the repository at this point in the history
Signed-off-by: Grigorii K. Shartsev <[email protected]>
  • Loading branch information
ShGKme committed Jul 25, 2023
1 parent 7c5e590 commit 7c2d094
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 35 deletions.
40 changes: 22 additions & 18 deletions src/app/app.tray.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,20 @@ const {
const path = require('path')
const { isMac, isWindows } = require('../shared/os.utils.js')

let isAppQuitting = false

/**
* Allow quitting the app if requested. It minimizes to a tray otherwise.
*/
app.on('before-quit', () => { isAppQuitting = true })

/**
* Setup tray with an icon that provides a context menu.
*
* @param {object} options Object with one property 'click' which is a function that will be called when a user clicks the tray icon or the 'Open' tray menu item.
* @param {import('electron').BrowserWindow} browserWindow Browser window, associated with the tray
* @return {import('electron').Tray} Tray instance
*/
function setupTray(options) {
function setupTray(browserWindow) {
const tray = new Tray(path.join(
__dirname, '..', '..', 'img', 'icons',
isMac() ? 'TrayIconTemplate.png' : isWindows() ? 'icon.ico' : 'icon.png',
Expand All @@ -41,32 +49,28 @@ function setupTray(options) {
tray.setContextMenu(Menu.buildFromTemplate([
{
label: 'Open',
click: () => options.click(),
click: () => browserWindow.show(),
},
{
role: 'quit',
},
]))
tray.on('click', () => {
options.click()
})
}
/**
* Sets up a window to minimize to tray.
*
* @param {import('electron').BrowserWindow} window Reference to the window.
* @param {Function} isAppQuitting Function that returns a bool value whether the app is in the process of quitting. In this case, minimizing to tray will be skipped.
*/
function setupMinimizeToTray(window, isAppQuitting) {
window.on('close', event => {
if (!isAppQuitting()) {
tray.on('click', () => browserWindow.show())

browserWindow.on('close', event => {
if (!isAppQuitting) {
event.preventDefault()
window.hide()
browserWindow.hide()
}
})

browserWindow.on('closed', event => {
tray.destroy()
})

return tray
}

module.exports = {
setupTray,
setupMinimizeToTray,
}
23 changes: 6 additions & 17 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const { createHelpWindow } = require('./help/help.window.js')
const { getOs, isLinux } = require('./shared/os.utils.js')
const { createTalkWindow } = require('./talk/talk.window.js')
const { createWelcomeWindow } = require('./welcome/welcome.window.js')
const { setupTray, setupMinimizeToTray } = require('./app/app.tray.js')

/**
* Separate production and development instances, including application and user data
Expand Down Expand Up @@ -82,7 +81,6 @@ app.whenReady().then(async () => {
*/
let mainWindow
let createMainWindow
let isAppQuitting = false

setupMenu()

Expand All @@ -98,11 +96,6 @@ app.whenReady().then(async () => {
*/
app.on('second-instance', () => focusMainWindow())

/**
* Allow to quit the app if requested. It minimizes to tray otherwise.
*/
app.on('before-quit', function() { isAppQuitting = true })

app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
event.preventDefault()

Expand Down Expand Up @@ -163,7 +156,6 @@ app.whenReady().then(async () => {
})
mainWindow = createTalkWindow()
createMainWindow = createTalkWindow
setupMinimizeToTray(mainWindow, () => isAppQuitting)
} else {
// User is unauthenticated - start login window
await welcomeWindow.webContents.session.clearStorageData()
Expand All @@ -185,29 +177,26 @@ app.whenReady().then(async () => {
mainWindow.close()
mainWindow = createTalkWindow()
createMainWindow = createTalkWindow
setupMinimizeToTray(mainWindow, () => isAppQuitting)
})

ipcMain.handle('authentication:logout', async (event) => {
if (createMainWindow === createTalkWindow) {
await mainWindow.webContents.session.clearStorageData()
mainWindow.close()
mainWindow = createAuthenticationWindow()
const authenticationWindow = createAuthenticationWindow()
createMainWindow = createAuthenticationWindow
mainWindow.once('ready-to-show', () => {
mainWindow.show()
authenticationWindow.once('ready-to-show', () => {
authenticationWindow.show()
})

mainWindow.destroy()
mainWindow = authenticationWindow
}
})

ipcMain.handle('help:show', () => {
createHelpWindow(mainWindow)
})

setupTray({
click: () => mainWindow.show(),
})

// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
app.on('activate', function() {
Expand Down
2 changes: 2 additions & 0 deletions src/talk/talk.window.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const {
const { applyContextMenu } = require('../app/applyContextMenu.js')
const { applyDownloadNotification } = require('../app/applyDownloadNotification.js')
const { applyWheelZoom } = require('../app/applyWheelZoom.js')
const { setupTray } = require('../app/app.tray.js')

/**
* @return {import('electron').BrowserWindow}
Expand Down Expand Up @@ -70,6 +71,7 @@ function createTalkWindow() {
applyContextMenu(window)
applyDownloadNotification(window)
applyWheelZoom(window)
setupTray(window)

window.loadURL(TALK_WINDOW_WEBPACK_ENTRY)

Expand Down

0 comments on commit 7c2d094

Please sign in to comment.