Kaleido-Desktop/src/player/miniWindow.ts
Elijah 624f01f745 Add a stop button to the player and resume after a server outage
The full player had no way to stop: Back only moved playback to the
miniplayer. A stop button now sits with the right-hand controls and
closes the player outright, as the miniplayer's close button does.

When the server went away mid-stream, mpv played out its buffer and then
took the cut-off stream for the end of the file: the title counted as
ended (opening Up Next for episodes), and Play restarted it from 0. A
stream ending more than 15 s before its duration is now treated as a
lost connection: the player shows "Lost connection to <server>", checks
/identity every few seconds, and reloads in place at the same position
(staying in the miniplayer) once the server answers; Play tries at once.
A second cut-off at the same spot counts as the real end of the file.

A file left paused at its end (keep-open) re-paused as soon as it was
unpaused, so the reload is unpaused again after loadfile.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 10:41:52 -07:00

99 lines
3.3 KiB
TypeScript

/**
* The miniplayer as a window of its own (Windows, src-tauri/src/mini.rs): the
* video moves into it, so it drags as one piece. This side keeps its page
* (src/mini/) up to date and carries out its buttons; macOS keeps the
* in-page miniplayer (PlayerOverlay).
*/
import { hostOS, invoke, isNative, listen } from '../platform/native';
import { episodeLabel } from '../app/format';
import { MINI_CORNER_KEY, type MiniAction, type MiniState } from '../mini/types';
import type { Item } from '../plex/types';
export const separateMini = isNative && hostOS === 'windows';
export async function showMiniWindow() {
await invoke('mini_show', { corner: localStorage.getItem(MINI_CORNER_KEY) ?? 'br' });
}
/** Moves the video back to the main window; harmless when already there. */
export async function hideMiniWindow() {
await invoke('mini_hide').catch(() => {});
}
interface PlayerView {
open: boolean;
mini: boolean;
item: Item | null;
paused: boolean;
ended: boolean;
position: number;
duration: number;
firstFrame: boolean;
buffering: boolean;
reconnecting: boolean;
error: string;
}
interface PlayerActions {
togglePause(): void;
seekBy(delta: number): void;
expand(): void;
close(): Promise<void>;
}
function view(st: PlayerView): MiniState {
const item = st.item;
return {
title: item ? (item.type === 'episode' ? item.grandparentTitle : item.title) : '',
subtitle: item?.type === 'episode' ? `${episodeLabel(item)} · ${item.title}` : '',
paused: st.paused,
ended: st.ended,
position: st.position,
duration: st.duration,
loading: !st.firstFrame && !st.error,
// Waiting for the server to come back shows as buffering.
buffering: st.buffering || st.reconnecting,
error: !!st.error,
};
}
/** Mirrors the player into the miniplayer window and listens for its buttons. */
export function bridgeMiniWindow(store: { getState(): PlayerView & PlayerActions; subscribe(listener: (st: PlayerView, prev: PlayerView) => void): () => void }) {
if (!separateMini) return;
let lastSent = 0;
let pending: ReturnType<typeof setTimeout> | null = null;
const send = async () => {
pending = null;
lastSent = Date.now();
const st = store.getState();
if (!st.open) return;
const { emitTo } = await import('@tauri-apps/api/event');
emitTo('mini', 'mini:state', view(st)).catch(() => {});
};
store.subscribe((st, prev) => {
if (!st.open || !st.mini) return;
// Everything but the position goes straight away; the position a few times a second.
const a = view(st);
const b = view(prev);
const only = (Object.keys(a) as (keyof MiniState)[]).every((k) => k === 'position' || a[k] === b[k]);
if (!only || st.mini !== prev.mini) {
if (pending) clearTimeout(pending);
send();
} else if (!pending) {
pending = setTimeout(send, Math.max(0, 250 - (Date.now() - lastSent)));
}
});
listen<{ type: MiniAction }>('mini:action', ({ type }) => {
const st = store.getState();
if (type === 'ready') return send();
if (!st.open) return;
if (type === 'toggle') st.togglePause();
else if (type === 'back') st.seekBy(-10);
else if (type === 'forward') st.seekBy(10);
else if (type === 'expand') st.expand();
else if (type === 'close') st.close();
});
}