1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-06-26 09:52:34 +00:00

perf(core): don't use Option<Insets> to save memory

Otherwise, the compiler adds 2 bytes per `Button` struct.

[no changelog]
This commit is contained in:
Roman Zeyde 2025-06-08 11:01:41 +03:00
parent 5dbf726160
commit a6f99a65b4
4 changed files with 17 additions and 22 deletions

View File

@ -539,6 +539,10 @@ impl Insets {
} }
} }
pub const fn zero() -> Self {
Self::new(0, 0, 0, 0)
}
pub const fn uniform(d: i16) -> Self { pub const fn uniform(d: i16) -> Self {
Self::new(d, d, d, d) Self::new(d, d, d, d)
} }

View File

@ -37,7 +37,7 @@ pub trait SelectHandler = Fn(ButtonMsg) -> Option<SelectWordMsg>;
pub struct Button { pub struct Button {
area: Rect, area: Rect,
touch_expand: Option<Insets>, touch_expand: Insets,
content: ButtonContent, content: ButtonContent,
styles: ButtonStyleSheet, styles: ButtonStyleSheet,
state: State, state: State,
@ -55,7 +55,7 @@ impl Button {
Self { Self {
content, content,
area: Rect::zero(), area: Rect::zero(),
touch_expand: None, touch_expand: Insets::zero(),
styles: theme::button_default(), styles: theme::button_default(),
state: State::Initial, state: State::Initial,
long_press: Duration::ZERO, long_press: Duration::ZERO,
@ -90,7 +90,7 @@ impl Button {
} }
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 = expand;
self self
} }
@ -235,11 +235,7 @@ impl Component for Button {
} }
fn event(&mut self, ctx: &mut EventCtx, event: Event) -> Option<Self::Msg> { fn event(&mut self, ctx: &mut EventCtx, event: Event) -> Option<Self::Msg> {
let touch_area = if let Some(expand) = self.touch_expand { let touch_area = self.area.outset(self.touch_expand);
self.area.outset(expand)
} else {
self.area
};
match event { match event {
Event::Touch(TouchEvent::TouchStart(pos)) => { Event::Touch(TouchEvent::TouchStart(pos)) => {

View File

@ -24,7 +24,7 @@ pub enum ButtonMsg {
pub struct Button { pub struct Button {
area: Rect, area: Rect,
touch_expand: Option<Insets>, touch_expand: Insets,
content: ButtonContent, content: ButtonContent,
styles: ButtonStyleSheet, styles: ButtonStyleSheet,
text_align: Alignment, text_align: Alignment,
@ -45,7 +45,7 @@ impl Button {
Self { Self {
content, content,
area: Rect::zero(), area: Rect::zero(),
touch_expand: None, touch_expand: Insets::zero(),
styles: theme::button_default(), styles: theme::button_default(),
text_align: Alignment::Start, text_align: Alignment::Start,
radius: None, radius: None,
@ -83,7 +83,7 @@ impl Button {
} }
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 = expand;
self self
} }
@ -255,11 +255,7 @@ impl Component for Button {
} }
fn event(&mut self, ctx: &mut EventCtx, event: Event) -> Option<Self::Msg> { fn event(&mut self, ctx: &mut EventCtx, event: Event) -> Option<Self::Msg> {
let touch_area = if let Some(expand) = self.touch_expand { let touch_area = self.area.outset(self.touch_expand);
self.area.outset(expand)
} else {
self.area
};
match event { match event {
Event::Touch(TouchEvent::TouchStart(pos)) => { Event::Touch(TouchEvent::TouchStart(pos)) => {

View File

@ -29,7 +29,7 @@ pub enum ButtonMsg {
pub struct Button { pub struct Button {
area: Rect, area: Rect,
touch_expand: Option<Insets>, touch_expand: Insets,
content: ButtonContent, content: ButtonContent,
content_offset: Offset, content_offset: Offset,
stylesheet: ButtonStyleSheet, stylesheet: ButtonStyleSheet,
@ -66,7 +66,7 @@ impl Button {
content, content,
content_offset: Offset::zero(), content_offset: Offset::zero(),
area: Rect::zero(), area: Rect::zero(),
touch_expand: None, touch_expand: Insets::zero(),
stylesheet: theme::button_default(), stylesheet: theme::button_default(),
text_align: Alignment::Center, text_align: Alignment::Center,
radius: None, radius: None,
@ -160,7 +160,7 @@ impl Button {
} }
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 = expand;
self self
} }
@ -237,7 +237,7 @@ impl Button {
} }
pub fn set_expanded_touch_area(&mut self, expand: Insets) { pub fn set_expanded_touch_area(&mut self, expand: Insets) {
self.touch_expand = Some(expand); self.touch_expand = expand;
} }
pub fn set_content_offset(&mut self, offset: Offset) { pub fn set_content_offset(&mut self, offset: Offset) {
@ -323,8 +323,7 @@ impl Button {
} }
pub fn touch_area(&self) -> Rect { pub fn touch_area(&self) -> Rect {
self.touch_expand self.area.outset(self.touch_expand)
.map_or(self.area, |expand| self.area.outset(expand))
} }
fn set(&mut self, ctx: &mut EventCtx, state: State) { fn set(&mut self, ctx: &mut EventCtx, state: State) {