Skip to content

Commit 7e055f2

Browse files
committed
refactor: hidden window is noe enabled rn due to slow boot noted
1 parent 971b4a8 commit 7e055f2

File tree

4 files changed

+52
-83
lines changed

4 files changed

+52
-83
lines changed

src-tauri/src/boot_config.rs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,18 @@
1-
use std::sync::Arc;
2-
use once_cell::sync::OnceCell;
31
use serde_json::Value;
42
use serde::Serialize;
53
use crate::utilities::read_json_file;
6-
use std::path::PathBuf;
7-
use std::fs::File;
8-
use std::io::Write;
9-
10-
pub struct AppConstants {
11-
pub tauri_config : Arc<tauri::Config>,
12-
pub app_local_data_dir: std::path::PathBuf
13-
}
14-
pub static APP_CONSTANTS: OnceCell<AppConstants> = OnceCell::new();
4+
use std::path::{Path, PathBuf};
155

166
#[derive(Serialize)]
177
pub struct BootConfig {
188
pub version: u32,
199
pub start_as_hidden_window: bool,
2010
}
21-
static BOOT_CONFIG_FILE_NAME: &'static str = "boot_config.json";
2211

23-
fn get_boot_config_file_path(app_local_data_dir: &PathBuf) -> PathBuf {
24-
let mut config_file_path = app_local_data_dir.clone();
25-
config_file_path.push(BOOT_CONFIG_FILE_NAME);
26-
return config_file_path;
12+
fn get_boot_config_file_path(base_path: &Path) -> PathBuf {
13+
let mut config_file_path = base_path.to_path_buf();
14+
config_file_path.push("boot_config.json");
15+
config_file_path
2716
}
2817

2918
fn _set_boot_config(boot_config: &mut BootConfig, value: &Value) {
@@ -34,23 +23,31 @@ fn _set_boot_config(boot_config: &mut BootConfig, value: &Value) {
3423
.unwrap_or(false); // Default to `false` if missing or invalid
3524
}
3625

37-
pub fn read_boot_config() -> BootConfig {
26+
/// Reads boot_config.json from the given `Option<PathBuf>`.
27+
/// If `None` is provided, it returns a default `BootConfig`.
28+
pub fn read_boot_config(base_path: &Option<PathBuf>) -> BootConfig {
3829
let mut boot_config = BootConfig {
3930
version: 1,
40-
start_as_hidden_window: false
31+
start_as_hidden_window: false,
4132
};
42-
if let Some(app_constants) = APP_CONSTANTS.get() {
43-
let boot_config_file_path = get_boot_config_file_path(&app_constants.app_local_data_dir);
33+
34+
if let Some(ref path) = base_path {
35+
let boot_config_file_path = get_boot_config_file_path(path);
36+
4437
match read_json_file(&boot_config_file_path) {
45-
Some(value) =>{
38+
Some(value) => {
4639
_set_boot_config(&mut boot_config, &value);
4740
}
4841
None => {
49-
eprintln!("No boot restore config file found {}", boot_config_file_path.display());
42+
eprintln!(
43+
"No boot restore config file found at {}",
44+
boot_config_file_path.display()
45+
);
5046
}
5147
}
48+
} else {
49+
eprintln!("Base path is None, using default boot config.");
5250
}
53-
return boot_config;
54-
}
5551

56-
// writing the boot config is always done from js to be simpler, as js is eazier to handle json.
52+
boot_config
53+
}

src-tauri/src/init.rs

Lines changed: 0 additions & 45 deletions
This file was deleted.

src-tauri/src/main.rs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ use native_dialog::{MessageDialog, MessageType};
3636
use regex::Regex;
3737
extern crate percent_encoding;
3838
use tauri::http::ResponseBuilder;
39-
use tauri::GlobalWindowEvent;
40-
mod init;
39+
use tauri::api::path::app_local_data_dir;
40+
use tauri::generate_context;
41+
use crate::utilities::ensure_dir_exists;
42+
use crate::boot_config::read_boot_config;
4143
mod bugsnag;
4244
mod utilities;
4345
mod boot_config;
@@ -258,6 +260,7 @@ fn zoom_window(window: tauri::Window, scale_factor: f64) {
258260
}
259261

260262
// here in case you need to process windows events
263+
// use tauri::GlobalWindowEvent;
261264
// fn process_window_event(event: &GlobalWindowEvent) {
262265
// if let tauri::WindowEvent::CloseRequested { .. } = event.event() {
263266
// // this does nothing and is here if in future you need to persist something on window close.
@@ -366,6 +369,28 @@ fn main() {
366369
tauri_plugin_deep_link::prepare("io.phcode");
367370
}
368371

372+
let context = generate_context!();
373+
let mut tauri_config = context.config().clone(); // Clone the config to modify it
374+
375+
// Get the app data directory before Tauri starts
376+
let app_data_dir: Option<PathBuf> = app_local_data_dir(&tauri_config);
377+
378+
if let Some(dir) = &app_data_dir {
379+
println!("App Data Directory: {}", dir.display());
380+
ensure_dir_exists(dir);
381+
} else {
382+
eprintln!("Failed to retrieve app data directory.");
383+
}
384+
385+
let boot_config = read_boot_config(&app_data_dir);
386+
387+
// Modify the first window's visibility based on boot_config.start_as_hidden_window
388+
if let Some(first_window) = tauri_config.tauri.windows.get_mut(0) {
389+
if !boot_config.start_as_hidden_window {
390+
first_window.visible = true;
391+
}
392+
}
393+
369394
// warning: any string that resembles the following strings will be rewritten in source in prod by build scripts.
370395
// This is so that app bundle IDs are correct. IF they are app bundle IDs use the strings. else dont.
371396
// do not use strings: "io.phcode.dev" "io.phcode.staging" "io.phcode" for anything other than bundle identifiers
@@ -439,15 +464,7 @@ fn main() {
439464
put_item, get_item, get_all_items, delete_item,
440465
_get_windows_drives, _rename_path, show_in_folder, move_to_trash, zoom_window,
441466
_get_clipboard_files, _open_url_in_browser_win])
442-
.setup(|app| {
443-
let boot_config = init::init_app(app);
444-
445-
if boot_config.start_as_hidden_window {
446-
if let Some(main_window) = app.get_window("main") {
447-
main_window.hide().expect("Failed to hide main window");
448-
}
449-
}
450-
467+
.setup(move |_app| {
451468
#[cfg(target_os = "linux")]
452469
{
453470
// In linux, f10 key press events are reserved for gtk-menu-bar-accel and not passed.
@@ -478,6 +495,6 @@ fn main() {
478495
}
479496
Ok(())
480497
})
481-
.run(tauri::generate_context!())
498+
.run(context)
482499
.expect("error while running tauri application");
483500
}

src-tauri/tauri.conf.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,4 +484,4 @@
484484
}
485485
]
486486
}
487-
}
487+
}

0 commit comments

Comments
 (0)