diff --git a/src-tauri/src/boot_config.rs b/src-tauri/src/boot_config.rs index 19b61742..b3d08bba 100644 --- a/src-tauri/src/boot_config.rs +++ b/src-tauri/src/boot_config.rs @@ -1,69 +1,53 @@ -use std::sync::Arc; -use once_cell::sync::OnceCell; use serde_json::Value; use serde::Serialize; use crate::utilities::read_json_file; -use std::path::PathBuf; -use std::fs::File; -use std::io::Write; - -pub struct AppConstants { - pub tauri_config : Arc, - pub app_local_data_dir: std::path::PathBuf -} -pub static APP_CONSTANTS: OnceCell = OnceCell::new(); +use std::path::{Path, PathBuf}; #[derive(Serialize)] pub struct BootConfig { - pub version: u32 + pub version: u32, + pub start_as_hidden_window: bool, } -static BOOT_CONFIG_FILE_NAME: &'static str = "boot_config.json"; -fn get_boot_config_file_path(app_local_data_dir: &PathBuf) -> PathBuf { - let mut config_file_path = app_local_data_dir.clone(); - config_file_path.push(BOOT_CONFIG_FILE_NAME); - return config_file_path; +fn get_boot_config_file_path(base_path: &Path) -> PathBuf { + let mut config_file_path = base_path.to_path_buf(); + config_file_path.push("boot_config.json"); + config_file_path } fn _set_boot_config(boot_config: &mut BootConfig, value: &Value) { - boot_config.version = match value["version"].as_u64() { - Some(value) => value as u32, - None => 0 - }; + boot_config.version = value["version"].as_u64().map(|v| v as u32).unwrap_or(0); + + boot_config.start_as_hidden_window = value["start_as_hidden_window"] + .as_bool() + .unwrap_or(false); // Default to `false` if missing or invalid } -pub fn read_boot_config() -> BootConfig { +/// Reads boot_config.json from the given `Option`. +/// If `None` is provided, it returns a default `BootConfig`. +pub fn read_boot_config(base_path: &Option) -> BootConfig { let mut boot_config = BootConfig { - version: 1 + version: 1, + start_as_hidden_window: false, }; - if let Some(app_constants) = APP_CONSTANTS.get() { - let boot_config_file_path = get_boot_config_file_path(&app_constants.app_local_data_dir); + + if let Some(ref path) = base_path { + let boot_config_file_path = get_boot_config_file_path(path); + match read_json_file(&boot_config_file_path) { - Some(value) =>{ + Some(value) => { _set_boot_config(&mut boot_config, &value); } None => { - eprintln!("No boot restore config file found {}", boot_config_file_path.display()); + eprintln!( + "No boot restore config file found at {}", + boot_config_file_path.display() + ); } } + } else { + eprintln!("Base path is None, using default boot config."); } - return boot_config; -} -fn _write_boot_config(boot_config: &BootConfig) { - if let Some(app_constants) = APP_CONSTANTS.get() { - let boot_config_file_path = get_boot_config_file_path(&app_constants.app_local_data_dir); - // Convert the BootConfig struct to JSON - let json_string = serde_json::to_string(boot_config).unwrap(); - let mut file = File::create(boot_config_file_path).expect("Failed to create file"); - file.write_all(json_string.as_bytes()) - .expect("Failed to write to boot config file"); - } + boot_config } - -// WARNING: If there are multiple windows, this will be called on each window close. -pub fn write_boot_config(version: u32) { - _write_boot_config(&BootConfig { - version - }) -} \ No newline at end of file diff --git a/src-tauri/src/init.rs b/src-tauri/src/init.rs deleted file mode 100644 index 1532a8c8..00000000 --- a/src-tauri/src/init.rs +++ /dev/null @@ -1,27 +0,0 @@ -use crate::utilities::ensure_dir_exists; -use crate::boot_config::read_boot_config; -use crate::boot_config::APP_CONSTANTS; -use crate::boot_config::AppConstants; - -pub fn init_app(app: &mut tauri::App) { - let config = app.config().clone(); - println!("Appdata path is {}", tauri::api::path::app_local_data_dir(&config).expect("failed to retrieve app_local_data_dir").display()); - ensure_dir_exists(&tauri::api::path::app_local_data_dir(&config).unwrap()); // canonicalize will work only if path exists - let _ = APP_CONSTANTS.set(AppConstants { - tauri_config: config.clone(), - app_local_data_dir: tauri::api::path::app_local_data_dir(&config).expect("failed to retrieve app_local_data_dir") - .canonicalize().expect("Failed to canonicalize app_local_data_dir") - }); - - // To get a value - if let Some(app_constants) = APP_CONSTANTS.get() { - #[cfg(debug_assertions)]{ - println!("Bundle ID is {}", app_constants.tauri_config.tauri.bundle.identifier); - } - ensure_dir_exists(&app_constants.app_local_data_dir); - read_boot_config(); - #[cfg(debug_assertions)]{ - println!("Bootconfig version is {}", read_boot_config().version); - } - } -} \ No newline at end of file diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index f9e30582..02bd2cb0 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -36,8 +36,10 @@ use native_dialog::{MessageDialog, MessageType}; use regex::Regex; extern crate percent_encoding; use tauri::http::ResponseBuilder; -use tauri::GlobalWindowEvent; -mod init; +use tauri::api::path::app_local_data_dir; +use tauri::generate_context; +use crate::utilities::ensure_dir_exists; +use crate::boot_config::read_boot_config; mod bugsnag; mod utilities; mod boot_config; @@ -257,12 +259,13 @@ fn zoom_window(window: tauri::Window, scale_factor: f64) { }); } -fn process_window_event(event: &GlobalWindowEvent) { - if let tauri::WindowEvent::CloseRequested { .. } = event.event() { - // this does nothing and is here if in future you need to persist something on window close. - boot_config::write_boot_config(1); - } -} +// here in case you need to process windows events +// use tauri::GlobalWindowEvent; +// fn process_window_event(event: &GlobalWindowEvent) { +// if let tauri::WindowEvent::CloseRequested { .. } = event.event() { +// // this does nothing and is here if in future you need to persist something on window close. +// } +// } // convert url of form "protocol://host/v1.2.3/path/to/something" to "protocol://host/path/to/something" fn remove_version_from_url(url: &str) -> String { @@ -366,6 +369,28 @@ fn main() { tauri_plugin_deep_link::prepare("io.phcode"); } + let context = generate_context!(); + let mut tauri_config = context.config().clone(); // Clone the config to modify it + + // Get the app data directory before Tauri starts + let app_data_dir: Option = app_local_data_dir(&tauri_config); + + if let Some(dir) = &app_data_dir { + println!("App Data Directory: {}", dir.display()); + ensure_dir_exists(dir); + } else { + eprintln!("Failed to retrieve app data directory."); + } + + let boot_config = read_boot_config(&app_data_dir); + + // Modify the first window's visibility based on boot_config.start_as_hidden_window + if let Some(first_window) = tauri_config.tauri.windows.get_mut(0) { + if !boot_config.start_as_hidden_window { + first_window.visible = true; + } + } + // warning: any string that resembles the following strings will be rewritten in source in prod by build scripts. // This is so that app bundle IDs are correct. IF they are app bundle IDs use the strings. else dont. // do not use strings: "io.phcode.dev" "io.phcode.staging" "io.phcode" for anything other than bundle identifiers @@ -431,7 +456,7 @@ fn main() { app.emit_all("single-instance", Payload { args: argv, cwd }).unwrap(); })) - .on_window_event(|event| process_window_event(&event)) + //.on_window_event(|event| process_window_event(&event)) .invoke_handler(tauri::generate_handler![ get_mac_deep_link_requests, get_process_id, toggle_devtools, console_log, console_error, _get_commandline_args, get_current_working_dir, @@ -439,8 +464,7 @@ fn main() { put_item, get_item, get_all_items, delete_item, _get_windows_drives, _rename_path, show_in_folder, move_to_trash, zoom_window, _get_clipboard_files, _open_url_in_browser_win]) - .setup(|app| { - init::init_app(app); + .setup(move |_app| { #[cfg(target_os = "linux")] { // In linux, f10 key press events are reserved for gtk-menu-bar-accel and not passed. @@ -471,6 +495,6 @@ fn main() { } Ok(()) }) - .run(tauri::generate_context!()) + .run(context) .expect("error while running tauri application"); } diff --git a/src-tauri/src/utilities.rs b/src-tauri/src/utilities.rs index 3458647d..4ad1c9bc 100644 --- a/src-tauri/src/utilities.rs +++ b/src-tauri/src/utilities.rs @@ -53,11 +53,15 @@ pub fn read_json_file(path: &PathBuf) -> Option { let mut contents = String::new(); if let Err(_) = file.read_to_string(&mut contents) { + eprintln!("Failed to read file: {}", path.display()); return None; // Error reading the file } match serde_json::from_str(&contents) { Ok(data) => Some(data), - Err(_) => None, // JSON parsing error + Err(err) => { + eprintln!("Failed to parse JSON in {}: {}", path.display(), err); + None + } } } diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index c4776b42..d30ad1c0 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -484,4 +484,4 @@ } ] } -} \ No newline at end of file +}