-
-
Notifications
You must be signed in to change notification settings - Fork 379
Expand file tree
/
Copy pathManualDeviceSelector.svelte
More file actions
204 lines (191 loc) · 5.96 KB
/
Copy pathManualDeviceSelector.svelte
File metadata and controls
204 lines (191 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<script lang="ts">
import { Button } from '@epicenter/ui/button';
import * as Command from '@epicenter/ui/command';
import * as Popover from '@epicenter/ui/popover';
import { useCombobox } from '@epicenter/ui/hooks';
import { rpc } from '$lib/query';
import { settings } from '$lib/stores/settings.svelte';
import { cn } from '@epicenter/ui/utils';
import { createQuery } from '@tanstack/svelte-query';
import CheckIcon from '@lucide/svelte/icons/check';
import MicIcon from '@lucide/svelte/icons/mic';
import RefreshCwIcon from '@lucide/svelte/icons/refresh-cw';
import { Spinner } from '@epicenter/ui/spinner';
import { Badge } from '@epicenter/ui/badge';
import { createDeviceChangeListener } from '$lib/services/isomorphic/device-change.svelte';
const combobox = useCombobox();
const selectedMethod = $derived(settings.value['recording.method']);
// Get the device ID for the current method
const selectedDeviceId = $derived(
settings.value[`recording.${selectedMethod}.deviceId`],
);
const isDeviceSelected = $derived(!!selectedDeviceId);
// Recording method options with descriptions
const RECORDING_METHODS = {
cpal: {
label: 'CPAL',
description: 'Native audio recording with low latency',
badge: 'Recommended',
isAvailable: window.__TAURI_INTERNALS__, // Desktop only
},
ffmpeg: {
label: 'FFmpeg',
description: 'Customizable command-line recording',
badge: 'Advanced',
isAvailable: window.__TAURI_INTERNALS__, // Desktop only
},
navigator: {
label: 'Navigator',
description: 'Browser MediaRecorder API',
badge: 'Universal',
isAvailable: true, // Always available
},
} as const;
const getDevicesQuery = createQuery(() => ({
...rpc.recorder.enumerateDevices.options,
enabled: combobox.open,
}));
const deviceChangeListener = createDeviceChangeListener();
// Auto-refresh device list when devices change (web only - desktop uses polling)
// Note: TanStack Query already fetches when enabled becomes true, so no manual refetch needed
$effect(() => {
if (combobox.open) {
deviceChangeListener.subscribe();
}
return () => {
deviceChangeListener.unsubscribe();
};
});
$effect(() => {
if (getDevicesQuery.isError) {
rpc.notify.warning(getDevicesQuery.error);
}
});
</script>
<Popover.Root bind:open={combobox.open}>
<Popover.Trigger bind:ref={combobox.triggerRef}>
{#snippet child({ props })}
<Button
{...props}
tooltip={isDeviceSelected
? `Recording via ${RECORDING_METHODS[selectedMethod].label} - Change device or method`
: `Select recording device (${RECORDING_METHODS[selectedMethod].label} method)`}
role="combobox"
aria-expanded={combobox.open}
variant="ghost"
size="icon"
>
{#if isDeviceSelected}
<MicIcon class="size-4 text-green-500" />
{:else}
<MicIcon class="size-4 text-warning" />
{/if}
</Button>
{/snippet}
</Popover.Trigger>
<Popover.Content class="p-0">
<Command.Root loop>
<Command.Input placeholder="Search devices and methods..." />
<Command.List class="max-h-[40vh]">
<Command.Empty>No recording devices found.</Command.Empty>
<!-- Recording Method Selection -->
<Command.Group heading="Recording Method">
{#each Object.entries(RECORDING_METHODS) as [methodKey, method]}
{@const isSelected = selectedMethod === methodKey}
{#if method.isAvailable}
<Command.Item
value={`method-${methodKey} ${method.label} ${method.description}`}
onSelect={() => {
settings.updateKey(
'recording.method',
methodKey as keyof typeof RECORDING_METHODS,
);
getDevicesQuery.refetch();
}}
class="flex items-center gap-3 px-3 py-2"
>
<CheckIcon
class={cn(
'size-4 shrink-0',
isSelected ? 'opacity-100' : 'opacity-0',
)}
/>
<div class="flex-1 min-w-0">
<div class="flex items-center gap-2">
<span class="font-medium text-sm">{method.label}</span>
<Badge
variant={isSelected ? 'default' : 'secondary'}
class="text-xs"
>
{method.badge}
</Badge>
</div>
<p class="text-xs text-muted-foreground mt-1">
{method.description}
</p>
</div>
</Command.Item>
{/if}
{/each}
</Command.Group>
<Command.Separator />
<!-- Device Selection -->
<Command.Group heading="Recording Device">
{#if getDevicesQuery.isPending}
<div class="p-4 text-center text-sm text-muted-foreground">
Loading devices...
</div>
{:else if getDevicesQuery.isError}
<div class="p-4 text-center text-sm text-destructive">
{getDevicesQuery.error.title}
</div>
{:else}
{#each getDevicesQuery.data as device (device.id)}
<Command.Item
value={`device-${device.id} ${device.label}`}
onSelect={() => {
const currentDeviceId = selectedDeviceId;
settings.updateKey(
`recording.${selectedMethod}.deviceId`,
currentDeviceId === device.id ? null : device.id,
);
}}
class="flex items-center gap-3 px-3 py-2"
>
<CheckIcon
class={cn(
'size-4 shrink-0',
selectedDeviceId === device.id
? 'opacity-100'
: 'opacity-0',
)}
/>
<span class="flex-1 text-sm">
{device.label}
{#if device.id === 'default'}
<Badge variant="secondary" class="ml-2 text-xs">Auto</Badge>
{/if}
</span>
</Command.Item>
{/each}
{/if}
</Command.Group>
<Command.Separator />
<Command.Group>
<Command.Item
onSelect={() => {
getDevicesQuery.refetch();
}}
>
{#if getDevicesQuery.isRefetching}
<Spinner />
{:else}
<RefreshCwIcon class="size-4" />
{/if}
Refresh devices
</Command.Item>
</Command.Group>
</Command.List>
</Command.Root>
</Popover.Content>
</Popover.Root>