fix: harden floating host-control avoidance after review

Independent review of 16e25ea (DeepSeek v4.1 Flash, Qwen4 Max) found the
runtime avoidance could flap, park on protected controls and drift from the
CSS on notched phones. Runtime only; still no skin.css restriction.

- Anti-flap: a move needs the collision to persist ~250 ms, moves are at least
  1 s apart, and more than 6 moves in 10 s park the control at its default
  corner for 10 s (an animated/hostile skin can no longer keep it moving).
- Protected parts now outweigh dialogs and the other host control; ordinary
  buttons inside the protected `app` root (or `home.footer`) are no longer
  weighted as protected.
- Never move onto the last pointer position or the focused control.
- Fold to the compact mark only when that frees at least one control (a
  full-screen click-away layer no longer hides the recovery buttons).
- Score with the live expanded size (language switch) and reset on resize;
  model margins include env(safe-area-inset-*) like the CSS.
- Skip invisible (opacity 0) controls; also count [tabindex], option/switch/
  checkbox/radio roles, iframes and media controls; count the other host
  control once; prefilter by candidate rects (cheap on 3 000-control skins);
  re-check after scrolls; ignore sticky :hover on touch; bind idempotently.
- Tests: weights, fold threshold, pointer, safe area, protected-vs-dialog,
  stability over repeated ticks, two host controls, anti-flap.
This commit is contained in:
OddTurtleHead committed 2026-11-15 21:38:39 +08:00
1 parent e46d7a0eb7
commit 2b019d82ff
7 files changed
+391 -42

No files matched your search

@@ -4,8 +4,10 @@ import {
FLOATING_WEIGHT_CONTROL,
FLOATING_WEIGHT_DIALOG,
FLOATING_WEIGHT_PROTECTED,
FLOATING_WEIGHT_POINTER,
chooseFloatingPosition,
floatingRect,
shouldFold,
floatingScore,
type FloatingObstacle,
} from "./host-floating-placement.js";
@@ -30,14 +32,14 @@ test("floating rects follow the corner margins and stay inside the viewport", ()
assert.ok(narrow.bottom <= 844);
});
test("score is zero without overlap, ignores 1px brushes and weights protected/dialog controls", () => {
test("score is zero without overlap, ignores 1px brushes and weights dialog and (above all) protected controls", () => {
const footprint = rect(12, 724, 200, 38);
assert.equal(floatingScore(footprint, [{ rect: rect(400, 400, 40, 40), weight: 1 }]), 0);
assert.equal(floatingScore(footprint, [{ rect: rect(211, 724, 40, 38), weight: 1 }]), 0);
const control = floatingScore(footprint, [{ rect: rect(150, 730, 34, 34), weight: FLOATING_WEIGHT_CONTROL }]);
const protectedPart = floatingScore(footprint, [{ rect: rect(150, 730, 34, 34), weight: FLOATING_WEIGHT_PROTECTED }]);
const dialog = floatingScore(footprint, [{ rect: rect(150, 730, 34, 34), weight: FLOATING_WEIGHT_DIALOG }]);
assert.ok(control > 0 && protectedPart > control && dialog > protectedPart);
assert.ok(control > 0 && dialog > control && protectedPart > dialog);
});
test("the default corner is kept while it is free", () => {
@@ -78,3 +80,52 @@ test("ties keep the current position (no flapping between equal corners)", () =>
const choice = chooseFloatingPosition({ current: "top-right", size: capsule, viewport, margins, obstacles });
assert.equal(choice.position, "top-right");
});
test("a move never lands on the pointer or the focused control, but the current spot is not pushed by them", () => {
// Default corner covers the microphone; top-left would be free but the
// pointer rests there, so the capsule takes another free spot.
const obstacles: FloatingObstacle[] = [{ rect: rect(150, 730, 34, 34), weight: FLOATING_WEIGHT_PROTECTED }];
const pointer: FloatingObstacle = { rect: rect(84, 15, 32, 32), weight: FLOATING_WEIGHT_POINTER };
const choice = chooseFloatingPosition({ current: "bottom-left", size: capsule, viewport, margins, obstacles, moveObstacles: [pointer] });
assert.notEqual(choice.position, "top-left");
assert.equal(choice.score, 0);
// A pointer next to (or on) a collision-free capsule never makes it move.
const stay = chooseFloatingPosition({ current: "top-left", size: capsule, viewport, margins, obstacles: [], moveObstacles: [pointer] });
assert.equal(stay.position, "top-left");
});
test("folding needs to free at least one control; a full-screen click-away layer does not fold the capsule", () => {
const backdrop: FloatingObstacle[] = [{ rect: rect(0, 0, 1280, 800), weight: FLOATING_WEIGHT_CONTROL }];
const full = chooseFloatingPosition({ current: "bottom-left", size: capsule, viewport, margins, obstacles: backdrop });
const reduced = chooseFloatingPosition({ current: full.position, size: { width: 38, height: 38 }, viewport, margins, obstacles: backdrop });
assert.ok(full.score > 0 && reduced.score < full.score);
assert.equal(shouldFold(full, reduced), false);
// Freeing a real control (score drops by at least 0.25 × weight) folds.
assert.equal(shouldFold({ position: "bottom-left", score: 1.25 }, { position: "bottom-left", score: 0 }), true);
assert.equal(shouldFold({ position: "bottom-left", score: 0.35 }, { position: "bottom-left", score: 0 }), true);
assert.equal(shouldFold({ position: "bottom-left", score: 0 }, { position: "bottom-left", score: 0 }), false);
});
test("safe-area margins shift the modelled rect exactly like the CSS env() offsets", () => {
const insets = { top: 47, right: 0, bottom: 34, left: 0 };
const phone = { width: 390, height: 844 };
const withInsets = { top: Math.max(12, insets.top), right: Math.max(12, insets.right), bottom: 38 + insets.bottom, left: Math.max(12, insets.left) };
// CSS: bottom: calc(38px + env(safe-area-inset-bottom)) -> 844 - 72 - 38.
assert.equal(floatingRect("bottom-left", capsule, phone, withInsets).top, 844 - 72 - 38);
assert.equal(floatingRect("top-left", capsule, phone, withInsets).top, 47);
});
test("with every spot taken, the capsule covers a dialog button before a protected control", () => {
const mobile = { width: 390, height: 844 };
const wide = { width: 250, height: 38 };
// Dialog buttons everywhere except top-left, where the microphone is.
const obstacles: FloatingObstacle[] = [
{ rect: rect(0, 0, 270, 60), weight: FLOATING_WEIGHT_PROTECTED },
{ rect: rect(120, 0, 270, 60), weight: FLOATING_WEIGHT_DIALOG },
{ rect: rect(0, 380, 390, 90), weight: FLOATING_WEIGHT_DIALOG },
{ rect: rect(0, 740, 390, 104), weight: FLOATING_WEIGHT_DIALOG },
];
const choice = chooseFloatingPosition({ current: "bottom-left", size: wide, viewport: mobile, margins, obstacles });
const footprint = floatingRect(choice.position, wide, mobile, margins);
assert.equal(floatingScore(footprint, [obstacles[0]]), 0, choice.position);
});