A photo manager for VRChat.

update to use proper config manager

phaz.uk 7e270585 ca4e21d6

verified
Changed files
+63 -31
src-tauri
+5 -3
src-tauri/src/frontend_calls/close_splashscreen.rs
··· 1 1 use std::env; 2 - use tauri::{ Emitter, Manager }; 2 + use tauri::{ Emitter, Manager, State }; 3 + 4 + use crate::frontend_calls::config::Config; 3 5 4 6 use super::config::get_config_value_string; 5 7 6 8 #[tauri::command] 7 - pub fn close_splashscreen( window: tauri::Window ) { 9 + pub fn close_splashscreen( window: tauri::Window, config: State<Config> ) { 8 10 let args: Vec<String> = env::args().collect(); 9 11 10 12 let mut show = true; ··· 14 16 } 15 17 } 16 18 17 - let value: String = match get_config_value_string("start-in-bg".to_owned()) { Some(val) => val, None => "false".to_owned() }; 19 + let value: String = match get_config_value_string("start-in-bg".to_owned(), config) { Some(val) => val, None => "false".to_owned() }; 18 20 if value == "true"{ 19 21 show = false; 20 22 }
+42 -23
src-tauri/src/frontend_calls/config.rs
··· 1 - use std::{fs, path::PathBuf}; 1 + use std::{fs, path::PathBuf, sync::Mutex}; 2 2 3 3 use serde_json::Value; 4 + use tauri::State; 4 5 5 6 pub fn get_config_path() -> PathBuf { 6 7 let path = dirs::config_dir() ··· 17 18 path 18 19 } 19 20 21 + pub struct Config{ 22 + config: Mutex<Value> 23 + } 20 24 21 - // TODO: Redo all of this just, stop please. 22 - #[tauri::command] 23 - pub fn set_config_value_string(key: String, value: String) { 24 - let path = get_config_path(); 25 + impl Config{ 26 + pub fn new() -> Config{ 27 + let path = get_config_path(); 28 + let config: Value = serde_json::from_str(&fs::read_to_string(&path).unwrap()).unwrap(); 29 + 30 + Config { 31 + config: Mutex::new(config) 32 + } 33 + } 34 + 35 + pub fn set( &self, key: String, value: Value ){ 36 + let mut lock = self.config.lock().unwrap(); 37 + lock[key] = value; 38 + } 39 + 40 + pub fn get( &self, key: String ) -> Value{ 41 + let lock = self.config.lock().unwrap(); 42 + lock[key].clone() 43 + } 44 + 45 + pub fn save( &self ){ 46 + let path = get_config_path(); 47 + let string = serde_json::to_string(&self.config).unwrap(); 25 48 26 - let mut config: Value = serde_json::from_str(&fs::read_to_string(&path).unwrap()).unwrap(); 27 - config[key] = Value::from(value); 49 + fs::write(path, string).unwrap(); 50 + } 51 + } 28 52 29 - fs::write(path, config.to_string()).unwrap(); 53 + #[tauri::command] 54 + pub fn set_config_value_string( key: String, value: String, config: State<Config> ) { 55 + config.set(key, Value::from(value)); 30 56 } 31 57 32 58 #[tauri::command] 33 - pub fn get_config_value_string(key: String) -> Option<String> { 34 - let config: Value = 35 - serde_json::from_str(&fs::read_to_string(get_config_path()).unwrap()).unwrap(); 36 - let string = config[key].as_str(); 59 + pub fn get_config_value_string( key: String, config: State<Config> ) -> Option<String> { 60 + let string = config.get(key); 61 + let string = string.as_str(); 37 62 38 63 if string.is_some() { 39 64 Some(string.unwrap().to_owned()) ··· 43 68 } 44 69 45 70 #[tauri::command] 46 - pub fn set_config_value_int(key: String, value: i64) { 47 - let path = get_config_path(); 48 - 49 - let mut config: Value = serde_json::from_str(&fs::read_to_string(&path).unwrap()).unwrap(); 50 - config[key] = Value::from(value); 51 - 52 - fs::write(path, config.to_string()).unwrap(); 71 + pub fn set_config_value_int( key: String, value: i64, config: State<Config> ) { 72 + config.set(key, Value::from(value)); 53 73 } 54 74 55 75 #[tauri::command] 56 - pub fn get_config_value_int(key: String) -> Option<i64> { 57 - let config: Value = 58 - serde_json::from_str(&fs::read_to_string(get_config_path()).unwrap()).unwrap(); 59 - config[key].as_i64() 76 + pub fn get_config_value_int( key: String, config: State<Config> ) -> Option<i64> { 77 + let string = config.get(key); 78 + string.as_i64() 60 79 }
+9 -3
src-tauri/src/main.rs
··· 17 17 use tauri::{ Emitter, Manager, State, WindowEvent }; 18 18 use tauri_plugin_deep_link::DeepLinkExt; 19 19 20 - use crate::frontend_calls::config::get_config_value_string; 20 + use crate::frontend_calls::config::{get_config_value_string, Config}; 21 21 22 22 fn main() { 23 23 #[cfg(target_os = "linux")] ··· 154 154 }) 155 155 .on_window_event(|window, event| match event { 156 156 WindowEvent::CloseRequested { api, .. } => { 157 - let val = get_config_value_string("minimise-on-close".into()); 158 - if val.is_some() && val.unwrap() == "false"{ return; } 157 + let config: State<Config> = window.state(); 158 + 159 + let val = get_config_value_string("minimise-on-close".into(), config.clone()); 160 + if val.is_some() && val.unwrap() == "false"{ 161 + config.save(); 162 + return; 163 + } 159 164 160 165 window.hide().unwrap(); 161 166 api.prevent_close(); 162 167 } 163 168 _ => {} 164 169 }) 170 + .manage(Config::new()) 165 171 .manage(cache) 166 172 .manage(Mutex::new(clipboard)) 167 173 .setup(|app| {
+1 -1
src-tauri/src/util/cache.rs
··· 1 - use std::{collections::HashMap, sync::Mutex}; 1 + use std::{ collections::HashMap, sync::Mutex }; 2 2 3 3 pub struct Cache{ 4 4 store: Mutex<HashMap<String, String>>,
+6 -1
src-tauri/src/util/setup_traymenu.rs
··· 1 1 use tauri::{ 2 2 menu::{MenuBuilder, MenuItemBuilder}, 3 3 tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent}, 4 - AppHandle, Emitter, Manager, 4 + AppHandle, Emitter, Manager, State, 5 5 }; 6 + 7 + use crate::frontend_calls::config::Config; 6 8 7 9 #[derive(serde::Serialize, Clone)] 8 10 struct EmptyEvent {} ··· 31 33 .tooltip("VRChat Photo Manager") 32 34 .on_menu_event(move |app: &AppHandle, event| match event.id().as_ref() { 33 35 "quit" => { 36 + let config: State<Config> = app.state(); 37 + config.save(); 38 + 34 39 std::process::exit(0); 35 40 } 36 41 "hide" => {