Skip to content

Commit b9a4314

Browse files
committed
enable reordering with drap and drop on websearches
1 parent 5ae159d commit b9a4314

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

Plugins/Flow.Launcher.Plugin.WebSearch/SettingsControl.xaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
MouseDoubleClick="MouseDoubleClickItem"
5353
SelectedItem="{Binding Settings.SelectedSearchSource}"
5454
SizeChanged="ListView_SizeChanged"
55+
PreviewMouseLeftButtonDown="ListView_PreviewMouseLeftButtonDown"
56+
PreviewMouseMove="ListView_PreviewMouseMove"
57+
AllowDrop="True"
58+
Drop="ListView_Drop"
5559
Style="{StaticResource {x:Static GridView.GridViewStyleKey}}">
5660
<ListView.View>
5761
<GridView>

Plugins/Flow.Launcher.Plugin.WebSearch/SettingsControl.xaml.cs

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
using System.Windows;
2-
using System.Windows.Controls;
1+
using System;
32
using System.ComponentModel;
3+
using System.Windows;
4+
using System.Windows.Controls;
45
using System.Windows.Data;
6+
using System.Windows.Input;
7+
using System.Windows.Media;
58

69
namespace Flow.Launcher.Plugin.WebSearch
710
{
@@ -12,6 +15,7 @@ public partial class SettingsControl : UserControl
1215
{
1316
private readonly Settings _settings;
1417
private readonly PluginInitContext _context;
18+
private Point _dragStartPoint;
1519

1620
public SettingsControl(PluginInitContext context, SettingsViewModel viewModel)
1721
{
@@ -163,5 +167,76 @@ private void ListView_SizeChanged(object sender, SizeChangedEventArgs e)
163167
gView.Columns[3].Width = workingWidth * col4;
164168
gView.Columns[4].Width = workingWidth * col5;
165169
}
170+
171+
private void ListView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
172+
{
173+
_dragStartPoint = e.GetPosition(null);
174+
}
175+
176+
private void ListView_PreviewMouseMove(object sender, MouseEventArgs e)
177+
{
178+
Point mousePos = e.GetPosition(null);
179+
Vector diff = _dragStartPoint - mousePos;
180+
181+
if (e.LeftButton == MouseButtonState.Pressed &&
182+
(Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
183+
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
184+
{
185+
ListView listView = sender as ListView;
186+
ListViewItem listViewItem = FindAncestor<ListViewItem>((DependencyObject)e.OriginalSource);
187+
188+
if (listViewItem == null) return;
189+
190+
SearchSource item = (SearchSource)listView.ItemContainerGenerator.ItemFromContainer(listViewItem);
191+
if (item == null) return;
192+
193+
DragDrop.DoDragDrop(listViewItem, item, DragDropEffects.Move);
194+
}
195+
}
196+
197+
private void ListView_Drop(object sender, DragEventArgs e)
198+
{
199+
if (e.Data.GetDataPresent(typeof(SearchSource)))
200+
{
201+
SearchSource droppedData = e.Data.GetData(typeof(SearchSource)) as SearchSource;
202+
ListView listView = sender as ListView;
203+
var target = GetNearestContainer(e.OriginalSource);
204+
205+
if (target == null)
206+
return;
207+
208+
SearchSource targetData = (SearchSource)listView.ItemContainerGenerator.ItemFromContainer(target);
209+
210+
var items = _settings.SearchSources;
211+
int removedIdx = items.IndexOf(droppedData);
212+
int targetIdx = items.IndexOf(targetData);
213+
214+
if (removedIdx == targetIdx)
215+
return;
216+
217+
items.RemoveAt(removedIdx);
218+
items.Insert(targetIdx, droppedData);
219+
}
220+
}
221+
222+
private ListViewItem GetNearestContainer(object source)
223+
{
224+
var element = source as UIElement;
225+
while (element != null && !(element is ListViewItem))
226+
element = VisualTreeHelper.GetParent(element) as UIElement;
227+
228+
return element as ListViewItem;
229+
}
230+
231+
private static T FindAncestor<T>(DependencyObject current) where T : DependencyObject
232+
{
233+
while (current != null)
234+
{
235+
if (current is T)
236+
return (T)current;
237+
current = VisualTreeHelper.GetParent(current);
238+
}
239+
return null;
240+
}
166241
}
167242
}

0 commit comments

Comments
 (0)