Example #1
0
        private void SetFocusEngagement()
        {
            var pFocusManager = VisualTree.GetFocusManagerForElement(this);

            if (pFocusManager != null)
            {
                if (IsFocusEngaged)
                {
                    bool hasFocusedElement = FocusProperties.HasFocusedElement(this);
                    //Check to see if the element or any of it's descendants has focus
                    if (!hasFocusedElement)
                    {
                        IsFocusEngaged = false;
                        throw new InvalidOperationException("Can't engage focus when the control nor any of its descendants has focus.");
                    }
                    if (!IsFocusEngagementEnabled)
                    {
                        IsFocusEngaged = false;
                        throw new InvalidOperationException("Can't engage focus when IsFocusEngagementEnabled is false on the control.");
                    }

                    //Control is focused and has IsFocusEngagementEnabled set to true
                    pFocusManager.EngagedControl = this;
                    UpdateEngagementState(true /*engaging*/);
                }
                else if (pFocusManager.EngagedControl != null)                 //prevents re-entrancy because we set the property to false above in error cases.
                {
                    pFocusManager.EngagedControl = null; /*No control is now engaged*/;
                    UpdateEngagementState(false /*Disengage*/);

                    var popupRoot = VisualTree.GetPopupRootForElement(this);
                    popupRoot?.ClearWasOpenedDuringEngagementOnAllOpenPopups();
                }
            }
        }
Example #2
0
        protected virtual IEnumerable <DependencyObject>?GetChildrenInTabFocusOrder()
        {
            var children = FocusProperties.GetFocusChildren(this);

            if (children != null && /*!children->IsLeaving() && */ children.Length > 0)
            {
                return(children);
            }
            return(Array.Empty <DependencyObject>());
        }
Example #3
0
        internal static void ProcessControlFocused(Control control)
        {
            if (_log.Value.IsEnabled(LogLevel.Debug))
            {
                _log.Value.LogDebug($"{nameof(ProcessControlFocused)}() focusedElement={GetFocusedElement()}, control={control}");
            }

            if (FocusProperties.IsFocusable(control))
            {
                var focusManager = VisualTree.GetFocusManagerForElement(control);
                focusManager?.UpdateFocus(new FocusMovement(control, FocusNavigationDirection.None, FocusState.Pointer));
            }
        }
Example #4
0
        private protected override void OnLoaded()
        {
            base.OnLoaded();

            var spCurrentFocusedElement = this.GetFocusedElement();

            var  focusManager    = VisualTree.GetFocusManagerForElement(this);
            bool setDefaultFocus = focusManager?.IsPluginFocused() == true;

            if (setDefaultFocus && spCurrentFocusedElement == null)
            {
                // Uno specific: If the page is focusable itself, we want to
                // give it focus instead of the first element.
                if (FocusProperties.IsFocusable(this))
                {
                    this.SetFocusedElement(
                        this,
                        FocusState.Programmatic,
                        animateIfBringIntoView: false);
                    return;
                }

                // Set the focus on the first focusable control
                var spFirstFocusableElementCDO = focusManager?.GetFirstFocusableElement(this);

                if (spFirstFocusableElementCDO != null && focusManager != null)
                {
                    var spFirstFocusableElementDO = spFirstFocusableElementCDO;

                    focusManager.InitialFocus = true;

                    TrySetFocusedElement(spFirstFocusableElementDO);

                    focusManager.InitialFocus = false;
                }

                if (spFirstFocusableElementCDO == null)
                {
                    // Narrator listens for focus changed events to determine when the UI Automation tree needs refreshed. If we don't set default focus (on Phone) or if we fail to find a focusable element,
                    // we will need notify the narror of the UIA tree change when page is loaded.
                    var bAutomationListener = AutomationPeer.ListenerExistsHelper(AutomationEvents.AutomationFocusChanged);

                    if (bAutomationListener)
                    {
                        Uno.UI.Xaml.Core.CoreServices.Instance.UIARaiseFocusChangedEventOnUIAWindow(this);
                    }
                }
            }
        }
Example #5
0
        private static IAsyncOperation <FocusMovementResult> TryFocusAsyncImpl(
            DependencyObject pElement,
            FocusState focusState)
        {
            FocusManager?focusManager = VisualTree.GetFocusManagerForElement(pElement);

            if (focusManager == null)
            {
                throw new InvalidOperationException("Element is not part of the visual tree.");
            }

            FocusMovement movement = new FocusMovement(pElement, FocusNavigationDirection.None, focusState);

            var spFocusAsyncOperation = new FocusAsyncOperation(movement.CorrelationId);
            var asyncOperation        = spFocusAsyncOperation.CreateAsyncOperation();

            if (FocusProperties.IsFocusable(pElement) == false)
            {
                // We need to start and complete the async operation since this is a no-op

                spFocusAsyncOperation.CoreSetResults(new FocusMovementResult());
                spFocusAsyncOperation.CoreFireCompletion();
                return(asyncOperation);
            }

            // TODO Uno specific: Do not use async operations, only simulated
            //movement.ShouldCompleteAsyncOperation = focusManager.TrySetAsyncOperation(spFocusAsyncOperation);
            if (movement.ShouldCompleteAsyncOperation)
            {
                //spFocusAsyncOperation.StartOperation();
            }

            var result = focusManager.SetFocusedElement(movement);

            // TODO Uno specific: Simulate async completion.
            spFocusAsyncOperation?.CoreSetResults(result);
            spFocusAsyncOperation?.CoreFireCompletion();

            // Async operation is not guaranteed to be released synchronously.
            // Therefore, we let UpdateFocus to handle the responsibility of releasing it.
            //spFocusAsyncOperation.Detach();

            return(asyncOperation);
        }