@@ -46,6 +46,7 @@ class DialectWindow(Adw.ApplicationWindow):
46
46
47
47
# Properties
48
48
translator_loading : bool = GObject .Property (type = bool , default = True ) # type: ignore
49
+ selection_translation_queued : bool = GObject .Property (type = bool , default = False ) # type: ignore
49
50
50
51
# Child widgets
51
52
menu_btn : Gtk .MenuButton = Gtk .Template .Child () # type: ignore
@@ -105,6 +106,7 @@ class DialectWindow(Adw.ApplicationWindow):
105
106
current_history = 0 # for history management
106
107
107
108
# Translation-related variables
109
+ selection_translation_langs : tuple [str | None , str | None ] = (None , None )
108
110
next_translation : TranslationRequest | None = None # for ongoing translation
109
111
translation_loading = False # for ongoing translation
110
112
@@ -511,8 +513,20 @@ async def translate_selection(self, src_lang: str | None, dest_lang: str | None)
511
513
"""Runs `translate` with the selection clipboard text"""
512
514
if display := Gdk .Display .get_default ():
513
515
clipboard = display .get_primary_clipboard ()
514
- if text := await clipboard .read_text_async (): # type: ignore
515
- self .translate (text , src_lang , dest_lang )
516
+ try :
517
+ if text := await clipboard .read_text_async (): # type: ignore
518
+ self .translate (text , src_lang , dest_lang )
519
+ except GLib .Error as exc :
520
+ logging .error (exc )
521
+ self .send_notification (_ ("Couldn’t read selection clip board!" ))
522
+
523
+ def queue_selection_translation (self , src_lang : str | None , dest_lang : str | None ):
524
+ """Call `translate_selection` or queue it until the window is focused"""
525
+ if self .props .is_active :
526
+ self .translate_selection (src_lang , dest_lang )
527
+ else :
528
+ self .selection_translation_queued = True
529
+ self .selection_translation_langs = (src_lang , dest_lang )
516
530
517
531
def save_settings (self , * args , ** kwargs ):
518
532
if not self .is_maximized ():
@@ -932,6 +946,14 @@ def _on_user_action_ended(self, _buffer):
932
946
if Settings .get ().live_translation :
933
947
self ._on_translation ()
934
948
949
+ @Gtk .Template .Callback ()
950
+ def _on_is_active_changed (self , * _args ):
951
+ if self .selection_translation_queued and self .props .is_active :
952
+ src , dest = self .selection_translation_langs
953
+ self .selection_translation_queued = False
954
+ self .selection_translation_langs = (None , None )
955
+ self .translate_selection (src , dest )
956
+
935
957
@Gtk .Template .Callback ()
936
958
def _on_retry_load_translator_clicked (self , * _args ):
937
959
self .reload_provider ("translator" )
0 commit comments