Skip to content
Open
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
69 changes: 42 additions & 27 deletions src/electron/tray.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,50 @@ import path from 'path';
import { app, nativeImage, Tray, Menu, nativeTheme } from 'electron';
import { isLinux } from '@/utils/platform';

function createMenuTemplate(win) {
function getTrayIconTheme(store) {
const trayIconTheme = store.get('settings.trayIconTheme');
if (trayIconTheme === 'light' || trayIconTheme === 'dark') {
return trayIconTheme;
}

return nativeTheme.shouldUseDarkColors ? 'light' : 'dark';
}

function createMenuIcon(filename, iconTheme) {
const image = nativeImage.createFromPath(
path.join(__static, `img/icons/${filename}.png`)
);

if (iconTheme === 'dark') return image;

const bitmap = image.toBitmap();
for (let offset = 0; offset < bitmap.length; offset += 4) {
// NativeImage bitmaps use premultiplied alpha, so white RGB values
// must match the pixel's alpha value to preserve antialiased edges.
const alpha = bitmap[offset + 3];
bitmap[offset] = alpha;
bitmap[offset + 1] = alpha;
bitmap[offset + 2] = alpha;
}

return nativeImage.createFromBitmap(bitmap, image.getSize());
}

function createMenuTemplate(win, store) {
const iconTheme = getTrayIconTheme(store);

return [
{
label: '播放',
icon: nativeImage.createFromPath(
path.join(__static, 'img/icons/play.png')
),
icon: createMenuIcon('play', iconTheme),
click: () => {
win.webContents.send('play');
},
id: 'play',
},
{
label: '暂停',
icon: nativeImage.createFromPath(
path.join(__static, 'img/icons/pause.png')
),
icon: createMenuIcon('pause', iconTheme),
click: () => {
win.webContents.send('play');
},
Expand All @@ -28,39 +55,31 @@ function createMenuTemplate(win) {
},
{
label: '上一首',
icon: nativeImage.createFromPath(
path.join(__static, 'img/icons/left.png')
),
icon: createMenuIcon('left', iconTheme),
accelerator: 'CmdOrCtrl+Left',
click: () => {
win.webContents.send('previous');
},
},
{
label: '下一首',
icon: nativeImage.createFromPath(
path.join(__static, 'img/icons/right.png')
),
icon: createMenuIcon('right', iconTheme),
accelerator: 'CmdOrCtrl+Right',
click: () => {
win.webContents.send('next');
},
},
{
label: '循环播放',
icon: nativeImage.createFromPath(
path.join(__static, 'img/icons/repeat.png')
),
icon: createMenuIcon('repeat', iconTheme),
accelerator: 'Alt+R',
click: () => {
win.webContents.send('repeat');
},
},
{
label: '加入喜欢',
icon: nativeImage.createFromPath(
path.join(__static, 'img/icons/like.png')
),
icon: createMenuIcon('like', iconTheme),
accelerator: 'CmdOrCtrl+L',
click: () => {
win.webContents.send('like');
Expand All @@ -69,9 +88,7 @@ function createMenuTemplate(win) {
},
{
label: '取消喜欢',
icon: nativeImage.createFromPath(
path.join(__static, 'img/icons/unlike.png')
),
icon: createMenuIcon('unlike', iconTheme),
accelerator: 'CmdOrCtrl+L',
click: () => {
win.webContents.send('like');
Expand All @@ -81,9 +98,7 @@ function createMenuTemplate(win) {
},
{
label: '退出',
icon: nativeImage.createFromPath(
path.join(__static, 'img/icons/exit.png')
),
icon: createMenuIcon('exit', iconTheme),
accelerator: 'CmdOrCtrl+W',
click: () => {
app.exit();
Expand Down Expand Up @@ -128,7 +143,7 @@ class YPMTrayLinuxImpl {
{
type: 'separator',
},
].concat(createMenuTemplate(this.win));
].concat(createMenuTemplate(this.win, this.store));
}

handleEvents() {
Expand Down Expand Up @@ -178,7 +193,7 @@ class YPMTrayWindowsImpl {
this.win = win;
this.emitter = emitter;
this.store = store;
this.template = createMenuTemplate(win);
this.template = createMenuTemplate(win, store);
this.contextMenu = Menu.buildFromTemplate(this.template);

this.isPlaying = false;
Expand Down