|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#include "Utils/XamlIslandUtils.h" |
| 5 | + |
| 6 | +#include <UI.Xaml.Input.h> |
| 7 | + |
| 8 | +namespace Microsoft::ReactNative { |
| 9 | + |
| 10 | +struct CustomAppBarButton : xaml::Controls::AppBarButtonT<CustomAppBarButton> { |
| 11 | + void OnPointerExited(xaml::Input::PointerRoutedEventArgs const &e) { |
| 12 | + // This method crashes in the superclass, so we purposely don't call super. But the superclass |
| 13 | + // implementation likely cancels a timer that will show the submenu shortly after pointer enter. |
| 14 | + // Since we don't have access to that timer, instead we reset the Flyout property, which resets |
| 15 | + // the timer. This also fixes a crash where you can get a zombie submenu showing if the app |
| 16 | + // loses focus while this timer is scheduled to show the submenu. |
| 17 | + if (auto flyout = this->Flyout()) { |
| 18 | + this->Flyout(nullptr); |
| 19 | + this->Flyout(flyout); |
| 20 | + } |
| 21 | + |
| 22 | + // The superclass implementation resets the button to the normal state, so we do this ourselves. |
| 23 | + this->SetValue(xaml::Controls::Primitives::ButtonBase::IsPointerOverProperty(), winrt::box_value(false)); |
| 24 | + xaml::VisualStateManager::GoToState(*this, L"Normal", false); |
| 25 | + } |
| 26 | + |
| 27 | + void OnPointerPressed(xaml::Input::PointerRoutedEventArgs const &e) { |
| 28 | + // Clicking AppBarButton by default will dismiss the menu, but since we only use this class for |
| 29 | + // submenus we override it to be a no-op so it behaves like MenuFlyoutSubItem. |
| 30 | + } |
| 31 | +}; |
| 32 | + |
| 33 | +void FixProofingMenuCrashForXamlIsland(xaml::Controls::TextCommandBarFlyout const &flyout) { |
| 34 | + flyout.Opening([](winrt::IInspectable const &sender, auto &&) { |
| 35 | + const auto &flyout = sender.as<xaml::Controls::TextCommandBarFlyout>(); |
| 36 | + if (const auto &textBox = flyout.Target().try_as<xaml::Controls::TextBox>()) { |
| 37 | + const auto &commands = flyout.SecondaryCommands(); |
| 38 | + for (uint32_t i = 0; i < commands.Size(); ++i) { |
| 39 | + if (const auto &appBarButton = commands.GetAt(i).try_as<xaml::Controls::AppBarButton>()) { |
| 40 | + if (appBarButton.Flyout() == textBox.ProofingMenuFlyout()) { |
| 41 | + // Replace the AppBarButton for the proofing menu with one that doesn't crash |
| 42 | + const auto customAppBarButton = winrt::make<CustomAppBarButton>(); |
| 43 | + customAppBarButton.Label(appBarButton.Label()); |
| 44 | + customAppBarButton.Icon(appBarButton.Icon()); |
| 45 | + customAppBarButton.Flyout(appBarButton.Flyout()); |
| 46 | + commands.RemoveAt(i); |
| 47 | + commands.InsertAt(i, customAppBarButton); |
| 48 | + |
| 49 | + // There is only one proofing menu option |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +} // namespace Microsoft::ReactNative |
0 commit comments