2022-10-30 16:59:59 +00:00
|
|
|
from typing import TYPE_CHECKING
|
2019-08-08 14:27:49 +00:00
|
|
|
|
2024-03-15 10:47:31 +00:00
|
|
|
from . import config
|
|
|
|
|
2022-10-30 16:59:59 +00:00
|
|
|
if TYPE_CHECKING:
|
2024-03-15 10:47:31 +00:00
|
|
|
from typing import Any, Container
|
2023-08-15 15:56:09 +00:00
|
|
|
|
2022-10-30 16:59:59 +00:00
|
|
|
from trezor.ui.layouts.common import ProgressLayout
|
2018-02-05 14:13:33 +00:00
|
|
|
|
2021-03-18 09:48:50 +00:00
|
|
|
_previous_seconds: int | None = None
|
2022-10-30 16:59:59 +00:00
|
|
|
_previous_remaining: str | None = None
|
|
|
|
_progress_layout: ProgressLayout | None = None
|
2023-01-13 15:21:27 +00:00
|
|
|
_started_with_empty_loader = False
|
2020-10-16 17:39:32 +00:00
|
|
|
keepalive_callback: Any = None
|
2019-08-08 14:27:49 +00:00
|
|
|
|
2024-03-15 10:47:31 +00:00
|
|
|
_ignore_loader_messages: Container[config.StorageMessage] = ()
|
2023-02-21 11:30:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
def ignore_nonpin_loader_messages() -> None:
|
|
|
|
global _ignore_loader_messages
|
2024-03-15 10:47:31 +00:00
|
|
|
_ignore_loader_messages = (
|
|
|
|
config.StorageMessage.PROCESSING_MSG,
|
|
|
|
config.StorageMessage.STARTING_MSG,
|
|
|
|
)
|
2023-02-21 11:30:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
def allow_all_loader_messages() -> None:
|
|
|
|
global _ignore_loader_messages
|
|
|
|
_ignore_loader_messages = ()
|
|
|
|
|
2019-04-11 10:32:37 +00:00
|
|
|
|
2024-03-15 10:47:31 +00:00
|
|
|
def render_empty_loader(message: config.StorageMessage, description: str = "") -> None:
|
2023-01-13 15:21:27 +00:00
|
|
|
"""Render empty loader to prevent the screen appear to be frozen."""
|
2023-05-04 12:38:31 +00:00
|
|
|
from trezor.ui.layouts.progress import pin_progress
|
2023-01-13 15:21:27 +00:00
|
|
|
|
|
|
|
global _progress_layout
|
|
|
|
global _started_with_empty_loader
|
|
|
|
|
|
|
|
_progress_layout = pin_progress(message, description)
|
|
|
|
_progress_layout.report(0, None)
|
|
|
|
|
|
|
|
_started_with_empty_loader = True
|
|
|
|
|
|
|
|
|
2024-03-15 10:47:31 +00:00
|
|
|
def show_pin_timeout(
|
|
|
|
seconds: int, progress: int, message: config.StorageMessage
|
|
|
|
) -> bool:
|
2023-08-11 15:57:32 +00:00
|
|
|
from trezor import TR
|
2023-05-04 12:38:31 +00:00
|
|
|
from trezor.ui.layouts.progress import pin_progress
|
2021-03-22 15:14:24 +00:00
|
|
|
|
2023-02-21 11:30:24 +00:00
|
|
|
# Possibility to ignore certain messages - not showing loader for them
|
|
|
|
if message in _ignore_loader_messages:
|
2023-02-21 11:25:55 +00:00
|
|
|
return False
|
|
|
|
|
2019-09-06 17:53:28 +00:00
|
|
|
global _previous_seconds
|
2022-10-30 16:59:59 +00:00
|
|
|
global _previous_remaining
|
|
|
|
global _progress_layout
|
2023-01-13 15:21:27 +00:00
|
|
|
global _started_with_empty_loader
|
2019-04-11 10:32:37 +00:00
|
|
|
|
2019-08-08 14:27:49 +00:00
|
|
|
if callable(keepalive_callback):
|
|
|
|
keepalive_callback()
|
|
|
|
|
2022-10-30 16:59:59 +00:00
|
|
|
if progress == 0 or _progress_layout is None:
|
|
|
|
_previous_seconds = None
|
2019-09-06 17:53:28 +00:00
|
|
|
|
|
|
|
if seconds != _previous_seconds:
|
|
|
|
if seconds == 0:
|
2023-08-11 15:57:32 +00:00
|
|
|
remaining = TR.progress__done
|
2019-09-06 17:53:28 +00:00
|
|
|
elif seconds == 1:
|
2023-08-11 15:57:32 +00:00
|
|
|
remaining = TR.progress__one_second_left
|
2019-09-06 17:53:28 +00:00
|
|
|
else:
|
2023-08-11 15:57:32 +00:00
|
|
|
# TODO: make this a plural
|
|
|
|
remaining = TR.progress__x_seconds_left_template.format(seconds)
|
2022-10-30 16:59:59 +00:00
|
|
|
_previous_remaining = remaining
|
2019-09-06 17:53:28 +00:00
|
|
|
_previous_seconds = seconds
|
2022-10-30 16:59:59 +00:00
|
|
|
else:
|
|
|
|
remaining = _previous_remaining
|
|
|
|
|
2023-01-13 15:21:27 +00:00
|
|
|
# create the layout if it doesn't exist yet or should be started again
|
|
|
|
if _progress_layout is None or (progress == 0 and not _started_with_empty_loader):
|
2022-10-30 16:59:59 +00:00
|
|
|
_progress_layout = pin_progress(message, description=remaining or "")
|
2023-01-13 15:21:27 +00:00
|
|
|
|
|
|
|
# reset the flag - the render_empty_loader() has the effect only in the first call
|
|
|
|
# of this function, where we do not want to re-initialize the layout
|
|
|
|
# to avoid a screen flicker
|
|
|
|
_started_with_empty_loader = False
|
|
|
|
|
|
|
|
# update the progress layout
|
2022-10-30 16:59:59 +00:00
|
|
|
_progress_layout.report(progress, remaining)
|
2023-01-13 15:21:27 +00:00
|
|
|
|
2022-10-30 16:59:59 +00:00
|
|
|
# drop the layout when done so trezor.ui doesn't have to remain in memory
|
2023-10-16 16:16:55 +00:00
|
|
|
if progress >= 1000:
|
2022-10-30 16:59:59 +00:00
|
|
|
_progress_layout = None
|
2019-04-11 10:32:37 +00:00
|
|
|
|
2019-02-07 11:01:08 +00:00
|
|
|
return False
|