From 88dd17083add2b1134334ad8a16ca5f5d3f20702 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro?= Date: Wed, 10 Feb 2021 01:49:23 +0000 Subject: [PATCH 01/13] Use deferred renderer on Android. --- .../Avalonia.Android/ActivityTracker.cs | 47 ------------ .../Avalonia.Android/AndroidPlatform.cs | 8 +- .../Avalonia.Android/AvaloniaActivity.cs | 3 +- src/Android/Avalonia.Android/AvaloniaView.cs | 27 ++++++- .../Avalonia.Android/ChoreographerTimer.cs | 74 +++++++++++++++++++ .../OpenGL/GlPlatformSurface.cs | 6 +- .../Avalonia.Android/OpenGL/GlRenderTarget.cs | 13 +++- .../Platform/SkiaPlatform/TopLevelImpl.cs | 8 +- 8 files changed, 122 insertions(+), 64 deletions(-) delete mode 100644 src/Android/Avalonia.Android/ActivityTracker.cs create mode 100644 src/Android/Avalonia.Android/ChoreographerTimer.cs diff --git a/src/Android/Avalonia.Android/ActivityTracker.cs b/src/Android/Avalonia.Android/ActivityTracker.cs deleted file mode 100644 index 2ad1d9e361..0000000000 --- a/src/Android/Avalonia.Android/ActivityTracker.cs +++ /dev/null @@ -1,47 +0,0 @@ -using Android.App; -using Android.OS; - -namespace Avalonia.Android -{ - internal class ActivityTracker : Java.Lang.Object, global::Android.App.Application.IActivityLifecycleCallbacks - { - public static Activity Current { get; private set; } - public void OnActivityCreated(Activity activity, Bundle savedInstanceState) - { - Current = activity; - } - - public void OnActivityDestroyed(Activity activity) - { - if (Current == activity) - Current = null; - } - - public void OnActivityPaused(Activity activity) - { - if (Current == activity) - Current = null; - } - - public void OnActivityResumed(Activity activity) - { - Current = activity; - } - - public void OnActivitySaveInstanceState(Activity activity, Bundle outState) - { - Current = activity; - } - - public void OnActivityStarted(Activity activity) - { - Current = activity; - } - - public void OnActivityStopped(Activity activity) - { - if (Current == activity) - Current = null; - } - } -} \ No newline at end of file diff --git a/src/Android/Avalonia.Android/AndroidPlatform.cs b/src/Android/Avalonia.Android/AndroidPlatform.cs index e0ceb0c8b7..043af6a8df 100644 --- a/src/Android/Avalonia.Android/AndroidPlatform.cs +++ b/src/Android/Avalonia.Android/AndroidPlatform.cs @@ -32,6 +32,7 @@ namespace Avalonia.Android class AndroidPlatform : IPlatformSettings, IWindowingPlatform { public static readonly AndroidPlatform Instance = new AndroidPlatform(); + public static AndroidPlatformOptions Options { get; private set; } public Size DoubleClickSize => new Size(4, 4); public TimeSpan DoubleClickTime => TimeSpan.FromMilliseconds(200); public double RenderScalingFactor => _scalingFactor; @@ -46,6 +47,8 @@ namespace Avalonia.Android public static void Initialize(Type appType, AndroidPlatformOptions options) { + Options = options; + AvaloniaLocator.CurrentMutable .Bind().ToTransient() .Bind().ToTransient() @@ -55,14 +58,12 @@ namespace Avalonia.Android .Bind().ToTransient() .Bind().ToConstant(Instance) .Bind().ToSingleton() - .Bind().ToConstant(new DefaultRenderTimer(60)) + .Bind().ToConstant(new ChoreographerTimer()) .Bind().ToConstant(new RenderLoop()) .Bind().ToSingleton() .Bind().ToConstant(new AssetLoader(appType.Assembly)); SkiaPlatform.Initialize(); - ((global::Android.App.Application) global::Android.App.Application.Context.ApplicationContext) - .RegisterActivityLifecycleCallbacks(new ActivityTracker()); if (options.UseGpu) { @@ -83,6 +84,7 @@ namespace Avalonia.Android public sealed class AndroidPlatformOptions { + public bool UseDeferredRendering { get; set; } = true; public bool UseGpu { get; set; } = true; } } diff --git a/src/Android/Avalonia.Android/AvaloniaActivity.cs b/src/Android/Avalonia.Android/AvaloniaActivity.cs index d3696aa41d..52b68f8e2f 100644 --- a/src/Android/Avalonia.Android/AvaloniaActivity.cs +++ b/src/Android/Avalonia.Android/AvaloniaActivity.cs @@ -13,9 +13,8 @@ namespace Avalonia.Android protected override void OnCreate(Bundle savedInstanceState) { - RequestWindowFeature(WindowFeatures.NoTitle); View = new AvaloniaView(this); - if(_content != null) + if (_content != null) View.Content = _content; SetContentView(View); TakeKeyEvents(true); diff --git a/src/Android/Avalonia.Android/AvaloniaView.cs b/src/Android/Avalonia.Android/AvaloniaView.cs index 72732a1f95..a60a17d08e 100644 --- a/src/Android/Avalonia.Android/AvaloniaView.cs +++ b/src/Android/Avalonia.Android/AvaloniaView.cs @@ -1,11 +1,12 @@ using System; using Android.Content; +using Android.Runtime; using Android.Views; using Android.Widget; using Avalonia.Android.Platform.SkiaPlatform; using Avalonia.Controls; using Avalonia.Controls.Embedding; -using Avalonia.Platform; +using Avalonia.Rendering; namespace Avalonia.Android { @@ -33,6 +34,30 @@ namespace Avalonia.Android return _view.View.DispatchKeyEvent(e); } + public override void OnVisibilityAggregated(bool isVisible) + { + base.OnVisibilityAggregated(isVisible); + OnVisibilityChanged(isVisible); + } + + protected override void OnVisibilityChanged(View changedView, [GeneratedEnum] ViewStates visibility) + { + base.OnVisibilityChanged(changedView, visibility); + OnVisibilityChanged(visibility == ViewStates.Visible); + } + + private void OnVisibilityChanged(bool isVisible) + { + if (isVisible) + { + _root.Renderer.Start(); + } + else + { + _root.Renderer.Stop(); + } + } + class ViewImpl : TopLevelImpl { public ViewImpl(Context context) : base(context) diff --git a/src/Android/Avalonia.Android/ChoreographerTimer.cs b/src/Android/Avalonia.Android/ChoreographerTimer.cs new file mode 100644 index 0000000000..12961fec83 --- /dev/null +++ b/src/Android/Avalonia.Android/ChoreographerTimer.cs @@ -0,0 +1,74 @@ +using System; +using System.Threading.Tasks; + +using Android.OS; +using Android.Views; + +using Avalonia.Rendering; + +using Java.Lang; + +namespace Avalonia.Android +{ + internal sealed class ChoreographerTimer : Java.Lang.Object, IRenderTimer, Choreographer.IFrameCallback + { + private readonly object _lock = new object(); + + private readonly Thread _thread; + private readonly TaskCompletionSource _choreographer = new TaskCompletionSource(); + + private Action _tick; + private int _count; + + public ChoreographerTimer() + { + _thread = new Thread(Loop); + _thread.Start(); + } + + public event Action Tick + { + add + { + lock (_lock) + { + _tick += value; + _count++; + + if (_count == 1) + { + _choreographer.Task.Result.PostFrameCallback(this); + } + } + } + remove + { + lock (_lock) + { + _tick -= value; + _count--; + } + } + } + + private void Loop() + { + Looper.Prepare(); + _choreographer.SetResult(Choreographer.Instance); + Looper.Loop(); + } + + public void DoFrame(long frameTimeNanos) + { + _tick?.Invoke(TimeSpan.FromTicks(frameTimeNanos / 100)); + + lock (_lock) + { + if (_count > 0) + { + Choreographer.Instance.PostFrameCallback(this); + } + } + } + } +} diff --git a/src/Android/Avalonia.Android/OpenGL/GlPlatformSurface.cs b/src/Android/Avalonia.Android/OpenGL/GlPlatformSurface.cs index 4f4c03fe77..a9710039f8 100644 --- a/src/Android/Avalonia.Android/OpenGL/GlPlatformSurface.cs +++ b/src/Android/Avalonia.Android/OpenGL/GlPlatformSurface.cs @@ -1,6 +1,4 @@ -using System.Linq; - -using Avalonia.OpenGL.Egl; +using Avalonia.OpenGL.Egl; using Avalonia.OpenGL.Surfaces; namespace Avalonia.Android.OpenGL @@ -17,7 +15,7 @@ namespace Avalonia.Android.OpenGL } public override IGlPlatformSurfaceRenderTarget CreateGlRenderTarget() => - new GlRenderTarget(_egl, _info, _egl.CreateWindowSurface(_info.Handle)); + new GlRenderTarget(_egl, _info, _egl.CreateWindowSurface(_info.Handle), _info.Handle); public static GlPlatformSurface TryCreate(IEglWindowGlPlatformSurfaceInfo info) { diff --git a/src/Android/Avalonia.Android/OpenGL/GlRenderTarget.cs b/src/Android/Avalonia.Android/OpenGL/GlRenderTarget.cs index 75bbd15e3e..f9071d9b27 100644 --- a/src/Android/Avalonia.Android/OpenGL/GlRenderTarget.cs +++ b/src/Android/Avalonia.Android/OpenGL/GlRenderTarget.cs @@ -1,23 +1,30 @@ -using Avalonia.OpenGL.Egl; +using System; + +using Avalonia.OpenGL.Egl; using Avalonia.OpenGL.Surfaces; namespace Avalonia.Android.OpenGL { - internal sealed class GlRenderTarget : EglPlatformSurfaceRenderTargetBase + internal sealed class GlRenderTarget : EglPlatformSurfaceRenderTargetBase, IGlPlatformSurfaceRenderTargetWithCorruptionInfo { private readonly EglGlPlatformSurfaceBase.IEglWindowGlPlatformSurfaceInfo _info; private readonly EglSurface _surface; + private readonly IntPtr _handle; public GlRenderTarget( EglPlatformOpenGlInterface egl, EglGlPlatformSurfaceBase.IEglWindowGlPlatformSurfaceInfo info, - EglSurface surface) + EglSurface surface, + IntPtr handle) : base(egl) { _info = info; _surface = surface; + _handle = handle; } + public bool IsCorrupted => _handle != _info.Handle; + public override IGlPlatformSurfaceRenderingSession BeginDraw() => BeginDraw(_surface, _info); } } diff --git a/src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs b/src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs index a8c7f7af9b..a71b574198 100644 --- a/src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs +++ b/src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs @@ -98,10 +98,10 @@ namespace Avalonia.Android.Platform.SkiaPlatform public IEnumerable Surfaces => new object[] { _gl, _framebuffer }; - public IRenderer CreateRenderer(IRenderRoot root) - { - return new ImmediateRenderer(root); - } + public IRenderer CreateRenderer(IRenderRoot root) => + AndroidPlatform.Options.UseDeferredRendering + ? new DeferredRenderer(root, AvaloniaLocator.Current.GetService()) { RenderOnlyOnRenderThread = true } + : new ImmediateRenderer(root); public virtual void Hide() { From b9a2f76cf0bcbb49e9bab5a50f00de8789c2e9e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro?= Date: Wed, 10 Feb 2021 23:58:51 +0000 Subject: [PATCH 02/13] Added timer subscriptions for views. --- src/Android/Avalonia.Android/AvaloniaView.cs | 8 +++++ .../Avalonia.Android/ChoreographerTimer.cs | 29 ++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/Android/Avalonia.Android/AvaloniaView.cs b/src/Android/Avalonia.Android/AvaloniaView.cs index a60a17d08e..8de3657283 100644 --- a/src/Android/Avalonia.Android/AvaloniaView.cs +++ b/src/Android/Avalonia.Android/AvaloniaView.cs @@ -15,6 +15,8 @@ namespace Avalonia.Android private readonly EmbeddableControlRoot _root; private readonly ViewImpl _view; + private IDisposable? _timerSubscription; + public AvaloniaView(Context context) : base(context) { _view = new ViewImpl(context); @@ -50,11 +52,17 @@ namespace Avalonia.Android { if (isVisible) { + if (AvaloniaLocator.Current.GetService() is ChoreographerTimer timer) + { + _timerSubscription = timer.SubscribeView(this); + } + _root.Renderer.Start(); } else { _root.Renderer.Stop(); + _timerSubscription?.Dispose(); } } diff --git a/src/Android/Avalonia.Android/ChoreographerTimer.cs b/src/Android/Avalonia.Android/ChoreographerTimer.cs index 12961fec83..1d898261a3 100644 --- a/src/Android/Avalonia.Android/ChoreographerTimer.cs +++ b/src/Android/Avalonia.Android/ChoreographerTimer.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Reactive.Disposables; using System.Threading.Tasks; using Android.OS; @@ -17,6 +19,8 @@ namespace Avalonia.Android private readonly Thread _thread; private readonly TaskCompletionSource _choreographer = new TaskCompletionSource(); + private readonly ISet _views = new HashSet(); + private Action _tick; private int _count; @@ -51,6 +55,29 @@ namespace Avalonia.Android } } + internal IDisposable SubscribeView(AvaloniaView view) + { + lock (_lock) + { + _views.Add(view); + + if (_views.Count == 1) + { + _choreographer.Task.Result.PostFrameCallback(this); + } + } + + return Disposable.Create( + () => + { + lock (_lock) + { + _views.Remove(view); + } + } + ); + } + private void Loop() { Looper.Prepare(); @@ -64,7 +91,7 @@ namespace Avalonia.Android lock (_lock) { - if (_count > 0) + if (_count > 0 && _views.Count > 0) { Choreographer.Instance.PostFrameCallback(this); } From e1bfe10f434e61fc4eaa36b8b2ed39c7cf6a0464 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro?= Date: Thu, 11 Feb 2021 00:02:10 +0000 Subject: [PATCH 03/13] Fixed CursorFactory. --- src/Android/Avalonia.Android/AndroidPlatform.cs | 2 +- src/Android/Avalonia.Android/CursorFactory.cs | 17 +++++++++++++---- .../Platform/SkiaPlatform/TopLevelImpl.cs | 2 +- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Android/Avalonia.Android/AndroidPlatform.cs b/src/Android/Avalonia.Android/AndroidPlatform.cs index 043af6a8df..e0ed9b1fda 100644 --- a/src/Android/Avalonia.Android/AndroidPlatform.cs +++ b/src/Android/Avalonia.Android/AndroidPlatform.cs @@ -51,7 +51,7 @@ namespace Avalonia.Android AvaloniaLocator.CurrentMutable .Bind().ToTransient() - .Bind().ToTransient() + .Bind().ToTransient() .Bind().ToSingleton() .Bind().ToConstant(Instance) .Bind().ToConstant(new AndroidThreadingInterface()) diff --git a/src/Android/Avalonia.Android/CursorFactory.cs b/src/Android/Avalonia.Android/CursorFactory.cs index 9eb28c67f9..6293637d4e 100644 --- a/src/Android/Avalonia.Android/CursorFactory.cs +++ b/src/Android/Avalonia.Android/CursorFactory.cs @@ -1,12 +1,21 @@ -using System; using Avalonia.Input; using Avalonia.Platform; namespace Avalonia.Android { - internal class CursorFactory : IStandardCursorFactory + internal class CursorFactory : ICursorFactory { - public IPlatformHandle GetCursor(StandardCursorType cursorType) - => new PlatformHandle(IntPtr.Zero, "ZeroCursor"); + public ICursorImpl CreateCursor(IBitmapImpl cursor, PixelPoint hotSpot) => CursorImpl.ZeroCursor; + + public ICursorImpl GetCursor(StandardCursorType cursorType) => CursorImpl.ZeroCursor; + + private sealed class CursorImpl : ICursorImpl + { + public static CursorImpl ZeroCursor { get; } = new CursorImpl(); + + private CursorImpl() { } + + public void Dispose() { } + } } } diff --git a/src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs b/src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs index a71b574198..10bf414f25 100644 --- a/src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs +++ b/src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs @@ -123,7 +123,7 @@ namespace Avalonia.Android.Platform.SkiaPlatform return PixelPoint.FromPoint(point, 1); } - public void SetCursor(IPlatformHandle cursor) + public void SetCursor(ICursorImpl cursor) { //still not implemented } From f22730aca8f975e7525d44651af952db7b4df32c Mon Sep 17 00:00:00 2001 From: aguahombre Date: Mon, 8 Mar 2021 11:16:17 +0000 Subject: [PATCH 04/13] AutoCompleteBox validation not working for SelectedItem #5586 --- src/Avalonia.Controls/AutoCompleteBox.cs | 8 +++-- .../AutoCompleteBoxTests.cs | 32 +++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/Avalonia.Controls/AutoCompleteBox.cs b/src/Avalonia.Controls/AutoCompleteBox.cs index b59fd7abde..aab6a41890 100644 --- a/src/Avalonia.Controls/AutoCompleteBox.cs +++ b/src/Avalonia.Controls/AutoCompleteBox.cs @@ -483,7 +483,9 @@ namespace Avalonia.Controls AvaloniaProperty.RegisterDirect( nameof(SelectedItem), o => o.SelectedItem, - (o, v) => o.SelectedItem = v); + (o, v) => o.SelectedItem = v, + defaultBindingMode: BindingMode.TwoWay, + enableDataValidation: true); /// /// Identifies the @@ -1333,7 +1335,7 @@ namespace Avalonia.Controls base.OnApplyTemplate(e); } - + /// /// Called to update the validation state for properties for which data validation is /// enabled. @@ -1342,7 +1344,7 @@ namespace Avalonia.Controls /// The new binding value for the property. protected override void UpdateDataValidation(AvaloniaProperty property, BindingValue value) { - if (property == TextProperty) + if (property == TextProperty || property == SelectedItemProperty) { DataValidationErrors.SetError(this, value.Error); } diff --git a/tests/Avalonia.Controls.UnitTests/AutoCompleteBoxTests.cs b/tests/Avalonia.Controls.UnitTests/AutoCompleteBoxTests.cs index 3e78e951e2..b346fca330 100644 --- a/tests/Avalonia.Controls.UnitTests/AutoCompleteBoxTests.cs +++ b/tests/Avalonia.Controls.UnitTests/AutoCompleteBoxTests.cs @@ -14,6 +14,8 @@ using Avalonia.UnitTests; using Moq; using Xunit; using System.Collections.ObjectModel; +using System.Reactive.Linq; +using System.Reactive.Subjects; namespace Avalonia.Controls.UnitTests { @@ -396,6 +398,36 @@ namespace Avalonia.Controls.UnitTests Assert.Equal(control.Text, control.ItemSelector(input, selectedItem)); }); } + + [Fact] + public void Text_Validation() + { + RunTest((control, textbox) => + { + var exception = new InvalidCastException("failed validation"); + var textObservable = new BehaviorSubject(new BindingNotification(exception, BindingErrorType.DataValidationError)); + control.Bind(AutoCompleteBox.TextProperty, textObservable); + Dispatcher.UIThread.RunJobs(); + + Assert.Equal(DataValidationErrors.GetHasErrors(control), true); + Assert.Equal(DataValidationErrors.GetErrors(control).SequenceEqual(new[] { exception }), true); + }); + } + + [Fact] + public void SelectedItem_Validation() + { + RunTest((control, textbox) => + { + var exception = new InvalidCastException("failed validation"); + var itemObservable = new BehaviorSubject(new BindingNotification(exception, BindingErrorType.DataValidationError)); + control.Bind(AutoCompleteBox.SelectedItemProperty, itemObservable); + Dispatcher.UIThread.RunJobs(); + + Assert.Equal(DataValidationErrors.GetHasErrors(control), true); + Assert.Equal(DataValidationErrors.GetErrors(control).SequenceEqual(new[] { exception }), true); + }); + } /// /// Retrieves a defined predicate filter through a new AutoCompleteBox From 5d130b5d7938efc36beda0eab7715e4a22e7ae06 Mon Sep 17 00:00:00 2001 From: aguahombre Date: Mon, 8 Mar 2021 15:21:31 +0000 Subject: [PATCH 05/13] Add data validation error support to NumericUpDown control #3591 --- .../NumericUpDown/NumericUpDown.cs | 18 +++- .../NumericUpDownTests.cs | 95 +++++++++++++++++++ 2 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 tests/Avalonia.Controls.UnitTests/NumericUpDownTests.cs diff --git a/src/Avalonia.Controls/NumericUpDown/NumericUpDown.cs b/src/Avalonia.Controls/NumericUpDown/NumericUpDown.cs index b6b71644a3..abfbc038eb 100644 --- a/src/Avalonia.Controls/NumericUpDown/NumericUpDown.cs +++ b/src/Avalonia.Controls/NumericUpDown/NumericUpDown.cs @@ -91,14 +91,14 @@ namespace Avalonia.Controls /// public static readonly DirectProperty TextProperty = AvaloniaProperty.RegisterDirect(nameof(Text), o => o.Text, (o, v) => o.Text = v, - defaultBindingMode: BindingMode.TwoWay); + defaultBindingMode: BindingMode.TwoWay, enableDataValidation: true); /// /// Defines the property. /// public static readonly DirectProperty ValueProperty = AvaloniaProperty.RegisterDirect(nameof(Value), updown => updown.Value, - (updown, v) => updown.Value = v, defaultBindingMode: BindingMode.TwoWay); + (updown, v) => updown.Value = v, defaultBindingMode: BindingMode.TwoWay, enableDataValidation: true); /// /// Defines the property. @@ -370,6 +370,20 @@ namespace Avalonia.Controls } } + /// + /// Called to update the validation state for properties for which data validation is + /// enabled. + /// + /// The property. + /// The new binding value for the property. + protected override void UpdateDataValidation(AvaloniaProperty property, BindingValue value) + { + if (property == TextProperty || property == ValueProperty) + { + DataValidationErrors.SetError(this, value.Error); + } + } + /// /// Called when the property value changed. /// diff --git a/tests/Avalonia.Controls.UnitTests/NumericUpDownTests.cs b/tests/Avalonia.Controls.UnitTests/NumericUpDownTests.cs new file mode 100644 index 0000000000..4cef7e4d05 --- /dev/null +++ b/tests/Avalonia.Controls.UnitTests/NumericUpDownTests.cs @@ -0,0 +1,95 @@ +using System; +using System.Linq; +using System.Reactive.Subjects; +using Avalonia.Controls.Templates; +using Avalonia.Data; +using Avalonia.Threading; +using Avalonia.UnitTests; +using Xunit; + +namespace Avalonia.Controls.UnitTests +{ + public class NumericUpDownTests + { + private static TestServices Services => TestServices.StyledWindow; + + [Fact] + public void Text_Validation() + { + RunTest((control, textbox) => + { + var exception = new InvalidCastException("failed validation"); + var textObservable = new BehaviorSubject(new BindingNotification(exception, BindingErrorType.DataValidationError)); + control.Bind(NumericUpDown.TextProperty, textObservable); + Dispatcher.UIThread.RunJobs(); + + Assert.True(DataValidationErrors.GetHasErrors(control)); + Assert.True(DataValidationErrors.GetErrors(control).SequenceEqual(new[] { exception })); + }); + } + + [Fact] + public void Value_Validation() + { + RunTest((control, textbox) => + { + var exception = new InvalidCastException("failed validation"); + var valueObservable = new BehaviorSubject(new BindingNotification(exception, BindingErrorType.DataValidationError)); + control.Bind(NumericUpDown.ValueProperty, valueObservable); + Dispatcher.UIThread.RunJobs(); + + Assert.True(DataValidationErrors.GetHasErrors(control)); + Assert.True(DataValidationErrors.GetErrors(control).SequenceEqual(new[] { exception })); + }); + } + + private void RunTest(Action test) + { + using (UnitTestApplication.Start(Services)) + { + var control = CreateControl(); + TextBox textBox = GetTextBox(control); + var window = new Window { Content = control }; + window.ApplyTemplate(); + window.Presenter.ApplyTemplate(); + Dispatcher.UIThread.RunJobs(); + test.Invoke(control, textBox); + } + } + + private NumericUpDown CreateControl() + { + var control = new NumericUpDown + { + Template = CreateTemplate() + }; + + control.ApplyTemplate(); + return control; + } + private TextBox GetTextBox(NumericUpDown control) + { + return control.GetTemplateChildren() + .OfType() + .Select(b => b.Content) + .OfType() + .First(); + } + private IControlTemplate CreateTemplate() + { + return new FuncControlTemplate((control, scope) => + { + var textBox = + new TextBox + { + Name = "PART_TextBox" + }.RegisterInNameScope(scope); + return new ButtonSpinner + { + Name = "PART_Spinner", + Content = textBox, + }.RegisterInNameScope(scope); + }); + } + } +} From 89e5f3888d6cbe7778fe56013c738fc82a7d765f Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 17 Mar 2021 12:26:44 +0100 Subject: [PATCH 06/13] Added Opening/Closed for NativeMenu on OSX. Adds the `Opening` and `Closed` events for OSX to `NativeMenu` which relate to `menuWillOpen` and `menuDidClose`. Note that `NativeMenu` already exposed `NeedsUpdate` as `Opening`; this event has been moved to a separate `NeedsUpdate` event. --- native/Avalonia.Native/src/OSX/menu.h | 5 +-- native/Avalonia.Native/src/OSX/menu.mm | 26 +++++++++++++++ .../INativeMenuExporterEventsImplBridge.cs | 2 ++ src/Avalonia.Controls/NativeMenu.cs | 33 ++++++++++++++++++- src/Avalonia.Native/IAvnMenu.cs | 22 +++++++++++++ src/Avalonia.Native/avn.idl | 5 ++- 6 files changed, 87 insertions(+), 6 deletions(-) diff --git a/native/Avalonia.Native/src/OSX/menu.h b/native/Avalonia.Native/src/OSX/menu.h index 564fdc132b..ea68b354bc 100644 --- a/native/Avalonia.Native/src/OSX/menu.h +++ b/native/Avalonia.Native/src/OSX/menu.h @@ -60,7 +60,6 @@ public: void RaiseOnClicked(); }; - class AvnAppMenu : public ComSingleObject { private: @@ -71,10 +70,12 @@ public: FORWARD_IUNKNOWN() AvnAppMenu(IAvnMenuEvents* events); - + AvnMenu* GetNative(); void RaiseNeedsUpdate (); + void RaiseOpening(); + void RaiseClosed(); virtual HRESULT InsertItem (int index, IAvnMenuItem* item) override; diff --git a/native/Avalonia.Native/src/OSX/menu.mm b/native/Avalonia.Native/src/OSX/menu.mm index ea5cca9ce8..c33a447fcc 100644 --- a/native/Avalonia.Native/src/OSX/menu.mm +++ b/native/Avalonia.Native/src/OSX/menu.mm @@ -298,6 +298,23 @@ void AvnAppMenu::RaiseNeedsUpdate() } } +void AvnAppMenu::RaiseOpening() +{ + if(_baseEvents != nullptr) + { + _baseEvents->Opening(); + } +} + +void AvnAppMenu::RaiseClosed() +{ + if(_baseEvents != nullptr) + { + _baseEvents->Closed(); + } +} + + HRESULT AvnAppMenu::InsertItem(int index, IAvnMenuItem *item) { @autoreleasepool @@ -382,6 +399,15 @@ HRESULT AvnAppMenu::Clear() _parent->RaiseNeedsUpdate(); } +- (void)menuWillOpen:(NSMenu *)menu +{ + _parent->RaiseOpening(); +} + +- (void)menuDidClose:(NSMenu *)menu +{ + _parent->RaiseClosed(); +} @end diff --git a/src/Avalonia.Controls/INativeMenuExporterEventsImplBridge.cs b/src/Avalonia.Controls/INativeMenuExporterEventsImplBridge.cs index 672d5c1a13..f492e6ca0f 100644 --- a/src/Avalonia.Controls/INativeMenuExporterEventsImplBridge.cs +++ b/src/Avalonia.Controls/INativeMenuExporterEventsImplBridge.cs @@ -3,5 +3,7 @@ namespace Avalonia.Controls public interface INativeMenuExporterEventsImplBridge { void RaiseNeedsUpdate (); + void RaiseOpening(); + void RaiseClosed(); } } diff --git a/src/Avalonia.Controls/NativeMenu.cs b/src/Avalonia.Controls/NativeMenu.cs index 38a9f03d29..58ee99722f 100644 --- a/src/Avalonia.Controls/NativeMenu.cs +++ b/src/Avalonia.Controls/NativeMenu.cs @@ -12,13 +12,34 @@ namespace Avalonia.Controls private readonly AvaloniaList _items = new AvaloniaList { ResetBehavior = ResetBehavior.Remove }; private NativeMenuItem _parent; + [Content] public IList Items => _items; /// - /// Raised when the user clicks the menu and before its opened. Use this event to update the menu dynamically. + /// Raised when the menu requests an update. + /// + /// + /// Use this event to add, remove or modify menu items before a menu is + /// shown or a hotkey is pressed. + /// + public event EventHandler NeedsUpdate; + + /// + /// Raised before the menu is opened. /// + /// + /// Do not update the menu in this event; use . + /// public event EventHandler Opening; + + /// + /// Raised after the menu is closed. + /// + /// + /// Do not update the menu in this event; use . + /// + public event EventHandler Closed; public NativeMenu() { @@ -27,10 +48,20 @@ namespace Avalonia.Controls } void INativeMenuExporterEventsImplBridge.RaiseNeedsUpdate() + { + NeedsUpdate?.Invoke(this, EventArgs.Empty); + } + + void INativeMenuExporterEventsImplBridge.RaiseOpening() { Opening?.Invoke(this, EventArgs.Empty); } + void INativeMenuExporterEventsImplBridge.RaiseClosed() + { + Closed?.Invoke(this, EventArgs.Empty); + } + private void Validator(NativeMenuItemBase obj) { if (obj.Parent != null) diff --git a/src/Avalonia.Native/IAvnMenu.cs b/src/Avalonia.Native/IAvnMenu.cs index dd9464284f..cba59c481d 100644 --- a/src/Avalonia.Native/IAvnMenu.cs +++ b/src/Avalonia.Native/IAvnMenu.cs @@ -20,11 +20,23 @@ namespace Avalonia.Native.Interop { _parent?.RaiseNeedsUpdate(); } + + public void Opening() + { + _parent?.RaiseOpening(); + } + + public void Closed() + { + _parent?.RaiseClosed(); + } } partial interface IAvnMenu { void RaiseNeedsUpdate(); + void RaiseOpening(); + void RaiseClosed(); void Deinitialise(); } } @@ -45,6 +57,16 @@ namespace Avalonia.Native.Interop.Impl _exporter.UpdateIfNeeded(); } + public void RaiseOpening() + { + (ManagedMenu as INativeMenuExporterEventsImplBridge).RaiseOpening(); + } + + public void RaiseClosed() + { + (ManagedMenu as INativeMenuExporterEventsImplBridge).RaiseClosed(); + } + internal NativeMenu ManagedMenu { get; private set; } public static __MicroComIAvnMenuProxy Create(IAvaloniaNativeFactory factory) diff --git a/src/Avalonia.Native/avn.idl b/src/Avalonia.Native/avn.idl index 2693f5f139..af39dd8c2f 100644 --- a/src/Avalonia.Native/avn.idl +++ b/src/Avalonia.Native/avn.idl @@ -685,10 +685,9 @@ interface IAvnMenuItem : IUnknown [uuid(0af7df53-7632-42f4-a650-0992c361b477)] interface IAvnMenuEvents : IUnknown { - /** - * NeedsUpdate - */ void NeedsUpdate(); + void Opening(); + void Closed(); } [uuid(5142bb41-66ab-49e7-bb37-cd079c000f27)] From 0b16629e9bc05af7e4da7ff47d4dba85bfd0ba92 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 17 Mar 2021 14:53:16 +0100 Subject: [PATCH 07/13] Update apicompat baseline. --- src/Avalonia.Controls/ApiCompatBaseline.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/ApiCompatBaseline.txt b/src/Avalonia.Controls/ApiCompatBaseline.txt index aa8db78087..0284463f1c 100644 --- a/src/Avalonia.Controls/ApiCompatBaseline.txt +++ b/src/Avalonia.Controls/ApiCompatBaseline.txt @@ -1,7 +1,9 @@ Compat issues with assembly Avalonia.Controls: +InterfacesShouldHaveSameMembers : Interface member 'public void Avalonia.Controls.INativeMenuExporterEventsImplBridge.RaiseClosed()' is present in the implementation but not in the contract. +InterfacesShouldHaveSameMembers : Interface member 'public void Avalonia.Controls.INativeMenuExporterEventsImplBridge.RaiseOpening()' is present in the implementation but not in the contract. MembersMustExist : Member 'public void Avalonia.Controls.Embedding.Offscreen.OffscreenTopLevelImplBase.SetCursor(Avalonia.Platform.IPlatformHandle)' does not exist in the implementation but it does exist in the contract. MembersMustExist : Member 'public Avalonia.AvaloniaProperty Avalonia.AvaloniaProperty Avalonia.Controls.Notifications.NotificationCard.CloseOnClickProperty' does not exist in the implementation but it does exist in the contract. InterfacesShouldHaveSameMembers : Interface member 'public void Avalonia.Platform.ITopLevelImpl.SetCursor(Avalonia.Platform.ICursorImpl)' is present in the implementation but not in the contract. InterfacesShouldHaveSameMembers : Interface member 'public void Avalonia.Platform.ITopLevelImpl.SetCursor(Avalonia.Platform.IPlatformHandle)' is present in the contract but not in the implementation. MembersMustExist : Member 'public void Avalonia.Platform.ITopLevelImpl.SetCursor(Avalonia.Platform.IPlatformHandle)' does not exist in the implementation but it does exist in the contract. -Total Issues: 5 +Total Issues: 7 From d5690cc83435f17afaceb95073f38e59560b2b52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro?= Date: Fri, 19 Mar 2021 19:59:41 +0000 Subject: [PATCH 08/13] Upgraded target Android SDK to 11.0 (API level 30). --- samples/ControlCatalog.Android/ControlCatalog.Android.csproj | 2 +- samples/ControlCatalog.Android/Properties/AndroidManifest.xml | 2 +- src/Android/Avalonia.Android/Avalonia.Android.csproj | 2 +- .../Avalonia.AndroidTestApplication.csproj | 4 ++-- .../Properties/AndroidManifest.xml | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/samples/ControlCatalog.Android/ControlCatalog.Android.csproj b/samples/ControlCatalog.Android/ControlCatalog.Android.csproj index 20ca0576d4..1a68c4d732 100644 --- a/samples/ControlCatalog.Android/ControlCatalog.Android.csproj +++ b/samples/ControlCatalog.Android/ControlCatalog.Android.csproj @@ -16,7 +16,7 @@ Resources\Resource.Designer.cs Off False - v10.0 + v11.0 Properties\AndroidManifest.xml diff --git a/samples/ControlCatalog.Android/Properties/AndroidManifest.xml b/samples/ControlCatalog.Android/Properties/AndroidManifest.xml index 02e97f3065..9effda7e79 100644 --- a/samples/ControlCatalog.Android/Properties/AndroidManifest.xml +++ b/samples/ControlCatalog.Android/Properties/AndroidManifest.xml @@ -1,5 +1,5 @@  - + \ No newline at end of file diff --git a/src/Android/Avalonia.Android/Avalonia.Android.csproj b/src/Android/Avalonia.Android/Avalonia.Android.csproj index c170e8449c..8c6775733f 100644 --- a/src/Android/Avalonia.Android/Avalonia.Android.csproj +++ b/src/Android/Avalonia.Android/Avalonia.Android.csproj @@ -1,6 +1,6 @@  - monoandroid90 + monoandroid11.0 true diff --git a/src/Android/Avalonia.AndroidTestApplication/Avalonia.AndroidTestApplication.csproj b/src/Android/Avalonia.AndroidTestApplication/Avalonia.AndroidTestApplication.csproj index 4f49f3a863..9104f1618c 100644 --- a/src/Android/Avalonia.AndroidTestApplication/Avalonia.AndroidTestApplication.csproj +++ b/src/Android/Avalonia.AndroidTestApplication/Avalonia.AndroidTestApplication.csproj @@ -16,7 +16,7 @@ Resources\Resource.Designer.cs Off False - v10.0 + v11.0 Properties\AndroidManifest.xml @@ -150,4 +150,4 @@ - \ No newline at end of file + diff --git a/src/Android/Avalonia.AndroidTestApplication/Properties/AndroidManifest.xml b/src/Android/Avalonia.AndroidTestApplication/Properties/AndroidManifest.xml index e8e81da9de..57ee503005 100644 --- a/src/Android/Avalonia.AndroidTestApplication/Properties/AndroidManifest.xml +++ b/src/Android/Avalonia.AndroidTestApplication/Properties/AndroidManifest.xml @@ -1,6 +1,6 @@  - + \ No newline at end of file From c530956a0b003cf0bafe89419c83cc5e94a933cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro?= Date: Fri, 19 Mar 2021 22:17:27 +0000 Subject: [PATCH 09/13] Avalonia.Android code cleanup. --- .../Avalonia.Android/AndroidPlatform.cs | 22 +------------------ .../AndroidThreadingInterface.cs | 15 +++++++------ .../InvalidationAwareSurfaceView.cs | 2 ++ src/Android/Avalonia.Android/app.config | 11 ---------- 4 files changed, 11 insertions(+), 39 deletions(-) delete mode 100644 src/Android/Avalonia.Android/app.config diff --git a/src/Android/Avalonia.Android/AndroidPlatform.cs b/src/Android/Avalonia.Android/AndroidPlatform.cs index e0ed9b1fda..5e11d8eab2 100644 --- a/src/Android/Avalonia.Android/AndroidPlatform.cs +++ b/src/Android/Avalonia.Android/AndroidPlatform.cs @@ -29,21 +29,12 @@ namespace Avalonia namespace Avalonia.Android { - class AndroidPlatform : IPlatformSettings, IWindowingPlatform + class AndroidPlatform : IPlatformSettings { public static readonly AndroidPlatform Instance = new AndroidPlatform(); public static AndroidPlatformOptions Options { get; private set; } public Size DoubleClickSize => new Size(4, 4); public TimeSpan DoubleClickTime => TimeSpan.FromMilliseconds(200); - public double RenderScalingFactor => _scalingFactor; - public double LayoutScalingFactor => _scalingFactor; - - private readonly double _scalingFactor = 1; - - public AndroidPlatform() - { - _scalingFactor = global::Android.App.Application.Context.Resources.DisplayMetrics.ScaledDensity; - } public static void Initialize(Type appType, AndroidPlatformOptions options) { @@ -56,7 +47,6 @@ namespace Avalonia.Android .Bind().ToConstant(Instance) .Bind().ToConstant(new AndroidThreadingInterface()) .Bind().ToTransient() - .Bind().ToConstant(Instance) .Bind().ToSingleton() .Bind().ToConstant(new ChoreographerTimer()) .Bind().ToConstant(new RenderLoop()) @@ -70,16 +60,6 @@ namespace Avalonia.Android EglPlatformOpenGlInterface.TryInitialize(); } } - - public IWindowImpl CreateWindow() - { - throw new NotSupportedException(); - } - - public IWindowImpl CreateEmbeddableWindow() - { - throw new NotSupportedException(); - } } public sealed class AndroidPlatformOptions diff --git a/src/Android/Avalonia.Android/AndroidThreadingInterface.cs b/src/Android/Avalonia.Android/AndroidThreadingInterface.cs index 6fe77adca1..e72f0aed90 100644 --- a/src/Android/Avalonia.Android/AndroidThreadingInterface.cs +++ b/src/Android/Avalonia.Android/AndroidThreadingInterface.cs @@ -1,25 +1,26 @@ using System; using System.Reactive.Disposables; using System.Threading; + using Android.OS; + using Avalonia.Platform; using Avalonia.Threading; +using App = Android.App.Application; + namespace Avalonia.Android { - class AndroidThreadingInterface : IPlatformThreadingInterface + internal sealed class AndroidThreadingInterface : IPlatformThreadingInterface { private Handler _handler; public AndroidThreadingInterface() { - _handler = new Handler(global::Android.App.Application.Context.MainLooper); + _handler = new Handler(App.Context.MainLooper); } - public void RunLoop(CancellationToken cancellationToken) - { - return; - } + public void RunLoop(CancellationToken cancellationToken) => throw new NotSupportedException(); public IDisposable StartTimer(DispatcherPriority priority, TimeSpan interval, Action tick) { @@ -57,7 +58,7 @@ namespace Avalonia.Android }); } }, null, TimeSpan.Zero, interval); - + return Disposable.Create(() => { lock (l) diff --git a/src/Android/Avalonia.Android/Platform/SkiaPlatform/InvalidationAwareSurfaceView.cs b/src/Android/Avalonia.Android/Platform/SkiaPlatform/InvalidationAwareSurfaceView.cs index 02ea702236..16c5bdae3d 100644 --- a/src/Android/Avalonia.Android/Platform/SkiaPlatform/InvalidationAwareSurfaceView.cs +++ b/src/Android/Avalonia.Android/Platform/SkiaPlatform/InvalidationAwareSurfaceView.cs @@ -43,11 +43,13 @@ namespace Avalonia.Android } } + [Obsolete("deprecated")] public override void Invalidate(global::Android.Graphics.Rect dirty) { Invalidate(); } + [Obsolete("deprecated")] public override void Invalidate(int l, int t, int r, int b) { Invalidate(); diff --git a/src/Android/Avalonia.Android/app.config b/src/Android/Avalonia.Android/app.config deleted file mode 100644 index fc064cedfb..0000000000 --- a/src/Android/Avalonia.Android/app.config +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file From 7fb6e14716712ff2168708b909b017bd8c7b0096 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro?= Date: Sat, 20 Mar 2021 16:26:19 +0000 Subject: [PATCH 10/13] Use TouchDevice on Android. --- .../Avalonia.Android/AvaloniaActivity.cs | 8 +- .../Platform/Input/AndroidMouseDevice.cs | 14 --- .../Platform/SkiaPlatform/TopLevelImpl.cs | 10 +- .../Helpers/AndroidTouchEventsHelper.cs | 91 +++++-------------- 4 files changed, 28 insertions(+), 95 deletions(-) delete mode 100644 src/Android/Avalonia.Android/Platform/Input/AndroidMouseDevice.cs diff --git a/src/Android/Avalonia.Android/AvaloniaActivity.cs b/src/Android/Avalonia.Android/AvaloniaActivity.cs index 52b68f8e2f..b3a7585520 100644 --- a/src/Android/Avalonia.Android/AvaloniaActivity.cs +++ b/src/Android/Avalonia.Android/AvaloniaActivity.cs @@ -1,4 +1,3 @@ - using Android.App; using Android.OS; using Android.Views; @@ -7,7 +6,6 @@ namespace Avalonia.Android { public abstract class AvaloniaActivity : Activity { - internal AvaloniaView View; object _content; @@ -35,9 +33,7 @@ namespace Avalonia.Android } } - public override bool DispatchKeyEvent(KeyEvent e) - { - return View.DispatchKeyEvent(e); - } + public override bool DispatchKeyEvent(KeyEvent e) => + View.DispatchKeyEvent(e) ? true : base.DispatchKeyEvent(e); } } diff --git a/src/Android/Avalonia.Android/Platform/Input/AndroidMouseDevice.cs b/src/Android/Avalonia.Android/Platform/Input/AndroidMouseDevice.cs deleted file mode 100644 index d52eeb15e4..0000000000 --- a/src/Android/Avalonia.Android/Platform/Input/AndroidMouseDevice.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Avalonia.Input; - -namespace Avalonia.Android.Platform.Input -{ - public class AndroidMouseDevice : MouseDevice - { - public static AndroidMouseDevice Instance { get; } = new AndroidMouseDevice(); - - public AndroidMouseDevice() - { - - } - } -} \ No newline at end of file diff --git a/src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs b/src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs index 10bf414f25..4fd9bc040b 100644 --- a/src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs +++ b/src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs @@ -6,7 +6,6 @@ using Android.Runtime; using Android.Views; using Avalonia.Android.OpenGL; -using Avalonia.Android.Platform.Input; using Avalonia.Android.Platform.Specific; using Avalonia.Android.Platform.Specific.Helpers; using Avalonia.Controls; @@ -35,7 +34,7 @@ namespace Avalonia.Android.Platform.SkiaPlatform _view = new ViewImpl(context, this, placeOnTop); _keyboardHelper = new AndroidKeyboardEventsHelper(this); _touchHelper = new AndroidTouchEventsHelper(this, () => InputRoot, - p => GetAvaloniaPointFromEvent(p)); + GetAvaloniaPointFromEvent); _gl = GlPlatformSurface.TryCreate(this); _framebuffer = new FramebufferManager(this); @@ -44,8 +43,6 @@ namespace Avalonia.Android.Platform.SkiaPlatform _view.Resources.DisplayMetrics.HeightPixels); } - - private bool _handleEvents; public bool HandleEvents @@ -58,7 +55,8 @@ namespace Avalonia.Android.Platform.SkiaPlatform } } - public virtual Point GetAvaloniaPointFromEvent(MotionEvent e) => new Point(e.GetX(), e.GetY()); + public virtual Point GetAvaloniaPointFromEvent(MotionEvent e, int pointerIndex) => + new Point(e.GetX(pointerIndex), e.GetY(pointerIndex)) / RenderScaling; public IInputRoot InputRoot { get; private set; } @@ -76,7 +74,7 @@ namespace Avalonia.Android.Platform.SkiaPlatform } } - public IMouseDevice MouseDevice => AndroidMouseDevice.Instance; + public IMouseDevice MouseDevice { get; } = new MouseDevice(); public Action Closed { get; set; } diff --git a/src/Android/Avalonia.Android/Platform/Specific/Helpers/AndroidTouchEventsHelper.cs b/src/Android/Avalonia.Android/Platform/Specific/Helpers/AndroidTouchEventsHelper.cs index 0bfbb1c2df..6142598514 100644 --- a/src/Android/Avalonia.Android/Platform/Specific/Helpers/AndroidTouchEventsHelper.cs +++ b/src/Android/Avalonia.Android/Platform/Specific/Helpers/AndroidTouchEventsHelper.cs @@ -11,7 +11,7 @@ namespace Avalonia.Android.Platform.Specific.Helpers private TView _view; public bool HandleEvents { get; set; } - public AndroidTouchEventsHelper(TView view, Func getInputRoot, Func getPointfunc) + public AndroidTouchEventsHelper(TView view, Func getInputRoot, Func getPointfunc) { this._view = view; HandleEvents = true; @@ -19,11 +19,9 @@ namespace Avalonia.Android.Platform.Specific.Helpers _getInputRoot = getInputRoot; } - private DateTime _lastTouchMoveEventTime = DateTime.Now; - private Point? _lastTouchMovePoint; - private Func _getPointFunc; + private TouchDevice _touchDevice = new TouchDevice(); + private Func _getPointFunc; private Func _getInputRoot; - private Point _point; public bool? DispatchTouchEvent(MotionEvent e, out bool callBase) { @@ -33,89 +31,44 @@ namespace Avalonia.Android.Platform.Specific.Helpers return null; } - RawPointerEventType? mouseEventType = null; var eventTime = DateTime.Now; + //Basic touch support - switch (e.Action) + var pointerEventType = e.Action switch { - case MotionEventActions.Move: - //may be bot flood the evnt system with too many event especially on not so powerfull mobile devices - if ((eventTime - _lastTouchMoveEventTime).TotalMilliseconds > 10) - { - mouseEventType = RawPointerEventType.Move; - } - break; - - case MotionEventActions.Down: - mouseEventType = RawPointerEventType.LeftButtonDown; + MotionEventActions.Down => RawPointerEventType.TouchBegin, + MotionEventActions.Up => RawPointerEventType.TouchEnd, + MotionEventActions.Cancel => RawPointerEventType.TouchCancel, + _ => RawPointerEventType.TouchUpdate + }; - break; + if (e.Action.HasFlag(MotionEventActions.PointerDown)) + { + pointerEventType = RawPointerEventType.TouchBegin; + } - case MotionEventActions.Up: - mouseEventType = RawPointerEventType.LeftButtonUp; - break; + if (e.Action.HasFlag(MotionEventActions.PointerUp)) + { + pointerEventType = RawPointerEventType.TouchEnd; } - if (mouseEventType != null) + for (int i = 0; i < e.PointerCount; i++) { //if point is in view otherwise it's possible avalonia not to find the proper window to dispatch the event - _point = _getPointFunc(e); + var point = _getPointFunc(e, i); double x = _view.View.GetX(); double y = _view.View.GetY(); double r = x + _view.View.Width; double b = y + _view.View.Height; - if (x <= _point.X && r >= _point.X && y <= _point.Y && b >= _point.Y) + if (x <= point.X && r >= point.X && y <= point.Y && b >= point.Y) { var inputRoot = _getInputRoot(); - var mouseDevice = Avalonia.Android.Platform.Input.AndroidMouseDevice.Instance; - - //in order the controls to work in a predictable way - //we need to generate mouse move before first mouse down event - //as this is the way buttons are working every time - //otherwise there is a problem sometimes - if (mouseEventType == RawPointerEventType.LeftButtonDown) - { - var me = new RawPointerEventArgs(mouseDevice, (uint)eventTime.Ticks, inputRoot, - RawPointerEventType.Move, _point, RawInputModifiers.None); - _view.Input(me); - } - var mouseEvent = new RawPointerEventArgs(mouseDevice, (uint)eventTime.Ticks, inputRoot, - mouseEventType.Value, _point, RawInputModifiers.LeftMouseButton); + var mouseEvent = new RawTouchEventArgs(_touchDevice, (uint)eventTime.Ticks, inputRoot, + i == e.ActionIndex ? pointerEventType : RawPointerEventType.TouchUpdate, point, RawInputModifiers.None, e.GetPointerId(i)); _view.Input(mouseEvent); - - if (e.Action == MotionEventActions.Move && mouseDevice.Captured == null) - { - if (_lastTouchMovePoint != null) - { - //raise mouse scroll event so the scrollers - //are moving with the cursor - double vectorX = _point.X - _lastTouchMovePoint.Value.X; - double vectorY = _point.Y - _lastTouchMovePoint.Value.Y; - //based on test correction of 0.02 is working perfect - double correction = 0.02; - var ps = AndroidPlatform.Instance.LayoutScalingFactor; - var mouseWheelEvent = new RawMouseWheelEventArgs( - mouseDevice, - (uint)eventTime.Ticks, - inputRoot, - _point, - new Vector(vectorX * correction / ps, vectorY * correction / ps), RawInputModifiers.LeftMouseButton); - _view.Input(mouseWheelEvent); - } - _lastTouchMovePoint = _point; - _lastTouchMoveEventTime = eventTime; - } - else if (e.Action == MotionEventActions.Down) - { - _lastTouchMovePoint = _point; - } - else - { - _lastTouchMovePoint = null; - } } } From 0257bdf6eddfa63b770c644aab41dc7ed18670c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro?= Date: Sun, 21 Mar 2021 01:37:35 +0000 Subject: [PATCH 11/13] Implemented render scaling on Android. --- .../SkiaPlatform/AndroidFramebuffer.cs | 6 ++-- .../SkiaPlatform/FramebufferManager.cs | 2 +- .../Platform/SkiaPlatform/TopLevelImpl.cs | 28 ++++++------------- 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/src/Android/Avalonia.Android/Platform/SkiaPlatform/AndroidFramebuffer.cs b/src/Android/Avalonia.Android/Platform/SkiaPlatform/AndroidFramebuffer.cs index 2afa4e83f1..b115917622 100644 --- a/src/Android/Avalonia.Android/Platform/SkiaPlatform/AndroidFramebuffer.cs +++ b/src/Android/Avalonia.Android/Platform/SkiaPlatform/AndroidFramebuffer.cs @@ -10,7 +10,7 @@ namespace Avalonia.Android.Platform.SkiaPlatform { private IntPtr _window; - public AndroidFramebuffer(Surface surface) + public AndroidFramebuffer(Surface surface, double scaling) { if(surface == null) throw new ArgumentNullException(nameof(surface)); @@ -31,6 +31,8 @@ namespace Avalonia.Android.Platform.SkiaPlatform RowBytes = buffer.stride * (Format == PixelFormat.Rgb565 ? 2 : 4); Address = buffer.bits; + + Dpi = scaling * new Vector(96, 96); } public void Dispose() @@ -44,7 +46,7 @@ namespace Avalonia.Android.Platform.SkiaPlatform public IntPtr Address { get; set; } public PixelSize Size { get; } public int RowBytes { get; } - public Vector Dpi { get; } = new Vector(96, 96); + public Vector Dpi { get; } public PixelFormat Format { get; } [DllImport("android")] diff --git a/src/Android/Avalonia.Android/Platform/SkiaPlatform/FramebufferManager.cs b/src/Android/Avalonia.Android/Platform/SkiaPlatform/FramebufferManager.cs index 18c4796fae..56a4eb22d4 100644 --- a/src/Android/Avalonia.Android/Platform/SkiaPlatform/FramebufferManager.cs +++ b/src/Android/Avalonia.Android/Platform/SkiaPlatform/FramebufferManager.cs @@ -12,6 +12,6 @@ namespace Avalonia.Android.Platform.SkiaPlatform _topLevel = topLevel; } - public ILockedFramebuffer Lock() => new AndroidFramebuffer(_topLevel.InternalView.Holder.Surface); + public ILockedFramebuffer Lock() => new AndroidFramebuffer(_topLevel.InternalView.Holder.Surface, _topLevel.RenderScaling); } } diff --git a/src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs b/src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs index 4fd9bc040b..fe237a1719 100644 --- a/src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs +++ b/src/Android/Avalonia.Android/Platform/SkiaPlatform/TopLevelImpl.cs @@ -39,8 +39,10 @@ namespace Avalonia.Android.Platform.SkiaPlatform _gl = GlPlatformSurface.TryCreate(this); _framebuffer = new FramebufferManager(this); - MaxClientSize = new Size(_view.Resources.DisplayMetrics.WidthPixels, - _view.Resources.DisplayMetrics.HeightPixels); + RenderScaling = (int)_view.Resources.DisplayMetrics.Density; + + MaxClientSize = new PixelSize(_view.Resources.DisplayMetrics.WidthPixels, + _view.Resources.DisplayMetrics.HeightPixels).ToSize(RenderScaling); } private bool _handleEvents; @@ -60,19 +62,7 @@ namespace Avalonia.Android.Platform.SkiaPlatform public IInputRoot InputRoot { get; private set; } - public virtual Size ClientSize - { - get - { - if (_view == null) - return new Size(0, 0); - return new Size(_view.Width, _view.Height); - } - set - { - - } - } + public virtual Size ClientSize => Size.ToSize(RenderScaling); public IMouseDevice MouseDevice { get; } = new MouseDevice(); @@ -113,12 +103,12 @@ namespace Avalonia.Android.Platform.SkiaPlatform public Point PointToClient(PixelPoint point) { - return point.ToPoint(1); + return point.ToPoint(RenderScaling); } public PixelPoint PointToScreen(Point point) { - return PixelPoint.FromPoint(point, 1); + return PixelPoint.FromPoint(point, RenderScaling); } public void SetCursor(ICursorImpl cursor) @@ -136,7 +126,7 @@ namespace Avalonia.Android.Platform.SkiaPlatform _view.Visibility = ViewStates.Visible; } - public double RenderScaling => 1; + public double RenderScaling { get; } void Draw() { @@ -191,7 +181,7 @@ namespace Avalonia.Android.Platform.SkiaPlatform void ISurfaceHolderCallback.SurfaceChanged(ISurfaceHolder holder, Format format, int width, int height) { - var newSize = new Size(width, height); + var newSize = new PixelSize(width, height).ToSize(_tl.RenderScaling); if (newSize != _oldSize) { From 4ba8c4ebb76fc45e96a9bdb506edf6608590a4ad Mon Sep 17 00:00:00 2001 From: Friedrich von Never Date: Sun, 21 Mar 2021 19:03:13 +0700 Subject: [PATCH 12/13] Fix #5699: filter BuildAvaloniaApp by signature --- src/Avalonia.DesignerSupport/Remote/RemoteDesignerEntryPoint.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.DesignerSupport/Remote/RemoteDesignerEntryPoint.cs b/src/Avalonia.DesignerSupport/Remote/RemoteDesignerEntryPoint.cs index 764fc7b332..be2405efde 100644 --- a/src/Avalonia.DesignerSupport/Remote/RemoteDesignerEntryPoint.cs +++ b/src/Avalonia.DesignerSupport/Remote/RemoteDesignerEntryPoint.cs @@ -169,7 +169,7 @@ namespace Avalonia.DesignerSupport.Remote if (entryPoint == null) throw Die($"Assembly {args.AppPath} doesn't have an entry point"); var builderMethod = entryPoint.DeclaringType.GetMethod(BuilderMethodName, - BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); + BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, Array.Empty(), null); if (builderMethod == null) throw Die($"{entryPoint.DeclaringType.FullName} doesn't have a method named {BuilderMethodName}"); Design.IsDesignMode = true; From 0527ae0b64ee3ba26464ed3dd04db1d74ab0507b Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Mon, 22 Mar 2021 10:19:53 +0000 Subject: [PATCH 13/13] add null pointer check. --- native/Avalonia.Native/src/OSX/window.mm | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/native/Avalonia.Native/src/OSX/window.mm b/native/Avalonia.Native/src/OSX/window.mm index 4d63c486c4..b46655c7df 100644 --- a/native/Avalonia.Native/src/OSX/window.mm +++ b/native/Avalonia.Native/src/OSX/window.mm @@ -2231,9 +2231,12 @@ protected: { @autoreleasepool { - [Window setContentSize:NSSize{x, y}]; + if (Window != nullptr) + { + [Window setContentSize:NSSize{x, y}]; - [Window setFrameTopLeftPoint:ToNSPoint(ConvertPointY(lastPositionSet))]; + [Window setFrameTopLeftPoint:ToNSPoint(ConvertPointY(lastPositionSet))]; + } return S_OK; }