-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract tray management into a new class
- Loading branch information
Showing
2 changed files
with
128 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { app, BrowserWindow, Menu, nativeImage, nativeTheme, Tray } from 'electron'; | ||
import { join } from 'path'; | ||
import { APP_ROOT } from './constants'; | ||
|
||
const TRAY_ICONS_ROOT = join(APP_ROOT, 'assets', 'icons', 'tray'); | ||
|
||
export default class TrayManager { | ||
mainWindow: BrowserWindow; | ||
tray: Tray; | ||
|
||
/** The current system theme */ | ||
get theme(): 'light' | 'dark' { | ||
return nativeTheme.shouldUseDarkColors ? 'dark' : 'light'; | ||
} | ||
|
||
/** The name of the menu icon file based on the platform and theme */ | ||
get menuIconName(): string { | ||
switch (process.platform) { | ||
case 'darwin': | ||
return '16x16Template.png'; | ||
case 'win32': | ||
return `16x16icon-${this.theme}.png`; | ||
case 'linux': | ||
return `96x96icon-${this.theme}.png`; | ||
default: | ||
return `96x96icon-${this.theme}.png`; | ||
} | ||
} | ||
|
||
constructor(mainWindow: BrowserWindow) { | ||
this.mainWindow = mainWindow; | ||
const theme = nativeTheme.shouldUseDarkColors ? 'dark' : 'light'; | ||
const trayIcon = | ||
process.platform === 'darwin' | ||
? join(TRAY_ICONS_ROOT, '16x16Template.png') | ||
: join(TRAY_ICONS_ROOT, `1024x1024-${theme}.png`); | ||
const nativeImageFromPath = nativeImage.createFromPath(trayIcon); | ||
nativeImageFromPath.setTemplateImage(true); | ||
this.tray = new Tray(nativeImageFromPath); | ||
|
||
// initial creation of tray menu | ||
this.updateTrayIcons(); | ||
|
||
this.tray.on('click', () => { | ||
// show the window when the user clicks on the tray icon | ||
this.handleOnShowClick(); | ||
}); | ||
|
||
nativeTheme.on('updated', () => { | ||
// re-create tray context menu when system theme changes | ||
this.updateTrayIcons(); | ||
}); | ||
} | ||
|
||
/** | ||
* Gets the full path to the menu icon icon based on platform and theme | ||
*/ | ||
getIconPath(iconDir: 'quit' | 'minimize' | 'show') { | ||
const imagePath = join(TRAY_ICONS_ROOT, iconDir, this.menuIconName); | ||
const image = nativeImage.createFromPath(imagePath); | ||
image.setTemplateImage(true); | ||
return image; | ||
} | ||
|
||
/** | ||
* Updates the tray context menu icons based on the current theme | ||
*/ | ||
updateTrayIcons() { | ||
const contextMenu = Menu.buildFromTemplate([ | ||
{ | ||
label: 'Minimize Window', | ||
click: this.handleOnHideClick, | ||
icon: this.getIconPath('minimize'), | ||
}, | ||
{ | ||
label: 'Show Window', | ||
click: this.handleOnShowClick, | ||
icon: this.getIconPath('show'), | ||
}, | ||
{ | ||
type: 'separator', | ||
}, | ||
{ | ||
label: 'Quit Polar', | ||
click: this.handleQuitClick, | ||
icon: this.getIconPath('quit'), | ||
}, | ||
]); | ||
|
||
this.tray.setContextMenu(contextMenu); | ||
} | ||
|
||
/** | ||
* Hides the main window | ||
*/ | ||
handleOnHideClick = () => { | ||
app.dock?.hide(); | ||
this.mainWindow.setSkipTaskbar(true); | ||
this.mainWindow.hide(); | ||
}; | ||
|
||
/** | ||
* Shows the main window | ||
*/ | ||
handleOnShowClick = () => { | ||
app.dock?.show(); | ||
this.mainWindow.setSkipTaskbar(false); | ||
this.mainWindow.show(); | ||
}; | ||
|
||
/** | ||
* Closes all windows and quits the app | ||
*/ | ||
handleQuitClick = () => { | ||
app.quit(); | ||
}; | ||
|
||
destroy() { | ||
this.tray.destroy(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters