Skip to content

Commit

Permalink
Refactor shortcuts using GTK
Browse files Browse the repository at this point in the history
  • Loading branch information
Franco Battista authored and Franco Battista committed May 7, 2024
1 parent cbb0fe2 commit f52ad0b
Showing 1 changed file with 11 additions and 27 deletions.
38 changes: 11 additions & 27 deletions src/mousam.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
import time
import threading
import gettext
import keyboard

gi.require_version("Gtk", "4.0")
gi.require_version("Adw", "1")
from gi.repository import Gtk, Adw, Gio, GLib
from gi.repository import Gtk, Adw, Gio, GLib, Gdk
from gettext import gettext as _, pgettext as C_


Expand Down Expand Up @@ -116,8 +115,9 @@ def __init__(self, *args, **kwargs):
thread.start()

#Set key listeners
keyboard_thread = threading.Thread(target=self.listen_to_keyboard_events)
keyboard_thread.start()
keycont = Gtk.EventControllerKey()
keycont.connect('key-pressed', self.on_key_press)
self.add_controller(keycont)


# =========== Create Loader =============
Expand Down Expand Up @@ -366,30 +366,14 @@ def _on_locations_clicked(self, *args, **kwargs):
adw_preferences_window.show()


#Exec shortcut method
def on_hotkey_pressed(self,event):
match event:
#Create a new thread if shortcut need a new window
case "location":
GLib.idle_add(self._on_locations_clicked)
case "weather":
#Def shortcuts key listeners
def on_key_press(self, key_controller, keyval, keycode, state,*args):
if state & Gdk.ModifierType.CONTROL_MASK:
if keyval == Gdk.KEY_r:
self._refresh_weather(None)
case "preferences":
if keyval == Gdk.KEY_l:
GLib.idle_add(self._on_locations_clicked)
if keyval == Gdk.KEY_comma:
GLib.idle_add(self._on_preferences_clicked)
case _:
return ""


#Def shortcuts key listeners
def listen_to_keyboard_events(self):

#Listeners
keyboard.add_hotkey('ctrl+l', lambda: self.on_hotkey_pressed("location"))
keyboard.add_hotkey('ctrl+r', lambda: self.on_hotkey_pressed("weather"))
keyboard.add_hotkey('ctrl+comma', lambda: self.on_hotkey_pressed("preferences"))

#Wait empty key. None key stop de listener
keyboard.wait("")

#Free hooks
keyboard.unhook_all()

0 comments on commit f52ad0b

Please sign in to comment.