Release Kaleido Desktop 0.1.0-beta.17
All checks were successful
Validate Kaleido Desktop / validate (push) Successful in 1m36s

This commit is contained in:
Elijah 2026-11-12 11:45:07 -07:00
parent dfe865d8f4
commit 28e633dad1
13 changed files with 165 additions and 156 deletions

View file

@ -70,6 +70,7 @@ interface PlayerState {
nextUp: Item | null;
/** The episode before this one, for the previous-episode button. */
prevUp: Item | null;
episodeContextReady: boolean;
countdown: number | null;
countdownTotal: number;
postPlay: boolean;
@ -115,13 +116,12 @@ let lastReport = 0;
let sessionAnswered = false;
let reportTimer: ReturnType<typeof setInterval> | null = null;
let countdownTimer: ReturnType<typeof setInterval> | null = null;
let unwatchCountdown: (() => void) | null = null;
let scrobbled = false;
let unlisten: (() => void) | null = null;
let loadToken = 0;
let wasFullscreenBeforePlay = false;
let pendingExternal: Track | null = null;
let neighboursReady = false;
let postPlayRequested = false;
/**
* The server this playback belongs to, fixed when it starts. Progress, watched state
@ -338,27 +338,33 @@ async function findNeighbours(item: Item): Promise<{ prevUp: Item | null; nextUp
}
}
function startCountdown() {
function openPostPlay() {
const st = usePlayer.getState();
if (!st.open || st.error || st.item?.type !== 'episode' || st.postPlay || st.postPlayDismissed) return;
usePlayer.setState({ postPlay: true });
startCountdown();
}
function startCountdown() {
const st = usePlayer.getState();
if (!st.open || !st.postPlay || st.error || !st.nextUp || st.postPlayDismissed || st.countdown !== null) return;
const settings = useSettings.getState();
// Don't mistake a slow context request for the end of the series.
if (!neighboursReady) return;
postPlayRequested = false;
const total = st.nextUp && settings.autoplayNext ? settings.countdownSeconds : 0;
usePlayer.setState({ postPlay: true, countdown: total > 0 ? total : null, countdownTotal: total });
const total = settings.autoplayNext ? settings.countdownSeconds : 0;
if (total <= 0) return;
stopCountdown();
usePlayer.setState({ countdown: total, countdownTotal: total });
let lastTick = performance.now();
countdownTimer && clearInterval(countdownTimer);
const visibilityChanged = () => { lastTick = performance.now(); };
document.addEventListener('visibilitychange', visibilityChanged);
unwatchCountdown = () => document.removeEventListener('visibilitychange', visibilityChanged);
countdownTimer = setInterval(() => {
const now = performance.now();
// Never spend time hidden, in the separate miniplayer, or behind another app.
// Cap a delayed tick so sleep / a suspended web view cannot skip an episode.
const elapsed = Math.min((now - lastTick) / 1000, 0.1);
const elapsed = Math.max(0, (now - lastTick) / 1000);
lastTick = now;
const current = usePlayer.getState();
if (!current.open || !current.postPlay || current.error || current.countdown === null) return stopCountdown();
if (document.hidden || !document.hasFocus() || current.mini || (current.paused && !current.ended)) return;
// Native video can own keyboard focus while the webview is still on screen.
if (document.hidden || current.mini) return;
const remaining = Math.max(0, current.countdown - elapsed);
usePlayer.setState({ countdown: remaining });
if (remaining === 0) {
@ -371,6 +377,8 @@ function startCountdown() {
function stopCountdown() {
if (countdownTimer) clearInterval(countdownTimer);
countdownTimer = null;
unwatchCountdown?.();
unwatchCountdown = null;
}
/**
@ -639,9 +647,8 @@ function fallbackAfterError() {
function maybeCredits(pos: number) {
const st = usePlayer.getState();
if (st.item?.type !== 'episode' || !st.firstFrame || st.paused || st.postPlayDismissed || Date.now() - st.lastSeek < 1500) return;
const credits = st.item.markers.find((m) => m.type === 'credits');
if (credits && pos * 1000 >= credits.start && pos * 1000 < credits.end && useSettings.getState().skipCredits === 'auto') startCountdown();
if (st.item?.type !== 'episode' || !st.firstFrame || st.postPlayDismissed) return;
if (st.item.markers.some((m) => m.type === 'credits' && pos * 1000 >= m.start && pos * 1000 < m.end)) openPostPlay();
}
function onEnded() {
@ -650,7 +657,7 @@ function onEnded() {
usePlayer.setState({ ended: true });
report('stopped');
checkScrobble();
if (st.item?.type === 'episode') startCountdown();
if (st.item?.type === 'episode') openPostPlay();
}
export const usePlayer = create<PlayerState>((set, get) => ({
@ -682,6 +689,7 @@ export const usePlayer = create<PlayerState>((set, get) => ({
ended: false,
nextUp: null,
prevUp: null,
episodeContextReady: false,
countdown: null,
countdownTotal: 0,
postPlay: false,
@ -697,8 +705,6 @@ export const usePlayer = create<PlayerState>((set, get) => ({
// Closing or starting something else while this one is still setting up wins.
const stale = () => token !== loadToken;
const prefs = useSettings.getState();
neighboursReady = false;
postPlayRequested = false;
stopThemeMusic();
stopCountdown();
if (!isNative) {
@ -743,6 +749,7 @@ export const usePlayer = create<PlayerState>((set, get) => ({
ended: false,
nextUp: null,
prevUp: null,
episodeContextReady: false,
countdown: null,
countdownTotal: 0,
postPlay: false,
@ -818,6 +825,15 @@ export const usePlayer = create<PlayerState>((set, get) => ({
...(tracks ?? {}),
});
findNeighbours(item).then((n) => {
if (token !== loadToken) return;
set({ ...n, episodeContextReady: true });
const current = get();
if (current.postPlay) startCountdown();
else if (current.ended) openPostPlay();
else maybeCredits(current.position);
});
await mpv.init({ hwdec: prefs.hwdec, hdr: prefs.hdrPassthrough });
if (!unlisten) unlisten = await mpv.on(onEvent);
if (stale()) return;
@ -850,18 +866,6 @@ export const usePlayer = create<PlayerState>((set, get) => ({
const st = get();
if (st.open && !st.ended) report(st.paused ? 'paused' : st.buffering ? 'buffering' : 'playing');
}, 10000);
findNeighbours(item).then((n) => {
if (token !== loadToken) return;
neighboursReady = true;
set(n);
const current = get();
// Metadata may arrive after EOF / the credits. Open only once it is known
// whether there is a next episode, without restarting a cancelled timer.
if (!current.postPlay) {
if (postPlayRequested || current.ended) startCountdown();
else maybeCredits(current.position);
}
});
} catch (e: any) {
if (token !== loadToken) return;
set({ error: e?.message ?? String(e), buffering: false });
@ -911,8 +915,7 @@ export const usePlayer = create<PlayerState>((set, get) => ({
seek(seconds) {
const st = get();
const target = Math.max(0, Math.min(seconds, (st.duration || seconds) - 0.5));
const choseCredits = st.item?.type === 'episode' && st.item.markers.some((m) => m.type === 'credits' && target * 1000 >= m.start && target * 1000 < m.end);
set({ position: target, ended: false, lastSeek: Date.now(), ...(choseCredits ? { postPlayDismissed: true } : {}) });
set({ position: target, ended: false, lastSeek: Date.now() });
if (st.postPlay) st.dismissPostPlay();
mpv.command('seek', target.toFixed(3), 'absolute+exact').catch(() => {});
},
@ -1024,9 +1027,8 @@ export const usePlayer = create<PlayerState>((set, get) => ({
showPostPlay() {
// A deliberate Skip credits can reopen a screen previously dismissed.
postPlayRequested = true;
set({ postPlayDismissed: false });
startCountdown();
openPostPlay();
},
dismissPostPlay() {