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
+44
View File
@@ -110,4 +110,48 @@ public sealed class WebView2ManagerTests
Assert.Equal(System.Drawing.Color.Transparent, webView.DefaultBackgroundColor);
});
}
[Fact]
public void FindContentBounds_Returns_Bounding_Box_Of_NonTransparent_Pixels()
{
const int w = 10, h = 6;
var pixels = new byte[w * h * 4];
for (int yy = 2; yy <= 4; yy++)
for (int xx = 3; xx <= 6; xx++)
{
var i = (yy * w + xx) * 4;
pixels[i] = 255; // B
pixels[i + 1] = 0; // G
pixels[i + 2] = 0; // R
pixels[i + 3] = 255; // A
}
var (x, y, bw, bh) = WebView2Manager.FindContentBounds(pixels, w, h, w * 4);
Assert.Equal((3, 2, 4, 3), (x, y, bw, bh));
}
[Fact]
public void FindContentBounds_No_Alpha_Means_No_Crop()
{
const int w = 10, h = 6;
var pixels = new byte[w * h * 4];
var (x, y, bw, bh) = WebView2Manager.FindContentBounds(pixels, w, h, w * 4);
Assert.Equal((0, 0, w, h), (x, y, bw, bh));
}
[Fact]
public void FindContentBounds_Full_Opacity_Means_No_Crop()
{
const int w = 10, h = 6;
var pixels = new byte[w * h * 4];
for (var i = 3; i < pixels.Length; i += 4)
pixels[i] = 255;
var (x, y, bw, bh) = WebView2Manager.FindContentBounds(pixels, w, h, w * 4);
Assert.Equal((0, 0, w, h), (x, y, bw, bh));
}
}