This repository was archived by the owner on Aug 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDragAndDropOverlay.iOS.cs
More file actions
134 lines (112 loc) · 4.95 KB
/
DragAndDropOverlay.iOS.cs
File metadata and controls
134 lines (112 loc) · 4.95 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using CoreGraphics;
using Foundation;
using ObjCRuntime;
using UIKit;
namespace DrasticOverlay.Overlays
{
public partial class DragAndDropOverlay
{
DragAndDropView dragAndDropView;
public override bool Initialize()
{
if (dragAndDropOverlayNativeElementsInitialized)
return true;
base.Initialize();
var nativeLayer = Window?.GetNative(true);
if (nativeLayer is not UIWindow nativeWindow)
return false;
if (nativeWindow?.RootViewController?.View == null)
return false;
// We're going to create a new view.
// This will handle the "drop" events, and nothing else.
dragAndDropView = new DragAndDropView(this, nativeWindow.RootViewController.View.Frame);
dragAndDropView.UserInteractionEnabled = true;
nativeWindow?.RootViewController.View.AddSubview(dragAndDropView);
nativeWindow?.RootViewController.View.BringSubviewToFront(dragAndDropView);
return dragAndDropOverlayNativeElementsInitialized = true;
}
public override bool Deinitialize()
{
if (this.dragAndDropView != null)
{
this.dragAndDropView.RemoveFromSuperview();
this.dragAndDropView.Dispose();
}
return base.Deinitialize();
}
class DragAndDropView : UIView, IUIDropInteractionDelegate
{
DragAndDropOverlay overlay;
public DragAndDropView(DragAndDropOverlay overlay, CGRect frame)
: base(frame)
{
this.overlay = overlay;
this.AddInteraction(new UIDropInteraction(this));
}
// The following are all of the sessions we can handle.
[Export("dropInteraction:canHandleSession:")]
public bool CanHandleSession(UIDropInteraction interaction, IUIDropSession session)
{
Console.WriteLine($"CanHandleSession ({interaction}, {session})");
return session.CanLoadObjects(new Class(typeof(UIImage)));
}
[Export("dropInteraction:sessionDidEnter:")]
public void SessionDidEnter(UIDropInteraction interaction, IUIDropSession session)
{
Console.WriteLine($"SessionDidEnter ({interaction}, {session})");
this.overlay.IsDragging = true;
}
[Export("dropInteraction:sessionDidExit:")]
public void SessionDidExit(UIDropInteraction interaction, IUIDropSession session)
{
Console.WriteLine($"SessionDidExit ({interaction}, {session})");
this.overlay.IsDragging = false;
}
[Export("dropInteraction:sessionDidUpdate:")]
public UIDropProposal SessionDidUpdate(UIDropInteraction interaction, IUIDropSession session)
{
Console.WriteLine($"sessionDidUpdate ({interaction}, {session})");
if (session.LocalDragSession == null && session.CanLoadObjects(typeof(UIImage)))
{
return new UIDropProposal(UIDropOperation.Copy);
}
return new UIDropProposal(UIDropOperation.Cancel);
}
[Export("dropInteraction:performDrop:")]
public void PerformDrop(UIDropInteraction interaction, IUIDropSession session)
{
Console.WriteLine($"performDrop ({interaction}, {session})");
session.ProgressIndicatorStyle = UIDropSessionProgressIndicatorStyle.None;
foreach (UIDragItem item in session.Items)
{
if (item.ItemProvider.CanLoadObject(typeof(UIImage)))
{
item.ItemProvider.LoadObject<UIImage>((img, err) => {
using (NSData imageData = img.AsPNG())
{
Byte[] barray = new Byte[imageData.Length];
System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, barray, 0, Convert.ToInt32(imageData.Length));
this.overlay.Drop?.Invoke(this.overlay, new DragAndDropOverlayTappedEventArgs("test", barray));
}
});
}
}
}
[Export("dropInteraction:concludeDrop:")]
public void ConcludeDrop(UIDropInteraction interaction, IUIDropSession session)
{
Console.WriteLine($"concludeDrop ({interaction}, {session})");
this.overlay.IsDragging = false;
}
public override bool PointInside(CGPoint point, UIEvent? uievent)
{
if (uievent != null && (long)uievent.Type == 9)
{
return true;
}
return false;
}
}
}
}