From 1b4dff62755487d818753b4d517e88a0c75bbc2b Mon Sep 17 00:00:00 2001 From: Martin Milata Date: Thu, 3 Nov 2022 11:51:04 +0100 Subject: [PATCH] fix(core/rust/ui): respect DISABLE_ANIMATION [no changelog] --- core/embed/rust/librust_qstr.h | 1 + .../rust/src/ui/component/text/paragraphs.rs | 4 ++++ core/embed/rust/src/ui/layout/util.rs | 18 +++++++++++--- core/embed/rust/src/ui/model_t1/layout.rs | 5 ++++ core/embed/rust/src/ui/model_tr/layout.rs | 5 ++++ .../ui/model_tt/component/hold_to_confirm.rs | 3 ++- .../rust/src/ui/model_tt/component/loader.rs | 5 +++- core/embed/rust/src/ui/model_tt/layout.rs | 8 ++++++- core/embed/rust/src/ui/util.rs | 24 +++++++++++++++++++ core/mocks/generated/trezorui2.pyi | 15 ++++++++++++ core/src/trezor/ui/layouts/tr/__init__.py | 5 ++++ core/src/trezor/ui/layouts/tt_v2/__init__.py | 6 +++++ 12 files changed, 93 insertions(+), 6 deletions(-) diff --git a/core/embed/rust/librust_qstr.h b/core/embed/rust/librust_qstr.h index a89dbfce26..198feeb13a 100644 --- a/core/embed/rust/librust_qstr.h +++ b/core/embed/rust/librust_qstr.h @@ -16,6 +16,7 @@ static void _librust_qstrs(void) { MP_QSTR_CONFIRMED; MP_QSTR_CANCELLED; MP_QSTR_INFO; + MP_QSTR_disable_animation; MP_QSTR_confirm_action; MP_QSTR_confirm_blob; MP_QSTR_confirm_properties; diff --git a/core/embed/rust/src/ui/component/text/paragraphs.rs b/core/embed/rust/src/ui/component/text/paragraphs.rs index f4e693493a..f654013d8a 100644 --- a/core/embed/rust/src/ui/component/text/paragraphs.rs +++ b/core/embed/rust/src/ui/component/text/paragraphs.rs @@ -89,6 +89,10 @@ where self } + pub fn inner(&self) -> &T { + &self.source + } + pub fn inner_mut(&mut self) -> &mut T { &mut self.source } diff --git a/core/embed/rust/src/ui/layout/util.rs b/core/embed/rust/src/ui/layout/util.rs index c07773954b..134b60707c 100644 --- a/core/embed/rust/src/ui/layout/util.rs +++ b/core/embed/rust/src/ui/layout/util.rs @@ -6,10 +6,14 @@ use crate::{ iter::{Iter, IterBuf}, list::List, obj::Obj, + util::try_or_raise, }, - ui::component::text::{ - paragraphs::{Paragraph, ParagraphSource, ParagraphStrType}, - TextStyle, + ui::{ + component::text::{ + paragraphs::{Paragraph, ParagraphSource, ParagraphStrType}, + TextStyle, + }, + util::set_animation_disabled, }, }; use cstr_core::cstr; @@ -228,3 +232,11 @@ impl ParagraphStrType for StrBuffer { self.offset(chars) } } + +pub extern "C" fn upy_disable_animation(disable: Obj) -> Obj { + let block = || { + set_animation_disabled(disable.try_into()?); + Ok(Obj::const_none()) + }; + unsafe { try_or_raise(block) } +} diff --git a/core/embed/rust/src/ui/model_t1/layout.rs b/core/embed/rust/src/ui/model_t1/layout.rs index d983771aa2..aefea39527 100644 --- a/core/embed/rust/src/ui/model_t1/layout.rs +++ b/core/embed/rust/src/ui/model_t1/layout.rs @@ -13,6 +13,7 @@ use crate::{ layout::{ obj::{ComponentMsgObj, LayoutObj}, result::{CANCELLED, CONFIRMED}, + util::upy_disable_animation, }, }, }; @@ -116,6 +117,10 @@ pub static mp_module_trezorui2: Module = obj_module! { /// CANCELLED: object Qstr::MP_QSTR_CANCELLED => CANCELLED.as_obj(), + /// def disable_animation(disable: bool) -> None: + /// """Disable animations, debug builds only.""" + Qstr::MP_QSTR_disable_animation => obj_fn_1!(upy_disable_animation).as_obj(), + /// def confirm_action( /// *, /// title: str, diff --git a/core/embed/rust/src/ui/model_tr/layout.rs b/core/embed/rust/src/ui/model_tr/layout.rs index bc8114925e..2aa7d98609 100644 --- a/core/embed/rust/src/ui/model_tr/layout.rs +++ b/core/embed/rust/src/ui/model_tr/layout.rs @@ -13,6 +13,7 @@ use crate::{ layout::{ obj::{ComponentMsgObj, LayoutObj}, result::{CANCELLED, CONFIRMED}, + util::upy_disable_animation, }, }, }; @@ -116,6 +117,10 @@ pub static mp_module_trezorui2: Module = obj_module! { /// CANCELLED: object Qstr::MP_QSTR_CANCELLED => CANCELLED.as_obj(), + /// def disable_animation(disable: bool) -> None: + /// """Disable animations, debug builds only.""" + Qstr::MP_QSTR_disable_animation => obj_fn_1!(upy_disable_animation).as_obj(), + /// def confirm_action( /// *, /// title: str, diff --git a/core/embed/rust/src/ui/model_tt/component/hold_to_confirm.rs b/core/embed/rust/src/ui/model_tt/component/hold_to_confirm.rs index 923e0ddc12..952cce46b9 100644 --- a/core/embed/rust/src/ui/model_tt/component/hold_to_confirm.rs +++ b/core/embed/rust/src/ui/model_tt/component/hold_to_confirm.rs @@ -3,6 +3,7 @@ use crate::{ ui::{ component::{Child, Component, ComponentExt, Event, EventCtx, FixedHeightBar, Pad}, geometry::{Grid, Insets, Rect}, + util::animation_disabled, }, }; @@ -220,7 +221,7 @@ where loader.start_shrinking(ctx, now); } Some(ButtonMsg::Clicked) => { - if loader.is_completely_grown(now) { + if loader.is_completely_grown(now) || animation_disabled() { return true; } else { loader.start_shrinking(ctx, now); diff --git a/core/embed/rust/src/ui/model_tt/component/loader.rs b/core/embed/rust/src/ui/model_tt/component/loader.rs index 09dd31983b..4c42bfc56a 100644 --- a/core/embed/rust/src/ui/model_tt/component/loader.rs +++ b/core/embed/rust/src/ui/model_tt/component/loader.rs @@ -6,6 +6,7 @@ use crate::{ display::{self, Color}, geometry::{Offset, Rect}, model_tt::constant, + util::animation_disabled, }, }; @@ -131,7 +132,9 @@ impl Component for Loader { if let Event::Timer(EventCtx::ANIM_FRAME_TIMER) = event { if self.is_animating() { // We have something to paint, so request to be painted in the next pass. - ctx.request_paint(); + if !animation_disabled() { + ctx.request_paint(); + } if self.is_completely_grown(now) { return Some(LoaderMsg::GrownCompletely); diff --git a/core/embed/rust/src/ui/model_tt/layout.rs b/core/embed/rust/src/ui/model_tt/layout.rs index 7a0b14ab2b..3107993668 100644 --- a/core/embed/rust/src/ui/model_tt/layout.rs +++ b/core/embed/rust/src/ui/model_tt/layout.rs @@ -32,7 +32,9 @@ use crate::{ layout::{ obj::{ComponentMsgObj, LayoutObj}, result::{CANCELLED, CONFIRMED, INFO}, - util::{iter_into_array, iter_into_objs, ConfirmBlob, PropsList}, + util::{ + iter_into_array, iter_into_objs, upy_disable_animation, ConfirmBlob, PropsList, + }, }, }, }; @@ -1175,6 +1177,10 @@ pub static mp_module_trezorui2: Module = obj_module! { /// INFO: object Qstr::MP_QSTR_INFO => INFO.as_obj(), + /// def disable_animation(disable: bool) -> None: + /// """Disable animations, debug builds only.""" + Qstr::MP_QSTR_disable_animation => obj_fn_1!(upy_disable_animation).as_obj(), + /// def confirm_action( /// *, /// title: str, diff --git a/core/embed/rust/src/ui/util.rs b/core/embed/rust/src/ui/util.rs index 1300aae164..e6b64821d7 100644 --- a/core/embed/rust/src/ui/util.rs +++ b/core/embed/rust/src/ui/util.rs @@ -31,6 +31,30 @@ pub fn u32_to_str(num: u32, buffer: &mut [u8]) -> Option<&str> { } } +#[cfg(feature = "ui_debug")] +static mut DISABLE_ANIMATION: bool = false; + +#[cfg(feature = "ui_debug")] +pub fn animation_disabled() -> bool { + // SAFETY: single-threaded access + unsafe { DISABLE_ANIMATION } +} + +#[cfg(feature = "ui_debug")] +pub fn set_animation_disabled(disabled: bool) { + // SAFETY: single-threaded access + unsafe { + DISABLE_ANIMATION = disabled; + } +} + +#[cfg(not(feature = "ui_debug"))] +pub fn animation_disabled() -> bool { + false +} +#[cfg(not(feature = "ui_debug"))] +pub fn set_animation_disabled(_disabled: bool) {} + #[cfg(test)] mod tests { use super::*; diff --git a/core/mocks/generated/trezorui2.pyi b/core/mocks/generated/trezorui2.pyi index 6785c6a9ff..6b043d903c 100644 --- a/core/mocks/generated/trezorui2.pyi +++ b/core/mocks/generated/trezorui2.pyi @@ -3,6 +3,11 @@ CONFIRMED: object CANCELLED: object +# rust/src/ui/model_t1/layout.rs +def disable_animation(disable: bool) -> None: + """Disable animations, debug builds only.""" + + # rust/src/ui/model_t1/layout.rs def confirm_action( *, @@ -29,6 +34,11 @@ CONFIRMED: object CANCELLED: object +# rust/src/ui/model_tr/layout.rs +def disable_animation(disable: bool) -> None: + """Disable animations, debug builds only.""" + + # rust/src/ui/model_tr/layout.rs def confirm_action( *, @@ -56,6 +66,11 @@ CANCELLED: object INFO: object +# rust/src/ui/model_tt/layout.rs +def disable_animation(disable: bool) -> None: + """Disable animations, debug builds only.""" + + # rust/src/ui/model_tt/layout.rs def confirm_action( *, diff --git a/core/src/trezor/ui/layouts/tr/__init__.py b/core/src/trezor/ui/layouts/tr/__init__.py index ca61dea6e2..11a03ff271 100644 --- a/core/src/trezor/ui/layouts/tr/__init__.py +++ b/core/src/trezor/ui/layouts/tr/__init__.py @@ -2,6 +2,7 @@ from typing import TYPE_CHECKING, Sequence from trezor import io, log, loop, ui, wire, workflow from trezor.enums import ButtonRequestType +from trezor.utils import DISABLE_ANIMATION import trezorui2 @@ -14,6 +15,10 @@ if TYPE_CHECKING: ExceptionType = BaseException | Type[BaseException] +if __debug__: + trezorui2.disable_animation(bool(DISABLE_ANIMATION)) + + class _RustLayout(ui.Layout): # pylint: disable=super-init-not-called def __init__(self, layout: Any): diff --git a/core/src/trezor/ui/layouts/tt_v2/__init__.py b/core/src/trezor/ui/layouts/tt_v2/__init__.py index 71fd2c552f..6efd7aa10c 100644 --- a/core/src/trezor/ui/layouts/tt_v2/__init__.py +++ b/core/src/trezor/ui/layouts/tt_v2/__init__.py @@ -24,6 +24,12 @@ CANCELLED = trezorui2.CANCELLED INFO = trezorui2.INFO +if __debug__: + from trezor.utils import DISABLE_ANIMATION + + trezorui2.disable_animation(bool(DISABLE_ANIMATION)) + + class _RustLayout(ui.Layout): # pylint: disable=super-init-not-called def __init__(self, layout: Any, is_backup: bool = False):