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