Skip to content

Commit

Permalink
feat: refactor configs dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
hcavarsan committed Jun 21, 2024
1 parent 4880052 commit c5ffbee
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
10 changes: 7 additions & 3 deletions crates/kftray-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,15 @@ fn main() {
is_pinned: is_pinned.clone(),
runtime: runtime.clone(),
})
.manage(http_log_state.clone()) // Ensure the state is shared
.manage(http_log_state.clone())
.setup(move |app| {
alert_multiple_configs();
let _ = config::clean_all_custom_hosts_entries();
let app_handle = app.app_handle();

tauri::async_runtime::spawn(async move {
alert_multiple_configs(app_handle).await;
});

let _ = config::clean_all_custom_hosts_entries();
let _ = db::init();

if let Err(e) = config::migrate_configs() {
Expand Down
37 changes: 24 additions & 13 deletions crates/kftray-tauri/src/utils/validate_configs.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
extern crate dirs;
extern crate native_dialog;
extern crate tauri;

use std::env;
use std::path::PathBuf;

use native_dialog::MessageDialog;
use native_dialog::MessageType;

use tauri::api::dialog::{
MessageDialogBuilder,
MessageDialogKind,
};
use tauri::{
async_runtime::spawn_blocking,
AppHandle,
};
#[derive(Clone)]
struct ConfigLocation {
path: PathBuf,
Expand Down Expand Up @@ -59,7 +64,9 @@ fn detect_multiple_configs() -> (Vec<ConfigLocation>, Option<ConfigLocation>) {
(config_locations, active_config)
}

fn show_alert_dialog(configs: Vec<ConfigLocation>, active_config: Option<ConfigLocation>) {
async fn show_alert_dialog(
app_handle: AppHandle, configs: Vec<ConfigLocation>, active_config: Option<ConfigLocation>,
) {
let msg = configs
.into_iter()
.map(|config| format!(" * {}: {}", config.origin, config.path.display()))
Expand Down Expand Up @@ -96,17 +103,21 @@ fn show_alert_dialog(configs: Vec<ConfigLocation>, active_config: Option<ConfigL
dirs::home_dir().map_or("<home_directory_not_found>".to_string(), |p| p.join(".kftray").display().to_string())
);

MessageDialog::new()
.set_type(MessageType::Warning)
.set_title("Multiple Configuration Directories Detected")
.set_text(&full_message)
.show_alert()
.unwrap();
// Use the app handle to trigger the dialog on the main thread
spawn_blocking(move || {
let _ = app_handle.run_on_main_thread(move || {
MessageDialogBuilder::new("Multiple Configuration Directories Detected", &full_message)
.kind(MessageDialogKind::Warning)
.show(|_| {});
});
})
.await
.unwrap();
}

pub fn alert_multiple_configs() {
pub async fn alert_multiple_configs(app_handle: AppHandle) {
let (configs, active_config) = detect_multiple_configs();
if configs.len() > 1 {
show_alert_dialog(configs, active_config);
show_alert_dialog(app_handle, configs, active_config).await;
}
}

0 comments on commit c5ffbee

Please sign in to comment.