Skip to content

Commit 719f1c3

Browse files
kekekeksMrJul
authored andcommitted
Don't alter any InputElement properties from in-process dnd handler (#18288)
1 parent 3a97c6a commit 719f1c3

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

src/Avalonia.Controls/Platform/InProcessDragSource.cs

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ class InProcessDragSource : IPlatformDragSource
1919

2020
private DragDropEffects _allowedEffects;
2121
private IDataObject? _draggedData;
22-
private IInputRoot? _lastRoot;
22+
private TopLevel? _lastRoot;
2323
private Point _lastPosition;
24-
private StandardCursorType _lastCursorType;
25-
private object? _originalCursor;
24+
private StandardCursorType? _lastCursorType;
2625
private RawInputModifiers? _initialInputModifiers;
2726

2827
public InProcessDragSource()
@@ -77,7 +76,7 @@ private DragDropEffects RaiseEventAndUpdateCursor(RawDragEventType type, IInputR
7776
tl?.PlatformImpl?.Input?.Invoke(rawEvent);
7877

7978
var effect = GetPreferredEffect(rawEvent.Effects & _allowedEffects, modifiers);
80-
UpdateCursor(root, effect);
79+
UpdateCursor(tl, effect);
8180
return effect;
8281
}
8382

@@ -103,41 +102,24 @@ private static StandardCursorType GetCursorForDropEffect(DragDropEffects effects
103102
return StandardCursorType.No;
104103
}
105104

106-
private void UpdateCursor(IInputRoot? root, DragDropEffects effect)
105+
private void UpdateCursor(TopLevel? root, DragDropEffects effect)
107106
{
108107
if (_lastRoot != root)
109108
{
110-
if (_lastRoot is InputElement ieLast)
111-
{
112-
if (_originalCursor == AvaloniaProperty.UnsetValue)
113-
ieLast.ClearValue(InputElement.CursorProperty);
114-
else
115-
ieLast.Cursor = _originalCursor as Cursor;
116-
}
117-
118-
if (root is InputElement ieNew)
119-
{
120-
if (!ieNew.IsSet(InputElement.CursorProperty))
121-
_originalCursor = AvaloniaProperty.UnsetValue;
122-
else
123-
_originalCursor = root.Cursor;
124-
}
125-
else
126-
_originalCursor = null;
127-
128-
_lastCursorType = StandardCursorType.Arrow;
109+
_lastRoot?.SetCursorOverride(null);
129110
_lastRoot = root;
111+
_lastCursorType = null;
130112
}
131113

132-
if (root is InputElement ie)
114+
if (root != null)
133115
{
134116
var ct = GetCursorForDropEffect(effect);
135-
if (ct != _lastCursorType)
117+
if (_lastCursorType != ct)
136118
{
137119
_lastCursorType = ct;
138-
ie.Cursor = new Cursor(ct);
120+
root.SetCursorOverride(new Cursor(ct));
139121
}
140-
}
122+
}
141123
}
142124

143125
private void CancelDragging()

src/Avalonia.Controls/TopLevel.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ private static readonly WeakEvent<IResourceHost, ResourcesChangedEventArgs>
139139
private IStorageProvider? _storageProvider;
140140
private Screens? _screens;
141141
private LayoutDiagnosticBridge? _layoutDiagnosticBridge;
142+
private Cursor? _cursor;
143+
private Cursor? _cursorOverride;
142144

143145
/// <summary>
144146
/// Initializes static members of the <see cref="TopLevel"/> class.
@@ -179,7 +181,7 @@ static TopLevel()
179181

180182
if (e.NewValue is InputElement newInputElement)
181183
{
182-
topLevel.PlatformImpl?.SetCursor(newInputElement.Cursor?.PlatformImpl);
184+
topLevel.SetCursor(newInputElement.Cursor);
183185
newInputElement.PropertyChanged += topLevel.PointerOverElementOnPropertyChanged;
184186
}
185187
});
@@ -867,11 +869,28 @@ private void HandleInput(RawInputEventArgs e)
867869
return candidate;
868870
}
869871

872+
private void UpdateCursor() => PlatformImpl?.SetCursor(_cursorOverride?.PlatformImpl ?? _cursor?.PlatformImpl);
873+
874+
private void SetCursor(Cursor? cursor)
875+
{
876+
_cursor = cursor;
877+
UpdateCursor();
878+
}
879+
880+
/// <summary>
881+
/// This should only be used by InProcessDragSource
882+
/// </summary>
883+
internal void SetCursorOverride(Cursor? cursor)
884+
{
885+
_cursorOverride = cursor;
886+
UpdateCursor();
887+
}
888+
870889
private void PointerOverElementOnPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e)
871890
{
872891
if (e.Property == CursorProperty && sender is InputElement inputElement)
873892
{
874-
PlatformImpl?.SetCursor(inputElement.Cursor?.PlatformImpl);
893+
SetCursor(inputElement.Cursor);
875894
}
876895
}
877896

0 commit comments

Comments
 (0)