diff --git a/src-tauri/capabilities/default.json b/src-tauri/capabilities/default.json index 4cdbf49..b4a8651 100644 --- a/src-tauri/capabilities/default.json +++ b/src-tauri/capabilities/default.json @@ -2,9 +2,12 @@ "$schema": "../gen/schemas/desktop-schema.json", "identifier": "default", "description": "Capability for the main window", - "windows": ["main"], + "windows": [ + "main" + ], "permissions": [ "core:default", - "opener:default" + "opener:default", + "core:window:allow-minimize" ] -} +} \ No newline at end of file diff --git a/src/pages/Index.tsx b/src/pages/Index.tsx index f784be3..9877901 100644 --- a/src/pages/Index.tsx +++ b/src/pages/Index.tsx @@ -8,7 +8,7 @@ import { Input } from '@/components/ui/input'; import { Slider } from '@/components/ui/slider'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { toast } from '@/hooks/use-toast'; -import { Quit } from '../../wailsjs/go/main/App'; +import { Window } from '@tauri-apps/api/window'; interface Note { id: string; @@ -52,7 +52,6 @@ const Index = () => { const [minLetterCount, setMinLetterCount] = useState([10]); const [isInitialized, setIsInitialized] = useState(false); const [fontSize, setFontSize] = useState('medium'); - const [isCurrentNoteModified, setIsCurrentNoteModified] = useState(false); const [isPreviousNoteModified, setIsPreviousNoteModified] = useState(false); const previousNoteRef = useRef(null); @@ -161,7 +160,7 @@ const Index = () => { const trimmedContent = cleanContent(content); const { mostFrequentLetter, mostFrequentLetterFrequency, letterCount } = calculateLetterFrequency(trimmedContent); const now = new Date(); - + const document = { id: generateRandomString(32), date: now.getTime(), @@ -199,7 +198,7 @@ const Index = () => { setNoteCache(prev => [newNote, ...prev]); setPreviousNote(newNote); setCurrentNoteIndex(0); - + toast({ title: "Note saved", description: "Your note has been saved successfully.", @@ -536,24 +535,28 @@ const Index = () => { }; // Handle current note save with debounce and content check - const handleSaveCurrentNote = async () => { - const trimmedContent = currentNote.trim(); - - // Clear any pending save timeout - if (saveTimeoutRef.current) { - clearTimeout(saveTimeoutRef.current); - } + const handleSaveCurrentNote = () => { + return new Promise((resolve) => { + const trimmedContent = currentNote.trim(); - // Only save if there's content and it's different from last saved - if (trimmedContent && trimmedContent !== lastSavedContentRef.current) { + // Clear any pending save timeout + if (saveTimeoutRef.current) { + clearTimeout(saveTimeoutRef.current); + } + + // Only save if there's content and it's different from last saved + if (trimmedContent && trimmedContent !== lastSavedContentRef.current) { // Set a timeout to prevent multiple saves in quick succession saveTimeoutRef.current = setTimeout(async () => { - await createNote(currentNote); - lastSavedContentRef.current = trimmedContent; - setCurrentNote(''); - setIsCurrentNoteModified(false); + await createNote(currentNote); + lastSavedContentRef.current = trimmedContent; + setCurrentNote(''); + resolve(); }, 300); // 300ms debounce - } + } else { + resolve(); + } + }); }; // Handle previous note scroll (reversed direction) @@ -625,37 +628,48 @@ const Index = () => { // Update the keyboard shortcuts effect useEffect(() => { - const handleKeyDown = (e: KeyboardEvent) => { + const handleKeyDown = async (e: KeyboardEvent) => { + try { if (e.ctrlKey && e.key === 'f') { - e.preventDefault(); - setIsSearchOpen(true); - setTimeout(() => { - searchInputRef.current?.focus(); - }, 100); + e.preventDefault(); + setIsSearchOpen(true); + setTimeout(() => { + searchInputRef.current?.focus(); + }, 100); } else if (e.key === 'Escape') { - e.preventDefault(); - // Save current note if it has content - no need for additional check since handleSaveCurrentNote has debounce - handleSaveCurrentNote(); - // Save previous note if it has been modified - if (previousNote && isPreviousNoteModified) { - handlePreviousNoteBlur(); - } - // Save scratch pad if it has content - if (scratchPad.trim()) { - saveScratch(scratchPad); - } - // Quit the application - Quit(); + e.preventDefault(); + // Save current note if it has content + if (currentNote.trim()) { + await handleSaveCurrentNote(); + } + // Save previous note if it has been modified + if (previousNote && isPreviousNoteModified) { + await handlePreviousNoteBlur(); + } + // Save scratch pad if it has content + if (scratchPad.trim()) { + await saveScratch(scratchPad); + } + // Minimize the window + try { + const win = await Window.getCurrent(); + await win.minimize(); + } catch (error) { + console.error('Failed to minimize window:', error); + } } + } catch (error) { + console.error('Error in keyboard handler:', error); + } }; document.addEventListener('keydown', handleKeyDown); return () => { - document.removeEventListener('keydown', handleKeyDown); - // Clear any pending save timeout on cleanup - if (saveTimeoutRef.current) { - clearTimeout(saveTimeoutRef.current); - } + document.removeEventListener('keydown', handleKeyDown); + // Clear any pending save timeout on cleanup + if (saveTimeoutRef.current) { + clearTimeout(saveTimeoutRef.current); + } }; }, [currentNote, previousNote, scratchPad, isPreviousNoteModified]); @@ -1085,7 +1099,6 @@ const Index = () => { value={currentNote} onChange={(e) => { setCurrentNote(e.target.value); - setIsCurrentNoteModified(true); }} onBlur={handleCurrentNoteBlur} className={`h-[calc(100%-2rem)] border-0 resize-none focus:ring-0 text-slate-200 bg-transparent ${getTextClass('2xl')}`} @@ -1133,7 +1146,7 @@ const Index = () => {
{formatDate(note.epochTime)}
-