Add 'Restore last session on launch' setting (reopen last note + view)

This commit is contained in:
Yuri Karamian
2026-06-08 01:12:32 +02:00
parent 6115ad4ab6
commit 0d0ecee06e
6 changed files with 90 additions and 3 deletions
+2
View File
@@ -226,6 +226,7 @@ export async function setGeneralSettings(
closeToTray: boolean,
enableWikiLinks: boolean,
showNoteDates: boolean,
restoreLastSession: boolean,
): Promise<void> {
return invoke("set_general_settings", {
compactNotes,
@@ -242,6 +243,7 @@ export async function setGeneralSettings(
closeToTray,
enableWikiLinks,
showNoteDates,
restoreLastSession,
});
}
+57 -2
View File
@@ -33,6 +33,8 @@
quickAccessPaths,
tags,
notes,
notebooks,
rootNoteCount,
viewMode,
sortMode,
activeTag,
@@ -53,7 +55,16 @@
import { debounce } from '$lib/utils/debounce';
import { openNoteWindow } from '$lib/utils/window';
import { get } from 'svelte/store';
import type { VaultState, FileEvent } from '$lib/types';
import type { VaultState, FileEvent, NotebookEntry } from '$lib/types';
function findNotebookByPath(list: NotebookEntry[], relPath: string): NotebookEntry | null {
for (const nb of list) {
if (nb.relative_path === relPath) return nb;
const found = findNotebookByPath(nb.children, relPath);
if (found) return found;
}
return null;
}
let sidebar: Sidebar;
let noteList: NoteList;
@@ -186,7 +197,10 @@
collapsed_notebooks: $collapsedNotebooks,
notebook_sort_mode: $notebookSortMode,
notebook_order: $notebookOrder,
sort_mode: $sortMode
sort_mode: $sortMode,
last_view_mode: $viewMode,
last_notebook: $activeNotebook?.relative_path ?? null,
last_tag: $activeTag
};
try {
await saveVaultState(state);
@@ -428,8 +442,18 @@
persistState();
});
$effect(() => {
$viewMode;
$activeNotebook;
$activeTag;
persistState();
});
onMount(async () => {
let lastNotePath: string | null = null;
let lastViewMode = '';
let lastNotebook: string | null = null;
let lastTag: string | null = null;
try {
const state = await loadVaultState();
$sidebarWidth = state.sidebar_width;
@@ -440,6 +464,9 @@
$notebookOrder = state.notebook_order ?? {};
if (state.sort_mode === 'created' || state.sort_mode === 'title' || state.sort_mode === 'modified') $sortMode = state.sort_mode;
lastNotePath = state.last_open_note ?? null;
lastViewMode = state.last_view_mode ?? '';
lastNotebook = state.last_notebook ?? null;
lastTag = state.last_tag ?? null;
} catch (_) {}
// On mobile, prefetch last-opened note so first tap is instant
@@ -457,6 +484,34 @@
// Run sidebar and note list refresh in parallel
await Promise.all([sidebar?.refresh(), noteList?.refresh()]);
// Restore the last session (view + open note) if enabled.
if ($appConfig?.restore_last_session) {
const vault = $appConfig?.active_vault;
if (lastViewMode === 'notebook' && lastNotebook === '' && vault) {
$viewMode = 'notebook';
$activeNotebook = { name: 'Unfiled Notes', path: vault, relative_path: '', children: [], note_count: $rootNoteCount };
$activeTag = null;
await noteList?.refresh();
} else if (lastViewMode === 'notebook' && lastNotebook) {
const nb = findNotebookByPath($notebooks, lastNotebook);
if (nb) { $viewMode = 'notebook'; $activeNotebook = nb; $activeTag = null; await noteList?.refresh(); }
} else if (lastViewMode === 'tag' && lastTag) {
$viewMode = 'tag'; $activeTag = lastTag; $activeNotebook = null; await noteList?.refresh();
} else if (lastViewMode === 'quickaccess') {
$viewMode = 'quickaccess'; $activeNotebook = null; $activeTag = null; await noteList?.refresh();
}
// Open the last note on desktop (mobile reopens it via its own prefetch path below).
if (!isMobile && lastNotePath) {
try {
const content = await readNote(lastNotePath);
$activeNote = content;
$activeNotePath = lastNotePath;
$editorDirty = false;
editor?.loadNote(lastNotePath, content.content);
} catch (_) {}
}
}
// On mobile, derive tags from the scanned notes (avoids a separate full-scan Rust call)
if (isMobile) {
const tagMap = new Map<string, number>();
+13 -1
View File
@@ -458,6 +458,7 @@
// General settings
let compactNotes = $state($appConfig?.compact_notes ?? false);
let showNoteDates = $state($appConfig?.show_note_dates ?? true);
let restoreLastSession = $state($appConfig?.restore_last_session ?? false);
let timeFormat = $state($appConfig?.time_format ?? 'relative');
let gpuAcceleration = $state($appConfig?.gpu_acceleration ?? true);
let autostart = $state($appConfig?.autostart ?? false);
@@ -514,6 +515,7 @@
if ($appConfig) {
$appConfig.compact_notes = compactNotes;
$appConfig.show_note_dates = showNoteDates;
$appConfig.restore_last_session = restoreLastSession;
$appConfig.time_format = timeFormat;
$appConfig.gpu_acceleration = gpuAcceleration;
$appConfig.autostart = autostart;
@@ -527,7 +529,7 @@
$appConfig.close_to_tray = closeToTray;
$appConfig.enable_wiki_links = enableWikiLinks;
}
setGeneralSettings(compactNotes, timeFormat, gpuAcceleration, autostart, pdfPreview, pdfHeight, titleMode, hideTitleInBody, showLineNumbers, defaultViewMode, showTrayIcon, closeToTray, enableWikiLinks, showNoteDates)
setGeneralSettings(compactNotes, timeFormat, gpuAcceleration, autostart, pdfPreview, pdfHeight, titleMode, hideTitleInBody, showLineNumbers, defaultViewMode, showTrayIcon, closeToTray, enableWikiLinks, showNoteDates, restoreLastSession)
.catch((e) => console.error('Failed to save general settings:', e));
}
@@ -649,6 +651,7 @@
if ($appConfig) {
compactNotes = $appConfig.compact_notes ?? false;
showNoteDates = $appConfig.show_note_dates ?? true;
restoreLastSession = $appConfig.restore_last_session ?? false;
timeFormat = $appConfig.time_format ?? 'relative';
gpuAcceleration = $appConfig.gpu_acceleration ?? true;
autostart = $appConfig.autostart ?? false;
@@ -881,6 +884,15 @@
<span class="toggle-knob"></span>
</button>
</label>
<label class="setting-toggle">
<span class="setting-label">
<span class="setting-name">Restore last session on launch</span>
<span class="setting-desc">Reopen the note and folder you were last using, instead of All Notes.</span>
</span>
<button class="toggle-switch" class:on={restoreLastSession} onclick={() => { restoreLastSession = !restoreLastSession; saveGeneralSettings(); }}>
<span class="toggle-knob"></span>
</button>
</label>
</div>
<div class="settings-section">
+4
View File
@@ -83,6 +83,7 @@ export interface AppConfig {
default_view_mode: boolean;
show_tray_icon: boolean;
enable_wiki_links: boolean;
restore_last_session: boolean;
sync_provider: string | null;
webdav_url: string | null;
webdav_username: string | null;
@@ -102,6 +103,9 @@ export interface VaultState {
notebook_sort_mode?: string;
notebook_order?: Record<string, number>;
sort_mode?: string;
last_view_mode?: string;
last_notebook?: string | null;
last_tag?: string | null;
}
export interface SearchResult {