1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-06-25 01:18:54 +00:00

feat(core): add disable haptic option to tt ui button

[no changelog]
This commit is contained in:
tychovrahe 2024-05-02 09:27:53 +02:00 committed by TychoVrahe
parent 04d5017228
commit c680187b0e

View File

@ -1,5 +1,5 @@
#[cfg(feature = "haptic")] #[cfg(feature = "haptic")]
use crate::trezorhal::haptic::{play, HapticEffect}; use crate::trezorhal::haptic::{self, HapticEffect};
use crate::{ use crate::{
strutil::TString, strutil::TString,
time::Duration, time::Duration,
@ -30,6 +30,7 @@ pub struct Button {
state: State, state: State,
long_press: Option<Duration>, long_press: Option<Duration>,
long_timer: Option<TimerToken>, long_timer: Option<TimerToken>,
haptics: bool,
} }
impl Button { impl Button {
@ -46,6 +47,7 @@ impl Button {
state: State::Initial, state: State::Initial,
long_press: None, long_press: None,
long_timer: None, long_timer: None,
haptics: true,
} }
} }
@ -84,6 +86,11 @@ impl Button {
self self
} }
pub const fn without_haptics(mut self) -> Self {
self.haptics = false;
self
}
pub fn enable_if(&mut self, ctx: &mut EventCtx, enabled: bool) { pub fn enable_if(&mut self, ctx: &mut EventCtx, enabled: bool) {
if enabled { if enabled {
self.enable(ctx); self.enable(ctx);
@ -252,7 +259,9 @@ impl Component for Button {
// Touch started in our area, transform to `Pressed` state. // Touch started in our area, transform to `Pressed` state.
if touch_area.contains(pos) { if touch_area.contains(pos) {
#[cfg(feature = "haptic")] #[cfg(feature = "haptic")]
play(HapticEffect::ButtonPress); if self.haptics {
haptic::play(HapticEffect::ButtonPress);
}
self.set(ctx, State::Pressed); self.set(ctx, State::Pressed);
if let Some(duration) = self.long_press { if let Some(duration) = self.long_press {
self.long_timer = Some(ctx.request_timer(duration)); self.long_timer = Some(ctx.request_timer(duration));
@ -296,7 +305,9 @@ impl Component for Button {
self.long_timer = None; self.long_timer = None;
if matches!(self.state, State::Pressed) { if matches!(self.state, State::Pressed) {
#[cfg(feature = "haptic")] #[cfg(feature = "haptic")]
play(HapticEffect::ButtonPress); if self.haptics {
haptic::play(HapticEffect::ButtonPress);
}
self.set(ctx, State::Initial); self.set(ctx, State::Initial);
return Some(ButtonMsg::LongPressed); return Some(ButtonMsg::LongPressed);
} }