Refactor hotkey handling and improve status updates with active language display
This commit is contained in:
@@ -87,6 +87,7 @@ class SpeechApp(Gtk.Window):
|
||||
)
|
||||
self.listener.daemon = True
|
||||
self.listener.start()
|
||||
self.update_status("Ready")
|
||||
|
||||
def on_button_pressed(self, widget):
|
||||
self.start_recording()
|
||||
@@ -142,7 +143,8 @@ class SpeechApp(Gtk.Window):
|
||||
|
||||
def update_status(self, status):
|
||||
# Helper method to update status
|
||||
self.status_bar.set_text(f"Status: {status}")
|
||||
language = self.get_active_language_code().upper()
|
||||
self.status_bar.set_text(f"Status: {status} [{language}]")
|
||||
return False # Return False to prevent being called again
|
||||
|
||||
def transcribe_and_type(self, language):
|
||||
@@ -209,33 +211,34 @@ class SpeechApp(Gtk.Window):
|
||||
|
||||
def on_key_press(self, key):
|
||||
if key == keyboard.Key.shift_r:
|
||||
self.hotkey_pressed.add("shift_r")
|
||||
self.hotkey_pressed.add("shift")
|
||||
elif key == keyboard.Key.ctrl_r:
|
||||
self.hotkey_pressed.add("ctrl_r")
|
||||
self.hotkey_pressed.add("ctrl")
|
||||
elif key == keyboard.Key.alt_r:
|
||||
self.hotkey_pressed.add("alt_r")
|
||||
self.hotkey_pressed.add("alt")
|
||||
|
||||
self.lang_modifier_active = (
|
||||
"alt_r" in self.hotkey_pressed
|
||||
"alt" in self.hotkey_pressed
|
||||
)
|
||||
if not self.recording:
|
||||
GLib.idle_add(self.update_status, "Ready")
|
||||
|
||||
if not self.hotkey_active and (self.hotkey_pressed == {"shift_r", "ctrl_r"} or self.hotkey_pressed == {"shift_r", "alt_r"}):
|
||||
if not self.hotkey_active and "shift" in self.hotkey_pressed and ("alt" in self.hotkey_pressed or "ctrl" in self.hotkey_pressed):
|
||||
self.hotkey_active = True
|
||||
GLib.idle_add(self.start_recording)
|
||||
|
||||
def on_key_release(self, key):
|
||||
if key == keyboard.Key.shift_r:
|
||||
self.hotkey_pressed.discard("shift_r")
|
||||
self.hotkey_pressed.discard("shift")
|
||||
elif key == keyboard.Key.ctrl_r:
|
||||
self.hotkey_pressed.discard("ctrl_r")
|
||||
self.hotkey_pressed.discard("ctrl")
|
||||
elif key == keyboard.Key.alt_r:
|
||||
self.hotkey_pressed.discard("alt_r")
|
||||
self.hotkey_pressed.discard("alt")
|
||||
|
||||
self.lang_modifier_active = (
|
||||
"alt_r" in self.hotkey_pressed
|
||||
)
|
||||
if not self.recording:
|
||||
GLib.idle_add(self.update_status, "Ready")
|
||||
|
||||
if self.hotkey_active and (self.hotkey_pressed != {"shift_r", "ctrl_r"} and self.hotkey_pressed != {"shift_r", "alt_r"}):
|
||||
if self.hotkey_active and not ("shift" in self.hotkey_pressed):
|
||||
self.hotkey_active = False
|
||||
GLib.idle_add(self.stop_recording)
|
||||
|
||||
@@ -245,6 +248,9 @@ class SpeechApp(Gtk.Window):
|
||||
def is_alt_key(self, key):
|
||||
return key in (keyboard.Key.alt, keyboard.Key.alt_l, keyboard.Key.alt_r, keyboard.Key.alt_gr)
|
||||
|
||||
def get_active_language_code(self):
|
||||
return self.alt_language if self.lang_modifier_active else self.default_language
|
||||
|
||||
def on_destroy(self, widget):
|
||||
try:
|
||||
self.listener.stop()
|
||||
|
||||
Reference in New Issue
Block a user