Skip to content

Commit a65e361

Browse files
authored
fix: empty title on many apps and wrong appname on firefox i.e. Navigator (#211)
Signed-off-by: divanshu-go <[email protected]>
1 parent 09c11cc commit a65e361

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

src/linux/impl_window.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,20 +185,51 @@ impl ImplWindow {
185185

186186
let wm_class = String::from_utf8(get_class_reply.value().to_vec())?;
187187

188+
// WM_CLASS contains two strings: instance name and class name
189+
// We want the class name (second string)
188190
let app_name = wm_class
189191
.split('\u{0}')
190-
.find(|str| !str.is_empty())
192+
.nth(1) // Take the second string (class name)
191193
.unwrap_or("")
192194
.to_string();
193195

194196
Ok(app_name)
195197
}
196198

197199
pub fn title(&self) -> XCapResult<String> {
198-
let get_title_reply = get_window_property(self.window, ATOM_WM_NAME, ATOM_STRING, 0, 1024)?;
199-
200+
// First try _NET_WM_NAME with UTF8_STRING type
201+
let net_wm_name_atom = get_atom("_NET_WM_NAME")?;
202+
let utf8_string_atom = get_atom("UTF8_STRING")?;
203+
let get_title_reply =
204+
get_window_property(self.window, net_wm_name_atom, utf8_string_atom, 0, 1024)?;
200205
let title = String::from_utf8(get_title_reply.value().to_vec())?;
201206

207+
// If _NET_WM_NAME is empty, fall back to WM_NAME with COMPOUND_TEXT type
208+
if title.is_empty() {
209+
let compound_text_atom = get_atom("COMPOUND_TEXT")?;
210+
let get_title_reply =
211+
get_window_property(self.window, ATOM_WM_NAME, compound_text_atom, 0, 1024)?;
212+
let title = String::from_utf8(get_title_reply.value().to_vec())?;
213+
214+
// If both are empty, try to get the parent window
215+
if title.is_empty() {
216+
let (conn, _) = get_xcb_connection_and_index()?;
217+
let query_tree_cookie = conn.send_request(&xcb::x::QueryTree {
218+
window: self.window,
219+
});
220+
if let Ok(query_tree_reply) = conn.wait_for_reply(query_tree_cookie) {
221+
let parent = query_tree_reply.parent();
222+
if parent.resource_id() != 0 {
223+
// Try to get title from parent window
224+
let parent_window = ImplWindow::new(parent);
225+
return parent_window.title();
226+
}
227+
}
228+
}
229+
230+
return Ok(title);
231+
}
232+
202233
Ok(title)
203234
}
204235

0 commit comments

Comments
 (0)