File: src\Aspire.Tray\Common\WindowsTrayPanelPlacement.cs
Web Access
Project: src\src\Aspire.Tray\Windows\Aspire.Tray.Windows.csproj (aspire-tray)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
namespace Aspire.Tray;
 
internal static class WindowsTrayPanelPlacement
{
    internal static double GetMaximumVerticalSpace(PanelRect workarea, PanelRect anchor, double margin)
    {
        var above = Math.Max(0, anchor.Y - workarea.Y - 2 * margin);
        var below = Math.Max(0, workarea.Bottom - anchor.Bottom - 2 * margin);
        return Math.Max(above, below);
    }
 
    internal static PanelRect PlaceAboveAnchor(PanelRect workarea, PanelRect anchor, double width, double height, double margin)
    {
        var left = workarea.X + margin;
        var top = workarea.Y + margin;
        var right = Math.Max(left, workarea.Right - width - margin);
        var bottom = Math.Max(top, workarea.Bottom - height - margin);
        var x = Math.Clamp(anchor.CenterX - width / 2, left, right);
        var above = anchor.Y - margin - height;
        if (above >= top)
        {
            return new(x, above, width, height);
        }
 
        var below = anchor.Bottom + margin;
        if (below <= bottom)
        {
            return new(x, below, width, height);
        }
 
        var y = Math.Clamp(anchor.CenterY - height / 2, top, bottom);
        var leftOfAnchor = anchor.X - margin - width;
        if (leftOfAnchor >= left)
        {
            return new(leftOfAnchor, y, width, height);
        }
 
        var rightOfAnchor = anchor.Right + margin;
        if (rightOfAnchor + width <= workarea.Right - margin)
        {
            return new(rightOfAnchor, y, width, height);
        }
 
        return new(x, Math.Clamp(above, top, bottom), width, height);
    }
}