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

refactor(core/rust): T1 title bar

[no changelog]
This commit is contained in:
Martin Milata 2021-12-07 21:05:32 +01:00 committed by matejcik
parent 7f224ab36d
commit 19b2358084
4 changed files with 104 additions and 37 deletions

View File

@ -4,8 +4,7 @@ use super::{
}; };
use crate::ui::{ use crate::ui::{
component::{Child, Component, Event, EventCtx}, component::{Child, Component, Event, EventCtx},
display, geometry::Rect,
geometry::{Offset, Rect},
}; };
pub enum DialogMsg<T> { pub enum DialogMsg<T> {
@ -15,7 +14,6 @@ pub enum DialogMsg<T> {
} }
pub struct Dialog<T, U> { pub struct Dialog<T, U> {
header: Option<(U, Rect)>,
content: Child<T>, content: Child<T>,
left_btn: Option<Child<Button<U>>>, left_btn: Option<Child<Button<U>>>,
right_btn: Option<Child<Button<U>>>, right_btn: Option<Child<Button<U>>>,
@ -27,46 +25,22 @@ impl<T: Component, U: AsRef<[u8]>> Dialog<T, U> {
content: impl FnOnce(Rect) -> T, content: impl FnOnce(Rect) -> T,
left: Option<impl FnOnce(Rect, ButtonPos) -> Button<U>>, left: Option<impl FnOnce(Rect, ButtonPos) -> Button<U>>,
right: Option<impl FnOnce(Rect, ButtonPos) -> Button<U>>, right: Option<impl FnOnce(Rect, ButtonPos) -> Button<U>>,
header: Option<U>,
) -> Self { ) -> Self {
let (header_area, content_area, button_area) = Self::areas(area, &header); let (content_area, button_area) = Self::areas(area);
let content = Child::new(content(content_area)); let content = Child::new(content(content_area));
let left_btn = left.map(|f| Child::new(f(button_area, ButtonPos::Left))); let left_btn = left.map(|f| Child::new(f(button_area, ButtonPos::Left)));
let right_btn = right.map(|f| Child::new(f(button_area, ButtonPos::Right))); let right_btn = right.map(|f| Child::new(f(button_area, ButtonPos::Right)));
Self { Self {
header: header.zip(header_area),
content, content,
left_btn, left_btn,
right_btn, right_btn,
} }
} }
fn paint_header(&self) { fn areas(area: Rect) -> (Rect, Rect) {
if let Some((title, area)) = &self.header {
display::text(
area.bottom_left() + Offset::new(0, -2),
title.as_ref(),
theme::FONT_BOLD,
theme::FG,
theme::BG,
);
display::dotted_line(area.bottom_left(), area.width(), theme::FG)
}
}
fn areas(area: Rect, header: &Option<U>) -> (Option<Rect>, Rect, Rect) {
const HEADER_SPACE: i32 = 4;
let button_height = theme::FONT_BOLD.line_height() + 2; let button_height = theme::FONT_BOLD.line_height() + 2;
let header_height = theme::FONT_BOLD.line_height();
let (content_area, button_area) = area.hsplit(-button_height); let (content_area, button_area) = area.hsplit(-button_height);
if header.is_none() { (content_area, button_area)
(None, content_area, button_area)
} else {
let (header_area, content_area) = content_area.hsplit(header_height);
let (_space, content_area) = content_area.hsplit(HEADER_SPACE);
(Some(header_area), content_area, button_area)
}
} }
} }
@ -86,7 +60,6 @@ impl<T: Component, U: AsRef<[u8]>> Component for Dialog<T, U> {
} }
fn paint(&mut self) { fn paint(&mut self) {
self.paint_header();
self.content.paint(); self.content.paint();
if let Some(b) = self.left_btn.as_mut() { if let Some(b) = self.left_btn.as_mut() {
b.paint(); b.paint();

View File

@ -1,7 +1,9 @@
mod button; mod button;
mod dialog; mod dialog;
mod title;
use super::{event, theme}; use super::{event, theme};
pub use button::{Button, ButtonContent, ButtonMsg, ButtonPos, ButtonStyle, ButtonStyleSheet}; pub use button::{Button, ButtonContent, ButtonMsg, ButtonPos, ButtonStyle, ButtonStyleSheet};
pub use dialog::{Dialog, DialogMsg}; pub use dialog::{Dialog, DialogMsg};
pub use title::Title;

View File

@ -0,0 +1,67 @@
use super::theme;
use crate::ui::{
component::{Child, Component, ComponentExt, Event, EventCtx},
display,
geometry::{Offset, Rect},
};
pub struct Title<T, U> {
area: Rect,
title: U,
content: Child<T>,
}
impl<T: Component, U: AsRef<[u8]>> Title<T, U> {
pub fn new(area: Rect, title: U, content: impl FnOnce(Rect) -> T) -> Self {
let (title_area, content_area) = Self::areas(area);
Self {
area: title_area,
title,
content: content(content_area).into_child(),
}
}
fn areas(area: Rect) -> (Rect, Rect) {
const HEADER_SPACE: i32 = 4;
let header_height = theme::FONT_BOLD.line_height();
let (header_area, content_area) = area.hsplit(header_height);
let (_space, content_area) = content_area.hsplit(HEADER_SPACE);
(header_area, content_area)
}
}
impl<T: Component, U: AsRef<[u8]>> Component for Title<T, U> {
type Msg = T::Msg;
fn event(&mut self, ctx: &mut EventCtx, event: Event) -> Option<Self::Msg> {
self.content.event(ctx, event)
}
fn paint(&mut self) {
display::text(
self.area.bottom_left() + Offset::new(0, -2),
self.title.as_ref(),
theme::FONT_BOLD,
theme::FG,
theme::BG,
);
display::dotted_line(self.area.bottom_left(), self.area.width(), theme::FG);
self.content.paint();
}
}
#[cfg(feature = "ui_debug")]
impl<T, U> crate::trace::Trace for Title<T, U>
where
T: crate::trace::Trace,
U: crate::trace::Trace + AsRef<[u8]>,
{
fn trace(&self, t: &mut dyn crate::trace::Tracer) {
t.open("Title");
t.field("title", &self.title);
t.field("content", &self.content);
t.close();
}
}

View File

@ -12,7 +12,7 @@ use crate::{
}; };
use super::{ use super::{
component::{Button, Dialog, DialogMsg}, component::{Button, Dialog, DialogMsg, Title},
theme, theme,
}; };
@ -64,13 +64,14 @@ extern "C" fn ui_layout_new_confirm_action(
let obj = LayoutObj::new(Child::new(Dialog::new( let obj = LayoutObj::new(Child::new(Dialog::new(
display::screen(), display::screen(),
|area| { |area| {
FormattedText::new::<theme::T1DefaultText>(area, format) Title::new(area, title, |area| {
.with(b"action", action.unwrap_or("".into())) FormattedText::new::<theme::T1DefaultText>(area, format)
.with(b"description", description.unwrap_or("".into())) .with(b"action", action.unwrap_or("".into()))
.with(b"description", description.unwrap_or("".into()))
})
}, },
left, left,
right, right,
Some(title),
)))?; )))?;
Ok(obj.into()) Ok(obj.into())
}; };
@ -137,7 +138,6 @@ mod tests {
}, },
Some(|area, pos| Button::with_text(area, pos, "Left", theme::button_cancel())), Some(|area, pos| Button::with_text(area, pos, "Left", theme::button_cancel())),
Some(|area, pos| Button::with_text(area, pos, "Right", theme::button_default())), Some(|area, pos| Button::with_text(area, pos, "Right", theme::button_default())),
None,
)); ));
assert_eq!( assert_eq!(
trace(&layout), trace(&layout),
@ -147,4 +147,29 @@ some more text. And p-
arameters! > left:<Button text:Left > right:<Button text:Right > >"# arameters! > left:<Button text:Left > right:<Button text:Right > >"#
) )
} }
#[test]
fn trace_layout_title() {
let layout = Child::new(Title::new(display::screen(), "Please confirm", |area| {
Dialog::new(
area,
|area| {
FormattedText::new::<theme::T1DefaultText>(
area,
"Testing text layout, with some text, and some more text. And {param}",
)
.with(b"param", b"parameters!")
},
Some(|area, pos| Button::with_text(area, pos, "Left", theme::button_cancel())),
Some(|area, pos| Button::with_text(area, pos, "Right", theme::button_default())),
)
}));
assert_eq!(
trace(&layout),
r#"<Title title:Please confirm content:<Dialog content:<Text content:Testing text layout,
with some text, and
some more text. And p-
arameters! > left:<Button text:Left > right:<Button text:Right > > >"#
)
}
} }