Home

dev / openkara

publicthedavidweng/OpenKara· sync paused
Overview Code History Branches Pull requestsIssuesInsights
main
HomeOverview Code PRsIssues
dev/openkara/src/runtime/app-runtime.ts
Raw
1import { useEffect } from "react";2import { usePlayerStore } from "@/stores/player-store";3import { useLibraryStore } from "@/stores/library-store";4import { useBootstrapStore } from "@/stores/bootstrap-store";5import {6  DEFAULT_APP_SETTINGS,
7 useSettingsStore,
8} from "@/stores/settings-store";
9import {
10 useEventListeners,
11 useLyricsAutoFetch,
12} from "@/hooks/use-playback-runtime";
13import { useCdgSync } from "@/hooks/use-cdg-sync";
14import { useKeyboardShortcuts } from "@/hooks/use-keyboard-shortcuts";
15import { useFileDrop } from "@/hooks/use-file-drop";
16import { notifyError } from "@/lib/errors";
17import * as api from "@/lib/tauri";
18import i18next, { detectSystemLanguage } from "@/lib/i18n";
19import {
20 useAirPlayAudienceSync,
21 useLocalAudienceOutputState,
22 useAirPlayOutputState,
23} from "@/runtime/airplay-runtime";
24import { useLocalAudienceRomanizeRuntime } from "@/runtime/local-audience-romanize-runtime";
25import { useAppMenuRuntime } from "./menu-runtime";
26import { loadStartupSettings } from "./settings-runtime";
27 
28type AnimationFrameScheduler = (callback: FrameRequestCallback) => number;
29 
30export function useAppStartupRuntime(
31 libraryReady: boolean | null,
32 setLibraryReady: (ready: boolean) => void,
33) {
34 const loadLibrary = useLibraryStore((s) => s.loadLibrary);
35 const loadBootstrapStatus = useBootstrapStore((s) => s.loadStatus);
36 const loadPlayerState = usePlayerStore((s) => s.loadState);
37 const hydrateAppSettings = useSettingsStore((s) => s.hydrateAppSettings);
38 const patchAppSettings = useSettingsStore((s) => s.patchAppSettings);
39 
40 useEffect(() => {
41 void loadStartupSettings({
42 getSettings: api.getSettings,
43 hydrateAppSettings,
44 changeLanguage: i18next.changeLanguage,
45 detectFallbackLanguage: detectSystemLanguage,
46 }).catch((error) => {
47 if (!useSettingsStore.getState().hydrated) {
48 patchAppSettings({ ...DEFAULT_APP_SETTINGS, hydrated: true });
49 }
50 notifyError(error);
51 
52 // Fallback system-language setup is independent of settings hydration so
53 // a language failure cannot clear the selected theme.
54 void i18next.changeLanguage(detectSystemLanguage()).catch(() => {
55 // Language failure after settings fallback is non-fatal; the default
56 // language remains active and the window can still be shown.
57 });
58 });
59 }, [hydrateAppSettings, patchAppSettings]);
60 
61 useEffect(() => {
62 api
63 .getLibraryRegistry()
64 .then((registry) => setLibraryReady(registry.active_library_id !== null))
65 .catch((error) => {
66 notifyError(error);
67 setLibraryReady(false);
68 });
69 }, [setLibraryReady]);
70 
71 useEffect(() => {
72 if (!libraryReady) {
73 return;
74 }
75 
76 void loadLibrary();
77 void loadBootstrapStatus();
78 void loadPlayerState();
79 }, [libraryReady, loadBootstrapStatus, loadLibrary, loadPlayerState]);
80}
81 
82/**
83 * Backstop for the reveal request. The main window starts hidden, and a hidden
84 * (occluded) WebView has its animation frames suspended — so the rAF callback
85 * that asks the backend to show the window would never run, leaving the app
86 * running with no window at all. The timer guarantees the request goes out;
87 * rAF still wins whenever frames are actually being produced.
88 */
89const WINDOW_REVEAL_FALLBACK_MS = 120;
90 
91export function useAppReadyRuntime(
92 libraryReady: boolean | null,
93 settingsHydrated: boolean,
94 startupThemeReady: boolean,
95 windowShown: boolean,
96 setWindowShown: (shown: boolean) => void,
97 scheduleFrame: AnimationFrameScheduler = requestAnimationFrame,
98) {
99 useEffect(() => {
100 if (
101 libraryReady === null ||
102 !settingsHydrated ||
103 !startupThemeReady ||
104 windowShown
105 ) {
106 return;
107 }
108 
109 let requested = false;
110 const requestReveal = () => {
111 if (requested) {
112 return;
113 }
114 requested = true;
115 void api.windowReady();
116 setWindowShown(true);
117 };
118 
119 const frameId = scheduleFrame(requestReveal);
120 const timeoutId = setTimeout(requestReveal, WINDOW_REVEAL_FALLBACK_MS);
121 
122 return () => {
123 cancelAnimationFrame(frameId);
124 clearTimeout(timeoutId);
125 };
126 }, [
127 libraryReady,
128 scheduleFrame,
129 setWindowShown,
130 settingsHydrated,
131 startupThemeReady,
132 windowShown,
133 ]);
134}
135 
136/**
137 * Single product runtime graph (library-ready gated). There is no second
138 * webview/sidebar runtime path.
139 */
140export function useAppRuntime(enabled: boolean) {
141 useEventListeners(enabled);
142 useLyricsAutoFetch(enabled);
143 useCdgSync(enabled);
144 useAirPlayAudienceSync(enabled);
145 useLocalAudienceOutputState(enabled);
146 useAirPlayOutputState(enabled);
147 useLocalAudienceRomanizeRuntime(enabled);
148 useKeyboardShortcuts(enabled);
149 useFileDrop(enabled);
150 useAppMenuRuntime(enabled);
151}
152