Create the hook will_show_web to control html5 media elements with Javascript (#2340)

* Replaced ankimedia object directly call by addon specific hook

# Conflicts:
#	qt/aqt/browser/previewer.py
#	qt/aqt/clayout.py
#	qt/aqt/reviewer.py

* Replaced ankimedia.js by addon specific hook

# Conflicts:
#	qt/aqt/browser/previewer.py
#	qt/aqt/clayout.py
#	qt/aqt/main.py

* Create specific location name for each hook to reuse control

* Created the card_review_webview_did_init hook

* Extended the hook card_will_show to replace will_show_web

The new hook card_will_show_state takes three new arguments

* Created the hook audio_did_pause_or_unpause to replace will_show_web

The new hook is called when audio toggle pause is called

* Created the hook audio_will_replay to replace will_show_web

The new hook is called when the audio is replayed by the user.

* Created the hook previewer_will_redraw_after_show_both_sides_toggled

to replace will_show_web.
The new hook fully replaces the last uses of will_show_web.

* Replaced card_will_show_state hook with reviewer_did_init and

equivalents. Instead of receiving the required state, it access it
by caching the object values with hooks as reviewer_did_init.
This commit is contained in:
evandrocoan 2023-02-13 01:50:26 -03:00 committed by GitHub
parent ad74a01491
commit d9f1e22648
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 0 deletions

View file

@ -83,6 +83,8 @@ class Previewer(QDialog):
self.bbox = QDialogButtonBox() self.bbox = QDialogButtonBox()
self.bbox.setLayoutDirection(Qt.LayoutDirection.LeftToRight) self.bbox.setLayoutDirection(Qt.LayoutDirection.LeftToRight)
gui_hooks.card_review_webview_did_init(self._web, AnkiWebViewKind.PREVIEWER)
self._replay = self.bbox.addButton( self._replay = self.bbox.addButton(
tr.actions_replay_audio(), QDialogButtonBox.ButtonRole.ActionRole tr.actions_replay_audio(), QDialogButtonBox.ButtonRole.ActionRole
) )
@ -110,6 +112,8 @@ class Previewer(QDialog):
self._on_close() self._on_close()
def _on_replay_audio(self) -> None: def _on_replay_audio(self) -> None:
gui_hooks.audio_will_replay(self._web, self.card(), self._state == "question")
if self._state == "question": if self._state == "question":
replay_audio(self.card(), True) replay_audio(self.card(), True)
elif self._state == "answer": elif self._state == "answer":
@ -240,6 +244,10 @@ class Previewer(QDialog):
def _on_show_both_sides(self, toggle: bool) -> None: def _on_show_both_sides(self, toggle: bool) -> None:
self._show_both_sides = toggle self._show_both_sides = toggle
self.mw.col.set_config_bool(Config.Bool.PREVIEW_BOTH_SIDES, toggle) self.mw.col.set_config_bool(Config.Bool.PREVIEW_BOTH_SIDES, toggle)
gui_hooks.previewer_will_redraw_after_show_both_sides_toggled(
self._web, self.card(), self._state == "question", toggle
)
if self._state == "answer" and not toggle: if self._state == "answer" and not toggle:
self._state = "question" self._state = "question"
self.render_card() self.render_card()

View file

@ -359,6 +359,10 @@ class CardLayout(QDialog):
self.preview_web.eval("_blockDefaultDragDropBehavior();") self.preview_web.eval("_blockDefaultDragDropBehavior();")
self.preview_web.set_bridge_command(self._on_bridge_cmd, self) self.preview_web.set_bridge_command(self._on_bridge_cmd, self)
gui_hooks.card_review_webview_did_init(
self.preview_web, AnkiWebViewKind.CARD_LAYOUT
)
if self._isCloze(): if self._isCloze():
nums = list(self.note.cloze_numbers_in_fields()) nums = list(self.note.cloze_numbers_in_fields())
if self.ord + 1 not in nums: if self.ord + 1 not in nums:

View file

@ -248,6 +248,8 @@ class AnkiQt(QMainWindow):
def finish_ui_setup(self) -> None: def finish_ui_setup(self) -> None:
"Actions that are deferred until after add-on loading." "Actions that are deferred until after add-on loading."
self.toolbar.draw() self.toolbar.draw()
# add-ons are only available here after setupAddons
gui_hooks.reviewer_did_init(self.reviewer)
def setupProfileAfterWebviewsLoaded(self) -> None: def setupProfileAfterWebviewsLoaded(self) -> None:
for w in (self.web, self.bottomWeb): for w in (self.web, self.bottomWeb):
@ -901,6 +903,8 @@ title="{}" {}>{}</button>""".format(
for webview in self.web, self.bottomWeb: for webview in self.web, self.bottomWeb:
webview.force_load_hack() webview.force_load_hack()
gui_hooks.card_review_webview_did_init(self.web, AnkiWebViewKind.MAIN)
def closeAllWindows(self, onsuccess: Callable) -> None: def closeAllWindows(self, onsuccess: Callable) -> None:
aqt.dialogs.closeAll(onsuccess) aqt.dialogs.closeAll(onsuccess)

View file

@ -291,6 +291,7 @@ class Reviewer:
replay_audio(self.card, True) replay_audio(self.card, True)
elif self.state == "answer": elif self.state == "answer":
replay_audio(self.card, False) replay_audio(self.card, False)
gui_hooks.audio_will_replay(self.web, self.card, self.state == "question")
# Initializing the webview # Initializing the webview
########################################################################## ##########################################################################
@ -505,6 +506,7 @@ class Reviewer:
def on_pause_audio(self) -> None: def on_pause_audio(self) -> None:
av_player.toggle_pause() av_player.toggle_pause()
gui_hooks.audio_did_pause_or_unpause(self.web)
seek_secs = 5 seek_secs = 5

View file

@ -215,6 +215,11 @@ hooks = [
name="reviewer_will_bury_card", name="reviewer_will_bury_card",
args=["id: int"], args=["id: int"],
), ),
Hook(
name="audio_did_pause_or_unpause",
args=["webview: aqt.webview.AnkiWebView"],
doc="""Called when the audio is paused or unpaused.""",
),
# Debug # Debug
################### ###################
Hook( Hook(
@ -238,8 +243,21 @@ hooks = [
doc="""Allow to change the display of the card layout. After most values are doc="""Allow to change the display of the card layout. After most values are
set and before the window is actually shown.""", set and before the window is actually shown.""",
), ),
# Reviewer
###################
Hook(
name="reviewer_did_init",
args=["reviewer: aqt.reviewer.Reviewer"],
doc="""Called after the reviewer is initialized.""",
),
# Multiple windows # Multiple windows
################### ###################
# reviewer and previewer
Hook(
name="audio_will_replay",
args=["webview: aqt.webview.AnkiWebView", "card: Card", "is_front_side: bool"],
doc="""Called when the user uses the 'replay audio' action, but not when they click on a play button.""",
),
# reviewer, clayout and browser # reviewer, clayout and browser
Hook( Hook(
name="card_will_show", name="card_will_show",
@ -248,6 +266,15 @@ hooks = [
legacy_hook="prepareQA", legacy_hook="prepareQA",
doc="Can modify card text before review/preview.", doc="Can modify card text before review/preview.",
), ),
# reviewer, main and clayout
Hook(
name="card_review_webview_did_init",
args=[
"webview: aqt.webview.AnkiWebView",
"kind: aqt.webview.AnkiWebViewKind",
],
doc="Called when initializing the webview for the review screen, the card layout screen, and the preview screen.",
),
# Deck browser # Deck browser
################### ###################
Hook( Hook(
@ -527,6 +554,16 @@ hooks = [
args=["previewer: aqt.browser.previewer.Previewer"], args=["previewer: aqt.browser.previewer.Previewer"],
doc="""Called after the previewer is initialized.""", doc="""Called after the previewer is initialized.""",
), ),
Hook(
name="previewer_will_redraw_after_show_both_sides_toggled",
args=[
"webview: aqt.webview.AnkiWebView",
"card: Card",
"is_front_side: bool",
"show_both_sides: bool",
],
doc="""Called when the checkbox <show both sides> is toggled by the user.""",
),
# Main window states # Main window states
################### ###################
# these refer to things like deckbrowser, overview and reviewer state, # these refer to things like deckbrowser, overview and reviewer state,