Skip to content

Commit 28decaf

Browse files
window: translate_selection: Only get clipboard when window is active (#408)
Fixes #407
1 parent 015b91f commit 28decaf

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

dialect/main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def process_command_line(self):
118118

119119
if self.window is not None:
120120
if not text and selection:
121-
self.window.translate_selection(langs["src"], langs["dest"])
121+
self.window.queue_selection_translation(langs["src"], langs["dest"])
122122
elif text:
123123
self.window.translate(text, langs["src"], langs["dest"])
124124

dialect/window.blp

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ template $DialectWindow : Adw.ApplicationWindow {
4444
height-request: 320;
4545
focus-widget: src_text;
4646

47+
notify::is-active => $_on_is_active_changed();
48+
4749
Adw.Breakpoint {
4850
condition ("max-width: 680px")
4951
setters {

dialect/window.py

+24-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class DialectWindow(Adw.ApplicationWindow):
4646

4747
# Properties
4848
translator_loading: bool = GObject.Property(type=bool, default=True) # type: ignore
49+
selection_translation_queued: bool = GObject.Property(type=bool, default=False) # type: ignore
4950

5051
# Child widgets
5152
menu_btn: Gtk.MenuButton = Gtk.Template.Child() # type: ignore
@@ -105,6 +106,7 @@ class DialectWindow(Adw.ApplicationWindow):
105106
current_history = 0 # for history management
106107

107108
# Translation-related variables
109+
selection_translation_langs: tuple[str | None, str | None] = (None, None)
108110
next_translation: TranslationRequest | None = None # for ongoing translation
109111
translation_loading = False # for ongoing translation
110112

@@ -511,8 +513,20 @@ async def translate_selection(self, src_lang: str | None, dest_lang: str | None)
511513
"""Runs `translate` with the selection clipboard text"""
512514
if display := Gdk.Display.get_default():
513515
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)
516530

517531
def save_settings(self, *args, **kwargs):
518532
if not self.is_maximized():
@@ -932,6 +946,14 @@ def _on_user_action_ended(self, _buffer):
932946
if Settings.get().live_translation:
933947
self._on_translation()
934948

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+
935957
@Gtk.Template.Callback()
936958
def _on_retry_load_translator_clicked(self, *_args):
937959
self.reload_provider("translator")

0 commit comments

Comments
 (0)