|
1 |
| -use std::{process::ExitStatus, time::Duration}; |
2 |
| - |
3 |
| -use anyhow::bail; |
4 |
| -use log::info; |
5 | 1 | use tauri::WebviewWindow;
|
6 |
| -use tokio::process::Command; |
7 | 2 |
|
8 | 3 | pub trait WindowExt {
|
9 | 4 | fn raise(&self) -> anyhow::Result<()>;
|
10 | 5 | }
|
11 | 6 |
|
12 | 7 | impl WindowExt for WebviewWindow {
|
| 8 | + #[cfg(any(target_os = "macos", target_os = "windows"))] |
13 | 9 | 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) |
15 | 17 | }
|
16 | 18 | }
|
17 | 19 |
|
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}; |
20 | 23 |
|
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 | + }); |
32 | 43 | }
|
33 |
| - }); |
34 |
| - } |
35 | 44 |
|
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 | + } |
39 | 58 |
|
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 | + } |
42 | 65 |
|
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; |
45 | 68 |
|
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 | + } |
51 | 75 | }
|
52 |
| - } |
53 | 76 |
|
54 |
| - if counter >= 10 { |
55 |
| - bail!("Failed to raise window: {}", title) |
56 |
| - } |
| 77 | + if counter >= 10 { |
| 78 | + bail!("Failed to raise window: {}", title) |
| 79 | + } |
57 | 80 |
|
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 | + } |
60 | 84 | }
|
61 |
| -} |
62 | 85 |
|
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?; |
71 | 94 |
|
72 |
| - Ok(exit_status) |
| 95 | + Ok(exit_status) |
| 96 | + } |
73 | 97 | }
|
0 commit comments