Skip to content

Commit

Permalink
Fix F6 unable to return from teaching tip in xaml island. (#6807)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenLPeters authored Mar 24, 2022
1 parent 573e4a2 commit 2e53d0b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
40 changes: 33 additions & 7 deletions dev/TeachingTip/TeachingTip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ void TeachingTip::IsOpenChangedToOpen()
return;
}
}
};
}

m_acceleratorKeyActivatedRevoker = Dispatcher().AcceleratorKeyActivated(winrt::auto_revoke, { this, &TeachingTip::OnF6AcceleratorKeyClicked });
return;
Expand Down Expand Up @@ -953,6 +953,7 @@ void TeachingTip::IsOpenChangedToClose()

m_acceleratorKeyActivatedRevoker.revoke();
m_previewKeyDownForF6Revoker.revoke();
m_popupPreviewKeyDownForF6Revoker.revoke();
m_currentEffectiveTipPlacementMode = winrt::TeachingTipPlacementMode::Auto;
TeachingTipTestHooks::NotifyEffectivePlacementChanged(*this);
}
Expand Down Expand Up @@ -1112,7 +1113,17 @@ void TeachingTip::OnF6PreviewKeyDownClicked(const winrt::IInspectable&, const wi
}
}

bool TeachingTip::HandleF6Clicked()
void TeachingTip::OnF6PopupPreviewKeyDownClicked(const winrt::IInspectable&, const winrt::KeyRoutedEventArgs& args)
{
if (!args.Handled() &&
IsOpen() &&
args.Key() == winrt::VirtualKey::F6)
{
args.Handled(HandleF6Clicked(/*fromPopup*/true));
}
}

bool TeachingTip::HandleF6Clicked(bool fromPopup)
{
// Logging usage telemetry
if (m_hasF6BeenInvoked)
Expand All @@ -1127,9 +1138,16 @@ bool TeachingTip::HandleF6Clicked()

auto const hasFocusInSubtree = [this]()
{
auto current = winrt::FocusManager::GetFocusedElement().try_as<winrt::DependencyObject>();
if (auto const rootElement = m_rootElement.get())
{
auto current = [rootElement]() {
if (winrt::IUIElement10 uiElement10 = rootElement)
{
return winrt::FocusManager::GetFocusedElement(uiElement10.XamlRoot()).try_as<winrt::DependencyObject>();
}
return winrt::FocusManager::GetFocusedElement().try_as<winrt::DependencyObject>();
}();

while (current)
{
if (current.try_as<winrt::UIElement>() == rootElement)
Expand All @@ -1142,13 +1160,13 @@ bool TeachingTip::HandleF6Clicked()
return false;
}();

if (hasFocusInSubtree)
if (hasFocusInSubtree && fromPopup)
{
bool setFocus = SetFocus(m_previouslyFocusedElement.get(), winrt::FocusState::Programmatic);
m_previouslyFocusedElement = nullptr;
return setFocus;
}
else
else if (!hasFocusInSubtree && !fromPopup)
{
const winrt::Button f6Button = [this]() -> winrt::Button
{
Expand Down Expand Up @@ -1178,9 +1196,8 @@ bool TeachingTip::HandleF6Clicked()
const bool setFocus = f6Button.Focus(winrt::FocusState::Keyboard);
return setFocus;
}

return false;
}
return false;
}

void TeachingTip::OnAutomationNameChanged(const winrt::IInspectable&, const winrt::IInspectable&)
Expand Down Expand Up @@ -1214,6 +1231,15 @@ void TeachingTip::OnPopupOpened(const winrt::IInspectable&, const winrt::IInspec
m_currentXamlRootSize = xamlRoot.Size();
m_xamlRoot.set(xamlRoot);
m_xamlRootChangedRevoker = RegisterXamlRootChanged(xamlRoot, { this, &TeachingTip::XamlRootChanged });

if (auto&& popup = m_popup.get())
{
if (auto const popupContent = popup.Child())
{
// This handler is not required for Winui3 because the framework bug this works around has been fixed.
m_popupPreviewKeyDownForF6Revoker = popupContent.PreviewKeyDown(winrt::auto_revoke, { this, &TeachingTip::OnF6PopupPreviewKeyDownClicked });
}
}
}
}
else
Expand Down
7 changes: 5 additions & 2 deletions dev/TeachingTip/TeachingTip.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class TeachingTip :
PropertyChanged_revoker m_automationIdChangedRevoker{};
winrt::CoreDispatcher::AcceleratorKeyActivated_revoker m_acceleratorKeyActivatedRevoker{};
winrt::UIElement::PreviewKeyDown_revoker m_previewKeyDownForF6Revoker{};
// This handler is not required for Winui3 because the framework bug this works around has been fixed.
winrt::UIElement::PreviewKeyDown_revoker m_popupPreviewKeyDownForF6Revoker{};
winrt::Button::Click_revoker m_closeButtonClickedRevoker{};
winrt::Button::Click_revoker m_alternateCloseButtonClickedRevoker{};
winrt::Button::Click_revoker m_actionButtonClickedRevoker{};
Expand Down Expand Up @@ -115,9 +117,10 @@ class TeachingTip :
void OnAutomationIdChanged(const winrt::IInspectable&, const winrt::IInspectable&);

void OnContentSizeChanged(const winrt::IInspectable&, const winrt::SizeChangedEventArgs& args);
void OnF6AcceleratorKeyClicked(const winrt::CoreDispatcher&, const winrt::AcceleratorKeyEventArgs& args);
void OnF6PreviewKeyDownClicked(const winrt::IInspectable&, const winrt::KeyRoutedEventArgs& args);
bool HandleF6Clicked();
void OnF6PopupPreviewKeyDownClicked(const winrt::IInspectable&, const winrt::KeyRoutedEventArgs& args);
void OnF6AcceleratorKeyClicked(const winrt::CoreDispatcher&, const winrt::AcceleratorKeyEventArgs& args);
bool HandleF6Clicked(bool fromPopup = false);
void OnCloseButtonClicked(const winrt::IInspectable&, const winrt::RoutedEventArgs&);
void OnActionButtonClicked(const winrt::IInspectable&, const winrt::RoutedEventArgs&);
void OnPopupOpened(const winrt::IInspectable&, const winrt::IInspectable&);
Expand Down

0 comments on commit 2e53d0b

Please sign in to comment.