Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tray icon/indicator Fix #2041

Merged
merged 6 commits into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 21 additions & 1 deletion source/browser/conversation-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,25 @@ function countUnread(mutationsList: MutationRecord[]): void {
}
}

async function updateTrayIcon(): Promise<void> {
const chatsIconSelector = selectors.chatsIcon;

const chatsIcon = await elementReady(chatsIconSelector, {
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});

if (sidebar) {
const conversationListObserver = new MutationObserver(async () => sendConversationList());
const conversationCountObserver = new MutationObserver(countUnread);
const chatsIconObserver = new MutationObserver(async () => updateTrayIcon());

conversationListObserver.observe(sidebar, {
subtree: true,
Expand All @@ -233,5 +246,12 @@ window.addEventListener('load', async () => {
attributes: true,
attributeFilter: ['class'],
});

chatsIconObserver.observe(sidebar, {
subtree: true,
childList: true,
attributes: true,
attributeFilter: ['class', 'aria-label'],
});
}
});
1 change: 1 addition & 0 deletions source/browser/selectors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default {
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"]',
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