Files
cinny/src/app/hooks/useTauriOverlay.ts
T
jaredandClaude Opus 5.5 01254291e5
CI / Build & Quality Checks (push) Skipped
CI / Secret scan (gitleaks) (push) Skipped
CI / Playwright smoke (e2e) (push) Skipped
CI / Secret scan (gitleaks) (pull_request) Successful in 23s
CI / Build & Quality Checks (pull_request) Successful in 5m9s
CI / Trigger Desktop Build (pull_request) Skipped
CI / Docker image build & smoke test (pull_request) Skipped
CI / Playwright smoke (e2e) (pull_request) Successful in 23m21s
feat(calls): in-game voice overlay for the desktop app (cinny-desktop #25)
Settings → Calls → "In-game overlay (beta)", desktop only, off by
default, device-local. While a call is joined it sends the call
snapshot to the desktop's new `set_overlay_state` command, which shows
a small always-on-top, click-through list while Lotus isn't focused.

- utils/callOverlay: pure snapshot builder. Everyone in the call, you
  first, one row per person across devices (speaking if any device is,
  muted only if all are), your own mute/deafen/PTT. In PTT mode you're
  not shown as Muted while the key is up.
- useTauriOverlay: live io.lotus.call_state participants plus control
  state, coalesced to at most one send per 100 ms (latest always
  lands), unchanged snapshots skipped, cleared on unmount.
- Overlay position setting (four corners).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PPmy3tPq869XDW4njjVaKA
2026-11-09 17:25:36 -05:00

102 lines
3.8 KiB
TypeScript

import { useEffect, useMemo, useRef, useState } from 'react';
import { useAtomValue } from 'jotai';
import { callEmbedAtom } from '../state/callEmbed';
import { useCallControlState } from '../plugins/call';
import type { LotusCallParticipant } from '../plugins/call/CallEmbed';
import { useSetting } from '../state/hooks/settings';
import { settingsAtom } from '../state/settings';
import { useMatrixClient } from './useMatrixClient';
import { useCallJoined } from './useCallEmbed';
import { invokeTauri, isTauri } from './useTauri';
import { getMemberName } from '../utils/room';
import {
buildOverlaySnapshot,
inactiveOverlay,
OverlaySnapshot,
overlaySnapshotKey,
} from '../utils/callOverlay';
/** At most this often (ms) while speaking flips; the last state always lands. */
const SEND_INTERVAL_MS = 100;
/**
* [cinny-desktop #25] Feeds the desktop app's in-game voice overlay: while a
* call is joined and Settings → Calls → In-game overlay is on, sends who's in
* the call, who's speaking and your own mute / deafen / PTT state to the native
* `set_overlay_state` command. The desktop decides when to show it (Lotus not
* focused). No-op in the browser.
*/
export function useTauriOverlay(): void {
const mx = useMatrixClient();
const callEmbed = useAtomValue(callEmbedAtom);
const joined = useCallJoined(callEmbed);
const { microphone, sound } = useCallControlState(callEmbed?.control);
const [enabled] = useSetting(settingsAtom, 'callOverlay');
const [corner] = useSetting(settingsAtom, 'callOverlayCorner');
const [pttMode] = useSetting(settingsAtom, 'pttMode');
// Live per-participant state from the fork's io.lotus.call_state, only
// tracked while the overlay could show.
const [participants, setParticipants] = useState<readonly LotusCallParticipant[]>([]);
const tracking = isTauri() && enabled && joined && callEmbed !== undefined;
useEffect(() => {
if (!tracking || !callEmbed) {
setParticipants([]);
return undefined;
}
const sync = (): void => setParticipants(callEmbed.getLotusParticipants() ?? []);
sync();
return callEmbed.onLotusCallState(sync);
}, [tracking, callEmbed]);
const snapshot: OverlaySnapshot = useMemo(() => {
if (!tracking || !callEmbed) return inactiveOverlay(corner);
const { room } = callEmbed;
return buildOverlaySnapshot({
enabled,
joined,
corner,
myUserId: mx.getUserId(),
participants,
nameOf: (userId) => getMemberName(room, userId),
muted: !microphone,
deafened: !sound,
pttMode,
});
}, [tracking, callEmbed, enabled, joined, corner, mx, participants, microphone, sound, pttMode]);
// Coalesced send: skip unchanged snapshots, send at most every
// SEND_INTERVAL_MS, and always deliver the latest one.
const key = overlaySnapshotKey(snapshot);
const latest = useRef(snapshot);
latest.current = snapshot;
const sentKey = useRef<string | null>(null);
const lastSentAt = useRef(0);
const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
useEffect(() => {
if (!isTauri() || key === sentKey.current) return;
const send = (): void => {
timer.current = undefined;
const s = latest.current;
sentKey.current = overlaySnapshotKey(s);
lastSentAt.current = Date.now();
invokeTauri('set_overlay_state', { state: s });
};
if (timer.current !== undefined) return;
const wait = SEND_INTERVAL_MS - (Date.now() - lastSentAt.current);
if (wait <= 0) send();
else timer.current = setTimeout(send, wait);
}, [key]);
// Hide the overlay when this unmounts (logout, client teardown).
useEffect(
() => () => {
if (timer.current !== undefined) clearTimeout(timer.current);
if (isTauri()) {
invokeTauri('set_overlay_state', { state: inactiveOverlay(latest.current.corner) });
}
},
[],
);
}