MainMenuBarView.axaml.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Interactivity;
  4. using Avalonia.Threading;
  5. using Gommon;
  6. using LibHac.Ncm;
  7. using LibHac.Tools.FsSystem.NcaUtils;
  8. using Ryujinx.Ava.Common.Locale;
  9. using Ryujinx.Ava.UI.Helpers;
  10. using Ryujinx.Ava.UI.ViewModels;
  11. using Ryujinx.Ava.UI.Windows;
  12. using Ryujinx.Common;
  13. using Ryujinx.Common.Utilities;
  14. using Ryujinx.UI.App.Common;
  15. using Ryujinx.UI.Common;
  16. using Ryujinx.UI.Common.Configuration;
  17. using Ryujinx.UI.Common.Helper;
  18. using System;
  19. using System.Collections.Generic;
  20. using System.IO;
  21. using System.Linq;
  22. namespace Ryujinx.Ava.UI.Views.Main
  23. {
  24. public partial class MainMenuBarView : UserControl
  25. {
  26. public MainWindow Window { get; private set; }
  27. public MainWindowViewModel ViewModel { get; private set; }
  28. public MainMenuBarView()
  29. {
  30. InitializeComponent();
  31. RyuLogo.IsVisible = !ConfigurationState.Instance.ShowTitleBar;
  32. ToggleFileTypesMenuItem.ItemsSource = GenerateToggleFileTypeItems();
  33. ChangeLanguageMenuItem.ItemsSource = GenerateLanguageMenuItems();
  34. }
  35. private CheckBox[] GenerateToggleFileTypeItems() =>
  36. Enum.GetValues<FileTypes>()
  37. .Select(it => (FileName: Enum.GetName(it)!, FileType: it))
  38. .Select(it =>
  39. new CheckBox
  40. {
  41. Content = $".{it.FileName}",
  42. IsChecked = it.FileType.GetConfigValue(ConfigurationState.Instance.UI.ShownFileTypes),
  43. Command = MiniCommand.Create(() => Window.ToggleFileType(it.FileName))
  44. }
  45. ).ToArray();
  46. private static MenuItem[] GenerateLanguageMenuItems()
  47. {
  48. List<MenuItem> menuItems = new();
  49. string localePath = "Ryujinx/Assets/Locales";
  50. string localeExt = ".json";
  51. string[] localesPath = EmbeddedResources.GetAllAvailableResources(localePath, localeExt);
  52. Array.Sort(localesPath);
  53. foreach (string locale in localesPath)
  54. {
  55. string languageCode = Path.GetFileNameWithoutExtension(locale).Split('.').Last();
  56. string languageJson = EmbeddedResources.ReadAllText($"{localePath}/{languageCode}{localeExt}");
  57. var strings = JsonHelper.Deserialize(languageJson, CommonJsonContext.Default.StringDictionary);
  58. if (!strings.TryGetValue("Language", out string languageName))
  59. {
  60. languageName = languageCode;
  61. }
  62. MenuItem menuItem = new()
  63. {
  64. Padding = new Thickness(10, 0, 0, 0),
  65. Header = " " + languageName,
  66. Command = MiniCommand.Create(() => MainWindowViewModel.ChangeLanguage(languageCode))
  67. };
  68. menuItems.Add(menuItem);
  69. }
  70. return menuItems.ToArray();
  71. }
  72. protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
  73. {
  74. base.OnAttachedToVisualTree(e);
  75. if (VisualRoot is MainWindow window)
  76. {
  77. Window = window;
  78. DataContext = ViewModel = window.ViewModel;
  79. }
  80. }
  81. private async void StopEmulation_Click(object sender, RoutedEventArgs e)
  82. {
  83. await ViewModel.AppHost?.ShowExitPrompt().OrCompleted()!;
  84. }
  85. private void PauseEmulation_Click(object sender, RoutedEventArgs e)
  86. {
  87. ViewModel.AppHost?.Pause();
  88. }
  89. private void ResumeEmulation_Click(object sender, RoutedEventArgs e)
  90. {
  91. ViewModel.AppHost?.Resume();
  92. }
  93. public async void OpenSettings(object sender, RoutedEventArgs e)
  94. {
  95. Window.SettingsWindow = new(Window.VirtualFileSystem, Window.ContentManager);
  96. await Window.SettingsWindow.ShowDialog(Window);
  97. Window.SettingsWindow = null;
  98. ViewModel.LoadConfigurableHotKeys();
  99. }
  100. public async void OpenMiiApplet(object sender, RoutedEventArgs e)
  101. {
  102. string contentPath = ViewModel.ContentManager.GetInstalledContentPath(0x0100000000001009, StorageId.BuiltInSystem, NcaContentType.Program);
  103. if (!string.IsNullOrEmpty(contentPath))
  104. {
  105. ApplicationData applicationData = new()
  106. {
  107. Name = "miiEdit",
  108. Id = 0x0100000000001009,
  109. Path = contentPath,
  110. };
  111. await ViewModel.LoadApplication(applicationData, ViewModel.IsFullScreen || ViewModel.StartGamesInFullscreen);
  112. }
  113. }
  114. public async void OpenAmiiboWindow(object sender, RoutedEventArgs e)
  115. => await ViewModel.OpenAmiiboWindow();
  116. public async void OpenCheatManagerForCurrentApp(object sender, RoutedEventArgs e)
  117. {
  118. if (!ViewModel.IsGameRunning)
  119. return;
  120. string name = ViewModel.AppHost.Device.Processes.ActiveApplication.ApplicationControlProperties.Title[(int)ViewModel.AppHost.Device.System.State.DesiredTitleLanguage].NameString.ToString();
  121. await new CheatWindow(
  122. Window.VirtualFileSystem,
  123. ViewModel.AppHost.Device.Processes.ActiveApplication.ProgramIdText,
  124. name,
  125. ViewModel.SelectedApplication.Path).ShowDialog(Window);
  126. ViewModel.AppHost.Device.EnableCheats();
  127. }
  128. private void ScanAmiiboMenuItem_AttachedToVisualTree(object sender, VisualTreeAttachmentEventArgs e)
  129. {
  130. if (sender is MenuItem)
  131. ViewModel.IsAmiiboRequested = ViewModel.AppHost.Device.System.SearchingForAmiibo(out _);
  132. }
  133. private async void InstallFileTypes_Click(object sender, RoutedEventArgs e)
  134. {
  135. if (FileAssociationHelper.Install())
  136. await ContentDialogHelper.CreateInfoDialog(LocaleManager.Instance[LocaleKeys.DialogInstallFileTypesSuccessMessage], string.Empty, LocaleManager.Instance[LocaleKeys.InputDialogOk], string.Empty, string.Empty);
  137. else
  138. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance[LocaleKeys.DialogInstallFileTypesErrorMessage]);
  139. }
  140. private async void UninstallFileTypes_Click(object sender, RoutedEventArgs e)
  141. {
  142. if (FileAssociationHelper.Uninstall())
  143. await ContentDialogHelper.CreateInfoDialog(LocaleManager.Instance[LocaleKeys.DialogUninstallFileTypesSuccessMessage], string.Empty, LocaleManager.Instance[LocaleKeys.InputDialogOk], string.Empty, string.Empty);
  144. else
  145. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance[LocaleKeys.DialogUninstallFileTypesErrorMessage]);
  146. }
  147. private async void ChangeWindowSize_Click(object sender, RoutedEventArgs e)
  148. {
  149. if (sender is not MenuItem { Tag: string resolution })
  150. return;
  151. (int height, int width) = resolution.Split(' ')
  152. .Into(parts => (int.Parse(parts[0]), int.Parse(parts[1])));
  153. await Dispatcher.UIThread.InvokeAsync(() =>
  154. {
  155. ViewModel.WindowState = WindowState.Normal;
  156. height += (int)Window.StatusBarHeight + (int)Window.MenuBarHeight;
  157. Window.Arrange(new Rect(Window.Position.X, Window.Position.Y, width, height));
  158. });
  159. }
  160. public async void CheckForUpdates(object sender, RoutedEventArgs e)
  161. {
  162. if (Updater.CanUpdate(true))
  163. await Updater.BeginParse(Window, true);
  164. }
  165. public async void OpenAboutWindow(object sender, RoutedEventArgs e) => await AboutWindow.Show();
  166. public void CloseWindow(object sender, RoutedEventArgs e) => Window.Close();
  167. }
  168. }