-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConnectorControl.xaml.cs
More file actions
103 lines (82 loc) · 2.82 KB
/
Copy pathConnectorControl.xaml.cs
File metadata and controls
103 lines (82 loc) · 2.82 KB
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
98
99
100
101
102
103
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
namespace ShortDev.NodeFlow.WinUI;
public sealed partial class ConnectorControl : UserControl
{
public ConnectorControl()
{
InitializeComponent();
Loaded += OnLoaded;
Unloaded += OnUnloaded;
}
public bool IsOutgoing { get; set; } = false;
public bool AllowMultipleConnections { get; set; } = false;
public Point ConnectorPosition
{
get => (Point)GetValue(ConnectorPositionProperty);
set => SetValue(ConnectorPositionProperty, value);
}
public static DependencyProperty ConnectorPositionProperty { get; } = DependencyProperty.Register(
nameof(ConnectorPosition),
typeof(Point),
typeof(ConnectorControl),
new PropertyMetadata(new Point(0, 0))
);
public event EventHandler<(object? source, object? target)>? Connected;
private void OnDragStarting(UIElement sender, DragStartingEventArgs args)
{
if (!IsOutgoing || _parent is null)
{
args.Cancel = true;
return;
}
args.AllowedOperations = DataPackageOperation.Link;
args.Data.Properties.Add("AttachedNode", _parent);
args.Data.Properties.Add("Connector", this);
}
private void OnDragOver(object sender, DragEventArgs e)
{
e.AcceptedOperation = IsOutgoing switch
{
true => DataPackageOperation.None,
false => DataPackageOperation.Link
};
}
private void OnDrop(object sender, DragEventArgs e)
{
if (!IsOutgoing && e.DataView != null && e.DataView.Properties != null)
{
// ToDo: Do we allow multiple connections?
var remoteConnector = (ConnectorControl)e.DataView.Properties["Connector"];
Connected?.Invoke(this, (remoteConnector.DataContext, DataContext));
}
e.AcceptedOperation = DataPackageOperation.None;
}
NodeControl? _parent;
private void OnLoaded(object sender, RoutedEventArgs e)
{
_parent = this.FindAscendant<NodeControl>();
if (_parent is null)
return;
_parent.PositionChanged += OnPositionChanged;
}
private void OnPositionChanged(object? sender, EventArgs e)
{
if (_parent is null)
return;
if (VisualTreeHelper.GetParent(_parent) is not UIElement parent)
return;
ConnectorPosition = TransformToVisual(parent)
.TransformPoint(new Point(ActualWidth / 2, ActualHeight / 2));
}
private void OnUnloaded(object sender, RoutedEventArgs e)
{
if (_parent is null)
return;
_parent.PositionChanged -= OnPositionChanged;
_parent = null;
}
}