Skip to content

Commit 618ed7e

Browse files
committed
feat: impl X11:activate_window
1 parent 466c988 commit 618ed7e

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/gui/utils.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ pub fn active_window<T>(_window: &mut Window, _cx: &mut Context<T>) {
4747
}
4848
#[cfg(target_os = "macos")]
4949
_cx.activate(true);
50+
#[cfg(target_os = "linux")]
51+
{
52+
if let Some(x11) = crate::gui::app::X11.get() {
53+
if let Err(e) = x11.activate_window() {
54+
eprintln!("[ropy] Failed to activate window: {e}")
55+
}
56+
}
57+
_cx.active_window();
58+
}
59+
5060
}
5161

5262
/// Set the window to be always on top

src/gui/x11.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub struct X11 {
1616
net_wm_state_above: u32,
1717
net_wm_state: u32,
1818
window_id: u32,
19+
net_active_window: u32,
1920
}
2021

2122
impl X11 {
@@ -28,6 +29,7 @@ impl X11 {
2829
let net_wm_pid = Self::get_atom(&conn, b"_NET_WM_PID")?;
2930
let net_wm_state_above = Self::get_atom(&conn, b"_NET_WM_STATE_ABOVE")?;
3031
let net_wm_state = Self::get_atom(&conn, b"_NET_WM_STATE")?;
32+
let net_active_window = Self::get_atom(&conn, b"_NET_ACTIVE_WINDOW")?;
3133

3234
let windows = Self::get_value32(&conn, root_id, net_client_list)?;
3335

@@ -50,6 +52,7 @@ impl X11 {
5052
net_wm_state_above,
5153
net_wm_state,
5254
window_id: window_id.ok_or_else(|| io::Error::other("Failed to get window id"))?,
55+
net_active_window,
5356
})
5457
}
5558

@@ -105,4 +108,33 @@ impl X11 {
105108
pub fn set_always_on_top(&self, always_on_top: bool) -> Result<(), Box<dyn Error>> {
106109
self.send_wm_state_and_sync(self.net_wm_state_above, always_on_top, self.root_id)
107110
}
111+
112+
pub fn activate_window(&self) -> Result<(), Box<dyn Error>> {
113+
self.connection.map_window(self.window_id)?;
114+
self.connection.sync()?;
115+
116+
let event = ClientMessageEvent::new(
117+
32, // 格式
118+
self.window_id,
119+
self.net_active_window,
120+
[
121+
2, // 来源:应用请求
122+
x11rb::CURRENT_TIME,
123+
0, // 当前活动窗口(0表示无)
124+
0,
125+
0,
126+
],
127+
);
128+
129+
self.connection.send_event(
130+
false,
131+
self.root_id,
132+
EventMask::SUBSTRUCTURE_REDIRECT | EventMask::SUBSTRUCTURE_NOTIFY,
133+
event,
134+
)?;
135+
136+
self.connection.sync()?;
137+
138+
Ok(())
139+
}
108140
}

0 commit comments

Comments
 (0)