Forráskód Böngészése

Fixed all drag drops

flabbet 1 éve
szülő
commit
61005ae490

+ 0 - 149
src/PixiEditor/Helpers/UI/DragDropEvents.cs

@@ -1,149 +0,0 @@
-using Avalonia.Input;
-using Avalonia.Interactivity;
-
-namespace PixiEditor.Helpers.UI;
-
-public delegate void DragEventHandler(object sender, DragEventArgs e);
-
-public static class DragDropEvents
-{
-    public static readonly RoutedEvent<DragEventArgs> DragEnterEvent =
-        RoutedEvent.Register<DragEventArgs>(
-            "DragEnter",
-            RoutingStrategies.Bubble,
-            typeof(DragDropEvents));
-
-
-    public static readonly RoutedEvent<DragEventArgs> DragLeaveEvent =
-        RoutedEvent.Register<DragEventArgs>(
-            "DragLeave",
-            RoutingStrategies.Bubble,
-            typeof(DragDropEvents));
-
-
-    public static readonly RoutedEvent<RoutedEventArgs> DragOverEvent =
-        RoutedEvent.Register<RoutedEventArgs>(
-            "DragOver",
-            RoutingStrategies.Bubble,
-            typeof(DragDropEvents));
-
-
-    public static readonly RoutedEvent<DragEventArgs> DropEvent =
-        RoutedEvent.Register<DragEventArgs>(
-            "Drop",
-            RoutingStrategies.Bubble,
-            typeof(DragDropEvents));
-
-    public static void AddDragEnterHandler(Interactive control, EventHandler<DragEventArgs> handler)
-    {
-        var checkedHandler = WithCheck(control, handler);
-        control.AddHandler(
-            DragEnterEvent,
-            checkedHandler,
-            RoutingStrategies.Bubble);
-
-        control.AddHandler(
-            DragDrop.DragEnterEvent,
-            checkedHandler,
-            RoutingStrategies.Bubble);
-    }
-
-    public static void AddDragLeaveHandler(Interactive control, EventHandler<DragEventArgs> handler)
-    {
-        var checkedHandler = WithCheck(control, handler);
-
-        control.AddHandler(
-            DragLeaveEvent,
-            checkedHandler,
-            RoutingStrategies.Bubble);
-
-        control.AddHandler(
-            DragDrop.DragLeaveEvent,
-            checkedHandler,
-            RoutingStrategies.Bubble);
-    }
-
-    public static void AddDragOverHandler(Interactive control, EventHandler<DragEventArgs> handler)
-    {
-        var checkedHandler = WithCheck(control, handler);
-
-        control.AddHandler(
-            DragOverEvent,
-            checkedHandler,
-            RoutingStrategies.Bubble);
-
-        control.AddHandler(
-            DragDrop.DragOverEvent,
-            checkedHandler,
-            RoutingStrategies.Bubble);
-    }
-
-    public static void AddDropHandler(Interactive control, EventHandler<DragEventArgs> handler)
-    {
-        var checkedHandler = WithCheck(control, handler);
-        control.AddHandler(
-            DropEvent,
-            checkedHandler,
-            RoutingStrategies.Bubble);
-
-        control.AddHandler(
-            DragDrop.DropEvent,
-            checkedHandler,
-            RoutingStrategies.Bubble);
-    }
-
-    public static void RemoveDragEnterHandler(Interactive control, EventHandler<DragEventArgs> handler)
-    {
-        control.RemoveHandler(
-            DragEnterEvent,
-            handler);
-
-        control.RemoveHandler(
-            DragDrop.DragEnterEvent,
-            handler);
-    }
-
-    public static void RemoveDragLeaveHandler(Interactive control, EventHandler<DragEventArgs> handler)
-    {
-        control.RemoveHandler(
-            DragLeaveEvent,
-            handler);
-
-        control.RemoveHandler(
-            DragDrop.DragLeaveEvent,
-            handler);
-    }
-
-    public static void RemoveDragOverHandler(Interactive control, EventHandler<DragEventArgs> handler)
-    {
-        control.RemoveHandler(
-            DragOverEvent,
-            handler);
-
-        control.RemoveHandler(
-            DragDrop.DragOverEvent,
-            handler);
-    }
-
-    public static void RemoveDropHandler(Interactive control, EventHandler<DragEventArgs> handler)
-    {
-        control.RemoveHandler(
-            DropEvent,
-            handler);
-
-        control.RemoveHandler(
-            DragDrop.DropEvent,
-            handler);
-    }
-
-    private static EventHandler<T> WithCheck<T>(object source, EventHandler<T> handler) where T : RoutedEventArgs
-    {
-        return (sender, args) =>
-        {
-            if (source == sender)
-            {
-                handler(sender, args);
-            }
-        };
-    }
-}

+ 2 - 4
src/PixiEditor/Views/Layers/ReferenceLayer.axaml

@@ -23,10 +23,8 @@
         </Style>
     </UserControl.Styles>
     <Border BorderThickness="0 2 0 0" MinWidth="60"
-            Focusable="True" DragDrop.AllowDrop="True"
-            ui1:DragDropEvents.DragEnter="ReferenceLayer_DragEnter"
-            ui1:DragDropEvents.Drop="ReferenceLayer_Drop"
-            ui1:DragDropEvents.DragLeave="ReferenceLayer_DragLeave">
+            Name="DragBorder"
+            Focusable="True" DragDrop.AllowDrop="True">
         <Interaction.Behaviors>
             <behaviours:ClearFocusOnClickBehavior />
         </Interaction.Behaviors>

+ 4 - 0
src/PixiEditor/Views/Layers/ReferenceLayer.axaml.cs

@@ -32,6 +32,10 @@ internal partial class ReferenceLayer : UserControl
     {
         command = CommandController.Current.Commands["PixiEditor.Clipboard.PasteReferenceLayer"];
         InitializeComponent();
+        
+        DragBorder.AddHandler(DragDrop.DragEnterEvent, ReferenceLayer_DragEnter);
+        DragBorder.AddHandler(DragDrop.DragLeaveEvent, ReferenceLayer_DragLeave);
+        DragBorder.AddHandler(DragDrop.DropEvent, ReferenceLayer_Drop);
     }
     
     private static void OnDocumentChanged(AvaloniaPropertyChangedEventArgs<DocumentViewModel> e)

+ 1 - 2
src/PixiEditor/Views/MainView.axaml

@@ -19,8 +19,7 @@
             <InvokeCommandAction Command="{Binding StartupCommand}" />
         </EventTriggerBehavior>
     </Interaction.Behaviors>
-    <Grid DragDrop.AllowDrop="True" ui:DragDropEvents.DragEnter="MainView_DragEnter" ui:DragDropEvents.DragLeave="MainView_DragLeave"
-          ui:DragDropEvents.Drop="MainView_Drop">
+    <Grid DragDrop.AllowDrop="True" Name="DropGrid">
         <DockPanel>
             <main1:MainTitleBar DockPanel.Dock="Top" DataContext="{Binding MenuBarViewModel}"/>
             <Grid Focusable="True" Name="FocusableGrid">

+ 3 - 0
src/PixiEditor/Views/MainView.axaml.cs

@@ -16,6 +16,9 @@ public partial class MainView : UserControl
     {
         InitializeComponent();
         TextBoxFocusBehavior.FallbackFocusElement = FocusableGrid;
+        DropGrid.AddHandler(DragDrop.DragEnterEvent, MainView_DragEnter);
+        DropGrid.AddHandler(DragDrop.DragLeaveEvent, MainView_DragLeave);
+        DropGrid.AddHandler(DragDrop.DropEvent, MainView_Drop);
     }
     
     private void MainView_Drop(object sender, DragEventArgs e)

+ 1 - 1
src/PixiEditor/Views/Palettes/ColorReplacer.axaml

@@ -36,7 +36,7 @@
                                                Height="35"
                                                Width="35"
                                                ui:Translator.TooltipKey="REPLACER_TOOLTIP"
-                                               ui1:DragDropEvents.Drop="PaletteColorControl_OnDrop"
+                                               Name="DropTarget"
                                                DragDrop.AllowDrop="True" />
                     <TextBlock Name="arrow" VerticalAlignment="Center" Text="{DynamicResource icon-arrow-right}" Classes="pixi-icon" 
                                FontSize="24" Margin="10 0" ui:Translator.UseLanguageFlowDirection="True"/>

+ 6 - 5
src/PixiEditor/Views/Palettes/ColorReplacer.axaml.cs

@@ -65,6 +65,12 @@ internal partial class ColorReplacer : UserControl
         set => SetValue(IsCollapsedProperty, value);
     }
 
+    public ColorReplacer()
+    {
+        InitializeComponent();
+        DropTarget.AddHandler(DragDrop.DropEvent, PaletteColorControl_OnDrop);
+    }
+
     private void PaletteColorControl_OnDrop(object sender, DragEventArgs e)
     {
         if (e.Data.Contains(PaletteColorControl.PaletteColorDaoFormat))
@@ -79,11 +85,6 @@ internal partial class ColorReplacer : UserControl
         }
     }
 
-    public ColorReplacer()
-    {
-        InitializeComponent();
-    }
-
     private void ReplaceButton_OnClick(object sender, RoutedEventArgs e)
     {
         PaletteColor first = ColorToReplace;

+ 17 - 1
src/PixiEditor/Views/Palettes/PaletteColorControl.axaml.cs

@@ -1,4 +1,5 @@
-using Avalonia;
+using System.Windows.Input;
+using Avalonia;
 using Avalonia.Controls;
 using Avalonia.Input;
 using PixiEditor.Extensions.CommonApi.Palettes;
@@ -33,15 +34,30 @@ internal partial class PaletteColorControl : UserControl
         set { SetValue(CornerRadiusProperty, value); }
     }
 
+    public ICommand DropCommand
+    {
+        get { return (ICommand)GetValue(DropCommandProperty); }
+        set { SetValue(DropCommandProperty, value); }
+    }
+
 
     public static readonly StyledProperty<CornerRadius> CornerRadiusProperty =
         AvaloniaProperty.Register<PaletteColorControl, CornerRadius>(nameof(CornerRadius), new CornerRadius(5f));
 
     private Point clickPoint;
+    public static readonly StyledProperty<ICommand> DropCommandProperty = AvaloniaProperty.Register<PaletteColorControl, ICommand>("DropCommand");
 
     public PaletteColorControl()
     {
         InitializeComponent();
+        
+        this.AddHandler(DragDrop.DropEvent, PaletteColor_OnDrop);
+    }
+
+    private void PaletteColor_OnDrop(object? sender, DragEventArgs e)
+    {
+        e.Source = this;
+        DropCommand?.Execute(e);
     }
 
     private void PaletteColor_OnMouseMove(object? sender, PointerEventArgs e)

+ 5 - 5
src/PixiEditor/Views/Palettes/PaletteViewer.axaml

@@ -14,9 +14,8 @@
                                     IsVisible="{Binding !IsCompact, ElementName=paletteControl}"
                                     ReplaceColorsCommand="{Binding ElementName=paletteControl, Path=ReplaceColorsCommand}"
                                     HintColor="{Binding ElementName=paletteControl, Path=HintColor}" />
-            <Grid DragDrop.AllowDrop="True" ui1:DragDropEvents.DragEnter="Grid_PreviewDragEnter"
-                  ui1:DragDropEvents.DragLeave="Grid_PreviewDragLeave"
-                  ui1:DragDropEvents.Drop="Grid_Drop">
+            <Grid DragDrop.AllowDrop="True"
+                  Name="MainDropTarget">
                 <Grid.RowDefinitions>
                     <RowDefinition Height="35" />
                     <RowDefinition Height="5" />
@@ -72,7 +71,7 @@
                                                               ui:Translator.TooltipKey="PALETTE_COLOR_TOOLTIP"
                                                               DragDrop.AllowDrop="True" Color="{Binding}"
                                                               Margin="2.5"
-                                                              ui1:DragDropEvents.Drop="PaletteColor_Drop">
+                                                              DropCommand="{Binding ElementName=paletteControl, Path=DropColorCommand}">
                                     <palettes:PaletteColorControl.AssociatedKey>
                                         <MultiBinding Converter="{converters:IndexToAssociatedKeyConverter}">
                                             <Binding Path="." />
@@ -122,7 +121,8 @@
                                                           ui:Translator.TooltipKey="PALETTE_COLOR_TOOLTIP"
                                                           DragDrop.AllowDrop="True" Color="{Binding}"
                                                           Height="24" Width="24" CornerRadius="0"
-                                                          ui1:DragDropEvents.Drop="PaletteColor_Drop">
+                                                          DropCommand="{Binding ElementName=paletteControl, Path=DropColorCommand}">
+                                >
                                 <Interaction.Behaviors>
                                     <EventTriggerBehavior EventName="PointerReleased">
                                         <InvokeCommandAction

+ 11 - 2
src/PixiEditor/Views/Palettes/PaletteViewer.axaml.cs

@@ -10,6 +10,7 @@ using Avalonia.Input;
 using Avalonia.Interactivity;
 using Avalonia.Media;
 using Avalonia.Platform.Storage;
+using CommunityToolkit.Mvvm.Input;
 using PixiEditor.Extensions.Common.Localization;
 using PixiEditor.Helpers.Extensions;
 using PixiEditor.Extensions.CommonApi.Palettes;
@@ -112,11 +113,19 @@ internal partial class PaletteViewer : UserControl
         get => GetValue(IsCompactProperty);
         set => SetValue(IsCompactProperty, value);
     }
+    
+    public ICommand DropColorCommand { get; set; }
 
     public PaletteViewer()
     {
         InitializeComponent();
         SizeChanged += OnSizeChanged;
+        
+        MainDropTarget.AddHandler(DragDrop.DragEnterEvent, Grid_PreviewDragEnter);
+        MainDropTarget.AddHandler(DragDrop.DragLeaveEvent, Grid_PreviewDragLeave);
+        MainDropTarget.AddHandler(DragDrop.DropEvent, Grid_Drop);
+
+        DropColorCommand = new RelayCommand<DragEventArgs>(PaletteColor_Drop);
     }
 
     private void OnSizeChanged(object? sender, SizeChangedEventArgs e)
@@ -310,7 +319,7 @@ internal partial class PaletteViewer : UserControl
         return false;
     }
 
-    private void PaletteColor_Drop(object sender, DragEventArgs e)
+    private void PaletteColor_Drop( DragEventArgs e)
     {
         if (e.Data.Contains(PaletteColorControl.PaletteColorDaoFormat))
         {
@@ -319,7 +328,7 @@ internal partial class PaletteViewer : UserControl
             PaletteColor paletteColor = PaletteColor.Parse(data);
             if (Colors.Contains(paletteColor))
             {
-                PaletteColorControl paletteColorControl = sender as PaletteColorControl;
+                PaletteColorControl paletteColorControl = e.Source as PaletteColorControl;
                 int currIndex = Colors.IndexOf(paletteColor);
                 if (paletteColorControl != null)
                 {