Skip to content

Commit 076746a

Browse files
authored
Implement custom text context menus to fix crashes (#18854)
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 ✅
1 parent f769597 commit 076746a

28 files changed

+521
-36
lines changed

.github/actions/spelling/expect/expect.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ Blt
128128
blu
129129
BLUESCROLL
130130
bmi
131+
bodgy
131132
BODGY
132133
BOLDFONT
133134
Borland
@@ -371,8 +372,8 @@ Dcd
371372
DColor
372373
dcommon
373374
DComposition
374-
dde
375375
DDDCCC
376+
dde
376377
DDESHARE
377378
DDevice
378379
DEADCHAR

src/cascadia/TerminalApp/AboutDialog.xaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
99
xmlns:local="using:TerminalApp"
1010
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
11+
xmlns:mtu="using:Microsoft.Terminal.UI"
1112
xmlns:mux="using:Microsoft.UI.Xaml.Controls"
1213
x:Uid="AboutDialog"
1314
DefaultButton="Close"
@@ -17,6 +18,9 @@
1718

1819
<StackPanel Orientation="Vertical">
1920
<TextBlock IsTextSelectionEnabled="True">
21+
<TextBlock.ContextFlyout>
22+
<mtu:TextMenuFlyout />
23+
</TextBlock.ContextFlyout>
2024
<Run AutomationProperties.HeadingLevel="1"
2125
Text="{x:Bind ApplicationDisplayName}" /> <LineBreak />
2226
<Run x:Uid="AboutDialog_VersionLabel" />
@@ -39,7 +43,7 @@
3943
VerticalAlignment="Center"
4044
Orientation="Vertical"
4145
Visibility="{x:Bind UpdatesAvailable, Mode=OneWay}">
42-
<TextBlock IsTextSelectionEnabled="False">
46+
<TextBlock>
4347
<Run x:Uid="AboutDialog_UpdateAvailableLabel" />
4448
</TextBlock>
4549
<!-- <Button x:Uid="AboutDialog_InstallUpdateButton"
@@ -59,4 +63,3 @@
5963
Click="_ThirdPartyNoticesOnClick" />
6064
</StackPanel>
6165
</ContentDialog>
62-

src/cascadia/TerminalApp/CommandPalette.xaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,11 @@
284284
IsSpellCheckEnabled="False"
285285
PlaceholderText="{x:Bind SearchBoxPlaceholderText, Mode=OneWay}"
286286
Text=""
287-
TextChanged="_filterTextChanged" />
287+
TextChanged="_filterTextChanged">
288+
<TextBox.ContextFlyout>
289+
<mtu:TextMenuFlyout />
290+
</TextBox.ContextFlyout>
291+
</TextBox>
288292

289293
<TextBlock x:Name="_prefixCharacter"
290294
Grid.Row="0"

src/cascadia/TerminalApp/MarkdownPaneContent.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ namespace winrt::TerminalApp::implementation
7878
void MarkdownPaneContent::_loadText()
7979
{
8080
auto block = WUX::Controls::TextBlock();
81+
block.ContextFlyout(winrt::Microsoft::Terminal::UI::TextMenuFlyout{});
8182
block.IsTextSelectionEnabled(true);
8283
block.FontFamily(WUX::Media::FontFamily{ L"Cascadia Code" });
8384
block.Text(FileContents());

src/cascadia/TerminalApp/MarkdownPaneContent.xaml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
99
xmlns:local="using:TerminalApp"
1010
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
11+
xmlns:mtu="using:Microsoft.Terminal.UI"
1112
xmlns:mux="using:Microsoft.UI.Xaml.Controls"
1213
mc:Ignorable="d">
1314

@@ -51,7 +52,11 @@
5152
Grid.Column="0"
5253
Margin="4"
5354
PlaceholderText="Enter a path to a markdown file..."
54-
Text="Z:\dev\simple-test.md" />
55+
Text="Z:\dev\simple-test.md">
56+
<TextBox.ContextFlyout>
57+
<mtu:TextMenuFlyout />
58+
</TextBox.ContextFlyout>
59+
</TextBox>
5560
<StackPanel Grid.Column="1"
5661
Orientation="Horizontal">
5762
<Button Margin="4"
@@ -105,7 +110,11 @@
105110
FontFamily="Cascadia Code"
106111
IsSpellCheckEnabled="False"
107112
Text="{x:Bind FileContents, Mode=TwoWay}"
108-
Visibility="{x:Bind Editing}" />
113+
Visibility="{x:Bind Editing}">
114+
<TextBox.ContextFlyout>
115+
<mtu:TextMenuFlyout />
116+
</TextBox.ContextFlyout>
117+
</TextBox>
109118

110119
<ScrollViewer x:Name="_scrollViewer"
111120
Grid.Column="1"

src/cascadia/TerminalApp/ScratchpadContent.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace winrt::TerminalApp::implementation
2020
_root.Background(bg.try_as<Media::Brush>());
2121

2222
_box = winrt::Windows::UI::Xaml::Controls::TextBox{};
23+
_box.ContextFlyout(winrt::Microsoft::Terminal::UI::TextMenuFlyout{});
2324
_box.Margin({ 10, 10, 10, 10 });
2425
_box.AcceptsReturn(true);
2526
_box.TextWrapping(TextWrapping::Wrap);

src/cascadia/TerminalApp/SnippetsPaneContent.xaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@
182182
Grid.Column="1"
183183
Margin="12,0,12,6"
184184
FontFamily="Cascadia Mono, Consolas"
185-
IsTextSelectionEnabled="False"
186185
Style="{ThemeResource BodyTextBlockStyle}"
187186
Text="{x:Bind Input}"
188187
Visibility="{Binding ElementName=rootItem, Path=IsSelected}" />
@@ -263,7 +262,11 @@
263262
Margin="8,0,8,8"
264263
AllowFocusOnInteraction="True"
265264
TextChanged="_filterTextChanged"
266-
Visibility="{x:Bind HasSnippets, Mode=OneWay}" />
265+
Visibility="{x:Bind HasSnippets, Mode=OneWay}">
266+
<TextBox.ContextFlyout>
267+
<mtu:TextMenuFlyout />
268+
</TextBox.ContextFlyout>
269+
</TextBox>
267270

268271
<mux:TreeView x:Name="_treeView"
269272
Grid.Row="2"

src/cascadia/TerminalApp/SuggestionsControl.xaml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,11 @@
141141
PlaceholderText="{x:Bind SearchBoxPlaceholderText, Mode=OneWay}"
142142
Text=""
143143
TextChanged="_filterTextChanged"
144-
Visibility="Collapsed" />
144+
Visibility="Collapsed">
145+
<TextBox.ContextFlyout>
146+
<mtu:TextMenuFlyout />
147+
</TextBox.ContextFlyout>
148+
</TextBox>
145149

146150
<StackPanel Grid.Row="1"
147151
Margin="8,0,8,8"
@@ -224,15 +228,23 @@
224228
FontSize="14"
225229
FontWeight="Bold"
226230
IsTextSelectionEnabled="True"
227-
TextWrapping="WrapWholeWords" />
231+
TextWrapping="WrapWholeWords">
232+
<TextBlock.ContextFlyout>
233+
<mtu:TextMenuFlyout />
234+
</TextBlock.ContextFlyout>
235+
</TextBlock>
228236
<ScrollViewer MaxHeight="64"
229237
VerticalScrollBarVisibility="Visible"
230238
VerticalScrollMode="Enabled"
231239
Visibility="Visible">
232240
<TextBlock x:Name="_descriptionComment"
233241
Margin="0,0,20,0"
234242
IsTextSelectionEnabled="True"
235-
TextWrapping="WrapWholeWords" />
243+
TextWrapping="WrapWholeWords">
244+
<TextBlock.ContextFlyout>
245+
<mtu:TextMenuFlyout />
246+
</TextBlock.ContextFlyout>
247+
</TextBlock>
236248
</ScrollViewer>
237249
</StackPanel>
238250

src/cascadia/TerminalApp/TabHeaderControl.xaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
1010
xmlns:local="using:TerminalApp"
1111
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
12+
xmlns:mtu="using:Microsoft.Terminal.UI"
1213
xmlns:mux="using:Microsoft.UI.Xaml.Controls"
1314
MinHeight="16"
1415
mc:Ignorable="d">
@@ -67,6 +68,10 @@
6768
IsSpellCheckEnabled="False"
6869
LostFocus="RenameBoxLostFocusHandler"
6970
MaxLength="1024"
70-
Visibility="Collapsed" />
71+
Visibility="Collapsed">
72+
<TextBox.ContextFlyout>
73+
<mtu:TextMenuFlyout />
74+
</TextBox.ContextFlyout>
75+
</TextBox>
7176
</StackPanel>
7277
</UserControl>

src/cascadia/TerminalApp/TabRowControl.xaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@
7070
FontSize="12">
7171
<ToolTipService.ToolTip>
7272
<ToolTip Placement="Mouse">
73-
<TextBlock IsTextSelectionEnabled="False"
74-
TextWrapping="Wrap">
73+
<TextBlock TextWrapping="Wrap">
7574
<Run x:Uid="NewTabRun" /> <LineBreak />
7675
<Run x:Uid="NewPaneRun"
7776
FontStyle="Italic" /> <LineBreak />

0 commit comments

Comments
 (0)