From 5f107299e3891eedd70da15498689110eaa6ca03 Mon Sep 17 00:00:00 2001 From: spinline Date: Sun, 8 Feb 2026 19:53:10 +0300 Subject: [PATCH] =?UTF-8?q?refactor:=20long=20press=20mant=C4=B1=C4=9F?= =?UTF-8?q?=C4=B1=20leptos-use::use=5Ftimeout=5Ffn=20ile=20modernize=20edi?= =?UTF-8?q?ldi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/Cargo.toml | 1 + frontend/src/components/torrent/table.rs | 67 +++++++++++------------- 2 files changed, 32 insertions(+), 36 deletions(-) diff --git a/frontend/Cargo.toml b/frontend/Cargo.toml index 4507e3f..d9f1b79 100644 --- a/frontend/Cargo.toml +++ b/frontend/Cargo.toml @@ -54,3 +54,4 @@ tailwind_fuse = "0.3.2" js-sys = "0.3.85" base64 = "0.22.1" serde-wasm-bindgen = "0.6.5" +leptos-use = "0.13" diff --git a/frontend/src/components/torrent/table.rs b/frontend/src/components/torrent/table.rs index fd63e9b..1ad7ef9 100644 --- a/frontend/src/components/torrent/table.rs +++ b/frontend/src/components/torrent/table.rs @@ -1,4 +1,5 @@ use leptos::*; +use leptos_use::use_timeout_fn; use crate::store::{get_action_messages, show_toast_with_signal}; use shared::NotificationLevel; @@ -421,50 +422,44 @@ pub fn TorrentTable() -> impl IntoView { let _t_hash = t.hash.clone(); let t_hash_click = t.hash.clone(); - let (timer_handle, set_timer_handle) = create_signal(Option::::None); let t_hash_long = t.hash.clone(); + let leptos_use::UseTimeoutFnReturn { start, stop, .. } = use_timeout_fn( + move |pos: (i32, i32)| { + set_menu_position.set(pos); + set_selected_hash.set(Some(t_hash_long.clone())); + set_menu_visible.set(true); - let clear_long_press_timer = move || { - if let Some(handle) = timer_handle.get_untracked() { - handle.clear(); - set_timer_handle.set(None); - } - }; + // Haptic feedback + let navigator = window().navigator(); + if let Ok(vibrate) = js_sys::Reflect::get(&navigator, &"vibrate".into()) { + if vibrate.is_function() { + let _ = navigator.vibrate_with_duration(50); + } + } + }, + 600.0, + ); let handle_touchstart = { - let t_hash = t_hash_long.clone(); - move |e: web_sys::TouchEvent| { - clear_long_press_timer(); + let start = start.clone(); + move |e: web_sys::TouchEvent| { if let Some(touch) = e.touches().get(0) { - let x = touch.client_x(); - let y = touch.client_y(); - let hash = t_hash.clone(); - - // Use Leptos set_timeout: cleaner, safer, no manual Closure needed - let handle = set_timeout_with_handle(move || { - set_menu_position.set((x, y)); - set_selected_hash.set(Some(hash.clone())); - set_menu_visible.set(true); - - // Haptic feedback - let navigator = window().navigator(); - if let Ok(vibrate) = js_sys::Reflect::get(&navigator, &"vibrate".into()) { - if vibrate.is_function() { - let _ = navigator.vibrate_with_duration(50); - } - } - set_timer_handle.set(None); - }, std::time::Duration::from_millis(600)); - - if let Ok(h) = handle { - set_timer_handle.set(Some(h)); - } + start((touch.client_x(), touch.client_y())); } } }; - let handle_touchmove = move |_| clear_long_press_timer(); - let handle_touchend = move |_| clear_long_press_timer(); + let handle_touchmove = { + let stop = stop.clone(); + move |_| stop() + }; + + let handle_touchend = { + let stop = stop.clone(); + move |_| stop() + }; + + let handle_touchcancel = move |_| stop(); view! {
impl IntoView { on:touchstart=handle_touchstart on:touchmove=handle_touchmove on:touchend=handle_touchend - on:touchcancel=handle_touchend + on:touchcancel=handle_touchcancel >