1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-01 11:58:28 +00:00

refactor(core): move functions to trezorui_api

- disable_animations
- check_homescreen_format
This commit is contained in:
obrusvit 2024-10-26 13:47:05 +02:00
parent 817dedcaf4
commit cd32737141
12 changed files with 176 additions and 177 deletions

View File

@ -1,6 +1,6 @@
use crate::{
micropython::{
macros::{obj_fn_kw, obj_module},
macros::{obj_fn_1, obj_fn_kw, obj_module},
map::Map,
module::Module,
obj::Obj,
@ -13,7 +13,7 @@ use crate::{
layout::{
base::LAYOUT_STATE,
obj::{LayoutObj, ATTACH_TYPE_OBJ},
result::{CANCELLED, CONFIRMED, INFO},
result::{CANCELLED, CONFIRMED, INFO}, util::upy_disable_animation,
},
ui_features::ModelUI,
ui_features_fw::UIFeaturesFirmware,
@ -22,6 +22,7 @@ use crate::{
// free-standing functions exported to MicroPython mirror `trait
// UIFeaturesFirmware`
// NOTE: `disable_animation` not a part of trait UiFeaturesFirmware
extern "C" fn new_request_bip39(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj {
let block = move |_args: &[Obj], kwargs: &Map| {
@ -86,6 +87,15 @@ extern "C" fn show_info(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Ob
unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) }
}
pub extern "C" fn upy_check_homescreen_format(data: Obj) -> Obj {
let block = || {
let buffer = data.try_into()?;
Ok(ModelUI::check_homescreen_format(buffer, false).into())
};
unsafe { util::try_or_raise(block) }
}
#[no_mangle]
pub static mp_module_trezorui_api: Module = obj_module! {
/// from trezor import utils
@ -182,6 +192,14 @@ pub static mp_module_trezorui_api: Module = obj_module! {
/// INFO: UiResult
Qstr::MP_QSTR_INFO => INFO.as_obj(),
/// def check_homescreen_format(data: bytes) -> bool:
/// """Check homescreen format and dimensions."""
Qstr::MP_QSTR_check_homescreen_format => obj_fn_1!(upy_check_homescreen_format).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 request_bip39(
/// *,
/// prompt: str,

View File

@ -1406,15 +1406,6 @@ extern "C" fn new_show_lockscreen(n_args: usize, args: *const Obj, kwargs: *mut
unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) }
}
pub extern "C" fn upy_check_homescreen_format(data: Obj) -> Obj {
let block = || {
let buffer = data.try_into()?;
Ok(check_homescreen_format(buffer).into())
};
unsafe { util::try_or_raise(block) }
}
extern "C" fn new_show_wait_text(message: Obj) -> Obj {
let block = || {
let message: TString<'static> = message.try_into()?;
@ -1455,31 +1446,6 @@ pub static mp_module_trezorui2: Module = obj_module! {
///
Qstr::MP_QSTR___name__ => Qstr::MP_QSTR_trezorui2.to_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 check_homescreen_format(data: bytes) -> bool:
/// """Check homescreen format and dimensions."""
Qstr::MP_QSTR_check_homescreen_format => obj_fn_1!(upy_check_homescreen_format).as_obj(),
/// def confirm_action(
/// *,
/// title: str,
/// action: str | None,
/// description: str | None,
/// subtitle: str | None = None,
/// verb: str | None = None,
/// verb_cancel: str | None = None,
/// hold: bool = False,
/// hold_danger: bool = False,
/// reverse: bool = False,
/// prompt_screen: bool = False,
/// prompt_title: str | None = None,
/// ) -> LayoutObj[UiResult]:
/// """Confirm action."""
Qstr::MP_QSTR_confirm_action => obj_fn_kw!(0, new_confirm_action).as_obj(),
/// def confirm_emphasized(
/// *,
/// title: str,

View File

@ -1,5 +1,6 @@
use crate::{
error::Error,
io::BinaryData,
micropython::gc::Gc,
strutil::TString,
translations::TR,
@ -21,6 +22,37 @@ use super::{
};
impl UIFeaturesFirmware for ModelMercuryFeatures {
fn confirm_action(
title: TString<'static>,
action: Option<TString<'static>>,
description: Option<TString<'static>>,
subtitle: Option<TString<'static>>,
verb: Option<TString<'static>>,
verb_cancel: Option<TString<'static>>,
hold: bool,
hold_danger: bool,
reverse: bool,
prompt_screen: bool,
prompt_title: Option<TString<'static>>,
) -> Result<impl LayoutMaybeTrace, Error> {
let flow = flow::confirm_action::new_confirm_action(
title,
action,
description,
subtitle,
verb_cancel,
reverse,
hold,
prompt_screen,
prompt_title.unwrap_or(TString::empty()),
)?;
Ok(flow)
}
fn check_homescreen_format(image: BinaryData, __accept_toif: bool) -> bool {
super::component::check_homescreen_format(image)
}
fn request_bip39(
prompt: TString<'static>,
prefill_word: TString<'static>,

View File

@ -48,9 +48,8 @@ use crate::{
layout::{
obj::{ComponentMsgObj, LayoutObj},
result::{CANCELLED, CONFIRMED, INFO},
util::{upy_disable_animation, ConfirmBlob, RecoveryType},
util::{ConfirmBlob, RecoveryType},
},
model_tr::component::check_homescreen_format,
},
};
@ -1543,15 +1542,6 @@ extern "C" fn new_confirm_firmware_update(
unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) }
}
pub extern "C" fn upy_check_homescreen_format(data: Obj) -> Obj {
let block = || {
let image = data.try_into()?;
Ok(check_homescreen_format(image).into())
};
unsafe { util::try_or_raise(block) }
}
extern "C" fn new_show_wait_text(message: Obj) -> Obj {
let block = || {
let message: TString<'static> = message.try_into()?;
@ -1569,31 +1559,6 @@ pub static mp_module_trezorui2: Module = obj_module! {
///
Qstr::MP_QSTR___name__ => Qstr::MP_QSTR_trezorui2.to_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 check_homescreen_format(data: bytes) -> bool:
/// """Check homescreen format and dimensions."""
Qstr::MP_QSTR_check_homescreen_format => obj_fn_1!(upy_check_homescreen_format).as_obj(),
/// def confirm_action(
/// *,
/// title: str,
/// action: str | None,
/// description: str | None,
/// subtitle: str | None = None,
/// verb: str = "CONFIRM",
/// verb_cancel: str | None = None,
/// hold: bool = False,
/// hold_danger: bool = False, # unused on TR
/// reverse: bool = False,
/// prompt_screen: bool = False,
/// prompt_title: str | None = None,
/// ) -> LayoutObj[UiResult]:
/// """Confirm action."""
Qstr::MP_QSTR_confirm_action => obj_fn_kw!(0, new_confirm_action).as_obj(),
/// def confirm_homescreen(
/// *,
/// title: str,

View File

@ -1,5 +1,7 @@
use crate::{
error::Error,
io::BinaryData,
maybe_trace::MaybeTrace,
micropython::gc::Gc,
strutil::TString,
ui::{
@ -18,6 +20,48 @@ use super::{
};
impl UIFeaturesFirmware for ModelTRFeatures {
fn confirm_action(
title: TString<'static>,
action: Option<TString<'static>>,
description: Option<TString<'static>>,
subtitle: Option<TString<'static>>,
verb: Option<TString<'static>>,
verb_cancel: Option<TString<'static>>,
hold: bool,
hold_danger: bool,
reverse: bool,
prompt_screen: bool,
prompt_title: Option<TString<'static>>,
) -> Result<impl LayoutMaybeTrace, Error> {
let paragraphs = {
let action = action.unwrap_or("".into());
let description = description.unwrap_or("".into());
let mut paragraphs = ParagraphVecShort::new();
if !reverse {
paragraphs
.add(Paragraph::new(&theme::TEXT_BOLD, action))
.add(Paragraph::new(&theme::TEXT_NORMAL, description));
} else {
paragraphs
.add(Paragraph::new(&theme::TEXT_NORMAL, description))
.add(Paragraph::new(&theme::TEXT_BOLD, action));
}
paragraphs.into_paragraphs()
};
content_in_button_page(
title,
paragraphs,
verb.unwrap_or(TString::empty()),
verb_cancel,
hold,
)
}
fn check_homescreen_format(image: BinaryData, _accept_toif: bool) -> bool {
super::component::check_homescreen_format(image)
}
fn request_bip39(
prompt: TString<'static>,
prefill_word: TString<'static>,

View File

@ -51,7 +51,7 @@ use crate::{
layout::{
obj::{ComponentMsgObj, LayoutObj},
result::{CANCELLED, CONFIRMED, INFO},
util::{upy_disable_animation, ConfirmBlob, PropsList, RecoveryType},
util::{ConfirmBlob, PropsList, RecoveryType},
},
model_tt::component::check_homescreen_format,
},
@ -1515,15 +1515,6 @@ extern "C" fn new_show_lockscreen(n_args: usize, args: *const Obj, kwargs: *mut
unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) }
}
pub extern "C" fn upy_check_homescreen_format(data: Obj) -> Obj {
let block = || {
let buffer = data.try_into()?;
Ok(check_homescreen_format(buffer, false).into())
};
unsafe { util::try_or_raise(block) }
}
#[no_mangle]
extern "C" fn new_confirm_firmware_update(
n_args: usize,
@ -1569,13 +1560,6 @@ pub static mp_module_trezorui2: Module = obj_module! {
/// from trezor import utils
/// from trezorui_api import *
///
/// 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 check_homescreen_format(data: bytes) -> bool:
/// """Check homescreen format and dimensions."""
Qstr::MP_QSTR_check_homescreen_format => obj_fn_1!(upy_check_homescreen_format).as_obj(),
/// def confirm_action(
/// *,

View File

@ -1,5 +1,6 @@
use crate::{
error::Error,
io::BinaryData,
micropython::gc::Gc,
strutil::TString,
translations::TR,
@ -19,6 +20,51 @@ use super::{
};
impl UIFeaturesFirmware for ModelTTFeatures {
fn confirm_action(
title: TString<'static>,
action: Option<TString<'static>>,
description: Option<TString<'static>>,
subtitle: Option<TString<'static>>,
verb: Option<TString<'static>>,
verb_cancel: Option<TString<'static>>,
hold: bool,
hold_danger: bool,
reverse: bool,
prompt_screen: bool,
prompt_title: Option<TString<'static>>,
) -> Result<impl LayoutMaybeTrace, Error> {
let paragraphs = {
let action = action.unwrap_or("".into());
let description = description.unwrap_or("".into());
let mut paragraphs = ParagraphVecShort::new();
if !reverse {
paragraphs
.add(Paragraph::new(&theme::TEXT_DEMIBOLD, action))
.add(Paragraph::new(&theme::TEXT_NORMAL, description));
} else {
paragraphs
.add(Paragraph::new(&theme::TEXT_NORMAL, description))
.add(Paragraph::new(&theme::TEXT_DEMIBOLD, action));
}
paragraphs.into_paragraphs()
};
let mut page = if hold {
ButtonPage::new(paragraphs, theme::BG).with_hold()?
} else {
ButtonPage::new(paragraphs, theme::BG).with_cancel_confirm(verb_cancel, verb)
};
if hold && hold_danger {
page = page.with_confirm_style(theme::button_danger())
}
let layout = RootComponent::new(Frame::left_aligned(theme::label_title(), title, page));
Ok(layout)
}
fn check_homescreen_format(image: BinaryData, accept_toif: bool) -> bool {
super::component::check_homescreen_format(image, false)
}
fn request_bip39(
prompt: TString<'static>,
prefill_word: TString<'static>,

View File

@ -1,8 +1,24 @@
use crate::{error::Error, micropython::gc::Gc, strutil::TString};
use crate::{error::Error, io::BinaryData, micropython::gc::Gc, strutil::TString};
use super::layout::obj::{LayoutMaybeTrace, LayoutObj};
pub trait UIFeaturesFirmware {
fn confirm_action(
title: TString<'static>,
action: Option<TString<'static>>,
description: Option<TString<'static>>,
subtitle: Option<TString<'static>>,
verb: Option<TString<'static>>,
verb_cancel: Option<TString<'static>>,
hold: bool,
hold_danger: bool,
reverse: bool,
prompt_screen: bool,
prompt_title: Option<TString<'static>>,
) -> Result<impl LayoutMaybeTrace, Error>;
fn check_homescreen_format(image: BinaryData, accept_toif: bool) -> bool;
fn request_bip39(
prompt: TString<'static>,
prefill_word: TString<'static>,

View File

@ -3,34 +3,6 @@ from trezor import utils
from trezorui_api import *
# rust/src/ui/model_mercury/layout.rs
def disable_animation(disable: bool) -> None:
"""Disable animations, debug builds only."""
# rust/src/ui/model_mercury/layout.rs
def check_homescreen_format(data: bytes) -> bool:
"""Check homescreen format and dimensions."""
# rust/src/ui/model_mercury/layout.rs
def confirm_action(
*,
title: str,
action: str | None,
description: str | None,
subtitle: str | None = None,
verb: str | None = None,
verb_cancel: str | None = None,
hold: bool = False,
hold_danger: bool = False,
reverse: bool = False,
prompt_screen: bool = False,
prompt_title: str | None = None,
) -> LayoutObj[UiResult]:
"""Confirm action."""
# rust/src/ui/model_mercury/layout.rs
def confirm_emphasized(
*,
@ -509,34 +481,6 @@ from trezor import utils
from trezorui_api import *
# 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 check_homescreen_format(data: bytes) -> bool:
"""Check homescreen format and dimensions."""
# rust/src/ui/model_tr/layout.rs
def confirm_action(
*,
title: str,
action: str | None,
description: str | None,
subtitle: str | None = None,
verb: str = "CONFIRM",
verb_cancel: str | None = None,
hold: bool = False,
hold_danger: bool = False, # unused on TR
reverse: bool = False,
prompt_screen: bool = False,
prompt_title: str | None = None,
) -> LayoutObj[UiResult]:
"""Confirm action."""
# rust/src/ui/model_tr/layout.rs
def confirm_homescreen(
*,
@ -926,34 +870,6 @@ from trezor import utils
from trezorui_api import *
# 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 check_homescreen_format(data: bytes) -> bool:
"""Check homescreen format and dimensions."""
# rust/src/ui/model_tt/layout.rs
def confirm_action(
*,
title: str,
action: str | None,
description: str | None,
subtitle: str | None = None,
verb: str | None = None,
verb_cancel: str | None = None,
hold: bool = False,
hold_danger: bool = False,
reverse: bool = False,
prompt_screen: bool = False,
prompt_title: str | None = None,
) -> LayoutObj[UiResult]:
"""Confirm action."""
# rust/src/ui/model_tt/layout.rs
def confirm_emphasized(
*,

View File

@ -68,6 +68,16 @@ CANCELLED: UiResult
INFO: UiResult
# rust/src/ui/api/firmware_upy.rs
def check_homescreen_format(data: bytes) -> bool:
"""Check homescreen format and dimensions."""
# rust/src/ui/api/firmware_upy.rs
def disable_animation(disable: bool) -> None:
"""Disable animations, debug builds only."""
# rust/src/ui/api/firmware_upy.rs
def request_bip39(
*,

View File

@ -1,7 +1,7 @@
from typing import TYPE_CHECKING
import storage.device as storage_device
import trezorui2
import trezorui_api
from trezor import TR, utils
from trezor.enums import ButtonRequestType, DisplayRotation
from trezor.ui.layouts import confirm_action
@ -25,7 +25,7 @@ def _validate_homescreen(homescreen: bytes) -> None:
raise DataError(
f"Homescreen is too large, maximum size is {storage_device.HOMESCREEN_MAXSIZE} bytes"
)
if not trezorui2.check_homescreen_format(homescreen):
if not trezorui_api.check_homescreen_format(homescreen):
raise DataError("Wrong homescreen format")

View File

@ -23,7 +23,9 @@ else:
if __debug__:
trezorui2.disable_animation(bool(utils.DISABLE_ANIMATION))
from trezorui_api import disable_animation
disable_animation(bool(utils.DISABLE_ANIMATION))
# all rendering is done through a singleton of `Display`