Skip to content

Commit 4e327ed

Browse files
Still apply Screen size algorithm for UWP and annotate calculation
1 parent 3401db0 commit 4e327ed

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

Microsoft.Toolkit.Uwp.UI.Controls.Input/RichSuggestBox/RichSuggestBox.Helpers.cs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,41 +21,57 @@ public partial class RichSuggestBox
2121
private static bool IsElementOnScreen(FrameworkElement element, double offsetX = 0, double offsetY = 0)
2222
{
2323
// DisplayInformation only works in UWP. No alternative to get DisplayInformation.ScreenHeightInRawPixels
24+
// Or Window position in Window.Current.Bounds
2425
// Tracking issues:
2526
// https://github.com/microsoft/WindowsAppSDK/issues/114
2627
// https://github.com/microsoft/microsoft-ui-xaml/issues/4228
2728
// TODO: Remove when DisplayInformation.ScreenHeightInRawPixels alternative is available
28-
return true;
29-
30-
#pragma warning disable CS0162 // Unreachable code detected
3129
if (Window.Current == null)
3230
{
33-
return !ControlHelpers.IsXamlRootAvailable || element.XamlRoot.IsHostVisible;
31+
return true;
3432
}
3533

36-
var toWindow = element.TransformToVisual(null);
34+
// Get bounds of element from root of tree
35+
var elementBounds = element.CoordinatesFrom(null).ToRect(element.ActualWidth, element.ActualHeight);
36+
37+
// Apply offset
38+
elementBounds.X += offsetX;
39+
elementBounds.Y += offsetY;
40+
41+
// Get Window position
3742
var windowBounds = Window.Current.Bounds;
38-
var elementBounds = new Rect(offsetX, offsetY, element.ActualWidth, element.ActualHeight);
39-
elementBounds = toWindow.TransformBounds(elementBounds);
43+
44+
// Offset Element within Window on Screen
4045
elementBounds.X += windowBounds.X;
4146
elementBounds.Y += windowBounds.Y;
47+
48+
// Get Screen DPI info
4249
var displayInfo = DisplayInformation.GetForCurrentView();
4350
var scaleFactor = displayInfo.RawPixelsPerViewPixel;
4451
var displayHeight = displayInfo.ScreenHeightInRawPixels;
52+
53+
// Check if top/bottom are within confines of screen
4554
return elementBounds.Top * scaleFactor >= 0 && elementBounds.Bottom * scaleFactor <= displayHeight;
46-
#pragma warning restore CS0162 // Unreachable code detected
4755
}
4856

4957
private static bool IsElementInsideWindow(FrameworkElement element, double offsetX = 0, double offsetY = 0)
5058
{
51-
var toWindow = element.TransformToVisual(null);
59+
// Get bounds of element from root of tree
60+
var elementBounds = element.CoordinatesFrom(null).ToRect(element.ActualWidth, element.ActualHeight);
61+
62+
// Apply offset
63+
elementBounds.X += offsetX;
64+
elementBounds.Y += offsetY;
65+
66+
// Get size of window itself
5267
var windowBounds = ControlHelpers.IsXamlRootAvailable
5368
? element.XamlRoot.Size.ToRect()
54-
: ApplicationView.GetForCurrentView().VisibleBounds;
55-
windowBounds = new Rect(0, 0, windowBounds.Width, windowBounds.Height);
56-
var elementBounds = new Rect(offsetX, offsetY, element.ActualWidth, element.ActualHeight);
57-
elementBounds = toWindow.TransformBounds(elementBounds);
69+
: ApplicationView.GetForCurrentView().VisibleBounds.ToSize().ToRect(); // Normalize
70+
71+
// Calculate if there's an intersection
5872
elementBounds.Intersect(windowBounds);
73+
74+
// See if we are still fully visible within the Window
5975
return elementBounds.Height >= element.ActualHeight;
6076
}
6177

Microsoft.Toolkit.Uwp/Extensions/RectExtensions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
using System.Diagnostics.Contracts;
66
using System.Runtime.CompilerServices;
7+
using Windows.Foundation;
78
using Rect = Windows.Foundation.Rect;
9+
using Size = Windows.Foundation.Size;
810

911
namespace Microsoft.Toolkit.Uwp
1012
{
@@ -33,5 +35,17 @@ public static bool IntersectsWith(this Rect rect1, Rect rect2)
3335
(rect1.Top <= rect2.Bottom) &&
3436
(rect1.Bottom >= rect2.Top);
3537
}
38+
39+
/// <summary>
40+
/// Creates a new <see cref="Size"/> of the specified <see cref="Rect"/>'s width and height.
41+
/// </summary>
42+
/// <param name="rect">Rectangle to size.</param>
43+
/// <returns>Size of rectangle.</returns>
44+
[Pure]
45+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
46+
public static Size ToSize(this Rect rect)
47+
{
48+
return new Size(rect.Width, rect.Height);
49+
}
3650
}
3751
}

0 commit comments

Comments
 (0)