Skip to content

Commit

Permalink
fix(core/ui): make button press cancel current hold
Browse files Browse the repository at this point in the history
  • Loading branch information
ibz committed Jan 17, 2025
1 parent dd3bf25 commit b1eaf8b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
1 change: 1 addition & 0 deletions core/.changelog.d/3772.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[T3B1] Fix behavior of a button press during "hold to confirm".
2 changes: 2 additions & 0 deletions core/embed/rust/src/ui/event/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ pub enum ButtonEvent {
/// Button released up.
/// ▲ * | * ▲
ButtonReleased(PhysicalButton),

HoldStarted,
HoldEnded,
HoldCanceled,
}

impl ButtonEvent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,19 +215,26 @@ impl ButtonContainer {
self.long_pressed_timer.expire(event)
}

/// Registering hold event.
/// Registering the hold event
pub fn hold_started(&mut self, ctx: &mut EventCtx) {
if let ButtonType::HoldToConfirm(htc) = &mut self.button_type {
htc.event(ctx, Event::Button(ButtonEvent::HoldStarted));
}
}

/// Cancelling hold event.
/// Ending the hold event by releasing the button held
pub fn hold_ended(&mut self, ctx: &mut EventCtx) {
if let ButtonType::HoldToConfirm(htc) = &mut self.button_type {
htc.event(ctx, Event::Button(ButtonEvent::HoldEnded));
}
}

/// Canceling the hold event
pub fn hold_canceled(&mut self, ctx: &mut EventCtx) {
if let ButtonType::HoldToConfirm(htc) = &mut self.button_type {
htc.event(ctx, Event::Button(ButtonEvent::HoldCanceled));
}
}
}

/// Component responsible for handling buttons.
Expand Down Expand Up @@ -469,11 +476,19 @@ impl Component for ButtonController {
Some(ButtonControllerMsg::Pressed(ButtonPos::Middle)),
)
} else {
self.got_pressed(ctx, b.into());
(
ButtonState::BothDown,
Some(ButtonControllerMsg::Pressed(b.into())),
)
// When a button gets pressed while another one is being held,
// cancel the hold and do not register the press.
match which_down {
PhysicalButton::Left => {
self.left_btn.hold_canceled(ctx);
}
PhysicalButton::Right => {
self.right_btn.hold_canceled(ctx);
}
_ => {}
}

return None;
}
}
_ => (self.state, None),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ impl Component for HoldToConfirm {
self.loader.start_shrinking(ctx, Instant::now());
}
}
Event::Button(ButtonEvent::HoldCanceled) => {
self.loader.shrink_completely(ctx, Instant::now());
}
_ => {}
};

Expand Down
22 changes: 15 additions & 7 deletions core/embed/rust/src/ui/layout_samson/component/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ impl Loader {
ctx.request_paint();
}

pub fn start_shrinking(&mut self, ctx: &mut EventCtx, now: Instant) {
let mut anim = Animation::new(
display::LOADER_MAX,
display::LOADER_MIN,
self.shrinking_duration,
now,
);
fn start_shrinking_with_duration(
&mut self,
ctx: &mut EventCtx,
now: Instant,
duration: Duration,
) {
let mut anim = Animation::new(display::LOADER_MAX, display::LOADER_MIN, duration, now);
if let State::Growing(growing) = &self.state {
anim.seek_to_value(display::LOADER_MAX - growing.value(now));
}
Expand All @@ -125,6 +125,14 @@ impl Loader {
ctx.request_paint();
}

pub fn start_shrinking(&mut self, ctx: &mut EventCtx, now: Instant) {
self.start_shrinking_with_duration(ctx, now, self.shrinking_duration);
}

pub fn shrink_completely(&mut self, ctx: &mut EventCtx, now: Instant) {
self.start_shrinking_with_duration(ctx, now, Duration::from_millis(0));
}

pub fn reset(&mut self) {
self.state = State::Initial;
}
Expand Down

0 comments on commit b1eaf8b

Please sign in to comment.