How to use gui_hooks reviewer_will_ show_context_menu?

[in the title I had to split up the name of the hook because the forum software won’t allow me to post long words in the title]

This hook does not work for me.

I have tried in windows and linux, with 2.1.49/qt5 and the latest dev version with qt 6.3.1.

I have this minimal add-on:

from aqt import mw
from aqt import gui_hooks
from aqt.utils import showText

print("... in general print messages are visible")

def helper(reviewer):
    showText("test")
 
def add_entry(reviewer, menu):
    print("...hook was called")
    
    # the next two lines are from add-on "Show Question"
    # https://ankiweb.net/shared/info/1566569834
    entry = [["Show Question", "", reviewer._showQuestion]]
    reviewer._addMenuItems(menu, entry)

    menutext = "show info box"
    action_nid = menu.addAction(menutext)
    qconnect(action_nid.triggered, lambda _, r=reviewer:helper(r))

gui_hooks.reviewer_will_show_context_menu.append(add_entry)

I have made this screencast to show what I’ve done and what I expect. In this screencast I use just one add-on - a stripped down version of Show Question - AnkiWeb (which didn’t work for me). This screencast was made in linux with the latest anki dev version and qt 6.3.1 and python 3.9.10.

with the add-on provided I expect to see the message printed “…hook was called” when I right click in the reviewer. This does not work. I just get the default “copy” in the context menu.

1 Like

The naming of this hook is a bit unfortunate, got confused by it my own share of times. It doesn’t modify the right-click context menu, but the menu you see when clicking on the “More” button in the reviewer bottom bar.

To modify the reviewer right-click context menu you’ll have to use webview_will_show_context_menu, and then assert that the reviewer is active through some heuristics like:

def on_webview_will_show_context_menu(webview: "AnkiWebView", menu: QMenu):
    # shaky heuristic to determine which web view we are in
    if hasattr(webview, "title"):
        if webview.title != "main webview":
            return

    if mw.state != "review" or not webview.selectedText():
        return
2 Likes

thanks. Indeed it’s obvious once you check what calls the function showContextMenu …