Skip to content

Commit

Permalink
fix: Clamp Popup placement in visible bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Apr 14, 2022
1 parent 82aabd6 commit 09a2c58
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/Popup/PlacementPopupPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,66 @@ ref controlYPos
{
this.Log().LogDebug($"Calculated placement, finalRect={finalRect}");
}

// Clamp the final rect to be within visible bounds. This can happen for example
// when the user is trying to show a flyout at Window.Current.Content - which will
// match Top position, which would be outside the visible bounds (negative Y).
// We nudge the position according to the FlowDirection (so in case the popup
// does not fit, the "start of sentences" are ideally visible.

if (finalRect.Bottom > visibleBounds.Bottom)
{
finalRect = new Rect(
finalRect.Left,
visibleBounds.Bottom - finalRect.Height,
finalRect.Width,
finalRect.Height);
}

if (finalRect.Top < visibleBounds.Top)
{
finalRect = new Rect(
finalRect.Left,
visibleBounds.Top,
finalRect.Width,
finalRect.Height);
}

void NudgeLeft()
{
if (finalRect.Left < visibleBounds.Left)
{
finalRect = new Rect(
visibleBounds.Left,
finalRect.Top,
finalRect.Width,
finalRect.Height);
}
}

void NudgeRight()
{
if (finalRect.Right > visibleBounds.Right)
{
finalRect = new Rect(
visibleBounds.Right - finalRect.Width,
finalRect.Top,
finalRect.Width,
finalRect.Height);
}
}

if (FlowDirection == FlowDirection.LeftToRight)
{
NudgeRight();
NudgeLeft();
}
else
{
NudgeLeft();
NudgeRight();
}

return finalRect;
}

Expand Down

0 comments on commit 09a2c58

Please sign in to comment.