Merge branch 'master' into patch-2

This commit is contained in:
Michael Hawker MSFT (XAML Llama) 2021-04-20 09:49:16 -07:00 коммит произвёл GitHub
Родитель 4556c8a5f4 3e69b156c3
Коммит 1f5537faab
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
9 изменённых файлов: 119 добавлений и 72 удалений

Просмотреть файл

@ -9,10 +9,10 @@ using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using Microsoft.Collections.Extensions;
using Microsoft.Toolkit.Mvvm.Messaging.Internals;
#if NETSTANDARD2_1
using RecipientsTable = System.Runtime.CompilerServices.ConditionalWeakTable<object, Microsoft.Collections.Extensions.IDictionarySlim>;
#else
#if NETSTANDARD2_0
using RecipientsTable = Microsoft.Toolkit.Mvvm.Messaging.WeakReferenceMessenger.ConditionalWeakTable<object, Microsoft.Collections.Extensions.IDictionarySlim>;
#else
using RecipientsTable = System.Runtime.CompilerServices.ConditionalWeakTable<object, Microsoft.Collections.Extensions.IDictionarySlim>;
#endif
namespace Microsoft.Toolkit.Mvvm.Messaging
@ -288,7 +288,7 @@ namespace Microsoft.Toolkit.Mvvm.Messaging
}
}
#if !NETSTANDARD2_1
#if NETSTANDARD2_0
/// <summary>
/// A wrapper for <see cref="System.Runtime.CompilerServices.ConditionalWeakTable{TKey,TValue}"/>
/// that backports the enumerable support to .NET Standard 2.0 through an auxiliary list.
@ -470,7 +470,7 @@ namespace Microsoft.Toolkit.Mvvm.Messaging
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ArrayPoolBufferWriter<T> Create()
{
return new ArrayPoolBufferWriter<T> { array = ArrayPool<T>.Shared.Rent(DefaultInitialBufferSize) };
return new() { array = ArrayPool<T>.Shared.Rent(DefaultInitialBufferSize) };
}
/// <summary>

Просмотреть файл

@ -6,7 +6,7 @@ int crossThreadReturnedValue = await Task.Run<int>( async () =>
{
// Task.Run() will guarantee the given piece of code be executed on a separate thread pool.
// This is used to simulate the scenario of updating the UI element from a different thread.
int returnedFromUIThread = await dispatcherQueue.ExecuteOnUIThreadAsync<int>(() =>
int returnedFromUIThread = await dispatcherQueue.EnqueueAsync<int>(() =>
{
NormalTextBlock.Text = "Updated from a random thread!";
return 1;
@ -18,4 +18,4 @@ int crossThreadReturnedValue = await Task.Run<int>( async () =>
});
NormalTextBlock.Text += $" And the value {crossThreadReturnedValue} was also returned successfully!";
NormalTextBlock.Text += $" And the value {crossThreadReturnedValue} was also returned successfully!";

Просмотреть файл

@ -12,7 +12,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
private readonly List<IDrawable> _drawableList;
private readonly TextDrawable _drawable;
public InfiniteCanvasCreateTextBoxCommand(List<IDrawable> drawableList, double x, double y, double width, double height, int textFontSize, string text, Color color, bool isBold, bool isItalic)
public InfiniteCanvasCreateTextBoxCommand(List<IDrawable> drawableList, double x, double y, double width, double height, float textFontSize, string text, Color color, bool isBold, bool isItalic)
{
_drawable = new TextDrawable(
x,

Просмотреть файл

@ -79,7 +79,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
ExecuteCommand(command);
}
internal void ExecuteCreateTextBox(double x, double y, double width, double height, int textFontSize, string text, Color color, bool isBold, bool isItalic)
internal void ExecuteCreateTextBox(double x, double y, double width, double height, float textFontSize, string text, Color color, bool isBold, bool isItalic)
{
var command = new InfiniteCanvasCreateTextBoxCommand(_drawableList, x, y, width, height, textFontSize, text, color, isBold, isItalic);
ExecuteCommand(command);

Просмотреть файл

@ -28,28 +28,20 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
"Right",
"Up",
"Left",
"Down"
"Down",
"Enter"
};
private Point _lastInputPoint;
private TextDrawable SelectedTextDrawable => _drawingSurfaceRenderer.GetSelectedTextDrawable();
private int _lastValidTextFontSizeValue = DefaultFontValue;
private float _textFontSize = DefaultFontValue;
private int TextFontSize
private void SetFontSize(float newSize)
{
get
{
if (!string.IsNullOrWhiteSpace(_canvasTextBoxFontSizeTextBox.Text) &&
Regex.IsMatch(_canvasTextBoxFontSizeTextBox.Text, "^[0-9]*$"))
{
var fontSize = int.Parse(_canvasTextBoxFontSizeTextBox.Text);
_lastValidTextFontSizeValue = fontSize;
}
return _lastValidTextFontSizeValue;
}
_textFontSize = newSize;
_canvasTextBox.UpdateFontSize(newSize);
}
private void InkScrollViewer_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
@ -93,13 +85,34 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
}
}
private void CanvasTextBoxFontSizeTextBox_TextChanged(object sender, TextChangedEventArgs e)
private void CanvasComboBoxFontSizeTextBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
_canvasTextBox.UpdateFontSize(TextFontSize);
if (SelectedTextDrawable != null)
if (sender is ComboBox s
&& s.SelectedItem is ComboBoxItem selectedItem
&& selectedItem.Content is string selectedText
&& float.TryParse(selectedText, out var sizeNumb))
{
_drawingSurfaceRenderer.ExecuteUpdateTextBoxFontSize(TextFontSize);
ReDrawCanvas();
SetFontSize(sizeNumb);
if (SelectedTextDrawable != null)
{
_drawingSurfaceRenderer.ExecuteUpdateTextBoxFontSize(sizeNumb);
ReDrawCanvas();
}
}
}
private void CanvasComboBoxFontSizeTextBox_TextSubmitted(ComboBox sender, ComboBoxTextSubmittedEventArgs args)
{
if (float.TryParse(args.Text, out var size))
{
SetFontSize(size);
if (SelectedTextDrawable != null)
{
_drawingSurfaceRenderer.ExecuteUpdateTextBoxFontSize(size);
ReDrawCanvas();
}
}
}
@ -147,20 +160,22 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
ReDrawCanvas();
return;
}
else
{
_drawingSurfaceRenderer.ExecuteCreateTextBox(
_lastInputPoint.X,
_lastInputPoint.Y,
_canvasTextBox.GetEditZoneWidth(),
_canvasTextBox.GetEditZoneHeight(),
_textFontSize,
text,
_canvasTextBoxColorPicker.Color,
_canvasTextBoxBoldButton.IsChecked ?? false,
_canvasTextBoxItalicButton.IsChecked ?? false);
_drawingSurfaceRenderer.ExecuteCreateTextBox(
_lastInputPoint.X,
_lastInputPoint.Y,
_canvasTextBox.GetEditZoneWidth(),
_canvasTextBox.GetEditZoneHeight(),
TextFontSize,
text,
_canvasTextBoxColorPicker.Color,
_canvasTextBoxBoldButton.IsChecked ?? false,
_canvasTextBoxItalicButton.IsChecked ?? false);
ReDrawCanvas();
_drawingSurfaceRenderer.UpdateSelectedTextDrawable();
ReDrawCanvas();
_drawingSurfaceRenderer.UpdateSelectedTextDrawable();
}
}
private void InkScrollViewer_PointerPressed(object sender, PointerRoutedEventArgs e)
@ -179,20 +194,17 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
Canvas.SetLeft(_canvasTextBox, SelectedTextDrawable.Bounds.X);
Canvas.SetTop(_canvasTextBox, SelectedTextDrawable.Bounds.Y);
_canvasTextBox.UpdateFontSize(SelectedTextDrawable.FontSize);
_canvasTextBox.UpdateFontStyle(SelectedTextDrawable.IsItalic);
_canvasTextBox.UpdateFontWeight(SelectedTextDrawable.IsBold);
// Updating toolbar
_canvasTextBoxColorPicker.Color = SelectedTextDrawable.TextColor;
_canvasTextBoxFontSizeTextBox.Text = SelectedTextDrawable.FontSize.ToString();
_canvasTextBoxBoldButton.IsChecked = SelectedTextDrawable.IsBold;
_canvasTextBoxItalicButton.IsChecked = SelectedTextDrawable.IsItalic;
return;
}
_canvasTextBox.UpdateFontSize(TextFontSize);
_canvasTextBox.UpdateFontStyle(_canvasTextBoxItalicButton.IsChecked ?? false);
_canvasTextBox.UpdateFontWeight(_canvasTextBoxBoldButton.IsChecked ?? false);
@ -210,7 +222,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
_canvasTextBox.Clear();
}
private void CanvasTextBoxFontSizeTextBox_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
private void CanvasComboBoxFontSizeTextBox_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (_allowedCommands.Contains(e.Key.ToString()))
{

Просмотреть файл

@ -21,7 +21,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
/// </summary>
[TemplatePart(Name = CanvasTextBoxToolsName, Type = typeof(StackPanel))]
[TemplatePart(Name = CanvasTextBoxColorPickerName, Type = typeof(Windows.UI.Xaml.Controls.ColorPicker))]
[TemplatePart(Name = CanvasTextBoxFontSizeTextBoxName, Type = typeof(TextBox))]
[TemplatePart(Name = CanvasComboBoxFontSizeTextBoxName, Type = typeof(TextBox))]
[TemplatePart(Name = CanvasTextBoxItalicButtonName, Type = typeof(ToggleButton))]
[TemplatePart(Name = CanvasTextBoxBoldButtonName, Type = typeof(ToggleButton))]
[TemplatePart(Name = DrawingSurfaceRendererName, Type = typeof(InfiniteCanvasVirtualDrawingSurface))]
@ -45,7 +45,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
private const string CanvasTextBoxToolsName = "CanvasTextBoxTools";
private const string CanvasTextBoxColorPickerName = "CanvasTextBoxColorPicker";
private const string CanvasTextBoxFontSizeTextBoxName = "CanvasTextBoxFontSizeTextBox";
private const string CanvasComboBoxFontSizeTextBoxName = "CanvasComboBoxFontSizeTextBox";
private const string CanvasTextBoxItalicButtonName = "CanvasTextBoxItalicButton";
private const string CanvasTextBoxBoldButtonName = "CanvasTextBoxBoldButton";
private const string DrawingSurfaceRendererName = "DrawingSurfaceRenderer";
@ -71,7 +71,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
private StackPanel _canvasTextBoxTools;
private Windows.UI.Xaml.Controls.ColorPicker _canvasTextBoxColorPicker;
private TextBox _canvasTextBoxFontSizeTextBox;
private ComboBox _canvasComboBoxFontSizeTextBox;
private ToggleButton _canvasTextBoxItalicButton;
private ToggleButton _canvasTextBoxBoldButton;
private Button _undoButton;
@ -244,7 +244,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
{
_canvasTextBoxTools = (StackPanel)GetTemplateChild(CanvasTextBoxToolsName);
this._canvasTextBoxColorPicker = (Windows.UI.Xaml.Controls.ColorPicker)GetTemplateChild(CanvasTextBoxColorPickerName);
_canvasTextBoxFontSizeTextBox = (TextBox)GetTemplateChild(CanvasTextBoxFontSizeTextBoxName);
_canvasComboBoxFontSizeTextBox = (ComboBox)GetTemplateChild(CanvasComboBoxFontSizeTextBoxName);
_canvasTextBoxItalicButton = (ToggleButton)GetTemplateChild(CanvasTextBoxItalicButtonName);
_canvasTextBoxBoldButton = (ToggleButton)GetTemplateChild(CanvasTextBoxBoldButtonName);
_drawingSurfaceRenderer = (InfiniteCanvasVirtualDrawingSurface)GetTemplateChild(DrawingSurfaceRendererName);
@ -296,7 +296,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
private void UnRegisterEvents()
{
_canvasTextBoxFontSizeTextBox.TextChanged -= CanvasTextBoxFontSizeTextBox_TextChanged;
_canvasComboBoxFontSizeTextBox.SelectionChanged -= CanvasComboBoxFontSizeTextBox_SelectionChanged;
_canvasTextBoxItalicButton.Click -= CanvasTextBoxItalicButton_Clicked;
_canvasTextBoxBoldButton.Click -= CanvasTextBoxBoldButton_Clicked;
_canvasTextBoxColorPicker.ColorChanged -= CanvasTextBoxColorPicker_ColorChanged;
@ -314,13 +314,14 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
Unloaded -= InfiniteCanvas_Unloaded;
Application.Current.LeavingBackground -= Current_LeavingBackground;
_drawingSurfaceRenderer.CommandExecuted -= DrawingSurfaceRenderer_CommandExecuted;
_canvasTextBoxFontSizeTextBox.PreviewKeyDown -= CanvasTextBoxFontSizeTextBox_PreviewKeyDown;
_canvasComboBoxFontSizeTextBox.PreviewKeyDown -= CanvasComboBoxFontSizeTextBox_PreviewKeyDown;
_canvasComboBoxFontSizeTextBox.TextSubmitted -= CanvasComboBoxFontSizeTextBox_TextSubmitted;
Loaded -= InfiniteCanvas_Loaded;
}
private void RegisterEvents()
{
_canvasTextBoxFontSizeTextBox.TextChanged += CanvasTextBoxFontSizeTextBox_TextChanged;
_canvasComboBoxFontSizeTextBox.SelectionChanged += CanvasComboBoxFontSizeTextBox_SelectionChanged;
_canvasTextBoxItalicButton.Click += CanvasTextBoxItalicButton_Clicked;
_canvasTextBoxBoldButton.Click += CanvasTextBoxBoldButton_Clicked;
_canvasTextBoxColorPicker.ColorChanged += CanvasTextBoxColorPicker_ColorChanged;
@ -338,7 +339,8 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
Unloaded += InfiniteCanvas_Unloaded;
Application.Current.LeavingBackground += Current_LeavingBackground;
_drawingSurfaceRenderer.CommandExecuted += DrawingSurfaceRenderer_CommandExecuted;
_canvasTextBoxFontSizeTextBox.PreviewKeyDown += CanvasTextBoxFontSizeTextBox_PreviewKeyDown;
_canvasComboBoxFontSizeTextBox.PreviewKeyDown += CanvasComboBoxFontSizeTextBox_PreviewKeyDown;
_canvasComboBoxFontSizeTextBox.TextSubmitted += CanvasComboBoxFontSizeTextBox_TextSubmitted;
Loaded += InfiniteCanvas_Loaded;
}
@ -366,7 +368,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
SetCanvasWidthHeight();
_canvasTextBox.UpdateFontSize(TextFontSize);
SetFontSize(_textFontSize);
}
private void SetZoomFactor()

Просмотреть файл

@ -97,13 +97,32 @@
</ToggleButton.Content>
</ToggleButton>
<TextBox x:Name="CanvasTextBoxFontSizeTextBox"
Width="64"
Height="32"
InputScope="Number"
MaxLength="3"
Text="22"
ToolTipService.ToolTip="Font Size" />
<ComboBox x:Name="CanvasComboBoxFontSizeTextBox"
MinWidth="64"
Height="32"
SelectedIndex="9"
Margin="0,0,12,0"
VerticalAlignment="Center"
ToolTipService.ToolTip="Font Size"
IsEditable="True"
>
<ComboBoxItem Content="8" />
<ComboBoxItem Content="9" />
<ComboBoxItem Content="10" />
<ComboBoxItem Content="11" />
<ComboBoxItem Content="12" />
<ComboBoxItem Content="14" />
<ComboBoxItem Content="16" />
<ComboBoxItem Content="18" />
<ComboBoxItem Content="20" />
<ComboBoxItem Content="22" />
<ComboBoxItem Content="24" />
<ComboBoxItem Content="26" />
<ComboBoxItem Content="28" />
<ComboBoxItem Content="36" />
<ComboBoxItem Content="48" />
<ComboBoxItem Content="72" />
</ComboBox>
</StackPanel>
</StackPanel>

Просмотреть файл

@ -37,21 +37,28 @@ namespace Microsoft.Toolkit
#endif
)
{
// Try to get the Task<T>.Result property. This method would've
// been called anyway after the type checks, but using that to
// validate the input type saves some additional reflection calls.
// Furthermore, doing this also makes the method flexible enough to
// cases whether the input Task<T> is actually an instance of some
// runtime-specific type that inherits from Task<T>.
PropertyInfo? propertyInfo =
// We need an explicit check to ensure the input task is not the cached
// Task.CompletedTask instance, because that can internally be stored as
// a Task<T> for some given T (eg. on .NET 5 it's VoidTaskResult), which
// would cause the following code to return that result instead of null.
if (task != Task.CompletedTask)
{
// Try to get the Task<T>.Result property. This method would've
// been called anyway after the type checks, but using that to
// validate the input type saves some additional reflection calls.
// Furthermore, doing this also makes the method flexible enough to
// cases whether the input Task<T> is actually an instance of some
// runtime-specific type that inherits from Task<T>.
PropertyInfo? propertyInfo =
#if NETSTANDARD1_4
task.GetType().GetRuntimeProperty(nameof(Task<object>.Result));
task.GetType().GetRuntimeProperty(nameof(Task<object>.Result));
#else
task.GetType().GetProperty(nameof(Task<object>.Result));
task.GetType().GetProperty(nameof(Task<object>.Result));
#endif
// Return the result, if possible
return propertyInfo?.GetValue(task);
// Return the result, if possible
return propertyInfo?.GetValue(task);
}
}
return null;

Просмотреть файл

@ -37,6 +37,13 @@ namespace UnitTests.Extensions
Assert.AreEqual(42, ((Task)tcs.Task).GetResultOrDefault());
}
[TestCategory("TaskExtensions")]
[TestMethod]
public void Test_TaskExtensions_ResultOrDefault_FromTaskCompleted()
{
Assert.AreEqual(null, Task.CompletedTask.GetResultOrDefault());
}
[TestCategory("TaskExtensions")]
[TestMethod]
public async Task Test_TaskExtensions_ResultOrDefault_FromAsyncTaskMethodBuilder()