Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(windowManager): added app tray, users can minimize, maximize and quit polar from system tray #842

Merged
merged 3 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/icons/tray/1024x1024-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/tray/1024x1024-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/tray/16x16Template.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/tray/minimize/16x16Template.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/tray/minimize/16x16icon-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/tray/minimize/16x16icon-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/tray/minimize/96x96icon-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/tray/minimize/96x96icon-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/tray/quit/16x16Template.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/tray/quit/16x16icon-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/tray/quit/16x16icon-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/tray/quit/96x96icon-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/tray/quit/96x96icon-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/tray/show/16x16Template.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/tray/show/16x16icon-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/tray/show/16x16icon-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/tray/show/96x96icon-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/tray/show/96x96icon-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
137 changes: 137 additions & 0 deletions electron/trayManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
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 tray icon file based on the platform and theme */
get trayIconName(): string {
switch (process.platform) {
case 'darwin':
return '16x16Template.png';
case 'linux':
// The linux tray background doesn't change with the theme. It's always dark
// on Ubuntu and always light on Mint. We use the dark icon for both. This may
// be fixable in the future, but there's no good solution right now.
return `1024x1024-dark.png`;
default:
return `1024x1024-${this.theme}.png`;
}
}

/** 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;

this.tray = new Tray(this.getIconPath('tray'));

// initial creation of tray menu
this.updateTrayIcons();

if (process.platform !== 'darwin') {
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 based on platform and theme
*/
getIconPath(name: 'tray' | 'quit' | 'minimize' | 'show') {
const imagePath =
name === 'tray'
? join(TRAY_ICONS_ROOT, this.trayIconName)
: join(TRAY_ICONS_ROOT, name, 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);
this.tray.setImage(this.getIconPath('tray'));
}

/**
* 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();
}
}
15 changes: 12 additions & 3 deletions electron/windowManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import {
initLndSubscriptions,
} from './lnd/lndProxyServer';
import { initTapdProxy } from './tapd/tapdProxyServer';
import TrayManager from './trayManager';

class WindowManager {
mainWindow: BrowserWindow | null = null;
trayManager: TrayManager | null = null;

start() {
app.on('ready', async () => {
Expand Down Expand Up @@ -50,6 +52,12 @@ class WindowManager {
enableRemoteModule: true,
},
});

// create App system tray icon with context menus
if (!this.trayManager) {
this.trayManager = new TrayManager(this.mainWindow);
}

this.mainWindow.setMenuBarVisibility(false);

if (IS_DEV) {
Expand Down Expand Up @@ -89,12 +97,13 @@ class WindowManager {

onMainClosed() {
this.mainWindow = null;
this.trayManager?.destroy();
app.quit();
}

onAllClosed() {
if (process.platform !== 'darwin') {
app.quit();
}
this.trayManager?.destroy();
app.quit();
}

onMainWindowClose() {
Expand Down