11using System ;
2+ using System . Collections ;
23using System . Collections . Generic ;
34using System . Linq ;
45using System . Threading . Tasks ;
1011using Avalonia . Interactivity ;
1112using Avalonia . LogicalTree ;
1213using Avalonia . Media ;
14+ using Avalonia . Threading ;
1315using Avalonia . VisualTree ;
1416using WDE . Common . Utils . DragDrop ;
1517using DragDropEffects = Avalonia . Input . DragDropEffects ;
@@ -289,7 +291,8 @@ private static void OnTreeViewDragOver(object? sender, DragEventArgs e)
289291 if ( dropHandler == null )
290292 return ;
291293
292- var parent = FindVisualParent < TreeView , TreeViewItem > ( dropElement ) ;
294+ var parent = dropElement == null ? treeView : FindVisualParent < TreeView , TreeViewItem > ( dropElement ) ;
295+
293296 ITreeItemContainerGenerator treeItemContainerGenerator ;
294297 if ( parent is TreeView tv )
295298 treeItemContainerGenerator = tv . ItemContainerGenerator ;
@@ -298,13 +301,16 @@ private static void OnTreeViewDragOver(object? sender, DragEventArgs e)
298301 else
299302 return ;
300303
301- adorner . AddAdorner ( parent ) ;
304+ adorner . AddAdorner ( treeView ) ; // parent
302305 var indexOfDrop = treeItemContainerGenerator . IndexFromContainer ( dropElement ) ;
303306 RelativeInsertPosition insertPosition = RelativeInsertPosition . None ;
304307
305308 if ( dropElement != null )
306309 {
307- var rel = e . GetPosition ( dropElement ) . Y / dropElement . Bounds . Height ;
310+ var header = dropElement . GetVisualChildren ( ) . FirstOrDefault ( ) . GetVisualChildren ( ) . FirstOrDefault ( ) ;
311+ var height = header ? . Bounds . Height ?? dropElement . Bounds . Height ;
312+
313+ var rel = e . GetPosition ( dropElement ) . Y / height ;
308314 if ( rel < 0.5f )
309315 insertPosition = RelativeInsertPosition . BeforeTargetItem ;
310316 else
@@ -316,11 +322,20 @@ private static void OnTreeViewDragOver(object? sender, DragEventArgs e)
316322 else
317323 indexOfDrop = treeView . ItemCount ;
318324
319- var dropInfo = new DropInfo ( dragInfo . Value . draggedElement [ 0 ] ! )
325+ if ( insertPosition . HasFlag ( RelativeInsertPosition . AfterTargetItem ) &&
326+ ( dropElement ? . IsExpanded ?? false ) &&
327+ dropElement . ItemCount > 0 )
328+ {
329+ indexOfDrop = 0 ;
330+ insertPosition = RelativeInsertPosition . BeforeTargetItem ;
331+ dropElement = ( TreeViewItem ) dropElement . ItemContainerGenerator . ContainerFromIndex ( 0 ) ;
332+ }
333+
334+ dropInfo = new DropInfo ( dragInfo . Value . draggedElement [ 0 ] ! )
320335 {
321336 InsertIndex = indexOfDrop ,
322337 InsertPosition = insertPosition ,
323- TargetItem = treeView . Items
338+ TargetItem = ( ( IControl ? ) dropElement ) ? . DataContext
324339 } ;
325340 dropHandler . DragOver ( dropInfo ) ;
326341
@@ -336,41 +351,48 @@ private static void OnTreeViewDragOver(object? sender, DragEventArgs e)
336351 }
337352 }*/
338353
354+ Console . WriteLine ( "Over " + dropElement + " (" + dropElement ? . DataContext ? . ToString ( ) + ")" ) ;
355+ Console . WriteLine ( "Best parent: " + parent + " (" + ( ( IControl ? ) parent ) ? . DataContext ? . ToString ( ) + ")" ) ;
356+ Console . WriteLine ( "Index: " + indexOfDrop + " insert position " + insertPosition . ToString ( ) ) ;
357+
358+ if ( dropInfo . DropTargetAdorner == DropTargetAdorners . Insert )
359+ dropInfo . InsertPosition =
360+ ( RelativeInsertPosition ) ( ( int ) dropInfo . InsertPosition &
361+ ~ ( int ) RelativeInsertPosition . TargetItemCenter ) ;
339362 adorner . Adorner ? . Update ( treeView , treeItemContainerGenerator , dropInfo ) ;
340363 }
364+
365+ private static IDropInfo ? dropInfo ;
341366
342367 private static void OnTreeViewDrop ( object ? sender , DragEventArgs e )
343368 {
344- var listBox = sender as ListBox ;
345- var dropElement = FindVisualParent < ListBoxItem > ( e . Source as IVisual ) ;
369+ var treeView = sender as TreeView ;
370+ var dropElement = FindVisualParent < TreeViewItem > ( e . Source as IVisual ) ;
346371 var dragInfo = e . Data . Get ( "" ) as DragInfo ? ;
347372 if ( dragInfo == null || dragInfo . Value . draggedElement . Count == 0 )
348373 return ;
349374
350- if ( listBox == null )
375+ if ( treeView == null )
351376 return ;
352377
353- var dropHandler = GetDropHandler ( listBox ) ;
378+ var dropHandler = GetDropHandler ( treeView ) ;
354379 if ( dropHandler == null )
355380 return ;
356381
357- adorner . RemoveAdorner ( listBox ) ;
382+ adorner . RemoveAdorner ( treeView ) ;
358383
359- var indexOfDrop = listBox . ItemContainerGenerator . IndexFromContainer ( dropElement ) ;
360- if ( dropElement != null )
361- {
362- var pos = e . GetPosition ( dropElement ) ;
363- if ( pos . Y > dropElement . Bounds . Height / 2 )
364- indexOfDrop ++ ;
365- }
366- else
367- indexOfDrop = listBox . ItemCount ;
384+ var scrollViewer = treeView . FindDescendantOfType < ScrollViewer > ( ) ;
385+ var previousOffset = scrollViewer ? . Offset ;
368386
369- dropHandler . Drop ( new DropInfo ( dragInfo . Value . draggedElement [ 0 ] ! )
387+ if ( dropInfo != null )
388+ dropHandler . Drop ( dropInfo ) ;
389+ dropInfo = null ;
390+
391+ Dispatcher . UIThread . Post ( ( ) =>
370392 {
371- InsertIndex = indexOfDrop ,
372- TargetItem = listBox . Items
373- } ) ;
393+ if ( previousOffset . HasValue )
394+ scrollViewer ! . Offset = previousOffset . Value ;
395+ } , DispatcherPriority . Render ) ;
374396 }
375397
376398
@@ -477,15 +499,33 @@ public void Update(TreeView treeView, ITreeItemContainerGenerator itemContainerG
477499 }
478500 else
479501 {
502+ double y = 0 ;
503+ IVisual parent = container . VisualParent ;
504+ while ( parent != null && parent != treeView )
505+ {
506+ y += parent . Bounds . Y ;
507+ parent = parent . VisualParent ;
508+ }
509+
510+ var header = container . GetVisualChildren ( ) . FirstOrDefault ( ) . GetVisualChildren ( ) . FirstOrDefault ( ) ;
511+ var height = header ? . Bounds . Height ?? container . Bounds . Height ;
512+
513+ double top = container . TranslatePoint ( new Point ( 0 , 0 ) , treeView ) ? . Y ?? 0 ;
514+ double bottom = container . TranslatePoint ( new Point ( 0 , height ) , treeView ) ? . Y ?? 0 ;
515+
480516 if ( dropInfo . InsertPosition . HasFlag ( RelativeInsertPosition . TargetItemCenter )
481517 && dropInfo . DropTargetAdorner == DropTargetAdorners . Highlight )
482- drawRect = new Rect ( container . Bounds . X + 1 , container . Bounds . Y , container . Bounds . Width - 2 , container . Bounds . Height ) ;
518+ drawRect = new Rect ( container . Bounds . X + 1 , top , container . Bounds . Width - 2 , container . Bounds . Height ) ;
483519 else if ( dropInfo . InsertPosition . HasFlag ( RelativeInsertPosition . BeforeTargetItem ) )
484- drawRect = new Rect ( container . Bounds . X , container . Bounds . Top , container . Bounds . Width , 1 ) ;
520+ drawRect = new Rect ( container . Bounds . X , top , container . Bounds . Width , 1 ) ;
485521 else
486- drawRect = new Rect ( container . Bounds . X , container . Bounds . Bottom , container . Bounds . Width , 1 ) ;
522+ drawRect = new Rect ( container . Bounds . X , bottom , container . Bounds . Width , 1 ) ;
487523 }
488524
525+ /*var scroll = treeView.FindDescendantOfType<ScrollViewer>();
526+ if (scroll != null)
527+ drawRect = new Rect(drawRect.X, drawRect.Y - scroll.Offset.Y, drawRect.Width, drawRect.Height);
528+ */
489529 InvalidateVisual ( ) ;
490530 }
491531
0 commit comments