Skip to content

Commit c47d009

Browse files
authored
feat: add tray icon color customization (#2414)
- Implemented functionality to change the tray icon color based on user settings. - Added new IPC event to update the tray icon dynamically. - Updated the settings view to allow users to select between auto, light, and dark themes for the tray icon. - Enhanced tray implementation to utilize the store for icon theme settings.
1 parent 210df76 commit c47d009

File tree

9 files changed

+118
-8
lines changed

9 files changed

+118
-8
lines changed

src/background.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,11 @@ class Background {
388388
// create tray
389389
if (isCreateTray) {
390390
this.trayEventEmitter = new EventEmitter();
391-
this.ypmTrayImpl = createTray(this.window, this.trayEventEmitter);
391+
this.ypmTrayImpl = createTray(
392+
this.window,
393+
this.trayEventEmitter,
394+
this.store
395+
);
392396
}
393397

394398
// init ipcMain

src/electron/ipcMain.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,5 +319,8 @@ export function initIpcMain(win, store, trayEventEmitter) {
319319
ipcMain.on('updateTrayLikeState', (_, isLiked) => {
320320
trayEventEmitter.emit('updateLikeState', isLiked);
321321
});
322+
ipcMain.on('updateTrayIcon', () => {
323+
trayEventEmitter.emit('updateIcon');
324+
});
322325
}
323326
}

src/electron/tray.js

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,11 @@ function createMenuTemplate(win) {
102102
// 添加左键支持
103103
// 2022.05.17
104104
class YPMTrayLinuxImpl {
105-
constructor(tray, win, emitter) {
105+
constructor(tray, win, emitter, store) {
106106
this.tray = tray;
107107
this.win = win;
108108
this.emitter = emitter;
109+
this.store = store;
109110
this.template = undefined;
110111
this.initTemplate();
111112
this.contextMenu = Menu.buildFromTemplate(this.template);
@@ -146,14 +147,37 @@ class YPMTrayLinuxImpl {
146147
this.contextMenu.getMenuItemById('unlike').visible = isLiked;
147148
this.tray.setContextMenu(this.contextMenu);
148149
});
150+
this.emitter.on('updateIcon', () => {
151+
this.updateIcon();
152+
});
153+
}
154+
155+
updateIcon() {
156+
let trayIconSetting = this.store.get('settings.trayIconTheme') || 'auto';
157+
let iconTheme;
158+
if (trayIconSetting === 'auto') {
159+
iconTheme = nativeTheme.shouldUseDarkColors ? 'light' : 'dark';
160+
} else {
161+
iconTheme = trayIconSetting;
162+
}
163+
164+
let icon = nativeImage
165+
.createFromPath(path.join(__static, `img/icons/menu-${iconTheme}@88.png`))
166+
.resize({
167+
height: 20,
168+
width: 20,
169+
});
170+
171+
this.tray.setImage(icon);
149172
}
150173
}
151174

152175
class YPMTrayWindowsImpl {
153-
constructor(tray, win, emitter) {
176+
constructor(tray, win, emitter, store) {
154177
this.tray = tray;
155178
this.win = win;
156179
this.emitter = emitter;
180+
this.store = store;
157181
this.template = createMenuTemplate(win);
158182
this.contextMenu = Menu.buildFromTemplate(this.template);
159183

@@ -193,12 +217,39 @@ class YPMTrayWindowsImpl {
193217
isPlaying => (this.isPlaying = isPlaying)
194218
);
195219
this.emitter.on('updateLikeState', isLiked => (this.isLiked = isLiked));
220+
this.emitter.on('updateIcon', () => {
221+
this.updateIcon();
222+
});
223+
}
224+
225+
updateIcon() {
226+
let trayIconSetting = this.store.get('settings.trayIconTheme') || 'auto';
227+
let iconTheme;
228+
if (trayIconSetting === 'auto') {
229+
iconTheme = nativeTheme.shouldUseDarkColors ? 'light' : 'dark';
230+
} else {
231+
iconTheme = trayIconSetting;
232+
}
233+
234+
let icon = nativeImage
235+
.createFromPath(path.join(__static, `img/icons/menu-${iconTheme}@88.png`))
236+
.resize({
237+
height: 20,
238+
width: 20,
239+
});
240+
241+
this.tray.setImage(icon);
196242
}
197243
}
198244

199-
export function createTray(win, eventEmitter) {
200-
// 感觉图标颜色应该不属于界面主题范畴,只需要跟随系统主题
201-
let iconTheme = nativeTheme.shouldUseDarkColors ? 'light' : 'dark';
245+
export function createTray(win, eventEmitter, store) {
246+
let trayIconSetting = store.get('settings.trayIconTheme') || 'auto';
247+
let iconTheme;
248+
if (trayIconSetting === 'auto') {
249+
iconTheme = nativeTheme.shouldUseDarkColors ? 'light' : 'dark';
250+
} else {
251+
iconTheme = trayIconSetting;
252+
}
202253

203254
let icon = nativeImage
204255
.createFromPath(path.join(__static, `img/icons/menu-${iconTheme}@88.png`))
@@ -211,6 +262,6 @@ export function createTray(win, eventEmitter) {
211262
tray.setToolTip('YesPlayMusic');
212263

213264
return isLinux
214-
? new YPMTrayLinuxImpl(tray, win, eventEmitter)
215-
: new YPMTrayWindowsImpl(tray, win, eventEmitter);
265+
? new YPMTrayLinuxImpl(tray, win, eventEmitter, store)
266+
: new YPMTrayWindowsImpl(tray, win, eventEmitter, store);
216267
}

src/locale/lang/en.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ export default {
169169
light: 'Light',
170170
dark: 'Dark',
171171
},
172+
trayIcon: {
173+
text: 'Tray Icon Color',
174+
auto: 'Auto',
175+
light: 'Light',
176+
dark: 'Dark',
177+
},
172178
automaticallyCacheSongs: 'Automatically cache songs',
173179
clearSongsCache: 'Clear Songs Cache',
174180
cacheCount: 'Cached {song} songs ({size})',

src/locale/lang/tr.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ export default {
164164
light: 'Aydınlık',
165165
dark: 'Karanlık',
166166
},
167+
trayIcon: {
168+
text: 'Tepsi Simgesi Rengi',
169+
auto: 'Otomatik',
170+
light: 'Aydınlık',
171+
dark: 'Karanlık',
172+
},
167173
automaticallyCacheSongs: 'Müzikleri otomatik çerezle',
168174
clearSongsCache: 'Müzik çerezlerini temizle',
169175
cacheCount: 'Çerezlenen {song} Müzikler ({size})',

src/locale/lang/zh-CN.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ export default {
170170
light: '浅色',
171171
dark: '深色',
172172
},
173+
trayIcon: {
174+
text: '托盘图标颜色',
175+
auto: '自动',
176+
light: '浅色',
177+
dark: '深色',
178+
},
173179
automaticallyCacheSongs: '自动缓存歌曲',
174180
clearSongsCache: '清除歌曲缓存',
175181
cacheCount: '已缓存 {song} 首 ({size})',

src/locale/lang/zh-TW.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ export default {
166166
light: '淺色',
167167
dark: '深色',
168168
},
169+
trayIcon: {
170+
text: '工作列圖示顏色',
171+
auto: '自動',
172+
light: '淺色',
173+
dark: '深色',
174+
},
169175
automaticallyCacheSongs: '自動快取歌曲',
170176
clearSongsCache: '清除歌曲快取',
171177
cacheCount: '已快取 {song} 首 ({size})',

src/store/initLocalStorage.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ let localStorage = {
3030
showLibraryDefault: false,
3131
subTitleDefault: false,
3232
linuxEnableCustomTitlebar: false,
33+
trayIconTheme: 'auto',
3334
enabledPlaylistCategories,
3435
proxyConfig: {
3536
protocol: 'noProxy',

src/views/settings.vue

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@
5656
</select>
5757
</div>
5858
</div>
59+
<div v-if="isElectron" class="item">
60+
<div class="left">
61+
<div class="title"> {{ $t('settings.trayIcon.text') }} </div>
62+
</div>
63+
<div class="right">
64+
<select v-model="trayIconTheme">
65+
<option value="auto">{{ $t('settings.trayIcon.auto') }}</option>
66+
<option value="light">{{ $t('settings.trayIcon.light') }}</option>
67+
<option value="dark">{{ $t('settings.trayIcon.dark') }}</option>
68+
</select>
69+
</div>
70+
</div>
5971
<div class="item">
6072
<div class="left">
6173
<div class="title">
@@ -907,6 +919,21 @@ export default {
907919
changeAppearance(value);
908920
},
909921
},
922+
trayIconTheme: {
923+
get() {
924+
if (this.settings.trayIconTheme === undefined) return 'auto';
925+
return this.settings.trayIconTheme;
926+
},
927+
set(value) {
928+
this.$store.commit('updateSettings', {
929+
key: 'trayIconTheme',
930+
value,
931+
});
932+
if (this.isElectron) {
933+
ipcRenderer.send('updateTrayIcon', value);
934+
}
935+
},
936+
},
910937
musicQuality: {
911938
get() {
912939
return this.settings.musicQuality ?? 320000;

0 commit comments

Comments
 (0)