@@ -185,20 +185,51 @@ impl ImplWindow {
185
185
186
186
let wm_class = String :: from_utf8 ( get_class_reply. value ( ) . to_vec ( ) ) ?;
187
187
188
+ // WM_CLASS contains two strings: instance name and class name
189
+ // We want the class name (second string)
188
190
let app_name = wm_class
189
191
. split ( '\u{0}' )
190
- . find ( |str| !str . is_empty ( ) )
192
+ . nth ( 1 ) // Take the second string (class name )
191
193
. unwrap_or ( "" )
192
194
. to_string ( ) ;
193
195
194
196
Ok ( app_name)
195
197
}
196
198
197
199
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 ) ?;
200
205
let title = String :: from_utf8 ( get_title_reply. value ( ) . to_vec ( ) ) ?;
201
206
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
+
202
233
Ok ( title)
203
234
}
204
235
0 commit comments