Skip to content

Commit

Permalink
add new action: browser search
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsumoto-ren committed Apr 4, 2024
1 parent a9b70b7 commit 4188c0f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
23 changes: 22 additions & 1 deletion context_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 4188c0f

Please sign in to comment.