-
-
Notifications
You must be signed in to change notification settings - Fork 672
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core/mercury): introduce binary_selection
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
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
core/embed/rust/src/ui/model_mercury/component/binary_selection.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters