Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

window: translate_selection: Only get clipboard when window is active #408

Merged
merged 1 commit into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dialect/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])

Expand Down
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
26 changes: 24 additions & 2 deletions dialect/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
mufeedali marked this conversation as resolved.
Show resolved Hide resolved
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():
Expand Down Expand Up @@ -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")
Expand Down
Loading