Example #1
0
        private static void WindowHidden(IntPtr hwnd)
        {
            Logger.Verbose($"Processing newly hidden window ({hwnd})...");

            var windowInfo = Find(hwnd);

            if (windowInfo == null)
            {
                var message = $"This window was not handled by the {nameof(WindowManager)} ({hwnd})";
                Logger.Verbose(message);
                return;
            }

            windowInfo.IsShown = false;
            AllWindowsList.Remove(windowInfo);

            if (MainWindow != null && MainWindow.Equals(windowInfo))
            {
                Logger.Info($"Main window ({hwnd}) closed.");
                MainWindow = null;
            }
            else if (windowInfo.IsBlocking)
            {
                Logger.Info($"Blocking window ({hwnd}) closed.");
                var index = BlockingWindowsList.IndexOf(windowInfo);
                if (index < 0)
                {
                    throw new InvalidOperationException("An unregistered blocking window has been closed.");
                }
                BlockingWindowsList.RemoveAt(index);
                windowInfo.IsBlocking = false;
                if (MainWindow != null && MainWindow.IsShown && BlockingWindowsList.Count == 0 && ModalWindows.Count == 0)
                {
                    Logger.Verbose($"Main window ({MainWindow.Hwnd}) enabled because no more modal nor blocking windows are visible.");
                    MainWindow.IsDisabled = false;
                }
                ActivateMainWindow();
            }
            else
            {
                // Note: We cannot check windowInfo.IsModal anymore at that point because the window is closed.
                var index = ModalWindowsList.IndexOf(windowInfo);
                if (index >= 0)
                {
                    Logger.Info($"Modal window ({hwnd}) closed.");
                    ModalWindowsList.RemoveAt(index);

                    if (ModalWindowsList.Count == 0)
                    {
                        foreach (var blockingWindow in BlockingWindowsList)
                        {
                            Logger.Verbose($"Blocking window ({blockingWindow.Hwnd}) enabled because no more modal windows are visible.");
                            blockingWindow.IsDisabled = false;
                        }
                        if (MainWindow != null && MainWindow.IsShown && BlockingWindowsList.Count == 0 && ModalWindows.Count == 0)
                        {
                            Logger.Verbose($"Main window ({MainWindow.Hwnd}) enabled because no more modal nor blocking windows are visible.");
                            MainWindow.IsDisabled = false;
                        }
                        // re-activate only after all popups have closed, since some popups are spawned from popups themselves,
                        // when their original parent closes, reactivating the main window causes the still living children to close.
                        ActivateMainWindow();
                    }
                }
            }
        }
Example #2
0
        private static void WindowShown(IntPtr hwnd)
        {
            if (!HwndHelper.HasStyleFlag(hwnd, NativeHelper.WS_VISIBLE))
            {
                Logger.Debug($"Discarding non-visible window ({hwnd})");
                return;
            }

            Logger.Verbose($"Processing newly shown window ({hwnd})...");
            var windowInfo = Find(hwnd);

            if (windowInfo == null)
            {
                windowInfo = new WindowInfo(hwnd);

                if (Debugger.IsAttached)
                {
                    // Some external processes might attach a window to ours, we want to discard them.
                    foreach (var debugWindowTypeName in DebugWindowTypeNames)
                    {
                        if (windowInfo.Window?.GetType().FullName.StartsWith(debugWindowTypeName) ?? false)
                        {
                            Logger.Debug($"Discarding debug/diagnostics window '{windowInfo.Window.GetType().FullName}' ({hwnd})");
                            return;
                        }
                    }
                }

                AllWindowsList.Add(windowInfo);
            }
            windowInfo.IsShown = true;

            if (windowInfo == MainWindow)
            {
                Logger.Info($"Main window ({hwnd}) shown.");
                foreach (var blockingWindow in BlockingWindowsList)
                {
                    Logger.Debug($"Setting owner of exiting blocking window {blockingWindow.Hwnd} to be the main window ({hwnd}).");
                    blockingWindow.Owner = MainWindow;
                }
                if (ModalWindowsList.Count > 0 || BlockingWindowsList.Count > 0)
                {
                    Logger.Verbose($"Main window ({MainWindow.Hwnd}) disabled because a modal or blocking window is already visible.");
                    MainWindow.IsDisabled = true;
                }
            }
            else if (windowInfo.IsBlocking)
            {
                Logger.Info($"Blocking window ({hwnd}) shown.");
                if (MainWindow != null && MainWindow.IsShown)
                {
                    Logger.Verbose($"Main window ({MainWindow.Hwnd}) disabled by new blocking window.");
                    MainWindow.IsDisabled = true;
                }
                if (ModalWindowsList.Count > 0)
                {
                    Logger.Verbose($"Blocking window ({hwnd}) disabled because a modal is already visible.");
                    windowInfo.IsDisabled = true;
                }
            }
            else if (windowInfo.IsModal)
            {
                Logger.Info($"Modal window ({hwnd}) shown.");
                ModalWindowsList.Add(windowInfo);
            }
        }