Skip to content

Commit

Permalink
basic dynamic tray implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
SomeAspy committed Sep 14, 2024
1 parent ea1f955 commit f3b85fd
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default tseslint.config(
},
rules: {
"no-constant-binary-expression": 0,
"@typescript-eslint/no-non-null-assertion": 0,
"@typescript-eslint/no-unused-vars": [
2,
{
Expand Down
Binary file added resources/placeholderTrayIcon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/@types/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export interface Config {
multiInstance: boolean;
mods: "vencord"[];
autoHideMenuBar: boolean;
tray: boolean;
}
5 changes: 3 additions & 2 deletions src/main/discord/discordWindow.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {BrowserWindow} from "electron";
import {getConfig} from "../../shared/config.js";
import { join } from "path";
import {join} from "path";

export async function createDiscordWindow() {
const window = new BrowserWindow({
Expand All @@ -19,7 +19,7 @@ export async function createDiscordWindow() {
await window.loadURL(`https://${getConfig("channel")}.discord.com/app`);
}

//!SECTION Block Analytics
//SECTION - Block Analytics
window.webContents.session.webRequest.onBeforeRequest(
{
urls: [
Expand All @@ -33,4 +33,5 @@ export async function createDiscordWindow() {
callback({cancel: true});
}
);
return window;
}
7 changes: 6 additions & 1 deletion src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ import {createDiscordWindow} from "./discord/discordWindow.js";
import "./startup.js";
import "./ipc.js";
import {fetchMods} from "./functions/fetchMods.js";
import {startTray} from "./tray.js";
import {getConfig} from "../shared/config.js";

await fetchMods();

void app.whenReady().then(async () => {
await createDiscordWindow();
const Acorn = await createDiscordWindow();
if (getConfig("tray")) {
startTray(Acorn.webContents);
}
});
24 changes: 24 additions & 0 deletions src/main/tray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {app, Menu, nativeImage, Tray, type WebContents} from "electron";
import icon from "../../resources/placeholderTrayIcon.png?asset";

export function startTray(pageData: WebContents) {
const tray = new Tray(icon as string);

Check failure on line 5 in src/main/tray.ts

View workflow job for this annotation

GitHub Actions / Run linters

This assertion is unnecessary since it does not change the type of the expression
tray.setToolTip("Acorn");
tray.setContextMenu(
Menu.buildFromTemplate([
{
label: "Quit",
click() {
app.quit();
}
}
])
);
pageData.on("page-favicon-updated", (_, favicons) => {
try {
tray.setImage(nativeImage.createFromDataURL(favicons[0]!));
} catch {
return; // NOTE - Discord will send a URL before fully loaded
}
});
}
3 changes: 2 additions & 1 deletion src/shared/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ function makeConfig() {
channel: "stable",
multiInstance: false,
mods: ["vencord"],
autoHideMenuBar: true
autoHideMenuBar: true,
tray: true
} as Config;
writeFileSync(configLocation, JSON.stringify(config, null, 2), {encoding: "utf8", flag: "w"});
}
Expand Down
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
"ESNext",
"DOM"
],
"types": [
"electron-vite/node"
],
"target": "ESNext",
},
"include": [
Expand Down

0 comments on commit f3b85fd

Please sign in to comment.