6
6
using System . Linq ;
7
7
using System . Threading . Tasks ;
8
8
using System . Windows ;
9
+ using System . Windows . Controls ;
9
10
using System . Windows . Input ;
11
+ using System . Windows . Media ;
10
12
using System . Windows . Media . Imaging ;
11
13
using IWshRuntimeLibrary ;
12
14
using XIVLauncher . Accounts ;
@@ -22,6 +24,9 @@ public partial class AccountSwitcher : Window
22
24
{
23
25
private readonly AccountManager _accountManager ;
24
26
27
+ private System . Windows . Point startPoint ;
28
+ private ListViewItem draggedItem ;
29
+
25
30
public EventHandler < XivAccount > OnAccountSwitchedEventHandler ;
26
31
27
32
public AccountSwitcher ( AccountManager accountManager )
@@ -225,5 +230,79 @@ private void DontSavePassword_OnUnchecked(object sender, RoutedEventArgs e)
225
230
account . SavePassword = true ;
226
231
_accountManager . Save ( ) ;
227
232
}
233
+
234
+ private void AccountListView_OnPreviewMouseLeftButtonDown ( object sender , MouseButtonEventArgs e )
235
+ {
236
+ this . startPoint = e . GetPosition ( null ) ;
237
+ this . draggedItem = FindAncestor < ListViewItem > ( ( DependencyObject ) e . OriginalSource ) ;
238
+
239
+ if ( this . draggedItem == null )
240
+ return ;
241
+
242
+ this . draggedItem . IsSelected = true ;
243
+ }
244
+
245
+ private void AccountListView_OnPreviewMouseMove ( object sender , MouseEventArgs e )
246
+ {
247
+ var mousePos = e . GetPosition ( null ) ;
248
+ var diff = this . startPoint - mousePos ;
249
+
250
+ if ( sender is ListView listView &&
251
+ FindAncestor < ListViewItem > ( ( DependencyObject ) e . OriginalSource ) is ListViewItem listViewItem &&
252
+ listView . ItemContainerGenerator . ItemFromContainer ( listViewItem ) is AccountSwitcherEntry accountEntry &&
253
+ e . LeftButton == MouseButtonState . Pressed &&
254
+ ( this . draggedItem != null && ( Math . Abs ( diff . X ) > SystemParameters . MinimumHorizontalDragDistance || Math . Abs ( diff . Y ) > SystemParameters . MinimumVerticalDragDistance ) ) )
255
+ {
256
+ var data = new DataObject ( "AccountSwitcherEntry" , accountEntry ) ;
257
+ DragDrop . DoDragDrop ( listViewItem , data , DragDropEffects . Move ) ;
258
+ }
259
+ }
260
+
261
+ private void AccountListView_OnDrop ( object sender , DragEventArgs e )
262
+ {
263
+ if ( this . draggedItem == null )
264
+ return ;
265
+
266
+ var targetItem = FindAncestor < ListViewItem > ( ( DependencyObject ) e . OriginalSource ) ;
267
+
268
+ if ( targetItem == null )
269
+ return ;
270
+
271
+ var targetIndex = AccountListView . ItemContainerGenerator . IndexFromContainer ( targetItem ) ;
272
+ var draggedIndex = AccountListView . ItemContainerGenerator . IndexFromContainer ( this . draggedItem ) ;
273
+
274
+ if ( targetIndex < 0 || draggedIndex < 0 )
275
+ return ;
276
+
277
+ var accountEntries = AccountListView . ItemsSource as List < AccountSwitcherEntry > ;
278
+
279
+ if ( accountEntries == null )
280
+ return ;
281
+
282
+ var draggedEntry = accountEntries [ draggedIndex ] ;
283
+ accountEntries . RemoveAt ( draggedIndex ) ;
284
+ accountEntries . Insert ( targetIndex , draggedEntry ) ;
285
+
286
+ _accountManager . Accounts . Clear ( ) ;
287
+ foreach ( var accountEntry in accountEntries )
288
+ _accountManager . Accounts . Add ( accountEntry . Account ) ;
289
+
290
+ _accountManager . Save ( ) ;
291
+ RefreshEntries ( ) ;
292
+ }
293
+
294
+ private static T FindAncestor < T > ( DependencyObject current ) where T : DependencyObject
295
+ {
296
+ do
297
+ {
298
+ if ( current is T ancestor )
299
+ return ancestor ;
300
+
301
+ current = VisualTreeHelper . GetParent ( current ) ;
302
+ }
303
+ while ( current != null ) ;
304
+
305
+ return null ;
306
+ }
228
307
}
229
- }
308
+ }
0 commit comments