2019-02-05 04:59:03 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
2012-12-21 08:51:59 +01:00
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
2021-04-13 10:45:05 +02:00
|
|
|
|
2020-02-08 23:59:29 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-05-18 09:47:51 +02:00
|
|
|
import functools
|
2019-03-04 08:25:19 +01:00
|
|
|
import json
|
2021-05-17 08:59:02 +02:00
|
|
|
import random
|
2019-12-20 10:19:03 +01:00
|
|
|
import re
|
2021-05-11 03:37:08 +02:00
|
|
|
from dataclasses import dataclass
|
2021-03-16 09:30:54 +01:00
|
|
|
from enum import Enum, auto
|
Migrate to protobuf-es (#2547)
* Fix .no-reduce-motion missing from graphs spinner, and not being honored
* Begin migration from protobuf.js -> protobuf-es
Motivation:
- Protobuf-es has a nicer API: messages are represented as classes, and
fields which should exist are not marked as nullable.
- As it uses modules, only the proto messages we actually use get included
in our bundle output. Protobuf.js put everything in a namespace, which
prevented tree-shaking, and made it awkward to access inner messages.
- ./run after touching a proto file drops from about 8s to 6s on my machine. The tradeoff
is slower decoding/encoding (#2043), but that was mainly a concern for the
graphs page, and was unblocked by
https://github.com/ankitects/anki/commit/37151213cd9d431f449ba4b3bc4c0329a1d9af78
Approach/notes:
- We generate the new protobuf-es interface in addition to existing
protobuf.js interface, so we can migrate a module at a time, starting
with the graphs module.
- rslib:proto now generates RPC methods for TS in addition to the Python
interface. The input-arg-unrolling behaviour of the Python generation is
not required here, as we declare the input arg as a PlainMessage<T>, which
marks it as requiring all fields to be provided.
- i64 is represented as bigint in protobuf-es. We were using a patch to
protobuf.js to get it to output Javascript numbers instead of long.js
types, but now that our supported browser versions support bigint, it's
probably worth biting the bullet and migrating to bigint use. Our IDs
fit comfortably within MAX_SAFE_INTEGER, but that may not hold for future
fields we add.
- Oneofs are handled differently in protobuf-es, and are going to need
some refactoring.
Other notable changes:
- Added a --mkdir arg to our build runner, so we can create a dir easily
during the build on Windows.
- Simplified the preference handling code, by wrapping the preferences
in an outer store, instead of a separate store for each individual
preference. This means a change to one preference will trigger a redraw
of all components that depend on the preference store, but the redrawing
is cheap after moving the data processing to Rust, and it makes the code
easier to follow.
- Drop async(Reactive).ts in favour of more explicit handling with await
blocks/updating.
- Renamed add_inputs_to_group() -> add_dependency(), and fixed it not adding
dependencies to parent groups. Renamed add() -> add_action() for clarity.
* Remove a couple of unused proto imports
* Migrate card info
* Migrate congrats, image occlusion, and tag editor
+ Fix imports for multi-word proto files.
* Migrate change-notetype
* Migrate deck options
* Bump target to es2020; simplify ts lib list
Have used caniuse.com to confirm Chromium 77, iOS 14.5 and the Chrome
on Android support the full es2017-es2020 features.
* Migrate import-csv
* Migrate i18n and fix missing output types in .js
* Migrate custom scheduling, and remove protobuf.js
To mostly maintain our old API contract, we make use of protobuf-es's
ability to convert to JSON, which follows the same format as protobuf.js
did. It doesn't cover all case: users who were previously changing the
variant of a type will need to update their code, as assigning to a new
variant no longer automatically removes the old one, which will cause an
error when we try to convert back from JSON. But I suspect the large majority
of users are adjusting the current variant rather than creating a new one,
and this saves us having to write proxy wrappers, so it seems like a
reasonable compromise.
One other change I made at the same time was to rename value->kind for
the oneofs in our custom study protos, as 'value' was easily confused
with the 'case/value' output that protobuf-es has.
With protobuf.js codegen removed, touching a proto file and invoking
./run drops from about 8s to 6s.
This closes #2043.
* Allow tree-shaking on protobuf types
* Display backend error messages in our ts alert()
* Make sourcemap generation opt-in for ts-run
Considerably slows down build, and not used most of the time.
2023-06-14 14:47:37 +02:00
|
|
|
from typing import Any, Literal, Match, Sequence, cast
|
2020-02-27 04:11:21 +01:00
|
|
|
|
2021-12-31 07:45:30 +01:00
|
|
|
import aqt
|
2022-02-13 04:40:47 +01:00
|
|
|
import aqt.browser
|
|
|
|
import aqt.operations
|
2021-03-27 12:38:20 +01:00
|
|
|
from anki.cards import Card, CardId
|
2021-04-06 02:14:11 +02:00
|
|
|
from anki.collection import Config, OpChanges, OpChangesWithCount
|
2022-03-09 07:51:41 +01:00
|
|
|
from anki.scheduler.base import ScheduleCardsAsNew
|
2022-09-05 08:48:01 +02:00
|
|
|
from anki.scheduler.v3 import CardAnswer, QueuedCards
|
2021-05-11 03:37:08 +02:00
|
|
|
from anki.scheduler.v3 import Scheduler as V3Scheduler
|
Migrate to protobuf-es (#2547)
* Fix .no-reduce-motion missing from graphs spinner, and not being honored
* Begin migration from protobuf.js -> protobuf-es
Motivation:
- Protobuf-es has a nicer API: messages are represented as classes, and
fields which should exist are not marked as nullable.
- As it uses modules, only the proto messages we actually use get included
in our bundle output. Protobuf.js put everything in a namespace, which
prevented tree-shaking, and made it awkward to access inner messages.
- ./run after touching a proto file drops from about 8s to 6s on my machine. The tradeoff
is slower decoding/encoding (#2043), but that was mainly a concern for the
graphs page, and was unblocked by
https://github.com/ankitects/anki/commit/37151213cd9d431f449ba4b3bc4c0329a1d9af78
Approach/notes:
- We generate the new protobuf-es interface in addition to existing
protobuf.js interface, so we can migrate a module at a time, starting
with the graphs module.
- rslib:proto now generates RPC methods for TS in addition to the Python
interface. The input-arg-unrolling behaviour of the Python generation is
not required here, as we declare the input arg as a PlainMessage<T>, which
marks it as requiring all fields to be provided.
- i64 is represented as bigint in protobuf-es. We were using a patch to
protobuf.js to get it to output Javascript numbers instead of long.js
types, but now that our supported browser versions support bigint, it's
probably worth biting the bullet and migrating to bigint use. Our IDs
fit comfortably within MAX_SAFE_INTEGER, but that may not hold for future
fields we add.
- Oneofs are handled differently in protobuf-es, and are going to need
some refactoring.
Other notable changes:
- Added a --mkdir arg to our build runner, so we can create a dir easily
during the build on Windows.
- Simplified the preference handling code, by wrapping the preferences
in an outer store, instead of a separate store for each individual
preference. This means a change to one preference will trigger a redraw
of all components that depend on the preference store, but the redrawing
is cheap after moving the data processing to Rust, and it makes the code
easier to follow.
- Drop async(Reactive).ts in favour of more explicit handling with await
blocks/updating.
- Renamed add_inputs_to_group() -> add_dependency(), and fixed it not adding
dependencies to parent groups. Renamed add() -> add_action() for clarity.
* Remove a couple of unused proto imports
* Migrate card info
* Migrate congrats, image occlusion, and tag editor
+ Fix imports for multi-word proto files.
* Migrate change-notetype
* Migrate deck options
* Bump target to es2020; simplify ts lib list
Have used caniuse.com to confirm Chromium 77, iOS 14.5 and the Chrome
on Android support the full es2017-es2020 features.
* Migrate import-csv
* Migrate i18n and fix missing output types in .js
* Migrate custom scheduling, and remove protobuf.js
To mostly maintain our old API contract, we make use of protobuf-es's
ability to convert to JSON, which follows the same format as protobuf.js
did. It doesn't cover all case: users who were previously changing the
variant of a type will need to update their code, as assigning to a new
variant no longer automatically removes the old one, which will cause an
error when we try to convert back from JSON. But I suspect the large majority
of users are adjusting the current variant rather than creating a new one,
and this saves us having to write proxy wrappers, so it seems like a
reasonable compromise.
One other change I made at the same time was to rename value->kind for
the oneofs in our custom study protos, as 'value' was easily confused
with the 'case/value' output that protobuf-es has.
With protobuf.js codegen removed, touching a proto file and invoking
./run drops from about 8s to 6s.
This closes #2043.
* Allow tree-shaking on protobuf types
* Display backend error messages in our ts alert()
* Make sourcemap generation opt-in for ts-run
Considerably slows down build, and not used most of the time.
2023-06-14 14:47:37 +02:00
|
|
|
from anki.scheduler.v3 import (
|
|
|
|
SchedulingContext,
|
|
|
|
SchedulingStates,
|
|
|
|
SetSchedulingStatesRequest,
|
|
|
|
)
|
2021-03-18 03:06:45 +01:00
|
|
|
from anki.tags import MARKED_TAG
|
2022-02-25 04:39:23 +01:00
|
|
|
from anki.types import assert_exhaustive
|
2020-01-13 09:37:08 +01:00
|
|
|
from aqt import AnkiQt, gui_hooks
|
2021-10-16 23:38:11 +02:00
|
|
|
from aqt.browser.card_info import PreviousReviewerCardInfo, ReviewerCardInfo
|
2021-05-27 05:11:20 +02:00
|
|
|
from aqt.deckoptions import confirm_deck_then_display_options
|
2021-04-03 08:26:10 +02:00
|
|
|
from aqt.operations.card import set_card_flag
|
|
|
|
from aqt.operations.note import remove_notes
|
|
|
|
from aqt.operations.scheduling import (
|
2021-05-11 03:37:08 +02:00
|
|
|
answer_card,
|
undoable ops now return changes directly; add new *_ops.py files
- Introduced a new transact() method that wraps the return value
in a separate struct that describes the changes that were made.
- Changes are now gathered from the undo log, so we don't need to
guess at what was changed - eg if update_note() is called with identical
note contents, no changes are returned. Card changes will only be set
if cards were actually generated by the update_note() call, and tag
will only be set if a new tag was added.
- mw.perform_op() has been updated to expect the op to return the changes,
or a structure with the changes in it, and it will use them to fire the
change hook, instead of fetching the changes from undo_status(), so there
is no risk of race conditions.
- the various calls to mw.perform_op() have been split into separate
files like card_ops.py. Aside from making the code cleaner, this works
around a rather annoying issue with mypy. Because we run it with
no_strict_optional, mypy is happy to accept an operation that returns None,
despite the type signature saying it requires changes to be returned.
Turning no_strict_optional on for the whole codebase is not practical
at the moment, but we can enable it for individual files.
Still todo:
- The cursor keeps moving back to the start of a field when typing -
we need to ignore the refresh hook when we are the initiator.
- The busy cursor icon should probably be delayed a few hundreds ms.
- Still need to think about a nicer way of handling saveNow()
- op_made_changes(), op_affects_study_queue() might be better embedded
as properties in the object instead
2021-03-16 05:26:42 +01:00
|
|
|
bury_cards,
|
2021-04-06 08:38:42 +02:00
|
|
|
bury_notes,
|
2022-02-07 10:56:31 +01:00
|
|
|
forget_cards,
|
undoable ops now return changes directly; add new *_ops.py files
- Introduced a new transact() method that wraps the return value
in a separate struct that describes the changes that were made.
- Changes are now gathered from the undo log, so we don't need to
guess at what was changed - eg if update_note() is called with identical
note contents, no changes are returned. Card changes will only be set
if cards were actually generated by the update_note() call, and tag
will only be set if a new tag was added.
- mw.perform_op() has been updated to expect the op to return the changes,
or a structure with the changes in it, and it will use them to fire the
change hook, instead of fetching the changes from undo_status(), so there
is no risk of race conditions.
- the various calls to mw.perform_op() have been split into separate
files like card_ops.py. Aside from making the code cleaner, this works
around a rather annoying issue with mypy. Because we run it with
no_strict_optional, mypy is happy to accept an operation that returns None,
despite the type signature saying it requires changes to be returned.
Turning no_strict_optional on for the whole codebase is not practical
at the moment, but we can enable it for individual files.
Still todo:
- The cursor keeps moving back to the start of a field when typing -
we need to ignore the refresh hook when we are the initiator.
- The busy cursor icon should probably be delayed a few hundreds ms.
- Still need to think about a nicer way of handling saveNow()
- op_made_changes(), op_affects_study_queue() might be better embedded
as properties in the object instead
2021-03-16 05:26:42 +01:00
|
|
|
set_due_date_dialog,
|
|
|
|
suspend_cards,
|
|
|
|
suspend_note,
|
|
|
|
)
|
2021-04-03 08:26:10 +02:00
|
|
|
from aqt.operations.tag import add_tags_to_notes, remove_tags_from_notes
|
|
|
|
from aqt.profiles import VideoDriver
|
|
|
|
from aqt.qt import *
|
2020-12-16 10:09:45 +01:00
|
|
|
from aqt.sound import av_player, play_clicked_audio, record_audio
|
2020-01-23 06:08:10 +01:00
|
|
|
from aqt.theme import theme_manager
|
2020-01-22 01:46:35 +01:00
|
|
|
from aqt.toolbar import BottomBar
|
2022-08-19 04:43:17 +02:00
|
|
|
from aqt.utils import (
|
|
|
|
askUserDialog,
|
|
|
|
downArrow,
|
|
|
|
qtMenuShortcutWorkaround,
|
|
|
|
show_warning,
|
|
|
|
tooltip,
|
|
|
|
tr,
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2013-10-22 07:28:45 +02:00
|
|
|
|
2021-03-16 09:30:54 +01:00
|
|
|
class RefreshNeeded(Enum):
|
|
|
|
NOTE_TEXT = auto()
|
|
|
|
QUEUES = auto()
|
2022-02-25 04:39:23 +01:00
|
|
|
FLAG = auto()
|
2021-03-16 09:30:54 +01:00
|
|
|
|
|
|
|
|
2020-02-08 23:59:29 +01:00
|
|
|
class ReviewerBottomBar:
|
2020-02-27 04:11:21 +01:00
|
|
|
def __init__(self, reviewer: Reviewer) -> None:
|
2020-02-08 23:59:29 +01:00
|
|
|
self.reviewer = reviewer
|
|
|
|
|
2020-04-13 01:04:30 +02:00
|
|
|
|
2020-04-13 00:59:36 +02:00
|
|
|
def replay_audio(card: Card, question_side: bool) -> None:
|
|
|
|
if question_side:
|
|
|
|
av_player.play_tags(card.question_av_tags())
|
|
|
|
else:
|
|
|
|
tags = card.answer_av_tags()
|
|
|
|
if card.replay_question_audio_on_answer_side():
|
|
|
|
tags = card.question_av_tags() + tags
|
|
|
|
av_player.play_tags(tags)
|
2020-02-08 23:59:29 +01:00
|
|
|
|
2020-04-13 01:04:30 +02:00
|
|
|
|
2021-05-11 03:37:08 +02:00
|
|
|
@dataclass
|
|
|
|
class V3CardInfo:
|
2022-09-05 08:48:01 +02:00
|
|
|
"""Stores the top of the card queue for the v3 scheduler.
|
2021-05-11 03:37:08 +02:00
|
|
|
|
2022-09-05 08:48:01 +02:00
|
|
|
This includes current and potential next states of the displayed card,
|
|
|
|
which may be mutated by a user's custom scheduling.
|
2021-05-11 03:37:08 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
queued_cards: QueuedCards
|
2022-09-05 08:48:01 +02:00
|
|
|
states: SchedulingStates
|
2023-03-16 07:31:00 +01:00
|
|
|
context: SchedulingContext
|
2021-05-11 03:37:08 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_queue(queued_cards: QueuedCards) -> V3CardInfo:
|
2022-09-05 08:48:01 +02:00
|
|
|
top_card = queued_cards.cards[0]
|
|
|
|
states = top_card.states
|
|
|
|
states.current.custom_data = top_card.card.custom_data
|
2021-05-11 03:37:08 +02:00
|
|
|
return V3CardInfo(
|
2023-03-16 07:31:00 +01:00
|
|
|
queued_cards=queued_cards, states=states, context=top_card.context
|
2021-05-11 03:37:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
def top_card(self) -> QueuedCards.QueuedCard:
|
|
|
|
return self.queued_cards.cards[0]
|
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def counts(self) -> tuple[int, list[int]]:
|
2021-05-11 03:37:08 +02:00
|
|
|
"Returns (idx, counts)."
|
|
|
|
counts = [
|
|
|
|
self.queued_cards.new_count,
|
|
|
|
self.queued_cards.learning_count,
|
|
|
|
self.queued_cards.review_count,
|
|
|
|
]
|
|
|
|
card = self.top_card()
|
|
|
|
if card.queue == QueuedCards.NEW:
|
|
|
|
idx = 0
|
|
|
|
elif card.queue == QueuedCards.LEARNING:
|
|
|
|
idx = 1
|
|
|
|
else:
|
|
|
|
idx = 2
|
|
|
|
return idx, counts
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def rating_from_ease(ease: int) -> CardAnswer.Rating.V:
|
|
|
|
if ease == 1:
|
|
|
|
return CardAnswer.AGAIN
|
|
|
|
elif ease == 2:
|
|
|
|
return CardAnswer.HARD
|
|
|
|
elif ease == 3:
|
|
|
|
return CardAnswer.GOOD
|
|
|
|
else:
|
|
|
|
return CardAnswer.EASY
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-05-11 03:37:08 +02:00
|
|
|
class Reviewer:
|
2020-01-13 04:57:51 +01:00
|
|
|
def __init__(self, mw: AnkiQt) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw = mw
|
|
|
|
self.web = mw.web
|
2021-10-03 10:59:42 +02:00
|
|
|
self.card: Card | None = None
|
|
|
|
self.previous_card: Card | None = None
|
|
|
|
self._answeredIds: list[CardId] = []
|
|
|
|
self._recordedAudio: str | None = None
|
2020-02-27 04:11:21 +01:00
|
|
|
self.typeCorrect: str = None # web init happens before this is set
|
2021-12-14 06:48:02 +01:00
|
|
|
self.state: Literal["question", "answer", "transition", None] = None
|
2021-10-03 10:59:42 +02:00
|
|
|
self._refresh_needed: RefreshNeeded | None = None
|
|
|
|
self._v3: V3CardInfo | None = None
|
2022-02-25 06:26:06 +01:00
|
|
|
self._state_mutation_key = str(random.randint(0, 2**64 - 1))
|
2020-01-22 01:46:35 +01:00
|
|
|
self.bottom = BottomBar(mw, mw.bottomWeb)
|
2021-10-16 23:38:11 +02:00
|
|
|
self._card_info = ReviewerCardInfo(self.mw)
|
|
|
|
self._previous_card_info = PreviousReviewerCardInfo(self.mw)
|
2023-03-16 07:31:00 +01:00
|
|
|
self._states_mutated = True
|
2023-08-07 07:10:47 +02:00
|
|
|
self._reps: int = None
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-02-27 04:11:21 +01:00
|
|
|
def show(self) -> None:
|
2023-09-16 08:09:26 +02:00
|
|
|
if self.mw.col.sched_ver() == 1 or not self.mw.col.v3_scheduler():
|
2021-11-24 04:33:01 +01:00
|
|
|
self.mw.moveToState("deckBrowser")
|
2023-09-16 08:09:26 +02:00
|
|
|
show_warning(tr.scheduling_update_required().replace("V2", "v3"))
|
2021-11-24 04:33:01 +01:00
|
|
|
return
|
2020-02-27 04:11:21 +01:00
|
|
|
self.mw.setStateShortcuts(self._shortcutKeys()) # type: ignore
|
2020-02-08 23:59:29 +01:00
|
|
|
self.web.set_bridge_command(self._linkHandler, self)
|
|
|
|
self.bottom.web.set_bridge_command(self._linkHandler, ReviewerBottomBar(self))
|
2021-05-17 08:59:02 +02:00
|
|
|
self._state_mutation_js = self.mw.col.get_config("cardStateCustomizer")
|
2023-08-07 07:10:47 +02:00
|
|
|
self._reps = None
|
2021-03-16 09:30:54 +01:00
|
|
|
self._refresh_needed = RefreshNeeded.QUEUES
|
2021-03-14 13:08:37 +01:00
|
|
|
self.refresh_if_needed()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-05-11 03:37:08 +02:00
|
|
|
# this is only used by add-ons
|
2021-10-03 10:59:42 +02:00
|
|
|
def lastCard(self) -> Card | None:
|
2012-12-21 08:51:59 +01:00
|
|
|
if self._answeredIds:
|
|
|
|
if not self.card or self._answeredIds[-1] != self.card.id:
|
|
|
|
try:
|
2021-11-26 03:29:48 +01:00
|
|
|
return self.mw.col.get_card(self._answeredIds[-1])
|
2012-12-21 08:51:59 +01:00
|
|
|
except TypeError:
|
|
|
|
# id was deleted
|
2020-02-27 04:11:21 +01:00
|
|
|
return None
|
|
|
|
return None
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
def cleanup(self) -> None:
|
2020-01-15 07:53:24 +01:00
|
|
|
gui_hooks.reviewer_will_end()
|
2020-10-14 11:54:29 +02:00
|
|
|
self.card = None
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-03-14 13:08:37 +01:00
|
|
|
def refresh_if_needed(self) -> None:
|
2021-03-16 09:30:54 +01:00
|
|
|
if self._refresh_needed is RefreshNeeded.QUEUES:
|
2021-03-13 14:59:32 +01:00
|
|
|
self.nextCard()
|
2021-03-14 15:03:41 +01:00
|
|
|
self.mw.fade_in_webview()
|
2021-04-06 02:14:11 +02:00
|
|
|
self._refresh_needed = None
|
2021-03-16 09:30:54 +01:00
|
|
|
elif self._refresh_needed is RefreshNeeded.NOTE_TEXT:
|
|
|
|
self._redraw_current_card()
|
|
|
|
self.mw.fade_in_webview()
|
2021-04-06 02:14:11 +02:00
|
|
|
self._refresh_needed = None
|
2022-02-25 04:39:23 +01:00
|
|
|
elif self._refresh_needed is RefreshNeeded.FLAG:
|
|
|
|
self.card.load()
|
|
|
|
self._update_flag_icon()
|
|
|
|
# for when modified in browser
|
|
|
|
self.mw.fade_in_webview()
|
|
|
|
self._refresh_needed = None
|
|
|
|
elif self._refresh_needed:
|
|
|
|
assert_exhaustive(self._refresh_needed)
|
2021-04-06 02:14:11 +02:00
|
|
|
|
|
|
|
def op_executed(
|
2021-10-03 10:59:42 +02:00
|
|
|
self, changes: OpChanges, handler: object | None, focused: bool
|
2021-04-06 02:14:11 +02:00
|
|
|
) -> bool:
|
|
|
|
if handler is not self:
|
2021-06-08 04:09:35 +02:00
|
|
|
if changes.study_queues:
|
2021-04-06 02:14:11 +02:00
|
|
|
self._refresh_needed = RefreshNeeded.QUEUES
|
2021-06-08 04:09:35 +02:00
|
|
|
elif changes.note_text:
|
2021-04-06 02:14:11 +02:00
|
|
|
self._refresh_needed = RefreshNeeded.NOTE_TEXT
|
2022-02-25 04:39:23 +01:00
|
|
|
elif changes.card:
|
|
|
|
self._refresh_needed = RefreshNeeded.FLAG
|
2021-04-06 02:14:11 +02:00
|
|
|
|
|
|
|
if focused and self._refresh_needed:
|
2021-03-14 13:08:37 +01:00
|
|
|
self.refresh_if_needed()
|
2021-03-13 14:59:32 +01:00
|
|
|
|
2021-04-06 02:14:11 +02:00
|
|
|
return bool(self._refresh_needed)
|
2021-03-16 09:30:54 +01:00
|
|
|
|
|
|
|
def _redraw_current_card(self) -> None:
|
|
|
|
self.card.load()
|
|
|
|
if self.state == "answer":
|
|
|
|
self._showAnswer()
|
|
|
|
else:
|
|
|
|
self._showQuestion()
|
2021-03-14 15:03:41 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Fetching a card
|
|
|
|
##########################################################################
|
|
|
|
|
2020-02-27 04:11:21 +01:00
|
|
|
def nextCard(self) -> None:
|
2021-09-08 04:52:33 +02:00
|
|
|
self.previous_card = self.card
|
2021-05-11 03:37:08 +02:00
|
|
|
self.card = None
|
|
|
|
self._v3 = None
|
2023-10-14 02:50:59 +02:00
|
|
|
self._get_next_v3_card()
|
2021-05-11 03:37:08 +02:00
|
|
|
|
2021-10-16 23:38:11 +02:00
|
|
|
self._previous_card_info.set_card(self.previous_card)
|
|
|
|
self._card_info.set_card(self.card)
|
|
|
|
|
2021-05-11 03:37:08 +02:00
|
|
|
if not self.card:
|
|
|
|
self.mw.moveToState("overview")
|
|
|
|
return
|
|
|
|
|
2022-08-30 13:52:22 +02:00
|
|
|
if self._reps is None:
|
2021-05-11 03:37:08 +02:00
|
|
|
self._initWeb()
|
|
|
|
|
|
|
|
self._showQuestion()
|
|
|
|
|
|
|
|
def _get_next_v3_card(self) -> None:
|
|
|
|
assert isinstance(self.mw.col.sched, V3Scheduler)
|
|
|
|
output = self.mw.col.sched.get_queued_cards()
|
2021-05-26 04:59:45 +02:00
|
|
|
if not output.cards:
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
2021-05-11 03:37:08 +02:00
|
|
|
self._v3 = V3CardInfo.from_queue(output)
|
|
|
|
self.card = Card(self.mw.col, backend_card=self._v3.top_card().card)
|
2021-06-27 04:12:23 +02:00
|
|
|
self.card.start_timer()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2023-10-14 02:50:59 +02:00
|
|
|
def get_scheduling_states(self) -> SchedulingStates:
|
|
|
|
return self._v3.states
|
2023-03-16 07:31:00 +01:00
|
|
|
|
2023-10-14 02:50:59 +02:00
|
|
|
def get_scheduling_context(self) -> SchedulingContext:
|
|
|
|
return self._v3.context
|
2021-05-17 08:59:02 +02:00
|
|
|
|
Migrate to protobuf-es (#2547)
* Fix .no-reduce-motion missing from graphs spinner, and not being honored
* Begin migration from protobuf.js -> protobuf-es
Motivation:
- Protobuf-es has a nicer API: messages are represented as classes, and
fields which should exist are not marked as nullable.
- As it uses modules, only the proto messages we actually use get included
in our bundle output. Protobuf.js put everything in a namespace, which
prevented tree-shaking, and made it awkward to access inner messages.
- ./run after touching a proto file drops from about 8s to 6s on my machine. The tradeoff
is slower decoding/encoding (#2043), but that was mainly a concern for the
graphs page, and was unblocked by
https://github.com/ankitects/anki/commit/37151213cd9d431f449ba4b3bc4c0329a1d9af78
Approach/notes:
- We generate the new protobuf-es interface in addition to existing
protobuf.js interface, so we can migrate a module at a time, starting
with the graphs module.
- rslib:proto now generates RPC methods for TS in addition to the Python
interface. The input-arg-unrolling behaviour of the Python generation is
not required here, as we declare the input arg as a PlainMessage<T>, which
marks it as requiring all fields to be provided.
- i64 is represented as bigint in protobuf-es. We were using a patch to
protobuf.js to get it to output Javascript numbers instead of long.js
types, but now that our supported browser versions support bigint, it's
probably worth biting the bullet and migrating to bigint use. Our IDs
fit comfortably within MAX_SAFE_INTEGER, but that may not hold for future
fields we add.
- Oneofs are handled differently in protobuf-es, and are going to need
some refactoring.
Other notable changes:
- Added a --mkdir arg to our build runner, so we can create a dir easily
during the build on Windows.
- Simplified the preference handling code, by wrapping the preferences
in an outer store, instead of a separate store for each individual
preference. This means a change to one preference will trigger a redraw
of all components that depend on the preference store, but the redrawing
is cheap after moving the data processing to Rust, and it makes the code
easier to follow.
- Drop async(Reactive).ts in favour of more explicit handling with await
blocks/updating.
- Renamed add_inputs_to_group() -> add_dependency(), and fixed it not adding
dependencies to parent groups. Renamed add() -> add_action() for clarity.
* Remove a couple of unused proto imports
* Migrate card info
* Migrate congrats, image occlusion, and tag editor
+ Fix imports for multi-word proto files.
* Migrate change-notetype
* Migrate deck options
* Bump target to es2020; simplify ts lib list
Have used caniuse.com to confirm Chromium 77, iOS 14.5 and the Chrome
on Android support the full es2017-es2020 features.
* Migrate import-csv
* Migrate i18n and fix missing output types in .js
* Migrate custom scheduling, and remove protobuf.js
To mostly maintain our old API contract, we make use of protobuf-es's
ability to convert to JSON, which follows the same format as protobuf.js
did. It doesn't cover all case: users who were previously changing the
variant of a type will need to update their code, as assigning to a new
variant no longer automatically removes the old one, which will cause an
error when we try to convert back from JSON. But I suspect the large majority
of users are adjusting the current variant rather than creating a new one,
and this saves us having to write proxy wrappers, so it seems like a
reasonable compromise.
One other change I made at the same time was to rename value->kind for
the oneofs in our custom study protos, as 'value' was easily confused
with the 'case/value' output that protobuf-es has.
With protobuf.js codegen removed, touching a proto file and invoking
./run drops from about 8s to 6s.
This closes #2043.
* Allow tree-shaking on protobuf types
* Display backend error messages in our ts alert()
* Make sourcemap generation opt-in for ts-run
Considerably slows down build, and not used most of the time.
2023-06-14 14:47:37 +02:00
|
|
|
def set_scheduling_states(self, request: SetSchedulingStatesRequest) -> None:
|
|
|
|
if request.key != self._state_mutation_key:
|
2021-05-17 08:59:02 +02:00
|
|
|
return
|
|
|
|
|
2023-10-14 02:50:59 +02:00
|
|
|
self._v3.states = request.states
|
2021-05-17 08:59:02 +02:00
|
|
|
|
|
|
|
def _run_state_mutation_hook(self) -> None:
|
2023-03-16 07:31:00 +01:00
|
|
|
def on_eval(result: Any) -> None:
|
|
|
|
if result is None:
|
|
|
|
# eval failed, usually a syntax error
|
|
|
|
self._states_mutated = True
|
|
|
|
|
2023-10-14 02:50:59 +02:00
|
|
|
if js := self._state_mutation_js:
|
2023-03-16 07:31:00 +01:00
|
|
|
self._states_mutated = False
|
|
|
|
self.web.evalWithCallback(
|
|
|
|
RUN_STATE_MUTATION.format(key=self._state_mutation_key, js=js),
|
|
|
|
on_eval,
|
2021-05-17 08:59:02 +02:00
|
|
|
)
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Audio
|
|
|
|
##########################################################################
|
|
|
|
|
2020-04-13 00:59:36 +02:00
|
|
|
def replayAudio(self) -> None:
|
|
|
|
if self.state == "question":
|
|
|
|
replay_audio(self.card, True)
|
|
|
|
elif self.state == "answer":
|
|
|
|
replay_audio(self.card, False)
|
2023-02-13 05:50:26 +01:00
|
|
|
gui_hooks.audio_will_replay(self.web, self.card, self.state == "question")
|
2020-01-21 05:47:03 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Initializing the webview
|
|
|
|
##########################################################################
|
|
|
|
|
2020-02-27 04:11:21 +01:00
|
|
|
def revHtml(self) -> str:
|
2017-08-10 13:39:04 +02:00
|
|
|
extra = self.mw.col.conf.get("reviewExtra", "")
|
2019-12-23 01:34:10 +01:00
|
|
|
fade = ""
|
2020-12-22 04:01:06 +01:00
|
|
|
if self.mw.pm.video_driver() == VideoDriver.Software:
|
2019-12-23 01:34:10 +01:00
|
|
|
fade = "<script>qFade=0;</script>"
|
2021-02-11 01:09:06 +01:00
|
|
|
return f"""
|
2021-04-13 01:00:09 +02:00
|
|
|
<div id="_mark" hidden>★</div>
|
|
|
|
<div id="_flag" hidden>⚑</div>
|
2021-02-11 01:09:06 +01:00
|
|
|
{fade}
|
2021-04-13 01:00:09 +02:00
|
|
|
<div id="qa"></div>
|
2021-02-11 01:09:06 +01:00
|
|
|
{extra}
|
|
|
|
"""
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-02-27 04:11:21 +01:00
|
|
|
def _initWeb(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self._reps = 0
|
|
|
|
# main window
|
2019-12-23 01:34:10 +01:00
|
|
|
self.web.stdHtml(
|
|
|
|
self.revHtml(),
|
2020-11-01 05:26:58 +01:00
|
|
|
css=["css/reviewer.css"],
|
2019-12-23 01:34:10 +01:00
|
|
|
js=[
|
2020-11-15 11:22:28 +01:00
|
|
|
"js/mathjax.js",
|
2020-11-03 15:49:07 +01:00
|
|
|
"js/vendor/mathjax/tex-chtml.js",
|
2020-11-01 05:26:58 +01:00
|
|
|
"js/reviewer.js",
|
2019-12-23 01:34:10 +01:00
|
|
|
],
|
2020-02-12 22:00:13 +01:00
|
|
|
context=self,
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2022-06-10 15:33:53 +02:00
|
|
|
# block default drag & drop behavior while allowing drop events to be received by JS handlers
|
|
|
|
self.web.allow_drops = True
|
|
|
|
self.web.eval("_blockDefaultDragDropBehavior();")
|
2012-12-21 08:51:59 +01:00
|
|
|
# show answer / ease buttons
|
|
|
|
self.bottom.web.stdHtml(
|
|
|
|
self._bottomHTML(),
|
2020-11-01 05:26:58 +01:00
|
|
|
css=["css/toolbar-bottom.css", "css/reviewer-bottom.css"],
|
2020-12-28 14:18:07 +01:00
|
|
|
js=["js/vendor/jquery.min.js", "js/reviewer-bottom.js"],
|
2020-02-12 22:00:13 +01:00
|
|
|
context=ReviewerBottomBar(self),
|
2017-07-28 08:19:06 +02:00
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Showing the question
|
|
|
|
##########################################################################
|
|
|
|
|
2020-02-27 04:11:21 +01:00
|
|
|
def _mungeQA(self, buf: str) -> str:
|
2020-01-24 02:06:11 +01:00
|
|
|
return self.typeAnsFilter(self.mw.prepare_card_text_for_display(buf))
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
def _showQuestion(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
self._reps += 1
|
|
|
|
self.state = "question"
|
2020-02-27 04:11:21 +01:00
|
|
|
self.typedAnswer: str = None
|
2012-12-21 08:51:59 +01:00
|
|
|
c = self.card
|
|
|
|
# grab the question and play audio
|
2021-06-27 04:12:23 +02:00
|
|
|
q = c.question()
|
2020-01-24 02:06:11 +01:00
|
|
|
# play audio?
|
2020-04-13 01:04:30 +02:00
|
|
|
if c.autoplay():
|
2021-12-07 23:08:56 +01:00
|
|
|
self.web.setPlaybackRequiresGesture(False)
|
2020-07-30 20:06:16 +02:00
|
|
|
sounds = c.question_av_tags()
|
|
|
|
gui_hooks.reviewer_will_play_question_sounds(c, sounds)
|
2020-02-25 08:49:06 +01:00
|
|
|
else:
|
2021-12-07 23:08:56 +01:00
|
|
|
self.web.setPlaybackRequiresGesture(True)
|
2020-07-30 20:06:16 +02:00
|
|
|
sounds = []
|
|
|
|
gui_hooks.reviewer_will_play_question_sounds(c, sounds)
|
2022-05-09 03:08:34 +02:00
|
|
|
gui_hooks.av_player_will_play_tags(sounds, self.state, self)
|
|
|
|
av_player.play_tags(sounds)
|
2012-12-21 08:51:59 +01:00
|
|
|
# render & update bottom
|
|
|
|
q = self._mungeQA(q)
|
2020-01-15 08:45:35 +01:00
|
|
|
q = gui_hooks.card_will_show(q, c, "reviewQuestion")
|
2021-05-17 08:59:02 +02:00
|
|
|
self._run_state_mutation_hook()
|
2017-07-29 06:24:45 +02:00
|
|
|
|
2020-01-23 06:08:10 +01:00
|
|
|
bodyclass = theme_manager.body_classes_for_card_ord(c.ord)
|
2021-07-16 16:57:49 +02:00
|
|
|
a = self.mw.col.media.escape_media_filenames(c.answer())
|
2017-07-29 06:24:45 +02:00
|
|
|
|
2021-07-01 22:45:19 +02:00
|
|
|
self.web.eval(
|
2021-07-16 16:57:49 +02:00
|
|
|
f"_showQuestion({json.dumps(q)}, {json.dumps(a)}, '{bodyclass}');"
|
2021-07-01 22:45:19 +02:00
|
|
|
)
|
2021-03-05 13:45:55 +01:00
|
|
|
self._update_flag_icon()
|
|
|
|
self._update_mark_icon()
|
2017-08-02 08:22:54 +02:00
|
|
|
self._showAnswerButton()
|
2020-10-03 22:33:01 +02:00
|
|
|
self.mw.web.setFocus()
|
2012-12-21 08:51:59 +01:00
|
|
|
# user hook
|
2020-01-15 08:45:35 +01:00
|
|
|
gui_hooks.reviewer_did_show_question(c)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-02-27 04:11:21 +01:00
|
|
|
def autoplay(self, card: Card) -> bool:
|
2020-04-13 01:04:30 +02:00
|
|
|
print("use card.autoplay() instead of reviewer.autoplay(card)")
|
|
|
|
return card.autoplay()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-03-05 13:45:55 +01:00
|
|
|
def _update_flag_icon(self) -> None:
|
2021-03-06 15:17:17 +01:00
|
|
|
self.web.eval(f"_drawFlag({self.card.user_flag()});")
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-03-05 13:45:55 +01:00
|
|
|
def _update_mark_icon(self) -> None:
|
2021-03-18 03:06:45 +01:00
|
|
|
self.web.eval(f"_drawMark({json.dumps(self.card.note().has_tag(MARKED_TAG))});")
|
2021-03-05 13:45:55 +01:00
|
|
|
|
|
|
|
_drawMark = _update_mark_icon
|
|
|
|
_drawFlag = _update_flag_icon
|
2017-08-16 12:30:29 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Showing the answer
|
|
|
|
##########################################################################
|
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
def _showAnswer(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
if self.mw.state != "review":
|
|
|
|
# showing resetRequired screen; ignore space
|
|
|
|
return
|
|
|
|
self.state = "answer"
|
|
|
|
c = self.card
|
2021-06-27 04:12:23 +02:00
|
|
|
a = c.answer()
|
2012-12-21 08:51:59 +01:00
|
|
|
# play audio?
|
2020-04-13 01:04:30 +02:00
|
|
|
if c.autoplay():
|
2020-07-30 20:06:16 +02:00
|
|
|
sounds = c.answer_av_tags()
|
|
|
|
gui_hooks.reviewer_will_play_answer_sounds(c, sounds)
|
2020-02-25 08:49:06 +01:00
|
|
|
else:
|
2020-07-30 20:06:16 +02:00
|
|
|
sounds = []
|
|
|
|
gui_hooks.reviewer_will_play_answer_sounds(c, sounds)
|
2022-05-09 03:08:34 +02:00
|
|
|
gui_hooks.av_player_will_play_tags(sounds, self.state, self)
|
|
|
|
av_player.play_tags(sounds)
|
2017-08-07 08:15:15 +02:00
|
|
|
a = self._mungeQA(a)
|
2020-01-15 08:45:35 +01:00
|
|
|
a = gui_hooks.card_will_show(a, c, "reviewAnswer")
|
2012-12-21 08:51:59 +01:00
|
|
|
# render and update bottom
|
2021-02-11 01:09:06 +01:00
|
|
|
self.web.eval(f"_showAnswer({json.dumps(a)});")
|
2012-12-21 08:51:59 +01:00
|
|
|
self._showEaseButtons()
|
2020-10-03 22:33:01 +02:00
|
|
|
self.mw.web.setFocus()
|
2012-12-21 08:51:59 +01:00
|
|
|
# user hook
|
2020-01-15 08:45:35 +01:00
|
|
|
gui_hooks.reviewer_did_show_answer(c)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Answering a card
|
|
|
|
############################################################
|
|
|
|
|
2021-05-19 08:05:12 +02:00
|
|
|
def _answerCard(self, ease: Literal[1, 2, 3, 4]) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
"Reschedule card and show next."
|
|
|
|
if self.mw.state != "review":
|
|
|
|
# showing resetRequired screen; ignore key
|
|
|
|
return
|
|
|
|
if self.state != "answer":
|
|
|
|
return
|
2020-01-24 15:36:05 +01:00
|
|
|
proceed, ease = gui_hooks.reviewer_will_answer_card(
|
|
|
|
(True, ease), self, self.card
|
|
|
|
)
|
|
|
|
if not proceed:
|
|
|
|
return
|
2021-05-11 03:37:08 +02:00
|
|
|
|
2023-10-14 02:50:59 +02:00
|
|
|
sched = cast(V3Scheduler, self.mw.col.sched)
|
|
|
|
answer = sched.build_answer(
|
|
|
|
card=self.card,
|
|
|
|
states=self._v3.states,
|
|
|
|
rating=self._v3.rating_from_ease(ease),
|
|
|
|
)
|
2021-05-13 07:51:02 +02:00
|
|
|
|
2023-10-14 02:50:59 +02:00
|
|
|
def after_answer(changes: OpChanges) -> None:
|
|
|
|
if gui_hooks.reviewer_did_answer_card.count() > 0:
|
|
|
|
self.card.load()
|
2021-05-11 03:37:08 +02:00
|
|
|
self._after_answering(ease)
|
2023-10-14 02:50:59 +02:00
|
|
|
if sched.state_is_leech(answer.new_state):
|
|
|
|
self.onLeech()
|
|
|
|
|
|
|
|
self.state = "transition"
|
|
|
|
answer_card(parent=self.mw, answer=answer).success(
|
|
|
|
after_answer
|
|
|
|
).run_in_background(initiator=self)
|
2021-05-11 03:37:08 +02:00
|
|
|
|
2021-05-19 08:05:12 +02:00
|
|
|
def _after_answering(self, ease: Literal[1, 2, 3, 4]) -> None:
|
2020-01-24 15:36:05 +01:00
|
|
|
gui_hooks.reviewer_did_answer_card(self, self.card, ease)
|
2012-12-21 08:51:59 +01:00
|
|
|
self._answeredIds.append(self.card.id)
|
2021-08-18 08:25:23 +02:00
|
|
|
if not self.check_timebox():
|
|
|
|
self.nextCard()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Handlers
|
|
|
|
############################################################
|
|
|
|
|
2023-06-07 06:54:52 +02:00
|
|
|
def korean_shortcuts(
|
|
|
|
self,
|
|
|
|
) -> Sequence[Union[tuple[str, Callable], tuple[Qt.Key, Callable]]]:
|
|
|
|
return [
|
|
|
|
("ㄷ", self.mw.onEditCurrent),
|
|
|
|
("ㅡ", self.showContextMenu),
|
|
|
|
("ㄱ", self.replayAudio),
|
|
|
|
("Ctrl+Alt+ㅜ", self.forget_current_card),
|
|
|
|
# does not work
|
|
|
|
# ("Ctrl+Alt+ㄷ", self.on_create_copy),
|
|
|
|
# does not work
|
|
|
|
# ("Ctrl+Shift+ㅇ", self.on_set_due),
|
|
|
|
("ㅍ", self.onReplayRecorded),
|
|
|
|
("Shift+ㅍ", self.onRecordVoice),
|
|
|
|
("ㅐ", self.onOptions),
|
|
|
|
("ㅑ", self.on_card_info),
|
|
|
|
("Ctrl+Alt+ㅑ", self.on_previous_card_info),
|
|
|
|
("ㅕ", self.mw.undo),
|
|
|
|
]
|
|
|
|
|
2020-02-27 04:11:21 +01:00
|
|
|
def _shortcutKeys(
|
|
|
|
self,
|
2021-10-03 10:59:42 +02:00
|
|
|
) -> Sequence[Union[tuple[str, Callable], tuple[Qt.Key, Callable]]]:
|
2017-06-22 08:36:54 +02:00
|
|
|
return [
|
|
|
|
("e", self.mw.onEditCurrent),
|
|
|
|
(" ", self.onEnterKey),
|
2021-10-05 05:53:01 +02:00
|
|
|
(Qt.Key.Key_Return, self.onEnterKey),
|
|
|
|
(Qt.Key.Key_Enter, self.onEnterKey),
|
2020-08-23 16:46:19 +02:00
|
|
|
("m", self.showContextMenu),
|
2017-06-22 08:36:54 +02:00
|
|
|
("r", self.replayAudio),
|
2021-10-05 05:53:01 +02:00
|
|
|
(Qt.Key.Key_F5, self.replayAudio),
|
2021-05-19 19:26:39 +02:00
|
|
|
*(
|
|
|
|
(f"Ctrl+{flag.index}", self.set_flag_func(flag.index))
|
2021-07-02 11:16:10 +02:00
|
|
|
for flag in self.mw.flags.all()
|
2021-05-19 19:26:39 +02:00
|
|
|
),
|
2021-03-05 13:45:55 +01:00
|
|
|
("*", self.toggle_mark_on_current_note),
|
2021-03-04 12:40:59 +01:00
|
|
|
("=", self.bury_current_note),
|
|
|
|
("-", self.bury_current_card),
|
|
|
|
("!", self.suspend_current_note),
|
|
|
|
("@", self.suspend_current_card),
|
2022-02-07 10:56:31 +01:00
|
|
|
("Ctrl+Alt+N", self.forget_current_card),
|
2021-12-31 07:45:30 +01:00
|
|
|
("Ctrl+Alt+E", self.on_create_copy),
|
2023-08-24 23:15:11 +02:00
|
|
|
("Ctrl+Backspace" if is_mac else "Ctrl+Delete", self.delete_current_note),
|
Rework reschedule tool
The old rescheduling dialog's two options have been split into two
separate menu items, "Forget", and "Set Due Date"
For cards that are not review cards, "Set Due Date" behaves like the
old reschedule option, changing the cards into a review card, and
and setting both the interval and due date to the provided number of
days.
When "Set Due Date" is applied to a review card, it no longer resets
the card's interval. Instead, it looks at how much the provided number
of days will change the original interval, and adjusts the interval by
that amount, so that cards that are answered earlier receive a smaller
next interval, and cards that are answered after a longer delay receive
a bonus.
For example, imagine a card was answered on day 5, and given an interval
of 10 days, so it has a due date of day 15.
- if on day 10 the due date is changed to day 12 (today+2), the card
is being scheduled 3 days earlier than it was supposed to be, so the
interval will be adjusted to 7 days.
- and if on day 10 the due date is changed to day 20, the interval will
be changed from 10 days to 15 days.
There is no separate option to reset the interval of a review card, but
it can be accomplished by forgetting the card(s), and then setting the
desired due date.
Other notes:
- Added the action to the review screen as well.
- Set the shortcut to Ctrl+Shift+D, and changed the existing Delete
Tags shortcut to Ctrl+Alt+Shift+A.
2021-02-07 11:58:16 +01:00
|
|
|
("Ctrl+Shift+D", self.on_set_due),
|
2017-06-22 08:36:54 +02:00
|
|
|
("v", self.onReplayRecorded),
|
|
|
|
("Shift+v", self.onRecordVoice),
|
|
|
|
("o", self.onOptions),
|
2021-06-08 06:23:23 +02:00
|
|
|
("i", self.on_card_info),
|
2021-11-24 06:44:25 +01:00
|
|
|
("Ctrl+Alt+i", self.on_previous_card_info),
|
2023-05-18 09:47:51 +02:00
|
|
|
*(
|
|
|
|
(key, functools.partial(self._answerCard, ease))
|
|
|
|
for ease in aqt.mw.pm.default_answer_keys
|
|
|
|
if (key := aqt.mw.pm.get_answer_key(ease))
|
|
|
|
),
|
2023-05-08 03:04:18 +02:00
|
|
|
("u", self.mw.undo),
|
2020-01-22 03:50:33 +01:00
|
|
|
("5", self.on_pause_audio),
|
|
|
|
("6", self.on_seek_backward),
|
|
|
|
("7", self.on_seek_forward),
|
2023-06-07 06:54:52 +02:00
|
|
|
*self.korean_shortcuts(),
|
2017-06-22 08:36:54 +02:00
|
|
|
]
|
|
|
|
|
2020-02-27 04:11:21 +01:00
|
|
|
def on_pause_audio(self) -> None:
|
2020-01-22 03:50:33 +01:00
|
|
|
av_player.toggle_pause()
|
2023-02-13 05:50:26 +01:00
|
|
|
gui_hooks.audio_did_pause_or_unpause(self.web)
|
2020-01-22 03:50:33 +01:00
|
|
|
|
|
|
|
seek_secs = 5
|
|
|
|
|
2020-02-27 04:11:21 +01:00
|
|
|
def on_seek_backward(self) -> None:
|
2020-01-22 03:50:33 +01:00
|
|
|
av_player.seek_relative(-self.seek_secs)
|
2023-09-05 03:15:15 +02:00
|
|
|
gui_hooks.audio_did_seek_relative(self.web, -self.seek_secs)
|
2020-01-22 03:50:33 +01:00
|
|
|
|
2020-02-27 04:11:21 +01:00
|
|
|
def on_seek_forward(self) -> None:
|
2020-01-22 03:50:33 +01:00
|
|
|
av_player.seek_relative(self.seek_secs)
|
2023-09-05 03:15:15 +02:00
|
|
|
gui_hooks.audio_did_seek_relative(self.web, self.seek_secs)
|
2020-01-22 03:50:33 +01:00
|
|
|
|
2020-02-27 04:11:21 +01:00
|
|
|
def onEnterKey(self) -> None:
|
2017-06-22 08:36:54 +02:00
|
|
|
if self.state == "question":
|
|
|
|
self._getTypedAnswer()
|
2023-05-08 03:04:18 +02:00
|
|
|
elif self.state == "answer" and aqt.mw.pm.spacebar_rates_card():
|
2019-12-23 01:34:10 +01:00
|
|
|
self.bottom.web.evalWithCallback(
|
|
|
|
"selectedAnswerButton()", self._onAnswerButton
|
|
|
|
)
|
2018-09-24 06:16:08 +02:00
|
|
|
|
2020-02-27 04:11:21 +01:00
|
|
|
def _onAnswerButton(self, val: str) -> None:
|
2018-09-24 06:16:08 +02:00
|
|
|
# button selected?
|
2018-09-26 08:31:31 +02:00
|
|
|
if val and val in "1234":
|
2021-05-19 08:05:12 +02:00
|
|
|
val2: Literal[1, 2, 3, 4] = int(val) # type: ignore
|
|
|
|
self._answerCard(val2)
|
2018-09-24 06:16:08 +02:00
|
|
|
else:
|
2017-06-22 08:36:54 +02:00
|
|
|
self._answerCard(self._defaultEase())
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-02-27 04:11:21 +01:00
|
|
|
def _linkHandler(self, url: str) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
if url == "ans":
|
2016-07-05 05:14:45 +02:00
|
|
|
self._getTypedAnswer()
|
2012-12-21 08:51:59 +01:00
|
|
|
elif url.startswith("ease"):
|
2021-05-19 08:05:12 +02:00
|
|
|
val: Literal[1, 2, 3, 4] = int(url[4:]) # type: ignore
|
|
|
|
self._answerCard(val)
|
2012-12-21 08:51:59 +01:00
|
|
|
elif url == "edit":
|
|
|
|
self.mw.onEditCurrent()
|
|
|
|
elif url == "more":
|
|
|
|
self.showContextMenu()
|
2020-01-21 05:47:03 +01:00
|
|
|
elif url.startswith("play:"):
|
2020-01-24 02:06:11 +01:00
|
|
|
play_clicked_audio(url, self.card)
|
2023-01-09 05:39:31 +01:00
|
|
|
elif url.startswith("updateToolbar"):
|
|
|
|
self.mw.toolbarWeb.update_background_image()
|
2023-03-16 07:31:00 +01:00
|
|
|
elif url == "statesMutated":
|
|
|
|
self._states_mutated = True
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2016-07-07 03:08:32 +02:00
|
|
|
print("unrecognized anki link:", url)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Type in the answer
|
|
|
|
##########################################################################
|
|
|
|
|
2019-03-04 08:03:43 +01:00
|
|
|
typeAnsPat = r"\[\[type:(.+?)\]\]"
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-02-27 04:11:21 +01:00
|
|
|
def typeAnsFilter(self, buf: str) -> str:
|
2012-12-21 08:51:59 +01:00
|
|
|
if self.state == "question":
|
|
|
|
return self.typeAnsQuestionFilter(buf)
|
|
|
|
else:
|
|
|
|
return self.typeAnsAnswerFilter(buf)
|
|
|
|
|
2020-02-27 04:11:21 +01:00
|
|
|
def typeAnsQuestionFilter(self, buf: str) -> str:
|
2012-12-21 08:51:59 +01:00
|
|
|
self.typeCorrect = None
|
|
|
|
clozeIdx = None
|
|
|
|
m = re.search(self.typeAnsPat, buf)
|
|
|
|
if not m:
|
|
|
|
return buf
|
|
|
|
fld = m.group(1)
|
|
|
|
# if it's a cloze, extract data
|
|
|
|
if fld.startswith("cloze:"):
|
|
|
|
# get field and cloze position
|
|
|
|
clozeIdx = self.card.ord + 1
|
|
|
|
fld = fld.split(":")[1]
|
|
|
|
# loop through fields for a match
|
2021-06-27 04:12:23 +02:00
|
|
|
for f in self.card.note_type()["flds"]:
|
2019-12-23 01:34:10 +01:00
|
|
|
if f["name"] == fld:
|
|
|
|
self.typeCorrect = self.card.note()[f["name"]]
|
2012-12-21 08:51:59 +01:00
|
|
|
if clozeIdx:
|
|
|
|
# narrow to cloze
|
2019-12-23 01:34:10 +01:00
|
|
|
self.typeCorrect = self._contentForCloze(self.typeCorrect, clozeIdx)
|
|
|
|
self.typeFont = f["font"]
|
|
|
|
self.typeSize = f["size"]
|
2012-12-21 08:51:59 +01:00
|
|
|
break
|
|
|
|
if not self.typeCorrect:
|
|
|
|
if self.typeCorrect is None:
|
|
|
|
if clozeIdx:
|
2021-03-26 04:48:26 +01:00
|
|
|
warn = tr.studying_please_run_toolsempty_cards()
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2021-03-26 05:21:04 +01:00
|
|
|
warn = tr.studying_type_answer_unknown_field(val=fld)
|
2012-12-21 08:51:59 +01:00
|
|
|
return re.sub(self.typeAnsPat, warn, buf)
|
|
|
|
else:
|
|
|
|
# empty field, remove type answer pattern
|
|
|
|
return re.sub(self.typeAnsPat, "", buf)
|
2019-12-23 01:34:10 +01:00
|
|
|
return re.sub(
|
|
|
|
self.typeAnsPat,
|
2021-02-11 01:09:06 +01:00
|
|
|
f"""
|
2012-12-21 08:51:59 +01:00
|
|
|
<center>
|
|
|
|
<input type=text id=typeans onkeypress="_typeAnsPress();"
|
2021-02-11 01:09:06 +01:00
|
|
|
style="font-family: '{self.typeFont}'; font-size: {self.typeSize}px;">
|
2012-12-21 08:51:59 +01:00
|
|
|
</center>
|
2021-02-11 01:09:06 +01:00
|
|
|
""",
|
2019-12-23 01:34:10 +01:00
|
|
|
buf,
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-02-27 04:11:21 +01:00
|
|
|
def typeAnsAnswerFilter(self, buf: str) -> str:
|
2013-06-12 04:22:13 +02:00
|
|
|
if not self.typeCorrect:
|
2013-04-27 16:12:03 +02:00
|
|
|
return re.sub(self.typeAnsPat, "", buf)
|
2013-05-20 10:56:01 +02:00
|
|
|
origSize = len(buf)
|
|
|
|
buf = buf.replace("<hr id=answer>", "")
|
|
|
|
hadHR = len(buf) != origSize
|
2022-07-22 11:20:04 +02:00
|
|
|
expected = self.typeCorrect
|
|
|
|
provided = self.typedAnswer
|
2023-02-02 09:01:23 +01:00
|
|
|
output = self.mw.col.compare_answer(expected, provided)
|
2023-03-31 06:02:40 +02:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# and update the type answer area
|
2021-02-01 14:28:21 +01:00
|
|
|
def repl(match: Match) -> str:
|
2012-12-21 08:51:59 +01:00
|
|
|
# can't pass a string in directly, and can't use re.escape as it
|
|
|
|
# escapes too much
|
2013-05-20 10:56:01 +02:00
|
|
|
s = """
|
2022-07-22 12:29:39 +02:00
|
|
|
<div style="font-family: '{}'; font-size: {}px">{}</div>""".format(
|
2019-12-23 01:34:10 +01:00
|
|
|
self.typeFont,
|
|
|
|
self.typeSize,
|
2022-07-22 11:20:04 +02:00
|
|
|
output,
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2013-05-20 10:56:01 +02:00
|
|
|
if hadHR:
|
|
|
|
# a hack to ensure the q/a separator falls before the answer
|
|
|
|
# comparison when user is using {{FrontSide}}
|
2021-02-11 01:09:06 +01:00
|
|
|
s = f"<hr id=answer>{s}"
|
2013-05-20 10:56:01 +02:00
|
|
|
return s
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
return re.sub(self.typeAnsPat, repl, buf)
|
|
|
|
|
2023-01-18 14:05:28 +01:00
|
|
|
def _contentForCloze(self, txt: str, idx: int) -> str | None:
|
|
|
|
return self.mw.col.extract_cloze_for_typing(txt, idx) or None
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-02-27 04:11:21 +01:00
|
|
|
def _getTypedAnswer(self) -> None:
|
2021-07-12 15:00:36 +02:00
|
|
|
self.web.evalWithCallback("getTypedAnswer();", self._onTypedAnswer)
|
2016-07-05 05:14:45 +02:00
|
|
|
|
2020-02-27 04:11:21 +01:00
|
|
|
def _onTypedAnswer(self, val: None) -> None:
|
2017-10-24 09:08:36 +02:00
|
|
|
self.typedAnswer = val or ""
|
2016-07-05 05:14:45 +02:00
|
|
|
self._showAnswer()
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Bottom bar
|
|
|
|
##########################################################################
|
|
|
|
|
2020-02-27 04:11:21 +01:00
|
|
|
def _bottomHTML(self) -> str:
|
2012-12-21 08:51:59 +01:00
|
|
|
return """
|
2016-06-07 06:27:33 +02:00
|
|
|
<center id=outer>
|
|
|
|
<table id=innertable width=100%% cellspacing=0 cellpadding=0>
|
2012-12-21 08:51:59 +01:00
|
|
|
<tr>
|
2022-12-08 13:29:56 +01:00
|
|
|
<td align=start valign=top class=stat>
|
2016-06-06 07:50:03 +02:00
|
|
|
<button title="%(editkey)s" onclick="pycmd('edit');">%(edit)s</button></td>
|
2012-12-21 08:51:59 +01:00
|
|
|
<td align=center valign=top id=middle>
|
|
|
|
</td>
|
2022-12-08 13:29:56 +01:00
|
|
|
<td align=end valign=top class=stat>
|
2022-11-17 01:03:38 +01:00
|
|
|
<button title="%(morekey)s" onclick="pycmd('more');">
|
2022-11-02 09:18:21 +01:00
|
|
|
%(more)s %(downArrow)s
|
|
|
|
<span id=time class=stattxt></span>
|
|
|
|
</button>
|
2012-12-21 08:51:59 +01:00
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
2016-06-07 06:27:33 +02:00
|
|
|
</center>
|
2012-12-21 08:51:59 +01:00
|
|
|
<script>
|
2017-07-28 08:19:06 +02:00
|
|
|
time = %(time)d;
|
2023-09-23 06:01:03 +02:00
|
|
|
timerStopped = false;
|
2012-12-21 08:51:59 +01:00
|
|
|
</script>
|
2019-12-23 01:34:10 +01:00
|
|
|
""" % dict(
|
2021-03-26 04:48:26 +01:00
|
|
|
edit=tr.studying_edit(),
|
2021-03-26 05:21:04 +01:00
|
|
|
editkey=tr.actions_shortcut_key(val="E"),
|
2021-03-26 04:48:26 +01:00
|
|
|
more=tr.studying_more(),
|
2022-11-17 01:03:38 +01:00
|
|
|
morekey=tr.actions_shortcut_key(val="M"),
|
2019-12-23 01:34:10 +01:00
|
|
|
downArrow=downArrow(),
|
2021-06-27 04:12:23 +02:00
|
|
|
time=self.card.time_taken() // 1000,
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-02-27 04:11:21 +01:00
|
|
|
def _showAnswerButton(self) -> None:
|
2019-12-23 01:34:10 +01:00
|
|
|
middle = """
|
2022-11-02 09:18:21 +01:00
|
|
|
<button title="{}" id="ansbut" onclick='pycmd("ans");'>{}<span class=stattxt>{}</span></button>""".format(
|
2021-03-26 05:21:04 +01:00
|
|
|
tr.actions_shortcut_key(val=tr.studying_space()),
|
2021-03-26 04:48:26 +01:00
|
|
|
tr.studying_show_answer(),
|
2022-11-02 09:18:21 +01:00
|
|
|
self._remaining(),
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
# wrap it in a table so it has the same top margin as the ease buttons
|
2019-12-23 01:34:10 +01:00
|
|
|
middle = (
|
|
|
|
"<table cellpadding=0><tr><td class=stat2 align=center>%s</td></tr></table>"
|
|
|
|
% middle
|
|
|
|
)
|
2021-06-27 04:12:23 +02:00
|
|
|
if self.card.should_show_timer():
|
|
|
|
maxTime = self.card.time_limit() / 1000
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
|
|
|
maxTime = 0
|
2019-12-23 01:34:10 +01:00
|
|
|
self.bottom.web.eval("showQuestion(%s,%d);" % (json.dumps(middle), maxTime))
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-02-27 04:11:21 +01:00
|
|
|
def _showEaseButtons(self) -> None:
|
2023-03-16 07:31:00 +01:00
|
|
|
if not self._states_mutated:
|
|
|
|
self.mw.progress.single_shot(50, self._showEaseButtons)
|
|
|
|
return
|
2012-12-21 08:51:59 +01:00
|
|
|
middle = self._answerButtons()
|
2023-09-27 08:10:14 +02:00
|
|
|
conf = self.mw.col.decks.config_dict_for_deck_id(self.card.current_deck_id())
|
2023-09-23 06:01:03 +02:00
|
|
|
self.bottom.web.eval(
|
2023-09-27 08:10:14 +02:00
|
|
|
f"showAnswer({json.dumps(middle)}, {json.dumps(conf['stopTimerOnAnswer'])});"
|
2023-09-23 06:01:03 +02:00
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-02-27 04:11:21 +01:00
|
|
|
def _remaining(self) -> str:
|
2019-12-23 01:34:10 +01:00
|
|
|
if not self.mw.col.conf["dueCounts"]:
|
2012-12-21 08:51:59 +01:00
|
|
|
return ""
|
2021-05-11 03:37:08 +02:00
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
counts: list[Union[int, str]]
|
2023-09-24 06:19:25 +02:00
|
|
|
idx, counts_ = self._v3.counts()
|
|
|
|
counts = cast(list[Union[int, str]], counts_)
|
2021-02-11 01:09:06 +01:00
|
|
|
counts[idx] = f"<u>{counts[idx]}</u>"
|
2021-05-11 03:37:08 +02:00
|
|
|
|
|
|
|
return f"""
|
|
|
|
<span class=new-count>{counts[0]}</span> +
|
|
|
|
<span class=learn-count>{counts[1]}</span> +
|
|
|
|
<span class=review-count>{counts[2]}</span>
|
|
|
|
"""
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-05-19 08:05:12 +02:00
|
|
|
def _defaultEase(self) -> Literal[2, 3]:
|
2023-09-24 06:19:25 +02:00
|
|
|
return 3
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def _answerButtonList(self) -> tuple[tuple[int, str], ...]:
|
2020-08-18 16:37:45 +02:00
|
|
|
button_count = self.mw.col.sched.answerButtons(self.card)
|
|
|
|
if button_count == 2:
|
2021-10-03 10:59:42 +02:00
|
|
|
buttons_tuple: tuple[tuple[int, str], ...] = (
|
2021-03-26 04:48:26 +01:00
|
|
|
(1, tr.studying_again()),
|
|
|
|
(2, tr.studying_good()),
|
2020-08-21 04:34:44 +02:00
|
|
|
)
|
2020-08-18 16:37:45 +02:00
|
|
|
elif button_count == 3:
|
2020-11-17 08:42:43 +01:00
|
|
|
buttons_tuple = (
|
2021-03-26 04:48:26 +01:00
|
|
|
(1, tr.studying_again()),
|
|
|
|
(2, tr.studying_good()),
|
|
|
|
(3, tr.studying_easy()),
|
2020-11-17 08:42:43 +01:00
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
2020-08-21 04:34:44 +02:00
|
|
|
buttons_tuple = (
|
2021-03-26 04:48:26 +01:00
|
|
|
(1, tr.studying_again()),
|
|
|
|
(2, tr.studying_hard()),
|
|
|
|
(3, tr.studying_good()),
|
|
|
|
(4, tr.studying_easy()),
|
2020-08-21 04:34:44 +02:00
|
|
|
)
|
|
|
|
buttons_tuple = gui_hooks.reviewer_will_init_answer_buttons(
|
|
|
|
buttons_tuple, self, self.card
|
|
|
|
)
|
2020-08-18 16:37:45 +02:00
|
|
|
return buttons_tuple
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-02-27 04:11:21 +01:00
|
|
|
def _answerButtons(self) -> str:
|
2012-12-21 08:51:59 +01:00
|
|
|
default = self._defaultEase()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2023-10-14 02:50:59 +02:00
|
|
|
assert isinstance(self.mw.col.sched, V3Scheduler)
|
|
|
|
labels = self.mw.col.sched.describe_next_states(self._v3.states)
|
2021-05-11 03:37:08 +02:00
|
|
|
|
2021-02-02 15:00:29 +01:00
|
|
|
def but(i: int, label: str) -> str:
|
2012-12-21 08:51:59 +01:00
|
|
|
if i == default:
|
2022-04-27 11:26:16 +02:00
|
|
|
extra = """id="defease" """
|
2012-12-21 08:51:59 +01:00
|
|
|
else:
|
|
|
|
extra = ""
|
2021-05-11 03:37:08 +02:00
|
|
|
due = self._buttonTime(i, v3_labels=labels)
|
2023-09-14 03:33:42 +02:00
|
|
|
key = (
|
|
|
|
tr.actions_shortcut_key(val=aqt.mw.pm.get_answer_key(i))
|
|
|
|
if aqt.mw.pm.get_answer_key(i)
|
|
|
|
else ""
|
|
|
|
)
|
2019-12-23 01:34:10 +01:00
|
|
|
return """
|
2022-11-02 09:18:21 +01:00
|
|
|
<td align=center><button %s title="%s" data-ease="%s" onclick='pycmd("ease%d");'>\
|
|
|
|
%s%s</button></td>""" % (
|
2019-12-23 01:34:10 +01:00
|
|
|
extra,
|
2023-09-14 03:33:42 +02:00
|
|
|
key,
|
2019-12-23 01:34:10 +01:00
|
|
|
i,
|
|
|
|
i,
|
|
|
|
label,
|
2022-11-02 09:18:21 +01:00
|
|
|
due,
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
|
|
|
|
2022-11-24 11:18:57 +01:00
|
|
|
buf = "<center><table cellpadding=0 cellspacing=0><tr>"
|
2012-12-21 08:51:59 +01:00
|
|
|
for ease, label in self._answerButtonList():
|
|
|
|
buf += but(ease, label)
|
|
|
|
buf += "</tr></table>"
|
2021-04-13 20:11:18 +02:00
|
|
|
return buf
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2023-09-24 06:19:25 +02:00
|
|
|
def _buttonTime(self, i: int, v3_labels: Sequence[str]) -> str:
|
2022-11-23 07:35:23 +01:00
|
|
|
if self.mw.col.conf["estTimes"]:
|
2023-09-24 06:19:25 +02:00
|
|
|
txt = v3_labels[i - 1]
|
2022-11-23 07:35:23 +01:00
|
|
|
return f"""<span class="nobold">{txt}</span>"""
|
2021-05-11 03:37:08 +02:00
|
|
|
else:
|
2022-11-23 07:35:23 +01:00
|
|
|
return ""
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
# Leeches
|
|
|
|
##########################################################################
|
|
|
|
|
2021-10-03 10:59:42 +02:00
|
|
|
def onLeech(self, card: Card | None = None) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
# for now
|
2021-03-26 04:48:26 +01:00
|
|
|
s = tr.studying_card_was_a_leech()
|
2021-05-13 07:51:02 +02:00
|
|
|
# v3 scheduler doesn't report this
|
|
|
|
if card and card.queue < 0:
|
2021-03-26 04:48:26 +01:00
|
|
|
s += f" {tr.studying_it_has_been_suspended()}"
|
2012-12-21 08:51:59 +01:00
|
|
|
tooltip(s)
|
|
|
|
|
2021-05-11 03:37:08 +02:00
|
|
|
# Timebox
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
def check_timebox(self) -> bool:
|
|
|
|
"True if answering should be aborted."
|
|
|
|
elapsed = self.mw.col.timeboxReached()
|
|
|
|
if elapsed:
|
|
|
|
assert not isinstance(elapsed, bool)
|
|
|
|
part1 = tr.studying_card_studied_in(count=elapsed[1])
|
|
|
|
mins = int(round(elapsed[0] / 60))
|
|
|
|
part2 = tr.studying_minute(count=mins)
|
|
|
|
fin = tr.studying_finish()
|
|
|
|
diag = askUserDialog(f"{part1} {part2}", [tr.studying_continue(), fin])
|
2021-10-05 05:53:01 +02:00
|
|
|
diag.setIcon(QMessageBox.Icon.Information)
|
2021-05-11 03:37:08 +02:00
|
|
|
if diag.run() == fin:
|
|
|
|
self.mw.moveToState("deckBrowser")
|
|
|
|
return True
|
|
|
|
self.mw.col.startTimebox()
|
|
|
|
return False
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# Context menu
|
|
|
|
##########################################################################
|
|
|
|
|
|
|
|
# note the shortcuts listed here also need to be defined above
|
2021-10-03 10:59:42 +02:00
|
|
|
def _contextMenu(self) -> list[Any]:
|
2021-03-06 15:17:17 +01:00
|
|
|
currentFlag = self.card and self.card.user_flag()
|
2012-12-21 08:51:59 +01:00
|
|
|
opts = [
|
2019-12-23 01:34:10 +01:00
|
|
|
[
|
2021-03-26 04:48:26 +01:00
|
|
|
tr.studying_flag_card(),
|
2019-12-23 01:34:10 +01:00
|
|
|
[
|
|
|
|
[
|
2021-05-19 19:18:49 +02:00
|
|
|
flag.label,
|
|
|
|
f"Ctrl+{flag.index}",
|
|
|
|
self.set_flag_func(flag.index),
|
|
|
|
dict(checked=currentFlag == flag.index),
|
|
|
|
]
|
2021-07-02 11:16:10 +02:00
|
|
|
for flag in self.mw.flags.all()
|
2019-12-23 01:34:10 +01:00
|
|
|
],
|
|
|
|
],
|
2021-03-26 04:48:26 +01:00
|
|
|
[tr.studying_bury_card(), "-", self.bury_current_card],
|
2022-06-20 02:25:50 +02:00
|
|
|
[
|
|
|
|
tr.actions_with_ellipsis(action=tr.actions_forget_card()),
|
|
|
|
"Ctrl+Alt+N",
|
|
|
|
self.forget_current_card,
|
|
|
|
],
|
2021-12-31 07:45:30 +01:00
|
|
|
[
|
|
|
|
tr.actions_with_ellipsis(action=tr.actions_set_due_date()),
|
|
|
|
"Ctrl+Shift+D",
|
|
|
|
self.on_set_due,
|
|
|
|
],
|
2021-03-26 04:48:26 +01:00
|
|
|
[tr.actions_suspend_card(), "@", self.suspend_current_card],
|
2021-06-08 06:23:23 +02:00
|
|
|
[tr.actions_options(), "O", self.onOptions],
|
|
|
|
[tr.actions_card_info(), "I", self.on_card_info],
|
2021-12-06 10:01:37 +01:00
|
|
|
[tr.actions_previous_card_info(), "Ctrl+Alt+I", self.on_previous_card_info],
|
2021-06-08 06:23:23 +02:00
|
|
|
None,
|
|
|
|
[tr.studying_mark_note(), "*", self.toggle_mark_on_current_note],
|
|
|
|
[tr.studying_bury_note(), "=", self.bury_current_note],
|
2021-03-26 04:48:26 +01:00
|
|
|
[tr.studying_suspend_note(), "!", self.suspend_current_note],
|
2021-12-31 07:45:30 +01:00
|
|
|
[
|
|
|
|
tr.actions_with_ellipsis(action=tr.actions_create_copy()),
|
|
|
|
"Ctrl+Alt+E",
|
|
|
|
self.on_create_copy,
|
|
|
|
],
|
2023-08-24 23:15:11 +02:00
|
|
|
[
|
|
|
|
tr.studying_delete_note(),
|
|
|
|
"Ctrl+Backspace" if is_mac else "Ctrl+Delete",
|
|
|
|
self.delete_current_note,
|
|
|
|
],
|
2012-12-21 08:51:59 +01:00
|
|
|
None,
|
2021-03-26 04:48:26 +01:00
|
|
|
[tr.actions_replay_audio(), "R", self.replayAudio],
|
|
|
|
[tr.studying_pause_audio(), "5", self.on_pause_audio],
|
|
|
|
[tr.studying_audio_5s(), "6", self.on_seek_backward],
|
|
|
|
[tr.studying_audio_and5s(), "7", self.on_seek_forward],
|
|
|
|
[tr.studying_record_own_voice(), "Shift+V", self.onRecordVoice],
|
|
|
|
[tr.studying_replay_own_voice(), "V", self.onReplayRecorded],
|
2012-12-21 08:51:59 +01:00
|
|
|
]
|
2018-01-07 18:19:49 +01:00
|
|
|
return opts
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
def showContextMenu(self) -> None:
|
2018-01-07 18:19:49 +01:00
|
|
|
opts = self._contextMenu()
|
2012-12-21 08:51:59 +01:00
|
|
|
m = QMenu(self.mw)
|
2017-08-12 08:08:10 +02:00
|
|
|
self._addMenuItems(m, opts)
|
|
|
|
|
2020-01-15 08:45:35 +01:00
|
|
|
gui_hooks.reviewer_will_show_context_menu(self, m)
|
2019-02-05 05:37:07 +01:00
|
|
|
qtMenuShortcutWorkaround(m)
|
2022-02-18 08:18:29 +01:00
|
|
|
m.popup(QCursor.pos())
|
2017-08-12 08:08:10 +02:00
|
|
|
|
2021-02-02 15:00:29 +01:00
|
|
|
def _addMenuItems(self, m: QMenu, rows: Sequence) -> None:
|
2017-08-12 08:08:10 +02:00
|
|
|
for row in rows:
|
2012-12-21 08:51:59 +01:00
|
|
|
if not row:
|
|
|
|
m.addSeparator()
|
|
|
|
continue
|
2017-08-12 08:08:10 +02:00
|
|
|
if len(row) == 2:
|
|
|
|
subm = m.addMenu(row[0])
|
|
|
|
self._addMenuItems(subm, row[1])
|
2019-02-05 05:37:07 +01:00
|
|
|
qtMenuShortcutWorkaround(subm)
|
2017-08-12 08:08:10 +02:00
|
|
|
continue
|
2018-11-12 03:01:54 +01:00
|
|
|
if len(row) == 4:
|
|
|
|
label, scut, func, opts = row
|
|
|
|
else:
|
|
|
|
label, scut, func = row
|
|
|
|
opts = {}
|
2012-12-21 08:51:59 +01:00
|
|
|
a = m.addAction(label)
|
2017-01-10 09:39:31 +01:00
|
|
|
if scut:
|
|
|
|
a.setShortcut(QKeySequence(scut))
|
2018-11-12 03:01:54 +01:00
|
|
|
if opts.get("checked"):
|
|
|
|
a.setCheckable(True)
|
|
|
|
a.setChecked(True)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(a.triggered, func)
|
2017-08-12 08:08:10 +02:00
|
|
|
|
2020-02-27 04:11:21 +01:00
|
|
|
def onOptions(self) -> None:
|
2021-05-27 05:11:20 +02:00
|
|
|
confirm_deck_then_display_options(self.card)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-09-08 04:52:33 +02:00
|
|
|
def on_previous_card_info(self) -> None:
|
2021-10-16 23:38:11 +02:00
|
|
|
self._previous_card_info.toggle()
|
2021-09-08 04:52:33 +02:00
|
|
|
|
2021-06-08 06:23:23 +02:00
|
|
|
def on_card_info(self) -> None:
|
2021-10-16 23:38:11 +02:00
|
|
|
self._card_info.toggle()
|
2021-06-08 06:23:23 +02:00
|
|
|
|
more reset refactoring
'card modified' covers the common case where we need to rebuild the
study queue, but is also set when changing the card flags. We want to
avoid a queue rebuild in that case, as it causes UI flicker, and may
result in a different card being shown. Note marking doesn't trigger
a queue build, but still causes flicker, and may return the user back
to the front side when they were looking at the answer.
I still think entity-based change tracking is the simplest in the
common case, but to solve the above, I've introduced an enum describing
the last operation that was taken. This currently is not trying to list
out all possible operations, and just describes the ones we want to
special-case.
Other changes:
- Fire the old 'state_did_reset' hook after an operation is performed,
so legacy code can refresh itself after an operation is performed.
- Fire the new `operation_did_execute` hook when mw.reset() is called,
so that as the UI is updated to the use the new hook, it will still
be able to refresh after legacy code calls mw.reset()
- Update the deck browser, overview and review screens to listen to
the new hook, instead of relying on the main window to call moveToState()
- Add a 'set flag' backend action, so we can distinguish it from a
normal card update.
- Drop the separate added/modified entries in the change list in
favour of a single entry per entity.
- Add typing to mw.state
- Tweak perform_op()
- Convert a few more actions to use perform_op()
2021-03-14 10:54:15 +01:00
|
|
|
def set_flag_on_current_card(self, desired_flag: int) -> None:
|
undoable ops now return changes directly; add new *_ops.py files
- Introduced a new transact() method that wraps the return value
in a separate struct that describes the changes that were made.
- Changes are now gathered from the undo log, so we don't need to
guess at what was changed - eg if update_note() is called with identical
note contents, no changes are returned. Card changes will only be set
if cards were actually generated by the update_note() call, and tag
will only be set if a new tag was added.
- mw.perform_op() has been updated to expect the op to return the changes,
or a structure with the changes in it, and it will use them to fire the
change hook, instead of fetching the changes from undo_status(), so there
is no risk of race conditions.
- the various calls to mw.perform_op() have been split into separate
files like card_ops.py. Aside from making the code cleaner, this works
around a rather annoying issue with mypy. Because we run it with
no_strict_optional, mypy is happy to accept an operation that returns None,
despite the type signature saying it requires changes to be returned.
Turning no_strict_optional on for the whole codebase is not practical
at the moment, but we can enable it for individual files.
Still todo:
- The cursor keeps moving back to the start of a field when typing -
we need to ignore the refresh hook when we are the initiator.
- The busy cursor icon should probably be delayed a few hundreds ms.
- Still need to think about a nicer way of handling saveNow()
- op_made_changes(), op_affects_study_queue() might be better embedded
as properties in the object instead
2021-03-16 05:26:42 +01:00
|
|
|
# need to toggle off?
|
|
|
|
if self.card.user_flag() == desired_flag:
|
|
|
|
flag = 0
|
|
|
|
else:
|
|
|
|
flag = desired_flag
|
more reset refactoring
'card modified' covers the common case where we need to rebuild the
study queue, but is also set when changing the card flags. We want to
avoid a queue rebuild in that case, as it causes UI flicker, and may
result in a different card being shown. Note marking doesn't trigger
a queue build, but still causes flicker, and may return the user back
to the front side when they were looking at the answer.
I still think entity-based change tracking is the simplest in the
common case, but to solve the above, I've introduced an enum describing
the last operation that was taken. This currently is not trying to list
out all possible operations, and just describes the ones we want to
special-case.
Other changes:
- Fire the old 'state_did_reset' hook after an operation is performed,
so legacy code can refresh itself after an operation is performed.
- Fire the new `operation_did_execute` hook when mw.reset() is called,
so that as the UI is updated to the use the new hook, it will still
be able to refresh after legacy code calls mw.reset()
- Update the deck browser, overview and review screens to listen to
the new hook, instead of relying on the main window to call moveToState()
- Add a 'set flag' backend action, so we can distinguish it from a
normal card update.
- Drop the separate added/modified entries in the change list in
favour of a single entry per entity.
- Add typing to mw.state
- Tweak perform_op()
- Convert a few more actions to use perform_op()
2021-03-14 10:54:15 +01:00
|
|
|
|
2021-04-06 06:36:13 +02:00
|
|
|
set_card_flag(parent=self.mw, card_ids=[self.card.id], flag=flag).success(
|
2022-02-25 04:39:23 +01:00
|
|
|
lambda _: None
|
|
|
|
).run_in_background()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2021-05-19 19:18:49 +02:00
|
|
|
def set_flag_func(self, desired_flag: int) -> Callable:
|
|
|
|
return lambda: self.set_flag_on_current_card(desired_flag)
|
|
|
|
|
2021-03-05 13:45:55 +01:00
|
|
|
def toggle_mark_on_current_note(self) -> None:
|
2021-04-06 02:14:11 +02:00
|
|
|
def redraw_mark(out: OpChangesWithCount) -> None:
|
|
|
|
self.card.load()
|
|
|
|
self._update_mark_icon()
|
|
|
|
|
undoable ops now return changes directly; add new *_ops.py files
- Introduced a new transact() method that wraps the return value
in a separate struct that describes the changes that were made.
- Changes are now gathered from the undo log, so we don't need to
guess at what was changed - eg if update_note() is called with identical
note contents, no changes are returned. Card changes will only be set
if cards were actually generated by the update_note() call, and tag
will only be set if a new tag was added.
- mw.perform_op() has been updated to expect the op to return the changes,
or a structure with the changes in it, and it will use them to fire the
change hook, instead of fetching the changes from undo_status(), so there
is no risk of race conditions.
- the various calls to mw.perform_op() have been split into separate
files like card_ops.py. Aside from making the code cleaner, this works
around a rather annoying issue with mypy. Because we run it with
no_strict_optional, mypy is happy to accept an operation that returns None,
despite the type signature saying it requires changes to be returned.
Turning no_strict_optional on for the whole codebase is not practical
at the moment, but we can enable it for individual files.
Still todo:
- The cursor keeps moving back to the start of a field when typing -
we need to ignore the refresh hook when we are the initiator.
- The busy cursor icon should probably be delayed a few hundreds ms.
- Still need to think about a nicer way of handling saveNow()
- op_made_changes(), op_affects_study_queue() might be better embedded
as properties in the object instead
2021-03-16 05:26:42 +01:00
|
|
|
note = self.card.note()
|
2021-03-18 03:06:45 +01:00
|
|
|
if note.has_tag(MARKED_TAG):
|
2021-04-01 07:01:08 +02:00
|
|
|
remove_tags_from_notes(
|
2021-04-06 04:47:55 +02:00
|
|
|
parent=self.mw, note_ids=[note.id], space_separated_tags=MARKED_TAG
|
2021-04-06 06:36:13 +02:00
|
|
|
).success(redraw_mark).run_in_background(initiator=self)
|
undoable ops now return changes directly; add new *_ops.py files
- Introduced a new transact() method that wraps the return value
in a separate struct that describes the changes that were made.
- Changes are now gathered from the undo log, so we don't need to
guess at what was changed - eg if update_note() is called with identical
note contents, no changes are returned. Card changes will only be set
if cards were actually generated by the update_note() call, and tag
will only be set if a new tag was added.
- mw.perform_op() has been updated to expect the op to return the changes,
or a structure with the changes in it, and it will use them to fire the
change hook, instead of fetching the changes from undo_status(), so there
is no risk of race conditions.
- the various calls to mw.perform_op() have been split into separate
files like card_ops.py. Aside from making the code cleaner, this works
around a rather annoying issue with mypy. Because we run it with
no_strict_optional, mypy is happy to accept an operation that returns None,
despite the type signature saying it requires changes to be returned.
Turning no_strict_optional on for the whole codebase is not practical
at the moment, but we can enable it for individual files.
Still todo:
- The cursor keeps moving back to the start of a field when typing -
we need to ignore the refresh hook when we are the initiator.
- The busy cursor icon should probably be delayed a few hundreds ms.
- Still need to think about a nicer way of handling saveNow()
- op_made_changes(), op_affects_study_queue() might be better embedded
as properties in the object instead
2021-03-16 05:26:42 +01:00
|
|
|
else:
|
2021-04-01 07:01:08 +02:00
|
|
|
add_tags_to_notes(
|
2021-04-06 04:47:55 +02:00
|
|
|
parent=self.mw,
|
2021-04-06 02:14:11 +02:00
|
|
|
note_ids=[note.id],
|
|
|
|
space_separated_tags=MARKED_TAG,
|
2021-04-06 06:36:13 +02:00
|
|
|
).success(redraw_mark).run_in_background(initiator=self)
|
2017-08-16 12:30:29 +02:00
|
|
|
|
Rework reschedule tool
The old rescheduling dialog's two options have been split into two
separate menu items, "Forget", and "Set Due Date"
For cards that are not review cards, "Set Due Date" behaves like the
old reschedule option, changing the cards into a review card, and
and setting both the interval and due date to the provided number of
days.
When "Set Due Date" is applied to a review card, it no longer resets
the card's interval. Instead, it looks at how much the provided number
of days will change the original interval, and adjusts the interval by
that amount, so that cards that are answered earlier receive a smaller
next interval, and cards that are answered after a longer delay receive
a bonus.
For example, imagine a card was answered on day 5, and given an interval
of 10 days, so it has a due date of day 15.
- if on day 10 the due date is changed to day 12 (today+2), the card
is being scheduled 3 days earlier than it was supposed to be, so the
interval will be adjusted to 7 days.
- and if on day 10 the due date is changed to day 20, the interval will
be changed from 10 days to 15 days.
There is no separate option to reset the interval of a review card, but
it can be accomplished by forgetting the card(s), and then setting the
desired due date.
Other notes:
- Added the action to the review screen as well.
- Set the shortcut to Ctrl+Shift+D, and changed the existing Delete
Tags shortcut to Ctrl+Alt+Shift+A.
2021-02-07 11:58:16 +01:00
|
|
|
def on_set_due(self) -> None:
|
|
|
|
if self.mw.state != "review" or not self.card:
|
|
|
|
return
|
|
|
|
|
2021-04-25 11:26:55 +02:00
|
|
|
if op := set_due_date_dialog(
|
Rework reschedule tool
The old rescheduling dialog's two options have been split into two
separate menu items, "Forget", and "Set Due Date"
For cards that are not review cards, "Set Due Date" behaves like the
old reschedule option, changing the cards into a review card, and
and setting both the interval and due date to the provided number of
days.
When "Set Due Date" is applied to a review card, it no longer resets
the card's interval. Instead, it looks at how much the provided number
of days will change the original interval, and adjusts the interval by
that amount, so that cards that are answered earlier receive a smaller
next interval, and cards that are answered after a longer delay receive
a bonus.
For example, imagine a card was answered on day 5, and given an interval
of 10 days, so it has a due date of day 15.
- if on day 10 the due date is changed to day 12 (today+2), the card
is being scheduled 3 days earlier than it was supposed to be, so the
interval will be adjusted to 7 days.
- and if on day 10 the due date is changed to day 20, the interval will
be changed from 10 days to 15 days.
There is no separate option to reset the interval of a review card, but
it can be accomplished by forgetting the card(s), and then setting the
desired due date.
Other notes:
- Added the action to the review screen as well.
- Set the shortcut to Ctrl+Shift+D, and changed the existing Delete
Tags shortcut to Ctrl+Alt+Shift+A.
2021-02-07 11:58:16 +01:00
|
|
|
parent=self.mw,
|
|
|
|
card_ids=[self.card.id],
|
2021-03-12 05:50:31 +01:00
|
|
|
config_key=Config.String.SET_DUE_REVIEWER,
|
2021-04-25 11:26:55 +02:00
|
|
|
):
|
|
|
|
op.run_in_background()
|
Rework reschedule tool
The old rescheduling dialog's two options have been split into two
separate menu items, "Forget", and "Set Due Date"
For cards that are not review cards, "Set Due Date" behaves like the
old reschedule option, changing the cards into a review card, and
and setting both the interval and due date to the provided number of
days.
When "Set Due Date" is applied to a review card, it no longer resets
the card's interval. Instead, it looks at how much the provided number
of days will change the original interval, and adjusts the interval by
that amount, so that cards that are answered earlier receive a smaller
next interval, and cards that are answered after a longer delay receive
a bonus.
For example, imagine a card was answered on day 5, and given an interval
of 10 days, so it has a due date of day 15.
- if on day 10 the due date is changed to day 12 (today+2), the card
is being scheduled 3 days earlier than it was supposed to be, so the
interval will be adjusted to 7 days.
- and if on day 10 the due date is changed to day 20, the interval will
be changed from 10 days to 15 days.
There is no separate option to reset the interval of a review card, but
it can be accomplished by forgetting the card(s), and then setting the
desired due date.
Other notes:
- Added the action to the review screen as well.
- Set the shortcut to Ctrl+Shift+D, and changed the existing Delete
Tags shortcut to Ctrl+Alt+Shift+A.
2021-02-07 11:58:16 +01:00
|
|
|
|
2021-03-04 12:40:59 +01:00
|
|
|
def suspend_current_note(self) -> None:
|
2022-07-18 04:59:56 +02:00
|
|
|
gui_hooks.reviewer_will_suspend_note(self.card.nid)
|
undoable ops now return changes directly; add new *_ops.py files
- Introduced a new transact() method that wraps the return value
in a separate struct that describes the changes that were made.
- Changes are now gathered from the undo log, so we don't need to
guess at what was changed - eg if update_note() is called with identical
note contents, no changes are returned. Card changes will only be set
if cards were actually generated by the update_note() call, and tag
will only be set if a new tag was added.
- mw.perform_op() has been updated to expect the op to return the changes,
or a structure with the changes in it, and it will use them to fire the
change hook, instead of fetching the changes from undo_status(), so there
is no risk of race conditions.
- the various calls to mw.perform_op() have been split into separate
files like card_ops.py. Aside from making the code cleaner, this works
around a rather annoying issue with mypy. Because we run it with
no_strict_optional, mypy is happy to accept an operation that returns None,
despite the type signature saying it requires changes to be returned.
Turning no_strict_optional on for the whole codebase is not practical
at the moment, but we can enable it for individual files.
Still todo:
- The cursor keeps moving back to the start of a field when typing -
we need to ignore the refresh hook when we are the initiator.
- The busy cursor icon should probably be delayed a few hundreds ms.
- Still need to think about a nicer way of handling saveNow()
- op_made_changes(), op_affects_study_queue() might be better embedded
as properties in the object instead
2021-03-16 05:26:42 +01:00
|
|
|
suspend_note(
|
2021-04-06 08:38:42 +02:00
|
|
|
parent=self.mw,
|
|
|
|
note_ids=[self.card.nid],
|
|
|
|
).success(lambda _: tooltip(tr.studying_note_suspended())).run_in_background()
|
2013-01-29 01:46:17 +01:00
|
|
|
|
2021-03-04 12:40:59 +01:00
|
|
|
def suspend_current_card(self) -> None:
|
2022-07-18 04:59:56 +02:00
|
|
|
gui_hooks.reviewer_will_suspend_card(self.card.id)
|
undoable ops now return changes directly; add new *_ops.py files
- Introduced a new transact() method that wraps the return value
in a separate struct that describes the changes that were made.
- Changes are now gathered from the undo log, so we don't need to
guess at what was changed - eg if update_note() is called with identical
note contents, no changes are returned. Card changes will only be set
if cards were actually generated by the update_note() call, and tag
will only be set if a new tag was added.
- mw.perform_op() has been updated to expect the op to return the changes,
or a structure with the changes in it, and it will use them to fire the
change hook, instead of fetching the changes from undo_status(), so there
is no risk of race conditions.
- the various calls to mw.perform_op() have been split into separate
files like card_ops.py. Aside from making the code cleaner, this works
around a rather annoying issue with mypy. Because we run it with
no_strict_optional, mypy is happy to accept an operation that returns None,
despite the type signature saying it requires changes to be returned.
Turning no_strict_optional on for the whole codebase is not practical
at the moment, but we can enable it for individual files.
Still todo:
- The cursor keeps moving back to the start of a field when typing -
we need to ignore the refresh hook when we are the initiator.
- The busy cursor icon should probably be delayed a few hundreds ms.
- Still need to think about a nicer way of handling saveNow()
- op_made_changes(), op_affects_study_queue() might be better embedded
as properties in the object instead
2021-03-16 05:26:42 +01:00
|
|
|
suspend_cards(
|
2021-04-06 08:38:42 +02:00
|
|
|
parent=self.mw,
|
undoable ops now return changes directly; add new *_ops.py files
- Introduced a new transact() method that wraps the return value
in a separate struct that describes the changes that were made.
- Changes are now gathered from the undo log, so we don't need to
guess at what was changed - eg if update_note() is called with identical
note contents, no changes are returned. Card changes will only be set
if cards were actually generated by the update_note() call, and tag
will only be set if a new tag was added.
- mw.perform_op() has been updated to expect the op to return the changes,
or a structure with the changes in it, and it will use them to fire the
change hook, instead of fetching the changes from undo_status(), so there
is no risk of race conditions.
- the various calls to mw.perform_op() have been split into separate
files like card_ops.py. Aside from making the code cleaner, this works
around a rather annoying issue with mypy. Because we run it with
no_strict_optional, mypy is happy to accept an operation that returns None,
despite the type signature saying it requires changes to be returned.
Turning no_strict_optional on for the whole codebase is not practical
at the moment, but we can enable it for individual files.
Still todo:
- The cursor keeps moving back to the start of a field when typing -
we need to ignore the refresh hook when we are the initiator.
- The busy cursor icon should probably be delayed a few hundreds ms.
- Still need to think about a nicer way of handling saveNow()
- op_made_changes(), op_affects_study_queue() might be better embedded
as properties in the object instead
2021-03-16 05:26:42 +01:00
|
|
|
card_ids=[self.card.id],
|
2021-04-06 08:38:42 +02:00
|
|
|
).success(lambda _: tooltip(tr.studying_card_suspended())).run_in_background()
|
2021-03-04 12:40:59 +01:00
|
|
|
|
|
|
|
def bury_current_note(self) -> None:
|
2022-07-18 04:59:56 +02:00
|
|
|
gui_hooks.reviewer_will_bury_note(self.card.nid)
|
2023-03-31 06:02:40 +02:00
|
|
|
bury_notes(
|
|
|
|
parent=self.mw,
|
|
|
|
note_ids=[self.card.nid],
|
|
|
|
).success(
|
2021-10-23 03:04:26 +02:00
|
|
|
lambda res: tooltip(tr.studying_cards_buried(count=res.count))
|
|
|
|
).run_in_background()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
undoable ops now return changes directly; add new *_ops.py files
- Introduced a new transact() method that wraps the return value
in a separate struct that describes the changes that were made.
- Changes are now gathered from the undo log, so we don't need to
guess at what was changed - eg if update_note() is called with identical
note contents, no changes are returned. Card changes will only be set
if cards were actually generated by the update_note() call, and tag
will only be set if a new tag was added.
- mw.perform_op() has been updated to expect the op to return the changes,
or a structure with the changes in it, and it will use them to fire the
change hook, instead of fetching the changes from undo_status(), so there
is no risk of race conditions.
- the various calls to mw.perform_op() have been split into separate
files like card_ops.py. Aside from making the code cleaner, this works
around a rather annoying issue with mypy. Because we run it with
no_strict_optional, mypy is happy to accept an operation that returns None,
despite the type signature saying it requires changes to be returned.
Turning no_strict_optional on for the whole codebase is not practical
at the moment, but we can enable it for individual files.
Still todo:
- The cursor keeps moving back to the start of a field when typing -
we need to ignore the refresh hook when we are the initiator.
- The busy cursor icon should probably be delayed a few hundreds ms.
- Still need to think about a nicer way of handling saveNow()
- op_made_changes(), op_affects_study_queue() might be better embedded
as properties in the object instead
2021-03-16 05:26:42 +01:00
|
|
|
def bury_current_card(self) -> None:
|
2022-07-18 04:59:56 +02:00
|
|
|
gui_hooks.reviewer_will_bury_card(self.card.id)
|
2023-03-31 06:02:40 +02:00
|
|
|
bury_cards(
|
|
|
|
parent=self.mw,
|
|
|
|
card_ids=[self.card.id],
|
|
|
|
).success(
|
2021-10-23 03:04:26 +02:00
|
|
|
lambda res: tooltip(tr.studying_cards_buried(count=res.count))
|
|
|
|
).run_in_background()
|
undoable ops now return changes directly; add new *_ops.py files
- Introduced a new transact() method that wraps the return value
in a separate struct that describes the changes that were made.
- Changes are now gathered from the undo log, so we don't need to
guess at what was changed - eg if update_note() is called with identical
note contents, no changes are returned. Card changes will only be set
if cards were actually generated by the update_note() call, and tag
will only be set if a new tag was added.
- mw.perform_op() has been updated to expect the op to return the changes,
or a structure with the changes in it, and it will use them to fire the
change hook, instead of fetching the changes from undo_status(), so there
is no risk of race conditions.
- the various calls to mw.perform_op() have been split into separate
files like card_ops.py. Aside from making the code cleaner, this works
around a rather annoying issue with mypy. Because we run it with
no_strict_optional, mypy is happy to accept an operation that returns None,
despite the type signature saying it requires changes to be returned.
Turning no_strict_optional on for the whole codebase is not practical
at the moment, but we can enable it for individual files.
Still todo:
- The cursor keeps moving back to the start of a field when typing -
we need to ignore the refresh hook when we are the initiator.
- The busy cursor icon should probably be delayed a few hundreds ms.
- Still need to think about a nicer way of handling saveNow()
- op_made_changes(), op_affects_study_queue() might be better embedded
as properties in the object instead
2021-03-16 05:26:42 +01:00
|
|
|
|
2022-02-07 10:56:31 +01:00
|
|
|
def forget_current_card(self) -> None:
|
2022-03-09 07:51:41 +01:00
|
|
|
if op := forget_cards(
|
2022-02-07 10:56:31 +01:00
|
|
|
parent=self.mw,
|
|
|
|
card_ids=[self.card.id],
|
2022-03-09 07:51:41 +01:00
|
|
|
context=ScheduleCardsAsNew.Context.REVIEWER,
|
|
|
|
):
|
|
|
|
op.run_in_background()
|
2022-02-07 10:56:31 +01:00
|
|
|
|
2021-12-31 07:45:30 +01:00
|
|
|
def on_create_copy(self) -> None:
|
|
|
|
if self.card:
|
|
|
|
aqt.dialogs.open("AddCards", self.mw).set_note(
|
|
|
|
self.card.note(), self.card.did
|
|
|
|
)
|
|
|
|
|
2021-03-05 10:09:08 +01:00
|
|
|
def delete_current_note(self) -> None:
|
2012-12-22 00:51:21 +01:00
|
|
|
# need to check state because the shortcut is global to the main
|
|
|
|
# window
|
|
|
|
if self.mw.state != "review" or not self.card:
|
|
|
|
return
|
undoable ops now return changes directly; add new *_ops.py files
- Introduced a new transact() method that wraps the return value
in a separate struct that describes the changes that were made.
- Changes are now gathered from the undo log, so we don't need to
guess at what was changed - eg if update_note() is called with identical
note contents, no changes are returned. Card changes will only be set
if cards were actually generated by the update_note() call, and tag
will only be set if a new tag was added.
- mw.perform_op() has been updated to expect the op to return the changes,
or a structure with the changes in it, and it will use them to fire the
change hook, instead of fetching the changes from undo_status(), so there
is no risk of race conditions.
- the various calls to mw.perform_op() have been split into separate
files like card_ops.py. Aside from making the code cleaner, this works
around a rather annoying issue with mypy. Because we run it with
no_strict_optional, mypy is happy to accept an operation that returns None,
despite the type signature saying it requires changes to be returned.
Turning no_strict_optional on for the whole codebase is not practical
at the moment, but we can enable it for individual files.
Still todo:
- The cursor keeps moving back to the start of a field when typing -
we need to ignore the refresh hook when we are the initiator.
- The busy cursor icon should probably be delayed a few hundreds ms.
- Still need to think about a nicer way of handling saveNow()
- op_made_changes(), op_affects_study_queue() might be better embedded
as properties in the object instead
2021-03-16 05:26:42 +01:00
|
|
|
|
2021-04-06 06:56:36 +02:00
|
|
|
remove_notes(parent=self.mw, note_ids=[self.card.nid]).run_in_background()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-02-27 04:11:21 +01:00
|
|
|
def onRecordVoice(self) -> None:
|
2021-02-01 14:28:21 +01:00
|
|
|
def after_record(path: str) -> None:
|
2020-12-16 10:09:45 +01:00
|
|
|
self._recordedAudio = path
|
|
|
|
self.onReplayRecorded()
|
|
|
|
|
2020-12-18 07:52:00 +01:00
|
|
|
record_audio(self.mw, self.mw, False, after_record)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-02-27 04:11:21 +01:00
|
|
|
def onReplayRecorded(self) -> None:
|
2022-05-18 05:44:56 +02:00
|
|
|
self._recordedAudio = gui_hooks.reviewer_will_replay_recording(
|
|
|
|
self._recordedAudio
|
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
if not self._recordedAudio:
|
2021-03-26 04:48:26 +01:00
|
|
|
tooltip(tr.studying_you_havent_recorded_your_voice_yet())
|
2020-02-27 04:11:21 +01:00
|
|
|
return
|
2020-01-20 11:10:38 +01:00
|
|
|
av_player.play_file(self._recordedAudio)
|
2021-03-04 12:40:59 +01:00
|
|
|
|
|
|
|
# legacy
|
|
|
|
|
|
|
|
onBuryCard = bury_current_card
|
|
|
|
onBuryNote = bury_current_note
|
|
|
|
onSuspend = suspend_current_note
|
|
|
|
onSuspendCard = suspend_current_card
|
2021-03-05 10:09:08 +01:00
|
|
|
onDelete = delete_current_note
|
2021-03-05 13:45:55 +01:00
|
|
|
onMark = toggle_mark_on_current_note
|
2021-03-06 15:17:17 +01:00
|
|
|
setFlag = set_flag_on_current_card
|
2023-03-16 07:31:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
RUN_STATE_MUTATION = """
|
|
|
|
anki.mutateNextCardStates('{key}', async (states, customData, ctx) => {{ {js} }})
|
|
|
|
.finally(() => bridgeCommand('statesMutated'));
|
|
|
|
"""
|