Skip to content

let your agent control your phone

capture: activate + region-capture fallback; swipe: momentum flick

ShawnPana6c3c6b033d3b
diff --git a/src/phone_harness/helpers.py b/src/phone_harness/helpers.py
index 7e12bbd..c40c441 100644
--- a/src/phone_harness/helpers.py
+++ b/src/phone_harness/helpers.py
@@ -87,7 +87,11 @@ def swipe(direction, distance=0.4):
dy = {"up": -1, "down": 1}.get(direction, 0) * w["h"] * distance
if not dx and not dy:
raise ValueError(f"unknown direction {direction!r}")
- mirror.drag(cx - dx / 2, cy - dy / 2, cx + dx / 2, cy + dy / 2)
+ # Fast, short drag = a momentum flick. A slow drag barely registers on iOS
+ # (it won't even flip a Home-Screen page); the flick is what snaps pages
+ # and carousels. For scrolling lists use scroll()/scroll_collect() instead.
+ mirror.drag(cx - dx / 2, cy - dy / 2, cx + dx / 2, cy + dy / 2,
+ duration=0.12, steps=6)
def scroll(amount=300):
diff --git a/src/phone_harness/mirror.py b/src/phone_harness/mirror.py
index 2ae4923..15f2530 100644
--- a/src/phone_harness/mirror.py
+++ b/src/phone_harness/mirror.py
@@ -81,26 +81,39 @@ def ensure_window(timeout=15.0):
# --- capture ---
+def _run_capture(args, path):
+ r = subprocess.run(args, capture_output=True)
+ ok = (r.returncode == 0 and Path(path).exists()
+ and Path(path).stat().st_size > 1000)
+ return ok, (r.stderr.decode(errors="replace").strip() or "empty capture")
+
+
def capture(path=None, retries=2):
- """Capture just the mirroring window (works even if partly covered).
+ """Capture the mirroring window as a PNG. Returns (path, window_bounds).
- Returns (path, window_bounds). -o omits the drop shadow so image pixels
- map linearly onto the window rect. The window id can go stale between
- lookup and capture (the mirroring window is re-created on some redraws), so
- on failure we re-query a fresh window and retry.
+ `screencapture -l <id>` grabs only the window (no shadow, even if covered)
+ but fails with "could not create image from window" when the window is not
+ frontmost/composited. So we activate first, and fall back to a region
+ capture of the window rect, which succeeds regardless of window backing
+ (the app is frontmost by then, so nothing occludes the rect).
"""
path = str(path or TMP / "window.png")
last = None
for attempt in range(retries + 1):
- win = find_window()
- if win is None:
- win = ensure_window()
- r = subprocess.run(
- ["screencapture", "-x", "-o", "-l", str(win["id"]), path],
- capture_output=True)
- if r.returncode == 0 and Path(path).exists() and Path(path).stat().st_size > 1000:
+ win = find_window() or ensure_window()
+ # -l: window-only, ideal when it works
+ ok, last = _run_capture(
+ ["screencapture", "-x", "-o", "-l", str(win["id"]), path], path)
+ if ok:
+ return path, win
+ # not backed — bring it frontmost and retry the region instead
+ activate()
+ win = find_window() or win
+ region = f"{int(win['x'])},{int(win['y'])},{int(win['w'])},{int(win['h'])}"
+ ok, last = _run_capture(
+ ["screencapture", "-x", "-R", region, path], path)
+ if ok:
return path, win
- last = r.stderr.decode(errors="replace").strip() or "empty capture"
time.sleep(0.3)
raise RuntimeError(f"window capture failed after {retries + 1} tries: {last}")