-
Notifications
You must be signed in to change notification settings - Fork 10
/
DragTabBehavior.cs
97 lines (75 loc) · 3.03 KB
/
DragTabBehavior.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// (c) Nick Polyak 2021 - http://awebpros.com/
// License: MIT License (https://opensource.org/licenses/MIT)
//
// short overview of copyright rules:
// 1. you can use this framework in any commercial or non-commercial
// product as long as you retain this copyright message
// 2. Do not blame the author of this software if something goes wrong.
//
// Also, please, mention this software in any documentation for the
// products that use it.
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.VisualTree;
using NP.Ava.Visuals;
using System;
using System.Collections.Generic;
using System.Linq;
namespace NP.Ava.UniDock
{
public class DragTabBehavior : DragItemBehavior<TabItem>
{
#region IsSet Attached Avalonia Property
public static bool GetIsSet(AvaloniaObject obj)
{
return obj.GetValue(IsSetProperty);
}
public static void SetIsSet(AvaloniaObject obj, bool value)
{
obj.SetValue(IsSetProperty, value);
}
public static readonly AttachedProperty<bool> IsSetProperty =
AvaloniaProperty.RegisterAttached<object, Control, bool>
(
"IsSet"
);
#endregion IsSet Attached Avalonia Property
private static DockItem? GetDockItem(TabItem tabItem) =>
tabItem.Content as DockItem;
private static IList<IDockGroup>? GetDockItemsCollection(DockItem dockItem) =>
(dockItem.DockParent as TabbedDockGroup)?.Items;
protected override bool MoveItemWithinContainer(Control itemsContainer, PointerEventArgs e)
{
Point pointerPositionWithinItemsContainer = e.GetPosition(itemsContainer);
DockItem? draggedDockItem = _draggedDockGroup as DockItem;
IList<IDockGroup> itemsList = GetDockItemsCollection(draggedDockItem!)!;
if (itemsContainer.IsPointWithinControl(pointerPositionWithinItemsContainer))
{
var siblingTabs =
itemsContainer.GetVisualDescendants().OfType<TabItem>().ToList();
TabItem? tabMouseOver =
siblingTabs?.FirstOrDefault(tab => tab.IsPointerWithinControl(e));
if (tabMouseOver != null && tabMouseOver != _startItem)
{
int draggedDockItemIdx = itemsList.IndexOf(_draggedDockGroup!);
IDockGroup dropDockItem = _dockGroupGetter(tabMouseOver);
int dropIdx = itemsList!.IndexOf(dropDockItem);
itemsList?.Remove(_draggedDockGroup!);
itemsList?.Insert(dropIdx, _draggedDockGroup!);
draggedDockItem!.IsSelected = true;
}
return true;
}
return false;
}
public DragTabBehavior() : base(GetDockItem!)
{
}
static DragTabBehavior()
{
Instance = new DragTabBehavior();
IsSetProperty.Changed.Subscribe(OnIsSetChanged);
}
}
}