ProfileSelectorDialog.axaml.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Input;
  4. using FluentAvalonia.UI.Controls;
  5. using Ryujinx.Ava.Common.Locale;
  6. using Ryujinx.Ava.UI.Controls;
  7. using Ryujinx.Ava.UI.Helpers;
  8. using Ryujinx.Ava.UI.ViewModels;
  9. using Ryujinx.Common.Logging;
  10. using Ryujinx.HLE.HOS.Services.Account.Acc;
  11. using System.Collections.ObjectModel;
  12. using System.ComponentModel;
  13. using System.Threading.Tasks;
  14. using UserProfile = Ryujinx.Ava.UI.Models.UserProfile;
  15. using UserProfileSft = Ryujinx.HLE.HOS.Services.Account.Acc.UserProfile;
  16. namespace Ryujinx.Ava.UI.Applet
  17. {
  18. public partial class ProfileSelectorDialog : UserControl
  19. {
  20. public ProfileSelectorDialogViewModel ViewModel { get; set; }
  21. public ProfileSelectorDialog(ProfileSelectorDialogViewModel viewModel)
  22. {
  23. DataContext = ViewModel = viewModel;
  24. InitializeComponent();
  25. }
  26. private void Grid_PointerEntered(object sender, PointerEventArgs e)
  27. {
  28. if (sender is Grid { DataContext: UserProfile profile })
  29. {
  30. profile.IsPointerOver = true;
  31. }
  32. }
  33. private void Grid_OnPointerExited(object sender, PointerEventArgs e)
  34. {
  35. if (sender is Grid { DataContext: UserProfile profile })
  36. {
  37. profile.IsPointerOver = false;
  38. }
  39. }
  40. private void ProfilesList_SelectionChanged(object sender, SelectionChangedEventArgs e)
  41. {
  42. if (sender is ListBox listBox)
  43. {
  44. int selectedIndex = listBox.SelectedIndex;
  45. if (selectedIndex >= 0 && selectedIndex < ViewModel.Profiles.Count)
  46. {
  47. if (ViewModel.Profiles[selectedIndex] is UserProfile userProfile)
  48. {
  49. ViewModel.SelectedUserId = userProfile.UserId;
  50. Logger.Info?.Print(LogClass.UI, $"Selected: {userProfile.UserId}", "ProfileSelector");
  51. ObservableCollection<BaseModel> newProfiles = [];
  52. foreach (BaseModel item in ViewModel.Profiles)
  53. {
  54. if (item is UserProfile originalItem)
  55. {
  56. UserProfileSft profile = new(originalItem.UserId, originalItem.Name, originalItem.Image);
  57. if (profile.UserId == ViewModel.SelectedUserId)
  58. {
  59. profile.AccountState = AccountState.Open;
  60. }
  61. newProfiles.Add(new UserProfile(profile, new NavigationDialogHost()));
  62. }
  63. }
  64. ViewModel.Profiles = newProfiles;
  65. }
  66. }
  67. }
  68. }
  69. public static async Task<(UserId Id, bool Result)> ShowInputDialog(ProfileSelectorDialogViewModel viewModel)
  70. {
  71. ContentDialog contentDialog = new()
  72. {
  73. Title = LocaleManager.Instance[LocaleKeys.UserProfileWindowTitle],
  74. PrimaryButtonText = LocaleManager.Instance[LocaleKeys.Continue],
  75. SecondaryButtonText = string.Empty,
  76. CloseButtonText = LocaleManager.Instance[LocaleKeys.Cancel],
  77. Content = new ProfileSelectorDialog(viewModel),
  78. Padding = new Thickness(0)
  79. };
  80. UserId result = UserId.Null;
  81. bool input = false;
  82. contentDialog.Closed += Handler;
  83. await ContentDialogHelper.ShowAsync(contentDialog);
  84. return (result, input);
  85. void Handler(ContentDialog sender, ContentDialogClosedEventArgs eventArgs)
  86. {
  87. if (eventArgs.Result == ContentDialogResult.Primary)
  88. {
  89. result = viewModel.SelectedUserId;
  90. input = true;
  91. }
  92. else
  93. {
  94. result = UserId.Null;
  95. input = false;
  96. }
  97. }
  98. }
  99. }
  100. }