fix(reader): close stuck reader window on book load failure, closes #3932 (#3980)

When a book's underlying file is missing, opening it in a dedicated
reader window showed an error toast then navigated that window to
/library, leaving a leftover library-in-reader-window the user had
to close manually. Route the recovery through a new
closeReaderWindowOrGoToLibrary() that closes the dedicated reader
window (after ensuring the main library window is visible) and only
falls back to /library navigation in the main window or on web.

Also fix a related macOS issue: the reader's CloseRequested handler
was running handleCloseBooks and calling currentWindow.destroy() on
the main window, which tore down the active book and bypassed the
Rust close-to-hide handler — making Cmd+W / traffic-light close
quit the app from the reader page (vs. correctly hiding from the
library page) and lose the active book even when the window did
hide. Skip both the cleanup and destroy on macOS for the main
window so the Rust handler hides it with the book intact, matching
the macOS minimize-to-dock convention.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Huang Xin
2026-04-28 00:32:04 +08:00
committed by GitHub
parent 94ede10f6e
commit 6fcda66b60
5 changed files with 238 additions and 5 deletions
+22 -1
View File
@@ -1,7 +1,7 @@
import { redirect, useRouter } from 'next/navigation';
import { getCurrentWindow, ScrollBarStyle } from '@tauri-apps/api/window';
import { WebviewWindow } from '@tauri-apps/api/webviewWindow';
import { isPWA, isWebAppPlatform } from '@/services/environment';
import { isPWA, isTauriAppPlatform, isWebAppPlatform } from '@/services/environment';
import { BOOK_IDS_SEPARATOR } from '@/services/constants';
import { AppService } from '@/types/system';
@@ -128,6 +128,27 @@ export const navigateToLibrary = (
router.replace(`/library${queryParams ? `?${queryParams}` : ''}`, navOptions);
};
// Recovery action when a reader has nothing to display — e.g. all books were
// closed, or a book failed to load in a freshly-opened reader window.
// In a dedicated reader window we close the window itself, ensuring the main
// library window is visible first; routing the reader window to /library
// instead would leave a leftover window the user has to close manually.
// In the main window or on web, fall back to /library navigation.
export const closeReaderWindowOrGoToLibrary = async (
appService: AppService | null,
router: ReturnType<typeof useRouter>,
) => {
if (isTauriAppPlatform() && appService?.hasWindow) {
const currentWindow = getCurrentWindow();
if (currentWindow.label !== 'main') {
await ensureMainLibraryWindow(appService);
await currentWindow.close();
return;
}
}
navigateToLibrary(router, '', undefined, true);
};
export const redirectToLibrary = () => {
redirect('/library');
};
+7
View File
@@ -51,6 +51,13 @@ export const tauriHandleOnCloseWindow = async (callback: () => void) => {
const currentWindow = getCurrentWindow();
return await currentWindow.onCloseRequested(async (event) => {
event.preventDefault();
// On macOS, the main window's close is intercepted by the Rust backend
// to hide the window (close-to-hide), keeping the app in the dock. Skip
// the in-app cleanup — the user is just minimizing the window and
// expects the active book to still be there when the window reopens.
if (currentWindow.label === 'main' && (await osType()) === 'macos') {
return;
}
await callback();
if (currentWindow.label.startsWith('reader')) {
await emitTo('main', 'close-reader-window', { label: currentWindow.label });