33
33
# We'll do it for i3 by applying commands to the focused window in open_menu method.
34
34
if wm == "sway" :
35
35
try :
36
- subprocess .run (['swaymsg' , 'for_window' , '[title=\" ~sgtk-menu \" ]' , 'floating' , 'enable' ],
36
+ subprocess .run (['swaymsg' , 'for_window' , '[title=\" ~sgtk-dmenu \" ]' , 'floating' , 'enable' ],
37
37
stdout = subprocess .DEVNULL ).returncode == 0
38
38
except :
39
39
pass
54
54
55
55
win = None # overlay window
56
56
args = None
57
- all_items_list = [] # list of all DesktopMenuItem objects assigned to a .desktop entry
58
- all_copies_list = [] # list of copies of above used while searching (not assigned to a submenu!)
57
+ all_items_list = [] # list of all DesktopMenuItem objects assigned to a command
59
58
menu_items_list = [] # created / updated with menu.get_children()
60
59
filtered_items_list = [] # used in the search method
61
60
@@ -142,7 +141,7 @@ def main():
142
141
143
142
global all_commands_list
144
143
all_commands_list = list_commands ()
145
- all_commands_list = sorted ( all_commands_list )
144
+ all_commands_list . sort ( )
146
145
147
146
# Overlay window
148
147
global win
@@ -202,8 +201,8 @@ def main():
202
201
class MainWindow (Gtk .Window ):
203
202
def __init__ (self ):
204
203
Gtk .Window .__init__ (self )
205
- self .set_title ('~sgtk-menu ' )
206
- self .set_role ('~sgtk-menu ' )
204
+ self .set_title ('~sgtk-dmenu ' )
205
+ self .set_role ('~sgtk-dmenu ' )
207
206
self .connect ("destroy" , Gtk .main_quit )
208
207
self .connect ('draw' , self .draw ) # transparency
209
208
@@ -256,47 +255,36 @@ def __init__(self):
256
255
self .add (outer_box )
257
256
258
257
def search_items (self , menu , event ):
259
- global filtered_items_list
258
+
260
259
if event .type == Gdk .EventType .KEY_RELEASE :
260
+ global filtered_items_list
261
261
update = False
262
262
# search box only accepts alphanumeric characters, and couple of special ones:
263
263
if event .string and event .string .isalnum () or event .string in [' ' , '-' , '+' , '_' , '.' ]:
264
264
update = True
265
- # remove menu items, except for search box (item #0)
266
- items = self .menu .get_children ()
267
- if len (items ) > 1 :
268
- for item in items [1 :]:
269
- self .menu .remove (item )
270
265
self .search_phrase += event .string
271
- self .search_box .set_text (self .search_phrase )
272
266
273
267
elif event .keyval == 65288 : # backspace
274
268
update = True
275
269
self .search_phrase = self .search_phrase [:- 1 ]
276
- self .search_box .set_text (self .search_phrase )
277
-
278
- # If our search result is a single item, we may want to activate the highlighted item with the Enter key,
279
- # but it does not work in GTK3. Here is a workaround:
280
- elif event .keyval == 65293 and len (filtered_items_list ) == 1 :
281
- filtered_items_list [0 ].activate ()
282
270
283
271
# filter items by search_phrase
284
272
if update :
285
- if len (self .search_phrase ) > 0 :
286
- filtered_items_list = []
287
- for item in all_copies_list :
288
- self .menu .remove ( item )
289
- label = item . get_label ()
290
- if " " not in self . search_phrase :
291
- if self .search_phrase . upper () in label . upper ():
292
- filtered_items_list . append ( item )
293
- else :
294
- # if the string ends with space, search exact 1st word
295
- if self .search_phrase .upper (). split ()[ 0 ] == label .upper ():
296
- filtered_items_list . append ( item )
297
-
298
- for item in self .menu . get_children ()[1 :]:
299
- self . menu . remove ( item )
273
+ self . search_box . set_text (self .search_phrase )
274
+ if self . search_phrase :
275
+ # remove menu items, except for search box (item #0)
276
+ items = self .menu .get_children ( )
277
+ if len ( items ) > 1 :
278
+ for item in items [ 1 :] :
279
+ self .menu . remove ( item )
280
+
281
+ if " " not in self . search_phrase :
282
+ filtered_items_list = [ item for item in all_items_list if
283
+ self .search_phrase .upper () in item . get_label () .upper ()]
284
+ else :
285
+ # if the string ends with space, search exact 1st word
286
+ first = self .search_phrase . split ()[0 ]. upper ()
287
+ filtered_items_list = [ item for item in all_items_list if first == item . get_label (). upper ()]
300
288
301
289
for item in filtered_items_list :
302
290
self .menu .append (item )
@@ -319,13 +307,15 @@ def search_items(self, menu, event):
319
307
# restore original menu
320
308
for item in menu_items_list :
321
309
self .menu .append (item )
322
- # better to have it insensitive when possible
323
- self .search_item .set_sensitive (False )
324
- self .menu .reposition ()
325
310
326
311
if len (self .search_phrase ) == 0 :
327
312
self .search_box .set_text ('Type to search' )
328
313
314
+ # If our search result is a single, programmatically selected item, we may want to activate it
315
+ # with the Enter key, but it does not work in GTK3. Here is a workaround:
316
+ if event .keyval == 65293 and len (filtered_items_list ) == 1 :
317
+ filtered_items_list [0 ].activate ()
318
+
329
319
# key-release-event callback must return a boolean
330
320
return True
331
321
@@ -383,7 +373,6 @@ def list_commands():
383
373
def build_menu (commands ):
384
374
icon_theme = Gtk .IconTheme .get_default ()
385
375
menu = Gtk .Menu ()
386
-
387
376
win .search_item = Gtk .MenuItem ()
388
377
win .search_item .add (win .search_box )
389
378
win .search_item .set_sensitive (False )
@@ -395,8 +384,8 @@ def build_menu(commands):
395
384
item .set_property ("name" , "item-dmenu" )
396
385
item .connect ('activate' , launch , command )
397
386
all_items_list .append (item )
398
- all_copies_list .append (item )
399
387
388
+ # At the beginning we'll only show args.t items. Nobody's gonna scroll through thousands of them.
400
389
for item in all_items_list [:args .t ]:
401
390
menu .append (item )
402
391
@@ -436,7 +425,7 @@ def build_menu(commands):
436
425
item = Gtk .MenuItem ()
437
426
item .set_property ("name" , "item" )
438
427
item .add (hbox )
439
- item .connect ('activate' , launch , exec , True ) # do not cache!
428
+ item .connect ('activate' , launch , exec ) # do not cache!
440
429
menu .append (item )
441
430
442
431
menu .connect ("hide" , win .die )
0 commit comments