Skip to content

Commit

Permalink
Add operating system specific ap icon
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverschwendener committed Feb 23, 2024
1 parent db4cde8 commit faa30ed
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 0 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Core/BrowserWindow/app-icon-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/Core/BrowserWindow/app-icon-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions src/main/Core/BrowserWindow/BrowserWindowModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { SearchResultItemAction } from "@common/Core";
import type { BrowserWindow, BrowserWindowConstructorOptions } from "electron";
import { join } from "path";
import { createBrowserWindow } from "./createBrowserWindow";
import { getAppIconFilePath } from "./getAppIconFilePath";
import { getBackgroundMaterial } from "./getBackgroundMaterial";
import { getVibrancy } from "./getVibrancy";
import { openAndFocusBrowserWindow } from "./openAndFocusBrowserWindow";
Expand All @@ -15,11 +16,22 @@ import { toggleBrowserWindow } from "./toggleBrowserWindow";
export class BrowserWindowModule {
public static async bootstrap(dependencyRegistry: DependencyRegistry<Dependencies>) {
const eventEmitter = dependencyRegistry.get("EventEmitter");
const nativeTheme = dependencyRegistry.get("NativeTheme");

const browserWindow = createBrowserWindow(dependencyRegistry);

eventEmitter.emitEvent("browserWindowCreated", { browserWindow });

nativeTheme.addListener("updated", () =>
browserWindow.setIcon(
getAppIconFilePath(
dependencyRegistry.get("NativeTheme"),
dependencyRegistry.get("AssetPathResolver"),
dependencyRegistry.get("OperatingSystem"),
),
),
);

BrowserWindowModule.registerBrowserWindowEventListeners(browserWindow, dependencyRegistry);
BrowserWindowModule.registerEvents(browserWindow, dependencyRegistry);
await BrowserWindowModule.loadFileOrUrl(browserWindow, dependencyRegistry);
Expand Down
6 changes: 6 additions & 0 deletions src/main/Core/BrowserWindow/createBrowserWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { DependencyRegistry } from "@Core/DependencyRegistry";
import type { OperatingSystem } from "@common/Core";
import { BrowserWindow, type BrowserWindowConstructorOptions } from "electron";
import { join } from "path";
import { getAppIconFilePath } from "./getAppIconFilePath";
import { getBackgroundMaterial } from "./getBackgroundMaterial";
import { getVibrancy } from "./getVibrancy";

Expand All @@ -28,6 +29,11 @@ export const createBrowserWindow = (dependencyRegistry: DependencyRegistry<Depen
spellcheck: false,
},
alwaysOnTop,
icon: getAppIconFilePath(
dependencyRegistry.get("NativeTheme"),
dependencyRegistry.get("AssetPathResolver"),
dependencyRegistry.get("OperatingSystem"),
),
};

const extendDefaultBrowserWindowOptions = (browserWindowOptions: BrowserWindowConstructorOptions) => {
Expand Down
60 changes: 60 additions & 0 deletions src/main/Core/BrowserWindow/getAppIconFilePath.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import type { AssetPathResolver } from "@Core/AssetPathResolver";
import type { NativeTheme } from "electron";
import { describe, expect, it, vi } from "vitest";
import { getAppIconFilePath } from "./getAppIconFilePath";

describe(getAppIconFilePath, () => {
it("it should return the correct app icon file path on Windows' dark theme", () => {
const getModuleAssetPathMock = vi.fn().mockReturnValue("windows-dark-theme-icon.png");
const assetPathResolver = <AssetPathResolver>{ getModuleAssetPath: (m, a) => getModuleAssetPathMock(m, a) };
const nativeTheme = <NativeTheme>{ shouldUseDarkColors: true };

expect(getAppIconFilePath(nativeTheme, assetPathResolver, "Windows")).toBe("windows-dark-theme-icon.png");
expect(getModuleAssetPathMock).toHaveBeenCalledWith("BrowserWindow", "app-icon-dark-transparent.png");
});

it("it should return the correct app icon file path on Windows' light theme", () => {
const getModuleAssetPathMock = vi.fn().mockReturnValue("windows-light-theme-icon.png");
const assetPathResolver = <AssetPathResolver>{ getModuleAssetPath: (m, a) => getModuleAssetPathMock(m, a) };
const nativeTheme = <NativeTheme>{ shouldUseDarkColors: false };

expect(getAppIconFilePath(nativeTheme, assetPathResolver, "Windows")).toBe("windows-light-theme-icon.png");
expect(getModuleAssetPathMock).toHaveBeenCalledWith("BrowserWindow", "app-icon-light-transparent.png");
});

it("it should return the correct app icon file path on macOS' dark theme", () => {
const getModuleAssetPathMock = vi.fn().mockReturnValue("macos-dark-theme-icon.png");
const assetPathResolver = <AssetPathResolver>{ getModuleAssetPath: (m, a) => getModuleAssetPathMock(m, a) };
const nativeTheme = <NativeTheme>{ shouldUseDarkColors: true };

expect(getAppIconFilePath(nativeTheme, assetPathResolver, "macOS")).toBe("macos-dark-theme-icon.png");
expect(getModuleAssetPathMock).toHaveBeenCalledWith("BrowserWindow", "app-icon-dark.png");
});

it("it should return the correct app icon file path on macOS' light theme", () => {
const getModuleAssetPathMock = vi.fn().mockReturnValue("macos-light-theme-icon.png");
const assetPathResolver = <AssetPathResolver>{ getModuleAssetPath: (m, a) => getModuleAssetPathMock(m, a) };
const nativeTheme = <NativeTheme>{ shouldUseDarkColors: false };

expect(getAppIconFilePath(nativeTheme, assetPathResolver, "macOS")).toBe("macos-light-theme-icon.png");
expect(getModuleAssetPathMock).toHaveBeenCalledWith("BrowserWindow", "app-icon-light.png");
});

it("it should return the correct app icon file path on Linux' dark theme", () => {
const getModuleAssetPathMock = vi.fn().mockReturnValue("linux-dark-theme-icon.png");
const assetPathResolver = <AssetPathResolver>{ getModuleAssetPath: (m, a) => getModuleAssetPathMock(m, a) };
const nativeTheme = <NativeTheme>{ shouldUseDarkColors: true };

expect(getAppIconFilePath(nativeTheme, assetPathResolver, "macOS")).toBe("linux-dark-theme-icon.png");
expect(getModuleAssetPathMock).toHaveBeenCalledWith("BrowserWindow", "app-icon-dark.png");
});

it("it should return the correct app icon file path on Linux' light theme", () => {
const getModuleAssetPathMock = vi.fn().mockReturnValue("linux-light-theme-icon.png");
const assetPathResolver = <AssetPathResolver>{ getModuleAssetPath: (m, a) => getModuleAssetPathMock(m, a) };
const nativeTheme = <NativeTheme>{ shouldUseDarkColors: false };

expect(getAppIconFilePath(nativeTheme, assetPathResolver, "macOS")).toBe("linux-light-theme-icon.png");
expect(getModuleAssetPathMock).toHaveBeenCalledWith("BrowserWindow", "app-icon-light.png");
});
});
21 changes: 21 additions & 0 deletions src/main/Core/BrowserWindow/getAppIconFilePath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { AssetPathResolver } from "@Core/AssetPathResolver";
import type { OperatingSystem } from "@common/Core";
import type { NativeTheme } from "electron";

export const getAppIconFilePath = (
nativeTheme: NativeTheme,
assetPathResolver: AssetPathResolver,
operatingSystem: OperatingSystem,
): string => {
const fileNames: Record<OperatingSystem, { dark: string; light: string }> = {
Linux: { dark: "app-icon-dark.png", light: "app-icon-light.png" },
macOS: { dark: "app-icon-dark.png", light: "app-icon-light.png" },
Windows: { dark: "app-icon-dark-transparent.png", light: "app-icon-light-transparent.png" },
};

const filename = nativeTheme.shouldUseDarkColors
? fileNames[operatingSystem].dark
: fileNames[operatingSystem].light;

return assetPathResolver.getModuleAssetPath("BrowserWindow", filename);
};

0 comments on commit faa30ed

Please sign in to comment.