mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-11-16 01:21:16 +01:00
# Description of Changes Locking to just having one instance of the app running unifies the experience across all OSs. Opening new files in Stirling will cause the files to be opened in the existing window rather than spawning a new instance of the app with just that file in the new instance. There's much more to explore here to allow multiple windows open at once, but that can be done all from one instance of the app, and will likely make it easier to allow movement of files etc. across different windows. Also fixes extra newlines in the logs and directly builds to `.app` on Mac because it's frustrating during development to have to repeatedly mount & unmount the `.dmg`.
95 lines
3.4 KiB
TypeScript
95 lines
3.4 KiB
TypeScript
import { useEffect, useRef } from 'react';
|
|
import { useBackendInitializer } from '@app/hooks/useBackendInitializer';
|
|
import { useOpenedFile } from '@app/hooks/useOpenedFile';
|
|
import { fileOpenService } from '@app/services/fileOpenService';
|
|
import { useFileManagement } from '@app/contexts/file/fileHooks';
|
|
|
|
/**
|
|
* App initialization hook
|
|
* Desktop version: Handles Tauri-specific initialization
|
|
* - Starts the backend on app startup
|
|
* - Handles files opened with the app (adds directly to FileContext)
|
|
*/
|
|
export function useAppInitialization(): void {
|
|
// Initialize backend on app startup
|
|
useBackendInitializer();
|
|
|
|
// Get file management actions
|
|
const { addFiles } = useFileManagement();
|
|
|
|
// Handle files opened with app (Tauri mode)
|
|
const { openedFilePaths, loading: openedFileLoading } = useOpenedFile();
|
|
|
|
// Track if we've already loaded the initial files to prevent duplicate loads
|
|
const initialFilesLoadedRef = useRef(false);
|
|
|
|
// Load opened files and add directly to FileContext
|
|
useEffect(() => {
|
|
if (openedFilePaths.length > 0 && !openedFileLoading && !initialFilesLoadedRef.current) {
|
|
initialFilesLoadedRef.current = true;
|
|
|
|
const loadOpenedFiles = async () => {
|
|
try {
|
|
const filesArray: File[] = [];
|
|
|
|
// Load all files in parallel
|
|
await Promise.all(
|
|
openedFilePaths.map(async (filePath) => {
|
|
try {
|
|
const fileData = await fileOpenService.readFileAsArrayBuffer(filePath);
|
|
if (fileData) {
|
|
const file = new File([fileData.arrayBuffer], fileData.fileName, {
|
|
type: 'application/pdf'
|
|
});
|
|
filesArray.push(file);
|
|
console.log('[Desktop] Loaded file:', fileData.fileName);
|
|
}
|
|
} catch (error) {
|
|
console.error('[Desktop] Failed to load file:', filePath, error);
|
|
}
|
|
})
|
|
);
|
|
|
|
if (filesArray.length > 0) {
|
|
// Add all files to FileContext at once
|
|
await addFiles(filesArray);
|
|
console.log(`[Desktop] ${filesArray.length} opened file(s) added to FileContext`);
|
|
}
|
|
} catch (error) {
|
|
console.error('[Desktop] Failed to load opened files:', error);
|
|
}
|
|
};
|
|
|
|
loadOpenedFiles();
|
|
}
|
|
}, [openedFilePaths, openedFileLoading, addFiles]);
|
|
|
|
// Listen for runtime file-opened events (from second instances on Windows/Linux)
|
|
useEffect(() => {
|
|
const handleRuntimeFileOpen = async (filePath: string) => {
|
|
try {
|
|
console.log('[Desktop] Runtime file-opened event received:', filePath);
|
|
const fileData = await fileOpenService.readFileAsArrayBuffer(filePath);
|
|
if (fileData) {
|
|
// Create a File object from the ArrayBuffer
|
|
const file = new File([fileData.arrayBuffer], fileData.fileName, {
|
|
type: 'application/pdf'
|
|
});
|
|
|
|
// Add directly to FileContext
|
|
await addFiles([file]);
|
|
console.log('[Desktop] Runtime opened file added to FileContext:', fileData.fileName);
|
|
}
|
|
} catch (error) {
|
|
console.error('[Desktop] Failed to load runtime opened file:', error);
|
|
}
|
|
};
|
|
|
|
// Set up event listener and get cleanup function
|
|
const unlisten = fileOpenService.onFileOpened(handleRuntimeFileOpen);
|
|
|
|
// Clean up listener on unmount
|
|
return unlisten;
|
|
}, [addFiles]);
|
|
}
|