@@ -105,6 +105,7 @@ class DialectWindow(Adw.ApplicationWindow):
105
105
current_history = 0 # for history management
106
106
107
107
# Translation-related variables
108
+ pending_trans_selection = False
108
109
next_translation : TranslationRequest | None = None # for ongoing translation
109
110
translation_loading = False # for ongoing translation
110
111
@@ -509,10 +510,15 @@ def translate(self, text: str, src_lang: str | None, dest_lang: str | None):
509
510
@background_task
510
511
async def translate_selection (self , src_lang : str | None , dest_lang : str | None ):
511
512
"""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 )
513
+ if self .props .is_active :
514
+ if display := Gdk .Display .get_default ():
515
+ clipboard = display .get_primary_clipboard ()
516
+ if text := await clipboard .read_text_async (): # type: ignore
517
+ self .translate (text , src_lang , dest_lang )
518
+ self .pending_trans_selection = False
519
+ else :
520
+ self .translate (self .src_buffer .props .text , src_lang , dest_lang ) # Just set the langs
521
+ self .pending_trans_selection = True
516
522
517
523
def save_settings (self , * args , ** kwargs ):
518
524
if not self .is_maximized ():
@@ -932,6 +938,11 @@ def _on_user_action_ended(self, _buffer):
932
938
if Settings .get ().live_translation :
933
939
self ._on_translation ()
934
940
941
+ @Gtk .Template .Callback ()
942
+ def _on_is_active_changed (self , * _args ):
943
+ if self .pending_trans_selection and self .props .is_active :
944
+ self .translate_selection (self .src_lang_selector .selected , self .dest_lang_selector .selected )
945
+
935
946
@Gtk .Template .Callback ()
936
947
def _on_retry_load_translator_clicked (self , * _args ):
937
948
self .reload_provider ("translator" )
0 commit comments