From b9a41342a525c9f0bf74cc23c4593dc92387bf3f Mon Sep 17 00:00:00 2001 From: Julian Freeman Date: Sun, 7 Dec 2025 19:05:35 -0400 Subject: [PATCH] add save config --- src-tauri/src/lib.rs | 46 ++++++++++++++++++++++++++++++++++++++++++-- src/App.vue | 45 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 87 insertions(+), 4 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 96f278e..57d3784 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,5 +1,15 @@ use std::path::Path; use fs_extra::dir::CopyOptions; +use tauri::Manager; +use serde::{Deserialize, Serialize}; +use std::fs; + +// Define the configuration structure +#[derive(Serialize, Deserialize, Default, Debug)] +pub struct AppConfig { + pub working_dir: String, + pub template_dir: String, +} // Learn more about Tauri commands at https://tauri.app/develop/calling-rust/ #[tauri::command] @@ -33,12 +43,44 @@ fn copy_directory(template_path: String, target_path: String, new_folder_name: S Ok(destination.to_string_lossy().to_string()) } +#[tauri::command] +fn save_config(app_handle: tauri::AppHandle, config: AppConfig) -> Result<(), String> { + let config_dir = app_handle.path().app_config_dir().map_err(|e| e.to_string())?; + + // Ensure the directory exists + if !config_dir.exists() { + fs::create_dir_all(&config_dir).map_err(|e| e.to_string())?; + } + + let config_path = config_dir.join("config.json"); + let json = serde_json::to_string_pretty(&config).map_err(|e| e.to_string())?; + + fs::write(config_path, json).map_err(|e| e.to_string())?; + Ok(()) +} + +#[tauri::command] +fn load_config(app_handle: tauri::AppHandle) -> Result { + let config_dir = app_handle.path().app_config_dir().map_err(|e| e.to_string())?; + let config_path = config_dir.join("config.json"); + + if !config_path.exists() { + return Ok(AppConfig::default()); + } + + let json = fs::read_to_string(config_path).map_err(|e| e.to_string())?; + let config: AppConfig = serde_json::from_str(&json).map_err(|e| e.to_string())?; + + Ok(config) +} + + #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { tauri::Builder::default() .plugin(tauri_plugin_opener::init()) .plugin(tauri_plugin_dialog::init()) - .invoke_handler(tauri::generate_handler![greet, copy_directory]) + .invoke_handler(tauri::generate_handler![greet, copy_directory, save_config, load_config]) .run(tauri::generate_context!()) .expect("error while running tauri application"); -} \ No newline at end of file +} diff --git a/src/App.vue b/src/App.vue index 69a6a59..14cad8f 100644 --- a/src/App.vue +++ b/src/App.vue @@ -24,7 +24,7 @@ const toggleTheme = (val: string | number | boolean) => { } }; -onMounted(() => { +onMounted(async () => { const savedTheme = localStorage.getItem('theme'); if (savedTheme === 'dark' || (!savedTheme && window.matchMedia('(prefers-color-scheme: dark)').matches)) { isDark.value = true; @@ -33,6 +33,8 @@ onMounted(() => { isDark.value = false; document.documentElement.classList.remove('dark'); } + + await loadConfig(); }); // Preparation Data @@ -42,6 +44,45 @@ const selectedDate = ref(new Date()); const currentDir = ref(""); const videoNamePrefix = ref(""); +// Configuration Types +interface AppConfig { + working_dir: string; + template_dir: string; +} + +// Load/Save Config Logic +const loadConfig = async () => { + try { + const config = await invoke('load_config'); + if (config.working_dir) workingDir.value = config.working_dir; + if (config.template_dir) templateDir.value = config.template_dir; + } catch (e) { + console.error("Failed to load config:", e); + } +} + +const saveConfig = async () => { + try { + await invoke('save_config', { + config: { + working_dir: workingDir.value, + template_dir: templateDir.value + } + }); + } catch (e) { + console.error("Failed to save config:", e); + } +} + +// Watchers for Auto-Save +watch(workingDir, () => { + saveConfig(); +}); + +watch(templateDir, () => { + saveConfig(); +}); + // Actions const selectWorkingDir = async () => { const selected = await open({ @@ -271,4 +312,4 @@ body { max-width: 800px; margin-bottom: 20px; } - \ No newline at end of file +