feat: persist project selection
This commit is contained in:
29
src-tauri/Cargo.lock
generated
29
src-tauri/Cargo.lock
generated
@@ -2066,6 +2066,7 @@ dependencies = [
|
||||
"tauri-build",
|
||||
"tauri-plugin-dialog",
|
||||
"tauri-plugin-opener",
|
||||
"tauri-plugin-store",
|
||||
"uuid",
|
||||
"walkdir",
|
||||
]
|
||||
@@ -4121,6 +4122,22 @@ dependencies = [
|
||||
"zbus",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tauri-plugin-store"
|
||||
version = "2.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "59a77036340a97eb5bbe1b3209c31e5f27f75e6f92a52fd9dd4b211ef08bf310"
|
||||
dependencies = [
|
||||
"dunce",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tauri",
|
||||
"tauri-plugin",
|
||||
"thiserror 2.0.17",
|
||||
"tokio",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tauri-runtime"
|
||||
version = "2.9.2"
|
||||
@@ -4339,10 +4356,22 @@ dependencies = [
|
||||
"pin-project-lite",
|
||||
"signal-hook-registry",
|
||||
"socket2",
|
||||
"tokio-macros",
|
||||
"tracing",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-macros"
|
||||
version = "2.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "af407857209536a95c8e56f8231ef2c2e2aff839b22e07a1ffcbc617e9db9fa5"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.111",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-native-tls"
|
||||
version = "0.3.1"
|
||||
|
||||
@@ -29,4 +29,5 @@ reqwest = { version = "0.12.28", features = ["json", "blocking"] }
|
||||
uuid = { version = "1.19.0", features = ["v4", "serde"] }
|
||||
chrono = { version = "0.4.42", features = ["serde"] }
|
||||
async-trait = "0.1.89"
|
||||
tauri-plugin-store = "2.4.1"
|
||||
|
||||
|
||||
@@ -3,5 +3,10 @@
|
||||
"identifier": "default",
|
||||
"description": "Capability for the main window",
|
||||
"windows": ["main"],
|
||||
"permissions": ["core:default", "opener:default", "dialog:default"]
|
||||
"permissions": [
|
||||
"core:default",
|
||||
"opener:default",
|
||||
"dialog:default",
|
||||
"store:default"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
use crate::state::SessionState;
|
||||
use serde::Serialize;
|
||||
use serde_json::json;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use tauri::State;
|
||||
use tauri::{AppHandle, State};
|
||||
use tauri_plugin_store::StoreExt;
|
||||
|
||||
const STORE_PATH: &str = "store.json";
|
||||
const KEY_LAST_PROJECT: &str = "last_project_path";
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Helper Functions
|
||||
@@ -32,7 +37,11 @@ fn resolve_path(state: &State<'_, SessionState>, relative_path: &str) -> Result<
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn open_project(path: String, state: State<'_, SessionState>) -> Result<String, String> {
|
||||
pub async fn open_project(
|
||||
app: AppHandle,
|
||||
path: String,
|
||||
state: State<'_, SessionState>,
|
||||
) -> Result<String, String> {
|
||||
let p = PathBuf::from(&path);
|
||||
|
||||
// Validate path existence in blocking thread
|
||||
@@ -49,13 +58,73 @@ pub async fn open_project(path: String, state: State<'_, SessionState>) -> Resul
|
||||
.await
|
||||
.map_err(|e| format!("Task failed: {}", e))??;
|
||||
|
||||
let mut root = state.project_root.lock().map_err(|e| e.to_string())?;
|
||||
*root = Some(p.clone());
|
||||
{
|
||||
let mut root = state.project_root.lock().map_err(|e| e.to_string())?;
|
||||
*root = Some(p.clone());
|
||||
}
|
||||
|
||||
// Persist to store
|
||||
let store = app
|
||||
.store(STORE_PATH)
|
||||
.map_err(|e| format!("Failed to access store: {}", e))?;
|
||||
store.set(KEY_LAST_PROJECT, json!(path));
|
||||
let _ = store.save();
|
||||
|
||||
println!("Project opened: {:?}", p);
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn close_project(app: AppHandle, state: State<'_, SessionState>) -> Result<(), String> {
|
||||
// Clear session state
|
||||
{
|
||||
let mut root = state.project_root.lock().map_err(|e| e.to_string())?;
|
||||
*root = None;
|
||||
}
|
||||
|
||||
// Clear from store
|
||||
let store = app
|
||||
.store(STORE_PATH)
|
||||
.map_err(|e| format!("Failed to access store: {}", e))?;
|
||||
store.delete(KEY_LAST_PROJECT);
|
||||
let _ = store.save();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_current_project(
|
||||
app: AppHandle,
|
||||
state: State<'_, SessionState>,
|
||||
) -> Result<Option<String>, String> {
|
||||
// 1. Check in-memory state
|
||||
{
|
||||
let root = state.project_root.lock().map_err(|e| e.to_string())?;
|
||||
if let Some(path) = &*root {
|
||||
return Ok(Some(path.to_string_lossy().to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Check store
|
||||
let store = app
|
||||
.store(STORE_PATH)
|
||||
.map_err(|e| format!("Failed to access store: {}", e))?;
|
||||
|
||||
if let Some(val) = store.get(KEY_LAST_PROJECT) {
|
||||
if let Some(path_str) = val.as_str() {
|
||||
let p = PathBuf::from(path_str);
|
||||
if p.exists() && p.is_dir() {
|
||||
// Update session state
|
||||
let mut root = state.project_root.lock().map_err(|e| e.to_string())?;
|
||||
*root = Some(p.clone());
|
||||
return Ok(Some(path_str.to_string()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn read_file(path: String, state: State<'_, SessionState>) -> Result<String, String> {
|
||||
let full_path = resolve_path(&state, &path)?;
|
||||
|
||||
@@ -9,9 +9,12 @@ pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.plugin(tauri_plugin_dialog::init())
|
||||
.plugin(tauri_plugin_store::Builder::default().build())
|
||||
.manage(SessionState::default())
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
commands::fs::open_project,
|
||||
commands::fs::close_project,
|
||||
commands::fs::get_current_project,
|
||||
commands::fs::read_file,
|
||||
commands::fs::write_file,
|
||||
commands::fs::list_directory,
|
||||
|
||||
Reference in New Issue
Block a user