Remember last opened file

This commit is contained in:
RunasSudo 2024-11-17 17:45:16 +11:00
parent 0a09d0fa6f
commit 86c007c28a
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
6 changed files with 71 additions and 7 deletions

View File

@ -15,6 +15,7 @@
"@tauri-apps/plugin-dialog": "~2", "@tauri-apps/plugin-dialog": "~2",
"@tauri-apps/plugin-shell": "^2", "@tauri-apps/plugin-shell": "^2",
"@tauri-apps/plugin-sql": "~2", "@tauri-apps/plugin-sql": "~2",
"@tauri-apps/plugin-store": "~2",
"clusterize.js": "^1.0.0", "clusterize.js": "^1.0.0",
"dayjs": "^1.11.13", "dayjs": "^1.11.13",
"vue": "^3.3.4", "vue": "^3.3.4",

View File

@ -23,6 +23,9 @@ importers:
'@tauri-apps/plugin-sql': '@tauri-apps/plugin-sql':
specifier: ~2 specifier: ~2
version: 2.0.1 version: 2.0.1
'@tauri-apps/plugin-store':
specifier: ~2
version: 2.1.0
clusterize.js: clusterize.js:
specifier: ^1.0.0 specifier: ^1.0.0
version: 1.0.0 version: 1.0.0
@ -443,6 +446,9 @@ packages:
'@tauri-apps/plugin-sql@2.0.1': '@tauri-apps/plugin-sql@2.0.1':
resolution: {integrity: sha512-SxvRO/qwq/dHHGJ+79Bx4tB/wlfUE44sP1+wpuGOp11fgmfmOaf3nlZAl0P0KX+U3h0rwR/f7PMRQ6Eg408DYQ==} 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': '@types/clusterize.js@0.18.3':
resolution: {integrity: sha512-udptC3aq8hfaXgmt9lC73OuE4RJYt26D2XIj+fTNDs0wuzAgQ6cyDpQOSkWhU65NroISAWhZ3/aovvV88IX7Gw==} resolution: {integrity: sha512-udptC3aq8hfaXgmt9lC73OuE4RJYt26D2XIj+fTNDs0wuzAgQ6cyDpQOSkWhU65NroISAWhZ3/aovvV88IX7Gw==}
@ -1283,6 +1289,10 @@ snapshots:
dependencies: dependencies:
'@tauri-apps/api': 2.1.1 '@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/clusterize.js@0.18.3': {}
'@types/estree@1.0.6': {} '@types/estree@1.0.6': {}

29
src-tauri/Cargo.lock generated
View File

@ -934,6 +934,7 @@ dependencies = [
"tauri-plugin-dialog", "tauri-plugin-dialog",
"tauri-plugin-shell", "tauri-plugin-shell",
"tauri-plugin-sql", "tauri-plugin-sql",
"tauri-plugin-store",
] ]
[[package]] [[package]]
@ -4398,6 +4399,22 @@ dependencies = [
"tokio", "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]] [[package]]
name = "tauri-runtime" name = "tauri-runtime"
version = "2.2.0" version = "2.2.0"
@ -4629,10 +4646,22 @@ dependencies = [
"pin-project-lite", "pin-project-lite",
"signal-hook-registry", "signal-hook-registry",
"socket2", "socket2",
"tokio-macros",
"tracing", "tracing",
"windows-sys 0.52.0", "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]] [[package]]
name = "tokio-stream" name = "tokio-stream"
version = "0.1.16" version = "0.1.16"

View File

@ -24,4 +24,5 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1" serde_json = "1"
tauri-plugin-dialog = "2" tauri-plugin-dialog = "2"
tauri-plugin-sql = { version = "2", features = ["sqlite"] } tauri-plugin-sql = { version = "2", features = ["sqlite"] }
tauri-plugin-store = "2"

View File

@ -14,6 +14,7 @@
"dialog:default", "dialog:default",
"shell:allow-open", "shell:allow-open",
"sql:default", "sql:default",
"sql:allow-execute" "sql:allow-execute",
"store:default"
] ]
} }

View File

@ -16,8 +16,10 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
use tauri::{Builder, Manager, State}; use tauri::{AppHandle, Builder, Manager, State};
use tauri_plugin_store::StoreExt;
use std::fs;
use std::sync::Mutex; use std::sync::Mutex;
struct AppState { struct AppState {
@ -31,24 +33,44 @@ fn get_open_filename(state: State<'_, Mutex<AppState>>) -> Option<String> {
} }
#[tauri::command] #[tauri::command]
fn set_open_filename(state: State<'_, Mutex<AppState>>, filename: Option<String>) { fn set_open_filename(state: State<'_, Mutex<AppState>>, app: AppHandle, filename: Option<String>) {
let mut state = state.lock().unwrap(); 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)] #[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() { pub fn run() {
Builder::default() Builder::default()
.setup(|app| { .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 { app.manage(Mutex::new(AppState {
db_filename: None db_filename: db_filename
})); }));
Ok(()) Ok(())
}) })
.plugin(tauri_plugin_dialog::init()) .plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_shell::init()) .plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_sql::Builder::new().build()) .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]) .invoke_handler(tauri::generate_handler![get_open_filename, set_open_filename])
.run(tauri::generate_context!()) .run(tauri::generate_context!())
.expect("error while running tauri application"); .expect("Error while running tauri application");
} }