207 lines
5.9 KiB
C++
207 lines
5.9 KiB
C++
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
||
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
module Nostro.Ui:Gesture_impl;
|
||
|
|
import std;
|
||
|
|
import :Gesture;
|
||
|
|
import :Geometry;
|
||
|
|
|
||
|
|
using namespace Nostro::Ui;
|
||
|
|
|
||
|
|
GestureEvent GestureDetector::LongPressed() {
|
||
|
|
longPressed = true;
|
||
|
|
GestureEvent event;
|
||
|
|
event.kind = GestureEvent::Kind::LongPress;
|
||
|
|
event.at = down;
|
||
|
|
return event;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Keeps the last 100 ms of movement: a fling's speed is the finger's as it left, not as it travelled.
|
||
|
|
void GestureDetector::Forget(std::int64_t tMs) {
|
||
|
|
while(samples.size() > 1 && tMs - samples.front().tMs > 100) {
|
||
|
|
samples.erase(samples.begin());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
GestureEvent GestureDetector::Feed(PointerPhase phase, Point p, std::int64_t tMs) {
|
||
|
|
GestureEvent event;
|
||
|
|
if(phase == PointerPhase::Down) {
|
||
|
|
pressed = true;
|
||
|
|
dragging = false;
|
||
|
|
longPressed = false;
|
||
|
|
down = p;
|
||
|
|
downAt = tMs;
|
||
|
|
last = p;
|
||
|
|
samples.clear();
|
||
|
|
samples.push_back({p, tMs});
|
||
|
|
return event;
|
||
|
|
}
|
||
|
|
if(phase == PointerPhase::Cancel) {
|
||
|
|
pressed = false;
|
||
|
|
dragging = false;
|
||
|
|
longPressed = false;
|
||
|
|
return event;
|
||
|
|
}
|
||
|
|
if(!pressed) {
|
||
|
|
return event;
|
||
|
|
}
|
||
|
|
// Held past the long-press time with no Timeout yet, the events of a stall having been dispatched before the timer.
|
||
|
|
const bool overdue = longPress && !dragging && !longPressed && tMs - downAt >= longPressMs;
|
||
|
|
if(phase == PointerPhase::Move) {
|
||
|
|
if(overdue) {
|
||
|
|
// The long press, before any movement is read.
|
||
|
|
return LongPressed();
|
||
|
|
}
|
||
|
|
if(!dragging && !longPressed && (std::abs(p.x - down.x) > slopPx || std::abs(p.y - down.y) > slopPx)) {
|
||
|
|
dragging = true;
|
||
|
|
event.kind = GestureEvent::Kind::DragStart;
|
||
|
|
event.at = down;
|
||
|
|
event.delta = {p.x - down.x, p.y - down.y};
|
||
|
|
} else if(dragging) {
|
||
|
|
event.kind = GestureEvent::Kind::Drag;
|
||
|
|
event.at = p;
|
||
|
|
event.delta = {p.x - last.x, p.y - last.y};
|
||
|
|
}
|
||
|
|
last = p;
|
||
|
|
samples.push_back({p, tMs});
|
||
|
|
Forget(tMs);
|
||
|
|
return event;
|
||
|
|
}
|
||
|
|
// The release.
|
||
|
|
pressed = false;
|
||
|
|
if(dragging) {
|
||
|
|
dragging = false;
|
||
|
|
// A finger held still before letting go has no speed, whatever it did before: no event comes while it is still.
|
||
|
|
Forget(tMs);
|
||
|
|
const std::int64_t dt = tMs - samples.front().tMs;
|
||
|
|
Point velocity;
|
||
|
|
if(dt > 0) {
|
||
|
|
velocity = {(p.x - samples.front().p.x) * 1000.0f / static_cast<float>(dt), (p.y - samples.front().p.y) * 1000.0f / static_cast<float>(dt)};
|
||
|
|
}
|
||
|
|
event.at = p;
|
||
|
|
if(std::hypot(velocity.x, velocity.y) >= flingMinPxPerS) {
|
||
|
|
event.kind = GestureEvent::Kind::Fling;
|
||
|
|
event.velocity = velocity;
|
||
|
|
} else {
|
||
|
|
event.kind = GestureEvent::Kind::Release;
|
||
|
|
}
|
||
|
|
return event;
|
||
|
|
}
|
||
|
|
if(overdue) {
|
||
|
|
// The long press, at the release.
|
||
|
|
return LongPressed();
|
||
|
|
}
|
||
|
|
if(longPressed) {
|
||
|
|
event.kind = GestureEvent::Kind::Release;
|
||
|
|
event.at = p;
|
||
|
|
return event;
|
||
|
|
}
|
||
|
|
event.kind = GestureEvent::Kind::Tap;
|
||
|
|
event.at = down;
|
||
|
|
return event;
|
||
|
|
}
|
||
|
|
|
||
|
|
GestureEvent GestureDetector::Timeout(std::int64_t tMs) {
|
||
|
|
if(!longPress || !pressed || dragging || longPressed || tMs - downAt < longPressMs) {
|
||
|
|
return GestureEvent();
|
||
|
|
}
|
||
|
|
return LongPressed();
|
||
|
|
}
|
||
|
|
|
||
|
|
bool GestureDetector::Pressed() const noexcept {
|
||
|
|
return pressed;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool GestureDetector::Dragging() const noexcept {
|
||
|
|
return dragging;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::optional<std::int64_t> GestureDetector::LongPressDue() const noexcept {
|
||
|
|
if(!longPress || !pressed || dragging || longPressed) {
|
||
|
|
return std::nullopt;
|
||
|
|
}
|
||
|
|
return downAt + longPressMs;
|
||
|
|
}
|
||
|
|
|
||
|
|
float ScrollState::Max() const noexcept {
|
||
|
|
return std::max(0.0f, content - viewport);
|
||
|
|
}
|
||
|
|
|
||
|
|
void ScrollState::Clamp() noexcept {
|
||
|
|
offset = std::clamp(offset, 0.0f, Max());
|
||
|
|
}
|
||
|
|
|
||
|
|
void ScrollState::ScrollBy(float dy) noexcept {
|
||
|
|
offset += dy;
|
||
|
|
velocity = 0;
|
||
|
|
Clamp();
|
||
|
|
}
|
||
|
|
|
||
|
|
void ScrollState::Fling(float pxPerS) noexcept {
|
||
|
|
velocity = pxPerS;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool ScrollState::Tick(float dt) noexcept {
|
||
|
|
if(std::abs(velocity) < 20.0f) {
|
||
|
|
velocity = 0;
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
const float before = offset;
|
||
|
|
offset += velocity * dt;
|
||
|
|
Clamp();
|
||
|
|
// Stopped by an end of the content, or going nowhere.
|
||
|
|
if(offset == before || offset == 0.0f || offset == Max()) {
|
||
|
|
velocity = 0;
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
velocity *= std::exp(-4.0f * dt);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool ScrollState::EnsureVisible(float top, float bottom, float margin) noexcept {
|
||
|
|
const float before = offset;
|
||
|
|
if(bottom + margin > offset + viewport) {
|
||
|
|
offset = bottom + margin - viewport;
|
||
|
|
}
|
||
|
|
// The top wins where the span is taller than the room: that is where reading starts.
|
||
|
|
if(top - margin < offset) {
|
||
|
|
offset = top - margin;
|
||
|
|
}
|
||
|
|
Clamp();
|
||
|
|
if(offset == before) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
velocity = 0;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
std::int64_t EventClock::At(std::uint32_t stamp, std::int64_t hostNowMs) noexcept {
|
||
|
|
if(stamp == 0) {
|
||
|
|
// The one millisecond in 2^32 that reads 0, and an event the compositor gave no time.
|
||
|
|
return hostNowMs;
|
||
|
|
}
|
||
|
|
// The difference of two stamps as a signed number takes the wrap in its stride.
|
||
|
|
unwrapped = lastStamp == 0 ? stamp : unwrapped + static_cast<std::int32_t>(stamp - lastStamp);
|
||
|
|
lastStamp = stamp;
|
||
|
|
ahead = ahead ? std::min(*ahead, hostNowMs - unwrapped) : hostNowMs - unwrapped;
|
||
|
|
return unwrapped + *ahead;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool FrameWait::Again(std::uint32_t waiting) noexcept {
|
||
|
|
if(waiting == 0) {
|
||
|
|
Reset();
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
if(last == 0 || waiting != last) {
|
||
|
|
gainless = 0;
|
||
|
|
} else {
|
||
|
|
gainless++;
|
||
|
|
}
|
||
|
|
last = waiting;
|
||
|
|
return gainless < patience;
|
||
|
|
}
|
||
|
|
|
||
|
|
void FrameWait::Reset() noexcept {
|
||
|
|
last = 0;
|
||
|
|
gainless = 0;
|
||
|
|
}
|