1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-20 14:39:22 +00:00

refactor(core/rust/ui): unhardcode screen size

[no changelog]
This commit is contained in:
Martin Milata 2022-01-26 17:21:10 +01:00
parent 82182ae078
commit d51072b8c1
10 changed files with 73 additions and 56 deletions

View File

@ -57,25 +57,12 @@ extern "C" {
);
}
#[cfg(not(feature = "model_tt"))]
use crate::ui::model_t1::constant;
#[cfg(feature = "model_tt")]
use crate::ui::model_tt::constant;
pub struct ToifInfo {
pub width: u16,
pub height: u16,
pub grayscale: bool,
}
pub fn width() -> i32 {
constant::WIDTH
}
pub fn height() -> i32 {
constant::HEIGHT
}
pub fn backlight(val: i32) -> i32 {
unsafe { display_backlight(val) }
}

View File

@ -7,22 +7,6 @@ use crate::ui::model_tt::constant;
use super::geometry::{Offset, Point, Rect};
pub fn width() -> i32 {
display::width()
}
pub fn height() -> i32 {
display::height()
}
pub fn size() -> Offset {
Offset::new(width(), height())
}
pub fn screen() -> Rect {
Rect::from_top_left_and_size(Point::zero(), size())
}
pub fn backlight() -> i32 {
display::backlight(-1)
}

View File

@ -135,8 +135,13 @@ impl Rect {
Self::new(Point::zero(), Point::zero())
}
pub fn from_top_left_and_size(p0: Point, size: Offset) -> Self {
Self::new(p0, p0 + size)
pub const fn from_top_left_and_size(p0: Point, size: Offset) -> Self {
Self {
x0: p0.x,
y0: p0.y,
x1: p0.x + size.x,
y1: p0.y + size.y,
}
}
pub fn from_center_and_size(p: Point, size: Offset) -> Self {
@ -213,7 +218,7 @@ impl Rect {
}
}
pub fn inset(&self, insets: Insets) -> Self {
pub const fn inset(&self, insets: Insets) -> Self {
Self {
x0: self.x0 + insets.left,
y0: self.y0 + insets.top,

View File

@ -26,6 +26,11 @@ use crate::ui::model_tt::event::TouchEvent;
#[cfg(feature = "model_t1")]
use crate::ui::model_t1::event::ButtonEvent;
#[cfg(not(feature = "model_tt"))]
use crate::ui::model_t1::constant;
#[cfg(feature = "model_tt")]
use crate::ui::model_tt::constant;
/// Conversion trait implemented by components that know how to convert their
/// message values into MicroPython `Obj`s. We can automatically implement
/// `ComponentMsgObj` for components whose message types implement `TryInto`.
@ -55,7 +60,6 @@ mod maybe_trace {
impl<T> MaybeTrace for T {}
}
use crate::ui::display;
/// Stand-in for the optionally-compiled trait `Trace`.
/// If UI debugging is enabled, `MaybeTrace` implies `Trace` and is implemented
/// for everything that implements Trace. If disabled, `MaybeTrace` is
@ -152,7 +156,7 @@ impl LayoutObj {
// Place the root component on the screen in case it was previously requested.
if inner.event_ctx.needs_place_before_next_event_or_paint() {
// SAFETY: `inner.root` is unique because of the `inner.borrow_mut()`.
unsafe { Gc::as_mut(&mut inner.root) }.obj_place(display::screen());
unsafe { Gc::as_mut(&mut inner.root) }.obj_place(constant::screen());
}
// Clear the leftover flags from the previous event pass.
@ -186,7 +190,7 @@ impl LayoutObj {
// Place the root component on the screen in case it was previously requested.
if inner.event_ctx.needs_place_before_next_event_or_paint() {
// SAFETY: `inner.root` is unique because of the `inner.borrow_mut()`.
unsafe { Gc::as_mut(&mut inner.root) }.obj_place(display::screen());
unsafe { Gc::as_mut(&mut inner.root) }.obj_place(constant::screen());
}
// SAFETY: `inner.root` is unique because of the `inner.borrow_mut()`.
@ -255,7 +259,7 @@ impl LayoutObj {
display::rect_stroke(r, color)
}
wireframe(display::screen());
// wireframe(display::screen());
self.inner.borrow().root.obj_bounds(&mut wireframe);
}

View File

@ -1,3 +1,13 @@
use crate::ui::geometry::{Offset, Point, Rect};
pub const WIDTH: i32 = 128;
pub const HEIGHT: i32 = 64;
pub const LINE_SPACE: i32 = 1;
pub const fn size() -> Offset {
Offset::new(WIDTH, HEIGHT)
}
pub const fn screen() -> Rect {
Rect::from_top_left_and_size(Point::zero(), size())
}

View File

@ -11,8 +11,8 @@ use crate::{
};
use super::{
component::{Button, ButtonPage, Frame},
theme,
component::{Button, ButtonPage, Dialog, DialogMsg, Frame},
constant, theme,
};
#[no_mangle]
@ -141,7 +141,7 @@ mod tests {
theme::button_default(),
)),
);
layout.place(display::screen());
layout.place(constant::screen());
assert_eq!(
trace(&layout),
r#"<Dialog content:<Text content:Testing text layout,
@ -172,7 +172,7 @@ arameters! > left:<Button text:Left > right:<Button text:Right > >"#
)),
),
);
layout.place(display::screen());
layout.place(constant::screen());
assert_eq!(
trace(&layout),
r#"<Frame title:Please confirm content:<Dialog content:<Text content:Testing text layout,

View File

@ -327,12 +327,14 @@ mod tests {
ui::{
component::{text::paragraphs::Paragraphs, Empty},
geometry::Point,
model_tt::{event::TouchEvent, theme},
model_tt::{constant, event::TouchEvent, theme},
},
};
use super::*;
const SCREEN: Rect = constant::screen().inset(theme::borders());
fn trace(val: &impl Trace) -> String {
let mut t = Vec::new();
val.trace(&mut t);
@ -368,7 +370,7 @@ mod tests {
#[test]
fn paragraphs_empty() {
let mut page = SwipePage::new(Paragraphs::<&str>::new(), Empty, theme::BG);
page.place(display::screen());
page.place(SCREEN);
let expected =
"<SwipePage active_page:0 page_count:1 content:<Paragraphs > buttons:<Empty > >";
@ -395,7 +397,7 @@ mod tests {
Empty,
theme::BG,
);
page.place(display::screen());
page.place(SCREEN);
let expected = "<SwipePage active_page:0 page_count:1 content:<Paragraphs This is the first paragraph\nand it should fit on the\nscreen entirely.\nSecond, bold, paragraph\nshould also fit on the\nscreen whole I think.\n> buttons:<Empty > >";
@ -417,10 +419,10 @@ mod tests {
Empty,
theme::BG,
);
page.place(display::screen());
page.place(SCREEN);
let expected1 = "<SwipePage active_page:0 page_count:2 content:<Paragraphs This is somewhat long\nparagraph that goes\non and on and on and\non and on and will\ndefinitely not fit on\njust a single screen.\nYou have to swipe a bit\nto see all the text it...\n> buttons:<Empty > >";
let expected2 = "<SwipePage active_page:1 page_count:2 content:<Paragraphs contains I guess.\nThere's just so much\nletters in it.\n> buttons:<Empty > >";
let expected1 = "<SwipePage active_page:0 page_count:2 content:<Paragraphs This is somewhat long\nparagraph that goes on\nand on and on and on\nand on and will definitely\nnot fit on just a single\nscreen. You have to\nswipe a bit to see all the\ntext it contains I guess....\n> buttons:<Empty > >";
let expected2 = "<SwipePage active_page:1 page_count:2 content:<Paragraphs There's just so much\nletters in it.\n> buttons:<Empty > >";
assert_eq!(trace(&page), expected1);
swipe_down(&mut page);
@ -452,11 +454,11 @@ mod tests {
Empty,
theme::BG,
);
page.place(display::screen());
page.place(SCREEN);
let expected1 = "<SwipePage active_page:0 page_count:3 content:<Paragraphs This paragraph is\nusing a bold font. It\ndoesn't need to be all\nthat long.\nAnd this one is\nusing MONO.\nMonospace is nice\nfor numbers, they...\n> buttons:<Empty > >";
let expected2 = "<SwipePage active_page:1 page_count:3 content:<Paragraphs have the same\nwidth and can be\nscanned quickly.\nEven if they span\nseveral pages or\nsomething.\nLet's add another one\nfor a good measure....\n> buttons:<Empty > >";
let expected3 = "<SwipePage active_page:2 page_count:3 content:<Paragraphs This one should\noverflow all the way to\nthe third page with a\nbit of luck.\n> buttons:<Empty > >";
let expected1 = "<SwipePage active_page:0 page_count:3 content:<Paragraphs This paragraph is using a\nbold font. It doesn't\nneed to be all that long.\nAnd this one is\nusing MONO.\nMonospace is\nnice for...\n> buttons:<Empty > >";
let expected2 = "<SwipePage active_page:1 page_count:3 content:<Paragraphs numbers, they\nhave the same\nwidth and can be\nscanned quickly.\nEven if they\nspan several\npages or...\n> buttons:<Empty > >";
let expected3 = "<SwipePage active_page:2 page_count:3 content:<Paragraphs something.\nLet's add another one\nfor a good measure. This\none should overflow all\nthe way to the third\npage with a bit of luck.\n> buttons:<Empty > >";
assert_eq!(trace(&page), expected1);
swipe_down(&mut page);

View File

@ -1,3 +1,13 @@
use crate::ui::geometry::{Offset, Point, Rect};
pub const WIDTH: i32 = 240;
pub const HEIGHT: i32 = 240;
pub const LINE_SPACE: i32 = 4;
pub const fn size() -> Offset {
Offset::new(WIDTH, HEIGHT)
}
pub const fn screen() -> Rect {
Rect::from_top_left_and_size(Point::zero(), size())
}

View File

@ -11,7 +11,11 @@ use crate::{
};
use super::{
component::{Button, ButtonMsg, DialogMsg, Frame, HoldToConfirm, HoldToConfirmMsg, SwipePage},
component::{
Bip39Input, Button, ButtonMsg, DialogMsg, Frame, HoldToConfirm, HoldToConfirmMsg,
MnemonicKeyboard, MnemonicKeyboardMsg, PassphraseKeyboard, PassphraseKeyboardMsg,
PinKeyboard, PinKeyboardMsg, Slip39Input, SwipePage,
},
theme,
};
@ -114,13 +118,18 @@ mod tests {
trace::Trace,
ui::{
component::Component,
display,
model_tt::component::{Button, Dialog},
geometry::Rect,
model_tt::{
component::{Button, Dialog},
constant,
},
},
};
use super::*;
const SCREEN: Rect = constant::screen().inset(theme::borders());
fn trace(val: &impl Trace) -> String {
let mut t = Vec::new();
val.trace(&mut t);
@ -137,7 +146,7 @@ mod tests {
Button::with_text(b"Left"),
Button::with_text(b"Right"),
);
layout.place(display::screen());
layout.place(SCREEN);
assert_eq!(
trace(&layout),
"<Dialog content:<Text content:Testing text layout, with\nsome text, and some more\ntext. And parameters! > left:<Button text:Left > right:<Button text:Right > >",

View File

@ -4,7 +4,9 @@ use crate::ui::{
geometry::Insets,
};
use super::component::{ButtonStyle, ButtonStyleSheet, LoaderStyle, LoaderStyleSheet};
use super::{
component::{ButtonStyle, ButtonStyleSheet, LoaderStyle, LoaderStyleSheet},
};
// Font constants.
pub const FONT_NORMAL: Font = Font::new(-1);
@ -165,6 +167,10 @@ pub const CONTENT_BORDER: i32 = 5;
/// | +----+ |
/// | 14 |
/// +----------+
pub fn borders_scroll() -> Insets {
pub const fn borders() -> Insets {
Insets::new(13, 10, 14, 10)
}
pub const fn borders_scroll() -> Insets {
Insets::new(13, 5, 14, 10)
}