1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-23 06:48:16 +00:00

feat(core/rust/ui): explicit breaks for Paragraphs

[no changelog]
This commit is contained in:
Martin Milata 2022-07-01 14:08:03 +02:00
parent d96d3e705b
commit 247d9d267a
3 changed files with 51 additions and 2 deletions

View File

@ -71,6 +71,13 @@ where
self
}
pub fn add_break(mut self) -> Self {
if let Some(ref mut para) = self.list.last_mut() {
para.break_after = true;
};
self
}
/// Update bounding boxes of paragraphs on the current page. First determine
/// the number of visible paragraphs and their sizes. These are then
/// arranged according to the layout.
@ -94,6 +101,10 @@ where
remaining_area = free;
self.visible += 1;
char_offset = 0;
if paragraph.break_after {
break;
}
}
self.placement.arrange(
@ -195,6 +206,7 @@ pub mod trace {
pub struct Paragraph<T> {
content: T,
layout: TextLayout,
break_after: bool,
}
impl<T> Paragraph<T>
@ -202,7 +214,11 @@ where
T: AsRef<str>,
{
pub fn new(content: T, layout: TextLayout) -> Self {
Self { content, layout }
Self {
content,
layout,
break_after: false,
}
}
pub fn content(&self, char_offset: usize) -> &str {
@ -274,6 +290,11 @@ where
current.par += 1;
current.chr = 0;
progress = true;
// Handle hard break if requested for this paragraph.
if paragraph.break_after && current.par < self.paragraphs.list.len() {
return self.current;
}
}
LayoutFit::OutOfBounds {
processed_chars, ..

View File

@ -470,4 +470,32 @@ mod tests {
swipe_down(&mut page);
assert_eq!(trace(&page), expected1);
}
#[test]
fn paragraphs_hard_break() {
let mut page = SwipePage::new(
Paragraphs::new()
.add(theme::TEXT_NORMAL, "Short one.")
.add_break()
.add(theme::TEXT_NORMAL, "Short two.")
.add_break()
.add(theme::TEXT_NORMAL, "Short three.")
.add_break(),
Empty,
theme::BG,
);
page.place(SCREEN);
let expected1 = "<SwipePage active_page:0 page_count:3 content:<Paragraphs Short one.\n> buttons:<Empty > >";
let expected2 = "<SwipePage active_page:1 page_count:3 content:<Paragraphs Short two.\n> buttons:<Empty > >";
let expected3 = "<SwipePage active_page:2 page_count:3 content:<Paragraphs Short three.\n> buttons:<Empty > >";
assert_eq!(trace(&page), expected1);
swipe_up(&mut page);
assert_eq!(trace(&page), expected2);
swipe_up(&mut page);
assert_eq!(trace(&page), expected3);
swipe_up(&mut page);
assert_eq!(trace(&page), expected3);
}
}

View File

@ -376,7 +376,7 @@ extern "C" fn new_confirm_modify_output(n_args: usize, args: *const Obj, kwargs:
let paragraphs = Paragraphs::new()
.add(theme::TEXT_NORMAL, "Address:".into())
.add(theme::TEXT_MONO, address)
// FIXME pagebreak
.add_break()
.add(theme::TEXT_NORMAL, description.into())
.add(theme::TEXT_MONO, amount_change)
.add(theme::TEXT_NORMAL, "New amount:".into())