-
Notifications
You must be signed in to change notification settings - Fork 2
/
menu.py
110 lines (85 loc) · 2.53 KB
/
menu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import subprocess
import sys
import time
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gio # noqa
from gi.repository import GLib # noqa
DMENU_CMD = ['rofi', '-dmenu', '-i', '-no-sort']
class Bus:
def __init__(self, conn, name, path):
self.conn = conn
self.name = name
self.path = path
def call_sync(self, interface, method, params, params_type, return_type):
return self.conn.call_sync(
self.name,
self.path,
interface,
method,
GLib.Variant(params_type, params),
GLib.VariantType(return_type),
Gio.DBusCallFlags.NONE,
-1,
None,
)
def get_menu_layout(self, *args):
return self.call_sync(
'com.canonical.dbusmenu',
'GetLayout',
args,
'(iias)',
'(u(ia{sv}av))',
)
def menu_event(self, *args):
self.call_sync('com.canonical.dbusmenu', 'Event', args, '(isvu)', '()')
def dmenu(_input):
p = subprocess.Popen(
DMENU_CMD,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
encoding='utf-8',
)
out, _ = p.communicate(_input)
return out
def format_toggle_value(props):
toggle_type = props.get('toggle-type', '')
toggle_value = props.get('toggle-state', -1)
if toggle_value == 0:
s = ' '
elif toggle_value == 1:
s = 'X'
else:
s = '~'
if toggle_type == 'checkmark':
return f'[{s}] '
elif toggle_type == 'radio':
return f'({s}) '
else:
return ''
def format_menu_item(item, level=1):
id, props, children = item
if not props.get('visible', True):
return ''
if props.get('type', 'standard') == 'separator':
label = '---'
else:
label = format_toggle_value(props) + props.get('label', '')
if not props.get('enabled', True):
label = f'({label})'
indentation = ' ' * level
ret = f'{id}{indentation}{label}\n'
for child in children:
ret += format_menu_item(child, level + 1)
return ret
def show_menu(conn, name, path):
bus = Bus(conn, name, path)
item = bus.get_menu_layout(0, -1, [])[1]
menu = format_menu_item(item)
selected = dmenu(menu)
if selected:
id = int(selected.split()[0])
bus.menu_event(id, 'clicked', GLib.Variant('s', ''), time.time())
if __name__ == '__main__':
conn = Gio.bus_get_sync(Gio.BusType.SESSION)
show_menu(conn, sys.argv[1], sys.argv[2])