From 4188c0f8bf4ce17675823beab5a39d296402cce2 Mon Sep 17 00:00:00 2001 From: Ren Tatsumoto Date: Thu, 4 Apr 2024 17:06:54 +0300 Subject: [PATCH] add new action: browser search --- config.json | 3 ++- context_menu.py | 23 ++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/config.json b/config.json index 8c8b509..7e708e9 100644 --- a/config.json +++ b/config.json @@ -70,7 +70,8 @@ "to_katakana": true, "to_hiragana": true, "literal_pronunciation": true, - "look_up_word": true + "look_up_word": true, + "browser_search": true }, "toolbar": { "generate_all_button": { diff --git a/context_menu.py b/context_menu.py index d975332..3347b4a 100644 --- a/context_menu.py +++ b/context_menu.py @@ -4,6 +4,7 @@ import abc from typing import Optional +import aqt from aqt import gui_hooks from aqt.editor import EditorWebView, Editor from aqt.qt import * @@ -29,6 +30,13 @@ def __init__(self, editor: Editor = None, webview: AnkiWebView = None): self.editor = editor self.webview = webview or editor.web + def _parent_window(self) -> QWidget: + if self.editor: + return self.editor.parentWindow + if self.webview: + return self.webview.window() or aqt.mw + raise RuntimeError("Parent should be passed to instance.") + @classmethod def enabled(cls) -> bool: return cfg.context_menu.get(cls.key) @@ -102,7 +110,20 @@ def action(self, text: str) -> None: try: lookup_goldendict(text) except RuntimeError as ex: - tooltip(str(ex)) + tooltip(str(ex), parent=self._parent_window()) + + +class BrowserSearch(ContextMenuAction): + key = "browser_search" + label = "Search Collection" + shown_when_not_editing = True + + def action(self, search_text: str) -> None: + if not search_text: + return tooltip("Empty selection.", parent=self._parent_window()) + browser = aqt.dialogs.open("Browser", aqt.mw) # browser requires mw (AnkiQt) to be passed as parent + browser.activateWindow() + browser.search_for(search_text) def add_editor_context_menu_items(webview: EditorWebView, menu: QMenu) -> None: