Преглед изворни кода

app: on android use system long press timeout otherwise 400ms consistently for long holds everywhere

x пре 2 дана
родитељ
комит
572ddeba22

+ 5 - 0
bin/app/java/MainActivity.java

@@ -1,5 +1,6 @@
 //% IMPORTS
 
+import android.view.ViewConfiguration;
 import android.view.ViewGroup;
 import android.view.WindowInsets.Type;
 import android.view.inputmethod.EditorInfo;
@@ -165,6 +166,10 @@ public VideoDecoder createVideoDecoder() {
     return decoder;
 }
 
+public int getLongPressTimeout() {
+    return ViewConfiguration.getLongPressTimeout();
+}
+
 public void openUrl(String url) {
     try {
         Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

+ 4 - 0
bin/app/src/android/mod.rs

@@ -69,6 +69,10 @@ pub fn get_keyboard_height() -> usize {
     call_mainactivity_int_method!("getKeyboardHeight", "()I") as usize
 }
 
+pub fn get_long_press_timeout() -> u32 {
+    call_mainactivity_int_method!("getLongPressTimeout", "()I") as u32
+}
+
 pub fn is_ime_visible() -> bool {
     call_mainactivity_bool_method!("isImeVisible")
 }

+ 0 - 5
bin/app/src/app/node.rs

@@ -614,11 +614,6 @@ pub fn create_chatview(name: &str) -> SceneNode {
     prop.set_defaults_f32(vec![0.9]).unwrap();
     node.add_property(prop).unwrap();
 
-    let mut prop = Property::new("select_hold_time", PropertyType::Float32, PropertySubType::Pixel);
-    prop.set_ui_text("Select Holding Time", "How long to hard press for selecting lines (ms)");
-    prop.set_defaults_f32(vec![1000.]).unwrap();
-    node.add_property(prop).unwrap();
-
     let mut prop = Property::new("key_scroll_speed", PropertyType::Float32, PropertySubType::Pixel);
     prop.set_ui_text("Page Up/Down Scroll Speed", "Scroll speed when pressing page up/down");
     prop.set_defaults_f32(vec![6.]).unwrap();

+ 3 - 6
bin/app/src/ui/chatview/mod.rs

@@ -16,6 +16,8 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+use super::long_press_timeout;
+
 use async_lock::Mutex as AsyncMutex;
 use async_trait::async_trait;
 use atomic_float::AtomicF32;
@@ -238,7 +240,6 @@ pub struct ChatView {
 
     scroll_start_accel: PropertyFloat32,
     scroll_resist: PropertyFloat32,
-    select_hold_time: PropertyFloat32,
     key_scroll_speed: PropertyFloat32,
 
     /// Scroll accel
@@ -344,8 +345,6 @@ impl ChatView {
             PropertyFloat32::wrap(node_ref, Role::Internal, "scroll_start_accel", 0).unwrap();
         let scroll_resist =
             PropertyFloat32::wrap(node_ref, Role::Internal, "scroll_resist", 0).unwrap();
-        let select_hold_time =
-            PropertyFloat32::wrap(node_ref, Role::Internal, "select_hold_time", 0).unwrap();
         let key_scroll_speed =
             PropertyFloat32::wrap(node_ref, Role::Internal, "key_scroll_speed", 0).unwrap();
 
@@ -391,7 +390,6 @@ impl ChatView {
 
             scroll_start_accel,
             scroll_resist,
-            select_hold_time,
             key_scroll_speed,
 
             motion_cv,
@@ -1453,7 +1451,7 @@ impl UIObject for ChatView {
             return false
         }
 
-        let select_hold_time = self.select_hold_time.get();
+        let hold_ms = long_press_timeout() as u64;
 
         // Simulate mouse events
         match phase {
@@ -1464,7 +1462,6 @@ impl UIObject for ChatView {
 
                 // Arm the long-press timer for text selection.
                 let version = self.touch_hold_version.fetch_add(1, Ordering::SeqCst) + 1;
-                let hold_ms = select_hold_time as u64;
                 let me = self.me.clone();
                 let ex = self.ex.clone();
                 let start_pos = touch_pos;

+ 2 - 3
bin/app/src/ui/edit/mod.rs

@@ -16,6 +16,7 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+use super::long_press_timeout;
 use async_trait::async_trait;
 use atomic_float::AtomicF32;
 use darkfi::system::msleep;
@@ -69,8 +70,6 @@ use repeat::{PressedKey, PressedKeysSmoothRepeat};
 
 /// The travel threshold on long hold select before activating select.
 const HOLD_TRAVEL_THRESHOLD_SQ: f32 = 100.;
-/// How long to hold before select is enabled in ms.
-const HOLD_ENABLE_TIME: u128 = 500;
 
 /// Minimum dist to update scroll when finger scrolling.
 /// Avoid updating too much makes scrolling smoother.
@@ -139,7 +138,7 @@ impl TouchInfo {
                 //debug!(target: "ui::chatedit::touch", "TouchInfo::update() [travel_dist_sq={travel_dist_sq}, grad={grad}]");
 
                 if travel_dist_sq < HOLD_TRAVEL_THRESHOLD_SQ {
-                    if elapsed > HOLD_ENABLE_TIME {
+                    if elapsed > long_press_timeout() as u128 {
                         debug!(target: "ui::chatedit::touch", "update touch state: Started -> StartSelect");
                         self.state = TouchStateAction::StartSelect;
                     }

+ 3 - 2
bin/app/src/ui/menu/mod.rs

@@ -16,6 +16,7 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+use super::long_press_timeout;
 use async_trait::async_trait;
 use atomic_float::AtomicF32;
 use darkfi::system::CondVar;
@@ -290,7 +291,7 @@ impl Menu {
         is_long_press_tap: bool,
         elapsed_ms: u128,
     ) {
-        let is_long_press = is_long_press_tap && elapsed_ms >= 500;
+        let is_long_press = is_long_press_tap && elapsed_ms >= long_press_timeout() as u128;
 
         if is_long_press {
             if !self.is_edit_mode.load(Ordering::Relaxed) {
@@ -774,7 +775,7 @@ impl UIObject for Menu {
 
         let ex = self.ex.clone();
         let long_press_task = ex.spawn(async move {
-            darkfi::system::msleep(500).await;
+            darkfi::system::msleep(long_press_timeout() as u64).await;
 
             let Some(arc_self) = me.upgrade() else { return };
             let current_mouse_pos = arc_self.mouse_pos.lock().clone();

+ 19 - 1
bin/app/src/ui/mod.rs

@@ -19,7 +19,7 @@
 use async_trait::async_trait;
 use futures::stream::{FuturesUnordered, StreamExt};
 use miniquad::{KeyCode, KeyMods, MouseButton, TouchPhase};
-use std::sync::{Arc, Weak};
+use std::sync::{Arc, OnceLock, Weak};
 
 use crate::{
     gfx::{DrawCall, Point, Rectangle},
@@ -29,6 +29,24 @@ use crate::{
     ExecutorPtr,
 };
 
+static LONG_PRESS_TIMEOUT: OnceLock<u32> = OnceLock::new();
+
+/// The system long-press timeout in milliseconds. Queried once from
+/// `ViewConfiguration.getLongPressTimeout()` on Android, defaults to 400
+/// on other platforms.
+pub fn long_press_timeout() -> u32 {
+    *LONG_PRESS_TIMEOUT.get_or_init(|| {
+        #[cfg(target_os = "android")]
+        {
+            crate::android::get_long_press_timeout()
+        }
+        #[cfg(not(target_os = "android"))]
+        {
+            400
+        }
+    })
+}
+
 mod button;
 pub use button::{Button, ButtonPtr};
 pub mod chatview;