Skip to content

Commit 67f5e08

Browse files
jonthyselllyahdav
andauthored
Switch to MUX:TextCommandBarFlyout in XAML Islands proofing menu (#8723)
This fixes the broken keyboard navigation in an islands app, however it can cause a crash when using WinUI 2.6 (but is fixed in WinUI 2.7, which was just checked in). Closes #8306 Co-authored-by: Liron Yahdav <[email protected]>
1 parent d76872d commit 67f5e08

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "Switch to MUX:CommandBarFlyout in XAML Islands proofing menu",
4+
"packageName": "react-native-windows",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

vnext/Microsoft.ReactNative/Utils/XamlIslandUtils.cpp

+12-3
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,23 @@ struct CustomAppBarButton : xaml::Controls::AppBarButtonT<CustomAppBarButton> {
3030
}
3131
};
3232

33-
void FixProofingMenuCrashForXamlIsland(xaml::Controls::TextCommandBarFlyout const &flyout) {
33+
void FixProofingMenuCrashForXamlIsland(xaml::Controls::Primitives::FlyoutBase const &flyout) {
3434
flyout.Opening([](winrt::IInspectable const &sender, auto &&) {
35-
const auto &flyout = sender.as<xaml::Controls::TextCommandBarFlyout>();
35+
const auto &flyout = sender.as<winrt::Microsoft::UI::Xaml::Controls::TextCommandBarFlyout>();
3636
if (const auto &textBox = flyout.Target().try_as<xaml::Controls::TextBox>()) {
3737
const auto &commands = flyout.SecondaryCommands();
3838
for (uint32_t i = 0; i < commands.Size(); ++i) {
3939
if (const auto &appBarButton = commands.GetAt(i).try_as<xaml::Controls::AppBarButton>()) {
40-
if (appBarButton.Flyout() == textBox.ProofingMenuFlyout()) {
40+
if (!appBarButton.Flyout()) {
41+
// This works around a loss of focus from the target element when clicking on
42+
// on the menu items.
43+
// https://github.com/microsoft/microsoft-ui-xaml/issues/5818
44+
appBarButton.Click([weakCommandBarFlyout = winrt::make_weak(flyout)](auto &&...) {
45+
if (auto flyout = weakCommandBarFlyout.get()) {
46+
xaml::Input::FocusManager::TryFocusAsync(flyout.Target(), xaml::FocusState::Programmatic);
47+
}
48+
});
49+
} else if (appBarButton.Flyout() == textBox.ProofingMenuFlyout()) {
4150
if (!appBarButton.try_as<CustomAppBarButton>()) {
4251
// Replace the AppBarButton for the proofing menu with one that doesn't crash
4352
const auto customAppBarButton = winrt::make<CustomAppBarButton>();

vnext/Microsoft.ReactNative/Utils/XamlIslandUtils.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
#include <UI.Xaml.Controls.Primitives.h>
99
#include <UI.Xaml.Controls.h>
1010

11+
#include <winrt/Microsoft.UI.Xaml.Controls.h>
12+
1113
namespace Microsoft::ReactNative {
1214

13-
void FixProofingMenuCrashForXamlIsland(xaml::Controls::TextCommandBarFlyout const &flyout);
15+
void FixProofingMenuCrashForXamlIsland(xaml::Controls::Primitives::FlyoutBase const &flyout);
1416

1517
template <typename T>
1618
inline void EnsureUniqueTextFlyoutForXamlIsland(T const &textView) {
@@ -19,7 +21,7 @@ inline void EnsureUniqueTextFlyoutForXamlIsland(T const &textView) {
1921
// to show the flyout on other windows cause the first window to get focus.
2022
// https://github.com/microsoft/microsoft-ui-xaml/issues/5341
2123
if (IsXamlIsland()) {
22-
xaml::Controls::TextCommandBarFlyout flyout;
24+
winrt::Microsoft::UI::Xaml::Controls::TextCommandBarFlyout flyout;
2325
flyout.Placement(xaml::Controls::Primitives::FlyoutPlacementMode::BottomEdgeAlignedLeft);
2426

2527
// This works around a XAML Islands bug where the Proofing sub-menu for

0 commit comments

Comments
 (0)