2021-02-27 21:24:46 +00:00
|
|
|
import tkinter
|
2020-01-07 22:52:22 +00:00
|
|
|
import os
|
|
|
|
import subprocess
|
2020-06-17 20:46:22 +00:00
|
|
|
import sys
|
2020-01-07 22:52:22 +00:00
|
|
|
|
2020-12-24 04:07:01 +00:00
|
|
|
from voussoirkit import pathclass
|
2021-12-22 01:01:23 +00:00
|
|
|
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'
|
2022-01-11 07:57:16 +00:00
|
|
|
FOLDER_EMOJI = '\U0001F4C1'
|
|
|
|
|
|
|
|
PGUI_FOLDER = pathclass.Path(__file__).parent.with_child('PGUI')
|
2020-01-07 22:52:22 +00:00
|
|
|
|
|
|
|
def load_programs():
|
2022-01-11 07:57:16 +00:00
|
|
|
log.debug('Loading programs from %s.', PGUI_FOLDER.absolute_path)
|
|
|
|
shortcuts = PGUI_FOLDER.glob_files('*.lnk')
|
2021-12-22 01:01:23 +00:00
|
|
|
shortcuts.sort()
|
2020-12-24 04:07:01 +00:00
|
|
|
return shortcuts
|
2020-01-07 22:52:22 +00:00
|
|
|
|
2021-12-22 01:01:23 +00:00
|
|
|
class PGUILauncher(tkinter.Frame):
|
2020-01-07 22:52:22 +00:00
|
|
|
def __init__(self, parent):
|
2021-12-22 01:01:23 +00:00
|
|
|
super().__init__(parent, bg=MAIN_BG)
|
2020-01-07 22:52:22 +00:00
|
|
|
|
2022-01-11 08:05:34 +00:00
|
|
|
self._init_upper_controls()
|
2021-12-22 01:01:23 +00:00
|
|
|
self._init_buttons()
|
2020-01-07 22:52:22 +00:00
|
|
|
|
2021-12-22 01:01:23 +00:00
|
|
|
self.ready_to_launch = None
|
2020-01-07 22:52:22 +00:00
|
|
|
|
2021-12-22 01:01:23 +00:00
|
|
|
self.pack()
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
def _init_buttons(self):
|
|
|
|
shortcuts = load_programs()
|
2021-02-27 21:24:46 +00:00
|
|
|
|
2021-12-22 01:01:23 +00:00
|
|
|
# This keeps the grid looking mostly square regardless of input count.
|
|
|
|
column_count = int(len(shortcuts) ** 0.5)
|
|
|
|
column_count = max(column_count, 1)
|
2020-01-07 22:52:22 +00:00
|
|
|
|
2021-02-27 21:24:46 +00:00
|
|
|
self.buttons = []
|
2021-12-22 01:01:23 +00:00
|
|
|
|
2020-12-24 04:07:01 +00:00
|
|
|
for (index, shortcut) in enumerate(shortcuts):
|
2021-12-22 01:01:23 +00:00
|
|
|
button = tkinter.Button(
|
2020-06-17 20:46:22 +00:00
|
|
|
self,
|
2021-12-22 01:01:23 +00:00
|
|
|
bg=BUTTON_BG_NORMAL,
|
2020-12-24 04:07:01 +00:00
|
|
|
command=lambda sc=shortcut: self.launch_program(sc),
|
2021-12-22 01:01:23 +00:00
|
|
|
fg=MAIN_FG,
|
|
|
|
height=2,
|
|
|
|
text=shortcut.replace_extension('').basename,
|
|
|
|
width=BUTTON_WIDTH,
|
2020-06-17 20:46:22 +00:00
|
|
|
)
|
2021-02-27 21:24:46 +00:00
|
|
|
button.shortcut = shortcut
|
2020-12-24 04:07:01 +00:00
|
|
|
print(f'Creating button for {shortcut.basename}')
|
2021-12-22 01:01:23 +00:00
|
|
|
# Plus 1 because row 0 is the search box.
|
|
|
|
button.grid(
|
|
|
|
row=(index // column_count) + 1,
|
|
|
|
column=index % column_count,
|
|
|
|
padx=1,
|
|
|
|
pady=1,
|
|
|
|
)
|
2021-02-27 21:24:46 +00:00
|
|
|
self.buttons.append(button)
|
2020-01-07 22:52:22 +00:00
|
|
|
|
2022-01-11 08:05:34 +00:00
|
|
|
def _init_upper_controls(self):
|
2021-12-22 01:01:23 +00:00
|
|
|
# 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)
|
2022-01-11 07:57:16 +00:00
|
|
|
self.upper_frame = tkinter.Frame(self, bg=ENTRY_BG)
|
2021-12-22 01:01:23 +00:00
|
|
|
self.filter_entry = tkinter.Entry(
|
2022-01-11 07:57:16 +00:00
|
|
|
self.upper_frame,
|
2021-12-22 01:01:23 +00:00
|
|
|
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)
|
2023-09-16 19:03:00 +00:00
|
|
|
self.filter_entry.bind('<Control-w>', self.quit)
|
2021-12-22 01:01:23 +00:00
|
|
|
|
2022-01-11 07:57:16 +00:00
|
|
|
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)
|
2020-01-07 22:52:22 +00:00
|
|
|
|
2021-02-27 21:24:46 +00:00
|
|
|
def filter(self, *args):
|
|
|
|
text = self.filter_entry.get().lower()
|
2021-12-22 01:01:23 +00:00
|
|
|
enabled = []
|
2021-02-27 21:24:46 +00:00
|
|
|
for button in self.buttons:
|
2021-12-22 01:01:23 +00:00
|
|
|
button.configure(bg=BUTTON_BG_NORMAL)
|
2021-03-11 00:30:24 +00:00
|
|
|
if text == '' or text in button['text'].lower():
|
2021-02-27 21:24:46 +00:00
|
|
|
button['state'] = 'normal'
|
2021-12-22 01:01:23 +00:00
|
|
|
enabled.append(button)
|
2021-03-11 00:30:24 +00:00
|
|
|
else:
|
2021-02-27 21:24:46 +00:00
|
|
|
button['state'] = 'disabled'
|
|
|
|
|
2021-12-22 01:01:23 +00:00
|
|
|
if len(enabled) == 1:
|
|
|
|
enabled[0].configure(bg=BUTTON_BG_HIGHLIGHT)
|
|
|
|
self.ready_to_launch = enabled[0]
|
|
|
|
else:
|
|
|
|
self.ready_to_launch = None
|
|
|
|
|
2021-02-27 21:24:46 +00:00
|
|
|
def launch_filtered(self, *args):
|
2021-12-22 01:01:23 +00:00
|
|
|
if self.ready_to_launch is None:
|
2021-02-27 21:24:46 +00:00
|
|
|
return
|
|
|
|
|
2021-12-22 01:01:23 +00:00
|
|
|
self.launch_program(self.ready_to_launch.shortcut)
|
2021-02-27 21:24:46 +00:00
|
|
|
|
2020-12-24 04:07:01 +00:00
|
|
|
def launch_program(self, shortcut):
|
|
|
|
print('opening application', shortcut.basename)
|
|
|
|
os.chdir(shortcut.parent.absolute_path)
|
|
|
|
command = f'"{shortcut.absolute_path}"'
|
2020-06-17 20:46:22 +00:00
|
|
|
subprocess.Popen(command, shell=True)
|
|
|
|
self.quit()
|
2020-01-07 22:52:22 +00:00
|
|
|
|
2022-01-11 07:57:16 +00:00
|
|
|
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)
|
|
|
|
|
2021-07-05 05:22:10 +00:00
|
|
|
def quit(self, *args):
|
|
|
|
return super().quit()
|
|
|
|
|
2021-12-22 01:01:23 +00:00
|
|
|
@vlogging.main_decorator
|
2020-06-17 20:46:22 +00:00
|
|
|
def main(argv):
|
2021-02-27 21:27:43 +00:00
|
|
|
root = tkinter.Tk()
|
2021-09-01 02:19:55 +00:00
|
|
|
root.withdraw()
|
2021-12-22 01:01:23 +00:00
|
|
|
root.title('PGUI')
|
2020-06-17 20:46:22 +00:00
|
|
|
root.resizable(0,0)
|
|
|
|
|
2021-12-22 01:01:23 +00:00
|
|
|
pgui = PGUILauncher(root)
|
|
|
|
pgui.pack(fill=tkinter.BOTH, expand=True)
|
|
|
|
pgui.filter_entry.focus()
|
2020-06-17 20:46:22 +00:00
|
|
|
|
|
|
|
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))
|
2021-09-01 02:19:55 +00:00
|
|
|
root.deiconify()
|
2020-01-07 22:52:22 +00:00
|
|
|
root.mainloop()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-06-17 20:46:22 +00:00
|
|
|
raise SystemExit(main(sys.argv[1:]))
|