UserSelectorDialog.axaml.cs 4.3 KB

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