internal static void EnsureWindowIsInVirtualScreenWithInjection()

in src/AccessibilityInsights/MainWindowHelpers/Initialize.cs [444:496]


        internal static void EnsureWindowIsInVirtualScreenWithInjection(AppLayout layout,
            double windowTop, double windowLeft, double virtualLeft,
            double virtualTop, double virtualWidth, double virtualHeight)
        {
            double virtualRight = virtualLeft + virtualWidth;
            double virtualBottom = virtualTop + virtualHeight;

            if (layout != null)
            {
                const double fallbackWindowPosition = 200.0;
                layout.Top = GetFirstDefinedNumber(new double[] { layout.Top, windowTop, fallbackWindowPosition });
                layout.Left = GetFirstDefinedNumber(new double[] { layout.Left, windowLeft, fallbackWindowPosition });

                double top = layout.Top;
                double left = layout.Left;
                double right = left + layout.Width;
                double bottom = top + layout.Height;

                // If the window is completely offscreen, open it in default location
                if ((right <= virtualLeft) ||
                    (bottom <= virtualTop) ||
                    (left >= virtualRight) ||
                    (top >= virtualBottom))
                {
                    top = layout.Top = windowTop;
                    left = layout.Left = windowLeft;
                }

                // Adjust the window extents to keep it fully within the virtual screen
                layout.Width = Math.Min(layout.Width, virtualWidth);
                layout.Height = Math.Min(layout.Height, virtualHeight);
                right = left + layout.Width;
                bottom = top + layout.Height;

                if (right > virtualRight)
                {
                    layout.Left = virtualRight - layout.Width;
                }
                else if (left < virtualLeft)
                {
                    layout.Left = virtualLeft;
                }

                if (bottom > virtualBottom)
                {
                    layout.Top = virtualBottom - layout.Height;
                }
                else if (top < virtualTop)
                {
                    layout.Top = virtualTop;
                }
            }
        }