internal static bool IsPopupAnchorPopupEnabled(IGamePopupAnchor popupAnchor) { var popupAnchorObj = popupAnchor as DependencyObject; if (popupAnchorObj == null) { return(true); } var popupContent = GetPopupContent(popupAnchorObj) as UIElement; if (popupContent == null) { return(true); } // Recurse through logical children... if there are none found, assume enabled var isEnabled = false; if (!IsPopupAnchorPopupEnabledRecursive(popupContent, ref isEnabled)) { isEnabled = true; } return(isEnabled); }
private static FrameworkElement GetPopupHost(IGamePopupAnchor popupAnchor) { if (popupAnchor != null && popupAnchor.Popup != null) { return(popupAnchor.Popup.PopupRoot); } return(null); }
private void ClosePopupCore(IGamePopupAnchor popupAnchor, GamePopupCloseReason closeReason) { // Update the close reason (must happen before changing mouse capture) if (popupAnchor.LastCloseReason == GamePopupCloseReason.Unknown) { popupAnchor.LastCloseReason = closeReason; } // Get the popup host var popupHost = GetPopupHost(popupAnchor); // Update the captured popup if ((popupHost != null) && (Mouse.Captured == popupHost)) { if (_popupAnchors.Count > 0) { Mouse.Capture(GetPopupHost(_popupAnchors[_popupAnchors.Count - 1]), CaptureMode.SubTree); } else { Mouse.Capture(null); } } if (popupAnchor.IsPopupOpen) { popupAnchor.IsPopupOpen = false; } if (popupHost != null) { popupHost.KeyDown -= OnKeyDown; popupHost.LostMouseCapture -= OnLostMouseCapture; popupHost.PreviewKeyDown -= OnPreviewKeyDown; popupHost.RemoveHandler( Mouse.PreviewMouseDownOutsideCapturedElementEvent, (MouseButtonEventHandler)OnClickThrough); popupHost.RemoveHandler( Mouse.PreviewMouseUpOutsideCapturedElementEvent, (MouseButtonEventHandler)OnClickThrough); } var popupAnchorElement = popupAnchor as UIElement; if ((popupAnchorElement != null) && (_popupAnchors.Count == 0)) { popupAnchorElement.IsKeyboardFocusWithinChanged -= OnIsKeyboardFocusWithinChanged; } return; }
internal static bool IsPopupBelowAnchor(IGamePopupAnchor popupAnchor) { var popupAnchorControl = popupAnchor as UIElement; if ((popupAnchorControl != null) && (popupAnchor.Popup != null)) { var popupChild = popupAnchor.Popup.Child; if (popupChild != null) { var popupAnchorLocation = popupAnchorControl.PointToScreen( new Point( 0, popupAnchorControl.RenderSize.Height / 2)); var popupControlLocation = popupChild.PointToScreen( new Point( 0, popupChild.RenderSize.Height / 2)); return(popupAnchorLocation.Y < popupControlLocation.Y); } } return(true); }
internal bool ClosePopup(IGamePopupAnchor popupAnchor, GamePopupCloseReason closeReason) { // If the popup is not currently being managed, simply call the core close code on it if (!_popupAnchors.Contains(popupAnchor)) { return(false); } while (_popupAnchors.Count > 0) { var isRootPopupAnchor = (RootPopupAnchor == popupAnchor); var isTargetPopupAnchor = (TopmostPopupAnchor == popupAnchor); if (!CloseTopmostPopup(closeReason)) { return(false); } if (!isTargetPopupAnchor) { continue; } // Focus the anchor if the escape key was pressed and the popup was not originally opened with the mouse if ((closeReason == GamePopupCloseReason.EscapeKeyPressed) && (!isRootPopupAnchor || !popupAnchor.PopupOpenedWithMouse) && popupAnchor.Focusable) { popupAnchor.Focus(); } return(true); } return(false); }
internal bool OpenPopup(IGamePopupAnchor popupAnchor) { if (popupAnchor == null) { return(false); } var parentPopupAnchor = GetParentPopupAnchor(popupAnchor as DependencyObject); while ((parentPopupAnchor != null) && (!parentPopupAnchor.IsPopupOpen)) { // Only use parent popup anchors that have open popups since there are cases like where Game has a minimized popup open // and a control inside an uncollapsed Group is clicked... without this, the Game minimized popup is closed inappropriately parentPopupAnchor = GetParentPopupAnchor(parentPopupAnchor as DependencyObject); } if (parentPopupAnchor != null) { // Close any popup anchors that don't share the same ancestry while (_popupAnchors.Count > 0) { // Quit since a common ancestor was found if (TopmostPopupAnchor == parentPopupAnchor) { break; } // Close the topmost popup CloseTopmostPopup(GamePopupCloseReason.OtherPopupOpened); } } else { // Close all open popups since the new one has no parent CloseAllPopupsCore(GamePopupCloseReason.OtherPopupOpened); } // Get the popup host var popupHost = GetPopupHost(popupAnchor); if (popupHost == null) { return(false); } popupAnchor.IgnoreNextLeftRelease = (Mouse.LeftButton == MouseButtonState.Pressed); popupAnchor.IgnoreNextRightRelease = (Mouse.RightButton == MouseButtonState.Pressed); // Reset the close reason popupAnchor.LastCloseReason = GamePopupCloseReason.Unknown; // Add the popup to the stack _popupAnchors.Insert(0, popupAnchor); // If the popup is not open... if (!popupAnchor.IsPopupOpen) { // Open the popup popupAnchor.IsPopupOpen = true; } // Attach to events popupHost.KeyDown += OnKeyDown; popupHost.LostMouseCapture += OnLostMouseCapture; popupHost.PreviewKeyDown += OnPreviewKeyDown; var popupAnchorElement = popupAnchor as UIElement; if (popupAnchorElement != null) { // If this is the first popup, attach to the root popup's keyboard focus change event if (_popupAnchors.Count == 1) { popupAnchorElement.IsKeyboardFocusWithinChanged += OnIsKeyboardFocusWithinChanged; } // Focus the popup popupAnchorElement.Dispatcher.BeginInvoke( DispatcherPriority.Input, (Action) delegate { popupAnchorElement.UpdateLayout(); if (popupAnchor.Popup == null || !popupAnchor.IsPopupOpen) { return; } var focusableElement = popupAnchor.Popup.Child.FindFirstFocusableDescendant <UIElement>(); if (focusableElement is GamePopupContentPresenter) { focusableElement = focusableElement.FindFirstFocusableDescendant <UIElement>() ?? focusableElement; } if (focusableElement != null) { focusableElement.Focus(); } Mouse.Capture(popupHost, CaptureMode.SubTree); popupHost.AddHandler( Mouse.PreviewMouseDownOutsideCapturedElementEvent, (MouseButtonEventHandler)OnClickThrough); popupHost.AddHandler( Mouse.PreviewMouseUpOutsideCapturedElementEvent, (MouseButtonEventHandler)OnClickThrough); }); } return(true); }
internal static bool IsTopLevel(IGamePopupAnchor popupAnchor) { return(GetParentPopupAnchor(popupAnchor as DependencyObject) == null); }
private static bool HasCapture(IGamePopupAnchor popupAnchor) { var popupHost = GetPopupHost(popupAnchor); return((popupHost != null) && (Mouse.Captured == popupHost)); }