Skip to content

fix: interim releif for dark theme white flash on startup #593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 29 additions & 45 deletions src-tauri/src/boot_config.rs
Original file line number Diff line number Diff line change
@@ -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<tauri::Config>,
pub app_local_data_dir: std::path::PathBuf
}
pub static APP_CONSTANTS: OnceCell<AppConstants> = 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<PathBuf>`.
/// If `None` is provided, it returns a default `BootConfig`.
pub fn read_boot_config(base_path: &Option<PathBuf>) -> 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
})
}
27 changes: 0 additions & 27 deletions src-tauri/src/init.rs

This file was deleted.

48 changes: 36 additions & 12 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<PathBuf> = 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
Expand Down Expand Up @@ -431,16 +456,15 @@ 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,
_get_window_labels,
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.
Expand Down Expand Up @@ -471,6 +495,6 @@ fn main() {
}
Ok(())
})
.run(tauri::generate_context!())
.run(context)
.expect("error while running tauri application");
}
6 changes: 5 additions & 1 deletion src-tauri/src/utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ pub fn read_json_file(path: &PathBuf) -> Option<Value> {

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
}
}
}
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -484,4 +484,4 @@
}
]
}
}
}
Loading