Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions crates/core/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ pub struct Pointer {
/// Secondary button last tick. Menu is the press edge, not held state.
was_secondary_held: bool,
/// Where the press began, so travel is measured from it rather than from
/// the previous tick — a slow drag never moves far in 16ms.
/// the previous tick. A slow drag never moves far in 16ms.
pressed_at: Point,
/// Where the platform saw the button go down, if that was not the cursor
/// this tick. Consumed by the press that starts a gesture.
press_origin: Option<Point>,
pressed_ms: u32,
/// `None` until the first tick. Assuming an origin would make the first
/// tick's travel the whole cursor position, and a Grab that began inside
Expand Down Expand Up @@ -92,6 +95,7 @@ impl Pointer {
was_held: false,
was_secondary_held: false,
pressed_at: Point::default(),
press_origin: None,
pressed_ms: 0,
cursor: None,
travel: Point::default(),
Expand Down Expand Up @@ -134,6 +138,9 @@ impl Pointer {

let pressed = held && !self.was_held;
self.was_held = held;
// Taken even when this tick does not start a gesture, so a miss
// cannot aim the next press.
let began = self.press_origin.take();

let secondary_pressed = secondary_held && !self.was_secondary_held;
self.was_secondary_held = secondary_held;
Expand All @@ -149,7 +156,7 @@ impl Pointer {
// already down when the cursor arrives was never pressed here.
(Phase::Idle, true) if over_sprite && pressed => {
self.phase = Phase::Pressed;
self.pressed_at = cursor;
self.pressed_at = began.unwrap_or(cursor);
self.pressed_ms = 0;
Vec::new()
}
Expand Down Expand Up @@ -235,6 +242,12 @@ impl Pointer {
self.phase != Phase::Idle
}

/// Where the button went down, when the platform saw that point and the
/// cursor has since moved. The next press measures its travel from here.
pub fn began_at(&mut self, at: Point) {
self.press_origin = Some(at);
}

/// Where the press under way landed, or `None` with no press under way.
/// The first Grab can arrive after the cursor has already moved off it.
pub fn pressed_at(&self) -> Option<Point> {
Expand Down Expand Up @@ -403,6 +416,24 @@ mod tests {
assert_eq!(click(&mut pointer), vec![Verb::Poke]);
}

/// The cursor can already be off the sprite when the tick that sees the
/// press runs. Travel still starts where the button went down.
#[test]
fn a_drag_that_finished_before_the_tick_is_a_grab_on_the_next_one() {
let mut pointer = Pointer::default();
pointer.began_at(at(100.0, 100.0));
assert!(
pointer
.update(true, true, false, at(140.0, 100.0), TICK)
.is_empty(),
"the press tick records the gesture"
);
assert_eq!(
pointer.update(true, true, false, at(140.0, 100.0), TICK),
vec![Verb::Grab]
);
}

#[test]
fn a_click_on_the_sprite_is_a_poke_and_never_moves_it() {
let mut pointer = Pointer::default();
Expand Down
4 changes: 4 additions & 0 deletions scripts/scenarios/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ unless Chat is open.

## Windows and X11

`quick-grab.win.ps1` presses on a resting sprite and drags away immediately,
without waiting for a hit-test flip. It asserts that the recorded press remains
on the sprite after the cursor leaves and that the gesture becomes a Grab.

`thinking-row`, `chat-header-narrow`, `landing-link-click`,
`launcher-dies-at-startup`, `sign-in-button`, `question-bubble`,
`control-click-menu` and `poke-mid-climb` each have `.x11.sh` and `.win.ps1`
Expand Down
179 changes: 179 additions & 0 deletions scripts/scenarios/ci/quick-grab.win.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
#!/usr/bin/env pwsh
# Scenario: quick-grab (Windows)
# On screen: launches Fidget with the Static Director. The sprite comes to
# rest, the cursor parks away from it, then a press on the sprite starts a
# drag that leaves the sprite before the next tick. Fidget quits when the
# scenario ends. No screenshots.
# Input: SetCursorPos and a real button-down. The cursor moves. Keep hands
# off the mouse for the run.
# Duration: a few seconds once the sprite is up, 30 s at most.
# Grants: a desktop session.
# Asserts: the click log's press point is on the sprite while the cursor the
# tick sampled has already left, and that press starts a Grab. It does not
# wait for the hit-test flip line.
# Fixture: none.
#
# Usage: quick-grab.win.ps1 --go <fidget binary>
# Without --go it prints this header, which is the takeover prompt, and exits 2.
# No param() block: Windows PowerShell binds `--go` as -Go and swallows the
# next token, so $Go never equals "--go" and the leaf always exits 2.
$Go = if ($args.Count -gt 0) { $args[0] } else { "" }
$Bin = if ($args.Count -gt 1) { $args[1] } else { "" }
$ErrorActionPreference = "Stop"

function Show-Header {
$lines = Get-Content -LiteralPath $PSCommandPath
$header = @()
foreach ($line in $lines | Select-Object -Skip 1) {
if ($line.StartsWith("#")) {
$header += ($line -replace '^# ?', '')
} elseif ($line.Trim().Length -gt 0) {
break
}
}
$header -join "`n"
}

if ($Go -ne "--go") {
Show-Header
exit 2
}
if (-not $Bin) {
Write-Error "usage: quick-grab.win.ps1 --go <fidget binary>"
exit 1
}

$root = (Resolve-Path (Join-Path $PSScriptRoot "../../..")).Path
. (Join-Path $PSScriptRoot "..\helpers\wait-helpers.win.ps1")
$script:Evidence = $null

function Fail([string]$Message) {
[Console]::Error.WriteLine("FAIL: $Message")
if ($script:Evidence) { [Console]::Error.WriteLine("evidence in $($script:Evidence)") }
exit 1
}

# process.log puts epoch seconds before each line.
$clickPattern = 'click: down .* cursor=\([^)]+\)->\((-?\d+),(-?\d+)\) press=\((-?\d+),(-?\d+)\)'
$grabPattern = '^(?:\d+ )?verbs: .*\[Grab\]'
$framePattern = '^(?:\d+ )?frame: '

function Near([int]$X, [int]$Y, [int]$ToX, [int]$ToY, [int]$Slop) {
return ([Math]::Abs($X - $ToX) -le $Slop) -and ([Math]::Abs($Y - $ToY) -le $Slop)
}

# Center of the sprite, and a point more than 48px off it, kept on screen.
function Get-Aim($Spot, [int]$Half, [int]$Size) {
$farY = $Spot.Y - (2 * $Size)
if ($farY -lt 0) { $farY = $Spot.Y + (2 * $Size) }
[pscustomobject]@{
CenterX = $Spot.X + $Half
CenterY = $Spot.Y + $Half
FarX = $Spot.X
FarY = $farY
}
}

function Last-ClickDown([string]$File) {
$line = Get-Content -LiteralPath $File | Where-Object { $_ -match $clickPattern } | Select-Object -Last 1
if (-not $line -or $line -notmatch $clickPattern) { return $null }
[pscustomobject]@{
CursorX = [int]$Matches[1]
CursorY = [int]$Matches[2]
PressX = [int]$Matches[3]
PressY = [int]$Matches[4]
}
}

Add-Type @"
using System;
using System.Runtime.InteropServices;
public class QuickGrab {
[DllImport("user32.dll")] static extern bool SetCursorPos(int x, int y);
[DllImport("user32.dll")] static extern void mouse_event(uint flags, int dx, int dy, uint data, UIntPtr extra);
const uint Down = 0x0002, Up = 0x0004;
public static void Move(int x, int y) { SetCursorPos(x, y); }
public static void Release() { mouse_event(Up, 0, 0, 0, UIntPtr.Zero); }
// Down and leave in one call, so the tick that sees the button finds the
// cursor already off the sprite. The hook records the down point first.
public static void PressAndLeave(int x, int y, int leaveX, int leaveY) {
SetCursorPos(x, y);
mouse_event(Down, 0, 0, 0, UIntPtr.Zero);
SetCursorPos(leaveX, leaveY);
}
}
"@

$stamp = Get-Date -Format "yyyyMMdd-HHmmss"
$out = Join-Path $env:TEMP "fidget-scenario-quick-grab-$stamp"
$script:Evidence = $out
$data = Initialize-ScenarioData $out
$log = Join-Path $out "app.log"
$err = Join-Path $out "app.err"
$trace = Join-Path $data "process.log"

$env:FIDGET_DIRECTOR_API_KEY = "x"
$env:FIDGET_TRACE_FRAMES = "1"
$env:FIDGET_CHARACTERS = Join-Path $root "characters"

$script:proc = Start-FidgetPiped $Bin @() $log $err
$startMs = Get-NowMs
try {
if (-not (Wait-ForFile 3 $trace)) { Fail "process.log never appeared; see $err" }
if (-not (Wait-ForUp $err)) { Fail "Fidget never logged overlay:; see $err" }
$rest = Wait-ForRest $trace
if (-not $rest) { Fail "the sprite never came to rest; see $trace" }

$overlay = @(Get-Content -LiteralPath $trace | Where-Object { $_ -match 'sprite (\d+)x\d+' })
$sizeLine = $overlay | Select-Object -First 1
if (-not ($sizeLine -match 'sprite (\d+)x\d+')) { Fail "no sprite size in $trace" }
$size = [int]$Matches[1]
$half = [int][Math]::Floor($size / 2)

$grabbed = $false
for ($try = 0; $try -lt 4 -and -not $grabbed; $try++) {
[QuickGrab]::Release()
$spot = Get-LastSpriteSpot $trace
if (-not $spot) { Fail "no sprite position in $trace" }
$aim = Get-Aim $spot $half $size
[QuickGrab]::Move($aim.FarX, $aim.FarY)
$frames = @(Select-String -LiteralPath $trace -Pattern $framePattern -CaseSensitive).Count
if (-not (Wait-ForCount 3 $trace $framePattern $frames)) {
Fail "no tick after the cursor left the sprite; see $trace"
}
$spot = Get-LastSpriteSpot $trace
if (-not $spot) { Fail "no sprite position in $trace" }
$aim = Get-Aim $spot $half $size
$cx = $aim.CenterX
$cy = $aim.CenterY

$clicks = @(Select-String -LiteralPath $trace -Pattern 'click: down ' -CaseSensitive).Count
$grabs = @(Select-String -LiteralPath $trace -Pattern $grabPattern -CaseSensitive).Count
[QuickGrab]::PressAndLeave($cx, $cy, $aim.FarX, $aim.FarY)
if (-not (Wait-ForCount 3 $trace 'click: down ' $clicks)) { continue }
$click = Last-ClickDown $trace
if (-not $click) {
Fail "a click: down line did not parse; see $trace"
}
$pressOnSprite = Near $click.PressX $click.PressY $cx $cy 8
$cursorLeft = -not (Near $click.CursorX $click.CursorY $cx $cy $half)
if (-not $pressOnSprite) {
Fail "press hit-tested at ($($click.PressX),$($click.PressY)), not the sprite at ($cx,$cy); cursor ($($click.CursorX),$($click.CursorY)); see $trace"
}
if (-not $cursorLeft) { continue }
if (-not (Wait-ForCount 3 $trace $grabPattern $grabs)) {
Fail "press at ($($click.PressX),$($click.PressY)) with the cursor at ($($click.CursorX),$($click.CursorY)) did not start a Grab; see $trace"
}
$grabbed = $true
Write-Output "ok: press ($($click.PressX),$($click.PressY)) on the sprite, cursor already ($($click.CursorX),$($click.CursorY))"
}
if (-not $grabbed) {
Fail "every press was sampled while the cursor was still on the sprite; see $trace"
}
} finally {
[QuickGrab]::Release()
Stop-FidgetTree $script:proc
}

$elapsedMs = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds() - $startMs
Write-Output "PASS: evidence in $out (${elapsedMs}ms)"
54 changes: 47 additions & 7 deletions src-tauri/src/frame_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,10 @@ pub(crate) fn run_frame_loop(
let mut input_events: Option<platform::EventTap> = None;
#[cfg(not(unix))]
let mut input_events: Option<mpsc::Receiver<()>> = None;
// Button edges only. The idle back-off still waits out motion, and a
// press dragged off the sprite is hit-tested where the hook saw it.
#[cfg(not(unix))]
let mut press_wakes = platform::spawn_press_listener();

let mut button_was_down = false;
let mut sound_allowed = true;
Expand Down Expand Up @@ -495,6 +499,8 @@ pub(crate) fn run_frame_loop(
// closed channel returns at once, which is a spin rather than a
// sleep. Dropped after the match, where nothing borrows it.
let mut listener_hung_up = false;
#[cfg(not(unix))]
let mut press_listener_hung_up = false;

// Active sleeps 16ms. Idle blocks until a real deadline with no cap. Hidden
// sleeps and ignores input. Visible, Asleep and DND included, keeps the
Expand Down Expand Up @@ -546,8 +552,9 @@ pub(crate) fn run_frame_loop(
thread::sleep(deadline);
}
_ => {
// No listener: poll with back-off. That is Windows always, Wayland
// always, and macOS until the user grants Input Monitoring.
// No motion listener: poll with back-off. Wayland always, macOS
// until Input Monitoring is granted. On Windows a button edge
// ends the visible idle wait; motion keeps the cap.
match (schedule_mode, was_visible) {
(scheduler::ScheduleMode::Active, _) => {
let now = Instant::now();
Expand Down Expand Up @@ -605,14 +612,28 @@ pub(crate) fn run_frame_loop(
let next_sense = SENSE_INTERVAL.saturating_sub(since_sense);
let deadline = next_director.min(next_sense);

// Visible idle input polling. Cap at 100ms when the cursor is near a
// sprite (within 48px) for hover gesture responsiveness. Otherwise cap
// at 1s to avoid burning CPU waiting for rare events (right-click).
// Idle poll: 100ms within 48px of a sprite, else 1s. A button edge
// ends the wait. Motion keeps the cap, so a still cursor does not
// tick and a drag is hit-tested where the button went down.
let capped = if cursor_near_sprite(last_cursor, &last_sprite_rects) {
deadline.min(Duration::from_millis(100))
} else {
deadline.min(Duration::from_secs(1))
};
#[cfg(not(unix))]
{
press_listener_hung_up = match &press_wakes {
Some(wakes) => matches!(
wakes.recv_timeout(capped),
Err(mpsc::RecvTimeoutError::Disconnected)
),
None => {
thread::sleep(capped);
false
}
};
}
#[cfg(unix)]
thread::sleep(capped);
}
}
Expand All @@ -629,6 +650,10 @@ pub(crate) fn run_frame_loop(
// iteration starts a new tap if the setting is still on.
input_events = None;
}
#[cfg(not(unix))]
if press_listener_hung_up {
press_wakes = None;
}

// Read per tick: the Development tab can flip these while the
// loop runs. Click-through is invisible; this trace is the only
Expand Down Expand Up @@ -784,8 +809,9 @@ pub(crate) fn run_frame_loop(
button_was_down = held;
// A press is hit-tested where it began: a drag started within
// a tick has moved the cursor off the sprite by now.
let press_at = platform::press_point()
.filter(|_| button_edge == Some("down") || buttons.secondary)
let press_at = (button_edge == Some("down") || buttons.secondary)
.then(platform::press_point)
.flatten()
.map_or(cursor_at, |(x, y)| {
(
(f64::from(x) / cursor_scale).round() as i32,
Expand Down Expand Up @@ -1506,6 +1532,15 @@ pub(crate) fn run_frame_loop(
// over it. The rest still get time: a pointer that stopped
// being told would measure the next gesture's velocity over the gap.
let was_gesturing = live.pointer.gesturing();
// The mask hit used where the button went down. Travel has to
// start there too, or a drag that finished before this tick
// looks like a still press.
if target == Some(index) && button_edge == Some("down") {
live.pointer.began_at(fidget_core::engine::Point {
x: f64::from(press_at.0),
y: f64::from(press_at.1),
});
}
live.verbs = live.pointer.update(
target == Some(index),
held,
Expand Down Expand Up @@ -2143,9 +2178,14 @@ pub(crate) fn run_frame_loop(
matches!(frame.state, State::Grounded | State::Perched);

any_moving |= scheduler::moving(&frame);
// Windows hears button edges, not motion, so a press that is
// not yet a Grab or a Poke would otherwise sit on the 1s cap.
// macOS and X11 already wake on the drag.
let resolving_press = cfg!(not(unix)) && live.pointer.gesturing();
if needs_active_for_motion
|| needs_active_for_animation
|| needs_active_for_sleep_accrual
|| resolving_press
{
any_needs_active = true;
}
Expand Down
Loading
Loading