-
Notifications
You must be signed in to change notification settings - Fork 0
/
kwikmenu.py
executable file
·142 lines (113 loc) · 3.69 KB
/
kwikmenu.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# http://wolfvollprecht.de/blog/gtk-python-and-css-are-an-awesome-combo/
# https://gist.github.com/fcwu/5794494
# Python 3 + gtk 3 !!!
# http://python-gtk-3-tutorial.readthedocs.io/en/latest/introduction.html
#!/usr/bin/python3
from gi.repository import Gtk, Gdk
import sys
import yaml
import os
import subprocess
import re
KEY_ESC=65307
KEY_ENTER=65293
KEY_UP=65362
KEY_RIGHT=65363
KEY_DOWN=65364
KEY_LEFT=65361
class KwikMenu(Gtk.Window):
def __init__(self,data):
# Style de fenetre
Gtk.Window.__init__(self)
self.set_name('KwikMenu')
self.connect("key-press-event",self.on_key_pressed)
self.set_decorated(False)
self.box = Gtk.VBox()
self.box.set_halign(Gtk.Align.CENTER)
self.box.set_valign(Gtk.Align.CENTER)
self.add(self.box)
# Creaton des boutons
self.buttonDict = {}
self.commandDict = {}
index = 0
for i in data['commands']:
button = Gtk.ToggleButton(i['name'])
self.buttonDict[index] = button
self.commandDict[index] = self.build_command(data['bin'],i['command'])
print("bin > "+self.commandDict[index] )
index += 1
# Activation des bouton au démarrage
self.selected = 0
self.buttonDict[self.selected].set_active(True)
self.render()
# rendering des boutons
def render(self):
for key in self.buttonDict:
self.box.pack_start(self.buttonDict[key], True, True, 0)
def on_key_pressed(self,widget,event):
# Escape
# http://stackoverflow.com/questions/23111362/key-binding-for-window-app-in-python-gtk-3-without-menu-items-ui-manager-etc
if (event.keyval == KEY_ESC):
Gtk.main_quit()
if (event.keyval == KEY_ENTER):
self.run_command()
if event.keyval == KEY_UP or event.keyval == KEY_LEFT:
self.select_next(-1)
if event.keyval == KEY_DOWN or event.keyval == KEY_RIGHT:
self.select_next(1)
def select_next(self,prevnext):
self.buttonDict[self.selected].set_active(False)
self.selected = (self.selected + prevnext) % (max(self.buttonDict.keys()) + 1)
self.buttonDict[self.selected].set_active(True)
def run_command(self):
command = self.commandDict[self.selected]
print("command="+ str(command.split()))
pid = subprocess.Popen(command.split())
print(pid)
Gtk.main_quit()
def build_command(self,binaries,command):
try:
m = re.search('([^\s]+)(.*)', command)
if(m.group(1) in binaries.keys()):
return str(binaries[m.group(1)]) + " " + str(m.group(2))
except:
pass
return command
def main(argv):
configfile = os.getenv("HOME") + "/.kwikmenu.yaml"
if os.path.isfile(configfile):
with open(configfile, 'r') as stream:
data = yaml.load(stream)
else:
print("Aucun fichier de configuration trouvé $HOME/.kwikmenu.yaml")
sys.exit()
def gtk_style():
css = b"""
.button {
border-style: none;
border-radius: 30px;
border-width: 2px 0 2px 2px;
padding: 12px 4px;
}
.button > #title-label {
font: Sans 30
}
GtkButton:active {
background-color: red;
}
"""
style_provider = Gtk.CssProvider()
style_provider.load_from_data(css)
Gtk.StyleContext.add_provider_for_screen(
Gdk.Screen.get_default(),
style_provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
)
gtk_style()
win = KwikMenu(data)
win.show_all()
Gtk.main()
if __name__ == "__main__":
main(sys.argv)