add save config

This commit is contained in:
Julian Freeman
2025-12-07 19:05:35 -04:00
parent fb8be899d5
commit b9a41342a5
2 changed files with 87 additions and 4 deletions

View File

@@ -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<AppConfig, String> {
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");
}
}

View File

@@ -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<Date | null>(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<AppConfig>('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;
}
</style>
</style>