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)); } }
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); } } } }
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); }