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,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