From e1de13fe97fab250059dd14dc8389f85f5916e36 Mon Sep 17 00:00:00 2001 From: izakusilov Date: Sat, 24 Oct 2026 13:47:17 +0000 Subject: [PATCH 1/7] Remove unused fields from PlotBase --- Source/OxyPlot.Avalonia/PlotBase.cs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/Source/OxyPlot.Avalonia/PlotBase.cs b/Source/OxyPlot.Avalonia/PlotBase.cs index f9f348e..6bf434f 100644 --- a/Source/OxyPlot.Avalonia/PlotBase.cs +++ b/Source/OxyPlot.Avalonia/PlotBase.cs @@ -84,16 +84,6 @@ public abstract partial class PlotBase : TemplatedControl, IPlotView /// private ContentControl zoomControl; - /// - /// The is visible to user cache. - /// - private bool isVisibleToUserCache; - - /// - /// The cached parent. - /// - private Control containerCache; - /// /// Initializes a new instance of the class. /// From e4afca085da63ffc02aa9e2b94ce978ce6b35599 Mon Sep 17 00:00:00 2001 From: izakusilov Date: Sat, 24 Oct 2026 13:49:00 +0000 Subject: [PATCH 2/7] Catch clipboard errors in SetClipboardText --- Source/OxyPlot.Avalonia/PlotBase.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Source/OxyPlot.Avalonia/PlotBase.cs b/Source/OxyPlot.Avalonia/PlotBase.cs index 6bf434f..b153f6e 100644 --- a/Source/OxyPlot.Avalonia/PlotBase.cs +++ b/Source/OxyPlot.Avalonia/PlotBase.cs @@ -356,9 +356,20 @@ public void ShowZoomRectangle(OxyRect r) /// The text. public async void SetClipboardText(string text) { - if (TopLevel.GetTopLevel(this) is { Clipboard: { } clipboard }) + // The method is declared as void in IPlotView, so there is nobody to catch an exception from here. + // Without try/catch an unavailable or busy clipboard would crash the whole application. + try { - await clipboard.SetTextAsync(text).ConfigureAwait(true); + if (TopLevel.GetTopLevel(this) is { Clipboard: { } clipboard }) + { + await clipboard.SetTextAsync(text).ConfigureAwait(true); + } + } + catch (Exception ex) + { + global::Avalonia.Logging.Logger + .TryGet(global::Avalonia.Logging.LogEventLevel.Warning, global::Avalonia.Logging.LogArea.Control) + ?.Log(this, "Failed to write text to the clipboard: {0}", ex.Message); } } From f2fd4f202e01a78a2a953fbae3dbcabc19932ec5 Mon Sep 17 00:00:00 2001 From: izakusilov Date: Sat, 24 Oct 2026 13:49:00 +0000 Subject: [PATCH 3/7] Use the control dispatcher in PlotBase --- Source/OxyPlot.Avalonia/PlotBase.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Source/OxyPlot.Avalonia/PlotBase.cs b/Source/OxyPlot.Avalonia/PlotBase.cs index b153f6e..0142e4b 100644 --- a/Source/OxyPlot.Avalonia/PlotBase.cs +++ b/Source/OxyPlot.Avalonia/PlotBase.cs @@ -552,15 +552,17 @@ private void UpdateVisuals() /// Invokes the specified action on the dispatcher, if necessary. /// /// The action. - private static void BeginInvoke(Action action) + private void BeginInvoke(Action action) { - if (Dispatcher.UIThread.CheckAccess()) + // The dispatcher of the control, not Dispatcher.UIThread: in Avalonia 12 a control can live + // in a window on another UI thread, and then the main thread has no access to it. + if (CheckAccess()) { action?.Invoke(); } else { - Dispatcher.UIThread.InvokeAsync(action, DispatcherPriority.Loaded); + Dispatcher.InvokeAsync(action, DispatcherPriority.Loaded); } } } From 46fda582769a66ddad166d87248c0c7dfaa1422d Mon Sep 17 00:00:00 2001 From: izakusilov Date: Sat, 24 Oct 2026 13:49:00 +0000 Subject: [PATCH 4/7] Clear the plot when the model is removed --- Source/OxyPlot.Avalonia/PlotBase.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Source/OxyPlot.Avalonia/PlotBase.cs b/Source/OxyPlot.Avalonia/PlotBase.cs index 0142e4b..922da33 100644 --- a/Source/OxyPlot.Avalonia/PlotBase.cs +++ b/Source/OxyPlot.Avalonia/PlotBase.cs @@ -398,7 +398,20 @@ protected override Size ArrangeOverride(Size finalSize) /// The update Data. protected void UpdateModel(bool updateData = true) { - if (Width <= 0 || Height <= 0 || ActualModel == null) + if (ActualModel == null) + { + // The model was removed: the old plot is erased by UpdateVisuals, but it is only called by + // the layout pass triggered by the isPlotInvalidated flag. Without the flag the picture would stay on the screen. + isUpdateRequired = 0; + if (Interlocked.CompareExchange(ref isPlotInvalidated, 1, 0) == 0) + { + BeginInvoke(InvalidateArrange); + } + + return; + } + + if (Width <= 0 || Height <= 0) { isUpdateRequired = 0; return; From 6ba22e56c7544ca6dc26c9d501cbcf5ff5f8b8ac Mon Sep 17 00:00:00 2001 From: izakusilov Date: Sat, 24 Oct 2026 13:49:00 +0000 Subject: [PATCH 5/7] Make PlotBase focusable --- Source/OxyPlot.Avalonia/PlotBase.Properties.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Source/OxyPlot.Avalonia/PlotBase.Properties.cs b/Source/OxyPlot.Avalonia/PlotBase.Properties.cs index 92dd5a8..d2b62d3 100644 --- a/Source/OxyPlot.Avalonia/PlotBase.Properties.cs +++ b/Source/OxyPlot.Avalonia/PlotBase.Properties.cs @@ -55,6 +55,11 @@ public partial class PlotBase static PlotBase() { + // Without this the control does not take focus, and the keyboard commands of the plot do not work. + FocusableProperty.OverrideDefaultValue(true); + + // The plot must not be part of the Tab order, it takes focus only by a click. + IsTabStopProperty.OverrideDefaultValue(false); } /// From 91889ad6a273c692803108077090098d60cac8aa Mon Sep 17 00:00:00 2001 From: izakusilov Date: Sat, 24 Oct 2026 13:49:00 +0000 Subject: [PATCH 6/7] Fix mouse wheel delta for touchpads --- Source/OxyPlot.Avalonia/Utilities/ConverterExtensions.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Source/OxyPlot.Avalonia/Utilities/ConverterExtensions.cs b/Source/OxyPlot.Avalonia/Utilities/ConverterExtensions.cs index 7eef9a9..0b55aa6 100644 --- a/Source/OxyPlot.Avalonia/Utilities/ConverterExtensions.cs +++ b/Source/OxyPlot.Avalonia/Utilities/ConverterExtensions.cs @@ -432,11 +432,16 @@ public static OxyModifierKeys ToModifierKeys(this KeyModifiers modifiers) /// A containing the converted event arguments. public static OxyMouseWheelEventArgs ToMouseWheelEventArgs(this PointerWheelEventArgs e, IInputElement relativeTo) { + // Delta is a fraction of a wheel step, and for a touchpad it is less than one. + // Casting to int before the multiplication zeroed such scrolling, so the rounding is done afterwards. + // The horizontal delta is used only when the vertical one is zero, otherwise a diagonal gesture doubled the step. + var wheel = e.Delta.Y != 0 ? e.Delta.Y : e.Delta.X; + return new OxyMouseWheelEventArgs { Position = e.GetPosition(relativeTo as Visual).ToScreenPoint(), ModifierKeys = e.KeyModifiers.ToModifierKeys(), - Delta = (int)(e.Delta.Y + e.Delta.X) * 120 + Delta = (int)Math.Round(wheel * 120, MidpointRounding.AwayFromZero) }; } From bbd4a979c80d60076b6514c6468a313425dd8f0c Mon Sep 17 00:00:00 2001 From: izakusilov Date: Sat, 24 Oct 2026 13:49:00 +0000 Subject: [PATCH 7/7] Fix context menu and pointer capture handling --- Source/OxyPlot.Avalonia/PlotBase.Events.cs | 113 +++++++++++++++++---- Source/OxyPlot.Avalonia/PlotBase.cs | 13 ++- 2 files changed, 107 insertions(+), 19 deletions(-) diff --git a/Source/OxyPlot.Avalonia/PlotBase.Events.cs b/Source/OxyPlot.Avalonia/PlotBase.Events.cs index 3443a61..0d53e27 100644 --- a/Source/OxyPlot.Avalonia/PlotBase.Events.cs +++ b/Source/OxyPlot.Avalonia/PlotBase.Events.cs @@ -12,7 +12,6 @@ namespace OxyPlot.Avalonia { using global::Avalonia.Input; - using System; using System.Collections.Generic; using System.Linq; @@ -57,6 +56,16 @@ protected override void OnPointerWheelChanged(PointerWheelEventArgs e) /// private SortedDictionary TouchPositions { get; } = new SortedDictionary(); + /// + /// The last known position of the pointer. + /// + private ScreenPoint lastPointerPosition; + + /// + /// Indicates that the capture is released by itself, not by the system. + /// + private bool isReleasingPointer; + /// /// Invoked when an unhandled MouseDown attached event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event. /// @@ -72,6 +81,12 @@ protected override void OnPointerPressed(PointerPressedEventArgs e) Focus(); e.Pointer.Capture(this); + // The press point is remembered for any pointer: it decides whether the context menu is shown. + mouseDownPoint = e.GetPosition(this).ToScreenPoint(); + mouseDownPointerType = e.Pointer.Type; + hasMouseDownPoint = true; + lastPointerPosition = mouseDownPoint; + if (e.Pointer.Type == PointerType.Touch) { var position = e.GetPosition(this).ToScreenPoint(); @@ -93,9 +108,6 @@ protected override void OnPointerPressed(PointerPressedEventArgs e) } else { - // store the mouse down point, check it when mouse button is released to determine if the context menu should be shown - mouseDownPoint = e.GetPosition(this).ToScreenPoint(); - e.Handled = ActualController.HandleMouseDown(this, e.ToMouseDownEventArgs(this)); } } @@ -112,6 +124,8 @@ protected override void OnPointerMoved(PointerEventArgs e) return; } + lastPointerPosition = e.GetPosition(this).ToScreenPoint(); + if (e.Pointer.Type == PointerType.Touch) { var point = e.GetPosition(this).ToScreenPoint(); @@ -135,13 +149,30 @@ protected override void OnPointerMoved(PointerEventArgs e) /// The that contains the event data. The event data reports that the mouse button was released. protected override void OnPointerReleased(PointerReleasedEventArgs e) { + // The state is remembered before the base method: it raises ContextRequested + // and copies its Handled here. Otherwise, when a context menu is set, + // the controller would not receive the release and the manipulator would stay active. + var handledBefore = e.Handled; base.OnPointerReleased(e); - if (e.Handled) + + // The capture is always released, even if the event has already been marked as handled. + // Otherwise the pointer stays captured after a right click and panning gets stuck. + // Releasing the capture synchronously raises PointerCaptureLost: the flag distinguishes it from a loss of + // capture by the system, otherwise the manipulator would be completed twice. + isReleasingPointer = true; + try { - return; + e.Pointer.Capture(null); + } + finally + { + isReleasingPointer = false; } - e.Pointer.Capture(null); + if (handledBefore) + { + return; + } if (e.Pointer.Type == PointerType.Touch) { @@ -165,24 +196,70 @@ protected override void OnPointerReleased(PointerReleasedEventArgs e) else { e.Handled = ActualController.HandleMouseUp(this, e.ToMouseReleasedEventArgs(this)); + } + } - // Open the context menu - var p = e.GetPosition(this).ToScreenPoint(); - var d = p.DistanceTo(mouseDownPoint); + /// + /// Completes the action started by a press if the pointer capture is lost without a release: + /// the window lost activation, the system cancelled the touch, etc. Otherwise the manipulator of the controller + /// stays active and the plot is panned by plain mouse movement, and the touch stays forever + /// in , so the following gestures do not start. + /// + /// The event data. + protected override void OnPointerCaptureLost(PointerCaptureLostEventArgs e) + { + base.OnPointerCaptureLost(e); + if (isReleasingPointer) + { + return; + } - if (ContextMenu != null) + if (e.Pointer.Type == PointerType.Touch) + { + if (TouchPositions.TryGetValue(e.Pointer.Id, out var position)) { - if (Math.Abs(d) < 1e-8 && e.InitialPressMouseButton == MouseButton.Right) + TouchPositions.Remove(e.Pointer.Id); + if (TouchPositions.Count == 0) { - ContextMenu.DataContext = DataContext; - ContextMenu.IsVisible = true; - } - else - { - ContextMenu.IsVisible = false; + ActualController.HandleTouchCompleted(this, new OxyTouchEventArgs + { + Position = position, + DeltaTranslation = new ScreenVector(0, 0), + DeltaScale = new ScreenVector(1, 1), + }); } } } + else + { + ActualController.HandleMouseUp(this, new OxyMouseEventArgs { Position = lastPointerPosition }); + } + } + + /// + /// Prepares the context menu for display. The menu is opened by Avalonia itself on the + /// event, so it does not need to be opened manually. + /// If the pointer was moved after the button was pressed (a zoom rectangle was drawn), the menu is not shown. + /// + /// The event source. + /// The event data. + private void OnPlotContextRequested(object sender, ContextRequestedEventArgs e) + { + // The menu is suppressed only for the mouse: for touch and pen it is opened by a long press, + // and the press point is irrelevant to that. + if (hasMouseDownPoint && mouseDownPointerType == PointerType.Mouse + && e.TryGetPosition(this, out var position) + && position.ToScreenPoint().DistanceTo(mouseDownPoint) > 1e-8) + { + // A handled event suppresses opening of the menu. + e.Handled = true; + return; + } + + if (ContextMenu != null) + { + ContextMenu.DataContext = DataContext; + } } /// diff --git a/Source/OxyPlot.Avalonia/PlotBase.cs b/Source/OxyPlot.Avalonia/PlotBase.cs index 922da33..e004e23 100644 --- a/Source/OxyPlot.Avalonia/PlotBase.cs +++ b/Source/OxyPlot.Avalonia/PlotBase.cs @@ -74,6 +74,16 @@ public abstract partial class PlotBase : TemplatedControl, IPlotView /// private ScreenPoint mouseDownPoint; + /// + /// Indicates that the press point has been filled in at least once. + /// + private bool hasMouseDownPoint; + + /// + /// The type of the pointer that made the last press. + /// + private PointerType mouseDownPointerType; + /// /// The overlays. /// @@ -91,8 +101,9 @@ protected PlotBase() { DisconnectCanvasWhileUpdating = true; trackerDefinitions = new ObservableCollection(); - + this.GetObservable(BoundsProperty).Subscribe(new AnonymousObserver(OnSizeChanged)); + AddHandler(ContextRequestedEvent, OnPlotContextRequested); } ///