From 65178a6a36d0cf46df8f08c056aec1ec930a71e9 Mon Sep 17 00:00:00 2001 From: matejcik Date: Wed, 7 Feb 2024 10:36:25 +0100 Subject: [PATCH] refactor(core/rust): reuse the same component for "waiting for host" screen --- core/embed/rust/src/ui/component/connect.rs | 79 +++++++++++++++++++ core/embed/rust/src/ui/component/mod.rs | 1 + .../src/ui/model_tr/bootloader/connect.rs | 47 ----------- .../rust/src/ui/model_tr/bootloader/mod.rs | 45 +++++------ .../src/ui/model_tt/bootloader/connect.rs | 52 ------------ .../rust/src/ui/model_tt/bootloader/mod.rs | 5 +- 6 files changed, 103 insertions(+), 126 deletions(-) create mode 100644 core/embed/rust/src/ui/component/connect.rs delete mode 100644 core/embed/rust/src/ui/model_tr/bootloader/connect.rs delete mode 100644 core/embed/rust/src/ui/model_tt/bootloader/connect.rs diff --git a/core/embed/rust/src/ui/component/connect.rs b/core/embed/rust/src/ui/component/connect.rs new file mode 100644 index 0000000000..5faa389242 --- /dev/null +++ b/core/embed/rust/src/ui/component/connect.rs @@ -0,0 +1,79 @@ +use crate::{ + strutil::TString, + ui::{ + component::{Component, Event, EventCtx, Never, Pad}, + display::{self, Color, Font}, + geometry::{Offset, Rect}, + }, +}; + +pub struct Connect { + fg: Color, + bg: Pad, + message: TString<'static>, +} + +impl Connect { + pub fn new(message: T, fg: Color, bg: Color) -> Self + where + T: Into>, + { + let mut instance = Self { + fg, + bg: Pad::with_background(bg), + message: message.into(), + }; + + instance.bg.clear(); + instance + } +} + +impl Component for Connect { + type Msg = Never; + + fn place(&mut self, bounds: Rect) -> Rect { + self.bg.place(bounds); + bounds + } + + fn event(&mut self, _ctx: &mut EventCtx, _event: Event) -> Option { + None + } + + fn paint(&mut self) { + let font = Font::NORMAL; + + self.bg.paint(); + self.message.map(|t| { + display::text_center( + self.bg.area.center() + Offset::y(font.text_height() / 2), + t, + font, + self.fg, + self.bg.color, + ) + }); + } +} + +#[cfg(feature = "micropython")] +mod micropython { + use crate::{error::Error, micropython::obj::Obj, ui::layout::obj::ComponentMsgObj}; + + use super::Connect; + + impl ComponentMsgObj for Connect { + fn msg_try_into_obj(&self, _msg: Self::Msg) -> Result { + unreachable!() + } + } +} + +#[cfg(feature = "ui_debug")] +impl crate::trace::Trace for Connect { + fn trace(&self, t: &mut dyn crate::trace::Tracer) { + t.component("Connect"); + t.string("message", self.message); + } +} diff --git a/core/embed/rust/src/ui/component/mod.rs b/core/embed/rust/src/ui/component/mod.rs index ae4c4d5632..bc54734d9c 100644 --- a/core/embed/rust/src/ui/component/mod.rs +++ b/core/embed/rust/src/ui/component/mod.rs @@ -2,6 +2,7 @@ pub mod base; pub mod border; +pub mod connect; pub mod empty; pub mod image; pub mod label; diff --git a/core/embed/rust/src/ui/model_tr/bootloader/connect.rs b/core/embed/rust/src/ui/model_tr/bootloader/connect.rs deleted file mode 100644 index ba2e5701ec..0000000000 --- a/core/embed/rust/src/ui/model_tr/bootloader/connect.rs +++ /dev/null @@ -1,47 +0,0 @@ -use crate::ui::{ - component::{Component, Event, EventCtx, Never, Pad}, - display::{self, Font}, - geometry::{Offset, Rect}, -}; - -use super::super::theme::bootloader::{BLD_BG, BLD_FG}; - -pub struct Connect { - bg: Pad, - message: &'static str, -} - -impl Connect { - pub fn new(message: &'static str) -> Self { - Self { - bg: Pad::with_background(BLD_BG).with_clear(), - message, - } - } -} - -impl Component for Connect { - type Msg = Never; - - fn place(&mut self, bounds: Rect) -> Rect { - self.bg.place(bounds); - bounds - } - - fn event(&mut self, _ctx: &mut EventCtx, _event: Event) -> Option { - None - } - - fn paint(&mut self) { - let font = Font::NORMAL; - - self.bg.paint(); - display::text_center( - self.bg.area.center() + Offset::y(font.text_height() / 2), - self.message, - font, - BLD_FG, - BLD_BG, - ); - } -} diff --git a/core/embed/rust/src/ui/model_tr/bootloader/mod.rs b/core/embed/rust/src/ui/model_tr/bootloader/mod.rs index 3f803800cc..4d8c5c7821 100644 --- a/core/embed/rust/src/ui/model_tr/bootloader/mod.rs +++ b/core/embed/rust/src/ui/model_tr/bootloader/mod.rs @@ -1,40 +1,37 @@ +use heapless::String; + use crate::{ strutil::hexlify, - trezorhal::io::io_button_read, + trezorhal::{io::io_button_read, secbool::secbool}, ui::{ - component::{Component, Event, EventCtx, Label, LineBreaking::BreakWordsNoHyphen, Never}, - constant::SCREEN, + component::{ + connect::Connect, Component, Event, EventCtx, Label, LineBreaking::BreakWordsNoHyphen, + Never, + }, + constant, + constant::{HEIGHT, SCREEN}, display::{self, Color, Font, Icon}, event::ButtonEvent, - geometry::{Alignment2D, Offset, Rect}, + geometry::{Alignment2D, Offset, Point, Rect}, util::{from_c_array, from_c_str}, }, }; -use heapless::String; -use super::component::{ResultScreen, WelcomeScreen}; +use super::{ + component::{ + bl_confirm::{Confirm, ConfirmMsg}, + ResultScreen, WelcomeScreen, + }, + theme::{ + bootloader::{BLD_BG, BLD_FG, ICON_ALERT, ICON_SPINNER, ICON_SUCCESS}, + ICON_ARM_LEFT, ICON_ARM_RIGHT, TEXT_BOLD, TEXT_NORMAL, WHITE, + }, +}; -mod connect; mod intro; mod menu; mod welcome; -use crate::{ - trezorhal::secbool::secbool, - ui::{ - constant, - constant::HEIGHT, - geometry::Point, - model_tr::{ - component::bl_confirm::{Confirm, ConfirmMsg}, - theme::{ - bootloader::{BLD_BG, BLD_FG, ICON_ALERT, ICON_SPINNER, ICON_SUCCESS}, - ICON_ARM_LEFT, ICON_ARM_RIGHT, TEXT_BOLD, TEXT_NORMAL, WHITE, - }, - }, - }, -}; -use connect::Connect; use intro::Intro; use menu::Menu; use welcome::Welcome; @@ -301,7 +298,7 @@ extern "C" fn screen_wipe_progress(progress: u16, initialize: bool) { #[no_mangle] extern "C" fn screen_connect(_initial_setup: bool) { - let mut frame = Connect::new("Waiting for host..."); + let mut frame = Connect::new("Waiting for host...", BLD_FG, BLD_BG); show(&mut frame); } diff --git a/core/embed/rust/src/ui/model_tt/bootloader/connect.rs b/core/embed/rust/src/ui/model_tt/bootloader/connect.rs deleted file mode 100644 index 164f0ed43d..0000000000 --- a/core/embed/rust/src/ui/model_tt/bootloader/connect.rs +++ /dev/null @@ -1,52 +0,0 @@ -use crate::ui::{ - component::{Component, Event, EventCtx, Never, Pad}, - constant::screen, - display::{self, Color, Font}, - geometry::{Offset, Rect}, - model_tt::theme::bootloader::BLD_TITLE_COLOR, -}; - -pub struct Connect { - fg: Color, - bg: Pad, - message: &'static str, -} - -impl Connect { - pub fn new(message: &'static str, fg: Color, bg: Color) -> Self { - let mut instance = Self { - fg, - bg: Pad::with_background(bg), - message, - }; - - instance.bg.clear(); - instance - } -} - -impl Component for Connect { - type Msg = Never; - - fn place(&mut self, bounds: Rect) -> Rect { - self.bg.place(screen()); - bounds - } - - fn event(&mut self, _ctx: &mut EventCtx, _event: Event) -> Option { - None - } - - fn paint(&mut self) { - let font = Font::NORMAL; - - self.bg.paint(); - display::text_center( - screen().center() + Offset::y(font.text_height() / 2), - self.message, - Font::NORMAL, - BLD_TITLE_COLOR, - self.bg.color, - ); - } -} diff --git a/core/embed/rust/src/ui/model_tt/bootloader/mod.rs b/core/embed/rust/src/ui/model_tt/bootloader/mod.rs index 2d87d4ad16..af5422bc81 100644 --- a/core/embed/rust/src/ui/model_tt/bootloader/mod.rs +++ b/core/embed/rust/src/ui/model_tt/bootloader/mod.rs @@ -5,7 +5,7 @@ use crate::{ strutil::hexlify, trezorhal::{io::io_touch_read, secbool::secbool}, ui::{ - component::{Component, Event, EventCtx, Label, Never}, + component::{connect::Connect, Component, Event, EventCtx, Label, Never}, constant::{screen, HEIGHT}, display::{self, Color, Font, Icon}, event::TouchEvent, @@ -15,7 +15,7 @@ use crate::{ }; use super::{ - bootloader::{connect::Connect, welcome::Welcome}, + bootloader::welcome::Welcome, component::{ bl_confirm::{Confirm, ConfirmTitle}, Button, ResultScreen, WelcomeScreen, @@ -35,7 +35,6 @@ use super::{ use intro::Intro; use menu::Menu; -mod connect; pub mod intro; pub mod menu; pub mod welcome;