1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-02-28 15:22:14 +00:00

feat(eckhart): Add parametrized content offset to Button

This commit is contained in:
Lukas Bielesch 2025-02-17 23:02:29 +01:00 committed by obrusvit
parent 7109b8c971
commit 78656bdfff

View File

@ -26,6 +26,7 @@ pub struct Button {
area: Rect, area: Rect,
touch_expand: Option<Insets>, touch_expand: Option<Insets>,
content: ButtonContent, content: ButtonContent,
content_offset: Offset,
styles: ButtonStyleSheet, styles: ButtonStyleSheet,
text_align: Alignment, text_align: Alignment,
radius: Option<u8>, radius: Option<u8>,
@ -36,13 +37,13 @@ pub struct Button {
} }
impl Button { impl Button {
pub const BASELINE_OFFSET: Offset = Offset::new(12, 6);
const LINE_SPACING: i16 = 7; const LINE_SPACING: i16 = 7;
const SUBTEXT_STYLE: TextStyle = theme::label_menu_item_subtitle(); const SUBTEXT_STYLE: TextStyle = theme::label_menu_item_subtitle();
pub const fn new(content: ButtonContent) -> Self { pub const fn new(content: ButtonContent) -> Self {
Self { Self {
content, content,
content_offset: Offset::zero(),
area: Rect::zero(), area: Rect::zero(),
touch_expand: None, touch_expand: None,
styles: theme::button_default(), styles: theme::button_default(),
@ -85,6 +86,11 @@ impl Button {
self self
} }
pub const fn with_content_offset(mut self, offset: Offset) -> Self {
self.content_offset = offset;
self
}
pub const fn with_expanded_touch_area(mut self, expand: Insets) -> Self { pub const fn with_expanded_touch_area(mut self, expand: Insets) -> Self {
self.touch_expand = Some(expand); self.touch_expand = Some(expand);
self self
@ -157,6 +163,10 @@ impl Button {
&self.content &self.content
} }
pub fn content_offset(&self) -> Offset {
self.content_offset
}
pub fn content_height(&self) -> i16 { pub fn content_height(&self) -> i16 {
match &self.content { match &self.content {
ButtonContent::Empty => 0, ButtonContent::Empty => 0,
@ -238,11 +248,9 @@ impl Button {
ButtonContent::Text(text) => { ButtonContent::Text(text) => {
let y_offset = Offset::y(self.content_height() / 2); let y_offset = Offset::y(self.content_height() / 2);
let start_of_baseline = match self.text_align { let start_of_baseline = match self.text_align {
Alignment::Start => { Alignment::Start => self.area.left_center() + self.content_offset,
self.area.left_center() + Offset::x(Self::BASELINE_OFFSET.x)
}
Alignment::Center => self.area.center(), Alignment::Center => self.area.center(),
Alignment::End => self.area.right_center() - Offset::x(Self::BASELINE_OFFSET.x), Alignment::End => self.area.right_center() - self.content_offset,
} + y_offset; } + y_offset;
text.map(|text| { text.map(|text| {
shape::Text::new(start_of_baseline, text, style.font) shape::Text::new(start_of_baseline, text, style.font)
@ -257,11 +265,9 @@ impl Button {
Offset::y(self.content_height() / 2 - self.style().font.allcase_text_height()); Offset::y(self.content_height() / 2 - self.style().font.allcase_text_height());
let subtext_y_offset = Offset::y(self.content_height() / 2); let subtext_y_offset = Offset::y(self.content_height() / 2);
let start_of_baseline = match self.text_align { let start_of_baseline = match self.text_align {
Alignment::Start => { Alignment::Start => self.area.left_center() + self.content_offset,
self.area.left_center() + Offset::x(Self::BASELINE_OFFSET.x)
}
Alignment::Center => self.area.center(), Alignment::Center => self.area.center(),
Alignment::End => self.area.right_center() - Offset::x(Self::BASELINE_OFFSET.x), Alignment::End => self.area.right_center() - self.content_offset,
}; };
let text_baseline = start_of_baseline - text_y_offset; let text_baseline = start_of_baseline - text_y_offset;
let subtext_baseline = start_of_baseline + subtext_y_offset; let subtext_baseline = start_of_baseline + subtext_y_offset;
@ -290,13 +296,7 @@ impl Button {
.render(target); .render(target);
} }
ButtonContent::IconAndText(child) => { ButtonContent::IconAndText(child) => {
child.render( child.render(target, self.area, self.style(), self.content_offset, alpha);
target,
self.area,
self.style(),
Self::BASELINE_OFFSET,
alpha,
);
} }
} }
} }