Skip to content

Commit

Permalink
Merge pull request #1502 from dbindley/master
Browse files Browse the repository at this point in the history
AccountSwitcher drag-and-drop reordering
  • Loading branch information
goaaats authored Jun 25, 2024
2 parents b14e0d3 + 7a2b9fb commit 6f388fb
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/XIVLauncher/Windows/AccountSwitcher.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@
<Grid Margin="0,0,0,0">
<materialDesign:Card Background="{DynamicResource MaterialDesignPaper}" Height="390" Margin="0,0,10,0">
<StackPanel Margin="16,16,10,0">
<ListView x:Name="AccountListView" MouseUp="AccountListView_OnMouseUp" MaxHeight="370">
<ListView x:Name="AccountListView"
MaxHeight="370"
AllowDrop="True"
MouseUp="AccountListView_OnMouseUp"
PreviewMouseLeftButtonDown="AccountListView_OnPreviewMouseLeftButtonDown"
PreviewMouseMove="AccountListView_OnPreviewMouseMove"
Drop="AccountListView_OnDrop">
<ListView.ContextMenu>
<ContextMenu Opened="AccountListViewContext_Opened" StaysOpen="true">
<MenuItem Header="{Binding AccountSwitcherSetProfilePicLoc}" Click="SetProfilePicture_OnClick" Foreground="{DynamicResource MaterialDesignBody}"/>
Expand Down Expand Up @@ -92,4 +98,4 @@
</StackPanel>
</materialDesign:Card>
</Grid>
</Window>
</Window>
81 changes: 80 additions & 1 deletion src/XIVLauncher/Windows/AccountSwitcher.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using IWshRuntimeLibrary;
using XIVLauncher.Accounts;
Expand All @@ -22,6 +24,9 @@ public partial class AccountSwitcher : Window
{
private readonly AccountManager _accountManager;

private System.Windows.Point startPoint;
private ListViewItem draggedItem;

public EventHandler<XivAccount> OnAccountSwitchedEventHandler;

public AccountSwitcher(AccountManager accountManager)
Expand Down Expand Up @@ -225,5 +230,79 @@ private void DontSavePassword_OnUnchecked(object sender, RoutedEventArgs e)
account.SavePassword = true;
_accountManager.Save();
}

private void AccountListView_OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.startPoint = e.GetPosition(null);
this.draggedItem = FindAncestor<ListViewItem>((DependencyObject)e.OriginalSource);

if (this.draggedItem == null)
return;

this.draggedItem.IsSelected = true;
}

private void AccountListView_OnPreviewMouseMove(object sender, MouseEventArgs e)
{
var mousePos = e.GetPosition(null);
var diff = this.startPoint - mousePos;

if (sender is ListView listView &&
FindAncestor<ListViewItem>((DependencyObject)e.OriginalSource) is ListViewItem listViewItem &&
listView.ItemContainerGenerator.ItemFromContainer(listViewItem) is AccountSwitcherEntry accountEntry &&
e.LeftButton == MouseButtonState.Pressed &&
(this.draggedItem != null && (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)))
{
var data = new DataObject("AccountSwitcherEntry", accountEntry);
DragDrop.DoDragDrop(listViewItem, data, DragDropEffects.Move);
}
}

private void AccountListView_OnDrop(object sender, DragEventArgs e)
{
if (this.draggedItem == null)
return;

var targetItem = FindAncestor<ListViewItem>((DependencyObject)e.OriginalSource);

if (targetItem == null)
return;

var targetIndex = AccountListView.ItemContainerGenerator.IndexFromContainer(targetItem);
var draggedIndex = AccountListView.ItemContainerGenerator.IndexFromContainer(this.draggedItem);

if (targetIndex < 0 || draggedIndex < 0)
return;

var accountEntries = AccountListView.ItemsSource as List<AccountSwitcherEntry>;

if (accountEntries == null)
return;

var draggedEntry = accountEntries[draggedIndex];
accountEntries.RemoveAt(draggedIndex);
accountEntries.Insert(targetIndex, draggedEntry);

_accountManager.Accounts.Clear();
foreach (var accountEntry in accountEntries)
_accountManager.Accounts.Add(accountEntry.Account);

_accountManager.Save();
RefreshEntries();
}

private static T FindAncestor<T>(DependencyObject current) where T : DependencyObject
{
do
{
if (current is T ancestor)
return ancestor;

current = VisualTreeHelper.GetParent(current);
}
while (current != null);

return null;
}
}
}
}

0 comments on commit 6f388fb

Please sign in to comment.