Skip to content

[display-space-ranchanimal-movement]: Display-space seam for RanchAnimal movement - #58

Merged
archae0pteryx merged 1 commit into
mainfrom
codex/display-space-ranchanimal-movement
May 14, 2026
Merged

[display-space-ranchanimal-movement]: Display-space seam for RanchAnimal movement#58
archae0pteryx merged 1 commit into
mainfrom
codex/display-space-ranchanimal-movement

Conversation

@archae0pteryx

@archae0pteryx archae0pteryx commented May 14, 2026

Copy link
Copy Markdown
Contributor

What changed

  • Added a Rust DisplaySpace payload with span, spawn region, movement regions, and hit-test scale.
  • Replaced the old primary-region-only event with display-space.
  • Added a pure advanceRanchAnimal movement module for shared movement behavior.
  • Updated usePigMovement to delegate roam/freeze/friction/turn/soft-steer/hard-clamp behavior to that module.
  • Updated issue 049 and CONTEXT.md with the RanchAnimal/display-space decisions.

Why

Issue file: issues/049-display-space-pig-movement-seam.md

Completion promise:

RanchAnimal movement consumes a display-space interface instead of raw viewport dimensions and ad hoc primary-region rules; multi-monitor geometry policy is local to the display-space module.

This keeps monitor geometry policy local to Rust display-space computation and makes movement behavior testable without opening Tauri windows.

Validation

  • task check

Summary by CodeRabbit

  • New Features
    • Animals now automatically reposition to the nearest valid location when display configuration changes, rather than respawning
    • Enhanced movement with soft steering near movement region edges to prevent impacts, plus boundary clamping and velocity reflection when crossing limits
    • Improved multi-monitor support with normalized coordinate space across all enabled monitors

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 14, 2026

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: fad12f5d-8bf1-4860-9069-acd4bacff30a

📥 Commits

Reviewing files that changed from the base of the PR and between 671048d and e1fbcf2.

📒 Files selected for processing (11)
  • CONTEXT.md
  • issues/049-display-space-pig-movement-seam.md
  • src-tauri/src/display/mod.rs
  • src-tauri/src/display/monitor.rs
  • src-tauri/src/display/overlay.rs
  • src/api/pig.ts
  • src/hooks/usePigMovement.ts
  • src/lib/ranchAnimalMovement.test.ts
  • src/lib/ranchAnimalMovement.ts
  • src/types/display.ts
  • src/types/pig.ts

📝 Walkthrough

Walkthrough

Replaces the Rust-to-React display-region interface from PrimaryRegion to a normalized DisplaySpace model, introduces a pure ranch-animal movement library with deterministic physics, and refactors the pig movement hook to delegate movement to the library.

Changes

RanchAnimal Display-Space Refactor

Layer / File(s) Summary
Specification & ubiquitous language
CONTEXT.md, issues/049-display-space-pig-movement-seam.md
Glossary entries introduce RanchAnimal as the generalized animal family and DisplaySpace as the normalized multi-monitor layout. Issue spec updates requirements to partition Rust ownership of monitor geometry (normalized regions, spawn location) from React's delegation to a pure movement-step module; acceptance criteria are expanded to include randomness adapters, clamping after display changes, hybrid edge behavior (soft steer + hard clamp), and test coverage expectations.
DisplaySpace data model
src/types/display.ts, src-tauri/src/display/monitor.rs
TypeScript interfaces (Rect, RectSize, DisplaySpace) define the normalized geometry. Corresponding Rust structs (DisplaySpan, DisplayRect, DisplaySpace with camelCase serialization) are added to match the frontend model.
Rust DisplaySpace computation & tests
src-tauri/src/display/monitor.rs
Implements compute_display_space to normalize multi-monitor geometry into overlay coordinates, designate the first enabled monitor as primary, and populate movement regions for all enabled monitors. Comprehensive unit tests validate normalization across single, multi-monitor side-by-side, portrait, and asymmetric layouts.
Rust integration: remove PrimaryRegion, wire DisplaySpace
src-tauri/src/display/mod.rs, src-tauri/src/display/overlay.rs
Removes PrimaryRegion struct and serialization. Updates DisplayManager::apply to compute display_space via compute_display_space. Updates overlay::ShowParams to pass display_space instead of primary_region. Changes frontend event from "display-region" to "display-space".
RanchAnimal movement simulation library
src/lib/ranchAnimalMovement.ts
Exports constants (RANCH_ANIMAL_SIZE, RANCH_EDGE_MARGIN, RANCH_ANIMAL_SPEED, RANCH_FRICTION), types (RanchAnimalDirection, RanchAnimalState, AdvanceRanchAnimalInput), and core advanceRanchAnimal function that evolves position/velocity through friction, minimum-speed enforcement, random turns, edge steering/reflection, and region clamping. Includes helpers for region selection, point clamping, direction derivation, and distance calculations.
RanchAnimal movement behavior tests
src/lib/ranchAnimalMovement.test.ts
Vitest suite validates position clamping into nearest movement region, edge-crossing hard clamp with velocity reflection, soft steering before impact, random turn scheduling via adapter, frozen-state time progression, friction and minimum-speed enforcement, and sprite frame advancement on fixed intervals.
Frontend API: subscription update & type imports
src/api/pig.ts, src/types/pig.ts
Replaces subscribeDisplayRegion with subscribeDisplaySpace, adjusting type imports to use DisplaySpace from ../types/display. Removes SpawnRegion interface, leaving only PigHitRect export.
Hook refactor: usePigMovement to advanceRanchAnimal & DisplaySpace
src/hooks/usePigMovement.ts
Major refactor replacing local movement tick/physics with advanceRanchAnimal calls. Replaces SpawnRegion state with DisplaySpace refs/state and adds defaultDisplaySpace() fallback for initial viewport bounds. Converts PigDirection/PigState to type aliases. Updates initPig to accept DisplaySpace, Rust subscription to subscribeDisplaySpace, focus synchronization to handle fallback vs. real display-space, rAF loop to invoke advanceRanchAnimal for non-dragged pigs, and hit-rect syncing to be DisplaySpace/hitTestScale aware. Re-exports DisplaySpace and ranch-animal constants.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • killallgit/adhd-ranch#27: Refactors the Tauri display subsystem (src-tauri/src/display/{mod.rs,overlay.rs,monitor.rs}) by evolving the PrimaryRegion/"display-region" contract toward DisplaySpace/"display-space".
  • killallgit/adhd-ranch#39: Modifies the pig IPC surface and usePigMovement Tauri subscription—the retrieved PR introduces subscribeDisplayRegion/SpawnRegion, while this PR replaces that plumbing with subscribeDisplaySpace/DisplaySpace.
  • killallgit/adhd-ranch#22: Introduces drag/toss control APIs (startDrag/moveDrag/endDrag) that this PR's refactored usePigMovement preserves while delegating movement simulation to advanceRanchAnimal.

🐰 Coordinates flow through space, a rabbit's embrace,
DisplaySpace maps each move with grace,
Physics pure, no Pig-specific trace,
RanchAnimals roam at their own pace,
Deterministic steps in the UI's case! 🎪

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/display-space-ranchanimal-movement

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint skipped: no ESLint configuration detected in root package.json. To enable, add eslint to devDependencies.


Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/hooks/usePigMovement.ts (1)

134-147: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

hitTestScale and span never reach updatePigRects.

These lines start treating Rust DisplaySpace as the geometry source, but syncRects() still sizes/scales rects from window.innerWidth / window.innerHeight and window.devicePixelRatio. That leaves the new span and hitTestScale fields unused, so hit regions can drift on mixed-DPI or non-primary-monitor layouts.

Suggested fix
-function syncRects(pigs: PigState[], wide: boolean): void {
-  const dpr = window.devicePixelRatio || 1;
+function syncRects(pigs: PigState[], wide: boolean, displaySpace: DisplaySpace): void {
+  const scale = displaySpace.hitTestScale;
   // Wide rect (detail open or dragging) keeps overlay interactive across the full viewport.
   const rects = wide
-    ? [{ x: 0, y: 0, size: Math.max(window.innerWidth, window.innerHeight) * dpr * 2 }]
-    : buildHitRects(pigs, dpr);
+    ? [{ x: 0, y: 0, size: Math.max(displaySpace.span.w, displaySpace.span.h) * scale * 2 }]
+    : buildHitRects(pigs, scale);
   updatePigRects(rects).catch(() => {});
 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/hooks/usePigMovement.ts` around lines 134 - 147, displaySpaceRef is
updated from Rust but syncRects()/updatePigRects still compute sizes from
window.innerWidth/innerHeight/DPR, so new DisplaySpace.hitTestScale and span are
never applied; update updatePigRects (and any call sites of syncRects) to read
displaySpaceRef.current.hitTestScale and .span (from DisplaySpace via
setDisplaySpace/displaySpaceRef) and use those values to scale and position pig
rects and hit regions instead of relying on window.* and devicePixelRatio,
ensuring hitTestScale multiplies geometry and span is used to offset/translate
coordinates for mixed-DPI or non-primary-monitor layouts.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src-tauri/src/display/mod.rs`:
- Around line 76-82: compute_display_space is redundantly recomputing the span:
change monitor::compute_display_space to return both the DisplaySpace and the
SpanBounds (e.g., Option<(DisplaySpace, SpanBounds)>) so the span is computed
once inside compute_display_space (use its existing compute_span call and return
its result), then update the caller in mod.rs to destructure the returned tuple
into (display_space, bounds) and remove the second call to
monitor::compute_span; update signatures and all call sites of
compute_display_space accordingly (symbols: compute_display_space, compute_span,
DisplaySpace, SpanBounds).

In `@src/api/pig.ts`:
- Line 3: The API file imports the DisplaySpace type from ranchAnimalMovement
which couples layers; extract DisplaySpace into a shared type module (e.g.,
create src/types/display.ts exporting DisplaySpace) and update both pig.ts and
ranchAnimalMovement.ts to import DisplaySpace from that new module instead of
../lib/ranchAnimalMovement so the API depends only on the shared types.

In `@src/hooks/usePigMovement.ts`:
- Around line 230-241: The initial pig seeding reads displaySpaceRef.current too
early so initPig uses defaultDisplaySpace() before the Rust display-space
listener is installed; fix by ensuring initPig always uses the up-to-date
display-space when mapping focuses: either read displaySpaceRef.current inside
the mapper (move the displaySpace read into the focuses.map callback) or delay
the initial sync until after the subscription effect that installs the Rust
listener updates displaySpaceRef (e.g., trigger setPigs once more when that
subscription sets displaySpaceRef). Update the useEffect that does setPigs, the
initPig calls, and the subscription effect that installs the Rust listener so
the mapper references the live displaySpaceRef (displaySpaceRef, initPig,
setPigs, focuses, subscription effect installing Rust listener).

---

Outside diff comments:
In `@src/hooks/usePigMovement.ts`:
- Around line 134-147: displaySpaceRef is updated from Rust but
syncRects()/updatePigRects still compute sizes from
window.innerWidth/innerHeight/DPR, so new DisplaySpace.hitTestScale and span are
never applied; update updatePigRects (and any call sites of syncRects) to read
displaySpaceRef.current.hitTestScale and .span (from DisplaySpace via
setDisplaySpace/displaySpaceRef) and use those values to scale and position pig
rects and hit regions instead of relying on window.* and devicePixelRatio,
ensuring hitTestScale multiplies geometry and span is used to offset/translate
coordinates for mixed-DPI or non-primary-monitor layouts.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: b4735f0d-e4d6-40be-b315-1ebbdd48323d

📥 Commits

Reviewing files that changed from the base of the PR and between 5d8fd06 and 671048d.

📒 Files selected for processing (10)
  • CONTEXT.md
  • issues/049-display-space-pig-movement-seam.md
  • src-tauri/src/display/mod.rs
  • src-tauri/src/display/monitor.rs
  • src-tauri/src/display/overlay.rs
  • src/api/pig.ts
  • src/hooks/usePigMovement.ts
  • src/lib/ranchAnimalMovement.test.ts
  • src/lib/ranchAnimalMovement.ts
  • src/types/pig.ts
💤 Files with no reviewable changes (1)
  • src/types/pig.ts

Comment thread src-tauri/src/display/mod.rs Outdated
Comment thread src/api/pig.ts Outdated
Comment thread src/hooks/usePigMovement.ts Outdated
@archae0pteryx
archae0pteryx force-pushed the codex/display-space-ranchanimal-movement branch from 671048d to e1fbcf2 Compare May 14, 2026 19:58
@archae0pteryx
archae0pteryx marked this pull request as ready for review May 14, 2026 20:34
@archae0pteryx
archae0pteryx merged commit fa26793 into main May 14, 2026
1 of 2 checks passed
@archae0pteryx
archae0pteryx deleted the codex/display-space-ranchanimal-movement branch May 14, 2026 20:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant