1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-05-22 08:48:46 +00:00

refactor(core/ui): parametrize show_pairing_code

This commit is contained in:
Martin Milata 2025-04-25 00:37:36 +02:00
parent 5b2e660f14
commit fc33f8dbc2
8 changed files with 61 additions and 14 deletions

View File

@ -844,8 +844,11 @@ extern "C" fn new_show_pairing_device_name(
extern "C" fn new_show_pairing_code(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj {
let block = move |_args: &[Obj], kwargs: &Map| {
let title: TString = kwargs.get(Qstr::MP_QSTR_title)?.try_into()?;
let description: TString = kwargs.get(Qstr::MP_QSTR_description)?.try_into()?;
let code: TString = kwargs.get(Qstr::MP_QSTR_code)?.try_into()?;
let layout = ModelUI::show_pairing_code(code)?;
let button: bool = kwargs.get_or(Qstr::MP_QSTR_button, true)?;
let layout = ModelUI::show_pairing_code(title, description, code, button)?;
let layout_obj = LayoutObj::new_root(layout)?;
Ok(layout_obj.into())
};
@ -1633,7 +1636,10 @@ pub static mp_module_trezorui_api: Module = obj_module! {
/// def show_pairing_code(
/// *,
/// title: str,
/// description: str,
/// code: str,
/// button: bool = True,
/// ) -> LayoutObj[UiResult]:
/// """Pairing device: second screen (pairing code).
/// Returns on BLEEvent::{PairingCanceled, Disconnected}."""

View File

@ -882,10 +882,25 @@ impl FirmwareUI for UIBolt {
))
}
fn show_pairing_code(_code: TString<'static>) -> Result<impl LayoutMaybeTrace, Error> {
Err::<RootComponent<Empty, ModelUI>, Error>(Error::ValueError(
c"show_pairing_code not supported",
))
fn show_pairing_code(
title: TString<'static>,
description: TString<'static>,
code: TString<'static>,
button: bool,
) -> Result<impl LayoutMaybeTrace, Error> {
Self::confirm_action(
title,
Some(code),
Some(description),
None,
button.then_some(TR::buttons__confirm.into()),
None,
false,
false,
false,
false,
None,
)
}
fn show_info(

View File

@ -1048,7 +1048,12 @@ impl FirmwareUI for UICaesar {
))
}
fn show_pairing_code(_code: TString<'static>) -> Result<impl LayoutMaybeTrace, Error> {
fn show_pairing_code(
title: TString<'static>,
description: TString<'static>,
code: TString<'static>,
button: bool,
) -> Result<impl LayoutMaybeTrace, Error> {
Err::<RootComponent<Empty, ModelUI>, Error>(Error::ValueError(
c"show_pairing_code not supported",
))

View File

@ -905,7 +905,12 @@ impl FirmwareUI for UIDelizia {
))
}
fn show_pairing_code(_code: TString<'static>) -> Result<impl LayoutMaybeTrace, Error> {
fn show_pairing_code(
title: TString<'static>,
description: TString<'static>,
code: TString<'static>,
button: bool,
) -> Result<impl LayoutMaybeTrace, Error> {
Err::<RootComponent<Empty, ModelUI>, Error>(Error::ValueError(
c"show_pairing_code not supported",
))

View File

@ -1010,16 +1010,21 @@ impl FirmwareUI for UIEckhart {
Ok(layout)
}
fn show_pairing_code(code: TString<'static>) -> Result<impl LayoutMaybeTrace, Error> {
let text: TString<'static> = "Pairing code match?".into();
fn show_pairing_code(
title: TString<'static>,
description: TString<'static>,
code: TString<'static>,
button: bool,
) -> Result<impl LayoutMaybeTrace, Error> {
let mut ops = OpTextLayout::new(theme::firmware::TEXT_REGULAR);
ops = ops.text(text, fonts::FONT_SATOSHI_REGULAR_38);
ops = ops.text(description, fonts::FONT_SATOSHI_REGULAR_38);
ops = ops.newline().newline().newline();
ops = ops.alignment(Alignment::Center);
ops = ops.text(code, fonts::FONT_SATOSHI_EXTRALIGHT_72);
let screen = TextScreen::new(FormattedText::new(ops))
.with_header(Header::new("Bluetooth pairing".into()))
.with_action_bar(ActionBar::new_cancel_confirm());
let mut screen = TextScreen::new(FormattedText::new(ops)).with_header(Header::new(title));
if button {
screen = screen.with_action_bar(ActionBar::new_cancel_confirm());
}
#[cfg(feature = "ble")]
let screen = crate::ui::component::BLEHandler::new(screen, false);
let layout = RootComponent::new(screen);

View File

@ -316,7 +316,12 @@ pub trait FirmwareUI {
device_name: TString<'static>,
) -> Result<impl LayoutMaybeTrace, Error>;
fn show_pairing_code(code: TString<'static>) -> Result<impl LayoutMaybeTrace, Error>;
fn show_pairing_code(
title: TString<'static>,
description: TString<'static>,
code: TString<'static>,
button: bool,
) -> Result<impl LayoutMaybeTrace, Error>;
fn show_info(
title: TString<'static>,

View File

@ -557,7 +557,10 @@ def show_pairing_device_name(
# rust/src/ui/api/firmware_micropython.rs
def show_pairing_code(
*,
title: str,
description: str,
code: str,
button: bool = True,
) -> LayoutObj[UiResult]:
"""Pairing device: second screen (pairing code).
Returns on BLEEvent::{PairingCanceled, Disconnected}."""

View File

@ -29,7 +29,10 @@ async def pair_new_device() -> None:
try:
result = await raise_if_not_confirmed(
trezorui_api.show_pairing_code(
title="Bluetooth pairing",
description="Pairing code match?",
code=f"{code:0>6}",
button=True,
),
None,
)