From 0befa22692d1fa32652f621042bc54188a7b5769 Mon Sep 17 00:00:00 2001 From: Nicolas Musset Date: Tue, 28 Aug 2018 21:49:34 +0900 Subject: [PATCH 1/5] Support invoking a task in the dispatcher. --- src/Avalonia.Base/Threading/Dispatcher.cs | 14 ++++++++++++++ src/Avalonia.Base/Threading/IDispatcher.cs | 18 ++++++++++++++++++ .../Avalonia.UnitTests/ImmediateDispatcher.cs | 19 ++++++++++++++++++- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Base/Threading/Dispatcher.cs b/src/Avalonia.Base/Threading/Dispatcher.cs index aa2a7a7a8e..55a9b6984a 100644 --- a/src/Avalonia.Base/Threading/Dispatcher.cs +++ b/src/Avalonia.Base/Threading/Dispatcher.cs @@ -92,6 +92,20 @@ namespace Avalonia.Threading return _jobRunner.InvokeAsync(function, priority); } + /// + public Task InvokeAsync(Func function, DispatcherPriority priority = DispatcherPriority.Normal) + { + Contract.Requires(function != null); + return _jobRunner.InvokeAsync(function, priority).Unwrap(); + } + + /// + public Task InvokeAsync(Func> function, DispatcherPriority priority = DispatcherPriority.Normal) + { + Contract.Requires(function != null); + return _jobRunner.InvokeAsync(function, priority).Unwrap(); + } + /// public void Post(Action action, DispatcherPriority priority = DispatcherPriority.Normal) { diff --git a/src/Avalonia.Base/Threading/IDispatcher.cs b/src/Avalonia.Base/Threading/IDispatcher.cs index 1fdc9da5fe..8f46f99283 100644 --- a/src/Avalonia.Base/Threading/IDispatcher.cs +++ b/src/Avalonia.Base/Threading/IDispatcher.cs @@ -40,5 +40,23 @@ namespace Avalonia.Threading /// The method. /// The priority with which to invoke the method. Task InvokeAsync(Func function, DispatcherPriority priority = DispatcherPriority.Normal); + + /// + /// Queues the specified work to run on the dispatcher thread and returns a proxy for the + /// task returned by . + /// + /// The work to execute asynchronously. + /// The priority with which to invoke the method. + /// A task that represents a proxy for the task returned by . + Task InvokeAsync(Func function, DispatcherPriority priority = DispatcherPriority.Normal); + + /// + /// Queues the specified work to run on the dispatcher thread and returns a proxy for the + /// task returned by . + /// + /// The work to execute asynchronously. + /// The priority with which to invoke the method. + /// A task that represents a proxy for the task returned by . + Task InvokeAsync(Func> function, DispatcherPriority priority = DispatcherPriority.Normal); } } \ No newline at end of file diff --git a/tests/Avalonia.UnitTests/ImmediateDispatcher.cs b/tests/Avalonia.UnitTests/ImmediateDispatcher.cs index 44d8c78054..fac4ee64e7 100644 --- a/tests/Avalonia.UnitTests/ImmediateDispatcher.cs +++ b/tests/Avalonia.UnitTests/ImmediateDispatcher.cs @@ -9,28 +9,45 @@ namespace Avalonia.UnitTests /// public class ImmediateDispatcher : IDispatcher { + /// public bool CheckAccess() { return true; } + /// public void Post(Action action, DispatcherPriority priority = DispatcherPriority.Normal) { action(); } + /// public Task InvokeAsync(Action action, DispatcherPriority priority = DispatcherPriority.Normal) { action(); - return Task.FromResult(null); + return Task.CompletedTask; } + /// public Task InvokeAsync(Func function, DispatcherPriority priority = DispatcherPriority.Normal) { var result = function(); return Task.FromResult(result); } + /// + public Task InvokeAsync(Func function, DispatcherPriority priority = DispatcherPriority.Normal) + { + return function(); + } + + /// + public Task InvokeAsync(Func> function, DispatcherPriority priority = DispatcherPriority.Normal) + { + return function(); + } + + /// public void VerifyAccess() { } From 298eef1c1b8e9950578c14a6b2396551868df0fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miha=20Marki=C4=8D?= Date: Sat, 1 Sep 2018 19:23:46 +0200 Subject: [PATCH 2/5] Fixes #1847 PasswordChar change doesn't affect render --- src/Avalonia.Controls/Presenters/TextPresenter.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Controls/Presenters/TextPresenter.cs b/src/Avalonia.Controls/Presenters/TextPresenter.cs index 636c836da5..349ae80362 100644 --- a/src/Avalonia.Controls/Presenters/TextPresenter.cs +++ b/src/Avalonia.Controls/Presenters/TextPresenter.cs @@ -36,7 +36,11 @@ namespace Avalonia.Controls.Presenters private int _selectionEnd; private bool _caretBlink; private IBrush _highlightBrush; - + + static TextPresenter() + { + AffectsRender(PasswordCharProperty); + } public TextPresenter() { _caretTimer = new DispatcherTimer(); @@ -50,6 +54,9 @@ namespace Avalonia.Controls.Presenters this.GetObservable(CaretIndexProperty) .Subscribe(CaretIndexChanged); + + this.GetObservable(PasswordCharProperty) + .Subscribe(_ => InvalidateFormattedText()); } public int CaretIndex @@ -116,7 +123,7 @@ namespace Avalonia.Controls.Presenters var start = Math.Min(selectionStart, selectionEnd); var length = Math.Max(selectionStart, selectionEnd) - start; - // issue #600: set constaint before any FormattedText manipulation + // issue #600: set constraint before any FormattedText manipulation // see base.Render(...) implementation FormattedText.Constraint = Bounds.Size; From 993f0a080ad3155c5c526c2ebb377b45797e8911 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miha=20Marki=C4=8D?= Date: Sat, 1 Sep 2018 20:07:00 +0200 Subject: [PATCH 3/5] Adds missing newline --- src/Avalonia.Controls/Presenters/TextPresenter.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Avalonia.Controls/Presenters/TextPresenter.cs b/src/Avalonia.Controls/Presenters/TextPresenter.cs index 349ae80362..a4dc35f736 100644 --- a/src/Avalonia.Controls/Presenters/TextPresenter.cs +++ b/src/Avalonia.Controls/Presenters/TextPresenter.cs @@ -41,6 +41,7 @@ namespace Avalonia.Controls.Presenters { AffectsRender(PasswordCharProperty); } + public TextPresenter() { _caretTimer = new DispatcherTimer(); From 9d89bfdfaba3c3332a010919d69a2f6740cbe711 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Wed, 5 Sep 2018 09:50:37 +0100 Subject: [PATCH 4/5] fix slider not updating value. --- src/Avalonia.Controls/Slider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/Slider.cs b/src/Avalonia.Controls/Slider.cs index 31113812d1..f1919dc236 100644 --- a/src/Avalonia.Controls/Slider.cs +++ b/src/Avalonia.Controls/Slider.cs @@ -172,7 +172,7 @@ namespace Avalonia.Controls private void MoveToNextTick(double value) { double next = SnapToTick(Math.Max(Minimum, Math.Min(Maximum, value))); - if (next != value) + if (Value != next) { Value = next; } From 39931f5fdeeb265efdf5b10fe93e2b4739ea82bb Mon Sep 17 00:00:00 2001 From: danwalmsley Date: Wed, 5 Sep 2018 10:28:41 +0100 Subject: [PATCH 5/5] use same fix as upcoming pr to prevent conflict. --- src/Avalonia.Controls/Slider.cs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Avalonia.Controls/Slider.cs b/src/Avalonia.Controls/Slider.cs index f1919dc236..32b0a1f259 100644 --- a/src/Avalonia.Controls/Slider.cs +++ b/src/Avalonia.Controls/Slider.cs @@ -171,11 +171,7 @@ namespace Avalonia.Controls /// Value that want to snap to closest Tick. private void MoveToNextTick(double value) { - double next = SnapToTick(Math.Max(Minimum, Math.Min(Maximum, value))); - if (Value != next) - { - Value = next; - } + Value = SnapToTick(Math.Max(Minimum, Math.Min(Maximum, value))); } /// @@ -194,4 +190,4 @@ namespace Avalonia.Controls return value; } } -} \ No newline at end of file +}