Skip to content

Commit

Permalink
window: translate_selection: Only get clipboard when window is active
Browse files Browse the repository at this point in the history
Fixes #407
  • Loading branch information
rafaelmardojai committed Nov 1, 2024
1 parent 015b91f commit b948c4f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
2 changes: 2 additions & 0 deletions dialect/window.blp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 15 additions & 4 deletions dialect/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class DialectWindow(Adw.ApplicationWindow):
current_history = 0 # for history management

# Translation-related variables
pending_trans_selection = False
next_translation: TranslationRequest | None = None # for ongoing translation
translation_loading = False # for ongoing translation

Expand Down Expand Up @@ -509,10 +510,15 @@ def translate(self, text: str, src_lang: str | None, dest_lang: str | None):
@background_task
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)
if self.props.is_active:
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)
self.pending_trans_selection = False
else:
self.translate(self.src_buffer.props.text, src_lang, dest_lang) # Just set the langs
self.pending_trans_selection = True

def save_settings(self, *args, **kwargs):
if not self.is_maximized():
Expand Down Expand Up @@ -932,6 +938,11 @@ 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.pending_trans_selection and self.props.is_active:
self.translate_selection(self.src_lang_selector.selected, self.dest_lang_selector.selected)

@Gtk.Template.Callback()
def _on_retry_load_translator_clicked(self, *_args):
self.reload_provider("translator")
Expand Down

0 comments on commit b948c4f

Please sign in to comment.