mirror of
https://github.com/Psychotoxical/psysonic.git
synced 2026-07-22 07:15:47 +00:00
1dd74f0fa1
Each Rust<->React bridge concern moves into its own hook under hooks/tauriBridge/; TauriEventBridge just composes them: - useZipDownloadBridge download:zip:progress - usePreviewBridge audio:preview-* lifecycle - useAudioDeviceBridge audio:device-changed / -reset - useCliBridge full cli:* listener surface - useTrayIconSync tray-icon visibility - useInAppKeybindings configurable keydown chords - useMediaAndWindowBridge media keys, tray, shortcuts, close/force-quit - usePlayerSnapshotPublisher psysonic --info JSON snapshot Pure code-move — event names, payloads, effect deps and cleanup all verbatim. The MainApp call site is unchanged.
46 lines
1.8 KiB
TypeScript
46 lines
1.8 KiB
TypeScript
import { useEffect } from 'react';
|
|
import { listen } from '@tauri-apps/api/event';
|
|
import { usePlayerStore } from '../../store/playerStore';
|
|
import { useAuthStore } from '../../store/authStore';
|
|
|
|
/** Audio output device lifecycle: device switches (Bluetooth headphones, USB
|
|
* DAC, …) and pinned-device-unplugged fallbacks emitted by the Rust
|
|
* device-watcher. */
|
|
export function useAudioDeviceBridge() {
|
|
// Audio output device changed (Bluetooth headphones, USB DAC, etc.)
|
|
// The Rust device-watcher has already reopened the stream on the new device
|
|
// and dropped the old Sink, so we just need to restart playback.
|
|
useEffect(() => {
|
|
let unlisten: (() => void) | undefined;
|
|
listen('audio:device-changed', () => {
|
|
const { currentTrack, isPlaying, playTrack, resetAudioPause } = usePlayerStore.getState();
|
|
if (!currentTrack) return;
|
|
if (isPlaying) {
|
|
playTrack(currentTrack);
|
|
} else {
|
|
// Paused: clear warm-pause flag so the next resume uses the cold path
|
|
// (audio_play + seek) which creates a new Sink on the new device.
|
|
resetAudioPause();
|
|
}
|
|
}).then(u => { unlisten = u; });
|
|
return () => { unlisten?.(); };
|
|
}, []);
|
|
|
|
// Pinned output device was unplugged — Rust already fell back to system default.
|
|
// Clear the stored device so the Settings dropdown resets to "System Default".
|
|
useEffect(() => {
|
|
let unlisten: (() => void) | undefined;
|
|
listen('audio:device-reset', () => {
|
|
useAuthStore.getState().setAudioOutputDevice(null);
|
|
const { currentTrack, currentTime, isPlaying, playTrack, resetAudioPause } = usePlayerStore.getState();
|
|
if (!currentTrack) return;
|
|
if (isPlaying) {
|
|
playTrack(currentTrack);
|
|
} else {
|
|
resetAudioPause();
|
|
}
|
|
}).then(u => { unlisten = u; });
|
|
return () => { unlisten?.(); };
|
|
}, []);
|
|
}
|