From 83753d5710f07e724512123bddefe2a828525bd6 Mon Sep 17 00:00:00 2001 From: 4JX <4JXcYvmyu3Hz8fV@protonmail.com> Date: Sat, 25 Jan 2025 15:49:39 +0100 Subject: [PATCH] Enable window hiding for Windows and X11 Hopefully nothing goes wrong, said everyone ever --- app/Cargo.toml | 1 - app/src/cli.rs | 12 ++++++++++-- app/src/gui/menu_bar.rs | 9 ++++----- app/src/gui/mod.rs | 43 ++++++++++++++++++++--------------------- app/src/main.rs | 5 +++++ app/src/tray.rs | 11 +++++------ flake.lock | 18 ++++++++--------- flake.nix | 2 +- 8 files changed, 55 insertions(+), 46 deletions(-) diff --git a/app/Cargo.toml b/app/Cargo.toml index 6e750c4..e408ab0 100644 --- a/app/Cargo.toml +++ b/app/Cargo.toml @@ -17,7 +17,6 @@ clap = { version = "4.5.23", features = ["color", "cargo", "derive"] } # Ui eframe = { version = "0.30.0", features = ["x11", "wayland"] } -# tokio = { version = "1.41.1" } egui-modal = "0.6.0" # egui-modal = { git = "https://github.com/n00kii/egui-modal", rev = "8443238" } egui_file = "0.20.0" diff --git a/app/src/cli.rs b/app/src/cli.rs index e88dc03..6f1a58c 100644 --- a/app/src/cli.rs +++ b/app/src/cli.rs @@ -13,6 +13,7 @@ use crate::{ profile::{self, Profile}, ManagerCreationError, }, + IS_WAYLAND, }; #[macro_export] @@ -146,7 +147,7 @@ pub fn try_cli() -> Result { match output_type { CliOutput::Gui { hide_window, output_type } => { - if hide_window { + if !*IS_WAYLAND && hide_window { println!("Window hiding is currently not supported. See https://github.com/4JX/L5P-Keyboard-RGB/issues/181"); } Ok(GuiCommand::Start { hide_window, output_type }) @@ -222,7 +223,14 @@ fn parse_cli() -> Result { profile.save_profile(&filename).expect("Failed to save."); } - return Ok(CliOutput::Cli(OutputType::Profile(profile))); + if cli.gui { + return Ok(CliOutput::Gui { + hide_window: cli.hide_window, + output_type: OutputType::Profile(profile), + }); + } else { + return Ok(CliOutput::Cli(OutputType::Profile(profile))); + } } Commands::List => { println!("List of available effects:"); diff --git a/app/src/gui/menu_bar.rs b/app/src/gui/menu_bar.rs index f9caf3e..894ed0d 100644 --- a/app/src/gui/menu_bar.rs +++ b/app/src/gui/menu_bar.rs @@ -10,13 +10,12 @@ use std::{path::PathBuf, time::Duration}; use crate::{ gui::modals, manager::{custom_effect::CustomEffect, profile::Profile}, + IS_WAYLAND, }; use super::{GuiMessage, LoadedEffect}; pub struct MenuBarState { - // TODO: Re-enable when upstream fixes window visibility - #[allow(dead_code)] gui_sender: Sender, load_profile_dialog: FileDialog, load_effect_dialog: FileDialog, @@ -132,9 +131,9 @@ impl MenuBarState { open::that("https://www.buymeacoffee.com/4JXdev").unwrap(); } - // if ui.button("Exit").clicked() { - // self.gui_sender.send(GuiMessage::Quit).unwrap(); - // } + if !*IS_WAYLAND && ui.button("Exit").clicked() { + self.gui_sender.send(GuiMessage::Quit).unwrap(); + } #[cfg(target_os = "windows")] { diff --git a/app/src/gui/mod.rs b/app/src/gui/mod.rs index 512a8eb..be7e2b5 100644 --- a/app/src/gui/mod.rs +++ b/app/src/gui/mod.rs @@ -25,6 +25,7 @@ use crate::{ manager::{self, custom_effect::CustomEffect, profile::Profile, EffectManager, ManagerCreationError}, persist::Settings, tray::{QUIT_ID, SHOW_ID}, + IS_WAYLAND, }; use self::{menu_bar::MenuBarState, saved_items::SavedItems, style::Theme}; @@ -153,7 +154,9 @@ impl App { } pub fn init(self, cc: &CreationContext<'_>) -> Self { - // cc.egui_ctx.send_viewport_cmd(ViewportCommand::Visible(self.visible)); + if !*IS_WAYLAND { + cc.egui_ctx.send_viewport_cmd(ViewportCommand::Visible(self.visible.load(Ordering::SeqCst))); + } let egui_ctx = cc.egui_ctx.clone(); let gui_tx = self.gui_tx.clone(); @@ -218,8 +221,7 @@ impl eframe::App for App { // Show active toast messages self.toasts.show(ctx); - // TODO: Remove when upstream fixes window hiding - if !self.visible.load(Ordering::SeqCst) { + if *IS_WAYLAND && !self.visible.load(Ordering::SeqCst) { self.visible.store(true, Ordering::SeqCst); self.toasts .warning("Window hiding is currently not supported.\nSee https://github.com/4JX/L5P-Keyboard-RGB/issues/181") @@ -250,7 +252,7 @@ impl eframe::App for App { self.update_state(); } - // self.handle_close_request(ctx); + self.handle_close_request(ctx); } fn on_exit(&mut self, _gl: Option<&eframe::glow::Context>) { @@ -400,22 +402,19 @@ impl App { self.state_changed = false; } - // fn handle_close_request(&mut self, ctx: &Context) { - // if ctx.input(|i| i.viewport().close_requested()) { - // #[cfg(target_os = "linux")] - // let is_wayland = std::env::var("WAYLAND_DISPLAY").is_ok(); - // #[cfg(target_os = "linux")] - // if is_wayland { - // // Force close normally on wayland - // return; - // } - - // if self.has_tray.load(Ordering::Relaxed) { - // ctx.send_viewport_cmd(ViewportCommand::CancelClose); - // ctx.send_viewport_cmd(ViewportCommand::Visible(false)); - // } else { - // // Close normally - // } - // } - // } + fn handle_close_request(&mut self, ctx: &Context) { + if ctx.input(|i| i.viewport().close_requested()) { + if *IS_WAYLAND { + // Force close normally on wayland + return; + } + + if self.has_tray.load(Ordering::Relaxed) { + ctx.send_viewport_cmd(ViewportCommand::CancelClose); + ctx.send_viewport_cmd(ViewportCommand::Visible(false)); + } else { + // Close normally + } + } + } } diff --git a/app/src/main.rs b/app/src/main.rs index e6bbaa4..69705ad 100644 --- a/app/src/main.rs +++ b/app/src/main.rs @@ -11,6 +11,7 @@ mod persist; mod tray; mod util; +use std::sync::LazyLock; #[cfg(not(target_os = "linux"))] use std::{cell::RefCell, rc::Rc}; @@ -26,6 +27,10 @@ use gui::App; const APP_ICON: &[u8; 14987] = include_bytes!("../res/trayIcon.ico"); const WINDOW_SIZE: Vec2 = Vec2::new(500., 400.); +#[cfg(target_os = "linux")] +pub static IS_WAYLAND: LazyLock = LazyLock::new(|| std::env::var("WAYLAND_DISPLAY").is_ok()); +#[cfg(not(target_os = "linux"))] +pub static IS_WAYLAND: LazyLock = LazyLock::new(|| false); fn main() { #[cfg(target_os = "windows")] diff --git a/app/src/tray.rs b/app/src/tray.rs index 786d5c9..7a3930a 100644 --- a/app/src/tray.rs +++ b/app/src/tray.rs @@ -3,7 +3,7 @@ use tray_icon::{ Icon, TrayIcon, TrayIconBuilder, }; -use crate::APP_ICON; +use crate::{APP_ICON, IS_WAYLAND}; pub const SHOW_ID: &str = "tray-show"; pub const QUIT_ID: &str = "tray-quit"; @@ -23,12 +23,11 @@ impl TrayMenuItems { } } -fn build_tray_menu(items: &TrayMenuItems, _has_gui: bool) -> Menu { +fn build_tray_menu(items: &TrayMenuItems, has_gui: bool) -> Menu { let menu = Menu::new(); - // TODO: Wait for upstream fix - // if has_gui { - // menu.append_items(&[&items.show]).unwrap(); - // } + if has_gui && !*IS_WAYLAND { + menu.append_items(&[&items.show]).unwrap(); + } menu.append_items(&[&items.quit]).unwrap(); menu } diff --git a/flake.lock b/flake.lock index bef2d28..4c1fca4 100644 --- a/flake.lock +++ b/flake.lock @@ -2,11 +2,11 @@ "nodes": { "crane": { "locked": { - "lastModified": 1734808813, - "narHash": "sha256-3aH/0Y6ajIlfy7j52FGZ+s4icVX0oHhqBzRdlOeztqg=", + "lastModified": 1737689766, + "narHash": "sha256-ivVXYaYlShxYoKfSo5+y5930qMKKJ8CLcAoIBPQfJ6s=", "owner": "ipetkov", "repo": "crane", - "rev": "72e2d02dbac80c8c86bf6bf3e785536acf8ee926", + "rev": "6fe74265bbb6d016d663b1091f015e2976c4a527", "type": "github" }, "original": { @@ -35,11 +35,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1735617354, - "narHash": "sha256-5zJyv66q68QZJZsXtmjDBazGnF0id593VSy+8eSckoo=", + "lastModified": 1737717945, + "narHash": "sha256-ET91TMkab3PmOZnqiJQYOtSGvSTvGeHoegAv4zcTefM=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "69b9a8c860bdbb977adfa9c5e817ccb717884182", + "rev": "ecd26a469ac56357fd333946a99086e992452b6a", "type": "github" }, "original": { @@ -64,11 +64,11 @@ ] }, "locked": { - "lastModified": 1735698720, - "narHash": "sha256-+skLL6mq/T7s6J5YmSp89ivQOHBPQ40GEU2n8yqp6bs=", + "lastModified": 1737771740, + "narHash": "sha256-lWIdF4qke63TdCHnJ0QaUHfG8YvsDrBqzL4jiHYQd+Y=", "owner": "oxalica", "repo": "rust-overlay", - "rev": "a00807363a8a6cae6c3fa84ff494bf9d96333674", + "rev": "cfaaa1dddd280af09aca84af84612fbccd986ae2", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 176e79c..5f0d78b 100644 --- a/flake.nix +++ b/flake.nix @@ -28,7 +28,7 @@ overlays = [ (import rust-overlay) ]; }; - rustVersion = "1.81.0"; + rustVersion = "1.84.0"; rust = pkgs.rust-bin.stable.${rustVersion}.default.override { extensions = [