1- using System . Windows ;
2- using System . Windows . Controls ;
1+ using System ;
32using System . ComponentModel ;
3+ using System . Windows ;
4+ using System . Windows . Controls ;
45using System . Windows . Data ;
6+ using System . Windows . Input ;
7+ using System . Windows . Media ;
58
69namespace 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