I’m working on customizing my Anki experience on Windows and need some advice on creating an addon. Specifically, I want to add a “Fullscreen Mode” toggle to the toolbar during card reviews. Clicking this toggle should switch Anki between fullscreen and normal mode.
I’ve been experimenting with Python code to achieve this. Here are two versions I’ve tried:
Version 1:
from aqt import mw
from aqt.qt import QAction
def toggle_fullscreen():
if mw.isFullScreen():
mw.showNormal()
else:
mw.showFullScreen()
def setup_fullscreen_action():
action = QAction("Fullscreen Mode", mw)
action.triggered.connect(toggle_fullscreen)
mw.form.menuTools.addAction(action)
setup_fullscreen_action()
This version successfully adds a “Fullscreen Mode” option to the Tools menu, and clicking it toggles Anki between fullscreen and normal mode.
But this is not what I need.
Version 2:
from aqt import mw
from aqt.qt import QAction
from aqt import gui_hooks
def toggle_fullscreen():
if mw.isFullScreen():
mw.showNormal()
else:
mw.showFullScreen()
def add_fullscreen_button(links, _):
action = QAction("Fullscreen Mode", mw)
action.triggered.connect(toggle_fullscreen)
mw.form.menuEdit.addAction(action)
links.append("Fullscreen Mode")
# Hook to add the action when the toolbar links are initialized
gui_hooks.top_toolbar_did_init_links.append(add_fullscreen_button)
This version adds a “Fullscreen Mode” button to the toolbar, but I’m having trouble making it functional. Clicking the button doesn’t toggle the fullscreen mode as intended.
Does anyone have experience with Anki addon development who could point out what I might be missing or suggest a better approach to achieve this functionality? I would greatly appreciate any help or insights you can provide!
I tried the “No Distractions Full Screen (Fixed by Shige)” add-on, and it’s indeed a great tool. Unfortunately, I encountered some issues on my Windows 10 computer, possibly due to conflicts with other add-ons I have installed.
I’m still looking for a simple, reduced version that would just add a Fullscreen Mode toggle to the top toolbar. Something similar to the flexibility available in the iOS app would be ideal.
If anyone has any further suggestions or advice on how to achieve this, I would greatly appreciate it. Thanks again for your help!
I’v been working on an Anki add-on to add a custom fullscreen toggle button to the top toolbar. I’ve managed to get the button to display, but the bridge command doesn’t seem to be working, and the toggle_fullscreen function is not being executed.
Here is my current code:
from aqt import mw
from aqt.qt import *
from aqt import gui_hooks
def toggle_fullscreen():
if mw.isFullScreen():
mw.showNormal()
else:
mw.showFullScreen()
def add_fullscreen_button(links, toolbar):
button_html = """
<button id="fullscreen-button" onclick="pycmd('toggle_fullscreen');">Fullscreen Mode</button>
"""
links.append(button_html)
def on_bridge_cmd(cmd):
if cmd == "toggle_fullscreen":
toggle_fullscreen()
def init_addon():
gui_hooks.top_toolbar_did_init_links.append(add_fullscreen_button)
mw.web.set_bridge_command(on_bridge_cmd, "custom_toggle_fullscreen")
init_addon()
Does anyone know what might be going wrong? Why isn’t the toggle_fullscreen function being executed when I click the button?
The args here appear to be incorrect. The 2nd arg should be context which shouldn’t be a string, despite the allowed type being Any:
The context in your case would the TopToolbar but I’m not sure how you’d get the active instance of that class in your current code.
Possibly, what you should be using is the webview_did_receive_js_message hook instead of set_bridge_command? That receives the context and then following the suggestion in the hook’s docstring, you’d do if not isinstance(context, aqt.toolbar.TopToolbar) (TopToolbar), I suppose.
Thank you for your input.
I think I’ve found a working code. So far I didn’t get any further debug informations.
This is the code:
from aqt import mw
from aqt.qt import *
from aqt import gui_hooks
def toggle_fullscreen():
if mw.isFullScreen():
mw.showNormal()
else:
mw.showFullScreen()
def add_fullscreen_button(links, toolbar):
button_html = """
<button onclick="pycmd('toggle_fullscreen');">Fullscreen Mode</button>
"""
links.append(button_html)
def on_js_message(handled, message, context):
if message == "toggle_fullscreen":
toggle_fullscreen()
return True, False
return handled
def init_addon():
gui_hooks.top_toolbar_did_init_links.append(add_fullscreen_button)
gui_hooks.webview_did_receive_js_message.append(on_js_message)
init_addon()
Edit:
The code is somewhat working.
It’s not perfect as the fullscreen mode is not entirely what you’d expect. It’s fullscreen but sometimes you still see the menu bar.