From 86c007c28ae77d9503f83f0a1f4ac132149e2d12 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Sun, 17 Nov 2024 17:45:16 +1100 Subject: [PATCH] Remember last opened file --- package.json | 1 + pnpm-lock.yaml | 10 +++++++++ src-tauri/Cargo.lock | 29 ++++++++++++++++++++++++++ src-tauri/Cargo.toml | 1 + src-tauri/capabilities/default.json | 5 +++-- src-tauri/src/lib.rs | 32 ++++++++++++++++++++++++----- 6 files changed, 71 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 11486a0..a84a44e 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "@tauri-apps/plugin-dialog": "~2", "@tauri-apps/plugin-shell": "^2", "@tauri-apps/plugin-sql": "~2", + "@tauri-apps/plugin-store": "~2", "clusterize.js": "^1.0.0", "dayjs": "^1.11.13", "vue": "^3.3.4", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e54a849..ec3a4cf 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -23,6 +23,9 @@ importers: '@tauri-apps/plugin-sql': specifier: ~2 version: 2.0.1 + '@tauri-apps/plugin-store': + specifier: ~2 + version: 2.1.0 clusterize.js: specifier: ^1.0.0 version: 1.0.0 @@ -443,6 +446,9 @@ packages: '@tauri-apps/plugin-sql@2.0.1': resolution: {integrity: sha512-SxvRO/qwq/dHHGJ+79Bx4tB/wlfUE44sP1+wpuGOp11fgmfmOaf3nlZAl0P0KX+U3h0rwR/f7PMRQ6Eg408DYQ==} + '@tauri-apps/plugin-store@2.1.0': + resolution: {integrity: sha512-GADqrc17opUKYIAKnGHIUgEeTZ2wJGu1ZITKQ1WMuOFdv8fvXRFBAqsqPjE3opgWohbczX6e1NpwmZK1AnuWVw==} + '@types/clusterize.js@0.18.3': resolution: {integrity: sha512-udptC3aq8hfaXgmt9lC73OuE4RJYt26D2XIj+fTNDs0wuzAgQ6cyDpQOSkWhU65NroISAWhZ3/aovvV88IX7Gw==} @@ -1283,6 +1289,10 @@ snapshots: dependencies: '@tauri-apps/api': 2.1.1 + '@tauri-apps/plugin-store@2.1.0': + dependencies: + '@tauri-apps/api': 2.1.1 + '@types/clusterize.js@0.18.3': {} '@types/estree@1.0.6': {} diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 22b1364..6190309 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -934,6 +934,7 @@ dependencies = [ "tauri-plugin-dialog", "tauri-plugin-shell", "tauri-plugin-sql", + "tauri-plugin-store", ] [[package]] @@ -4398,6 +4399,22 @@ dependencies = [ "tokio", ] +[[package]] +name = "tauri-plugin-store" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e9a580be53f04bb62422d239aa798e88522877f58a0d4a0e745f030055a51bb4" +dependencies = [ + "dunce", + "log", + "serde", + "serde_json", + "tauri", + "tauri-plugin", + "thiserror 1.0.69", + "tokio", +] + [[package]] name = "tauri-runtime" version = "2.2.0" @@ -4629,10 +4646,22 @@ dependencies = [ "pin-project-lite", "signal-hook-registry", "socket2", + "tokio-macros", "tracing", "windows-sys 0.52.0", ] +[[package]] +name = "tokio-macros" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.87", +] + [[package]] name = "tokio-stream" version = "0.1.16" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 9988221..f267ddd 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -24,4 +24,5 @@ serde = { version = "1", features = ["derive"] } serde_json = "1" tauri-plugin-dialog = "2" tauri-plugin-sql = { version = "2", features = ["sqlite"] } +tauri-plugin-store = "2" diff --git a/src-tauri/capabilities/default.json b/src-tauri/capabilities/default.json index c2563c6..4a93c57 100644 --- a/src-tauri/capabilities/default.json +++ b/src-tauri/capabilities/default.json @@ -14,6 +14,7 @@ "dialog:default", "shell:allow-open", "sql:default", - "sql:allow-execute" + "sql:allow-execute", + "store:default" ] -} +} \ No newline at end of file diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 4769ea2..5ddbd40 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -16,8 +16,10 @@ along with this program. If not, see . */ -use tauri::{Builder, Manager, State}; +use tauri::{AppHandle, Builder, Manager, State}; +use tauri_plugin_store::StoreExt; +use std::fs; use std::sync::Mutex; struct AppState { @@ -31,24 +33,44 @@ fn get_open_filename(state: State<'_, Mutex>) -> Option { } #[tauri::command] -fn set_open_filename(state: State<'_, Mutex>, filename: Option) { +fn set_open_filename(state: State<'_, Mutex>, app: AppHandle, filename: Option) { let mut state = state.lock().unwrap(); - state.db_filename = filename; + state.db_filename = filename.clone(); + + // Persist in store + let store = app.store("store.json").expect("Error opening store"); + store.set("db_filename", filename); } #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { Builder::default() .setup(|app| { + // Get open filename + let store = app.store("store.json")?; + let db_filename = match store.get("db_filename") { + None => None, + Some(serde_json::Value::String(s)) => { + if fs::exists(&s)? { + Some(s) + } else { + None + } + }, + _ => panic!("Unexpected db_filename in store") + }; + app.manage(Mutex::new(AppState { - db_filename: None + db_filename: db_filename })); + Ok(()) }) .plugin(tauri_plugin_dialog::init()) .plugin(tauri_plugin_shell::init()) .plugin(tauri_plugin_sql::Builder::new().build()) + .plugin(tauri_plugin_store::Builder::new().build()) .invoke_handler(tauri::generate_handler![get_open_filename, set_open_filename]) .run(tauri::generate_context!()) - .expect("error while running tauri application"); + .expect("Error while running tauri application"); }