diff --git a/Application/FileConverter/FileConverter.csproj b/Application/FileConverter/FileConverter.csproj index aa27337..01038e7 100644 --- a/Application/FileConverter/FileConverter.csproj +++ b/Application/FileConverter/FileConverter.csproj @@ -229,6 +229,7 @@ + HelpWindow.xaml diff --git a/Application/FileConverter/Views/BindableSelectedItemBehavior.cs b/Application/FileConverter/Views/BindableSelectedItemBehavior.cs index 19dc6fc..8204034 100644 --- a/Application/FileConverter/Views/BindableSelectedItemBehavior.cs +++ b/Application/FileConverter/Views/BindableSelectedItemBehavior.cs @@ -196,4 +196,4 @@ namespace FileConverter.Views this.UpdateTreeViewItem((TreeViewItem)sender, false); } } -} \ No newline at end of file +} diff --git a/Application/FileConverter/Views/DragDropExtension.cs b/Application/FileConverter/Views/DragDropExtension.cs new file mode 100644 index 0000000..793764e --- /dev/null +++ b/Application/FileConverter/Views/DragDropExtension.cs @@ -0,0 +1,118 @@ +// License: http://www.gnu.org/licenses/gpl.html GPL version 3. + +using System; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Media; + +namespace FileConverter.Views +{ + /// + /// Provides extended support for drag drop operation + /// + + public static class DragDropExtension + { + public static readonly DependencyProperty ScrollOnDragDropProperty = DependencyProperty.RegisterAttached("ScrollOnDragDrop", typeof(bool), typeof(DragDropExtension), new PropertyMetadata(false, HandleScrollOnDragDropChanged)); + + public static bool GetScrollOnDragDrop(DependencyObject element) + { + if (element == null) + { + throw new ArgumentNullException(nameof(element)); + } + + return (bool)element.GetValue(ScrollOnDragDropProperty); + } + + public static void SetScrollOnDragDrop(DependencyObject element, bool value) + { + if (element == null) + { + throw new ArgumentNullException(nameof(element)); + } + + element.SetValue(ScrollOnDragDropProperty, value); + } + + private static void HandleScrollOnDragDropChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) + { + FrameworkElement container = dependencyObject as FrameworkElement; + + if (dependencyObject == null) + { + Diagnostics.Debug.LogError("Invalid types!"); + return; + } + + Unsubscribe(container); + + if (true.Equals(args.NewValue)) + { + Subscribe(container); + } + } + + private static void Subscribe(FrameworkElement container) + { + container.PreviewDragOver += OnContainerPreviewDragOver; + } + + private static void OnContainerPreviewDragOver(object sender, DragEventArgs args) + { + const double Tolerance = 60; + const double Offset = 20; + + FrameworkElement container = sender as FrameworkElement; + if (container == null) + { + return; + } + + ScrollViewer scrollViewer = GetFirstVisualChild(container); + if (scrollViewer == null) + { + return; + } + + double verticalPos = args.GetPosition(container).Y; + + if (verticalPos < Tolerance) // Top of visible list? + { + scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - Offset); //Scroll up. + } + else if (verticalPos > container.ActualHeight - Tolerance) // Bottom of visible list? + { + scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + Offset); //Scroll down. + } + } + + private static void Unsubscribe(FrameworkElement container) + { + container.PreviewDragOver -= OnContainerPreviewDragOver; + } + + public static T GetFirstVisualChild(DependencyObject dependencyObject) where T : DependencyObject + { + if (dependencyObject != null) + { + for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject); i++) + { + DependencyObject child = VisualTreeHelper.GetChild(dependencyObject, i); + if (child is T visualChild) + { + return visualChild; + } + + T childItem = GetFirstVisualChild(child); + if (childItem != null) + { + return childItem; + } + } + } + + return null; + } + } +} diff --git a/Application/FileConverter/Views/SettingsWindow.xaml b/Application/FileConverter/Views/SettingsWindow.xaml index 372d321..a053ac5 100644 --- a/Application/FileConverter/Views/SettingsWindow.xaml +++ b/Application/FileConverter/Views/SettingsWindow.xaml @@ -85,7 +85,8 @@ + Width="{Binding ActualWidth, ElementName=PresetListGrid, Mode=OneWay}" AllowDrop="True" + views:DragDropExtension.ScrollOnDragDrop="True">