Skip to content

Commit 971b4a8

Browse files
committed
fix: interim releif for dark theme white flash on startup
1 parent 2782cd9 commit 971b4a8

File tree

4 files changed

+56
-40
lines changed

4 files changed

+56
-40
lines changed

src-tauri/src/boot_config.rs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ pub static APP_CONSTANTS: OnceCell<AppConstants> = OnceCell::new();
1515

1616
#[derive(Serialize)]
1717
pub struct BootConfig {
18-
pub version: u32
18+
pub version: u32,
19+
pub start_as_hidden_window: bool,
1920
}
2021
static BOOT_CONFIG_FILE_NAME: &'static str = "boot_config.json";
2122

@@ -26,15 +27,17 @@ fn get_boot_config_file_path(app_local_data_dir: &PathBuf) -> PathBuf {
2627
}
2728

2829
fn _set_boot_config(boot_config: &mut BootConfig, value: &Value) {
29-
boot_config.version = match value["version"].as_u64() {
30-
Some(value) => value as u32,
31-
None => 0
32-
};
30+
boot_config.version = value["version"].as_u64().map(|v| v as u32).unwrap_or(0);
31+
32+
boot_config.start_as_hidden_window = value["start_as_hidden_window"]
33+
.as_bool()
34+
.unwrap_or(false); // Default to `false` if missing or invalid
3335
}
3436

3537
pub fn read_boot_config() -> BootConfig {
3638
let mut boot_config = BootConfig {
37-
version: 1
39+
version: 1,
40+
start_as_hidden_window: false
3841
};
3942
if let Some(app_constants) = APP_CONSTANTS.get() {
4043
let boot_config_file_path = get_boot_config_file_path(&app_constants.app_local_data_dir);
@@ -50,20 +53,4 @@ pub fn read_boot_config() -> BootConfig {
5053
return boot_config;
5154
}
5255

53-
fn _write_boot_config(boot_config: &BootConfig) {
54-
if let Some(app_constants) = APP_CONSTANTS.get() {
55-
let boot_config_file_path = get_boot_config_file_path(&app_constants.app_local_data_dir);
56-
// Convert the BootConfig struct to JSON
57-
let json_string = serde_json::to_string(boot_config).unwrap();
58-
let mut file = File::create(boot_config_file_path).expect("Failed to create file");
59-
file.write_all(json_string.as_bytes())
60-
.expect("Failed to write to boot config file");
61-
}
62-
}
63-
64-
// WARNING: If there are multiple windows, this will be called on each window close.
65-
pub fn write_boot_config(version: u32) {
66-
_write_boot_config(&BootConfig {
67-
version
68-
})
69-
}
56+
// writing the boot config is always done from js to be simpler, as js is eazier to handle json.

src-tauri/src/init.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ use crate::utilities::ensure_dir_exists;
22
use crate::boot_config::read_boot_config;
33
use crate::boot_config::APP_CONSTANTS;
44
use crate::boot_config::AppConstants;
5+
use crate::boot_config::BootConfig;
56

6-
pub fn init_app(app: &mut tauri::App) {
7+
pub fn init_app(app: &mut tauri::App) -> BootConfig {
78
let config = app.config().clone();
89
println!("Appdata path is {}", tauri::api::path::app_local_data_dir(&config).expect("failed to retrieve app_local_data_dir").display());
910
ensure_dir_exists(&tauri::api::path::app_local_data_dir(&config).unwrap()); // canonicalize will work only if path exists
@@ -14,14 +15,31 @@ pub fn init_app(app: &mut tauri::App) {
1415
});
1516

1617
// To get a value
17-
if let Some(app_constants) = APP_CONSTANTS.get() {
18-
#[cfg(debug_assertions)]{
19-
println!("Bundle ID is {}", app_constants.tauri_config.tauri.bundle.identifier);
18+
let boot_config = if let Some(app_constants) = APP_CONSTANTS.get() {
19+
#[cfg(debug_assertions)]
20+
{
21+
println!(
22+
"Bundle ID is {}",
23+
app_constants.tauri_config.tauri.bundle.identifier
24+
);
2025
}
2126
ensure_dir_exists(&app_constants.app_local_data_dir);
22-
read_boot_config();
23-
#[cfg(debug_assertions)]{
24-
println!("Bootconfig version is {}", read_boot_config().version);
27+
let boot_config = read_boot_config();
28+
29+
#[cfg(debug_assertions)]
30+
{
31+
println!("Bootconfig version is {}", boot_config.version);
32+
println!("start as hidden: {}", boot_config.start_as_hidden_window);
33+
}
34+
35+
boot_config
36+
} else {
37+
// Return a default boot config if APP_CONSTANTS is not set
38+
BootConfig {
39+
version: 1,
40+
start_as_hidden_window: false,
2541
}
26-
}
42+
};
43+
44+
boot_config
2745
}

src-tauri/src/main.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,12 @@ fn zoom_window(window: tauri::Window, scale_factor: f64) {
257257
});
258258
}
259259

260-
fn process_window_event(event: &GlobalWindowEvent) {
261-
if let tauri::WindowEvent::CloseRequested { .. } = event.event() {
262-
// this does nothing and is here if in future you need to persist something on window close.
263-
boot_config::write_boot_config(1);
264-
}
265-
}
260+
// here in case you need to process windows events
261+
// fn process_window_event(event: &GlobalWindowEvent) {
262+
// if let tauri::WindowEvent::CloseRequested { .. } = event.event() {
263+
// // this does nothing and is here if in future you need to persist something on window close.
264+
// }
265+
// }
266266

267267
// convert url of form "protocol://host/v1.2.3/path/to/something" to "protocol://host/path/to/something"
268268
fn remove_version_from_url(url: &str) -> String {
@@ -431,7 +431,7 @@ fn main() {
431431

432432
app.emit_all("single-instance", Payload { args: argv, cwd }).unwrap();
433433
}))
434-
.on_window_event(|event| process_window_event(&event))
434+
//.on_window_event(|event| process_window_event(&event))
435435
.invoke_handler(tauri::generate_handler![
436436
get_mac_deep_link_requests, get_process_id,
437437
toggle_devtools, console_log, console_error, _get_commandline_args, get_current_working_dir,
@@ -440,7 +440,14 @@ fn main() {
440440
_get_windows_drives, _rename_path, show_in_folder, move_to_trash, zoom_window,
441441
_get_clipboard_files, _open_url_in_browser_win])
442442
.setup(|app| {
443-
init::init_app(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+
444451
#[cfg(target_os = "linux")]
445452
{
446453
// In linux, f10 key press events are reserved for gtk-menu-bar-accel and not passed.

src-tauri/src/utilities.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,15 @@ pub fn read_json_file(path: &PathBuf) -> Option<Value> {
5353

5454
let mut contents = String::new();
5555
if let Err(_) = file.read_to_string(&mut contents) {
56+
eprintln!("Failed to read file: {}", path.display());
5657
return None; // Error reading the file
5758
}
5859

5960
match serde_json::from_str(&contents) {
6061
Ok(data) => Some(data),
61-
Err(_) => None, // JSON parsing error
62+
Err(err) => {
63+
eprintln!("Failed to parse JSON in {}: {}", path.display(), err);
64+
None
65+
}
6266
}
6367
}

0 commit comments

Comments
 (0)