mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-11-16 01:21:16 +01:00
Redesign file opening to be simpler
This commit is contained in:
parent
cb2446ae83
commit
0649a1c89e
@ -32,9 +32,6 @@ pub fn run() {
|
|||||||
// Store file for later retrieval (in case frontend isn't ready yet)
|
// Store file for later retrieval (in case frontend isn't ready yet)
|
||||||
add_opened_file(arg.clone());
|
add_opened_file(arg.clone());
|
||||||
|
|
||||||
// Also emit event for immediate handling if frontend is ready
|
|
||||||
let _ = app.emit("file-opened", arg.clone());
|
|
||||||
|
|
||||||
// Bring the existing window to front
|
// Bring the existing window to front
|
||||||
if let Some(window) = app.get_webview_window("main") {
|
if let Some(window) = app.get_webview_window("main") {
|
||||||
let _ = window.set_focus();
|
let _ = window.set_focus();
|
||||||
@ -42,6 +39,9 @@ pub fn run() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Emit a generic notification that files were added (frontend will re-read storage)
|
||||||
|
let _ = app.emit("files-changed", ());
|
||||||
}))
|
}))
|
||||||
.setup(|_app| {
|
.setup(|_app| {
|
||||||
add_log("🚀 Tauri app setup started".to_string());
|
add_log("🚀 Tauri app setup started".to_string());
|
||||||
@ -75,6 +75,7 @@ pub fn run() {
|
|||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
RunEvent::Opened { urls } => {
|
RunEvent::Opened { urls } => {
|
||||||
add_log(format!("📂 Tauri file opened event: {:?}", urls));
|
add_log(format!("📂 Tauri file opened event: {:?}", urls));
|
||||||
|
let mut added_files = false;
|
||||||
for url in urls {
|
for url in urls {
|
||||||
let url_str = url.as_str();
|
let url_str = url.as_str();
|
||||||
if url_str.starts_with("file://") {
|
if url_str.starts_with("file://") {
|
||||||
@ -82,11 +83,14 @@ pub fn run() {
|
|||||||
if file_path.ends_with(".pdf") {
|
if file_path.ends_with(".pdf") {
|
||||||
add_log(format!("📂 Processing opened PDF: {}", file_path));
|
add_log(format!("📂 Processing opened PDF: {}", file_path));
|
||||||
add_opened_file(file_path.to_string());
|
add_opened_file(file_path.to_string());
|
||||||
// Use unified event name for consistency across platforms
|
added_files = true;
|
||||||
let _ = app_handle.emit("file-opened", file_path.to_string());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Emit a generic notification that files were added (frontend will re-read storage)
|
||||||
|
if added_files {
|
||||||
|
let _ = app_handle.emit("files-changed", ());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
// Only log unhandled events in debug mode to reduce noise
|
// Only log unhandled events in debug mode to reduce noise
|
||||||
|
|||||||
@ -20,19 +20,16 @@ export function useAppInitialization(): void {
|
|||||||
// Handle files opened with app (Tauri mode)
|
// Handle files opened with app (Tauri mode)
|
||||||
const { openedFilePaths, loading: openedFileLoading } = useOpenedFile();
|
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
|
// Load opened files and add directly to FileContext
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (openedFilePaths.length > 0 && !openedFileLoading && !initialFilesLoadedRef.current) {
|
if (openedFilePaths.length === 0 || openedFileLoading) {
|
||||||
initialFilesLoadedRef.current = true;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const loadOpenedFiles = async () => {
|
const loadOpenedFiles = async () => {
|
||||||
try {
|
try {
|
||||||
const filesArray: File[] = [];
|
const filesArray: File[] = [];
|
||||||
|
|
||||||
// Load all files in parallel
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
openedFilePaths.map(async (filePath) => {
|
openedFilePaths.map(async (filePath) => {
|
||||||
try {
|
try {
|
||||||
@ -51,7 +48,6 @@ export function useAppInitialization(): void {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (filesArray.length > 0) {
|
if (filesArray.length > 0) {
|
||||||
// Add all files to FileContext at once
|
|
||||||
await addFiles(filesArray);
|
await addFiles(filesArray);
|
||||||
console.log(`[Desktop] ${filesArray.length} opened file(s) added to FileContext`);
|
console.log(`[Desktop] ${filesArray.length} opened file(s) added to FileContext`);
|
||||||
}
|
}
|
||||||
@ -61,34 +57,5 @@ export function useAppInitialization(): void {
|
|||||||
};
|
};
|
||||||
|
|
||||||
loadOpenedFiles();
|
loadOpenedFiles();
|
||||||
}
|
|
||||||
}, [openedFilePaths, openedFileLoading, addFiles]);
|
}, [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]);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,45 +1,46 @@
|
|||||||
import { useState, useEffect } from 'react';
|
import { useState, useEffect } from 'react';
|
||||||
import { fileOpenService } from '@app/services/fileOpenService';
|
import { fileOpenService } from '@app/services/fileOpenService';
|
||||||
|
import { listen } from '@tauri-apps/api/event';
|
||||||
|
|
||||||
export function useOpenedFile() {
|
export function useOpenedFile() {
|
||||||
const [openedFilePaths, setOpenedFilePaths] = useState<string[]>([]);
|
const [openedFilePaths, setOpenedFilePaths] = useState<string[]>([]);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const checkForOpenedFile = async () => {
|
// Function to read and process files from storage
|
||||||
console.log('🔍 Checking for opened file(s)...');
|
const readFilesFromStorage = async () => {
|
||||||
|
console.log('🔍 Reading files from storage...');
|
||||||
try {
|
try {
|
||||||
const filePaths = await fileOpenService.getOpenedFiles();
|
const filePaths = await fileOpenService.getOpenedFiles();
|
||||||
console.log('🔍 fileOpenService.getOpenedFiles() returned:', filePaths);
|
console.log('🔍 fileOpenService.getOpenedFiles() returned:', filePaths);
|
||||||
|
|
||||||
if (filePaths.length > 0) {
|
if (filePaths.length > 0) {
|
||||||
console.log(`✅ App opened with ${filePaths.length} file(s):`, filePaths);
|
console.log(`✅ Found ${filePaths.length} file(s) in storage:`, filePaths);
|
||||||
setOpenedFilePaths(filePaths);
|
setOpenedFilePaths(filePaths);
|
||||||
|
|
||||||
// Clear the files from service state after consuming them
|
|
||||||
await fileOpenService.clearOpenedFiles();
|
await fileOpenService.clearOpenedFiles();
|
||||||
} else {
|
|
||||||
console.log('ℹ️ No files were opened with the app');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('❌ Failed to check for opened files:', error);
|
console.error('❌ Failed to read files from storage:', error);
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
checkForOpenedFile();
|
// Read files on mount
|
||||||
|
readFilesFromStorage();
|
||||||
|
|
||||||
// Listen for runtime file open events (abstracted through service)
|
// Listen for files-changed events (when new files are added to storage)
|
||||||
const unlistenRuntimeEvents = fileOpenService.onFileOpened((filePath: string) => {
|
let unlisten: (() => void) | undefined;
|
||||||
console.log('📂 Runtime file open event:', filePath);
|
listen('files-changed', async () => {
|
||||||
setOpenedFilePaths(prev => [...prev, filePath]);
|
console.log('📂 files-changed event received, re-reading storage...');
|
||||||
|
await readFilesFromStorage();
|
||||||
|
}).then(unlistenFn => {
|
||||||
|
unlisten = unlistenFn;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Cleanup function
|
// Cleanup function
|
||||||
return () => {
|
return () => {
|
||||||
unlistenRuntimeEvents();
|
if (unlisten) unlisten();
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user