Skip to content

Commit

Permalink
feat: add all active sessions and media control functions to media
Browse files Browse the repository at this point in the history
…provider (#158)
  • Loading branch information
lars-berger authored Nov 22, 2024
1 parent 315608a commit 1dfeacc
Show file tree
Hide file tree
Showing 10 changed files with 570 additions and 195 deletions.
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,23 +358,26 @@ No config options.

| Variable | Description | Return type | Supported OS |
| ------------------- | ----------- | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `session` | Currently playing media session. | `MediaSession \| null` | <img src="https://github.com/glzr-io/zebar/assets/34844898/568e90c8-cd32-49a5-a17f-ab233d41f1aa" alt="microsoft icon" width="24"> |
| `currentSession` | Currently playing media session. | `MediaSession \| null` | <img src="https://github.com/glzr-io/zebar/assets/34844898/568e90c8-cd32-49a5-a17f-ab233d41f1aa" alt="microsoft icon" width="24"> |
| `allSessions` | All active media sessions. | `MediaSession[]` | <img src="https://github.com/glzr-io/zebar/assets/34844898/568e90c8-cd32-49a5-a17f-ab233d41f1aa" alt="microsoft icon" width="24"> |

#### Return types

#### `MediaSession`

| Variable | Description | Return type |
| ------------------ | ----------------------------- | ----------------------- |
| `title` | TODO | `string` |
| `artist` | TODO | `string \| null` |
| `albumTitle` | TODO | `string \| null` |
| `albumArtist` | TODO | `string \| null` |
| `trackNumber` | TODO | `number` |
| `startTime` | TODO | `number` |
| `endTime` | TODO | `number` |
| `position` | TODO | `number` |
| `isPlaying` | TODO | `boolean` |
| `sessionId` | ID of the media session. | `string` |
| `title` | Title of the media session. | `string` |
| `artist` | Artist of the media session. | `string \| null` |
| `albumTitle` | Album title of the media session. | `string \| null` |
| `albumArtist` | Album artist of the media session. | `string \| null` |
| `trackNumber` | Track number of the media session. | `number` |
| `startTime` | Start time of the media session. | `number` |
| `endTime` | End time of the media session. | `number` |
| `position` | Position of the media session. | `number` |
| `isPlaying` | Whether the media session is playing. | `boolean` |
| `isCurrentSession` | Whether this is the currently active session (i.e. `currentSession`). | `boolean` |

#### `DataSizeMeasure`

Expand Down
7 changes: 5 additions & 2 deletions examples/boilerplate-react-buildless/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@
return (
<div className="app">
<div class="chip">
Media: {output.media?.session?.title} -
{output.media?.session?.artist}
Media: {output.media?.currentSession?.title} -
{output.media?.currentSession?.artist}
<button onClick={() => output.media?.togglePlayPause()}>
</button>
</div>
<div className="chip">CPU usage: {output.cpu?.usage}</div>
<div className="chip">
Expand Down
5 changes: 3 additions & 2 deletions examples/boilerplate-solid-ts/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ function App() {
{output.audio?.defaultPlaybackDevice?.volume}
</div>
<div class="chip">
Media: {output.media?.session?.title} -
{output.media?.session?.artist}
Media: {output.media?.currentSession?.title} -
{output.media?.currentSession?.artist}
<button onClick={() => output.media?.togglePlayPause()}></button>
</div>
<div class="chip">CPU usage: {output.cpu?.usage}</div>
<div class="chip">
Expand Down
25 changes: 25 additions & 0 deletions packages/client-api/src/desktop/desktop-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,25 @@ export const desktopCommands = {
startPreset,
listenProvider,
unlistenProvider,
callProviderFunction,
setAlwaysOnTop,
setSkipTaskbar,
};

export type ProviderFunction = MediaFunction;

export interface MediaFunction {
type: 'media';
function: {
name: 'play' | 'pause' | 'toggle_play_pause' | 'next' | 'previous';
args: MediaControlArgs;
};
}

export interface MediaControlArgs {
sessionId?: string;
}

function startWidget(
configPath: string,
placement: WidgetPlacement,
Expand All @@ -43,6 +58,16 @@ function unlistenProvider(configHash: string): Promise<void> {
return invoke<void>('unlisten_provider', { configHash });
}

function callProviderFunction(
configHash: string,
fn: ProviderFunction,
): Promise<void> {
return invoke<void>('call_provider_function', {
configHash,
function: fn,
});
}

function setAlwaysOnTop(): Promise<void> {
return invoke<void>('set_always_on_top');
}
Expand Down
68 changes: 60 additions & 8 deletions packages/client-api/src/providers/media/create-media-provider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { z } from 'zod';
import { createBaseProvider } from '../create-base-provider';
import { onProviderEmit } from '~/desktop';
import { desktopCommands, onProviderEmit } from '~/desktop';
import type {
MediaControlArgs,
MediaOutput,
MediaProvider,
MediaProviderConfig,
Expand All @@ -17,12 +18,63 @@ export function createMediaProvider(
const mergedConfig = mediaProviderConfigSchema.parse(config);

return createBaseProvider(mergedConfig, async queue => {
return onProviderEmit<MediaOutput>(mergedConfig, ({ result }) => {
if ('error' in result) {
queue.error(result.error);
} else {
queue.output(result.output);
}
});
return onProviderEmit<MediaOutput>(
mergedConfig,
({ result, configHash }) => {
if ('error' in result) {
queue.error(result.error);
} else {
queue.output({
...result.output,
session: result.output.currentSession,
play: (args?: MediaControlArgs) => {
desktopCommands.callProviderFunction(configHash, {
type: 'media',
function: {
name: 'play',
args: args ?? {},
},
});
},
pause: (args?: MediaControlArgs) => {
desktopCommands.callProviderFunction(configHash, {
type: 'media',
function: {
name: 'pause',
args: args ?? {},
},
});
},
togglePlayPause: (args?: MediaControlArgs) => {
desktopCommands.callProviderFunction(configHash, {
type: 'media',
function: {
name: 'toggle_play_pause',
args: args ?? {},
},
});
},
next: (args?: MediaControlArgs) => {
desktopCommands.callProviderFunction(configHash, {
type: 'media',
function: {
name: 'next',
args: args ?? {},
},
});
},
previous: (args?: MediaControlArgs) => {
desktopCommands.callProviderFunction(configHash, {
type: 'media',
function: {
name: 'previous',
args: args ?? {},
},
});
},
});
}
},
);
});
}
16 changes: 15 additions & 1 deletion packages/client-api/src/providers/media/media-provider-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,24 @@ export interface MediaProviderConfig {
}

export interface MediaOutput {
/** @deprecated Use {@link currentSession} instead */
session: MediaSession | null;
currentSession: MediaSession | null;
allSessions: MediaSession[];
play(args?: MediaControlArgs): void;
pause(args?: MediaControlArgs): void;
togglePlayPause(args?: MediaControlArgs): void;
next(args?: MediaControlArgs): void;
previous(args?: MediaControlArgs): void;
}

export interface MediaControlArgs {
sessionId?: string;
}

export interface MediaSession {
title: string;
sessionId: string;
title: string | null;
artist: string | null;
albumTitle: string | null;
albumArtist: string | null;
Expand All @@ -18,6 +31,7 @@ export interface MediaSession {
endTime: number;
position: number;
isPlaying: boolean;
isCurrentSession: boolean;
}

export type MediaProvider = Provider<MediaProviderConfig, MediaOutput>;
3 changes: 2 additions & 1 deletion packages/desktop/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ komorebi-client = { git = "https://github.com/LGUG2Z/komorebi", tag = "v0.1.28"
windows-core = "0.58"
windows = { version = "0.58", features = [
"Foundation",
"Foundation_Collections",
"implement",
"Win32_Devices_FunctionDiscovery",
"Media_Control",
"Win32_Devices_FunctionDiscovery",
"Win32_Globalization",
"Win32_Media",
"Win32_Media_Audio",
Expand Down
Loading

0 comments on commit 1dfeacc

Please sign in to comment.