diff --git a/gallery/src/lib.rs b/gallery/src/lib.rs index f37e2f3..2e29165 100644 --- a/gallery/src/lib.rs +++ b/gallery/src/lib.rs @@ -895,11 +895,31 @@ fn Scrolled() -> Element { const HEIGHT: f64 = 160.0; let mut scroll = use_signal(|| unit::scroll::Scroll::new((0.0, HEIGHT), (0.0, LINES as f64 * LINE))); + // Where a finger or a held button last was, while it drags the lines. + let mut dragging = use_signal(|| None::); let s = scroll(); let top = -s.y; rsx! { div { class: "gallery-scrolled", - style: "position: relative; height: {HEIGHT}px; overflow: hidden;", + // A finger here is the box's to scroll, not the page's. + style: "position: relative; height: {HEIGHT}px; overflow: hidden; touch-action: none;", + onpointerdown: move |e: PointerEvent| dragging.set(Some(e.client_coordinates().y)), + onpointermove: move |e: PointerEvent| { + let Some(was) = *dragging.peek() else { + return; + }; + if e.held_buttons().is_empty() { + dragging.set(None); + return; + } + let y = e.client_coordinates().y; + dragging.set(Some(y)); + let mut s = scroll.write(); + s.y -= y - was; + s.clamp(); + }, + onpointerup: move |_| dragging.set(None), + onpointercancel: move |_| dragging.set(None), onwheel: move |e: WheelEvent| { // The box's own: the page under it does not scroll too. e.prevent_default(); diff --git a/src/components/collection.rs b/src/components/collection.rs index 5f99193..3ccc159 100644 --- a/src/components/collection.rs +++ b/src/components/collection.rs @@ -595,6 +595,10 @@ fn TileView( if chooser.has_menu() { e.prevent_default(); e.stop_propagation(); + // The browser's own long press, after ours opened it. + if held.held() { + return; + } let p = e.client_coordinates(); chooser.menu(unit, &key, (p.x, p.y)); } @@ -941,6 +945,10 @@ fn RowView( if chooser.has_menu() { e.prevent_default(); e.stop_propagation(); + // The browser's own long press, after ours opened it. + if held.held() { + return; + } let p = e.client_coordinates(); chooser.menu(unit, &key, (p.x, p.y)); } diff --git a/src/components/hold.rs b/src/components/hold.rs index 58b3354..16a165b 100644 --- a/src/components/hold.rs +++ b/src/components/hold.rs @@ -3,11 +3,13 @@ //! //! unit keeps no clock of its own, so it cannot tell by itself when the //! finger has been down long enough. A host gives it one with -//! [`Unit::set_timer`]: a sleep, its own runtime's (tokio's, the -//! browser's). With it, the hold fires while the finger is still down, as -//! a phone's does; without one, it fires when the finger lifts after -//! [`HOLD`] or more, which a page in a browser seldom needs, since a -//! browser sends a long press as `contextmenu` itself. +//! [`Unit::set_timer`]: a sleep, its own runtime's (tokio's). In a browser +//! with none, unit takes the browser's own timer: some browsers send a +//! long press as `contextmenu` (Chrome on Android), and iOS Safari does +//! not, selecting the text under the finger instead. With a clock the hold +//! fires while the finger is still down, as a phone's does; without one, +//! it fires when the finger lifts after [`HOLD`] or more. A `contextmenu` +//! that follows a hold already fired opens nothing more ([`Hold::held`]). //! //! Only a finger or a pen holds: the mouse's long press is its secondary //! button. A finger that moves more than [`SLOP`] is scrolling, not @@ -101,7 +103,7 @@ impl Hold { self.press.clone().set(press); #[cfg(not(target_arch = "wasm32"))] self.since.clone().set(Some(std::time::Instant::now())); - let sleep = self.unit.timer.peek().clone(); + let sleep = self.unit.sleep(); if let Some(sleep) = sleep { let me = *self; spawn(async move { @@ -135,7 +137,7 @@ impl Hold { { let since = self.since.peek().to_owned(); if let (Some(at), Some(t)) = (at, since) - && self.unit.timer.peek().is_none() + && self.unit.sleep().is_none() && t.elapsed() >= HOLD { self.fire(at); @@ -145,6 +147,12 @@ impl Hold { let _ = at; } + /// Whether this press has already fired as a hold: a `contextmenu` the + /// browser sends for the same long press opens nothing more. + pub fn held(&self) -> bool { + *self.held.peek() + } + /// Whether the press just ended was a hold, so the click it sends is /// not a press: asked once, by the click. pub fn take_held(&self) -> bool { @@ -164,6 +172,12 @@ pub fn LongPress( let hold = use_hold(move |at| onhold.call(at)); rsx! { div { class: "unit-hold {class}", + // A browser's own long press, once ours has fired: nothing more. + oncontextmenu: move |e: MouseEvent| { + if hold.held() { + e.prevent_default(); + } + }, onpointerdown: move |e: PointerEvent| hold.down(&e), onpointermove: move |e: PointerEvent| hold.moved(&e), onpointerup: move |e: PointerEvent| hold.up(&e), diff --git a/src/components/menus.rs b/src/components/menus.rs index ffd408a..8961607 100644 --- a/src/components/menus.rs +++ b/src/components/menus.rs @@ -293,6 +293,10 @@ pub fn ContextMenu( oncontextmenu: move |e: MouseEvent| { e.prevent_default(); e.stop_propagation(); + // The browser's own long press, after ours opened it. + if hold.held() { + return; + } let p = e.client_coordinates(); open((p.x, p.y), entries.clone()); }, diff --git a/src/components/slider.rs b/src/components/slider.rs index 1cd1397..b28a423 100644 --- a/src/components/slider.rs +++ b/src/components/slider.rs @@ -118,6 +118,12 @@ pub fn Slider( onchange.call(v); } }; + // Where the slider is on the page, read as it is pressed: a finger in + // a browser sends every move to the stop it went down on (iOS Safari + // and Chrome both capture a touch), so the stops alone would never see + // it travel. A move anywhere is read against the rail instead. + let mut mounted = use_signal(|| None::>); + let mut placed = use_signal(|| None::<(f64, f64)>); let off = if disabled { " off" } else { "" }; rsx! { div { @@ -130,7 +136,29 @@ pub fn Slider( "aria-valuemax": "{max}", "aria-valuetext": text, "aria-disabled": disabled.then_some("true"), - onpointerdown: move |_| focus.press(), + onmounted: move |e: MountedEvent| mounted.set(Some(e.data())), + onpointerdown: move |_| { + focus.press(); + if let Some(m) = mounted.peek().clone() { + spawn(async move { + if let Ok(r) = m.get_client_rect().await { + placed.set(Some((r.origin.x, r.size.width))); + } + }); + } + }, + // A press held and moved: the stop under it along the rail, + // which is in by half a thumb (8px) at each end. + onpointermove: move |e: PointerEvent| { + let Some((left, width)) = *placed.peek() else { + return; + }; + if e.held_buttons().is_empty() || width <= 16.0 { + return; + } + let along = ((e.client_coordinates().x - left - 8.0) / (width - 16.0)).clamp(0.0, 1.0); + set((along * n as f64).round() as usize); + }, onfocus: move |_| focus.dom_focus(), onblur: move |_| focus.dom_blur(), div { class: "unit-slider-rail", "aria-hidden": "true", diff --git a/src/style.rs b/src/style.rs index e767a6a..7c708fe 100644 --- a/src/style.rs +++ b/src/style.rs @@ -276,7 +276,8 @@ pub const CSS: &str = r#" /* Slider: the thumb's and the stops' room inside its own box */ .unit-scrollbar:hover .unit-scrollbar-thumb { opacity: 0.85 !important; } -.unit-slider { position: relative; height: var(--unit-control-height); min-width: 120px; width: 100%; } +.unit-slider { position: relative; height: var(--unit-control-height); min-width: 120px; width: 100%; + touch-action: pan-y; } .unit-slider-rail { position: absolute; left: 8px; right: 8px; top: 0; bottom: 0; } .unit-slider-line { position: absolute; left: 0; right: 0; top: 50%; height: 4px; margin-top: -2px; border-radius: 2px; @@ -602,6 +603,13 @@ pub const CSS: &str = r#" text-align: center; color: var(--unit-text); background: linear-gradient(rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.6)); } +/* What a finger holds or drags is not text to select: a long press opens + a menu, and on iOS Safari would otherwise select the words under it and + show the system's callout. */ +.unit-hold, .unit-context, .unit-tile, .unit-tr, .unit-slider, .unit-switch, .unit-segmented, +.unit-chip, .unit-button, .unit-stepper { + -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; } + /* Motion: every duration and curve is the theme's (`Motion`), and 0 where the page asks for less motion or is drawn in a terminal's cells, so nothing here moves there. Blitz runs these as a browser does. diff --git a/src/unit.rs b/src/unit.rs index f78f432..9c2bda5 100644 --- a/src/unit.rs +++ b/src/unit.rs @@ -374,7 +374,7 @@ impl Unit { /// no way to wait (no host's sleep, and not a browser), so it goes at /// once. pub(crate) fn exit_time(&self) -> Option { - let can_wait = self.timer.peek().is_some() || cfg!(target_arch = "wasm32"); + let can_wait = self.sleep().is_some(); let ms = self .theme .peek() @@ -382,14 +382,24 @@ impl Unit { (can_wait && ms > 0.0).then(|| Duration::from_millis(ms as u64)) } - /// Do `then` once `time` has passed: by the host's sleep, or in a - /// browser by its own timer. + /// How the page waits: the host's sleep ([`Unit::set_timer`]), or in a + /// browser its own timer; `None` where neither is, and nothing can be + /// timed. + pub(crate) fn sleep(&self) -> Option { + let host = self.timer.peek().clone(); + if host.is_some() || !cfg!(target_arch = "wasm32") { + return host; + } + Some(Rc::new(|time| Box::pin(browser_sleep(time)))) + } + + /// Do `then` once `time` has passed, where the page can wait; at once + /// where it cannot. pub(crate) fn after(&self, time: Duration, then: impl FnOnce() + 'static) { - let sleep = self.timer.peek().clone(); + let sleep = self.sleep(); dioxus::core::spawn_forever(async move { - match sleep { - Some(sleep) => sleep(time).await, - None => browser_sleep(time).await, + if let Some(sleep) = sleep { + sleep(time).await; } then(); });