Skip to content

Commit

Permalink
Fix tray icon indicator (#2041)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
stkrknds and dusansimic authored Sep 2, 2023
1 parent d1ce863 commit 0d38a71
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
24 changes: 23 additions & 1 deletion source/browser/conversation-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,19 @@ function countUnread(mutationsList: MutationRecord[]): void {
}
}

async function updateTrayIcon(): Promise<void> {
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());
Expand All @@ -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'],
});
}
});
2 changes: 2 additions & 0 deletions source/browser/selectors.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
27 changes: 3 additions & 24 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,7 @@ app.on('ready', () => {
});
});

function getMessageCount(conversations: Conversation[]): number {
return conversations.filter(({unread}) => unread).length;
}

async function updateBadge(conversations: Conversation[]): Promise<void> {
// Ignore `Sindre messaged you` blinking
if (!Array.isArray(conversations)) {
return;
}

const messageCount = getMessageCount(conversations);

async function updateBadge(messageCount: number): Promise<void> {
if (!is.windows) {
if (config.get('showUnreadBadge') && !isDNDEnabled) {
app.badgeCount = messageCount;
Expand Down Expand Up @@ -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<string, string>;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 0d38a71

Please sign in to comment.