Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First time clicking on Cut, Copy, Paste, Undo, or Select All menu items from TextCommandBarFlyout of a TextBox/TextBlock/PasswordBox makes the control lose focus #5818

Closed
lyahdav opened this issue Sep 1, 2021 · 7 comments
Assignees
Labels
area-ComboBox area-Commanding AppBar, UICommand, MVVM, etc area-FocusManager Focus APIs such as Control.Focus(), FocusManager.Try[Move]Focus[Async], keyboarding with [shift+]tab area-TextBox TextBox, RichEditBox bug Something isn't working needs-assignee-attention Assignee needs to follow-up or investigate this issue team-Controls Issue for the Controls team

Comments

@lyahdav
Copy link

lyahdav commented Sep 1, 2021

Describe the bug

See title.

Steps to reproduce the bug

Steps to reproduce the behavior:

  1. Run XAML Controls Gallery app
  2. Go to TextBox example
  3. Type "test" in a TextBox
  4. Right click TextBox
  5. Click "Select All"

Expected behavior
Expected: "test" text in TextBox shows up as selected
Actual: "test" text is selected but it doesn't show as selected (Blue background) because the TextBox isn't focused. Subsequent clicks on "Select All" will correctly keep the TextBox focused.

Screenshots
Here's a video of the steps above showing the bug and how it works correctly the second time:

Screen.Recording.2021-09-01.at.11.37.58.AM.mov

Version Info
I'm on version 1.2.20.0 of XAML Controls Gallery.

Windows version Saw the problem?
Insider Build (xxxxx)
May 2021 Update (19043) Yes
October 2020 Update (19042)
May 2020 Update (19041)
November 2019 Update (18363)
May 2019 Update (18362)
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop Yes
Xbox
Surface Hub
IoT

Additional context
I also confirmed this issue happens when using TextCommandBarFlyout from WinUI version 2.7.0-prerelease.210816001 in a simple UWP app. Here's an example project demonstrating that: https://github.com/lyahdav/XamlPlayground/tree/text-box-with-win-ui-2-cbf

@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Sep 1, 2021
@lyahdav
Copy link
Author

lyahdav commented Sep 2, 2021

I also confirmed this bug happens on other controls that use TextCommandBarFlyout, e.g. TextBlock, RichTextBlock, PasswordBox. RichEditBox does not have the bug.

One workaround I found is to add:

    if (auto control = target.try_as<winrt::Control>())
    {
        control.Focus(winrt::FocusState::Programmatic);
    }

before

if (IsButtonInPrimaryCommands(TextControlButtons::SelectAll))

@lyahdav
Copy link
Author

lyahdav commented Sep 2, 2021

I realized this happens on all the menu items in TextCommandBarFlyout. A more general workaround if you don't want to touch WinUI 2 code is in your app's code to hook into the TextCommandBarFlyout's Opening event and run this:

			auto commands = cbf.SecondaryCommands();
			for (uint32_t i = 0; i < commands.Size(); ++i) {
				if (const auto appBarButton = commands.GetAt(i).try_as<AppBarButton>()) {
					if (!appBarButton.Flyout()) {
						appBarButton.Click([weakCommandBarFlyout = winrt::make_weak(cbf)](auto &&...){
							if (auto flyout = weakCommandBarFlyout.get()) {
								Xaml::Input::FocusManager::TryFocusAsync(flyout.Target(), FocusState::Programmatic);
							}
						});
					}
				}
			}

@lyahdav lyahdav changed the title First time clicking on "Select All" menu item from TextCommandBarFlyout of a TextBox makes the TextBox lose focus First time clicking on Cut, Copy, Paste, Undo, or Select All menu items from TextCommandBarFlyout of a TextBox/TextBlock makes the control lose focus Sep 2, 2021
@lyahdav lyahdav changed the title First time clicking on Cut, Copy, Paste, Undo, or Select All menu items from TextCommandBarFlyout of a TextBox/TextBlock makes the control lose focus First time clicking on Cut, Copy, Paste, Undo, or Select All menu items from TextCommandBarFlyout of a TextBox/TextBlock/PasswordBox makes the control lose focus Sep 2, 2021
@lyahdav
Copy link
Author

lyahdav commented Sep 3, 2021

I found a related bug which I think is the same root cause: The first time you right click on a TextBox, if you hit escape, the TextBox doesn't get focused. If you try again it will get focused.

@StephenLPeters StephenLPeters added area-Commanding AppBar, UICommand, MVVM, etc area-FocusManager Focus APIs such as Control.Focus(), FocusManager.Try[Move]Focus[Async], keyboarding with [shift+]tab area-TextBox TextBox, RichEditBox team-Controls Issue for the Controls team labels Sep 3, 2021
@StephenLPeters
Copy link
Contributor

@llongley Do you know whats going on here? Sounds vaguely familiar.

@StephenLPeters StephenLPeters added needs-assignee-attention Assignee needs to follow-up or investigate this issue and removed needs-triage Issue needs to be triaged by the area owners labels Sep 3, 2021
@github-actions
Copy link

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@lyahdav
Copy link
Author

lyahdav commented Jul 31, 2023

Likely still relevant

@DmitriyKomin
Copy link
Contributor

@lyahdav, thank you for reporting. Unfortunately at this time we are not able to investigate WinUI2-specific issues that are not business or security critical (more on the policy here).

I tried this out in WinUI3 Gallery and it does not repro there.

@DmitriyKomin DmitriyKomin closed this as not planned Won't fix, can't repro, duplicate, stale Mar 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-ComboBox area-Commanding AppBar, UICommand, MVVM, etc area-FocusManager Focus APIs such as Control.Focus(), FocusManager.Try[Move]Focus[Async], keyboarding with [shift+]tab area-TextBox TextBox, RichEditBox bug Something isn't working needs-assignee-attention Assignee needs to follow-up or investigate this issue team-Controls Issue for the Controls team
Projects
None yet
Development

No branches or pull requests

5 participants