Skip to content
Draft
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: add media metadata properties and update methods to Tab class
Add mediaTitle, mediaArtist, mediaArtwork, mediaPlaybackState properties.
Add updateMediaMetadata() and clearMediaMetadata() methods that diff
incoming data and emit 'updated' only when values change.
Extend TabContentProperty union with the four new keys.
  • Loading branch information
iamEvanYT committed Mar 15, 2026
commit b7fd77c811575874825d6463c6a0c12c7766de4b
89 changes: 88 additions & 1 deletion src/main/controllers/tabs-controller/tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,18 @@ type TabStateProperty =
| "asleep"
| "lastActiveAt"
| "position";
type TabContentProperty = "title" | "url" | "isLoading" | "audible" | "muted" | "navHistory" | "navHistoryIndex";
type TabContentProperty =
| "title"
| "url"
| "isLoading"
| "audible"
| "muted"
| "navHistory"
| "navHistoryIndex"
| "mediaTitle"
| "mediaArtist"
| "mediaArtwork"
| "mediaPlaybackState";

export type TabPublicProperty = TabStateProperty | TabContentProperty;

Expand Down Expand Up @@ -162,6 +173,12 @@ export class Tab extends TypedEventEmitter<TabEvents> {
public navHistory: NavigationEntry[] = [];
public navHistoryIndex: number = 0;

// Media metadata (from page's navigator.mediaSession, polled while audible)
public mediaTitle: string | null = null;
public mediaArtist: string | null = null;
public mediaArtwork: string | null = null;
public mediaPlaybackState: "playing" | "paused" | "none" = "none";

// Cached for nav history diff (avoids JSON.stringify every time)
private lastNavHistoryLength: number = 0;
private lastNavHistoryIndex: number = 0;
Expand Down Expand Up @@ -722,6 +739,76 @@ export class Tab extends TypedEventEmitter<TabEvents> {
this.loadURL(errorPageURL.toString(), replace);
}

// --- Media Metadata (from preload) ---

/**
* Updates media metadata from the page's navigator.mediaSession.
* Called by the IPC handler when the preload script detects a change.
* Emits "updated" if any values change.
*/
public updateMediaMetadata(metadata: {
title: string | null;
artist: string | null;
artwork: string | null;
playbackState: "playing" | "paused" | "none";
}): void {
if (this.isDestroyed) return;

const changedKeys: TabContentProperty[] = [];

if (metadata.title !== this.mediaTitle) {
this.mediaTitle = metadata.title;
changedKeys.push("mediaTitle");
}
if (metadata.artist !== this.mediaArtist) {
this.mediaArtist = metadata.artist;
changedKeys.push("mediaArtist");
}
if (metadata.artwork !== this.mediaArtwork) {
this.mediaArtwork = metadata.artwork;
changedKeys.push("mediaArtwork");
}
if (metadata.playbackState !== this.mediaPlaybackState) {
this.mediaPlaybackState = metadata.playbackState;
changedKeys.push("mediaPlaybackState");
}

if (changedKeys.length > 0) {
this.emit("updated", changedKeys);
}
}

/**
* Clears media metadata when the page loses MediaSession data.
* Called by the preload when metadata becomes null.
*/
public clearMediaMetadata(): void {
if (this.isDestroyed) return;

const changedKeys: TabContentProperty[] = [];

if (this.mediaTitle !== null) {
this.mediaTitle = null;
changedKeys.push("mediaTitle");
}
if (this.mediaArtist !== null) {
this.mediaArtist = null;
changedKeys.push("mediaArtist");
}
if (this.mediaArtwork !== null) {
this.mediaArtwork = null;
changedKeys.push("mediaArtwork");
}
if (this.mediaPlaybackState !== "none") {
this.mediaPlaybackState = "none";
changedKeys.push("mediaPlaybackState");
}

if (changedKeys.length > 0) {
this.emit("updated", changedKeys);
}
}

// --- Destruction ---

/**
Expand Down