Skip to content

Commit 1070cee

Browse files
committed
Xaml Islands Fixes for FocusManager.GetFocusedElement and ContentDialog's XamlRoot.
1 parent 5767037 commit 1070cee

File tree

17 files changed

+367
-34
lines changed

17 files changed

+367
-34
lines changed

Microsoft.Toolkit.Uwp.DeveloperTools/FocusTracker/FocusTracker.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,16 @@ private void ClearContent()
106106

107107
private void UpdateTimer_Tick(object sender, object e)
108108
{
109-
var focusedControl = FocusManager.GetFocusedElement() as FrameworkElement;
109+
FrameworkElement focusedControl;
110+
111+
if (Windows.Foundation.Metadata.ApiInformation.IsPropertyPresent("Windows.UI.Xaml.UIElement", "XamlRoot") && XamlRoot != null)
112+
{
113+
focusedControl = FocusManager.GetFocusedElement(XamlRoot) as FrameworkElement;
114+
}
115+
else
116+
{
117+
focusedControl = FocusManager.GetFocusedElement() as FrameworkElement;
118+
}
110119

111120
if (focusedControl == null)
112121
{

Microsoft.Toolkit.Uwp.SampleApp/Shell.Search.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System;
65
using System.Linq;
76
using Microsoft.Toolkit.Uwp.Helpers;
87
using Microsoft.Toolkit.Uwp.UI.Extensions;
@@ -16,7 +15,18 @@ public sealed partial class Shell
1615
{
1716
internal void StartSearch(string startingText = null)
1817
{
19-
if (FocusManager.GetFocusedElement() == SearchBox.FindDescendant<TextBox>())
18+
object focusedElement;
19+
20+
if (Windows.Foundation.Metadata.ApiInformation.IsPropertyPresent("Windows.UI.Xaml.UIElement", "XamlRoot") && XamlRoot != null)
21+
{
22+
focusedElement = FocusManager.GetFocusedElement(XamlRoot);
23+
}
24+
else
25+
{
26+
focusedElement = FocusManager.GetFocusedElement();
27+
}
28+
29+
if (focusedElement == SearchBox.FindDescendant<TextBox>())
2030
{
2131
return;
2232
}

Microsoft.Toolkit.Uwp.UI.Animations/ApiInformationHelper.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,7 @@ internal class ApiInformationHelper
2020

2121
public static bool IsFallCreatorsUpdateOrAbove => (bool)(_isFallCreatorsUpdateOrAbove ??
2222
(_isFallCreatorsUpdateOrAbove = ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 5)));
23+
24+
public static bool IsXamlRootAvailable { get; } = ApiInformation.IsPropertyPresent("Windows.UI.Xaml.UIElement", "XamlRoot");
2325
}
2426
}

Microsoft.Toolkit.Uwp.UI.Animations/Behaviors/QuickReturnHeaderBehavior.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,15 @@ private void ScrollViewer_GotFocus(object sender, RoutedEventArgs e)
258258
{
259259
var scroller = (ScrollViewer)sender;
260260

261-
var focusedElement = FocusManager.GetFocusedElement();
261+
object focusedElement;
262+
if (ApiInformationHelper.IsXamlRootAvailable && scroller.XamlRoot != null)
263+
{
264+
focusedElement = FocusManager.GetFocusedElement(scroller.XamlRoot);
265+
}
266+
else
267+
{
268+
focusedElement = FocusManager.GetFocusedElement();
269+
}
262270

263271
if (focusedElement is UIElement element)
264272
{

Microsoft.Toolkit.Uwp.UI.Animations/Behaviors/StickyHeaderBehavior.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,15 @@ private void ScrollViewer_GotFocus(object sender, RoutedEventArgs e)
236236
{
237237
var scroller = (ScrollViewer)sender;
238238

239-
var focusedElement = FocusManager.GetFocusedElement();
239+
object focusedElement;
240+
if (ApiInformationHelper.IsXamlRootAvailable && scroller.XamlRoot != null)
241+
{
242+
focusedElement = FocusManager.GetFocusedElement(scroller.XamlRoot);
243+
}
244+
else
245+
{
246+
focusedElement = FocusManager.GetFocusedElement();
247+
}
240248

241249
if (focusedElement is UIElement element)
242250
{

Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGrid.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5730,7 +5730,7 @@ private void DataGrid_LostFocus(object sender, RoutedEventArgs e)
57305730

57315731
// Walk up the visual tree of the newly focused element
57325732
// to determine if focus is still within DataGrid.
5733-
object focusedObject = FocusManager.GetFocusedElement();
5733+
object focusedObject = GetFocusedElement();
57345734
DependencyObject focusedDependencyObject = focusedObject as DependencyObject;
57355735

57365736
while (focusedDependencyObject != null)
@@ -5796,6 +5796,18 @@ private void DataGrid_LostFocus(object sender, RoutedEventArgs e)
57965796
}
57975797
}
57985798

5799+
private object GetFocusedElement()
5800+
{
5801+
if (TypeHelper.IsXamlRootAvailable && XamlRoot != null)
5802+
{
5803+
return FocusManager.GetFocusedElement(XamlRoot);
5804+
}
5805+
else
5806+
{
5807+
return FocusManager.GetFocusedElement();
5808+
}
5809+
}
5810+
57995811
private void DataGrid_PointerEntered(object sender, PointerRoutedEventArgs e)
58005812
{
58015813
if (e.Pointer.PointerDeviceType != PointerDeviceType.Touch)
@@ -6001,7 +6013,7 @@ private bool EndCellEdit(DataGridEditAction editAction, bool exitEditingMode, bo
60016013

60026014
// TODO: Figure out if we should restore a cached this.IsTabStop.
60036015
this.IsTabStop = true;
6004-
if (keepFocus && editingElement.ContainsFocusedElement())
6016+
if (keepFocus && editingElement.ContainsFocusedElement(this))
60056017
{
60066018
this.Focus(FocusState.Programmatic);
60076019
}
@@ -6240,7 +6252,7 @@ private void FlushCurrentCellChanged()
62406252
_previousAutomationFocusCoordinates = new DataGridCellCoordinates(this.CurrentCellCoordinates);
62416253

62426254
// If the DataGrid itself has focus, we want to move automation focus to the new current element
6243-
object focusedObject = FocusManager.GetFocusedElement();
6255+
object focusedObject = GetFocusedElement();
62446256
if (focusedObject == this && AutomationPeer.ListenerExists(AutomationEvents.AutomationFocusChanged))
62456257
{
62466258
peer.RaiseAutomationFocusChangedEvent(this.CurrentSlot, this.CurrentColumnIndex);
@@ -6295,7 +6307,7 @@ private bool FocusEditingCell(bool setFocus)
62956307
DataGridCell dataGridCell = this.EditingRow.Cells[_editingColumnIndex];
62966308
if (setFocus)
62976309
{
6298-
if (dataGridCell.ContainsFocusedElement())
6310+
if (dataGridCell.ContainsFocusedElement(this))
62996311
{
63006312
success = true;
63016313
}
@@ -7058,7 +7070,7 @@ private bool ProcessEnterKey(bool shift, bool ctrl)
70587070
}
70597071

70607072
// If Enter was used by a TextBox, we shouldn't handle the key
7061-
TextBox focusedTextBox = FocusManager.GetFocusedElement() as TextBox;
7073+
TextBox focusedTextBox = GetFocusedElement() as TextBox;
70627074
if (focusedTextBox != null && focusedTextBox.AcceptsReturn)
70637075
{
70647076
return false;

Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/Utilities/Extensions.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,23 @@ internal static bool ContainsChild(this DependencyObject element, DependencyObje
6868
/// the currently focused element, which is updated synchronously.
6969
/// </summary>
7070
/// <param name="element">Parent DependencyObject</param>
71+
/// <param name="uiElement">Parent UIElement. Used to query the element's XamlRoot.</param>
7172
/// <returns>True if the currently focused element is within the visual tree of the parent</returns>
72-
internal static bool ContainsFocusedElement(this DependencyObject element)
73+
internal static bool ContainsFocusedElement(this DependencyObject element, UIElement uiElement)
7374
{
74-
return (element == null) ? false : element.ContainsChild(FocusManager.GetFocusedElement() as DependencyObject);
75+
return (element == null) ? false : element.ContainsChild(GetFocusedElement(uiElement) as DependencyObject);
76+
}
77+
78+
private static object GetFocusedElement(UIElement uiElement)
79+
{
80+
if (TypeHelper.IsXamlRootAvailable && uiElement.XamlRoot != null)
81+
{
82+
return FocusManager.GetFocusedElement(uiElement.XamlRoot);
83+
}
84+
else
85+
{
86+
return FocusManager.GetFocusedElement();
87+
}
7588
}
7689

7790
/// <summary>

Microsoft.Toolkit.Uwp.UI.Controls/Carousel/Carousel.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,17 @@ private static void OnCarouselPropertyChanged(DependencyObject d, DependencyProp
257257

258258
private void FocusContainerFromIndex(int index)
259259
{
260-
var oldElem = FocusManager.GetFocusedElement() as ContentControl;
260+
ContentControl oldElem;
261+
262+
if (ControlHelpers.IsXamlRootAvailable && XamlRoot != null)
263+
{
264+
oldElem = FocusManager.GetFocusedElement(XamlRoot) as ContentControl;
265+
}
266+
else
267+
{
268+
oldElem = FocusManager.GetFocusedElement() as ContentControl;
269+
}
270+
261271
var newElem = ContainerFromIndex(index) as ContentControl;
262272

263273
if (oldElem == newElem)

Microsoft.Toolkit.Uwp.UI.Controls/Menu/Menu.Events.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,15 @@ private void CoreWindow_KeyDown(CoreWindow sender, KeyEventArgs args)
119119

120120
private void Menu_LostFocus(object sender, RoutedEventArgs e)
121121
{
122-
var menuItem = FocusManager.GetFocusedElement() as MenuItem;
122+
MenuItem menuItem;
123+
if (ControlHelpers.IsXamlRootAvailable && XamlRoot != null)
124+
{
125+
menuItem = FocusManager.GetFocusedElement(XamlRoot) as MenuItem;
126+
}
127+
else
128+
{
129+
menuItem = FocusManager.GetFocusedElement() as MenuItem;
130+
}
123131

124132
if (AllowTooltip)
125133
{
@@ -145,7 +153,14 @@ private void Dispatcher_AcceleratorKeyActivated(CoreDispatcher sender, Accelerat
145153
return;
146154
}
147155

148-
_lastFocusElement = FocusManager.GetFocusedElement() as Control;
156+
if (ControlHelpers.IsXamlRootAvailable && XamlRoot != null)
157+
{
158+
_lastFocusElement = FocusManager.GetFocusedElement(XamlRoot) as Control;
159+
}
160+
else
161+
{
162+
_lastFocusElement = FocusManager.GetFocusedElement() as Control;
163+
}
149164

150165
if (args.KeyStatus.ScanCode != AltScanCode)
151166
{

Microsoft.Toolkit.Uwp.UI.Controls/Menu/Menu.Logic.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@ public partial class Menu
3030

3131
private static bool NavigateUsingKeyboard(KeyEventArgs args, Menu menu, Orientation orientation)
3232
{
33-
var element = FocusManager.GetFocusedElement();
33+
object element;
34+
if (ControlHelpers.IsXamlRootAvailable && menu.XamlRoot != null)
35+
{
36+
element = FocusManager.GetFocusedElement(menu.XamlRoot);
37+
}
38+
else
39+
{
40+
element = FocusManager.GetFocusedElement();
41+
}
3442

3543
if (element is MenuFlyoutPresenter &&
3644
((args.VirtualKey == VirtualKey.Down) ||

0 commit comments

Comments
 (0)