feat(core/ui): add text align to T3T1 button

obrusvit 1 month ago
parent 34f6964241
commit c16800a9e0

@ -9,7 +9,7 @@ use crate::{
}, },
display::{self, toif::Icon, Color, Font}, display::{self, toif::Icon, Color, Font},
event::TouchEvent, event::TouchEvent,
geometry::{Alignment2D, Insets, Offset, Point, Rect}, geometry::{Alignment, Alignment2D, Insets, Offset, Point, Rect},
shape, shape,
shape::Renderer, shape::Renderer,
}, },
@ -30,6 +30,7 @@ pub struct Button {
touch_expand: Option<Insets>, touch_expand: Option<Insets>,
content: ButtonContent, content: ButtonContent,
styles: ButtonStyleSheet, styles: ButtonStyleSheet,
text_align: Alignment,
state: State, state: State,
long_press: Option<Duration>, long_press: Option<Duration>,
long_timer: Option<TimerToken>, long_timer: Option<TimerToken>,
@ -47,6 +48,7 @@ impl Button {
area: Rect::zero(), area: Rect::zero(),
touch_expand: None, touch_expand: None,
styles: theme::button_default(), styles: theme::button_default(),
text_align: Alignment::Start,
state: State::Initial, state: State::Initial,
long_press: None, long_press: None,
long_timer: None, long_timer: None,
@ -78,6 +80,11 @@ impl Button {
self self
} }
pub const fn with_text_align(mut self, align: Alignment) -> Self {
self.text_align = align;
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
@ -217,11 +224,19 @@ impl Button {
match &self.content { match &self.content {
ButtonContent::Empty => {} ButtonContent::Empty => {}
ButtonContent::Text(text) => { ButtonContent::Text(text) => {
let start_of_baseline = self.area.left_center() + Self::BASELINE_OFFSET; let y_offset = Offset::y(self.style().font.allcase_text_height() / 2);
let start_of_baseline = match self.text_align {
Alignment::Start => {
self.area.left_center() + Offset::x(Self::BASELINE_OFFSET.x)
}
Alignment::Center => self.area.center(),
Alignment::End => self.area.right_center() - Offset::x(Self::BASELINE_OFFSET.x),
} + y_offset;
text.map(|text| { text.map(|text| {
shape::Text::new(start_of_baseline, text) shape::Text::new(start_of_baseline, text)
.with_font(style.font) .with_font(style.font)
.with_fg(style.text_color) .with_fg(style.text_color)
.with_align(self.text_align)
.render(target); .render(target);
}); });
} }

Loading…
Cancel
Save