Skip to content

Commit

Permalink
Refactor ReviewerSidebar to update theme after page load
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroven committed Dec 27, 2024
1 parent 4a798ae commit b13cda5
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions ankihub/gui/reviewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def _update_header_webview(self):
html = get_header_webview_html(
self.resources,
self.current_active_tab_url,
f"{RESOURCE_TYPE_TO_DISPLAY_NAME[self.resource_type]} Viewer",
f"{RESOURCE_TYPE_TO_DISPLAY_NAME[self.resource_type]}",
_ankihub_theme(),
)

Expand Down Expand Up @@ -296,20 +296,31 @@ def update_chatbot_header_on_sidebar(self):

def update_sidebar_content_with_chatbot_url(self) -> None:
ah_nid = ankihub_db.ankihub_nid_for_anki_nid(self.reviewer.card.nid)
self._update_content_webview_theme()

self.url_pages[ResourceType.CHATBOT].setUrl(
aqt.QUrl(f"{config.app_url}/ai/chatbot/{ah_nid}/?is_on_anki=true")
)
if self.content_webview.page() != self.url_pages[ResourceType.CHATBOT]:
self.content_webview.setPage(self.url_pages[ResourceType.CHATBOT])
self._update_theme_after_page_load()
self.last_card_ah_nid = ah_nid

def _update_content_webview_theme(self):
self.content_webview.eval(
f"localStorage.setItem('theme', '{_ankihub_theme()}');"
)

def _update_theme_after_page_load(self):
from .js_message_handling import _post_message_to_ankihub_js

qconnect(
self.content_webview.loadFinished,
lambda ok: _post_message_to_ankihub_js(
message={"changeTheme": _ankihub_theme()},
web=self.content_webview,
),
)

def _on_url_page_loaded(self, ok: bool) -> None:
if ok:
return
Expand Down

0 comments on commit b13cda5

Please sign in to comment.