Skip to content

Commit

Permalink
Enable window hiding for Windows and X11
Browse files Browse the repository at this point in the history
Hopefully nothing goes wrong, said everyone ever
  • Loading branch information
4JX committed Jan 25, 2025
1 parent 49c3609 commit 83753d5
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 46 deletions.
1 change: 0 additions & 1 deletion app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 10 additions & 2 deletions app/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
profile::{self, Profile},
ManagerCreationError,
},
IS_WAYLAND,
};

#[macro_export]
Expand Down Expand Up @@ -146,7 +147,7 @@ pub fn try_cli() -> Result<GuiCommand, CliError> {

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 })
Expand Down Expand Up @@ -222,7 +223,14 @@ fn parse_cli() -> Result<CliOutput, CliError> {
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:");
Expand Down
9 changes: 4 additions & 5 deletions app/src/gui/menu_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<GuiMessage>,
load_profile_dialog: FileDialog,
load_effect_dialog: FileDialog,
Expand Down Expand Up @@ -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")]
{
Expand Down
43 changes: 21 additions & 22 deletions app/src/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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>) {
Expand Down Expand Up @@ -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
}
}
}
}
5 changes: 5 additions & 0 deletions app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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<bool> = LazyLock::new(|| std::env::var("WAYLAND_DISPLAY").is_ok());
#[cfg(not(target_os = "linux"))]
pub static IS_WAYLAND: LazyLock<bool> = LazyLock::new(|| false);

fn main() {
#[cfg(target_os = "windows")]
Expand Down
11 changes: 5 additions & 6 deletions app/src/tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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
}
Expand Down
18 changes: 9 additions & 9 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit 83753d5

Please sign in to comment.