/** * [cinny-desktop #25] In-game voice overlay: the call snapshot the desktop app * draws in its always-on-top overlay window. Pure, so it can be tested without * a call; `useTauriOverlay` feeds it live state and sends the result to the * native `set_overlay_state` command. */ export type OverlayCorner = 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'; export const OVERLAY_CORNERS: readonly OverlayCorner[] = [ 'top-left', 'top-right', 'bottom-left', 'bottom-right', ]; export type OverlayPerson = { userId: string; name: string; speaking: boolean; muted: boolean; isSelf: boolean; }; /** Mirrors `OverlayState` in cinny-desktop `src-tauri/src/native/overlay.rs`. */ export type OverlaySnapshot = { active: boolean; corner: OverlayCorner; muted: boolean; deafened: boolean; ptt: boolean; people: OverlayPerson[]; }; /** The subset of an `io.lotus.call_state` participant the overlay needs. */ export type OverlayParticipant = { userId: string; speaking: boolean; audioEnabled: boolean; }; export type OverlayInput = { /** Settings → Calls → In-game overlay. */ enabled: boolean; /** In a joined call. */ joined: boolean; corner: OverlayCorner; myUserId: string | null; /** Per device, in the fork's order (stable while people stay). */ participants: readonly OverlayParticipant[]; nameOf: (userId: string) => string; /** Own mic is off (`microphone === false`). */ muted: boolean; /** Own call audio is off (`sound === false`). */ deafened: boolean; pttMode: boolean; }; export const inactiveOverlay = (corner: OverlayCorner): OverlaySnapshot => ({ active: false, corner, muted: false, deafened: false, ptt: false, people: [], }); /** * Build the snapshot: everyone in the call (you first, then everyone else in * call order), one row per person even with several devices (speaking if any * device is, muted only if every device is), and your own mute / deafen / PTT * state for the header. * * In push-to-talk mode the mic is "off" whenever the key isn't held, so you * are not shown as Muted then: the header says PTT, and your row lights up * while you transmit. */ export function buildOverlaySnapshot(input: OverlayInput): OverlaySnapshot { const { corner } = input; if (!input.enabled || !input.joined) return inactiveOverlay(corner); const byUser = new Map(); input.participants.forEach((p) => { if (!p.userId.startsWith('@')) return; const prev = byUser.get(p.userId); byUser.set(p.userId, { speaking: (prev?.speaking ?? false) || p.speaking, muted: (prev?.muted ?? true) && !p.audioEnabled, }); }); const selfMuted = input.pttMode ? false : input.muted; const people: OverlayPerson[] = []; const me = input.myUserId; if (me) { const mine = byUser.get(me); people.push({ userId: me, name: input.nameOf(me), speaking: (mine?.speaking ?? false) && !selfMuted && !input.deafened, muted: selfMuted || input.deafened, isSelf: true, }); } byUser.forEach((state, userId) => { if (userId === me) return; people.push({ userId, name: input.nameOf(userId), ...state, isSelf: false }); }); return { active: true, corner, muted: selfMuted, deafened: input.deafened, ptt: input.pttMode, people, }; } /** Key for skipping sends when nothing visible changed. */ export const overlaySnapshotKey = (s: OverlaySnapshot): string => JSON.stringify(s);