Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions src/Bermuda.Fdc3.Tests/ContentWindowFitTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
using Bermuda.IpcContract;

namespace Bermuda.Fdc3.Tests;

/// <summary>
/// The sizing rules behind <c>window.setContentSize</c>: DPI conversion, the
/// caption strip added on top of the content, and clamping to min/max and the
/// monitor work area.
/// </summary>
public class ContentWindowFitTests
{
private static readonly ContentWindowFit.Scales Identity = new(1, 1, 1, 1, 1);
private static readonly ContentWindowFit.Constraints Unconstrained = new(0, 0, double.PositiveInfinity, double.PositiveInfinity);
private static readonly ContentWindowFit.Area Desktop = new(0, 0, 2000, 1200);

[Fact]
public void Css_pixels_become_dips_and_the_caption_strip_is_added()
{
// Client title bar: 3px resize border each side, 40px caption + 3px bottom border.
var fit = Fit(400, 240, chrome: new ContentWindowFit.Chrome(6, 43));

Assert.Equal(10, fit.Left);
Assert.Equal(20, fit.Top);
Assert.Equal(406, fit.Width);
Assert.Equal(283, fit.Height);
}

[Fact]
public void Matching_raster_and_dip_scales_cancel_so_dpi_does_not_double_the_window()
{
var fit = Fit(400, 240, scales: new ContentWindowFit.Scales(1, 1.5, 1.5, 1.5, 1.5), chrome: new ContentWindowFit.Chrome(6, 43));

Assert.Equal(406, fit.Width);
Assert.Equal(283, fit.Height);
}

[Fact]
public void A_raster_scale_above_the_dip_scale_grows_the_window()
{
var fit = Fit(100, 80, scales: new ContentWindowFit.Scales(1, 2, 2, 1, 1));

Assert.Equal(200, fit.Width);
Assert.Equal(160, fit.Height);
}

[Fact]
public void Page_zoom_scales_the_content_before_chrome_is_added()
{
var fit = Fit(100, 80, scales: new ContentWindowFit.Scales(2, 1, 1, 1, 1), chrome: new ContentWindowFit.Chrome(6, 43));

Assert.Equal(206, fit.Width);
Assert.Equal(203, fit.Height);
}

[Theory]
[InlineData(0)]
[InlineData(-1)]
public void A_non_positive_zoom_is_treated_as_one(double zoom)
{
var fit = Fit(100, 80, scales: new ContentWindowFit.Scales(zoom, 1, 1, 1, 1));

Assert.Equal(100, fit.Width);
Assert.Equal(80, fit.Height);
}

[Fact]
public void A_non_positive_content_size_is_ignored()
{
Assert.Null(ContentWindowFit.Fit(
new ContentSize(0, 240),
Identity,
default,
Unconstrained,
Desktop,
10,
20));
}

[Fact]
public void A_huge_content_size_is_clamped_to_the_work_area_and_pulled_on_screen()
{
var fit = Fit(100_000, 100_000, work: new ContentWindowFit.Area(50, 80, 800, 600), left: 900, top: 900);

Assert.Equal(800, fit.Width);
Assert.Equal(600, fit.Height);
Assert.Equal(50, fit.Left);
Assert.Equal(80, fit.Top);
}

[Fact]
public void Position_is_left_alone_when_the_fitted_window_already_fits()
{
var fit = Fit(100, 80, left: 40, top: 60);

Assert.Equal(40, fit.Left);
Assert.Equal(60, fit.Top);
}

[Fact]
public void Min_and_max_constraints_bound_the_outer_size()
{
var small = Fit(100, 80, constraints: new ContentWindowFit.Constraints(500, 400, double.PositiveInfinity, double.PositiveInfinity));
Assert.Equal(500, small.Width);
Assert.Equal(400, small.Height);

var large = Fit(1000, 800, constraints: new ContentWindowFit.Constraints(0, 0, 300, 200));
Assert.Equal(300, large.Width);
Assert.Equal(200, large.Height);
}

[Fact]
public void A_minimum_larger_than_the_work_area_does_not_push_the_window_off_screen()
{
var fit = Fit(100, 80, constraints: new ContentWindowFit.Constraints(900, 900, double.PositiveInfinity, double.PositiveInfinity), work: new ContentWindowFit.Area(0, 0, 800, 600));

Assert.Equal(800, fit.Width);
Assert.Equal(600, fit.Height);
}

private static ContentWindowFit.Placement Fit(
int width,
int height,
ContentWindowFit.Scales? scales = null,
ContentWindowFit.Chrome chrome = default,
ContentWindowFit.Constraints? constraints = null,
ContentWindowFit.Area? work = null,
double left = 10,
double top = 20)
{
var placement = ContentWindowFit.Fit(
new ContentSize(width, height),
scales ?? Identity,
chrome,
constraints ?? Unconstrained,
work ?? Desktop,
left,
top);

Assert.NotNull(placement);
return placement.Value;
}
}
30 changes: 30 additions & 0 deletions src/Bermuda.Fdc3.Tests/IpcDispatchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class IpcDispatchTests
[InlineData("fdc3.getInfo", typeof(IFdc3Commands))]
[InlineData("fdc3.raiseIntent", typeof(IFdc3Commands))] // "Async" suffix dropped
[InlineData("fdc3.getIntentResult", typeof(IFdc3Commands))]
[InlineData("window.setContentSize", typeof(IWindowCommands))]
public void Commands_are_named_from_their_interface_methods(string name, Type declaringInterface)
{
var command = IpcContract.IpcContract.Find(name);
Expand Down Expand Up @@ -82,6 +83,35 @@ public async Task Command_errors_propagate_unwrapped()
Assert.Equal(Fdc3Errors.NoChannelFound, error.Message);
}

[Fact]
public async Task Window_setContentSize_binds_a_camelCase_payload()
{
var window = new RecordingWindow();

var result = await Invoke(window, "window.setContentSize", """{ "width": 480, "height": 320 }""");

Assert.Null(result);
Assert.Equal(new ContentSize(480, 320), window.Applied);
}

[Fact]
public async Task Window_setContentSize_without_a_payload_is_InvalidArguments()
{
var window = new RecordingWindow();

var error = await Assert.ThrowsAsync<IpcInvalidPayloadException>(() => Invoke(window, "window.setContentSize", null));

Assert.Equal("InvalidArguments", error.Message);
Assert.Null(window.Applied);
}

private sealed class RecordingWindow : IWindowCommands
{
public ContentSize? Applied { get; private set; }

public void SetContentSize(ContentSize size) => Applied = size;
}

private static Task<object?> Invoke(object target, string name, string? payload) =>
IpcContract.IpcContract.Find(name)!.InvokeAsync(
target,
Expand Down
98 changes: 98 additions & 0 deletions src/Bermuda.IpcContract/ContentWindowFit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
namespace Bermuda.IpcContract;

/// <summary>
/// Turns a <see cref="ContentSize"/> reported by a page into an outer window
/// placement. The host measures DPI, page zoom, the caption strip and the
/// monitor work area; this applies them. Pure so the rules can be tested
/// without a window.
/// </summary>
public static class ContentWindowFit
{
/// <summary>
/// <paramref name="PageZoom"/> is the WebView zoom (1 = 100%).
/// <paramref name="RasterX"/>/<paramref name="RasterY"/> are physical pixels
/// per CSS pixel; <paramref name="DipX"/>/<paramref name="DipY"/> are physical
/// pixels per device-independent pixel. On a per-monitor-aware window those
/// two scales match, so they cancel and a CSS pixel is one DIP before zoom.
/// </summary>
public readonly record struct Scales(double PageZoom, double RasterX, double RasterY, double DipX, double DipY);

/// <summary>Host chrome around the page, in DIPs: caption strip plus any resize border the page does not cover.</summary>
public readonly record struct Chrome(double Width, double Height);

/// <summary>Outer-window min/max in DIPs. Non-positive or non-finite limits are treated as unset.</summary>
public readonly record struct Constraints(double MinWidth, double MinHeight, double MaxWidth, double MaxHeight);

/// <summary>Monitor work area in DIPs.</summary>
public readonly record struct Area(double Left, double Top, double Width, double Height);

/// <summary>Outer window placement in DIPs.</summary>
public readonly record struct Placement(double Left, double Top, double Width, double Height);

/// <summary>
/// Returns null when <paramref name="content"/> is non-positive, so a page
/// that reports 0 while it is hidden does not collapse the window.
/// </summary>
public static Placement? Fit(
ContentSize content,
Scales scales,
Chrome chrome,
Constraints constraints,
Area workArea,
double currentLeft,
double currentTop)
{
var contentW = CssToDip(content.Width, scales.PageZoom, scales.RasterX, scales.DipX);
var contentH = CssToDip(content.Height, scales.PageZoom, scales.RasterY, scales.DipY);
if (contentW <= 0 || contentH <= 0) return null;

var desiredW = Math.Ceiling(contentW + NonNegative(chrome.Width));
var desiredH = Math.Ceiling(contentH + NonNegative(chrome.Height));

var clampToWork = workArea.Width > 0 && workArea.Height > 0
&& double.IsFinite(workArea.Width) && double.IsFinite(workArea.Height);

var maxW = Cap(constraints.MaxWidth, clampToWork ? workArea.Width : double.PositiveInfinity);
var maxH = Cap(constraints.MaxHeight, clampToWork ? workArea.Height : double.PositiveInfinity);
// A minimum larger than the work area (or the maximum) would push the
// window off the monitor, so the smaller limit wins.
var minW = Math.Min(NonNegative(constraints.MinWidth), maxW);
var minH = Math.Min(NonNegative(constraints.MinHeight), maxH);

var width = Math.Clamp(desiredW, minW, maxW);
var height = Math.Clamp(desiredH, minH, maxH);

var left = currentLeft;
var top = currentTop;
if (clampToWork)
{
var maxLeft = workArea.Left + workArea.Width - width;
var maxTop = workArea.Top + workArea.Height - height;
left = maxLeft >= workArea.Left ? Math.Clamp(left, workArea.Left, maxLeft) : workArea.Left;
top = maxTop >= workArea.Top ? Math.Clamp(top, workArea.Top, maxTop) : workArea.Top;
}

return new Placement(left, top, width, height);
}

/// <summary>CSS pixels → DIPs. See <see cref="Scales"/> for what the scales mean.</summary>
public static double CssToDip(double cssPixels, double pageZoom, double rasterScale, double dipScale)
{
if (!double.IsFinite(cssPixels) || cssPixels <= 0) return 0;
if (!double.IsFinite(pageZoom) || pageZoom <= 0) pageZoom = 1;
if (!double.IsFinite(dipScale) || dipScale <= 0) dipScale = 1;
if (!double.IsFinite(rasterScale) || rasterScale <= 0) rasterScale = dipScale;
return cssPixels * pageZoom * rasterScale / dipScale;
}

private static double Cap(double constraint, double workLimit)
{
var cap = double.IsFinite(constraint) && constraint > 0 ? constraint : double.PositiveInfinity;
if (double.IsFinite(workLimit) && workLimit > 0)
cap = Math.Min(cap, workLimit);
return cap;
}

private static double NonNegative(double value) =>
double.IsFinite(value) && value > 0 ? value : 0;
}
25 changes: 25 additions & 0 deletions src/Bermuda.IpcContract/IpcModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,31 @@ public interface IHostCommands
SimulateNotificationActivationResult SimulateNotificationActivation(SimulateNotificationActivationRequest request);
}

/// <summary>
/// CSS-pixel size of the page content a window should fit. Width and height are
/// the content box the page measured (a <c>ResizeObserver</c> border box), not
/// the outer window.
/// </summary>
public sealed record ContentSize(int Width, int Height);

/// <summary>
/// Per-window commands (<c>window.*</c>) on the <c>invoke()</c> channel.
/// The host window the page is shown in implements them. Nothing is resized
/// until a page calls <c>window.setContentSize</c>.
/// </summary>
[IpcCommands("window")]
public interface IWindowCommands
{
/// <summary>
/// Sizes this window so its content area fits <paramref name="size"/>.
/// The host converts those CSS pixels to device-independent pixels, adds
/// the caption strip (and the resize border kept outside the page), then
/// clamps to the monitor work area and the window's min/max. A non-positive
/// size is ignored.
/// </summary>
void SetContentSize(ContentSize size);
}

/// <summary>
/// A postMessage request from the web side to <c>HostBridge</c>. <c>Payload</c>
/// is deliberately untyped (its shape depends on <c>Command</c>).
Expand Down
5 changes: 5 additions & 0 deletions src/Bermuda.Web/e2e/fdc3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ test.describe('FDC3', () => {
expect(opened.appId).toBe('fdc3-probe')
await expect(probe.locator('#status')).toHaveText('fdc3 ready')
await expect(probe.locator('#received li')).toHaveText(['probe-7'])
// The probe opts in to content sizing, so the window leaves the default 1280×768.
await expect.poll(async () => probe.evaluate(async () => await window.chrome.webview.hostObjects.windowController.Width), {
message: 'probe window should shrink to its content',
}).toBeLessThan(800)
await expect.poll(async () => probe.evaluate(async () => await window.chrome.webview.hostObjects.windowController.Height)).toBeLessThan(600)
} finally {
const closed = probe.waitForEvent('close')
await probe.evaluate(() => window.chrome.webview.hostObjects.windowController.Close()).catch(() => {})
Expand Down
Loading
Loading