From cba8c58af78580786ff5c63d48bb986bbcd90f2d Mon Sep 17 00:00:00 2001 From: Ultradesu Date: Tue, 22 Jul 2025 23:14:55 +0300 Subject: [PATCH] UI code reworked --- Cargo.toml | 17 +++++++++++------ src/gui/mod.rs | 19 +++++++++++++++++-- src/gui/tray/app.rs | 6 ++++++ src/main.rs | 17 ++++++++++++++--- 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a83d38d..f89286d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,13 +27,18 @@ trust-dns-resolver = "0.23" futures = "0.3" hostname = "0.3" rust-embed = "8.0" -tray-icon = "0.19" -notify = "6.1" -notify-debouncer-mini = "0.4" +tray-icon = { version = "0.19", optional = true } +notify = { version = "6.1", optional = true } +notify-debouncer-mini = { version = "0.4", optional = true } dirs = "5.0" -eframe = "0.29" -egui = "0.29" -winit = "0.30" +eframe = { version = "0.29", optional = true } +egui = { version = "0.29", optional = true } +winit = { version = "0.30", optional = true } env_logger = "0.11" urlencoding = "2.1" +[features] +default = ["gui"] +gui = ["tray-icon", "eframe", "egui", "winit", "notify", "notify-debouncer-mini"] +server = [] + diff --git a/src/gui/mod.rs b/src/gui/mod.rs index 7c25092..da9ba4c 100644 --- a/src/gui/mod.rs +++ b/src/gui/mod.rs @@ -1,28 +1,43 @@ use log::info; -use tray_icon::menu::MenuEvent; // Modules mod api; mod admin; mod common; + +#[cfg(feature = "gui")] mod settings; +#[cfg(feature = "gui")] mod tray; // Re-exports for backward compatibility and external usage +#[cfg(feature = "gui")] pub use settings::run_settings_window; +#[cfg(feature = "gui")] pub use tray::run_tray_app; // User events for GUI communication +#[cfg(feature = "gui")] #[derive(Debug)] pub enum UserEvent { TrayIconEvent, - MenuEvent(MenuEvent), + MenuEvent(tray_icon::menu::MenuEvent), ConfigFileChanged, UpdateMenu, } /// Run GUI application in tray mode +#[cfg(feature = "gui")] pub async fn run_gui() -> std::io::Result<()> { info!("Starting KHM tray application"); run_tray_app().await } + +/// Stub function when GUI is disabled +#[cfg(not(feature = "gui"))] +pub async fn run_gui() -> std::io::Result<()> { + return Err(std::io::Error::new( + std::io::ErrorKind::Unsupported, + "GUI features not compiled. Install system dependencies and rebuild with --features gui" + )); +} diff --git a/src/gui/tray/app.rs b/src/gui/tray/app.rs index 801613d..b02d738 100644 --- a/src/gui/tray/app.rs +++ b/src/gui/tray/app.rs @@ -1,5 +1,8 @@ use log::{error, info}; + +#[cfg(feature = "gui")] use notify::RecursiveMode; +#[cfg(feature = "gui")] use notify_debouncer_mini::{new_debouncer, DebounceEventResult}; use std::sync::{Arc, Mutex}; use std::time::Duration; @@ -24,6 +27,7 @@ pub struct TrayApplication { menu_ids: Option, settings: Arc>, sync_status: Arc>, + #[cfg(feature = "gui")] _debouncer: Option>, proxy: EventLoopProxy, auto_sync_handle: Option>, @@ -36,12 +40,14 @@ impl TrayApplication { menu_ids: None, settings: Arc::new(Mutex::new(load_settings())), sync_status: Arc::new(Mutex::new(SyncStatus::default())), + #[cfg(feature = "gui")] _debouncer: None, proxy, auto_sync_handle: None, } } + #[cfg(feature = "gui")] fn setup_file_watcher(&mut self) { let config_path = get_config_path(); let (tx, rx) = std::sync::mpsc::channel::(); diff --git a/src/main.rs b/src/main.rs index 067a262..7488926 100644 --- a/src/main.rs +++ b/src/main.rs @@ -154,9 +154,20 @@ async fn main() -> std::io::Result<()> { // Settings UI mode - just show settings window and exit if args.settings_ui { - info!("Running settings UI window"); - gui::run_settings_window(); - return Ok(()); + #[cfg(feature = "gui")] + { + info!("Running settings UI window"); + gui::run_settings_window(); + return Ok(()); + } + #[cfg(not(feature = "gui"))] + { + error!("GUI features not compiled. Install system dependencies and rebuild with --features gui"); + return Err(std::io::Error::new( + std::io::ErrorKind::Unsupported, + "GUI features not compiled" + )); + } } // GUI mode has priority