Repository navigation
feat(electron): system tray with playback controls #361
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
Open
Arylite
wants to merge
2
commits into
tryliner:main
Choose a base branch
from
Arylite:feat/system-tray
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| type FakeItem = { | ||
| label?: string; | ||
| type?: string; | ||
| enabled?: boolean; | ||
| click?: () => void; | ||
| }; | ||
|
|
||
| const { trays, FakeTray } = vi.hoisted(() => { | ||
| const trays: InstanceType<typeof FakeTray>[] = []; | ||
|
|
||
| class FakeTray { | ||
| destroyed = false; | ||
| listeners = new Map<string, () => void>(); | ||
| setToolTip = vi.fn(); | ||
| setContextMenu = vi.fn<(menu: FakeItem[]) => void>(); | ||
| constructor() { | ||
| trays.push(this); | ||
| } | ||
| on(event: string, listener: () => void) { | ||
| this.listeners.set(event, listener); | ||
| } | ||
| isDestroyed() { | ||
| return this.destroyed; | ||
| } | ||
| destroy() { | ||
| this.destroyed = true; | ||
| } | ||
| } | ||
|
|
||
| return { trays, FakeTray }; | ||
| }); | ||
|
|
||
| vi.mock("electron", () => ({ | ||
| Tray: FakeTray, | ||
| Menu: { buildFromTemplate: (template: FakeItem[]) => template }, | ||
| nativeImage: { | ||
| createFromPath: vi.fn(() => ({ resize: vi.fn(() => ({})) })), | ||
| }, | ||
| })); | ||
|
|
||
| import { createTray } from "./tray"; | ||
| import type { ThumbarAction } from "./thumbar"; | ||
|
|
||
| describe("TrayController", () => { | ||
| beforeEach(() => { | ||
| trays.length = 0; | ||
| }); | ||
|
|
||
| function setup(platform: NodeJS.Platform = "win32") { | ||
| const sent: ThumbarAction[] = []; | ||
| const showWindow = vi.fn(); | ||
| const quit = vi.fn(); | ||
| const controller = createTray({ | ||
| iconPath: "icon.png", | ||
| showWindow, | ||
| send: (action) => sent.push(action), | ||
| quit, | ||
| platform, | ||
| }); | ||
| const tray = trays[0]!; | ||
| const lastMenu = () => tray.setContextMenu.mock.calls.at(-1)?.[0] ?? []; | ||
| const item = (label: string) => lastMenu().find((i) => i.label === label); | ||
| return { sent, showWindow, quit, controller, tray, lastMenu, item }; | ||
| } | ||
|
|
||
| it("starts with playback disabled until a track is loaded", () => { | ||
| const { item } = setup(); | ||
| expect(item("Show Liner")?.enabled).toBeUndefined(); | ||
| expect(item("Previous")?.enabled).toBe(false); | ||
| expect(item("Play")?.enabled).toBe(false); | ||
| expect(item("Next")?.enabled).toBe(false); | ||
| }); | ||
|
|
||
| it("routes playback items to the player while a track plays", () => { | ||
| const { sent, controller, item } = setup(); | ||
| controller.update({ status: "playing", track: { id: "a" } }); | ||
| item("Previous")?.click?.(); | ||
| item("Pause")?.click?.(); | ||
| item("Next")?.click?.(); | ||
| expect(sent).toEqual(["prev", "togglePlay", "next"]); | ||
| }); | ||
|
|
||
| it("only rebuilds the menu when the play state changes", () => { | ||
| const { tray, controller, item } = setup(); | ||
| controller.update({ status: "playing", track: { id: "a" } }); | ||
| controller.update({ status: "buffering", track: { id: "b" } }); | ||
| expect(tray.setContextMenu).toHaveBeenCalledTimes(2); | ||
| controller.update({ status: "paused", track: { id: "b" } }); | ||
| expect(item("Play")?.enabled).toBe(true); | ||
| expect(tray.setContextMenu).toHaveBeenCalledTimes(3); | ||
| }); | ||
|
|
||
| it("disables playback items when the track is removed", () => { | ||
| const { controller, item } = setup(); | ||
| controller.update({ status: "playing", track: { id: "a" } }); | ||
| controller.update({ status: "idle", track: null }); | ||
| expect(item("Previous")?.enabled).toBe(false); | ||
| expect(item("Play")?.enabled).toBe(false); | ||
| expect(item("Next")?.enabled).toBe(false); | ||
| }); | ||
|
|
||
| it("shows the window and quits from the menu", () => { | ||
| const { showWindow, quit, item } = setup(); | ||
| item("Show Liner")?.click?.(); | ||
| item("Quit Liner")?.click?.(); | ||
| expect(showWindow).toHaveBeenCalledOnce(); | ||
| expect(quit).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it("brings the window back on click outside macos", () => { | ||
| const { tray, showWindow } = setup(); | ||
| tray.listeners.get("click")?.(); | ||
| expect(showWindow).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it("leaves the click to the menu on macos", () => { | ||
| const { tray, item } = setup("darwin"); | ||
| expect(tray.listeners.has("click")).toBe(false); | ||
| expect(item("Show Liner")).toBeDefined(); | ||
| }); | ||
|
|
||
| it("stops touching the tray once destroyed", () => { | ||
| const { tray, controller } = setup(); | ||
| controller.destroy(); | ||
| expect(tray.destroyed).toBe(true); | ||
| controller.update({ status: "playing", track: { id: "a" } }); | ||
| expect(tray.setContextMenu).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import { | ||
| Menu, | ||
| Tray, | ||
| nativeImage, | ||
| type MenuItemConstructorOptions, | ||
| } from "electron"; | ||
| import { | ||
| PLAYING_STATUSES, | ||
| type ThumbarAction, | ||
| type ThumbarPlayerState, | ||
| } from "./thumbar.js"; | ||
|
|
||
| const MACOS_ICON_SIZE = 16; | ||
| const ICON_SIZE = 32; | ||
|
|
||
| export interface TrayDeps { | ||
| iconPath: string; | ||
| showWindow: () => void; | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| send: (action: ThumbarAction) => void; | ||
| quit: () => void; | ||
| platform?: NodeJS.Platform; | ||
| } | ||
|
|
||
| export function trayMenuTemplate( | ||
| state: { hasTrack: boolean; playing: boolean }, | ||
| deps: Pick<TrayDeps, "showWindow" | "send" | "quit">, | ||
| ): MenuItemConstructorOptions[] { | ||
| return [ | ||
| { label: "Show Liner", click: () => deps.showWindow() }, | ||
| { type: "separator" }, | ||
| { | ||
| label: "Previous", | ||
| enabled: state.hasTrack, | ||
| click: () => deps.send("prev"), | ||
| }, | ||
| { | ||
| label: state.playing ? "Pause" : "Play", | ||
| enabled: state.hasTrack, | ||
| click: () => deps.send("togglePlay"), | ||
| }, | ||
| { | ||
| label: "Next", | ||
| enabled: state.hasTrack, | ||
| click: () => deps.send("next"), | ||
| }, | ||
| { type: "separator" }, | ||
| { label: "Quit Liner", click: () => deps.quit() }, | ||
| ]; | ||
| } | ||
|
|
||
| export class TrayController { | ||
| private tray: Tray | null = null; | ||
| private hasTrack = false; | ||
| private playing = false; | ||
| private appliedKey: string | null = null; | ||
|
|
||
| constructor(private readonly deps: TrayDeps) {} | ||
|
|
||
| create() { | ||
| if (this.tray) return; | ||
| const platform = this.deps.platform ?? process.platform; | ||
| const opensMenuOnClick = platform === "darwin"; | ||
| try { | ||
| const size = opensMenuOnClick ? MACOS_ICON_SIZE : ICON_SIZE; | ||
| const icon = nativeImage | ||
| .createFromPath(this.deps.iconPath) | ||
| .resize({ width: size, height: size }); | ||
| this.tray = new Tray(icon); | ||
| } catch (err) { | ||
| console.warn("[tray] could not create tray icon:", err); | ||
| return; | ||
| } | ||
| this.tray.setToolTip("Liner"); | ||
| if (!opensMenuOnClick) { | ||
| this.tray.on("click", () => this.deps.showWindow()); | ||
| } | ||
| this.apply(true); | ||
| } | ||
|
|
||
| update(state: ThumbarPlayerState) { | ||
| this.hasTrack = Boolean(state?.track); | ||
| this.playing = PLAYING_STATUSES.has(state?.status ?? ""); | ||
| this.apply(false); | ||
| } | ||
|
|
||
| destroy() { | ||
| if (this.tray && !this.tray.isDestroyed()) this.tray.destroy(); | ||
| this.tray = null; | ||
| this.appliedKey = null; | ||
| } | ||
|
|
||
| private apply(force: boolean) { | ||
| if (!this.tray || this.tray.isDestroyed()) return; | ||
| const key = this.hasTrack ? (this.playing ? "playing" : "paused") : "none"; | ||
| if (!force && key === this.appliedKey) return; | ||
| this.tray.setContextMenu( | ||
| Menu.buildFromTemplate( | ||
| trayMenuTemplate( | ||
| { hasTrack: this.hasTrack, playing: this.playing }, | ||
| this.deps, | ||
| ), | ||
| ), | ||
| ); | ||
| this.appliedKey = key; | ||
| } | ||
| } | ||
|
|
||
| export function createTray(deps: TrayDeps): TrayController { | ||
| const tray = new TrayController(deps); | ||
| tray.create(); | ||
| return tray; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.