1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 15:38:11 +00:00

refactor(core/ui): introduce show_danger

This is a unified interface for flow_warning_hi_prio,
which was available only on Mercury before.

[no changelog]
This commit is contained in:
Ioan Bizău 2024-11-21 12:05:34 +01:00
parent f4a67564b7
commit 945b9010fa
9 changed files with 109 additions and 75 deletions

View File

@ -262,7 +262,6 @@ static void _librust_qstrs(void) {
MP_QSTR_flow_request_number; MP_QSTR_flow_request_number;
MP_QSTR_flow_request_passphrase; MP_QSTR_flow_request_passphrase;
MP_QSTR_flow_show_share_words; MP_QSTR_flow_show_share_words;
MP_QSTR_flow_warning_hi_prio;
MP_QSTR_get_language; MP_QSTR_get_language;
MP_QSTR_get_transition_out; MP_QSTR_get_transition_out;
MP_QSTR_haptic_feedback__disable; MP_QSTR_haptic_feedback__disable;
@ -652,6 +651,7 @@ static void _librust_qstrs(void) {
MP_QSTR_share_words__wrote_down_all; MP_QSTR_share_words__wrote_down_all;
MP_QSTR_show_address_details; MP_QSTR_show_address_details;
MP_QSTR_show_checklist; MP_QSTR_show_checklist;
MP_QSTR_show_danger;
MP_QSTR_show_error; MP_QSTR_show_error;
MP_QSTR_show_group_share_success; MP_QSTR_show_group_share_success;
MP_QSTR_show_homescreen; MP_QSTR_show_homescreen;

View File

@ -22,13 +22,13 @@ use super::super::{
}; };
#[derive(Copy, Clone, PartialEq, Eq)] #[derive(Copy, Clone, PartialEq, Eq)]
pub enum WarningHiPrio { pub enum Danger {
Message, Message,
Menu, Menu,
Cancelled, Cancelled,
} }
impl FlowController for WarningHiPrio { impl FlowController for Danger {
#[inline] #[inline]
fn index(&'static self) -> usize { fn index(&'static self) -> usize {
*self as usize *self as usize
@ -57,7 +57,7 @@ impl FlowController for WarningHiPrio {
const EXTRA_PADDING: i16 = 6; const EXTRA_PADDING: i16 = 6;
pub fn new_warning_hi_prio( pub fn new_show_danger(
title: TString<'static>, title: TString<'static>,
description: TString<'static>, description: TString<'static>,
value: TString<'static>, value: TString<'static>,
@ -106,9 +106,9 @@ pub fn new_warning_hi_prio(
.with_result_icon(theme::ICON_BULLET_CHECKMARK, theme::GREY_DARK) .with_result_icon(theme::ICON_BULLET_CHECKMARK, theme::GREY_DARK)
.map(|_| Some(FlowMsg::Cancelled)); .map(|_| Some(FlowMsg::Cancelled));
let res = SwipeFlow::new(&WarningHiPrio::Message)? let res = SwipeFlow::new(&Danger::Message)?
.with_page(&WarningHiPrio::Message, content_message)? .with_page(&Danger::Message, content_message)?
.with_page(&WarningHiPrio::Menu, content_menu)? .with_page(&Danger::Menu, content_menu)?
.with_page(&WarningHiPrio::Cancelled, content_cancelled)?; .with_page(&Danger::Cancelled, content_cancelled)?;
Ok(res) Ok(res)
} }

View File

@ -8,6 +8,7 @@ pub mod confirm_set_new_pin;
pub mod confirm_summary; pub mod confirm_summary;
pub mod confirm_with_info; pub mod confirm_with_info;
pub mod continue_recovery; pub mod continue_recovery;
pub mod danger;
pub mod get_address; pub mod get_address;
pub mod prompt_backup; pub mod prompt_backup;
pub mod request_number; pub mod request_number;
@ -16,7 +17,6 @@ pub mod set_brightness;
pub mod show_share_words; pub mod show_share_words;
pub mod show_tutorial; pub mod show_tutorial;
pub mod util; pub mod util;
pub mod warning_hi_prio;
pub use confirm_action::{ pub use confirm_action::{
new_confirm_action, new_confirm_action_simple, ConfirmActionExtra, ConfirmActionMenuStrings, new_confirm_action, new_confirm_action_simple, ConfirmActionExtra, ConfirmActionMenuStrings,
@ -31,6 +31,7 @@ pub use confirm_set_new_pin::SetNewPin;
pub use confirm_summary::new_confirm_summary; pub use confirm_summary::new_confirm_summary;
pub use confirm_with_info::new_confirm_with_info; pub use confirm_with_info::new_confirm_with_info;
pub use continue_recovery::new_continue_recovery; pub use continue_recovery::new_continue_recovery;
pub use danger::Danger;
pub use get_address::GetAddress; pub use get_address::GetAddress;
pub use prompt_backup::PromptBackup; pub use prompt_backup::PromptBackup;
pub use request_number::RequestNumber; pub use request_number::RequestNumber;
@ -39,4 +40,3 @@ pub use set_brightness::SetBrightness;
pub use show_share_words::ShowShareWords; pub use show_share_words::ShowShareWords;
pub use show_tutorial::ShowTutorial; pub use show_tutorial::ShowTutorial;
pub use util::{ConfirmBlobParams, ShowInfoParams}; pub use util::{ConfirmBlobParams, ShowInfoParams};
pub use warning_hi_prio::WarningHiPrio;

View File

@ -1510,7 +1510,7 @@ extern "C" fn new_confirm_fido(n_args: usize, args: *const Obj, kwargs: *mut Map
panic!(); panic!();
} }
extern "C" fn new_warning_hi_prio(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj { extern "C" fn new_show_danger(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj {
let block = move |_args: &[Obj], kwargs: &Map| { let block = move |_args: &[Obj], kwargs: &Map| {
let title: TString = kwargs.get(Qstr::MP_QSTR_title)?.try_into()?; let title: TString = kwargs.get(Qstr::MP_QSTR_title)?.try_into()?;
let description: TString = kwargs.get(Qstr::MP_QSTR_description)?.try_into()?; let description: TString = kwargs.get(Qstr::MP_QSTR_description)?.try_into()?;
@ -1521,7 +1521,7 @@ extern "C" fn new_warning_hi_prio(n_args: usize, args: *const Obj, kwargs: *mut
.try_into_option()?; .try_into_option()?;
let flow = let flow =
flow::warning_hi_prio::new_warning_hi_prio(title, description, value, verb_cancel)?; flow::danger::new_show_danger(title, description, value, verb_cancel)?;
Ok(LayoutObj::new_root(flow)?.into()) Ok(LayoutObj::new_root(flow)?.into())
}; };
unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) }
@ -1748,6 +1748,16 @@ pub static mp_module_trezorui2: Module = obj_module! {
/// """Warning modal. No buttons shown when `button` is empty string.""" /// """Warning modal. No buttons shown when `button` is empty string."""
Qstr::MP_QSTR_show_warning => obj_fn_kw!(0, new_show_warning).as_obj(), Qstr::MP_QSTR_show_warning => obj_fn_kw!(0, new_show_warning).as_obj(),
/// def show_danger(
/// *,
/// title: str,
/// description: str,
/// value: str = "",
/// verb_cancel: str | None = None,
/// ) -> LayoutObj[UiResult]:
/// """Warning modal that makes it easier to cancel than to continue."""
Qstr::MP_QSTR_show_danger => obj_fn_kw!(0, new_show_danger).as_obj(),
/// def show_success( /// def show_success(
/// *, /// *,
/// title: str, /// title: str,
@ -2002,16 +2012,6 @@ pub static mp_module_trezorui2: Module = obj_module! {
/// """Get address / receive funds.""" /// """Get address / receive funds."""
Qstr::MP_QSTR_flow_get_address => obj_fn_kw!(0, new_get_address).as_obj(), Qstr::MP_QSTR_flow_get_address => obj_fn_kw!(0, new_get_address).as_obj(),
/// def flow_warning_hi_prio(
/// *,
/// title: str,
/// description: str,
/// value: str = "",
/// verb_cancel: str | None = None,
/// ) -> LayoutObj[UiResult]:
/// """Warning modal with multiple steps to confirm."""
Qstr::MP_QSTR_flow_warning_hi_prio => obj_fn_kw!(0, new_warning_hi_prio).as_obj(),
/// def flow_confirm_output( /// def flow_confirm_output(
/// *, /// *,
/// title: str | None, /// title: str | None,

View File

@ -229,6 +229,17 @@ def show_warning(
"""Warning modal. No buttons shown when `button` is empty string.""" """Warning modal. No buttons shown when `button` is empty string."""
# rust/src/ui/model_mercury/layout.rs
def show_danger(
*,
title: str,
description: str,
value: str = "",
verb_cancel: str | None = None,
) -> LayoutObj[UiResult]:
"""Warning modal that makes it easier to cancel than to continue."""
# rust/src/ui/model_mercury/layout.rs # rust/src/ui/model_mercury/layout.rs
def show_success( def show_success(
*, *,
@ -510,17 +521,6 @@ def flow_get_address(
"""Get address / receive funds.""" """Get address / receive funds."""
# rust/src/ui/model_mercury/layout.rs
def flow_warning_hi_prio(
*,
title: str,
description: str,
value: str = "",
verb_cancel: str | None = None,
) -> LayoutObj[UiResult]:
"""Warning modal with multiple steps to confirm."""
# rust/src/ui/model_mercury/layout.rs # rust/src/ui/model_mercury/layout.rs
def flow_confirm_output( def flow_confirm_output(
*, *,

View File

@ -144,12 +144,14 @@ async def require_confirm_claim(
async def require_confirm_unknown_token(address_bytes: bytes) -> None: async def require_confirm_unknown_token(address_bytes: bytes) -> None:
from ubinascii import hexlify from ubinascii import hexlify
from trezor.ui.layouts import ( from trezor.ui.layouts import confirm_address, show_danger
confirm_address,
confirm_ethereum_unknown_contract_warning,
)
await confirm_ethereum_unknown_contract_warning() await show_danger(
"unknown_contract_warning",
TR.ethereum__unknown_contract_address,
TR.ethereum__unknown_contract_address_short,
verb_cancel=TR.send__cancel_sign,
)
contract_address_hex = "0x" + hexlify(address_bytes).decode() contract_address_hex = "0x" + hexlify(address_bytes).decode()
await confirm_address( await confirm_address(

View File

@ -119,23 +119,19 @@ def confirm_path_warning(
if not path_type if not path_type
else f"{TR.words__unknown} {path_type.lower()}." else f"{TR.words__unknown} {path_type.lower()}."
) )
return raise_if_not_confirmed( return show_danger(
trezorui2.flow_warning_hi_prio(
title=f"{TR.words__warning}!", description=description, value=path
),
"path_warning", "path_warning",
description,
value=path,
br_code=ButtonRequestType.UnknownDerivationPath, br_code=ButtonRequestType.UnknownDerivationPath,
) )
def confirm_multisig_warning() -> Awaitable[None]: def confirm_multisig_warning() -> Awaitable[None]:
return raise_if_not_confirmed( return show_danger(
trezorui2.flow_warning_hi_prio(
title=f"{TR.words__important}!",
description=TR.send__receiving_to_multisig,
),
"warning_multisig", "warning_multisig",
br_code=ButtonRequestType.Warning, TR.send__receiving_to_multisig,
title=f"{TR.words__important}!",
) )
@ -316,6 +312,29 @@ def show_warning(
) )
def show_danger(
br_name: str,
content: str,
content_short: str | None = None,
value: str | None = None,
title: str | None = None,
verb_cancel: str | None = None,
br_code: ButtonRequestType = ButtonRequestType.Warning,
) -> Awaitable[None]:
title = title or TR.words__warning
verb_cancel = verb_cancel or TR.buttons__cancel
return raise_if_not_confirmed(
trezorui2.show_danger(
title=title,
description=content,
value=(value or ""),
verb_cancel=verb_cancel,
),
br_name,
br_code,
)
def show_success( def show_success(
br_name: str, br_name: str,
content: str, content: str,
@ -712,17 +731,6 @@ def _confirm_summary(
if not utils.BITCOIN_ONLY: if not utils.BITCOIN_ONLY:
def confirm_ethereum_unknown_contract_warning() -> Awaitable[None]:
return raise_if_not_confirmed(
trezorui2.flow_warning_hi_prio(
title=TR.words__warning,
description=TR.ethereum__unknown_contract_address,
verb_cancel=TR.send__cancel_sign,
),
"unknown_contract_warning",
ButtonRequestType.Warning,
)
async def confirm_ethereum_tx( async def confirm_ethereum_tx(
recipient: str | None, recipient: str | None,
total_amount: str, total_amount: str,

View File

@ -412,6 +412,27 @@ def show_warning(
) )
def show_danger(
br_name: str,
content: str,
content_short: str | None = None,
value: str | None = None,
title: str | None = None,
verb_cancel: str | None = None,
br_code: ButtonRequestType = ButtonRequestType.Warning,
) -> Awaitable[ui.UiResult]:
title = title or TR.words__warning
if content_short is None:
content_short = content
return show_warning(
br_name,
title,
content_short,
TR.words__continue_anyway,
br_code=br_code,
)
def show_success( def show_success(
br_name: str, br_name: str,
content: str, content: str,
@ -556,7 +577,6 @@ def confirm_blob(
title: str, title: str,
data: bytes | str, data: bytes | str,
description: str | None = None, description: str | None = None,
text_mono: bool = True,
subtitle: str | None = None, subtitle: str | None = None,
verb: str | None = None, verb: str | None = None,
verb_cancel: str | None = None, # icon verb_cancel: str | None = None, # icon
@ -806,14 +826,6 @@ def confirm_total(
if not utils.BITCOIN_ONLY: if not utils.BITCOIN_ONLY:
def confirm_ethereum_unknown_contract_warning() -> Awaitable[ui.UiResult]:
return show_warning(
"unknown_contract_warning",
TR.words__warning,
TR.ethereum__unknown_contract_address_short,
TR.words__continue_anyway,
)
async def confirm_ethereum_staking_tx( async def confirm_ethereum_staking_tx(
title: str, title: str,
intro_question: str, intro_question: str,

View File

@ -364,6 +364,25 @@ def show_warning(
) )
def show_danger(
br_name: str,
content: str,
content_short: str | None = None,
value: str | None = None,
title: str | None = None,
verb_cancel: str | None = None,
br_code: ButtonRequestType = ButtonRequestType.Warning,
) -> Awaitable[None]:
if content_short is None:
content_short = content
return show_warning(
br_name,
content_short,
TR.words__continue_anyway_question,
br_code=br_code,
)
def show_success( def show_success(
br_name: str, br_name: str,
content: str, content: str,
@ -770,13 +789,6 @@ def _confirm_summary(
if not utils.BITCOIN_ONLY: if not utils.BITCOIN_ONLY:
def confirm_ethereum_unknown_contract_warning() -> Awaitable[None]:
return show_warning(
"unknown_contract_warning",
TR.ethereum__unknown_contract_address_short,
TR.words__continue_anyway_question,
)
async def confirm_ethereum_tx( async def confirm_ethereum_tx(
recipient: str | None, recipient: str | None,
total_amount: str, total_amount: str,