Skip to content

Commit

Permalink
feat: add audio provider (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
HolbyFPV authored Nov 20, 2024
1 parent e59644b commit a1e18f9
Show file tree
Hide file tree
Showing 13 changed files with 529 additions and 16 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ In some cases, updating to the latest Microsoft Webview2 version is needed ([sta

Through the `zebar` NPM package, Zebar exposes various system information via reactive "providers". Providers are a collection of functions and variables that can change over time.

- [audio](#Audio)
- [battery](#Battery)
- [cpu](#CPU)
- [date](#Date)
Expand All @@ -72,6 +73,30 @@ Through the `zebar` NPM package, Zebar exposes various system information via re
- [network](#Network)
- [weather](#Weather)

### Audio

#### Config

No config options.

#### Outputs

| Variable | Description | Return type | Supported OS |
| ------------------- | ----------- | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `defaultPlaybackDevice` | Default audio playback device. | `AudioDevice \| null` | <img src="https://github.com/glzr-io/zebar/assets/34844898/568e90c8-cd32-49a5-a17f-ab233d41f1aa" alt="microsoft icon" width="24"> |
| `playbackDevices` | All audio playback devices. | `AudioDevice[]` | <img src="https://github.com/glzr-io/zebar/assets/34844898/568e90c8-cd32-49a5-a17f-ab233d41f1aa" alt="microsoft icon" width="24"> |

#### Return types

#### `AudioDevice`

| Variable | Description | Return type |
| ------------------ | ----------------------------- | ----------------------- |
| `deviceId` | Device ID. | `string` |
| `name` | Friendly display name of device. | `string` |
| `volume` | Volume as a % of maximum volume. Returned value is between `0` and `100`. | `number` |
| `isDefault` | `true` if the device is selected as the default playback device.| `boolean` |

### Battery

#### Config
Expand Down
5 changes: 5 additions & 0 deletions examples/boilerplate-solid-ts/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createStore } from 'solid-js/store';
import * as zebar from 'zebar';

const providers = zebar.createProviderGroup({
audio: { type: 'audio' },
cpu: { type: 'cpu' },
battery: { type: 'battery' },
memory: { type: 'memory' },
Expand All @@ -21,6 +22,10 @@ function App() {

return (
<div class="app">
<div class="chip">
{output.audio?.defaultPlaybackDevice?.name} -
{output.audio?.defaultPlaybackDevice?.volume}
</div>
<div class="chip">
Media: {output.media?.session?.title} -
{output.media?.session?.artist}
Expand Down
19 changes: 19 additions & 0 deletions packages/client-api/src/providers/audio/audio-provider-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { Provider } from '../create-base-provider';

export interface AudioProviderConfig {
type: 'audio';
}

export type AudioProvider = Provider<AudioProviderConfig, AudioOutput>;

export interface AudioOutput {
defaultPlaybackDevice: AudioDevice;
playbackDevices: AudioDevice[];
}

export interface AudioDevice {
deviceId: string;
name: string;
volume: number;
isDefault: boolean;
}
29 changes: 29 additions & 0 deletions packages/client-api/src/providers/audio/create-audio-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { z } from 'zod';

import { createBaseProvider } from '../create-base-provider';
import { onProviderEmit } from '~/desktop';
import type {
AudioOutput,
AudioProvider,
AudioProviderConfig,
} from './audio-provider-types';

const audioProviderConfigSchema = z.object({
type: z.literal('audio'),
});

export function createAudioProvider(
config: AudioProviderConfig,
): AudioProvider {
const mergedConfig = audioProviderConfigSchema.parse(config);

return createBaseProvider(mergedConfig, async queue => {
return onProviderEmit<AudioOutput>(mergedConfig, ({ result }) => {
if ('error' in result) {
queue.error(result.error);
} else {
queue.output(result.output);
}
});
});
}
9 changes: 9 additions & 0 deletions packages/client-api/src/providers/create-provider.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { createAudioProvider } from './audio/create-audio-provider';
import type {
AudioProviderConfig,
AudioProvider,
} from './audio/audio-provider-types';
import { createBatteryProvider } from './battery/create-battery-provider';
import type {
BatteryProviderConfig,
Expand Down Expand Up @@ -62,6 +67,7 @@ import type {
} from './disk/disk-provider-types';

export interface ProviderConfigMap {
audio: AudioProviderConfig;
battery: BatteryProviderConfig;
cpu: CpuProviderConfig;
date: DateProviderConfig;
Expand All @@ -78,6 +84,7 @@ export interface ProviderConfigMap {
}

export interface ProviderMap {
audio: AudioProvider;
battery: BatteryProvider;
cpu: CpuProvider;
date: DateProvider;
Expand Down Expand Up @@ -114,6 +121,8 @@ export function createProvider<T extends ProviderConfig>(
config: T,
): ProviderMap[T['type']] {
switch (config.type) {
case 'audio':
return createAudioProvider(config) as any;
case 'battery':
return createBatteryProvider(config) as any;
case 'cpu':
Expand Down
9 changes: 8 additions & 1 deletion packages/desktop/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,22 @@ regex = "1"

[target.'cfg(target_os = "windows")'.dependencies]
komorebi-client = { git = "https://github.com/LGUG2Z/komorebi", tag = "v0.1.28" }
windows-core = "0.58"
windows = { version = "0.58", features = [
"Foundation",
"implement",
"Win32_Devices_FunctionDiscovery",
"Media_Control",
"Win32_Globalization",
"Win32_Media",
"Win32_Media_Audio",
"Win32_Media_Audio_Endpoints",
"Win32_System_Console",
"Win32_System_SystemServices",
"Win32_UI_WindowsAndMessaging",
"Win32_UI_Input_KeyboardAndMouse",
"Win32_UI_Shell_PropertiesSystem",
"Win32_UI_TextServices",
"Win32_UI_WindowsAndMessaging",
"Win32_NetworkManagement_WiFi",
] }

Expand Down
Loading

0 comments on commit a1e18f9

Please sign in to comment.