Skip to content

Commit 01bbb54

Browse files
authored
Merge pull request #4038 from AWAS666/dragdropwebsearch
Enable reordering of websearches with drap and drop
2 parents b297f3d + d7dd89d commit 01bbb54

File tree

2 files changed

+83
-2
lines changed

2 files changed

+83
-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: 79 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,78 @@ 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+
var listView = (ListView)sender;
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+
var listView = (ListView)sender;
203+
var target = GetNearestContainer(e.OriginalSource);
204+
205+
if (target == null)
206+
return;
207+
208+
SearchSource targetData = (SearchSource)listView.ItemContainerGenerator.ItemFromContainer(target);
209+
210+
if (targetData == null)
211+
return;
212+
213+
var items = _settings.SearchSources;
214+
int removedIdx = items.IndexOf(droppedData);
215+
int targetIdx = items.IndexOf(targetData);
216+
217+
if (removedIdx == targetIdx)
218+
return;
219+
220+
items.Move(removedIdx, targetIdx);
221+
}
222+
}
223+
224+
private ListViewItem GetNearestContainer(object source)
225+
{
226+
var element = source as UIElement;
227+
while (element != null && !(element is ListViewItem))
228+
element = VisualTreeHelper.GetParent(element) as UIElement;
229+
230+
return element as ListViewItem;
231+
}
232+
233+
private static T FindAncestor<T>(DependencyObject current) where T : DependencyObject
234+
{
235+
while (current != null)
236+
{
237+
if (current is T)
238+
return (T)current;
239+
current = VisualTreeHelper.GetParent(current);
240+
}
241+
return null;
242+
}
166243
}
167244
}

0 commit comments

Comments
 (0)