Skip to content

Commit 1f5537f

Browse files
Merge branch 'master' into patch-2
2 parents 4556c8a + 3e69b15 commit 1f5537f

File tree

9 files changed

+119
-72
lines changed

9 files changed

+119
-72
lines changed

Microsoft.Toolkit.Mvvm/Messaging/WeakReferenceMessenger.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
using System.Runtime.CompilerServices;
1010
using Microsoft.Collections.Extensions;
1111
using Microsoft.Toolkit.Mvvm.Messaging.Internals;
12-
#if NETSTANDARD2_1
13-
using RecipientsTable = System.Runtime.CompilerServices.ConditionalWeakTable<object, Microsoft.Collections.Extensions.IDictionarySlim>;
14-
#else
12+
#if NETSTANDARD2_0
1513
using RecipientsTable = Microsoft.Toolkit.Mvvm.Messaging.WeakReferenceMessenger.ConditionalWeakTable<object, Microsoft.Collections.Extensions.IDictionarySlim>;
14+
#else
15+
using RecipientsTable = System.Runtime.CompilerServices.ConditionalWeakTable<object, Microsoft.Collections.Extensions.IDictionarySlim>;
1616
#endif
1717

1818
namespace Microsoft.Toolkit.Mvvm.Messaging
@@ -288,7 +288,7 @@ public void Reset()
288288
}
289289
}
290290

291-
#if !NETSTANDARD2_1
291+
#if NETSTANDARD2_0
292292
/// <summary>
293293
/// A wrapper for <see cref="System.Runtime.CompilerServices.ConditionalWeakTable{TKey,TValue}"/>
294294
/// that backports the enumerable support to .NET Standard 2.0 through an auxiliary list.
@@ -470,7 +470,7 @@ private ref struct ArrayPoolBufferWriter<T>
470470
[MethodImpl(MethodImplOptions.AggressiveInlining)]
471471
public static ArrayPoolBufferWriter<T> Create()
472472
{
473-
return new ArrayPoolBufferWriter<T> { array = ArrayPool<T>.Shared.Rent(DefaultInitialBufferSize) };
473+
return new() { array = ArrayPool<T>.Shared.Rent(DefaultInitialBufferSize) };
474474
}
475475

476476
/// <summary>

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/DispatcherQueueHelper/DispatcherQueueHelperCode.bind

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ int crossThreadReturnedValue = await Task.Run<int>( async () =>
66
{
77
// Task.Run() will guarantee the given piece of code be executed on a separate thread pool.
88
// This is used to simulate the scenario of updating the UI element from a different thread.
9-
int returnedFromUIThread = await dispatcherQueue.ExecuteOnUIThreadAsync<int>(() =>
9+
int returnedFromUIThread = await dispatcherQueue.EnqueueAsync<int>(() =>
1010
{
1111
NormalTextBlock.Text = "Updated from a random thread!";
1212
return 1;
@@ -18,4 +18,4 @@ int crossThreadReturnedValue = await Task.Run<int>( async () =>
1818
});
1919

2020

21-
NormalTextBlock.Text += $" And the value {crossThreadReturnedValue} was also returned successfully!";
21+
NormalTextBlock.Text += $" And the value {crossThreadReturnedValue} was also returned successfully!";

Microsoft.Toolkit.Uwp.UI.Controls.Media/InfiniteCanvas/Commands/InfiniteCanvasCreateTextBoxCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ internal class InfiniteCanvasCreateTextBoxCommand : IInfiniteCanvasCommand
1212
private readonly List<IDrawable> _drawableList;
1313
private readonly TextDrawable _drawable;
1414

15-
public InfiniteCanvasCreateTextBoxCommand(List<IDrawable> drawableList, double x, double y, double width, double height, int textFontSize, string text, Color color, bool isBold, bool isItalic)
15+
public InfiniteCanvasCreateTextBoxCommand(List<IDrawable> drawableList, double x, double y, double width, double height, float textFontSize, string text, Color color, bool isBold, bool isItalic)
1616
{
1717
_drawable = new TextDrawable(
1818
x,

Microsoft.Toolkit.Uwp.UI.Controls.Media/InfiniteCanvas/Controls/InfiniteCanvasVirtualDrawingSurface.Commands.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ internal void ExecuteUpdateTextBoxFontSize(float newValue)
7979
ExecuteCommand(command);
8080
}
8181

82-
internal void ExecuteCreateTextBox(double x, double y, double width, double height, int textFontSize, string text, Color color, bool isBold, bool isItalic)
82+
internal void ExecuteCreateTextBox(double x, double y, double width, double height, float textFontSize, string text, Color color, bool isBold, bool isItalic)
8383
{
8484
var command = new InfiniteCanvasCreateTextBoxCommand(_drawableList, x, y, width, height, textFontSize, text, color, isBold, isItalic);
8585
ExecuteCommand(command);

Microsoft.Toolkit.Uwp.UI.Controls.Media/InfiniteCanvas/InfiniteCanvas.TextBox.cs

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,20 @@ public partial class InfiniteCanvas
2828
"Right",
2929
"Up",
3030
"Left",
31-
"Down"
31+
"Down",
32+
"Enter"
3233
};
3334

3435
private Point _lastInputPoint;
3536

3637
private TextDrawable SelectedTextDrawable => _drawingSurfaceRenderer.GetSelectedTextDrawable();
3738

38-
private int _lastValidTextFontSizeValue = DefaultFontValue;
39+
private float _textFontSize = DefaultFontValue;
3940

40-
private int TextFontSize
41+
private void SetFontSize(float newSize)
4142
{
42-
get
43-
{
44-
if (!string.IsNullOrWhiteSpace(_canvasTextBoxFontSizeTextBox.Text) &&
45-
Regex.IsMatch(_canvasTextBoxFontSizeTextBox.Text, "^[0-9]*$"))
46-
{
47-
var fontSize = int.Parse(_canvasTextBoxFontSizeTextBox.Text);
48-
_lastValidTextFontSizeValue = fontSize;
49-
}
50-
51-
return _lastValidTextFontSizeValue;
52-
}
43+
_textFontSize = newSize;
44+
_canvasTextBox.UpdateFontSize(newSize);
5345
}
5446

5547
private void InkScrollViewer_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
@@ -93,13 +85,34 @@ private void CanvasTextBoxItalicButton_Clicked(object sender, RoutedEventArgs e)
9385
}
9486
}
9587

96-
private void CanvasTextBoxFontSizeTextBox_TextChanged(object sender, TextChangedEventArgs e)
88+
private void CanvasComboBoxFontSizeTextBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
9789
{
98-
_canvasTextBox.UpdateFontSize(TextFontSize);
99-
if (SelectedTextDrawable != null)
90+
if (sender is ComboBox s
91+
&& s.SelectedItem is ComboBoxItem selectedItem
92+
&& selectedItem.Content is string selectedText
93+
&& float.TryParse(selectedText, out var sizeNumb))
10094
{
101-
_drawingSurfaceRenderer.ExecuteUpdateTextBoxFontSize(TextFontSize);
102-
ReDrawCanvas();
95+
SetFontSize(sizeNumb);
96+
97+
if (SelectedTextDrawable != null)
98+
{
99+
_drawingSurfaceRenderer.ExecuteUpdateTextBoxFontSize(sizeNumb);
100+
ReDrawCanvas();
101+
}
102+
}
103+
}
104+
105+
private void CanvasComboBoxFontSizeTextBox_TextSubmitted(ComboBox sender, ComboBoxTextSubmittedEventArgs args)
106+
{
107+
if (float.TryParse(args.Text, out var size))
108+
{
109+
SetFontSize(size);
110+
111+
if (SelectedTextDrawable != null)
112+
{
113+
_drawingSurfaceRenderer.ExecuteUpdateTextBoxFontSize(size);
114+
ReDrawCanvas();
115+
}
103116
}
104117
}
105118

@@ -147,20 +160,22 @@ private void CanvasTextBox_TextChanged(object sender, string text)
147160
ReDrawCanvas();
148161
return;
149162
}
163+
else
164+
{
165+
_drawingSurfaceRenderer.ExecuteCreateTextBox(
166+
_lastInputPoint.X,
167+
_lastInputPoint.Y,
168+
_canvasTextBox.GetEditZoneWidth(),
169+
_canvasTextBox.GetEditZoneHeight(),
170+
_textFontSize,
171+
text,
172+
_canvasTextBoxColorPicker.Color,
173+
_canvasTextBoxBoldButton.IsChecked ?? false,
174+
_canvasTextBoxItalicButton.IsChecked ?? false);
150175

151-
_drawingSurfaceRenderer.ExecuteCreateTextBox(
152-
_lastInputPoint.X,
153-
_lastInputPoint.Y,
154-
_canvasTextBox.GetEditZoneWidth(),
155-
_canvasTextBox.GetEditZoneHeight(),
156-
TextFontSize,
157-
text,
158-
_canvasTextBoxColorPicker.Color,
159-
_canvasTextBoxBoldButton.IsChecked ?? false,
160-
_canvasTextBoxItalicButton.IsChecked ?? false);
161-
162-
ReDrawCanvas();
163-
_drawingSurfaceRenderer.UpdateSelectedTextDrawable();
176+
ReDrawCanvas();
177+
_drawingSurfaceRenderer.UpdateSelectedTextDrawable();
178+
}
164179
}
165180

166181
private void InkScrollViewer_PointerPressed(object sender, PointerRoutedEventArgs e)
@@ -179,20 +194,17 @@ private void InkScrollViewer_PointerPressed(object sender, PointerRoutedEventArg
179194

180195
Canvas.SetLeft(_canvasTextBox, SelectedTextDrawable.Bounds.X);
181196
Canvas.SetTop(_canvasTextBox, SelectedTextDrawable.Bounds.Y);
182-
_canvasTextBox.UpdateFontSize(SelectedTextDrawable.FontSize);
183197
_canvasTextBox.UpdateFontStyle(SelectedTextDrawable.IsItalic);
184198
_canvasTextBox.UpdateFontWeight(SelectedTextDrawable.IsBold);
185199

186200
// Updating toolbar
187201
_canvasTextBoxColorPicker.Color = SelectedTextDrawable.TextColor;
188-
_canvasTextBoxFontSizeTextBox.Text = SelectedTextDrawable.FontSize.ToString();
189202
_canvasTextBoxBoldButton.IsChecked = SelectedTextDrawable.IsBold;
190203
_canvasTextBoxItalicButton.IsChecked = SelectedTextDrawable.IsItalic;
191204

192205
return;
193206
}
194207

195-
_canvasTextBox.UpdateFontSize(TextFontSize);
196208
_canvasTextBox.UpdateFontStyle(_canvasTextBoxItalicButton.IsChecked ?? false);
197209
_canvasTextBox.UpdateFontWeight(_canvasTextBoxBoldButton.IsChecked ?? false);
198210

@@ -210,7 +222,7 @@ private void ClearTextBoxValue()
210222
_canvasTextBox.Clear();
211223
}
212224

213-
private void CanvasTextBoxFontSizeTextBox_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
225+
private void CanvasComboBoxFontSizeTextBox_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
214226
{
215227
if (_allowedCommands.Contains(e.Key.ToString()))
216228
{

Microsoft.Toolkit.Uwp.UI.Controls.Media/InfiniteCanvas/InfiniteCanvas.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
2121
/// </summary>
2222
[TemplatePart(Name = CanvasTextBoxToolsName, Type = typeof(StackPanel))]
2323
[TemplatePart(Name = CanvasTextBoxColorPickerName, Type = typeof(Windows.UI.Xaml.Controls.ColorPicker))]
24-
[TemplatePart(Name = CanvasTextBoxFontSizeTextBoxName, Type = typeof(TextBox))]
24+
[TemplatePart(Name = CanvasComboBoxFontSizeTextBoxName, Type = typeof(TextBox))]
2525
[TemplatePart(Name = CanvasTextBoxItalicButtonName, Type = typeof(ToggleButton))]
2626
[TemplatePart(Name = CanvasTextBoxBoldButtonName, Type = typeof(ToggleButton))]
2727
[TemplatePart(Name = DrawingSurfaceRendererName, Type = typeof(InfiniteCanvasVirtualDrawingSurface))]
@@ -45,7 +45,7 @@ public partial class InfiniteCanvas : Control
4545

4646
private const string CanvasTextBoxToolsName = "CanvasTextBoxTools";
4747
private const string CanvasTextBoxColorPickerName = "CanvasTextBoxColorPicker";
48-
private const string CanvasTextBoxFontSizeTextBoxName = "CanvasTextBoxFontSizeTextBox";
48+
private const string CanvasComboBoxFontSizeTextBoxName = "CanvasComboBoxFontSizeTextBox";
4949
private const string CanvasTextBoxItalicButtonName = "CanvasTextBoxItalicButton";
5050
private const string CanvasTextBoxBoldButtonName = "CanvasTextBoxBoldButton";
5151
private const string DrawingSurfaceRendererName = "DrawingSurfaceRenderer";
@@ -71,7 +71,7 @@ public partial class InfiniteCanvas : Control
7171
private StackPanel _canvasTextBoxTools;
7272
private Windows.UI.Xaml.Controls.ColorPicker _canvasTextBoxColorPicker;
7373

74-
private TextBox _canvasTextBoxFontSizeTextBox;
74+
private ComboBox _canvasComboBoxFontSizeTextBox;
7575
private ToggleButton _canvasTextBoxItalicButton;
7676
private ToggleButton _canvasTextBoxBoldButton;
7777
private Button _undoButton;
@@ -244,7 +244,7 @@ protected override void OnApplyTemplate()
244244
{
245245
_canvasTextBoxTools = (StackPanel)GetTemplateChild(CanvasTextBoxToolsName);
246246
this._canvasTextBoxColorPicker = (Windows.UI.Xaml.Controls.ColorPicker)GetTemplateChild(CanvasTextBoxColorPickerName);
247-
_canvasTextBoxFontSizeTextBox = (TextBox)GetTemplateChild(CanvasTextBoxFontSizeTextBoxName);
247+
_canvasComboBoxFontSizeTextBox = (ComboBox)GetTemplateChild(CanvasComboBoxFontSizeTextBoxName);
248248
_canvasTextBoxItalicButton = (ToggleButton)GetTemplateChild(CanvasTextBoxItalicButtonName);
249249
_canvasTextBoxBoldButton = (ToggleButton)GetTemplateChild(CanvasTextBoxBoldButtonName);
250250
_drawingSurfaceRenderer = (InfiniteCanvasVirtualDrawingSurface)GetTemplateChild(DrawingSurfaceRendererName);
@@ -296,7 +296,7 @@ protected override void OnApplyTemplate()
296296

297297
private void UnRegisterEvents()
298298
{
299-
_canvasTextBoxFontSizeTextBox.TextChanged -= CanvasTextBoxFontSizeTextBox_TextChanged;
299+
_canvasComboBoxFontSizeTextBox.SelectionChanged -= CanvasComboBoxFontSizeTextBox_SelectionChanged;
300300
_canvasTextBoxItalicButton.Click -= CanvasTextBoxItalicButton_Clicked;
301301
_canvasTextBoxBoldButton.Click -= CanvasTextBoxBoldButton_Clicked;
302302
_canvasTextBoxColorPicker.ColorChanged -= CanvasTextBoxColorPicker_ColorChanged;
@@ -314,13 +314,14 @@ private void UnRegisterEvents()
314314
Unloaded -= InfiniteCanvas_Unloaded;
315315
Application.Current.LeavingBackground -= Current_LeavingBackground;
316316
_drawingSurfaceRenderer.CommandExecuted -= DrawingSurfaceRenderer_CommandExecuted;
317-
_canvasTextBoxFontSizeTextBox.PreviewKeyDown -= CanvasTextBoxFontSizeTextBox_PreviewKeyDown;
317+
_canvasComboBoxFontSizeTextBox.PreviewKeyDown -= CanvasComboBoxFontSizeTextBox_PreviewKeyDown;
318+
_canvasComboBoxFontSizeTextBox.TextSubmitted -= CanvasComboBoxFontSizeTextBox_TextSubmitted;
318319
Loaded -= InfiniteCanvas_Loaded;
319320
}
320321

321322
private void RegisterEvents()
322323
{
323-
_canvasTextBoxFontSizeTextBox.TextChanged += CanvasTextBoxFontSizeTextBox_TextChanged;
324+
_canvasComboBoxFontSizeTextBox.SelectionChanged += CanvasComboBoxFontSizeTextBox_SelectionChanged;
324325
_canvasTextBoxItalicButton.Click += CanvasTextBoxItalicButton_Clicked;
325326
_canvasTextBoxBoldButton.Click += CanvasTextBoxBoldButton_Clicked;
326327
_canvasTextBoxColorPicker.ColorChanged += CanvasTextBoxColorPicker_ColorChanged;
@@ -338,7 +339,8 @@ private void RegisterEvents()
338339
Unloaded += InfiniteCanvas_Unloaded;
339340
Application.Current.LeavingBackground += Current_LeavingBackground;
340341
_drawingSurfaceRenderer.CommandExecuted += DrawingSurfaceRenderer_CommandExecuted;
341-
_canvasTextBoxFontSizeTextBox.PreviewKeyDown += CanvasTextBoxFontSizeTextBox_PreviewKeyDown;
342+
_canvasComboBoxFontSizeTextBox.PreviewKeyDown += CanvasComboBoxFontSizeTextBox_PreviewKeyDown;
343+
_canvasComboBoxFontSizeTextBox.TextSubmitted += CanvasComboBoxFontSizeTextBox_TextSubmitted;
342344
Loaded += InfiniteCanvas_Loaded;
343345
}
344346

@@ -366,7 +368,7 @@ private void ConfigureControls()
366368

367369
SetCanvasWidthHeight();
368370

369-
_canvasTextBox.UpdateFontSize(TextFontSize);
371+
SetFontSize(_textFontSize);
370372
}
371373

372374
private void SetZoomFactor()

Microsoft.Toolkit.Uwp.UI.Controls.Media/InfiniteCanvas/InfiniteCanvas.xaml

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,32 @@
9797
</ToggleButton.Content>
9898
</ToggleButton>
9999

100-
<TextBox x:Name="CanvasTextBoxFontSizeTextBox"
101-
Width="64"
102-
Height="32"
103-
InputScope="Number"
104-
MaxLength="3"
105-
Text="22"
106-
ToolTipService.ToolTip="Font Size" />
100+
<ComboBox x:Name="CanvasComboBoxFontSizeTextBox"
101+
MinWidth="64"
102+
Height="32"
103+
SelectedIndex="9"
104+
Margin="0,0,12,0"
105+
VerticalAlignment="Center"
106+
ToolTipService.ToolTip="Font Size"
107+
IsEditable="True"
108+
>
109+
<ComboBoxItem Content="8" />
110+
<ComboBoxItem Content="9" />
111+
<ComboBoxItem Content="10" />
112+
<ComboBoxItem Content="11" />
113+
<ComboBoxItem Content="12" />
114+
<ComboBoxItem Content="14" />
115+
<ComboBoxItem Content="16" />
116+
<ComboBoxItem Content="18" />
117+
<ComboBoxItem Content="20" />
118+
<ComboBoxItem Content="22" />
119+
<ComboBoxItem Content="24" />
120+
<ComboBoxItem Content="26" />
121+
<ComboBoxItem Content="28" />
122+
<ComboBoxItem Content="36" />
123+
<ComboBoxItem Content="48" />
124+
<ComboBoxItem Content="72" />
125+
</ComboBox>
107126

108127
</StackPanel>
109128
</StackPanel>

Microsoft.Toolkit/Extensions/TaskExtensions.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,28 @@ public static class TaskExtensions
3737
#endif
3838
)
3939
{
40-
// Try to get the Task<T>.Result property. This method would've
41-
// been called anyway after the type checks, but using that to
42-
// validate the input type saves some additional reflection calls.
43-
// Furthermore, doing this also makes the method flexible enough to
44-
// cases whether the input Task<T> is actually an instance of some
45-
// runtime-specific type that inherits from Task<T>.
46-
PropertyInfo? propertyInfo =
40+
// We need an explicit check to ensure the input task is not the cached
41+
// Task.CompletedTask instance, because that can internally be stored as
42+
// a Task<T> for some given T (eg. on .NET 5 it's VoidTaskResult), which
43+
// would cause the following code to return that result instead of null.
44+
if (task != Task.CompletedTask)
45+
{
46+
// Try to get the Task<T>.Result property. This method would've
47+
// been called anyway after the type checks, but using that to
48+
// validate the input type saves some additional reflection calls.
49+
// Furthermore, doing this also makes the method flexible enough to
50+
// cases whether the input Task<T> is actually an instance of some
51+
// runtime-specific type that inherits from Task<T>.
52+
PropertyInfo? propertyInfo =
4753
#if NETSTANDARD1_4
48-
task.GetType().GetRuntimeProperty(nameof(Task<object>.Result));
54+
task.GetType().GetRuntimeProperty(nameof(Task<object>.Result));
4955
#else
50-
task.GetType().GetProperty(nameof(Task<object>.Result));
56+
task.GetType().GetProperty(nameof(Task<object>.Result));
5157
#endif
5258

53-
// Return the result, if possible
54-
return propertyInfo?.GetValue(task);
59+
// Return the result, if possible
60+
return propertyInfo?.GetValue(task);
61+
}
5562
}
5663

5764
return null;

UnitTests/UnitTests.Shared/Extensions/Test_TaskExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ public void Test_TaskExtensions_ResultOrDefault()
3737
Assert.AreEqual(42, ((Task)tcs.Task).GetResultOrDefault());
3838
}
3939

40+
[TestCategory("TaskExtensions")]
41+
[TestMethod]
42+
public void Test_TaskExtensions_ResultOrDefault_FromTaskCompleted()
43+
{
44+
Assert.AreEqual(null, Task.CompletedTask.GetResultOrDefault());
45+
}
46+
4047
[TestCategory("TaskExtensions")]
4148
[TestMethod]
4249
public async Task Test_TaskExtensions_ResultOrDefault_FromAsyncTaskMethodBuilder()

0 commit comments

Comments
 (0)