Web source: crop to the frame's ALPHA bounding box — the widget's true rendered extent, no DOM timing, can't break the render

- FindContentBounds scans the Bgra32 capture for the bounding box of non-transparent pixels
  (background is injected transparent, so alpha = widget extent). Crop to that rect + Stretch=Fill
  maps the widget flush under the selection box: full-canvas widgets fall through to the full frame
  (identical to the confirmed-good image); floating widgets get tight-fitted with no dead space.
- Removes QueryContentBoundsAsync + the JS script entirely — the DOM-union attempt (a5b9952) broke
  rendering by measuring an unsettled layout on NavigationCompleted; alpha measurement is immune.
- 3 new unit tests: tight box, no-alpha no-crop, full-alpha no-crop. 7/7 pass, 0 warnings.
This commit is contained in:
2026-08-28 11:53:36 -07:00
parent 5348b5c480
commit bccdb4892c
5 changed files with 117 additions and 80 deletions
+26 -23
View File
@@ -6,34 +6,36 @@
working). **NOT pushed — user rule: never push without explicit instruction (2026-08-24).** No feature
branches pre-1.0: all work lands on `main` per work unit.
## The web-source rendering work (2026-08-28) — CURRENT MODEL: canvas-size viewport + content crop
## The web-source rendering work (2026-08-28) — CURRENT MODEL: canvas-size viewport + ALPHA-BBOX crop
**Root causes finally isolated:** (1) hosting the page at the element-box size clips full-bleed
widgets to their upper-left chunk (fixed — page now renders at the master canvas 1920×1080,
stable). (2) The content-bounds crop that was supposed to kill right/bottom dead space never ran:
the script returned `JSON.stringify(...)` (a string), and `ExecuteScriptAsync` JSON-encodes the
result, so `JsonDocument.Parse` hit a string node and `GetProperty` threw → bounds stayed unset →
no crop. ALSO the intended measure was wrong: scroll extents ≠ widget rect (they include the
page's own margins). Fixed together: the script now returns a real OBJECT (union of every visible
`body *` `getBoundingClientRect`), giving the widget's true rect INCLUDING its top-left offset,
and `CaptureFrame` crops AT that offset — so the widget anchors at (0,0) and the box is tight on
all four sides. `Stretch="Fill"` maps it flush under the box. Creator confirmed render+resize are
solid at the canvas-size change; this commit delivers the tight/0,0 bounding box.
**History of failures (see MyMistakes.md for the full log):** box-size rendering clipped full-bleed
widgets to their upper-left chunk (fixed — page renders at the master canvas 1920×1080, stable, the
one render the creator confirmed solid). Then four crop attempts failed in different ways: (1) the
`JSON.stringify` return double-encoded under `ExecuteScriptAsync`, silently disabling the crop;
(2) `scrollWidth/scrollHeight` at canvas size returns the whole viewport for full-canvas wrapper
bodies → no trim (ghost boundary); (3) union-of-DOM-elements measured an unsettled layout on
NavigationCompleted and BROKE the image (creator's "don't break the rendering" boundary crossed).
(4) Reverting to f39ebd9's scroll-crop was another dead end (#2 again).
**The CURRENT MODEL:**
- `Services/WebView2Manager.cs` — page renders at 1920×1080 (stable); `overflow:hidden` +
transparent-bg injection; `QueryContentBoundsAsync` measures real widget bounds (object return
from JS, NOT JSON.stringify); `CaptureFrame` crops at the widget's (x,y) offset → anchored 0,0.
- `MainWindow.xaml` — web `<Image>` `Stretch="Fill"`.
- Invariant: **render at canvas size; crop to the widget's true bounds; Fill to the box.**
**The CURRENT MODEL (this commit):**
- `Services/WebView2Manager.cs` — page renders at 1920×1080 (stable, never tracked); `overflow:hidden`
+ transparent-bg injection. `FindContentBounds` scans the Bgra32 capture per frame and crops to the
bounding box of NON-TRANSPARENT pixels — the widget's true rendered extent, measured from the frame
itself. No DOM query, no layout timing: it can never truncate visible content (worst case = full
frame = the confirmed-good image). `Stretch="Fill"` maps the cropped frame flush under the selection
box → the box hugs the widget on all four sides. `QueryContentBoundsAsync` + the JS script are
DELETED.
- `MainWindow.xaml` — web `<Image>` Stretch set to Fill via the IsWebSource DataTrigger (unchanged).
- Invariant: **render at canvas size; crop to the alpha extent; Fill to the box.**
- Unit tests: 3 new for `FindContentBounds` (tight box, no-alpha no-crop, full-alpha no-crop).
**Property panel / sliders (2026-08-28):** web URI ✕ clears the textbox; web ✓/✕ icon buttons
`IsTabStop="False"` so Tab walks X→Y→W→H→URI; Slider style `IsMoveToPointEnabled="True"`
restores click-anywhere-on-bar. All three confirmed fixed by the creator.
Build: 0 warnings; tests 4/4. Rejected for the record: box-sized viewport (upper-left truncation),
fixed 1920×1080 + `Uniform` (letterbox dead space), CSS margin/100% reset (enlarged gap),
viewport==box (reflow/truncation/scrollbars).
Build: 0 warnings; tests 7/7 (WebView2Manager). Rejected for the record: box-sized viewport (upper-left
truncation), fixed 1920×1080 + `Uniform` (letterbox dead space), CSS margin/100% reset (enlarged gap),
viewport==box (reflow/truncation/scrollbars), DOM-union crop (broke the image).
## Key facts / landmines
@@ -50,7 +52,8 @@ viewport==box (reflow/truncation/scrollbars).
- No `CODING.md`; conventions live in `AGENTS.md` + `ai.md`.
## Next steps
1. AWAIT creator's verdict on viewport==box (geometry-agnostic) rendering — visual check with a
widget URL at several box sizes; the selection frame must snap flush with no padding.
1. AWAIT creator's verdict on the ALPHA-BBOX crop — reload a widget URL at several box sizes; the
selection frame must snap flush to the widget with no dead space, image must stay identical for
full-bleed widgets. If an animated widget causes visible jitter, add crop hysteresis as a follow-up.
2. Follow-up only if the creator asks: graceful handling of navigation errors / invalid URIs (TASK 17 ×9).
3. Continue queue: TASK 18 local recording, TASK 21 media source, TASK 22 audio sync offset.