Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mac: Fix drag/drop changes #1787

Merged
merged 1 commit into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"type": "mono",
"request": "launch",
"preLaunchTask": "build-xammac2",
"program": "${workspaceFolder}/artifacts/test/${config:var.configuration}/net461/Eto.Test.XamMac2.app/Contents/MacOS/Eto.Test.XamMac2",
"program": "${workspaceFolder}/artifacts/test/${config:var.configuration}/net472/Eto.Test.XamMac2.app/Contents/MacOS/Eto.Test.XamMac2",
"useRuntime": false,
"args": [],
"console": "internalConsole",
Expand Down
30 changes: 30 additions & 0 deletions src/Eto.Mac/Forms/Controls/GridHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,36 @@ protected bool IsMouseDragging
get { return Widget.Properties.Get(GridHandler.IsMouseDragging_Key, false); }
set { Widget.Properties.Set(GridHandler.IsMouseDragging_Key, value, false); }
}

static readonly object DragPasteboard_Key = new object();

protected NSPasteboard DragPasteboard
{
get { return Widget.Properties.Get<NSPasteboard>(DragPasteboard_Key); }
set { Widget.Properties.Set(DragPasteboard_Key, value); }
}

internal GridDragInfo DragInfo { get; set; }

public override void DoDragDrop(DataObject data, DragEffects allowedAction, Image image, PointF origin)
{
if (DragPasteboard != null)
{
var handler = data.Handler as IDataObjectHandler;
handler?.Apply(DragPasteboard);
SetupDragPasteboard(DragPasteboard);
DragInfo = new GridDragInfo
{
AllowedOperation = allowedAction.ToNS(),
DragImage = image.ToNS(),
ImageOffset = origin
};
}
else
{
base.DoDragDrop(data, allowedAction, image, origin);
}
}
}
}

47 changes: 13 additions & 34 deletions src/Eto.Mac/Forms/Controls/GridViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,10 @@ public EtoTableView(GridViewHandler handler)
SetDraggingSourceOperationMask(NSDragOperation.All, false);
}

internal GridDragInfo DragInfo { get; set; }

[Export("draggingSession:sourceOperationMaskForDraggingContext:")]
public NSDragOperation DraggingSessionSourceOperationMask(NSDraggingSession session, IntPtr context)
{
return DragInfo?.AllowedOperation ?? NSDragOperation.None;
return Handler?.DragInfo?.AllowedOperation ?? NSDragOperation.None;
}

public override void Layout()
Expand All @@ -161,6 +159,13 @@ public override void Layout()
#if XAMMAC2
public override NSImage DragImageForRowsWithIndexestableColumnseventoffset(NSIndexSet dragRows, NSTableColumn[] tableColumns, NSEvent dragEvent, ref CGPoint dragImageOffset)
{
var dragInfo = Handler?.DragInfo;
var img = dragInfo?.DragImage;
if (img != null)
{
dragImageOffset = dragInfo.GetDragImageOffset();
return img;
}
return base.DragImageForRowsWithIndexestableColumnseventoffset(dragRows, tableColumns, dragEvent, ref dragImageOffset);
}
#else
Expand All @@ -170,10 +175,11 @@ public override NSImage DragImageForRowsWithIndexestableColumnseventoffset(NSInd
[Export("dragImageForRowsWithIndexes:tableColumns:event:offset:")]
public NSImage DragImageForRows(NSIndexSet dragRows, NSTableColumn[] tableColumns, NSEvent dragEvent, ref CGPoint dragImageOffset)
{
var img = DragInfo?.DragImage;
var dragInfo = Handler?.DragInfo;
var img = dragInfo?.DragImage;
if (img != null)
{
dragImageOffset = DragInfo.GetDragImageOffset();
dragImageOffset = dragInfo.GetDragImageOffset();
return img;
}

Expand Down Expand Up @@ -310,7 +316,7 @@ public override bool WriteRows(NSTableView tableView, NSIndexSet rowIndexes, NSP

if (h.IsMouseDragging)
{
h.Control.DragInfo = null;
h.DragInfo = null;
// give MouseMove event a chance to start the drag
h.DragPasteboard = pboard;

Expand Down Expand Up @@ -343,7 +349,7 @@ public override bool WriteRows(NSTableView tableView, NSIndexSet rowIndexes, NSP
h.Callback.OnMouseMove(h.Widget, args);
h.DragPasteboard = null;

return h.Control.DragInfo != null;
return h.DragInfo != null;
}

return false;
Expand Down Expand Up @@ -642,33 +648,6 @@ public object GetCellAt(PointF location, out int column, out int row)
return row >= 0 ? GetItem(row) : null;
}

static readonly object DragPasteboard_Key = new object();

NSPasteboard DragPasteboard
{
get { return Widget.Properties.Get<NSPasteboard>(DragPasteboard_Key); }
set { Widget.Properties.Set(DragPasteboard_Key, value); }
}

public override void DoDragDrop(DataObject data, DragEffects allowedAction, Image image, PointF origin)
{
if (DragPasteboard != null)
{
var handler = data.Handler as IDataObjectHandler;
handler?.Apply(DragPasteboard);
Control.DragInfo = new GridDragInfo
{
AllowedOperation = allowedAction.ToNS(),
DragImage = image.ToNS(),
ImageOffset = origin
};
}
else
{
base.DoDragDrop(data, allowedAction, image, origin);
}
}

public GridViewDragInfo GetDragInfo(DragEventArgs args) => args.ControlObject as GridViewDragInfo;

public override ContextMenu ContextMenu
Expand Down
45 changes: 9 additions & 36 deletions src/Eto.Mac/Forms/Controls/TreeGridViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ public override bool OutlineViewwriteItemstoPasteboard(NSOutlineView outlineView

if (h.IsMouseDragging)
{
h.Control.DragInfo = null;
h.DragInfo = null;
// give MouseMove event a chance to start the drag
h.DragPasteboard = pboard;

Expand Down Expand Up @@ -536,7 +536,7 @@ public override bool OutlineViewwriteItemstoPasteboard(NSOutlineView outlineView
var args = MacConversions.GetMouseEvent(h, NSApplication.SharedApplication.CurrentEvent, false);
h.Callback.OnMouseMove(h.Widget, args);
h.DragPasteboard = null;
return h.Control.DragInfo != null;
return h.DragInfo != null;
}

return false;
Expand All @@ -553,15 +553,14 @@ public TreeGridViewHandler Handler
set { WeakHandler = new WeakReference(value); }
}

internal GridDragInfo DragInfo { get; set; }

#if XAMMAC2
public override NSImage DragImageForRowsWithIndexestableColumnseventoffset(NSIndexSet dragRows, NSTableColumn[] tableColumns, NSEvent dragEvent, ref CGPoint dragImageOffset)
{
var img = DragInfo?.DragImage;
var dragInfo = Handler?.DragInfo;
var img = dragInfo?.DragImage;
if (img != null)
{
dragImageOffset = DragInfo.GetDragImageOffset();
dragImageOffset = dragInfo.GetDragImageOffset();
return img;
}
return base.DragImageForRowsWithIndexestableColumnseventoffset(dragRows, tableColumns, dragEvent, ref dragImageOffset);
Expand All @@ -572,10 +571,11 @@ public override NSImage DragImageForRowsWithIndexestableColumnseventoffset(NSInd
[Export("dragImageForRowsWithIndexes:tableColumns:event:offset:")]
public NSImage DragImageForRows(NSIndexSet dragRows, NSTableColumn[] tableColumns, NSEvent dragEvent, ref CGPoint dragImageOffset)
{
var img = DragInfo?.DragImage;
var dragInfo = Handler?.DragInfo;
var img = dragInfo?.DragImage;
if (img != null)
{
dragImageOffset = DragInfo.GetDragImageOffset();
dragImageOffset = dragInfo.GetDragImageOffset();
return img;
}

Expand All @@ -589,7 +589,7 @@ public NSImage DragImageForRows(NSIndexSet dragRows, NSTableColumn[] tableColumn
[Export("draggingSession:sourceOperationMaskForDraggingContext:")]
public NSDragOperation DraggingSessionSourceOperationMask(NSDraggingSession session, IntPtr context)
{
return DragInfo?.AllowedOperation ?? NSDragOperation.None;
return Handler?.DragInfo?.AllowedOperation ?? NSDragOperation.None;
}

public override void MouseDown(NSEvent theEvent)
Expand Down Expand Up @@ -1034,14 +1034,6 @@ public ITreeGridItem GetCellAt(PointF location, out int column)
public TreeGridViewDragInfo GetDragInfo(DragEventArgs args) => args.ControlObject as TreeGridViewDragInfo;


static readonly object DragPasteboard_Key = new object();

NSPasteboard DragPasteboard
{
get { return Widget.Properties.Get<NSPasteboard>(DragPasteboard_Key); }
set { Widget.Properties.Set(DragPasteboard_Key, value); }
}

static readonly object CustomSelectedItems_Key = new object();

IList<object> CustomSelectedItems
Expand All @@ -1050,25 +1042,6 @@ IList<object> CustomSelectedItems
set { Widget.Properties.Set(CustomSelectedItems_Key, value); }
}

public override void DoDragDrop(DataObject data, DragEffects allowedAction, Image image, PointF origin)
{
if (DragPasteboard != null)
{
var handler = data.Handler as IDataObjectHandler;
handler?.Apply(DragPasteboard);
Control.DragInfo = new GridDragInfo
{
AllowedOperation = allowedAction.ToNS(),
DragImage = image.ToNS(),
ImageOffset = origin
};
}
else
{
base.DoDragDrop(data, allowedAction, image, origin);
}
}

static IEnumerable<object> GetItems(NSArray items)
{
for (nnuint i = 0; i < items.Count; i++)
Expand Down
15 changes: 0 additions & 15 deletions src/Eto.Mac/Forms/DataObjectHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,6 @@ public abstract class DataObjectHandler<TWidget, TCallback> : WidgetHandler<NSPa
{
nint _changeCount;

static string TranslateType(string type)
{
// register custom types with a UTType.Item base type so it gets picked up by the drag/drop infrastructure
if (!UTType.IsDynamic(type) && !UTType.IsDeclared(type))
type = UTType.CreatePreferredIdentifier(UTType.TagClassNSPboardType, type, UTType.Item);
return type;
}

void ClearIfNeeded()
{
if (Control.ChangeCount != _changeCount)
Expand All @@ -88,7 +80,6 @@ void ClearIfNeeded()
public void SetData(byte[] value, string type)
{
ClearIfNeeded();
type = TranslateType(type);
Control.SetDataForType(NSData.FromArray(value), type);
}

Expand All @@ -108,7 +99,6 @@ public string Html
public void SetString(string value, string type)
{
ClearIfNeeded();
type = TranslateType(type);
Control.SetStringForType(value, type);
}

Expand Down Expand Up @@ -155,7 +145,6 @@ public Image Image

public byte[] GetData(string type)
{
type = TranslateType(type);
var availableType = Control.GetAvailableTypeFromArray(new string[] { type });

if (availableType != null)
Expand All @@ -172,7 +161,6 @@ public byte[] GetData(string type)

public string GetString(string type)
{
type = TranslateType(type);
return Control.GetStringForType(type);
}

Expand Down Expand Up @@ -242,13 +230,11 @@ public void Clear()

public bool Contains(string type)
{
type = TranslateType(type);
return Control.GetAvailableTypeFromArray(new[] { type }) != null;
}

public bool TryGetObject(string type, out object value)
{
type = TranslateType(type);
if (type == NSPasteboard.NSPasteboardTypeColor)
{
value = NSColor.FromPasteboard(Control)?.ToEto();
Expand All @@ -260,7 +246,6 @@ public bool TryGetObject(string type, out object value)

public bool TrySetObject(object value, string type)
{
type = TranslateType(type);
if (value is Color color && type == NSPasteboard.NSPasteboardTypeColor)
{
Control.WriteObjects(new[] { color.ToNSUI() });
Expand Down
41 changes: 30 additions & 11 deletions src/Eto.Mac/Forms/MacView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1216,19 +1216,35 @@ static void FireOnShown(Control control)

protected virtual void FireOnShown() => FireOnShown(Widget);

static readonly string[] s_dropTypes = { UTType.Item, UTType.Content };

public bool AllowDrop
{
get { return Widget.Properties.Get<bool>(MacView.AllowDrop_Key); }
get => Widget.Properties.Get<bool>(MacView.AllowDrop_Key);
set
{
Widget.Properties.Set(MacView.AllowDrop_Key, value);
if (value)
DragControl.RegisterForDraggedTypes(new string[] { UTType.Item });
else
DragControl.UnregisterDraggedTypes();
if (Widget.Properties.TrySet(MacView.AllowDrop_Key, value))
{
if (value)
DragControl.RegisterForDraggedTypes(s_dropTypes);
else
DragControl.UnregisterDraggedTypes();
}
}
}

static string s_etoDragItemType;
static string s_etoDragImageType;

protected void SetupDragPasteboard(NSPasteboard pasteboard)
{
// add a UTType.Item base type so dragging works regardless of what was put in the DataObject.
if (s_etoDragItemType == null)
s_etoDragItemType = UTType.CreatePreferredIdentifier(UTType.TagClassNSPboardType, "eto.dragitem", UTType.Item);

pasteboard.SetStringForType(string.Empty, s_etoDragItemType);
}

public virtual void DoDragDrop(DataObject data, DragEffects allowedAction, Image image, PointF origin)
{
var handler = data.Handler as IDataObjectHandler;
Expand All @@ -1239,11 +1255,12 @@ public virtual void DoDragDrop(DataObject data, DragEffects allowedAction, Image
if (image != null)
{
var pasteboardItem = new NSPasteboardItem();
// item needs to have data, but we don't want to supply a standard UTI
const string utdragimage = "eto.dragimage";
pasteboardItem.SetStringForType(string.Empty, utdragimage);
// custom types need to be registered when using an NSPasteboardItem..
ContainerControl.RegisterForDraggedTypes(new string[] { utdragimage });

// register custom UTI for the drag image
if (s_etoDragImageType == null)
s_etoDragImageType = UTType.CreatePreferredIdentifier(UTType.TagClassNSPboardType, "eto.dragimage", UTType.Image);

pasteboardItem.SetStringForType(string.Empty, s_etoDragImageType);
#if XAMMAC2
var draggingItem = new NSDraggingItem(pasteboardItem);
#else
Expand All @@ -1267,6 +1284,8 @@ public virtual void DoDragDrop(DataObject data, DragEffects allowedAction, Image
var session = ContainerControl.BeginDraggingSession(draggingItems, NSApplication.SharedApplication.CurrentEvent, source);
handler.Apply(session.DraggingPasteboard);

SetupDragPasteboard(session.DraggingPasteboard);

// TODO: block until drag is complete?
}

Expand Down
Loading