Browse Source

OSX do not enforce DND copy effect

Lubomir Tetak 4 years ago
parent
commit
3cc31f7912

+ 1 - 1
native/Avalonia.Native/src/OSX/dnd.mm

@@ -32,7 +32,7 @@ extern NSString* GetAvnCustomDataType()
 
 - (NSDragOperation)draggingSession:(nonnull NSDraggingSession *)session sourceOperationMaskForDraggingContext:(NSDraggingContext)context
 {
-    return NSDragOperationCopy;
+    return _operation;
 }
 
 - (AvnDndSource*) initWithOperation: (NSDragOperation)operation

+ 7 - 2
samples/ControlCatalog/Pages/DragAndDropPage.xaml

@@ -16,11 +16,16 @@
                 <Border BorderBrush="{DynamicResource SystemAccentColor}" BorderThickness="2" Padding="16" Name="DragMeCustom">
                   <TextBlock Name="DragStateCustom">Drag Me (custom)</TextBlock>
                 </Border>
+                <TextBlock Name="DropState"></TextBlock>
             </StackPanel>
 
             <Border Background="{DynamicResource SystemAccentColorDark1}" Padding="16" 
-                    DragDrop.AllowDrop="True">
-                <TextBlock Name="DropState">Drop some text or files here</TextBlock>
+                    DragDrop.AllowDrop="True" Name="CopyTarget">
+                <TextBlock>Drop some text or files here (Copy)</TextBlock>
+            </Border>
+            <Border Background="{DynamicResource SystemAccentColorDark1}" Padding="16" 
+                    DragDrop.AllowDrop="True" Name="MoveTarget">
+                <TextBlock>Drop some text or files here (Move)</TextBlock>
             </Border>
         </StackPanel>
     </StackPanel>

+ 27 - 6
samples/ControlCatalog/Pages/DragAndDropPage.xaml.cs

@@ -21,12 +21,12 @@ namespace ControlCatalog.Pages
 
             int textCount = 0;
             SetupDnd("Text", d => d.Set(DataFormats.Text,
-                $"Text was dragged {++textCount} times"));
+                $"Text was dragged {++textCount} times"), DragDropEffects.Copy | DragDropEffects.Move | DragDropEffects.Link);
 
-            SetupDnd("Custom", d => d.Set(CustomFormat, "Test123"));
+            SetupDnd("Custom", d => d.Set(CustomFormat, "Test123"), DragDropEffects.Move);
         }
 
-        void SetupDnd(string suffix, Action<DataObject> factory, DragDropEffects effects = DragDropEffects.Copy)
+        void SetupDnd(string suffix, Action<DataObject> factory, DragDropEffects effects)
         {
             var dragMe = this.Find<Border>("DragMe" + suffix);
             var dragState = this.Find<TextBlock>("DragState"+suffix);
@@ -36,9 +36,12 @@ namespace ControlCatalog.Pages
                 var dragData = new DataObject();
                 factory(dragData);
 
-                var result = await DragDrop.DoDragDrop(e, dragData, DragDropEffects.Copy);
+                var result = await DragDrop.DoDragDrop(e, dragData, effects);
                 switch (result)
                 {
+                    case DragDropEffects.Move:
+                        dragState.Text = "Data was moved";
+                        break;
                     case DragDropEffects.Copy:
                         dragState.Text = "Data was copied";
                         break;
@@ -48,13 +51,22 @@ namespace ControlCatalog.Pages
                     case DragDropEffects.None:
                         dragState.Text = "The drag operation was canceled";
                         break;
+                    default:
+                        dragState.Text = "Unknown result";
+                        break;
                 }
             }
 
             void DragOver(object sender, DragEventArgs e)
             {
-                // Only allow Copy or Link as Drop Operations.
-                e.DragEffects = e.DragEffects & (DragDropEffects.Copy | DragDropEffects.Link);
+                if (e.Source is Control c && c.Name == "MoveTarget")
+                {
+                    e.DragEffects = e.DragEffects & (DragDropEffects.Move);
+                }
+                else
+                {
+                    e.DragEffects = e.DragEffects & (DragDropEffects.Copy);
+                }
 
                 // Only allow if the dragged data contains text or filenames.
                 if (!e.Data.Contains(DataFormats.Text)
@@ -65,6 +77,15 @@ namespace ControlCatalog.Pages
 
             void Drop(object sender, DragEventArgs e)
             {
+                if (e.Source is Control c && c.Name == "MoveTarget")
+                {
+                    e.DragEffects = e.DragEffects & (DragDropEffects.Move);
+                }
+                else
+                {
+                    e.DragEffects = e.DragEffects & (DragDropEffects.Copy);
+                }
+                
                 if (e.Data.Contains(DataFormats.Text))
                     _DropState.Text = e.Data.GetText();
                 else if (e.Data.Contains(DataFormats.FileNames))