1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-08 22:40:59 +00:00

refactor(core/rust): simplify code for internal paging events

[no changelog]
This commit is contained in:
matejcik 2024-09-04 14:12:35 +02:00 committed by matejcik
parent a5ddd13e8f
commit 30c08b6442
2 changed files with 53 additions and 61 deletions

View File

@ -5,7 +5,7 @@ use crate::{
component::{Event, EventCtx, SwipeDirection},
constant::screen,
event::TouchEvent,
geometry::{Offset, Point},
geometry::{Axis, Offset, Point},
util::animation_disabled,
},
};
@ -35,8 +35,7 @@ impl SwipeSettings {
#[derive(Copy, Clone, Default)]
pub struct SwipeConfig {
pub horizontal_pages: bool,
pub vertical_pages: bool,
pub page_axis: Option<Axis>,
pub up: Option<SwipeSettings>,
pub down: Option<SwipeSettings>,
pub left: Option<SwipeSettings>,
@ -46,8 +45,7 @@ pub struct SwipeConfig {
impl SwipeConfig {
pub const fn new() -> Self {
Self {
horizontal_pages: false,
vertical_pages: false,
page_axis: None,
up: None,
down: None,
left: None,
@ -97,23 +95,53 @@ impl SwipeConfig {
pub fn duration(&self, dir: SwipeDirection) -> Option<Duration> {
self[dir].as_ref().map(|s| s.duration)
}
pub fn has_horizontal_pages(&self) -> bool {
self.horizontal_pages
}
pub fn has_vertical_pages(&self) -> bool {
self.vertical_pages
}
pub fn with_horizontal_pages(mut self) -> Self {
self.horizontal_pages = true;
self.page_axis = Some(Axis::Horizontal);
self
}
pub fn with_vertical_pages(mut self) -> Self {
self.vertical_pages = true;
self.page_axis = Some(Axis::Vertical);
self
}
pub fn with_pagination(mut self, current_page: u16, total_pages: u16) -> Self {
let has_prev = current_page > 0;
let has_next = current_page < total_pages.saturating_sub(1);
match self.page_axis {
Some(Axis::Horizontal) => {
if has_prev {
self.right = Some(SwipeSettings::default());
}
if has_next {
self.left = Some(SwipeSettings::default());
}
}
Some(Axis::Vertical) => {
if has_prev {
self.down = Some(SwipeSettings::default());
}
if has_next {
self.up = Some(SwipeSettings::default());
}
}
_ => {}
}
self
}
pub fn paging_event(&self, dir: SwipeDirection, current_page: u16, total_pages: u16) -> u16 {
let prev_page = current_page.saturating_sub(1);
let next_page = (current_page + 1).min(total_pages.saturating_sub(1));
match (self.page_axis, dir) {
(Some(Axis::Horizontal), SwipeDirection::Right) => prev_page,
(Some(Axis::Horizontal), SwipeDirection::Left) => next_page,
(Some(Axis::Vertical), SwipeDirection::Down) => prev_page,
(Some(Axis::Vertical), SwipeDirection::Up) => next_page,
_ => current_page,
}
}
}
impl core::ops::Index<SwipeDirection> for SwipeConfig {

View File

@ -7,8 +7,7 @@ use crate::{
},
ui::{
component::{
base::{AttachType, AttachType::Swipe},
swipe_detect::SwipeSettings,
base::AttachType::{self, Swipe},
Component, Event, EventCtx, FlowMsg, SwipeDetect, SwipeDetectMsg, SwipeDirection,
},
display::Color,
@ -204,26 +203,12 @@ impl SwipeFlow {
let e = if self.allow_swipe {
let page = self.current_page();
let mut config = page.get_swipe_config();
let config = page
.get_swipe_config()
.with_pagination(self.internal_state, self.internal_pages);
self.internal_pages = page.get_internal_page_count() as u16;
// add additional swipe directions if there are more internal pages
// todo can we get internal settings from config somehow?
// might wanna different duration or something
if config.vertical_pages && self.internal_state > 0 {
config = config.with_swipe(SwipeDirection::Down, SwipeSettings::default())
}
if config.horizontal_pages && self.internal_state > 0 {
config = config.with_swipe(SwipeDirection::Right, SwipeSettings::default())
}
if config.vertical_pages && self.internal_state < self.internal_pages - 1 {
config = config.with_swipe(SwipeDirection::Up, SwipeSettings::default())
}
if config.horizontal_pages && self.internal_state < self.internal_pages - 1 {
config = config.with_swipe(SwipeDirection::Left, SwipeSettings::default())
}
match self.swipe.event(ctx, event, config) {
Some(SwipeDetectMsg::Trigger(dir)) => {
if let Some(override_decision) = self.decision_override.take() {
@ -234,41 +219,20 @@ impl SwipeFlow {
return_transition = AttachType::Swipe(dir);
let states_num = self.internal_pages;
if states_num > 0 {
if config.has_horizontal_pages() {
let current_state = self.internal_state;
if dir == SwipeDirection::Left && current_state < states_num - 1 {
self.internal_state += 1;
state_change = self.state_unchanged();
attach = true;
} else if dir == SwipeDirection::Right && current_state > 0 {
self.internal_state -= 1;
state_change = self.state_unchanged();
attach = true;
}
}
if config.has_vertical_pages() {
let current_state = self.internal_state;
if dir == SwipeDirection::Up && current_state < states_num - 1 {
self.internal_state += 1;
state_change = self.state_unchanged();
attach = true;
} else if dir == SwipeDirection::Down && current_state > 0 {
self.internal_state -= 1;
state_change = self.state_unchanged();
attach = true;
}
}
let new_internal_state =
config.paging_event(dir, self.internal_state, self.internal_pages);
if new_internal_state != self.internal_state {
self.internal_state = new_internal_state;
state_change = self.state_unchanged();
attach = true;
}
Event::Swipe(SwipeEvent::End(dir))
}
Some(SwipeDetectMsg::Move(dir, progress)) => {
Event::Swipe(SwipeEvent::Move(dir, progress as i16))
}
Some(SwipeDetectMsg::Start(_)) => Event::Touch(TouchEvent::TouchAbort),
_ => event,
None => event,
}
} else {
event