@@ -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
0 commit comments