Skip to content

Commit

Permalink
NF: remove bridge command from deck options
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur-Milchior committed Dec 4, 2024
1 parent b8b97f9 commit 5d8d3ba
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 21 deletions.
9 changes: 9 additions & 0 deletions proto/anki/frontend.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ service FrontendService {
rpc ImportDone(generic.Empty) returns (generic.Empty);

rpc SearchInBrowser(search.SearchNode) returns (generic.Empty);

// The RPC Belows are sent by the deck options.
// Force closing the deck options.
rpc deckOptionsRequireClose(generic.Empty) returns (generic.Empty);
// Query the user whether they want to close the deck options and clear the
// changes they made.
rpc deckOptionsConfirmDiscardChanges(generic.Empty) returns (generic.Empty);
// Warns python that the deck option web view is ready to receive requests.
rpc deckOptionsReady(generic.Empty) returns (generic.Empty);
}

service BackendFrontendService {}
Expand Down
13 changes: 2 additions & 11 deletions qt/aqt/deckoptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def _setup_ui(self) -> None:
addCloseShortcut(self)

self.web = AnkiWebView(kind=AnkiWebViewKind.DECK_OPTIONS)
self.web.set_bridge_command(self._on_bridge_cmd, self)
self.web.load_sveltekit_page(f"deck-options/{self._deck['id']}")
layout = QVBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
Expand All @@ -58,17 +57,9 @@ def _setup_ui(self) -> None:
without_unicode_isolation(tr.actions_options_for(val=self._deck["name"]))
)

def _on_bridge_cmd(self, cmd: str) -> None:
if cmd == "deck_options_ready":
self.deckOptionsReady()
elif cmd == "confirmDiscardChanges":
self.confirm_discard_changes()
elif cmd == "_close":
self.require_close()

def deck_options_ready(self):
def ready(self):
self._ready = True
gui_hooks.deck_options_did_load(self)
gui_hooks.did_load(self)

def closeEvent(self, evt: QCloseEvent | None) -> None:
if self._close_event_has_cleaned_up:
Expand Down
35 changes: 35 additions & 0 deletions qt/aqt/mediasrv.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class PageContext(enum.IntEnum):
REVIEWER = enum.auto()
PREVIEWER = enum.auto()
CARD_LAYOUT = enum.auto()
DECK_OPTIONS = enum.auto()
# something in /_anki/pages/
NON_LEGACY_PAGE = enum.auto()
# Do not use this if you present user content (e.g. content from cards), as it's a
Expand Down Expand Up @@ -574,6 +575,36 @@ def handle_on_main() -> None:
return b""


def require_close() -> bytes:
def handle_on_main() -> None:
window = aqt.mw.app.activeWindow()
if isinstance(window, DeckOptionsDialog):
window.require_close()

aqt.mw.taskman.run_on_main(handle_on_main)
return b""


def deck_options_confirm_discard_changes() -> bytes:
def handle_on_main() -> None:
window = aqt.mw.app.activeWindow()
if isinstance(window, DeckOptionsDialog):
window.deck_options_confirm_discard_changes()

aqt.mw.taskman.run_on_main(handle_on_main)
return b""


def deck_options_ready() -> bytes:
def handle_on_main() -> None:
window = aqt.mw.app.activeWindow()
if isinstance(window, DeckOptionsDialog):
window.deck_options_ready()

aqt.mw.taskman.run_on_main(handle_on_main)
return b""


post_handler_list = [
congrats_info,
get_deck_configs_for_update,
Expand All @@ -587,6 +618,9 @@ def handle_on_main() -> None:
import_json_file,
import_json_string,
search_in_browser,
deck_options_require_close,
deck_options_confirm_discard_changes,
deck_options_ready,
]


Expand Down Expand Up @@ -689,6 +723,7 @@ def warn() -> None:
PageContext.NON_LEGACY_PAGE,
PageContext.EDITOR,
PageContext.ADDON_PAGE,
PageContext.DECK_OPTIONS,
]:
pass
elif context == PageContext.REVIEWER and request.path in (
Expand Down
3 changes: 3 additions & 0 deletions qt/aqt/webview.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ def stdHtml(
# print(html)
import aqt.browser.previewer
import aqt.clayout
import aqt.deckoptions
import aqt.editor
import aqt.reviewer
from aqt.mediasrv import PageContext
Expand All @@ -623,6 +624,8 @@ def stdHtml(
page_context = PageContext.PREVIEWER
elif isinstance(context, aqt.clayout.CardLayout):
page_context = PageContext.CARD_LAYOUT
elif isinstance(context, aqt.deckoptions.DeckOptionsDialog):
page_context = PageContext.DECK_OPTIONS
else:
page_context = PageContext.UNKNOWN
self.setHtml(html, page_context)
Expand Down
20 changes: 10 additions & 10 deletions ts/routes/deck-options/[deckId]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import DeckOptionsPage from "../DeckOptionsPage.svelte";
import { commitEditing } from "../lib";
import type { PageData } from "./$types";
import { bridgeCommand, bridgeCommandsAvailable } from "@tslib/bridgecommand";
import {
deckOptionsRequireClose,
deckOptionsConfirmDiscardChanges,
deckOptionsReady,
} from "@generated/backend";
export let data: PageData;
let page: DeckOptionsPage;
globalThis.anki ||= {};
globalThis.anki.deckOptionsPendingChanges = async (): Promise<void> => {
await commitEditing();
if (bridgeCommandsAvailable()) {
if (await data.state.isModified()) {
bridgeCommand("confirmDiscardChanges");
} else {
bridgeCommand("_close");
}
if (await data.state.isModified()) {
deckOptionsConfirmDiscardChanges({});
} else {
deckOptionsRequireClose({});
}
};
Expand All @@ -29,9 +31,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
resolve(page);
});
data.state.resolveOriginalConfigs();
if (bridgeCommandsAvailable()) {
bridgeCommand("deckOptionsReady");
}
deckOptionsReady({});
});
</script>

Expand Down

0 comments on commit 5d8d3ba

Please sign in to comment.