Skip to content

Commit c70c7ee

Browse files
committed
fix: multiple tray icon related: #464
1 parent fe3d3df commit c70c7ee

File tree

4 files changed

+77
-55
lines changed

4 files changed

+77
-55
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/gpapi/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ clap-verbosity-flag = { workspace = true, optional = true }
3939
env_logger = { workspace = true, optional = true }
4040
log-reload = { version = "0.1", optional = true }
4141

42+
[target.'cfg(not(any(target_os="macos", target_os="windows")))'.dependencies]
43+
gtk = "0.18"
44+
4245
[features]
4346
tauri = ["dep:tauri"]
4447
clap = ["dep:clap", "dep:clap-verbosity-flag"]

crates/gpapi/src/utils/env_utils.rs

-6
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ pub fn patch_gui_runtime_env(hidpi: bool) {
4141
// This is to avoid blank screen on some systems
4242
std::env::set_var("WEBKIT_DISABLE_COMPOSITING_MODE", "1");
4343

44-
// Workaround for https://github.com/tauri-apps/tao/issues/929
45-
let is_wayland = std::env::var("XDG_SESSION_TYPE").unwrap_or_default() == "wayland";
46-
if is_wayland {
47-
env::set_var("GDK_BACKEND", "x11");
48-
}
49-
5044
if hidpi {
5145
info!("Setting GDK_SCALE=2 and GDK_DPI_SCALE=0.5");
5246
std::env::set_var("GDK_SCALE", "2");

crates/gpapi/src/utils/window.rs

+73-49
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,97 @@
1-
use std::{process::ExitStatus, time::Duration};
2-
3-
use anyhow::bail;
4-
use log::info;
51
use tauri::WebviewWindow;
6-
use tokio::process::Command;
72

83
pub trait WindowExt {
94
fn raise(&self) -> anyhow::Result<()>;
105
}
116

127
impl WindowExt for WebviewWindow {
8+
#[cfg(any(target_os = "macos", target_os = "windows"))]
139
fn raise(&self) -> anyhow::Result<()> {
14-
raise_window(self)
10+
self.show()?;
11+
Ok(())
12+
}
13+
14+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
15+
fn raise(&self) -> anyhow::Result<()> {
16+
unix::raise_window(self)
1517
}
1618
}
1719

18-
pub fn raise_window(win: &WebviewWindow) -> anyhow::Result<()> {
19-
let is_wayland = std::env::var("XDG_SESSION_TYPE").unwrap_or_default() == "wayland";
20+
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
21+
mod unix {
22+
use std::{process::ExitStatus, time::Duration};
2023

21-
if is_wayland {
22-
win.hide()?;
23-
win.show()?;
24-
} else {
25-
if !win.is_visible()? {
26-
win.show()?;
27-
}
28-
let title = win.title()?;
29-
tokio::spawn(async move {
30-
if let Err(err) = wmctrl_raise_window(&title).await {
31-
info!("Window not raised: {}", err);
24+
use anyhow::bail;
25+
use gtk::{
26+
glib::Cast,
27+
traits::{EventBoxExt, GtkWindowExt, WidgetExt},
28+
EventBox,
29+
};
30+
use log::info;
31+
use tauri::WebviewWindow;
32+
use tokio::process::Command;
33+
34+
pub fn raise_window(win: &WebviewWindow) -> anyhow::Result<()> {
35+
let is_wayland = std::env::var("XDG_SESSION_TYPE").unwrap_or_default() == "wayland";
36+
37+
if is_wayland {
38+
let gtk_win = win.gtk_window()?;
39+
if let Some(header) = gtk_win.titlebar() {
40+
let _ = header.downcast::<EventBox>().map(|event_box| {
41+
event_box.set_above_child(false);
42+
});
3243
}
33-
});
34-
}
3544

36-
// Calling window.show() on Windows will cause the menu to be shown.
37-
// We need to hide it again.
38-
win.hide_menu()?;
45+
gtk_win.hide();
46+
gtk_win.show_all();
47+
} else {
48+
if !win.is_visible()? {
49+
win.show()?;
50+
}
51+
let title = win.title()?;
52+
tokio::spawn(async move {
53+
if let Err(err) = wmctrl_raise_window(&title).await {
54+
info!("Window not raised: {}", err);
55+
}
56+
});
57+
}
3958

40-
Ok(())
41-
}
59+
// Calling window.show() on window object will cause the menu to be shown.
60+
// We need to hide it again.
61+
win.hide_menu()?;
62+
63+
Ok(())
64+
}
4265

43-
async fn wmctrl_raise_window(title: &str) -> anyhow::Result<()> {
44-
let mut counter = 0;
66+
async fn wmctrl_raise_window(title: &str) -> anyhow::Result<()> {
67+
let mut counter = 0;
4568

46-
loop {
47-
if let Ok(exit_status) = wmctrl_try_raise_window(title).await {
48-
if exit_status.success() {
49-
info!("Window raised after {} attempts", counter + 1);
50-
return Ok(());
69+
loop {
70+
if let Ok(exit_status) = wmctrl_try_raise_window(title).await {
71+
if exit_status.success() {
72+
info!("Window raised after {} attempts", counter + 1);
73+
return Ok(());
74+
}
5175
}
52-
}
5376

54-
if counter >= 10 {
55-
bail!("Failed to raise window: {}", title)
56-
}
77+
if counter >= 10 {
78+
bail!("Failed to raise window: {}", title)
79+
}
5780

58-
counter += 1;
59-
tokio::time::sleep(Duration::from_millis(100)).await;
81+
counter += 1;
82+
tokio::time::sleep(Duration::from_millis(100)).await;
83+
}
6084
}
61-
}
6285

63-
async fn wmctrl_try_raise_window(title: &str) -> anyhow::Result<ExitStatus> {
64-
let exit_status = Command::new("wmctrl")
65-
.arg("-F")
66-
.arg("-a")
67-
.arg(title)
68-
.spawn()?
69-
.wait()
70-
.await?;
86+
async fn wmctrl_try_raise_window(title: &str) -> anyhow::Result<ExitStatus> {
87+
let exit_status = Command::new("wmctrl")
88+
.arg("-F")
89+
.arg("-a")
90+
.arg(title)
91+
.spawn()?
92+
.wait()
93+
.await?;
7194

72-
Ok(exit_status)
95+
Ok(exit_status)
96+
}
7397
}

0 commit comments

Comments
 (0)