Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added tray icon, and a basic gui to change hotkey #9

Merged
merged 1 commit into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ readme = "README.md"
python = ">=3.11,<3.13"
keyboard = "^0.13.5"
pyinstaller = "^6.1.0"
pystray = "^0.19.5"
pydantic = "^2.4.2"


[tool.poetry.group.dev.dependencies]
Expand Down
92 changes: 91 additions & 1 deletion toggle_taskbar/gui.py
Original file line number Diff line number Diff line change
@@ -1 +1,91 @@
pass
import pystray
from PIL import Image
import tkinter as tk
from tkinter import simpledialog

from . import keys
import tkinter as tk

key_combination = []


# def key_pressed(event):
# key = event.keysym
# if key not in key_combination:
# key_combination.append(key)


# def get_user_input() -> str:
# global key_combination
# key_combination = [] # Reset the key combination
# root = tk.Tk()
# root.title("Change Hotkey")

# h1_label = tk.Label(root, text="Change Hotkey", font=("Arial", 24))
# h1_label.pack(pady=10)

# p_label = tk.Label(
# root,
# text="Press a combination of keys to change this title",
# font=("Arial", 12),
# )
# p_label.pack(pady=10)

# confirm_button = tk.Button(root, text="Confirm", command=root.quit)
# confirm_button.pack(pady=10)

# root.bind("<Key>", key_pressed)
# root.mainloop()

# # Convert list of keys to a string separated by '+'
# user_input = "+".join(key_combination)
# root.destroy()
# return user_input


def get_user_input() -> str:
root = tk.Tk()
root.withdraw() # Hide the main window
user_input = simpledialog.askstring("Input", "Please enter the new hotkey:")
root.destroy() # Close the window after getting input
return user_input


# TODO: Add a better icon
def create_image():
# Generate an image and return it
width, height = 64, 64
color1, color2 = (255, 255, 255), (0, 0, 0) # RGB tuples for white and black

image = Image.new("RGB", (width, height), color1)
pixels = image.load()

# Draw a black cross
for i in range(width):
pixels[i, height // 2] = color2
for i in range(height):
pixels[width // 2, i] = color2

return image


def on_change_hotkey(icon, item):
new_hotkey = get_user_input()
keys.remove_hotkey()
keys.change_hotkey(new_hotkey)


def on_exit(icon, item):
icon.stop()
keys.exit_application()


def create_tray_icon():
image = create_image()
menu = [
pystray.MenuItem("Change Hotkey", on_change_hotkey),
pystray.MenuItem("Exit", on_exit),
]
tray_name = "Toggle Taskbar"
icon = pystray.Icon(tray_name, image, tray_name, menu)
icon.run()
14 changes: 14 additions & 0 deletions toggle_taskbar/keys.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Bind the function to a key
import keyboard
from .utils import toggle_taskbar
import sys


current_hotkey = "ctrl+esc"
Expand All @@ -15,3 +16,16 @@ def change_hotkey(new_hotkey):

def default_hotkey():
keyboard.add_hotkey(current_hotkey, toggle_taskbar, suppress=True)


def remove_hotkey():
keyboard.remove_hotkey(current_hotkey)


def exit_application():
try:
sys.exit(0)
except SystemExit: # prevents sd.out exit error
pass
finally:
keyboard.unhook_all_hotkeys()
6 changes: 2 additions & 4 deletions toggle_taskbar/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import keyboard
from . import cli
from . import gui
from toggle_taskbar import cli, gui


def run():
Expand All @@ -12,7 +10,7 @@ def run():
else:
gui.main() # Assuming there's a main function in gui.py

keyboard.wait() # Wait for a keypress to exit
gui.create_tray_icon()


if __name__ == "__main__":
Expand Down
5 changes: 4 additions & 1 deletion toggle_taskbar/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import ctypes
import keyboard

# Define the necessary constants for Windows API
SW_HIDE = 0 # Constant to hide the window
Expand All @@ -11,6 +10,10 @@
isActive = False


def format_user_input():
pass


# Hide the taskbar
def toggle_taskbar():
# Toggles isPressed
Expand Down
18 changes: 18 additions & 0 deletions toggle_taskbar/validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from pydantic import BaseModel, Field, validator


class HotkeyModel(BaseModel):
hotkey: str = Field(..., alias="_ParseableHotkey")

@validator("hotkey", pre=True, allow_reuse=True)
def validate_hotkey(cls, value):
# Define your hotkey validation logic here
if not isinstance(value, str):
raise ValueError("Hotkey must be a string")

keys = value.split("+")
if len(keys) < 2:
raise ValueError("Invalid hotkey format. Must be like 'Ctrl+Alt+Del'")

# Additional validation logic can be added
return value
Loading