diff --git a/Cargo.lock b/Cargo.lock index ed8eb11..3cc2cd6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2993,6 +2993,7 @@ dependencies = [ "mdns-sd", "serde", "serde_json", + "shared_constants", "tauri", "tauri-build", "tauri-plugin-clipboard-manager", @@ -3019,6 +3020,7 @@ dependencies = [ "log", "serde", "serde-wasm-bindgen", + "shared_constants", "strsim", "tauri-sys", "thaw", @@ -4910,6 +4912,10 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "shared_constants" +version = "0.1.0" + [[package]] name = "shlex" version = "1.3.0" diff --git a/Cargo.toml b/Cargo.toml index 97be9f9..35beb54 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,7 @@ tauri-sys = { git = "https://github.com/JonasKruckenberg/tauri-sys", branch = "v thaw = { version = "0.3.4", features = ["csr"] } thaw_utils = { version = "0.0.6", features = ["csr"] } tokio = "1.40.0" +shared_constants = { path = "./shared_constants" } [workspace] -members = ["src-tauri"] +members = ["shared_constants", "src-tauri"] diff --git a/shared_constants/Cargo.toml b/shared_constants/Cargo.toml new file mode 100644 index 0000000..21fc6af --- /dev/null +++ b/shared_constants/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "shared_constants" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/shared_constants/src/lib.rs b/shared_constants/src/lib.rs new file mode 100644 index 0000000..1cff5e9 --- /dev/null +++ b/shared_constants/src/lib.rs @@ -0,0 +1,6 @@ +use std::time::Duration; +pub const MDNS_SD_META_SERVICE: &str = "_services._dns-sd._udp.local."; +pub const METRICS_CHECK_INTERVAL: Duration = Duration::from_secs(1); +pub const SPLASH_SCREEN_DURATION: Duration = Duration::from_secs(2); +pub const AUTO_COMPLETE_AUTO_FOCUS_DELAY: Duration = Duration::from_secs(5); +pub const SHOW_NO_UPDATE_DURATION: Duration = Duration::from_secs(3); diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 8894c7a..db97cf6 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -23,6 +23,7 @@ serde_json = "1.0.128" tauri = { version = "2.0.0", features = [] } tauri-plugin-clipboard-manager = "2.0.0" tauri-plugin-shell = "2" +shared_constants = { path = "../shared_constants" } [lib] name = "mdns_browser_lib" diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 4d0b441..67563cc 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -4,13 +4,16 @@ use clap::builder::TypedValueParser as _; use clap::Parser; use mdns_sd::{ServiceDaemon, ServiceEvent, ServiceInfo}; use serde::Serialize; + +#[cfg(not(debug_assertions))] +use shared_constants::SPLASH_SCREEN_DURATION; +use shared_constants::{MDNS_SD_META_SERVICE, METRICS_CHECK_INTERVAL}; use std::{ collections::HashMap, net::IpAddr, sync::{Arc, Mutex}, - time::{Duration, SystemTime}, + time::SystemTime, }; - use tauri::Emitter; use tauri::{AppHandle, Manager, State, Window}; use tauri_plugin_clipboard_manager::ClipboardExt; @@ -144,15 +147,13 @@ type ServiceFoundEvent = ServiceRemovedEvent; type ServiceTypeFoundEvent = SearchStartedEvent; type ServiceTypeRemovedEvent = SearchStartedEvent; -const META_SERVICE: &str = "_services._dns-sd._udp.local."; - #[tauri::command] fn browse_types(window: Window, state: State) { if let Ok(mdns) = state.daemon.lock() { let mdns_for_thread = mdns.clone(); std::thread::spawn(move || { let receiver = mdns_for_thread - .browse(META_SERVICE) + .browse(MDNS_SD_META_SERVICE) .expect("Failed to browse"); while let Ok(event) = receiver.recv() { match event { @@ -179,7 +180,7 @@ fn browse_types(window: Window, state: State) { .expect("To emit"); } ServiceEvent::SearchStopped(service_type) => { - if service_type == META_SERVICE { + if service_type == MDNS_SD_META_SERVICE { break; } } @@ -272,15 +273,13 @@ fn browse(service_type: String, window: Window, state: State) { } } -const METRIC_CHECK_INTERVAL: Duration = Duration::from_secs(1); - #[tauri::command] fn send_metrics(window: Window, state: State) { if let Ok(mdns) = state.daemon.lock() { let mdns_for_thread = mdns.clone(); let mut old_metrics = HashMap::new(); std::thread::spawn(move || loop { - std::thread::sleep(METRIC_CHECK_INTERVAL); + std::thread::sleep(METRICS_CHECK_INTERVAL); if let Ok(metrics_receiver) = mdns_for_thread.get_metrics() { if let Ok(metrics) = metrics_receiver.recv() { if old_metrics != metrics { @@ -543,7 +542,7 @@ pub fn run() { let main_window = app.get_webview_window("main").unwrap(); tauri::async_runtime::spawn(async move { #[cfg(not(debug_assertions))] - tokio::time::sleep(Duration::from_millis(2000)).await; + tokio::time::sleep(SPLASH_SCREEN_DURATION).await; splashscreen_window.close().unwrap(); main_window.show().unwrap(); #[cfg(debug_assertions)] diff --git a/src/app.rs b/src/app.rs index 7baaafd..c369d0d 100644 --- a/src/app.rs +++ b/src/app.rs @@ -5,6 +5,9 @@ use leptos::*; use leptos_meta::provide_meta_context; use leptos_meta::Style; use serde::{Deserialize, Serialize}; +use shared_constants::{ + AUTO_COMPLETE_AUTO_FOCUS_DELAY, SHOW_NO_UPDATE_DURATION, SPLASH_SCREEN_DURATION, +}; use std::{ collections::{HashMap, HashSet}, fmt::Display, @@ -330,7 +333,7 @@ fn AutoCompleteServiceType( comp.focus(); } }, - std::time::Duration::from_millis(5000), + SPLASH_SCREEN_DURATION + AUTO_COMPLETE_AUTO_FOCUS_DELAY, ); }); }); @@ -833,7 +836,7 @@ pub fn About() -> impl IntoView { move || { show_no_update.set(false); }, - std::time::Duration::new(3, 0), + SHOW_NO_UPDATE_DURATION, ); };