Skip to content

Commit 0d38a71

Browse files
stkrkndsdusansimic
andauthored
Fix tray icon indicator (#2041)
* tray icon fix * use chats icon * Update source/browser/conversation-list.ts Co-authored-by: Dušan Simić <dusan.simic1810@gmail.com> * Update source/browser/selectors.ts Co-authored-by: Dušan Simić <dusan.simic1810@gmail.com> * fix typo # -> // * observe left sidebar --------- Co-authored-by: Dušan Simić <dusan.simic1810@gmail.com>
1 parent d1ce863 commit 0d38a71

File tree

3 files changed

+28
-25
lines changed

3 files changed

+28
-25
lines changed

source/browser/conversation-list.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,19 @@ function countUnread(mutationsList: MutationRecord[]): void {
213213
}
214214
}
215215

216+
async function updateTrayIcon(): Promise<void> {
217+
const chatsIcon = await elementReady(selectors.chatsIcon, {
218+
stopOnDomReady: false,
219+
});
220+
221+
// Extract messageCount from ariaLabel
222+
const messageCount = chatsIcon?.ariaLabel?.match(/\d+/g) ?? 0;
223+
ipc.callMain('update-tray-icon', messageCount);
224+
}
225+
216226
window.addEventListener('load', async () => {
217-
const sidebar = await elementReady('[role=navigation]', {stopOnDomReady: false});
227+
const sidebar = await elementReady('[role=navigation]:has([role=grid])', {stopOnDomReady: false});
228+
const leftSidebar = await elementReady(`${selectors.leftSidebar}:has(${selectors.chatsIcon})`, {stopOnDomReady: false});
218229

219230
if (sidebar) {
220231
const conversationListObserver = new MutationObserver(async () => sendConversationList());
@@ -234,4 +245,15 @@ window.addEventListener('load', async () => {
234245
attributeFilter: ['class'],
235246
});
236247
}
248+
249+
if (leftSidebar) {
250+
const chatsIconObserver = new MutationObserver(async () => updateTrayIcon());
251+
252+
chatsIconObserver.observe(leftSidebar, {
253+
subtree: true,
254+
childList: true,
255+
attributes: true,
256+
attributeFilter: ['aria-label'],
257+
});
258+
}
237259
});

source/browser/selectors.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export default {
2+
leftSidebar: '[class="x9f619 x1n2onr6 x1ja2u2z x78zum5 xdt5ytf x2lah0s x193iq5w xeuugli xycxndf xkhd6sd x4uap5 xexx8yu x18d9i69"]', // ! Tray icon dependency
3+
chatsIcon: '[class="x1i10hfl xjqpnuy xa49m3k xqeqjp1 x2hbi6w x13fuv20 xu3j5b3 x1q0q8m5 x26u7qi x972fbf xcfux6l x1qhh985 xm0m39n x9f619 x1ypdohk xdl72j9 x2lah0s xe8uvvx x2lwn1j xeuugli x4uap5 xkhd6sd x1n2onr6 x16tdsg8 x1hl2dhg xggy1nq x1ja2u2z x1t137rt x87ps6o x1lku1pv x1a2a7pz x6s0dn4 x1q0g3np xn3w4p2 x1nn3v0j x1120s5i x1av1boa x1lq5wgf xgqcy7u x30kzoy x9jhf4c xdj266r x11i5rnm xat24cr x1mh8g0r x78zum5"]', // ! Tray icon dependency
24
conversationList: '[role=navigation] [role=grid]',
35
conversationSelector: '[role=main] [role=grid]',
46
notificationCheckbox: '._374b:nth-of-type(4) ._4ng2 input',

source/index.ts

+3-24
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,7 @@ app.on('ready', () => {
9999
});
100100
});
101101

102-
function getMessageCount(conversations: Conversation[]): number {
103-
return conversations.filter(({unread}) => unread).length;
104-
}
105-
106-
async function updateBadge(conversations: Conversation[]): Promise<void> {
107-
// Ignore `Sindre messaged you` blinking
108-
if (!Array.isArray(conversations)) {
109-
return;
110-
}
111-
112-
const messageCount = getMessageCount(conversations);
113-
102+
async function updateBadge(messageCount: number): Promise<void> {
114103
if (!is.windows) {
115104
if (config.get('showUnreadBadge') && !isDNDEnabled) {
116105
app.badgeCount = messageCount;
@@ -154,16 +143,6 @@ function updateOverlayIcon({data, text}: {data: string; text: string}): void {
154143
mainWindow.setOverlayIcon(img, text);
155144
}
156145

157-
function updateTrayIcon(): void {
158-
if (!config.get('showTrayIcon') || config.get('quitOnWindowClose')) {
159-
tray.destroy();
160-
} else {
161-
tray.create(mainWindow);
162-
}
163-
}
164-
165-
ipc.answerRenderer('update-tray-icon', updateTrayIcon);
166-
167146
interface BeforeSendHeadersResponse {
168147
cancel?: boolean;
169148
requestHeaders?: Record<string, string>;
@@ -437,8 +416,8 @@ function createMainWindow(): BrowserWindow {
437416
}
438417

439418
// Update badge on conversations change
440-
ipc.answerRenderer('conversations', async (conversations: Conversation[]) => {
441-
updateBadge(conversations);
419+
ipc.answerRenderer('update-tray-icon', async (messageCount: number) => {
420+
updateBadge(messageCount);
442421
});
443422

444423
enableHiresResources();

0 commit comments

Comments
 (0)