-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPGUI.pyw
163 lines (134 loc) · 4.98 KB
/
PGUI.pyw
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import tkinter
import os
import subprocess
import sys
from voussoirkit import pathclass
from voussoirkit import vlogging
log = vlogging.get_logger(__name__, 'pgui')
BUTTON_WIDTH = 12
MAIN_BG = '#000'
MAIN_FG = '#FFF'
ENTRY_BG = '#222'
BUTTON_BG_NORMAL = '#000'
BUTTON_BG_HIGHLIGHT = '#00aa00'
FOLDER_EMOJI = '\U0001F4C1'
PGUI_FOLDER = pathclass.Path(__file__).parent.with_child('PGUI')
def load_programs():
log.debug('Loading programs from %s.', PGUI_FOLDER.absolute_path)
shortcuts = PGUI_FOLDER.glob_files('*.lnk')
shortcuts.sort()
return shortcuts
class PGUILauncher(tkinter.Frame):
def __init__(self, parent):
super().__init__(parent, bg=MAIN_BG)
self._init_upper_controls()
self._init_buttons()
self.ready_to_launch = None
self.pack()
self.update()
def _init_buttons(self):
shortcuts = load_programs()
# This keeps the grid looking mostly square regardless of input count.
column_count = int(len(shortcuts) ** 0.5)
column_count = max(column_count, 1)
self.buttons = []
for (index, shortcut) in enumerate(shortcuts):
button = tkinter.Button(
self,
bg=BUTTON_BG_NORMAL,
command=lambda sc=shortcut: self.launch_program(sc),
fg=MAIN_FG,
height=2,
text=shortcut.replace_extension('').basename,
width=BUTTON_WIDTH,
)
button.shortcut = shortcut
print(f'Creating button for {shortcut.basename}')
# Plus 1 because row 0 is the search box.
button.grid(
row=(index // column_count) + 1,
column=index % column_count,
padx=1,
pady=1,
)
self.buttons.append(button)
def _init_upper_controls(self):
# The only way to add padding around the text entry is to put it in its
# own frame element. Thanks Kevin
# https://stackoverflow.com/a/51823093/5430534
self.filter_var = tkinter.StringVar()
self.filter_var.trace('w', self.filter)
self.upper_frame = tkinter.Frame(self, bg=ENTRY_BG)
self.filter_entry = tkinter.Entry(
self.upper_frame,
bg=ENTRY_BG,
fg=MAIN_FG,
insertbackground=MAIN_FG,
relief=tkinter.FLAT,
textvariable=self.filter_var,
)
self.filter_entry.bind('<Return>', self.launch_filtered)
self.filter_entry.bind('<Escape>', self.quit)
self.filter_entry.bind('<Control-w>', self.quit)
self.open_folder_button = tkinter.Button(
self.upper_frame,
text=FOLDER_EMOJI,
bg=BUTTON_BG_NORMAL,
fg=MAIN_FG,
command=lambda: self.open_pgui_folder(),
)
self.upper_frame.columnconfigure(0, weight=1)
self.upper_frame.grid(row=0, column=0, columnspan=999, sticky='ew', padx=8, pady=8)
self.filter_entry.grid(row=0, column=0, sticky='news', padx=2, pady=2)
self.open_folder_button.grid(row=0, column=1, sticky='news', padx=2, pady=2)
def filter(self, *args):
text = self.filter_entry.get().lower()
enabled = []
for button in self.buttons:
button.configure(bg=BUTTON_BG_NORMAL)
if text == '' or text in button['text'].lower():
button['state'] = 'normal'
enabled.append(button)
else:
button['state'] = 'disabled'
if len(enabled) == 1:
enabled[0].configure(bg=BUTTON_BG_HIGHLIGHT)
self.ready_to_launch = enabled[0]
else:
self.ready_to_launch = None
def launch_filtered(self, *args):
if self.ready_to_launch is None:
return
self.launch_program(self.ready_to_launch.shortcut)
def launch_program(self, shortcut):
print('opening application', shortcut.basename)
os.chdir(shortcut.parent.absolute_path)
command = f'"{shortcut.absolute_path}"'
subprocess.Popen(command, shell=True)
self.quit()
def open_pgui_folder(self):
if os.name == 'nt':
command = ['explorer.exe', PGUI_FOLDER.absolute_path]
else:
command = ['xdg-open', PGUI_FOLDER.absolute_path]
subprocess.Popen(command, shell=True)
def quit(self, *args):
return super().quit()
@vlogging.main_decorator
def main(argv):
root = tkinter.Tk()
root.withdraw()
root.title('PGUI')
root.resizable(0,0)
pgui = PGUILauncher(root)
pgui.pack(fill=tkinter.BOTH, expand=True)
pgui.filter_entry.focus()
width = root.winfo_reqwidth()
height = root.winfo_reqheight()
x_offset = (root.winfo_screenwidth() - width) / 2
y_offset = (root.winfo_screenheight() - height) / 2
root.geometry('%dx%d+%d+%d' % (width, height, x_offset, y_offset-50))
root.deiconify()
root.mainloop()
if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:]))