Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(captions): pick the closest position preset, not the first one checked

EtienneLescota7536fdaa7ef
diff --git a/src/lib/ai-edition/captions/captions.test.ts b/src/lib/ai-edition/captions/captions.test.ts
index a4d12f257..d6529489e 100644
--- a/src/lib/ai-edition/captions/captions.test.ts
+++ b/src/lib/ai-edition/captions/captions.test.ts
@@ -6,6 +6,7 @@ import {
activeHorizontalPositionPreset,
activeVerticalPositionPreset,
CAPTION_BAND_HEIGHT_PCT,
+ CAPTION_POSITION_PRESET_EPSILON,
captionBackgroundCss,
captionBandRect,
captionHorizontalPositionOffset,
@@ -277,6 +278,18 @@ describe("caption position presets", () => {
expect(activeHorizontalPositionPreset({ ...ON, width: 100, offsetX: 0 })).toBe("center");
});
+ it("still picks left/right over center when a near-full-width band squeezes them inside the epsilon", () => {
+ // At width this close to 100, range.x.min/max themselves fall inside
+ // CAPTION_POSITION_PRESET_EPSILON of 0 — checking "is this near center?"
+ // first would wrongly claim an offset that is exactly at the true edge.
+ const squeezed = { ...ON, width: 100 - 1e-7 };
+ const range = captionOffsetRange(squeezed);
+ expect(Math.abs(range.x.min)).toBeLessThan(CAPTION_POSITION_PRESET_EPSILON);
+ expect(activeHorizontalPositionPreset({ ...squeezed, offsetX: range.x.min })).toBe("left");
+ expect(activeHorizontalPositionPreset({ ...squeezed, offsetX: range.x.max })).toBe("right");
+ expect(activeHorizontalPositionPreset({ ...squeezed, offsetX: 0 })).toBe("center");
+ });
+
it("sets offsetX to the true frame edge for left/right, matching the reachable range", () => {
const range = captionOffsetRange(ON);
expect(captionHorizontalPositionOffset(ON, "left")).toBeCloseTo(range.x.min, 6);
diff --git a/src/lib/ai-edition/captions/settings.ts b/src/lib/ai-edition/captions/settings.ts
index 3f80bd05b..469c67aaf 100644
--- a/src/lib/ai-edition/captions/settings.ts
+++ b/src/lib/ai-edition/captions/settings.ts
@@ -211,18 +211,33 @@ export function activeVerticalPositionPreset(
* There is no stored `horizontalPosition` field — `offsetX` is already an
* absolute-feeling value centred on 0 with a range that reaches both frame
* edges (see `captionOffsetRange`), so "left"/"center"/"right" are just names
- * for three points on that existing range, checked in this order because all
- * three coincide at `offsetX === 0` when the band is full-width (no travel).
+ * for three points on that existing range. All three coincide at `offsetX===0`
+ * when the band is full-width (no travel) — and can also *nearly* coincide
+ * for a band merely close to full-width, where `range.x.min`/`max` shrink
+ * toward 0 as well. Picking the CLOSEST candidate (not the first one within
+ * epsilon) is what keeps that near-degenerate case from reporting "center"
+ * for an offset that is actually sitting exactly on `range.x.min`/`max`.
*/
export function activeHorizontalPositionPreset(
settings: CaptionSettings,
): CaptionHorizontalPosition | null {
const range = captionOffsetRange(settings);
const { offsetX } = settings;
- if (Math.abs(offsetX) < CAPTION_POSITION_PRESET_EPSILON) return "center";
- if (Math.abs(offsetX - range.x.min) < CAPTION_POSITION_PRESET_EPSILON) return "left";
- if (Math.abs(offsetX - range.x.max) < CAPTION_POSITION_PRESET_EPSILON) return "right";
- return null;
+ const candidates: ReadonlyArray<[CaptionHorizontalPosition, number]> = [
+ ["center", 0],
+ ["left", range.x.min],
+ ["right", range.x.max],
+ ];
+ let closest: CaptionHorizontalPosition | null = null;
+ let closestDistance = CAPTION_POSITION_PRESET_EPSILON;
+ for (const [preset, target] of candidates) {
+ const distance = Math.abs(offsetX - target);
+ if (distance < closestDistance) {
+ closest = preset;
+ closestDistance = distance;
+ }
+ }
+ return closest;
}
/** The `offsetX` that puts the band at a given horizontal preset, for a preset