Skip to content

Commit

Permalink
Fixing SplitButton's handling on keyboard input (#5064)
Browse files Browse the repository at this point in the history
  • Loading branch information
RBrid authored May 21, 2021
1 parent d2121a0 commit 4fc34de
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
19 changes: 17 additions & 2 deletions dev/SplitButton/InteractionTests/SplitButtonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,20 @@ public void CommandTest()
Log.Comment("Click primary button to execute command");
ClickPrimaryButton(splitButton);
Verify.AreEqual("1", executeCountTextBlock.DocumentText);


Log.Comment("Click primary button with SPACE key to execute command");
ClickPrimaryButtonWithKey(splitButton, "SPACE");
Verify.AreEqual("2", executeCountTextBlock.DocumentText);

Log.Comment("Click primary button with ENTER key to execute command");
ClickPrimaryButtonWithKey(splitButton, "ENTER");
Verify.AreEqual("3", executeCountTextBlock.DocumentText);

Log.Comment("Verify that setting CanExecute to false disables the primary button");
canExecuteCheckBox.Uncheck();
Wait.ForIdle();
ClickPrimaryButton(splitButton);
Verify.AreEqual("1", executeCountTextBlock.DocumentText);
Verify.AreEqual("3", executeCountTextBlock.DocumentText);
}
}

Expand Down Expand Up @@ -270,6 +278,13 @@ public void ClickPrimaryButton(SplitButton splitButton)
Wait.ForIdle();
}

public void ClickPrimaryButtonWithKey(SplitButton splitButton, string key)
{
Log.Comment("Click primary button area with %s key", key);
splitButton.SendKeys("{" + key + "}");
Wait.ForIdle();
}

public void ClickSecondaryButton(SplitButton splitButton)
{
Log.Comment("Click secondary button area");
Expand Down
14 changes: 14 additions & 0 deletions dev/SplitButton/SplitButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,19 @@ void SplitButton::CloseFlyout()
}
}

void SplitButton::ExecuteCommand()
{
if (const auto& command = Command())
{
const auto& commandParameter = CommandParameter();

if (command.CanExecute(commandParameter))
{
command.Execute(commandParameter);
}
}
}

void SplitButton::OnFlyoutOpened(const winrt::IInspectable& sender, const winrt::IInspectable& args)
{
m_isFlyoutOpen = true;
Expand Down Expand Up @@ -313,6 +326,7 @@ void SplitButton::OnSplitButtonKeyUp(const winrt::IInspectable& sender, const wi
if (IsEnabled())
{
OnClickPrimary(nullptr, nullptr);
ExecuteCommand();
args.Handled(true);
}
}
Expand Down
10 changes: 4 additions & 6 deletions dev/SplitButton/SplitButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ class SplitButton :

void OnPropertyChanged(const winrt::DependencyPropertyChangedEventArgs& args);

private:

void OnVisualPropertyChanged(const winrt::DependencyObject& sender, const winrt::DependencyProperty& args);

protected:
bool m_hasLoaded{ false };

private:
void ExecuteCommand();
void RegisterFlyoutEvents();
void UnregisterEvents();

void OnVisualPropertyChanged(const winrt::DependencyObject& sender, const winrt::DependencyProperty& args);

// Internal event handlers
void OnClickSecondary(const winrt::IInspectable& sender, const winrt::RoutedEventArgs& args);

Expand All @@ -56,8 +56,6 @@ class SplitButton :
void OnSplitButtonKeyDown(const winrt::IInspectable& sender, const winrt::KeyRoutedEventArgs& args);
void OnSplitButtonKeyUp(const winrt::IInspectable& sender, const winrt::KeyRoutedEventArgs& args);

void RegisterFlyoutEvents();

tracker_ref<winrt::Button> m_primaryButton{ this };
tracker_ref<winrt::Button> m_secondaryButton{ this };

Expand Down

0 comments on commit 4fc34de

Please sign in to comment.