Resizable sidebar remembered in settings.json; nest tags by / like branches
This commit is contained in:
+18
-1
@@ -1,5 +1,6 @@
|
||||
mod filters;
|
||||
mod git;
|
||||
mod settings;
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
@@ -21,6 +22,16 @@ fn save_filters(repo: String, value: serde_json::Value) -> Result<(), String> {
|
||||
filters::save(&repo, value)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn load_settings() -> Result<serde_json::Value, String> {
|
||||
settings::load()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn save_settings(value: serde_json::Value) -> Result<(), String> {
|
||||
settings::save(value)
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
let path = std::env::args_os()
|
||||
@@ -31,7 +42,13 @@ pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_clipboard_manager::init())
|
||||
.manage(RepoArg(path))
|
||||
.invoke_handler(tauri::generate_handler![load_repo, load_filters, save_filters])
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
load_repo,
|
||||
load_filters,
|
||||
save_filters,
|
||||
load_settings,
|
||||
save_settings
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
//! App-wide settings, such as the sidebar width, in `~/.local/share/gites/settings.json`.
|
||||
|
||||
use serde_json::Value;
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn file() -> Result<PathBuf, String> {
|
||||
Ok(dirs::data_dir().ok_or("no data folder found")?.join("gites").join("settings.json"))
|
||||
}
|
||||
|
||||
pub fn load() -> Result<Value, String> {
|
||||
let path = file()?;
|
||||
match std::fs::read_to_string(&path) {
|
||||
Ok(text) => Ok(serde_json::from_str(&text).unwrap_or(Value::Null)),
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Value::Null),
|
||||
Err(e) => Err(format!("could not read {}: {e}", path.display())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save(value: Value) -> Result<(), String> {
|
||||
let path = file()?;
|
||||
if let Some(dir) = path.parent() {
|
||||
std::fs::create_dir_all(dir).map_err(|e| e.to_string())?;
|
||||
}
|
||||
let text = serde_json::to_string_pretty(&value).map_err(|e| e.to_string())?;
|
||||
std::fs::write(&path, text).map_err(|e| format!("could not write {}: {e}", path.display()))
|
||||
}
|
||||
Reference in New Issue
Block a user