Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the MultiFieldReview to align with app-bitcoin #194

Merged
merged 4 commits into from
Sep 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 72 additions & 22 deletions ledger_device_sdk/src/ui/gadgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,9 @@ impl<'a> Page<'a> {
self.label[0].place(Location::Bottom, Layout::Centered, true);
} else {
icon_x = 57;
icon_y = 17;
self.label[0].place(Location::Custom(35), Layout::Centered, true);
icon_y = 10;
self.label[0].place(Location::Custom(28), Layout::Centered, true);
self.label[1].place(Location::Custom(42), Layout::Centered, true);
}
if let Some(glyph) = self.glyph {
let icon = Icon::from(glyph);
Expand Down Expand Up @@ -729,7 +730,7 @@ pub struct Field<'a> {
}

impl<'a> Field<'a> {
pub fn event_loop(&self, incoming_direction: ButtonEvent) -> ButtonEvent {
pub fn event_loop(&self, incoming_direction: ButtonEvent, is_first_field: bool) -> ButtonEvent {
let mut buttons = ButtonsState::new();
let chunk_max_lines = layout::MAX_LINES - 1;
let page_count = 1 + self.value.len() / (chunk_max_lines * MAX_CHAR_PER_LINE);
Expand Down Expand Up @@ -777,7 +778,9 @@ impl<'a> Field<'a> {
.trim_end_matches(' ');
chunks[0] = Label::from(header).bold();

LEFT_ARROW.display();
if !is_first_field {
LEFT_ARROW.display();
}
RIGHT_ARROW.display();

chunks.place(Location::Middle, Layout::Centered, false);
Expand Down Expand Up @@ -823,7 +826,7 @@ pub struct MultiFieldReview<'a> {
fields: &'a [Field<'a>],
review_message: &'a [&'a str],
review_glyph: Option<&'a Glyph<'a>>,
validation_message: &'a str,
validation_message: [&'a str; 2],
validation_glyph: Option<&'a Glyph<'a>>,
cancel_message: &'a str,
cancel_glyph: Option<&'a Glyph<'a>>,
Expand Down Expand Up @@ -856,6 +859,26 @@ impl<'a> MultiFieldReview<'a> {
validation_glyph: Option<&'a Glyph<'a>>,
cancel_message: &'a str,
cancel_glyph: Option<&'a Glyph<'a>>,
) -> Self {
Self::new_with_validation_messages(
fields,
review_message,
review_glyph,
[validation_message, ""],
validation_glyph,
cancel_message,
cancel_glyph,
)
}

pub fn new_with_validation_messages(
fields: &'a [Field<'a>],
review_message: &'a [&'a str],
review_glyph: Option<&'a Glyph<'a>>,
validation_message: [&'a str; 2],
validation_glyph: Option<&'a Glyph<'a>>,
cancel_message: &'a str,
cancel_glyph: Option<&'a Glyph<'a>>,
) -> Self {
MultiFieldReview {
fields,
Expand All @@ -869,27 +892,25 @@ impl<'a> MultiFieldReview<'a> {
}

pub fn show(&self) -> bool {
let first_page = match self.review_message.len() {
0 => Page::new(PageStyle::PictureNormal, ["", ""], self.review_glyph),
1 => Page::new(
let first_page_opt = match self.review_message.len() {
0 => None,
1 => Some(Page::new(
PageStyle::PictureBold,
[self.review_message[0], ""],
self.review_glyph,
),
_ => Page::new(
)),
_ => Some(Page::new(
PageStyle::PictureNormal,
[self.review_message[0], self.review_message[1]],
self.review_glyph,
),
)),
};

clear_screen();
first_page.place_and_wait();
crate::ui::screen_util::screen_update();
display_first_page(&first_page_opt);

let validation_page = Page::new(
PageStyle::PictureBold,
[self.validation_message, ""],
self.validation_message,
self.validation_glyph,
);
let cancel_page = Page::new(
Expand All @@ -903,9 +924,10 @@ impl<'a> MultiFieldReview<'a> {

loop {
match cur_page {
cancel if cancel == self.fields.len() => {
cancel if cancel == self.fields.len() + 1 => {
let mut buttons = ButtonsState::new();
clear_screen();
LEFT_ARROW.display();
cancel_page.place();
crate::ui::screen_util::screen_update();
loop {
Expand All @@ -914,24 +936,31 @@ impl<'a> MultiFieldReview<'a> {
cur_page = cur_page.saturating_sub(1);
break;
}
Some(ButtonEvent::RightButtonRelease) => {
cur_page += 1;
break;
}
Some(ButtonEvent::BothButtonsRelease) => return false,
_ => (),
}
}
}
validation if validation == self.fields.len() + 1 => {
validation if validation == self.fields.len() => {
let mut buttons = ButtonsState::new();
clear_screen();
LEFT_ARROW.display();
RIGHT_ARROW.display();
validation_page.place();
crate::ui::screen_util::screen_update();
loop {
match get_event(&mut buttons) {
Some(ButtonEvent::LeftButtonRelease) => {
cur_page = cur_page.saturating_sub(1);
if cur_page == 0 && self.fields.is_empty() {
display_first_page(&first_page_opt);
} else {
direction = ButtonEvent::LeftButtonRelease;
}
break;
}
Some(ButtonEvent::RightButtonRelease) => {
cur_page += 1;
break;
}
Some(ButtonEvent::BothButtonsRelease) => return true,
Expand All @@ -940,10 +969,12 @@ impl<'a> MultiFieldReview<'a> {
}
}
_ => {
direction = self.fields[cur_page].event_loop(direction);
direction = self.fields[cur_page]
.event_loop(direction, cur_page == 0 && first_page_opt.is_none());
match direction {
ButtonEvent::LeftButtonRelease => {
if cur_page == 0 {
display_first_page(&first_page_opt);
direction = ButtonEvent::RightButtonRelease;
} else {
cur_page -= 1;
Expand All @@ -959,3 +990,22 @@ impl<'a> MultiFieldReview<'a> {
}
}
}

fn display_first_page(page_opt: &Option<Page>) {
match page_opt {
Some(page) => {
clear_screen();
RIGHT_ARROW.display();
page.place();
crate::ui::screen_util::screen_update();

let mut buttons = ButtonsState::new();
loop {
if let Some(ButtonEvent::RightButtonRelease) = get_event(&mut buttons) {
return;
}
}
}
None => (),
}
}