@@ -105,6 +105,8 @@ class DialectWindow(Adw.ApplicationWindow):
105
105
current_history = 0 # for history management
106
106
107
107
# Translation-related variables
108
+ pending_trans_selection = False
109
+ trans_selection_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
@@ -509,10 +511,16 @@ def translate(self, text: str, src_lang: str | None, dest_lang: str | None):
509
511
@background_task
510
512
async def translate_selection (self , src_lang : str | None , dest_lang : str | None ):
511
513
"""Runs `translate` with the selection clipboard text"""
512
- if display := Gdk .Display .get_default ():
513
- clipboard = display .get_primary_clipboard ()
514
- if text := await clipboard .read_text_async (): # type: ignore
515
- self .translate (text , src_lang , dest_lang )
514
+ if self .props .is_active :
515
+ if display := Gdk .Display .get_default ():
516
+ clipboard = display .get_primary_clipboard ()
517
+ if text := await clipboard .read_text_async (): # type: ignore
518
+ self .translate (text , src_lang , dest_lang )
519
+ self .pending_trans_selection = False
520
+ self .trans_selection_langs = (None , None )
521
+ else :
522
+ self .pending_trans_selection = True
523
+ self .trans_selection_langs = (src_lang , dest_lang )
516
524
517
525
def save_settings (self , * args , ** kwargs ):
518
526
if not self .is_maximized ():
@@ -932,6 +940,12 @@ def _on_user_action_ended(self, _buffer):
932
940
if Settings .get ().live_translation :
933
941
self ._on_translation ()
934
942
943
+ @Gtk .Template .Callback ()
944
+ def _on_is_active_changed (self , * _args ):
945
+ if self .pending_trans_selection and self .props .is_active :
946
+ src , dest = self .trans_selection_langs
947
+ self .translate_selection (src , dest )
948
+
935
949
@Gtk .Template .Callback ()
936
950
def _on_retry_load_translator_clicked (self , * _args ):
937
951
self .reload_provider ("translator" )
0 commit comments