From e82988665ccb4134d8c523a0514eef9e9b14ce82 Mon Sep 17 00:00:00 2001 From: obrusvit Date: Thu, 31 Oct 2024 13:29:32 +0100 Subject: [PATCH] refactor(core): move set_brightness to UiFeatures - this is a first commit where a trait function is not implemented for particular model, namely `set_brightness` for `model_r` --- core/embed/rust/src/ui/api/firmware_upy.rs | 28 ++++++++++++++++++- .../embed/rust/src/ui/model_mercury/layout.rs | 16 ----------- .../src/ui/model_mercury/ui_features_fw.rs | 7 +++++ .../rust/src/ui/model_tr/ui_features_fw.rs | 6 ++++ .../ui/model_tt/component/set_brightness.rs | 3 +- core/embed/rust/src/ui/model_tt/layout.rs | 20 ------------- .../rust/src/ui/model_tt/ui_features_fw.rs | 14 +++++++++- core/embed/rust/src/ui/ui_features_fw.rs | 2 ++ core/mocks/generated/trezorui2.pyi | 16 ----------- core/mocks/generated/trezorui_api.pyi | 8 ++++++ .../src/trezor/ui/layouts/mercury/__init__.py | 2 +- core/src/trezor/ui/layouts/tt/__init__.py | 2 +- 12 files changed, 66 insertions(+), 58 deletions(-) diff --git a/core/embed/rust/src/ui/api/firmware_upy.rs b/core/embed/rust/src/ui/api/firmware_upy.rs index 2e4e1a15b9..d0ac07ee57 100644 --- a/core/embed/rust/src/ui/api/firmware_upy.rs +++ b/core/embed/rust/src/ui/api/firmware_upy.rs @@ -12,9 +12,10 @@ use crate::{ trezorhal::model, ui::{ backlight::BACKLIGHT_LEVELS_OBJ, + component::Empty, layout::{ base::LAYOUT_STATE, - obj::{LayoutObj, ATTACH_TYPE_OBJ}, + obj::{ComponentMsgObj, LayoutObj, ATTACH_TYPE_OBJ}, result::{CANCELLED, CONFIRMED, INFO}, util::{upy_disable_animation, RecoveryType}, }, @@ -23,6 +24,14 @@ use crate::{ }, }; +/// Dummy implementation so that we can use `Empty` in a return type of unimplemented trait +/// function +impl ComponentMsgObj for Empty { + fn msg_try_into_obj(&self, _msg: Self::Msg) -> Result { + Ok(Obj::const_none()) + } +} + // free-standing functions exported to MicroPython mirror `trait // UIFeaturesFirmware` // NOTE: `disable_animation` not a part of trait UiFeaturesFirmware @@ -172,6 +181,16 @@ extern "C" fn new_select_word_count(n_args: usize, args: *const Obj, kwargs: *mu unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } } +extern "C" fn new_set_brightness(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj { + let block = move |_args: &[Obj], kwargs: &Map| { + let current: Option = kwargs.get(Qstr::MP_QSTR_current)?.try_into_option()?; + + let layout = ModelUI::set_brightness(current)?; + Ok(LayoutObj::new_root(layout)?.into()) + }; + unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } +} + extern "C" fn new_show_homescreen(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj { let block = move |_args: &[Obj], kwargs: &Map| { let label: TString<'static> = kwargs @@ -429,6 +448,13 @@ pub static mp_module_trezorui_api: Module = obj_module! { /// For unlocking a repeated backup, select from 20 or 33.""" Qstr::MP_QSTR_select_word_count => obj_fn_kw!(0, new_select_word_count).as_obj(), + /// def set_brightness( + /// *, + /// current: int | None = None + /// ) -> LayoutObj[UiResult]: + /// """Show the brightness configuration dialog.""" + Qstr::MP_QSTR_set_brightness => obj_fn_kw!(0, new_set_brightness).as_obj(), + /// def show_homescreen( /// *, /// label: str | None, diff --git a/core/embed/rust/src/ui/model_mercury/layout.rs b/core/embed/rust/src/ui/model_mercury/layout.rs index 9499641f69..d3e2de7c7f 100644 --- a/core/embed/rust/src/ui/model_mercury/layout.rs +++ b/core/embed/rust/src/ui/model_mercury/layout.rs @@ -572,15 +572,6 @@ extern "C" fn new_confirm_summary(n_args: usize, args: *const Obj, kwargs: *mut unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } } -extern "C" fn new_set_brightness(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj { - let block = move |_args: &[Obj], kwargs: &Map| { - let current: u8 = kwargs.get(Qstr::MP_QSTR_current)?.try_into()?; - let flow = flow::set_brightness::new_set_brightness(current)?; - Ok(LayoutObj::new_root(flow)?.into()) - }; - unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } -} - extern "C" fn new_show_info_with_cancel(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()?; @@ -1501,13 +1492,6 @@ pub static mp_module_trezorui2: Module = obj_module! { /// info.""" Qstr::MP_QSTR_flow_request_number => obj_fn_kw!(0, new_request_number).as_obj(), - /// def set_brightness( - /// *, - /// current: int | None = None - /// ) -> LayoutObj[UiResult]: - /// """Show the brightness configuration dialog.""" - Qstr::MP_QSTR_set_brightness => obj_fn_kw!(0, new_set_brightness).as_obj(), - /// def show_checklist( /// *, /// title: str, diff --git a/core/embed/rust/src/ui/model_mercury/ui_features_fw.rs b/core/embed/rust/src/ui/model_mercury/ui_features_fw.rs index d8d51bc463..453b14ccd5 100644 --- a/core/embed/rust/src/ui/model_mercury/ui_features_fw.rs +++ b/core/embed/rust/src/ui/model_mercury/ui_features_fw.rs @@ -179,6 +179,13 @@ impl UIFeaturesFirmware for ModelMercuryFeatures { Ok(layout) } + fn set_brightness(current_brightness: Option) -> Result { + let flow = flow::set_brightness::new_set_brightness( + current_brightness.unwrap_or(theme::backlight::get_backlight_normal()), + )?; + Ok(flow) + } + fn show_homescreen( label: TString<'static>, hold: bool, diff --git a/core/embed/rust/src/ui/model_tr/ui_features_fw.rs b/core/embed/rust/src/ui/model_tr/ui_features_fw.rs index a65f3e9b55..b34008af5a 100644 --- a/core/embed/rust/src/ui/model_tr/ui_features_fw.rs +++ b/core/embed/rust/src/ui/model_tr/ui_features_fw.rs @@ -197,6 +197,12 @@ impl UIFeaturesFirmware for ModelTRFeatures { Ok(layout) } + fn set_brightness(current_brightness: Option) -> Result { + Err::, Error>(Error::ValueError( + c"setting brightness not supported", + )) + } + fn show_homescreen( label: TString<'static>, hold: bool, diff --git a/core/embed/rust/src/ui/model_tt/component/set_brightness.rs b/core/embed/rust/src/ui/model_tt/component/set_brightness.rs index a19ccc49f9..8222666fce 100644 --- a/core/embed/rust/src/ui/model_tt/component/set_brightness.rs +++ b/core/embed/rust/src/ui/model_tt/component/set_brightness.rs @@ -17,8 +17,7 @@ use super::{ pub struct SetBrightnessDialog(NumberInputSliderDialog); impl SetBrightnessDialog { - pub fn new(current: Option) -> Self { - let current = current.unwrap_or(theme::backlight::get_backlight_normal()); + pub fn new(current: u8) -> Self { Self(NumberInputSliderDialog::new( theme::backlight::get_backlight_min() as u16, theme::backlight::get_backlight_max() as u16, diff --git a/core/embed/rust/src/ui/model_tt/layout.rs b/core/embed/rust/src/ui/model_tt/layout.rs index 10567198e1..45865f11fb 100644 --- a/core/embed/rust/src/ui/model_tt/layout.rs +++ b/core/embed/rust/src/ui/model_tt/layout.rs @@ -1172,19 +1172,6 @@ extern "C" fn new_request_number(n_args: usize, args: *const Obj, kwargs: *mut M unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } } -extern "C" fn new_set_brightness(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj { - let block = move |_args: &[Obj], kwargs: &Map| { - let current: Option = kwargs.get(Qstr::MP_QSTR_current)?.try_into_option()?; - let obj = LayoutObj::new(Frame::centered( - theme::label_title(), - TR::brightness__title.into(), - SetBrightnessDialog::new(current), - ))?; - Ok(obj.into()) - }; - unsafe { util::try_with_args_and_kwargs(n_args, args, kwargs, block) } -} - extern "C" fn new_show_checklist(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()?; @@ -1618,13 +1605,6 @@ pub static mp_module_trezorui2: Module = obj_module! { /// """Number input with + and - buttons, description, and info button.""" Qstr::MP_QSTR_request_number => obj_fn_kw!(0, new_request_number).as_obj(), - /// def set_brightness( - /// *, - /// current: int | None = None - /// ) -> LayoutObj[UiResult]: - /// """Show the brightness configuration dialog.""" - Qstr::MP_QSTR_set_brightness => obj_fn_kw!(0, new_set_brightness).as_obj(), - /// def show_checklist( /// *, /// title: str, diff --git a/core/embed/rust/src/ui/model_tt/ui_features_fw.rs b/core/embed/rust/src/ui/model_tt/ui_features_fw.rs index 9c1e2ac4d4..0dccc576a9 100644 --- a/core/embed/rust/src/ui/model_tt/ui_features_fw.rs +++ b/core/embed/rust/src/ui/model_tt/ui_features_fw.rs @@ -22,7 +22,7 @@ use super::{ component::{ check_homescreen_format, Bip39Input, Button, ButtonMsg, ButtonPage, ButtonStyleSheet, CancelConfirmMsg, Dialog, Frame, Homescreen, IconDialog, Lockscreen, MnemonicKeyboard, - PassphraseKeyboard, PinKeyboard, SelectWordCount, Slip39Input, + PassphraseKeyboard, PinKeyboard, SelectWordCount, SetBrightnessDialog, Slip39Input, }, theme, ModelTTFeatures, }; @@ -209,6 +209,18 @@ impl UIFeaturesFirmware for ModelTTFeatures { Ok(layout) } + fn set_brightness(current_brightness: Option) -> Result { + let layout = RootComponent::new(Frame::centered( + theme::label_title(), + TR::brightness__title.into(), + SetBrightnessDialog::new( + current_brightness.unwrap_or(theme::backlight::get_backlight_normal()), + ), + )); + + Ok(layout) + } + fn show_homescreen( label: TString<'static>, hold: bool, diff --git a/core/embed/rust/src/ui/ui_features_fw.rs b/core/embed/rust/src/ui/ui_features_fw.rs index 1aa486db85..9a8d4f6d5a 100644 --- a/core/embed/rust/src/ui/ui_features_fw.rs +++ b/core/embed/rust/src/ui/ui_features_fw.rs @@ -64,6 +64,8 @@ pub trait UIFeaturesFirmware { fn select_word_count(recovery_type: RecoveryType) -> Result; + fn set_brightness(current_brightness: Option) -> Result; + fn show_homescreen( label: TString<'static>, hold: bool, diff --git a/core/mocks/generated/trezorui2.pyi b/core/mocks/generated/trezorui2.pyi index 5c5bef1e61..34f10ef34d 100644 --- a/core/mocks/generated/trezorui2.pyi +++ b/core/mocks/generated/trezorui2.pyi @@ -268,14 +268,6 @@ def flow_request_number( info.""" -# rust/src/ui/model_mercury/layout.rs -def set_brightness( - *, - current: int | None = None -) -> LayoutObj[UiResult]: - """Show the brightness configuration dialog.""" - - # rust/src/ui/model_mercury/layout.rs def show_checklist( *, @@ -1003,14 +995,6 @@ def request_number( """Number input with + and - buttons, description, and info button.""" -# rust/src/ui/model_tt/layout.rs -def set_brightness( - *, - current: int | None = None -) -> LayoutObj[UiResult]: - """Show the brightness configuration dialog.""" - - # rust/src/ui/model_tt/layout.rs def show_checklist( *, diff --git a/core/mocks/generated/trezorui_api.pyi b/core/mocks/generated/trezorui_api.pyi index 30c28dbe82..8fa9ec0f79 100644 --- a/core/mocks/generated/trezorui_api.pyi +++ b/core/mocks/generated/trezorui_api.pyi @@ -174,6 +174,14 @@ def select_word_count( For unlocking a repeated backup, select from 20 or 33.""" +# rust/src/ui/api/firmware_upy.rs +def set_brightness( + *, + current: int | None = None +) -> LayoutObj[UiResult]: + """Show the brightness configuration dialog.""" + + # rust/src/ui/api/firmware_upy.rs def show_homescreen( *, diff --git a/core/src/trezor/ui/layouts/mercury/__init__.py b/core/src/trezor/ui/layouts/mercury/__init__.py index e5a4b0ca20..abd1985d1d 100644 --- a/core/src/trezor/ui/layouts/mercury/__init__.py +++ b/core/src/trezor/ui/layouts/mercury/__init__.py @@ -1193,7 +1193,7 @@ def confirm_firmware_update(description: str, fingerprint: str) -> Awaitable[Non def set_brightness(current: int | None = None) -> Awaitable[None]: return raise_if_not_confirmed( - trezorui2.set_brightness(current=current), + trezorui_api.set_brightness(current=current), "set_brightness", BR_CODE_OTHER, ) diff --git a/core/src/trezor/ui/layouts/tt/__init__.py b/core/src/trezor/ui/layouts/tt/__init__.py index 4f6c4be577..3d9aba3e49 100644 --- a/core/src/trezor/ui/layouts/tt/__init__.py +++ b/core/src/trezor/ui/layouts/tt/__init__.py @@ -1262,7 +1262,7 @@ def confirm_firmware_update(description: str, fingerprint: str) -> Awaitable[Non async def set_brightness(current: int | None = None) -> None: await interact( - trezorui2.set_brightness(current=current), + trezorui_api.set_brightness(current=current), "set_brightness", BR_CODE_OTHER, )