@@ -21,41 +21,57 @@ public partial class RichSuggestBox
21
21
private static bool IsElementOnScreen ( FrameworkElement element , double offsetX = 0 , double offsetY = 0 )
22
22
{
23
23
// DisplayInformation only works in UWP. No alternative to get DisplayInformation.ScreenHeightInRawPixels
24
+ // Or Window position in Window.Current.Bounds
24
25
// Tracking issues:
25
26
// https://github.com/microsoft/WindowsAppSDK/issues/114
26
27
// https://github.com/microsoft/microsoft-ui-xaml/issues/4228
27
28
// TODO: Remove when DisplayInformation.ScreenHeightInRawPixels alternative is available
28
- return true ;
29
-
30
- #pragma warning disable CS0162 // Unreachable code detected
31
29
if ( Window . Current == null )
32
30
{
33
- return ! ControlHelpers . IsXamlRootAvailable || element . XamlRoot . IsHostVisible ;
31
+ return true ;
34
32
}
35
33
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
37
42
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
40
45
elementBounds . X += windowBounds . X ;
41
46
elementBounds . Y += windowBounds . Y ;
47
+
48
+ // Get Screen DPI info
42
49
var displayInfo = DisplayInformation . GetForCurrentView ( ) ;
43
50
var scaleFactor = displayInfo . RawPixelsPerViewPixel ;
44
51
var displayHeight = displayInfo . ScreenHeightInRawPixels ;
52
+
53
+ // Check if top/bottom are within confines of screen
45
54
return elementBounds . Top * scaleFactor >= 0 && elementBounds . Bottom * scaleFactor <= displayHeight ;
46
- #pragma warning restore CS0162 // Unreachable code detected
47
55
}
48
56
49
57
private static bool IsElementInsideWindow ( FrameworkElement element , double offsetX = 0 , double offsetY = 0 )
50
58
{
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
52
67
var windowBounds = ControlHelpers . IsXamlRootAvailable
53
68
? 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
58
72
elementBounds . Intersect ( windowBounds ) ;
73
+
74
+ // See if we are still fully visible within the Window
59
75
return elementBounds . Height >= element . ActualHeight ;
60
76
}
61
77
0 commit comments