From 0d38a719ef29db2b1f1594ed6008d18550595047 Mon Sep 17 00:00:00 2001 From: Stratos Karakondis <69848524+stkrknds@users.noreply.github.com> Date: Sat, 2 Sep 2023 11:53:53 +0300 Subject: [PATCH] Fix tray icon indicator (#2041) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * tray icon fix * use chats icon * Update source/browser/conversation-list.ts Co-authored-by: Dušan Simić * Update source/browser/selectors.ts Co-authored-by: Dušan Simić * fix typo # -> // * observe left sidebar --------- Co-authored-by: Dušan Simić --- source/browser/conversation-list.ts | 24 +++++++++++++++++++++++- source/browser/selectors.ts | 2 ++ source/index.ts | 27 +++------------------------ 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/source/browser/conversation-list.ts b/source/browser/conversation-list.ts index 8d6773b10..935ed612a 100644 --- a/source/browser/conversation-list.ts +++ b/source/browser/conversation-list.ts @@ -213,8 +213,19 @@ function countUnread(mutationsList: MutationRecord[]): void { } } +async function updateTrayIcon(): Promise { + const chatsIcon = await elementReady(selectors.chatsIcon, { + stopOnDomReady: false, + }); + + // Extract messageCount from ariaLabel + const messageCount = chatsIcon?.ariaLabel?.match(/\d+/g) ?? 0; + ipc.callMain('update-tray-icon', messageCount); +} + window.addEventListener('load', async () => { - const sidebar = await elementReady('[role=navigation]', {stopOnDomReady: false}); + const sidebar = await elementReady('[role=navigation]:has([role=grid])', {stopOnDomReady: false}); + const leftSidebar = await elementReady(`${selectors.leftSidebar}:has(${selectors.chatsIcon})`, {stopOnDomReady: false}); if (sidebar) { const conversationListObserver = new MutationObserver(async () => sendConversationList()); @@ -234,4 +245,15 @@ window.addEventListener('load', async () => { attributeFilter: ['class'], }); } + + if (leftSidebar) { + const chatsIconObserver = new MutationObserver(async () => updateTrayIcon()); + + chatsIconObserver.observe(leftSidebar, { + subtree: true, + childList: true, + attributes: true, + attributeFilter: ['aria-label'], + }); + } }); diff --git a/source/browser/selectors.ts b/source/browser/selectors.ts index e14c1fc9e..61894fe33 100644 --- a/source/browser/selectors.ts +++ b/source/browser/selectors.ts @@ -1,4 +1,6 @@ export default { + leftSidebar: '[class="x9f619 x1n2onr6 x1ja2u2z x78zum5 xdt5ytf x2lah0s x193iq5w xeuugli xycxndf xkhd6sd x4uap5 xexx8yu x18d9i69"]', // ! Tray icon dependency + 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 conversationList: '[role=navigation] [role=grid]', conversationSelector: '[role=main] [role=grid]', notificationCheckbox: '._374b:nth-of-type(4) ._4ng2 input', diff --git a/source/index.ts b/source/index.ts index a4d5ee9a9..1332c1d19 100644 --- a/source/index.ts +++ b/source/index.ts @@ -99,18 +99,7 @@ app.on('ready', () => { }); }); -function getMessageCount(conversations: Conversation[]): number { - return conversations.filter(({unread}) => unread).length; -} - -async function updateBadge(conversations: Conversation[]): Promise { - // Ignore `Sindre messaged you` blinking - if (!Array.isArray(conversations)) { - return; - } - - const messageCount = getMessageCount(conversations); - +async function updateBadge(messageCount: number): Promise { if (!is.windows) { if (config.get('showUnreadBadge') && !isDNDEnabled) { app.badgeCount = messageCount; @@ -154,16 +143,6 @@ function updateOverlayIcon({data, text}: {data: string; text: string}): void { mainWindow.setOverlayIcon(img, text); } -function updateTrayIcon(): void { - if (!config.get('showTrayIcon') || config.get('quitOnWindowClose')) { - tray.destroy(); - } else { - tray.create(mainWindow); - } -} - -ipc.answerRenderer('update-tray-icon', updateTrayIcon); - interface BeforeSendHeadersResponse { cancel?: boolean; requestHeaders?: Record; @@ -437,8 +416,8 @@ function createMainWindow(): BrowserWindow { } // Update badge on conversations change - ipc.answerRenderer('conversations', async (conversations: Conversation[]) => { - updateBadge(conversations); + ipc.answerRenderer('update-tray-icon', async (messageCount: number) => { + updateBadge(messageCount); }); enableHiresResources();