Config
This commit is contained in:
@@ -0,0 +1,64 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::fs;
|
||||||
|
use std::sync::Mutex;
|
||||||
|
use tauri::{AppHandle, Manager, State};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct Config {
|
||||||
|
pub audio_device_name: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Config {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
audio_device_name: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ConfigState(pub Mutex<Config>);
|
||||||
|
|
||||||
|
fn config_file_path(app: &AppHandle) -> Result<std::path::PathBuf, String> {
|
||||||
|
let dir = app
|
||||||
|
.path()
|
||||||
|
.app_data_dir()
|
||||||
|
.map_err(|e| format!("Failed to resolve app data dir: {e}"))?;
|
||||||
|
fs::create_dir_all(&dir).map_err(|e| format!("Failed to create app data dir: {e}"))?;
|
||||||
|
Ok(dir.join("config.json"))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn update_config(state: State<ConfigState>, config: Config) -> Result<(), String> {
|
||||||
|
*state.0.lock().unwrap() = config;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn save_config(app: AppHandle, state: State<ConfigState>) -> Result<(), String> {
|
||||||
|
let path = config_file_path(&app)?;
|
||||||
|
let config = state.0.lock().unwrap();
|
||||||
|
|
||||||
|
let json = serde_json::to_string_pretty(&*config)
|
||||||
|
.map_err(|e| format!("Failed to serialize config: {e}"))?;
|
||||||
|
|
||||||
|
fs::write(&path, json).map_err(|e| format!("Failed to write config: {e}"))?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn get_config(app: AppHandle, state: State<ConfigState>) -> Result<Config, String> {
|
||||||
|
let path = config_file_path(&app)?;
|
||||||
|
|
||||||
|
let config = if path.exists() {
|
||||||
|
let json = fs::read_to_string(&path).map_err(|e| format!("Failed to read config: {e}"))?;
|
||||||
|
serde_json::from_str(&json).map_err(|e| format!("Failed to parse config: {e}"))?
|
||||||
|
} else {
|
||||||
|
Config::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
*state.0.lock().unwrap() = config.clone();
|
||||||
|
|
||||||
|
Ok(config)
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
pub mod accounts;
|
pub mod accounts;
|
||||||
pub mod audio;
|
pub mod audio;
|
||||||
|
pub mod config;
|
||||||
pub mod server_list;
|
pub mod server_list;
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
|
use std::sync::Mutex;
|
||||||
|
|
||||||
|
use crate::commands::config::ConfigState;
|
||||||
|
|
||||||
pub mod commands;
|
pub mod commands;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
|
|
||||||
@@ -5,12 +9,16 @@ pub mod types;
|
|||||||
pub fn run() {
|
pub fn run() {
|
||||||
tauri::Builder::default()
|
tauri::Builder::default()
|
||||||
.plugin(tauri_plugin_opener::init())
|
.plugin(tauri_plugin_opener::init())
|
||||||
|
.manage(ConfigState(Mutex::new(commands::config::Config::default())))
|
||||||
.invoke_handler(tauri::generate_handler![
|
.invoke_handler(tauri::generate_handler![
|
||||||
commands::server_list::save_server_list,
|
commands::server_list::save_server_list,
|
||||||
commands::server_list::get_server_list,
|
commands::server_list::get_server_list,
|
||||||
commands::accounts::save_accounts,
|
commands::accounts::save_accounts,
|
||||||
commands::accounts::get_accounts,
|
commands::accounts::get_accounts,
|
||||||
commands::audio::list_input_devices,
|
commands::audio::list_input_devices,
|
||||||
|
commands::config::update_config,
|
||||||
|
commands::config::save_config,
|
||||||
|
commands::config::get_config,
|
||||||
])
|
])
|
||||||
.run(tauri::generate_context!())
|
.run(tauri::generate_context!())
|
||||||
.expect("error while running tauri application");
|
.expect("error while running tauri application");
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import { invoke } from "@tauri-apps/api/core";
|
||||||
|
|
||||||
|
export type Config = {
|
||||||
|
audioDeviceName: string | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function updateConfig(config: Config): Promise<void> {
|
||||||
|
await invoke("update_config", { config });
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function saveConfig(): Promise<void> {
|
||||||
|
await invoke("save_config");
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getConfig(): Promise<Config> {
|
||||||
|
return await invoke("get_config");
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user