From 28decaf4ea2db31b447c7f27becd9c283be290f6 Mon Sep 17 00:00:00 2001 From: Rafael Mardojai CM Date: Sun, 3 Nov 2024 12:26:29 -0500 Subject: [PATCH] window: `translate_selection`: Only get clipboard when window is active (#408) Fixes #407 --- dialect/main.py | 2 +- dialect/window.blp | 2 ++ dialect/window.py | 26 ++++++++++++++++++++++++-- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/dialect/main.py b/dialect/main.py index 77411c64..df660cdc 100755 --- a/dialect/main.py +++ b/dialect/main.py @@ -118,7 +118,7 @@ def process_command_line(self): if self.window is not None: if not text and selection: - self.window.translate_selection(langs["src"], langs["dest"]) + self.window.queue_selection_translation(langs["src"], langs["dest"]) elif text: self.window.translate(text, langs["src"], langs["dest"]) diff --git a/dialect/window.blp b/dialect/window.blp index 2e61b359..e21dadc5 100644 --- a/dialect/window.blp +++ b/dialect/window.blp @@ -44,6 +44,8 @@ template $DialectWindow : Adw.ApplicationWindow { height-request: 320; focus-widget: src_text; + notify::is-active => $_on_is_active_changed(); + Adw.Breakpoint { condition ("max-width: 680px") setters { diff --git a/dialect/window.py b/dialect/window.py index 90a14e4f..4c97c3a9 100644 --- a/dialect/window.py +++ b/dialect/window.py @@ -46,6 +46,7 @@ class DialectWindow(Adw.ApplicationWindow): # Properties translator_loading: bool = GObject.Property(type=bool, default=True) # type: ignore + selection_translation_queued: bool = GObject.Property(type=bool, default=False) # type: ignore # Child widgets menu_btn: Gtk.MenuButton = Gtk.Template.Child() # type: ignore @@ -105,6 +106,7 @@ class DialectWindow(Adw.ApplicationWindow): current_history = 0 # for history management # Translation-related variables + selection_translation_langs: tuple[str | None, str | None] = (None, None) next_translation: TranslationRequest | None = None # for ongoing translation translation_loading = False # for ongoing translation @@ -511,8 +513,20 @@ async def translate_selection(self, src_lang: str | None, dest_lang: str | None) """Runs `translate` with the selection clipboard text""" if display := Gdk.Display.get_default(): clipboard = display.get_primary_clipboard() - if text := await clipboard.read_text_async(): # type: ignore - self.translate(text, src_lang, dest_lang) + try: + if text := await clipboard.read_text_async(): # type: ignore + self.translate(text, src_lang, dest_lang) + except GLib.Error as exc: + logging.error(exc) + self.send_notification(_("Couldn’t read selection clip board!")) + + def queue_selection_translation(self, src_lang: str | None, dest_lang: str | None): + """Call `translate_selection` or queue it until the window is focused""" + if self.props.is_active: + self.translate_selection(src_lang, dest_lang) + else: + self.selection_translation_queued = True + self.selection_translation_langs = (src_lang, dest_lang) def save_settings(self, *args, **kwargs): if not self.is_maximized(): @@ -932,6 +946,14 @@ def _on_user_action_ended(self, _buffer): if Settings.get().live_translation: self._on_translation() + @Gtk.Template.Callback() + def _on_is_active_changed(self, *_args): + if self.selection_translation_queued and self.props.is_active: + src, dest = self.selection_translation_langs + self.selection_translation_queued = False + self.selection_translation_langs = (None, None) + self.translate_selection(src, dest) + @Gtk.Template.Callback() def _on_retry_load_translator_clicked(self, *_args): self.reload_provider("translator")