Skip to content

Commit

Permalink
refactor(TrayIcon): move event listeners from the TrayIconManager to …
Browse files Browse the repository at this point in the history
…the TrayIconModule (oliverschwendener#1218)
  • Loading branch information
oliverschwendener authored Sep 23, 2024
1 parent 8e541d8 commit 6988277
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 48 deletions.
37 changes: 1 addition & 36 deletions src/main/Core/TrayIcon/TrayIconManager.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { EventSubscriber } from "@Core/EventSubscriber";
import type { Menu, MenuItemConstructorOptions, NativeTheme, Tray } from "electron";
import type { Menu, MenuItemConstructorOptions, Tray } from "electron";
import { describe, expect, it, vi } from "vitest";
import type { ContextMenuBuilder } from "./ContextMenuBuilder";
import type { ContextMenuTemplateProvider } from "./ContextMenuTemplateProvider";
Expand Down Expand Up @@ -39,8 +38,6 @@ describe(TrayIconManager, () => {
trayIconFilePathResolver,
contextMenuTemplateProvider,
contextMenuBuilder,
<NativeTheme>{},
<EventSubscriber>{},
);

await trayIconManager.createTrayIcon();
Expand All @@ -65,8 +62,6 @@ describe(TrayIconManager, () => {
<TrayIconFilePathResolver>{ resolve: () => resolveTrayIconFilePath() },
<ContextMenuTemplateProvider>{},
<ContextMenuBuilder>{},
<NativeTheme>{},
<EventSubscriber>{},
);

trayIconManager.updateImage();
Expand All @@ -83,8 +78,6 @@ describe(TrayIconManager, () => {
<TrayIconFilePathResolver>{ resolve: () => resolveTrayIconFilePath() },
<ContextMenuTemplateProvider>{},
<ContextMenuBuilder>{},
<NativeTheme>{},
<EventSubscriber>{},
);

trayIconManager["tray"] = <Tray>{ setImage: (i) => setImageMock(i) };
Expand All @@ -96,32 +89,4 @@ describe(TrayIconManager, () => {
expect(setImageMock).toHaveBeenCalledWith("test-tray-icon-file-path");
});
});

describe(TrayIconManager.prototype.registerEventListeners, () => {
it("should register the native theme updated event listener and the setting updated event listener", () => {
const onUpdatedMock = vi.fn();
const onMock = vi.fn().mockReturnValue({ on: onUpdatedMock });
const nativeTheme = <NativeTheme>{ on: (e, c) => onMock(e, c) };

const subscribeMock = vi.fn();
const eventSubscriber = <EventSubscriber>{ subscribe: (e, c) => subscribeMock(e, c) };

const trayIconManager = new TrayIconManager(
<TrayCreator>{},
<TrayIconFilePathResolver>{},
<ContextMenuTemplateProvider>{},
<ContextMenuBuilder>{},
nativeTheme,
eventSubscriber,
);

trayIconManager.registerEventListeners();

expect(onMock).toHaveBeenCalledOnce();
expect(onMock).toHaveBeenCalledWith("updated", expect.any(Function));

expect(subscribeMock).toHaveBeenCalledOnce();
expect(subscribeMock).toHaveBeenCalledWith("settingUpdated[general.language]", expect.any(Function));
});
});
});
10 changes: 1 addition & 9 deletions src/main/Core/TrayIcon/TrayIconManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { EventSubscriber } from "@Core/EventSubscriber";
import type { NativeTheme, Tray } from "electron";
import type { Tray } from "electron";
import type { ContextMenuBuilder } from "./ContextMenuBuilder";
import type { ContextMenuTemplateProvider } from "./ContextMenuTemplateProvider";
import type { TrayCreator } from "./TrayCreator";
Expand All @@ -13,8 +12,6 @@ export class TrayIconManager {
private readonly trayIconFilePathResolver: TrayIconFilePathResolver,
private readonly contextMenuTemplateProvider: ContextMenuTemplateProvider,
private readonly contextMenuBuilder: ContextMenuBuilder,
private readonly nativeTheme: NativeTheme,
private readonly eventSubscriber: EventSubscriber,
) {}

public async createTrayIcon() {
Expand All @@ -32,9 +29,4 @@ export class TrayIconManager {
const menu = this.contextMenuBuilder.buildFromTemplate(template);
this.tray?.setContextMenu(menu);
}

public registerEventListeners() {
this.nativeTheme.on("updated", () => this.updateImage());
this.eventSubscriber.subscribe("settingUpdated[general.language]", () => this.updateContextMenu());
}
}
6 changes: 3 additions & 3 deletions src/main/Core/TrayIcon/TrayIconModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ export class TrayIconModule {
trayIconFilePathResolvers[operatingSystem](),
new ContextMenuTemplateProvider(translator, ueliCommandInvoker, resources),
new ContextMenuBuilder(),
nativeTheme,
eventSubscriber,
);

await trayIconManager.createTrayIcon();

trayIconManager.registerEventListeners();
nativeTheme.on("updated", () => trayIconManager.updateImage());

eventSubscriber.subscribe("settingUpdated[general.language]", () => trayIconManager.updateContextMenu());
}
}

0 comments on commit 6988277

Please sign in to comment.