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(tray): add support for tray icon themes #2809

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
8392b07
refactor(assets): update tray icons
yilmazcabuk Dec 30, 2024
10743b3
refactor: update tray icon
yilmazcabuk Dec 30, 2024
0111eaf
refactor(tray): improve tray setup and icon handling
yilmazcabuk Dec 30, 2024
86fc1a9
Merge branch 'th-ch:master' into master
yilmazcabuk Dec 31, 2024
082ebcc
feat(tray): add support for tray icon themes
yilmazcabuk Dec 31, 2024
1a9372f
Merge remote-tracking branch 'origin/master'
yilmazcabuk Dec 31, 2024
da71433
refactor(tray): update tray tooltip to use SongInfo type
yilmazcabuk Dec 31, 2024
4f7ffe9
Merge branch 'th-ch:master' into master
yilmazcabuk Dec 31, 2024
b0af7fc
refactor(tray): streamline tray icon management
yilmazcabuk Jan 1, 2025
25a2787
refactor(tray): improve handling and encapsulate logic
yilmazcabuk Jan 1, 2025
594abd1
Merge branch 'th-ch:master' into master
yilmazcabuk Jan 1, 2025
7a1260e
Merge branch 'th-ch:master' into master
yilmazcabuk Jan 2, 2025
781a49a
refactor(tray): simplify tray icon and menu setup
yilmazcabuk Jan 2, 2025
bfb9bfd
Merge branch 'th-ch:master' into master
yilmazcabuk Jan 3, 2025
437d805
fix(tray): standardize setUpTray argument structure
yilmazcabuk Jan 3, 2025
d6ba730
Merge remote-tracking branch 'origin/master'
yilmazcabuk Jan 3, 2025
3f6efc5
fix(tray): correct tooltip update logic
yilmazcabuk Jan 3, 2025
ab0440f
Merge branch 'th-ch:master' into master
yilmazcabuk Jan 4, 2025
c9f1d1c
Merge branch 'th-ch:master' into master
yilmazcabuk Jan 5, 2025
fcdca3a
Merge branch 'th-ch:master' into master
yilmazcabuk Jan 8, 2025
f0c579c
Merge branch 'th-ch:master' into master
yilmazcabuk Jan 9, 2025
8c0db3f
Merge branch 'th-ch:master' into master
yilmazcabuk Jan 10, 2025
0c8c732
Merge branch 'th-ch:master' into master
yilmazcabuk Jan 12, 2025
581e01c
Merge branch 'th-ch:master' into master
yilmazcabuk Jan 17, 2025
e1a864d
Merge branch 'th-ch:master' into master
yilmazcabuk Jan 18, 2025
585f074
Merge branch 'th-ch:master' into master
yilmazcabuk Jan 19, 2025
f645577
Merge branch 'th-ch:master' into master
yilmazcabuk Jan 19, 2025
f286024
Merge branch 'th-ch:master' into master
yilmazcabuk Jan 23, 2025
8ecc5be
Merge branch 'th-ch:master' into master
yilmazcabuk Jan 24, 2025
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/tray-icons/pause.png
yilmazcabuk marked this conversation as resolved.
Show resolved Hide resolved
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/tray-icons/play.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 removed assets/youtube-music-tray-paused.png
Binary file not shown.
Binary file removed assets/youtube-music-tray.png
Binary file not shown.
2 changes: 1 addition & 1 deletion src/providers/prompt-options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import youtubeMusicTrayIcon from '@assets/youtube-music-tray.png?asset&asarUnpack';
import youtubeMusicTrayIcon from '@assets/tray-icons/play.png?asset&asarUnpack';

const promptOptions = {
customStylesheet: 'dark',
Expand Down
182 changes: 82 additions & 100 deletions src/tray.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Menu, nativeImage, screen, Tray } from 'electron';
import is from 'electron-is';

import defaultTrayIconAsset from '@assets/youtube-music-tray.png?asset&asarUnpack';
import pausedTrayIconAsset from '@assets/youtube-music-tray-paused.png?asset&asarUnpack';
import playTrayIconAsset from '@assets/tray-icons/play.png?asset&asarUnpack';
import pauseTrayIconAsset from '@assets/tray-icons/pause.png?asset&asarUnpack';

import config from './config';

Expand All @@ -14,134 +14,116 @@ import { t } from '@/i18n';

import type { MenuTemplate } from './menu';

// Prevent tray being garbage collected
yilmazcabuk marked this conversation as resolved.
Show resolved Hide resolved
let tray: Electron.Tray | undefined;

type TrayEvent = (
event: Electron.KeyboardEvent,
bounds: Electron.Rectangle,
) => void;

export const setTrayOnClick = (fn: TrayEvent) => {
if (!tray) {
return;
interface AppWindowControls {
yilmazcabuk marked this conversation as resolved.
Show resolved Hide resolved
playPause: () => void;
next: () => void;
previous: () => void;
}

const getTrayIcon = (
iconPath: string,
pixelRatio: number,
): Electron.NativeImage =>
nativeImage.createFromPath(iconPath).resize({
width: 16 * pixelRatio,
height: 16 * pixelRatio,
});

const handleTrayClick = (
app: Electron.App,
win: Electron.BrowserWindow,
playPause: () => void,
): void => {
if (config.get('options.trayClickPlayPause')) {
playPause();
} else if (win.isVisible()) {
win.hide();
app.dock?.hide();
} else {
win.show();
app.dock?.show();
}
};

const setMacSpecificTraySettings = (trayInstance: Electron.Tray): void => {
trayInstance.setIgnoreDoubleClickEvents(true);
};

const getPixelRatio = (): number => {
return is.windows() ? screen.getPrimaryDisplay().scaleFactor || 1 : 1;
};

export const setTrayOnClick = (fn: TrayEvent): void => {
if (!tray) return;
tray.removeAllListeners('click');
tray.on('click', fn);
};

// Won't do anything on macOS since its disabled
yilmazcabuk marked this conversation as resolved.
Show resolved Hide resolved
export const setTrayOnDoubleClick = (fn: TrayEvent) => {
if (!tray) {
return;
}

export const setTrayOnDoubleClick = (fn: TrayEvent): void => {
if (!tray) return;
tray.removeAllListeners('double-click');
tray.on('double-click', fn);
};

export const setUpTray = (app: Electron.App, win: Electron.BrowserWindow) => {
export const setUpTray = (
app: Electron.App,
win: Electron.BrowserWindow,
): void => {
if (!config.get('options.tray')) {
tray = undefined;
return;
}

const { playPause, next, previous } = getSongControls(win);

const pixelRatio = is.windows()
? screen.getPrimaryDisplay().scaleFactor || 1
: 1;
const defaultTrayIcon = nativeImage
.createFromPath(defaultTrayIconAsset)
.resize({
width: 16 * pixelRatio,
height: 16 * pixelRatio,
});
const pausedTrayIcon = nativeImage
.createFromPath(pausedTrayIconAsset)
.resize({
width: 16 * pixelRatio,
height: 16 * pixelRatio,
});

tray = new Tray(defaultTrayIcon);
const { playPause, next, previous }: AppWindowControls = getSongControls(win);
const pixelRatio = getPixelRatio();
const playTrayIcon = getTrayIcon(playTrayIconAsset, pixelRatio);
const pauseTrayIcon = getTrayIcon(pauseTrayIconAsset, pixelRatio);

tray.setToolTip(t('main.tray.tooltip.default'));
tray = new Tray(playTrayIcon);
setMacSpecificTraySettings(tray);

// MacOS only
tray.setIgnoreDoubleClickEvents(true);

tray.on('click', () => {
if (config.get('options.trayClickPlayPause')) {
playPause();
} else if (win.isVisible()) {
win.hide();
app.dock?.hide();
} else {
win.show();
app.dock?.show();
}
});

const template: MenuTemplate = [
{
label: t('main.tray.play-pause'),
click() {
playPause();
},
},
{
label: t('main.tray.next'),
click() {
next();
},
},
{
label: t('main.tray.previous'),
click() {
previous();
},
},
{
label: t('main.tray.show'),
click() {
win.show();
app.dock?.show();
},
},
tray.setToolTip(t('main.tray.tooltip.default'));
tray.on('click', () => handleTrayClick(app, win, playPause));

const showWindow = (): void => {
win.show();
app.dock?.show();
};

const trayMenuTemplate: MenuTemplate = [
{ label: t('main.tray.play-pause'), click: playPause },
yilmazcabuk marked this conversation as resolved.
Show resolved Hide resolved
{ label: t('main.tray.next'), click: next },
{ label: t('main.tray.previous'), click: previous },
{ label: t('main.tray.show'), click: showWindow },
{ type: 'separator' },
{
label: t('main.tray.restart'),
click: restart,
},
{ label: t('main.tray.restart'), click: restart },
{ type: 'separator' },
{
label: t('main.tray.quit'),
role: 'quit',
},
{ label: t('main.tray.quit'), role: 'quit' },
];

const trayMenu = Menu.buildFromTemplate(template);
tray.setContextMenu(trayMenu);
tray.setContextMenu(Menu.buildFromTemplate(trayMenuTemplate));

registerCallback((songInfo, event) => {
if (event === SongInfoEvent.TimeChanged) return;

if (tray) {
if (typeof songInfo.isPaused === 'undefined') {
tray.setImage(defaultTrayIcon);
return;
}

tray.setToolTip(
t('main.tray.tooltip.with-song-info', {
artist: songInfo.artist,
title: songInfo.title,
}),
);

tray.setImage(songInfo.isPaused ? pausedTrayIcon : defaultTrayIcon);
if (event === SongInfoEvent.TimeChanged || !tray) return;

if (typeof songInfo.isPaused === 'undefined') {
tray.setImage(playTrayIcon);
return;
}

tray.setToolTip(
t('main.tray.tooltip.with-song-info', {
artist: songInfo.artist,
title: songInfo.title,
}),
);
tray.setImage(songInfo.isPaused ? playTrayIcon : pauseTrayIcon);
});
};
Loading