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::{
component::{Child, Component, Event, EventCtx},
display,
geometry::{Offset, Rect},
geometry::Rect,
};
pub enum DialogMsg<T> {
@ -15,7 +14,6 @@ pub enum DialogMsg<T> {
}
pub struct Dialog<T, U> {
header: Option<(U, Rect)>,
content: Child<T>,
left_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,
left: Option<impl FnOnce(Rect, ButtonPos) -> Button<U>>,
right: Option<impl FnOnce(Rect, ButtonPos) -> Button<U>>,
header: Option<U>,
) -> 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 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)));
Self {
header: header.zip(header_area),
content,
left_btn,
right_btn,
}
}
fn paint_header(&self) {
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;
fn areas(area: Rect) -> (Rect, Rect) {
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);
if header.is_none() {
(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)
}
(content_area, button_area)
}
}
@ -86,7 +60,6 @@ impl<T: Component, U: AsRef<[u8]>> Component for Dialog<T, U> {
}
fn paint(&mut self) {
self.paint_header();
self.content.paint();
if let Some(b) = self.left_btn.as_mut() {
b.paint();

View File

@ -1,7 +1,9 @@
mod button;
mod dialog;
mod title;
use super::{event, theme};
pub use button::{Button, ButtonContent, ButtonMsg, ButtonPos, ButtonStyle, ButtonStyleSheet};
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::{
component::{Button, Dialog, DialogMsg},
component::{Button, Dialog, DialogMsg, Title},
theme,
};
@ -64,13 +64,14 @@ extern "C" fn ui_layout_new_confirm_action(
let obj = LayoutObj::new(Child::new(Dialog::new(
display::screen(),
|area| {
FormattedText::new::<theme::T1DefaultText>(area, format)
.with(b"action", action.unwrap_or("".into()))
.with(b"description", description.unwrap_or("".into()))
Title::new(area, title, |area| {
FormattedText::new::<theme::T1DefaultText>(area, format)
.with(b"action", action.unwrap_or("".into()))
.with(b"description", description.unwrap_or("".into()))
})
},
left,
right,
Some(title),
)))?;
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, "Right", theme::button_default())),
None,
));
assert_eq!(
trace(&layout),
@ -147,4 +147,29 @@ some more text. And p-
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 > > >"#
)
}
}