refactor(core/ui): T3T1: drop duplicate Swipe implementation

[no changelog]
pull/3874/head
Martin Milata 2 months ago
parent d2bfe56293
commit d6152c02e9

@ -25,6 +25,7 @@ impl SwipeDirection {
}
/// Copy of `model_tt/component/swipe.rs` but without the backlight handling.
#[derive(Clone)]
pub struct Swipe {
pub area: Rect,
pub allow_up: bool,

@ -5,7 +5,7 @@ use crate::{
animation::Animation,
component::{Component, Event, EventCtx, Swipe, SwipeDirection},
flow::{base::Decision, FlowMsg, FlowState, FlowStore},
geometry::{Offset, Rect},
geometry::Rect,
shape::Renderer,
util,
},

@ -1,14 +1,12 @@
use crate::{
strutil::TString,
ui::{
component::{image::Image, Child, Component, Event, EventCtx, Label},
component::{
image::Image, Child, Component, Event, EventCtx, Label, Swipe, SwipeDirection,
},
display,
geometry::{Insets, Rect},
model_mercury::component::{
fido_icons::get_fido_icon_data,
swipe::{Swipe, SwipeDirection},
theme, ScrollBar,
},
model_mercury::component::{fido_icons::get_fido_icon_data, theme, ScrollBar},
shape,
shape::Renderer,
},

@ -1,10 +1,13 @@
use crate::{
strutil::TString,
ui::{
component::{maybe::paint_overlapping, Child, Component, Event, EventCtx, Label, Maybe},
component::{
maybe::paint_overlapping, Child, Component, Event, EventCtx, Label, Maybe, Swipe,
SwipeDirection,
},
geometry::{Alignment, Grid, Insets, Rect},
model_mercury::{
component::{Button, ButtonMsg, Swipe, SwipeDirection},
component::{Button, ButtonMsg},
theme,
},
shape::Renderer,

@ -29,7 +29,6 @@ mod scroll;
mod share_words;
mod simple_page;
mod status_screen;
mod swipe;
mod swipe_up_screen;
mod welcome_screen;
@ -70,7 +69,6 @@ pub use scroll::ScrollBar;
pub use share_words::ShareWords;
pub use simple_page::SimplePage;
pub use status_screen::StatusScreen;
pub use swipe::{Swipe, SwipeDirection};
pub use swipe_up_screen::{SwipeUpScreen, SwipeUpScreenMsg};
pub use vertical_menu::{VerticalMenu, VerticalMenuChoiceMsg};
pub use welcome_screen::WelcomeScreen;

@ -4,7 +4,10 @@ use crate::{
time::Instant,
translations::TR,
ui::{
component::{paginated::PageMsg, Component, ComponentExt, Event, EventCtx, Pad, Paginate},
component::{
paginated::PageMsg, Component, ComponentExt, Event, EventCtx, Pad, Paginate, Swipe,
SwipeDirection,
},
constant,
display::{self, Color},
geometry::{Insets, Rect},
@ -14,8 +17,7 @@ use crate::{
};
use super::{
theme, Button, ButtonContent, ButtonMsg, ButtonStyleSheet, Loader, LoaderMsg, ScrollBar, Swipe,
SwipeDirection,
theme, Button, ButtonContent, ButtonMsg, ButtonStyleSheet, Loader, LoaderMsg, ScrollBar,
};
use core::cell::Cell;

@ -1,11 +1,14 @@
use crate::ui::{
component::{base::ComponentExt, Component, Event, EventCtx, Pad, PageMsg, Paginate},
component::{
base::ComponentExt, Component, Event, EventCtx, Pad, PageMsg, Paginate, Swipe,
SwipeDirection,
},
display::{self, Color},
geometry::{Axis, Insets, Rect},
shape::Renderer,
};
use super::{theme, ScrollBar, Swipe, SwipeDirection};
use super::{theme, ScrollBar};
use core::cell::Cell;
const SCROLLBAR_HEIGHT: i16 = 18;

@ -1,12 +1,12 @@
use crate::ui::{
component::{Component, Event, EventCtx, Timeout},
component::{Component, Event, EventCtx, Swipe, SwipeDirection, Timeout},
display::{Color, Icon},
geometry::{Alignment2D, Rect},
shape,
shape::Renderer,
};
use super::{theme, Swipe, SwipeDirection};
use super::theme;
/// Component showing status of an operation. Most typically embedded as a
/// content of a Frame and showing success (checkmark with a circle around).

@ -1,161 +0,0 @@
use crate::ui::{
component::{Component, Event, EventCtx},
display,
event::TouchEvent,
geometry::{Point, Rect},
shape::Renderer,
};
use super::theme;
pub use crate::ui::component::SwipeDirection;
#[derive(Clone)]
pub struct Swipe {
pub area: Rect,
pub allow_up: bool,
pub allow_down: bool,
pub allow_left: bool,
pub allow_right: bool,
backlight_start: u16,
backlight_end: u16,
origin: Option<Point>,
}
impl Swipe {
const DISTANCE: i32 = 120;
const THRESHOLD: f32 = 0.3;
pub fn new() -> Self {
Self {
area: Rect::zero(),
allow_up: false,
allow_down: false,
allow_left: false,
allow_right: false,
backlight_start: theme::BACKLIGHT_NORMAL,
backlight_end: theme::BACKLIGHT_NONE,
origin: None,
}
}
pub fn vertical() -> Self {
Self::new().up().down()
}
pub fn horizontal() -> Self {
Self::new().left().right()
}
pub fn up(mut self) -> Self {
self.allow_up = true;
self
}
pub fn down(mut self) -> Self {
self.allow_down = true;
self
}
pub fn left(mut self) -> Self {
self.allow_left = true;
self
}
pub fn right(mut self) -> Self {
self.allow_right = true;
self
}
fn is_active(&self) -> bool {
self.allow_up || self.allow_down || self.allow_left || self.allow_right
}
fn ratio(&self, dist: i16) -> f32 {
(dist as f32 / Self::DISTANCE as f32).min(1.0)
}
fn backlight(&self, ratio: f32) {
let start = self.backlight_start as f32;
let end = self.backlight_end as f32;
let value = start + ratio * (end - start);
display::set_backlight(value as u16);
}
}
impl Component for Swipe {
type Msg = SwipeDirection;
fn place(&mut self, bounds: Rect) -> Rect {
self.area = bounds;
self.area
}
fn event(&mut self, _ctx: &mut EventCtx, event: Event) -> Option<Self::Msg> {
if !self.is_active() {
return None;
}
match (event, self.origin) {
(Event::Touch(TouchEvent::TouchStart(pos)), _) if self.area.contains(pos) => {
// Mark the starting position of this touch.
self.origin.replace(pos);
}
(Event::Touch(TouchEvent::TouchMove(pos)), Some(origin)) => {
// Consider our allowed directions and the touch distance and modify the display
// backlight accordingly.
let ofs = pos - origin;
let abs = ofs.abs();
if abs.x > abs.y && (self.allow_left || self.allow_right) {
// Horizontal direction.
if (ofs.x < 0 && self.allow_left) || (ofs.x > 0 && self.allow_right) {
self.backlight(self.ratio(abs.x));
}
} else if abs.x < abs.y && (self.allow_up || self.allow_down) {
// Vertical direction.
if (ofs.y < 0 && self.allow_up) || (ofs.y > 0 && self.allow_down) {
self.backlight(self.ratio(abs.y));
}
};
}
(Event::Touch(TouchEvent::TouchEnd(pos)), Some(origin)) => {
// Touch interaction is over, reset the position.
self.origin.take();
// Compare the touch distance with our allowed directions and determine if it
// constitutes a valid swipe.
let ofs = pos - origin;
let abs = ofs.abs();
if abs.x > abs.y && (self.allow_left || self.allow_right) {
// Horizontal direction.
if self.ratio(abs.x) >= Self::THRESHOLD {
if ofs.x < 0 && self.allow_left {
return Some(SwipeDirection::Left);
} else if ofs.x > 0 && self.allow_right {
return Some(SwipeDirection::Right);
}
}
} else if abs.x < abs.y && (self.allow_up || self.allow_down) {
// Vertical direction.
if self.ratio(abs.y) >= Self::THRESHOLD {
if ofs.y < 0 && self.allow_up {
return Some(SwipeDirection::Up);
} else if ofs.y > 0 && self.allow_down {
return Some(SwipeDirection::Down);
}
}
};
// Swipe did not happen, reset the backlight.
self.backlight(0.0);
}
_ => {
// Do nothing.
}
}
None
}
fn paint(&mut self) {}
fn render<'s>(&'s self, _target: &mut impl Renderer<'s>) {}
}

@ -1,11 +1,9 @@
use crate::ui::{
component::{Component, Event, EventCtx},
component::{Component, Event, EventCtx, Swipe, SwipeDirection},
geometry::Rect,
shape::Renderer,
};
use super::{Swipe, SwipeDirection};
/// Wrapper component adding "swipe up" handling to `content`.
pub struct SwipeUpScreen<T> {
content: T,

@ -6,7 +6,7 @@ use crate::{
ui::{
component::{
text::paragraphs::{Paragraph, Paragraphs},
ComponentExt,
ComponentExt, SwipeDirection,
},
flow::{base::Decision, flow_store, FlowMsg, FlowState, FlowStore, SwipeFlow, SwipePage},
layout::obj::LayoutObj,
@ -15,7 +15,7 @@ use crate::{
use heapless::Vec;
use super::super::{
component::{Frame, FrameMsg, PromptScreen, ShareWords, SwipeDirection},
component::{Frame, FrameMsg, PromptScreen, ShareWords},
theme,
};

Loading…
Cancel
Save