app: finger scroll for single/multi working

This commit is contained in:
darkfi
2025-09-21 17:50:42 +04:00
parent 5cbd321700
commit fcd0ea3a35
2 changed files with 39 additions and 7 deletions

View File

@@ -53,6 +53,29 @@ pub(super) trait EditorBehavior: Send + Sync {
fn inner_pos(&self) -> Point;
fn allow_endl(&self) -> bool;
fn finger_scroll_dir(&self) -> FingerScrollDir;
}
pub(super) enum FingerScrollDir {
Vert,
Horiz,
}
impl FingerScrollDir {
pub fn cmp(&self, grad: f32) -> bool {
match self {
Self::Vert => grad.abs() > 0.5,
Self::Horiz => grad.abs() < 0.5,
}
}
pub fn travel(&self, start_pos: Point, touch_pos: Point) -> f32 {
match self {
Self::Vert => start_pos.y - touch_pos.y,
Self::Horiz => start_pos.x - touch_pos.x,
}
}
}
pub(super) struct MultiLine {
@@ -196,6 +219,10 @@ impl EditorBehavior for MultiLine {
fn allow_endl(&self) -> bool {
true
}
fn finger_scroll_dir(&self) -> FingerScrollDir {
FingerScrollDir::Vert
}
}
pub(super) struct SingleLine {
@@ -272,4 +299,8 @@ impl EditorBehavior for SingleLine {
fn allow_endl(&self) -> bool {
false
}
fn finger_scroll_dir(&self) -> FingerScrollDir {
FingerScrollDir::Horiz
}
}

View File

@@ -57,7 +57,7 @@ use super::{
mod behave;
pub use behave::BaseEditType;
use behave::{EditorBehavior, MultiLine, SingleLine};
use behave::{EditorBehavior, FingerScrollDir, MultiLine, SingleLine};
/// The travel threshold on long hold select before activating select.
const HOLD_TRAVEL_THRESHOLD_SQ: f32 = 100.;
@@ -85,11 +85,12 @@ enum TouchStateAction {
struct TouchInfo {
state: TouchStateAction,
scroll: PropertyFloat32,
finger_scroll_dir: FingerScrollDir,
}
impl TouchInfo {
fn new(scroll: PropertyFloat32) -> Self {
Self { state: TouchStateAction::Inactive, scroll }
fn new(scroll: PropertyFloat32, finger_scroll_dir: FingerScrollDir) -> Self {
Self { state: TouchStateAction::Inactive, scroll, finger_scroll_dir }
}
fn start(&mut self, pos: Point) {
@@ -115,7 +116,7 @@ impl TouchInfo {
debug!(target: "ui::chatedit::touch", "update touch state: Started -> StartSelect");
self.state = TouchStateAction::StartSelect;
}
} else if grad.abs() > 0.5 {
} else if self.finger_scroll_dir.cmp(grad) {
// Vertical movement
debug!(target: "ui::chatedit::touch", "update touch state: Started -> ScrollVert");
let scroll_start = self.scroll.get();
@@ -387,7 +388,7 @@ impl BaseEdit {
blink_is_paused: AtomicBool::new(false),
hide_cursor: AtomicBool::new(false),
touch_info: SyncMutex::new(TouchInfo::new(scroll)),
touch_info: SyncMutex::new(TouchInfo::new(scroll, behave.finger_scroll_dir())),
is_phone_select: AtomicBool::new(false),
window_scale: window_scale.clone(),
@@ -858,8 +859,8 @@ impl BaseEdit {
self.redraw_select(atom.batch_id).await;
}
TouchStateAction::ScrollVert { start_pos, scroll_start } => {
let y_dist = start_pos.y - touch_pos.y;
let mut scroll = scroll_start + y_dist;
let travel_dist = self.behave.finger_scroll_dir().travel(*start_pos, touch_pos);
let mut scroll = scroll_start + travel_dist;
scroll = scroll.clamp(0., self.behave.max_scroll().await);
if (self.scroll.get() - scroll).abs() < VERT_SCROLL_UPDATE_INC {
return true