Skip to content

Implement custom text context menus to fix crashes #18854

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

lhecker
Copy link
Member

@lhecker lhecker commented Apr 29, 2025

This works around a bug in WinUI where it creates a single context menu/flyout for text elements per thread, not per XamlRoot, similar to many other areas. Since the XamlRoot cannot change after creation, this means that once you've opened the flyout, you're locked into that window (= XAML root) forever. You can't open the flyout in another window and once you've closed that window, you can't open it anywhere at all.

Closes #18599

Validation Steps Performed

  • Flies out right click in the
    • About dialog ✅
    • Search dialog ✅
    • Word delimiters setting ✅
    • Launch size setting ✅
  • Across two windows ✅

// Once the flyout was open once, we'll get Ctrl+A events and the TextBox will
// ignore them. As such, we have to dig out the focused element as a fallback,
// because otherwise Ctrl+A will be permanently broken. Put differently,
// this is bodgy because WinUI 2.8 is buggy. There's no other solution here.

Check failure

Code scanning / check-spelling

Unrecognized Spelling

[bodgy](#security-tab) is not a recognized word. \(unrecognized-spelling\)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess BODGY is allowed, but bodgy just ain't right

@lhecker lhecker force-pushed the dev/lhecker/18599-context-menu branch from 7c01c15 to 6770ace Compare April 29, 2025 16:45
Comment on lines +135 to +139
<Setter Property="ContextFlyout">
<Setter.Value>
<mtu:TextMenuFlyout />
</Setter.Value>
</Setter>
Copy link
Member Author

@lhecker lhecker Apr 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to do this app-wide? This here fixes almost all occurrences of selectable text, thankfully.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the only thing I can think of is doing it similar to above, but without an x:Key value, so it would apply it to all the textboxes. Like this:

<!--- BODGY: <reason> --->
<Style BasedOn="{StaticResource DefaultTextBoxStyle}"
       TargetType="TextBox">
   <Setter Property="ContextFlyout">
      <Setter.Value>
         <mtu:TextMenuFlyout />
      </Setter.Value>
   </Setter>
</Style>

At the bug bash today, I think Dustin also suggested trying to add it at the App.xaml layer or something like that (probably the XAML file you import WinUI 2).

Copy link
Member

@carlos-zamora carlos-zamora left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this fix #18246 too?

Comment on lines +135 to +139
<Setter Property="ContextFlyout">
<Setter.Value>
<mtu:TextMenuFlyout />
</Setter.Value>
</Setter>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the only thing I can think of is doing it similar to above, but without an x:Key value, so it would apply it to all the textboxes. Like this:

<!--- BODGY: <reason> --->
<Style BasedOn="{StaticResource DefaultTextBoxStyle}"
       TargetType="TextBox">
   <Setter Property="ContextFlyout">
      <Setter.Value>
         <mtu:TextMenuFlyout />
      </Setter.Value>
   </Setter>
</Style>

At the bug bash today, I think Dustin also suggested trying to add it at the App.xaml layer or something like that (probably the XAML file you import WinUI 2).

Comment on lines +74 to +76
<ItemGroup>
<PRIResource Include="Resources\en-US\Resources.resw" />
</ItemGroup>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double-checking: Just en-US?

@@ -0,0 +1,35 @@
#pragma once
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#pragma once
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#pragma once

Probably also worth adding the BODGY comment here and referencing the GH issue for historical reasons.

@@ -0,0 +1,201 @@
#include "pch.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include "pch.h"
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "pch.h"

// > Common group of selectable controls with common actions
// > The I in MIDL stands for...
// No common interface.
if (const auto box = target.try_as<NumberBox>())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about ComboBox with IsEditable=True?

// Once the flyout was open once, we'll get Ctrl+A events and the TextBox will
// ignore them. As such, we have to dig out the focused element as a fallback,
// because otherwise Ctrl+A will be permanently broken. Put differently,
// this is bodgy because WinUI 2.8 is buggy. There's no other solution here.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess BODGY is allowed, but bodgy just ain't right

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

WinUI text context menu not showing up (Was: Crash when right clicking text)
2 participants