Skip to content

Commit

Permalink
feat(core/mercury): introduce binary_selection
Browse files Browse the repository at this point in the history
This commit adds a Component which prompts a user with a pair of two
buttons - left and right. The Component is parametrized by the buttons
contents and styles.

[no changelog]
  • Loading branch information
obrusvit committed Aug 7, 2024
1 parent c882985 commit ebc297f
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
88 changes: 88 additions & 0 deletions core/embed/rust/src/ui/model_mercury/component/binary_selection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use crate::ui::{
component::{Component, Event, EventCtx},
geometry::{Alignment2D, Offset, Rect},
shape::Renderer,
};

use super::{super::cshape, theme, Button, ButtonContent, ButtonMsg, ButtonStyleSheet};

pub enum BinarySelectionMsg {
Left,
Right,
}

#[derive(Clone)]
pub struct BinarySelection {
buttons_area: Rect,
button_left: Button,
button_right: Button,
}

impl BinarySelection {
pub fn new(
left_content: ButtonContent,
right_content: ButtonContent,
left_style: ButtonStyleSheet,
right_style: ButtonStyleSheet,
) -> Self {
Self {
buttons_area: Rect::zero(),
button_left: Button::new(left_content).styled(left_style),
button_right: Button::new(right_content).styled(right_style),
}
}
}

impl Component for BinarySelection {
type Msg = BinarySelectionMsg;

fn place(&mut self, bounds: Rect) -> Rect {
// Ensure reasonable space. Other than that, this component fits itself into any
// bounds.
let bounds_width = bounds.width();
let bounds_height = bounds.height();
assert!(bounds_width > 62);
assert!(bounds_height > 30);

let buttons_area = Rect::snap(
bounds.center(),
Offset::new(bounds_width, theme::BUTTON_HEIGHT.min(bounds_height)),
Alignment2D::CENTER,
);
let (left_area, _, right_area) = buttons_area.split_center(theme::SPACING);
self.button_left.place(left_area);
self.button_right.place(right_area);

self.buttons_area = buttons_area;
bounds
}

fn event(&mut self, ctx: &mut EventCtx, event: Event) -> Option<Self::Msg> {
if let Some(ButtonMsg::Clicked) = self.button_left.event(ctx, event) {
return Some(BinarySelectionMsg::Left);
}
if let Some(ButtonMsg::Clicked) = self.button_right.event(ctx, event) {
return Some(BinarySelectionMsg::Right);
}
None
}

fn paint(&mut self) {
unimplemented!()
}

fn render<'s>(&'s self, target: &mut impl Renderer<'s>) {
self.button_left.render(target);
self.button_right.render(target);
cshape::KeyboardOverlay::new(self.buttons_area).render(target);
}
}

#[cfg(feature = "ui_debug")]
impl crate::trace::Trace for BinarySelection {
fn trace(&self, t: &mut dyn crate::trace::Tracer) {
t.component("BinarySelection");
t.child("button_left", &self.button_left);
t.child("button_right", &self.button_right);
}
}
2 changes: 2 additions & 0 deletions core/embed/rust/src/ui/model_mercury/component/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(feature = "translations")]
mod address_details;
mod binary_selection;
pub mod bl_confirm;
mod button;
#[cfg(feature = "translations")]
Expand Down Expand Up @@ -41,6 +42,7 @@ mod welcome_screen;

#[cfg(feature = "translations")]
pub use address_details::AddressDetails;
pub use binary_selection::{BinarySelection, BinarySelectionMsg};
pub use button::{
Button, ButtonContent, ButtonMsg, ButtonStyle, ButtonStyleSheet, CancelConfirmMsg,
CancelInfoConfirmMsg, IconText,
Expand Down

0 comments on commit ebc297f

Please sign in to comment.