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

feat(linux): Emit D-Bus signal directly for dock notification badges #686

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"electron-updater": "^6.3.9"
},
"optionalDependencies": {
"@vencord/venmic": "^6.1.0"
"@vencord/venmic": "^6.1.0",
"@homebridge/dbus-native": "0.6.0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding a new library that adds more than 200kb of javascript just for one single dbus call is pretty bad

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#517 also uses it for accent calls.

we were shelling out before but #686 (comment)

},
"devDependencies": {
"@fal-works/esbuild-plugin-global-externals": "^2.1.2",
Expand Down
119 changes: 119 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 24 additions & 2 deletions src/main/appBadge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { app, NativeImage, nativeImage } from "electron";
import { join } from "path";
import { BADGE_DIR } from "shared/paths";

import { dbus, getSessionBus } from "./utils/dbus";

const imgCache = new Map<number, NativeImage>();
function loadBadge(index: number) {
const cached = imgCache.get(index);
Expand All @@ -24,8 +26,28 @@ let lastIndex: null | number = -1;
export function setBadgeCount(count: number) {
switch (process.platform) {
case "linux":
if (count === -1) count = 0;
Covkie marked this conversation as resolved.
Show resolved Hide resolved
app.setBadgeCount(count);
if (typeof count !== "number") {
throw new Error("count must be a number"); // sanitize
}

const sessionBus = getSessionBus();
sessionBus.connection.message({
type: dbus.messageType.signal,
serial: 1,
path: "/",
interface: "com.canonical.Unity.LauncherEntry",
member: "Update",
signature: "sa{sv}",
body: [
process.env.container === "1"
? "application://dev.vencord.Vesktop.desktop" // flatpak handling
: "application://vesktop.desktop",
[
["count", ["x", count === -1 ? 0 : count]],
["count-visible", ["b", count !== 0]]
]
]
});
break;
case "darwin":
if (count === 0) {
Expand Down
1 change: 1 addition & 0 deletions src/main/mainWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ function initStaticTitle(win: BrowserWindow) {
win.setTitle("Vesktop");
win.on("page-title-updated", listener);
} else {
win.setTitle(win.getTitle().replace(/^\(\d+\)\s*|•\s/, ""));
win.off("page-title-updated", listener);
}
});
Expand Down
16 changes: 16 additions & 0 deletions src/main/utils/dbus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* SPDX-License-Identifier: GPL-3.0
* Vesktop, a desktop app aiming to give you a snappier Discord Experience
* Copyright (c) 2023 Vendicated and Vencord contributors
*/

import dbus from "@homebridge/dbus-native";

let sessionBus: dbus.MessageBus | null;

export function getSessionBus(): dbus.MessageBus {
if (!sessionBus) sessionBus = dbus.sessionBus();
return sessionBus;
}

export { dbus };
5 changes: 4 additions & 1 deletion src/renderer/appBadge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ import { Settings } from "./settings";

let GuildReadStateStore: any;
let NotificationSettingsStore: any;
let MessageRequestStore: any;

export function setBadge() {
if (Settings.store.appBadge === false) return;

try {
const mentionCount = GuildReadStateStore.getTotalMentionCount();
const pendingRequests = RelationshipStore.getPendingCount();
const messageRequests = MessageRequestStore.getMessageRequestsCount();
const hasUnread = GuildReadStateStore.hasAnyUnread();
const disableUnreadBadge = NotificationSettingsStore.getDisableUnreadBadge();

let totalCount = mentionCount + pendingRequests;
let totalCount = mentionCount + pendingRequests + messageRequests;
Covkie marked this conversation as resolved.
Show resolved Hide resolved
if (!totalCount && hasUnread && !disableUnreadBadge) totalCount = -1;

VesktopNative.app.setBadgeCount(totalCount);
Expand All @@ -45,4 +47,5 @@ function waitForAndSubscribeToStore(name: string, cb?: (m: any) => void) {

waitForAndSubscribeToStore("GuildReadStateStore", store => (GuildReadStateStore = store));
waitForAndSubscribeToStore("NotificationSettingsStore", store => (NotificationSettingsStore = store));
waitForAndSubscribeToStore("MessageRequestStore", store => (MessageRequestStore = store));
waitForAndSubscribeToStore("RelationshipStore");