MainWindow.axaml.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Input;
  4. using Avalonia.Interactivity;
  5. using Avalonia.Media;
  6. using Avalonia.Threading;
  7. using FluentAvalonia.UI.Controls;
  8. using Ryujinx.Ava.Common;
  9. using Ryujinx.Ava.Common.Locale;
  10. using Ryujinx.Ava.Input;
  11. using Ryujinx.Ava.UI.Applet;
  12. using Ryujinx.Ava.UI.Controls;
  13. using Ryujinx.Ava.UI.Helpers;
  14. using Ryujinx.Ava.UI.Models;
  15. using Ryujinx.Ava.UI.ViewModels;
  16. using Ryujinx.Common.Configuration;
  17. using Ryujinx.Common.Logging;
  18. using Ryujinx.Graphics.Gpu;
  19. using Ryujinx.HLE.FileSystem;
  20. using Ryujinx.HLE.HOS;
  21. using Ryujinx.HLE.HOS.Services.Account.Acc;
  22. using Ryujinx.Input.SDL2;
  23. using Ryujinx.Modules;
  24. using Ryujinx.Ui.App.Common;
  25. using Ryujinx.Ui.Common;
  26. using Ryujinx.Ui.Common.Configuration;
  27. using Ryujinx.Ui.Common.Helper;
  28. using SixLabors.ImageSharp.PixelFormats;
  29. using System;
  30. using System.ComponentModel;
  31. using System.IO;
  32. using System.Threading;
  33. using System.Threading.Tasks;
  34. using InputManager = Ryujinx.Input.HLE.InputManager;
  35. namespace Ryujinx.Ava.UI.Windows
  36. {
  37. public partial class MainWindow : StyleableWindow
  38. {
  39. internal static MainWindowViewModel MainWindowViewModel { get; private set; }
  40. private bool _canUpdate;
  41. private bool _isClosing;
  42. private bool _isLoading;
  43. private Control _mainViewContent;
  44. private UserChannelPersistence _userChannelPersistence;
  45. private static bool _deferLoad;
  46. private static string _launchPath;
  47. private static bool _startFullscreen;
  48. private string _currentEmulatedGamePath;
  49. internal readonly AvaHostUiHandler UiHandler;
  50. private AutoResetEvent _rendererWaitEvent;
  51. public VirtualFileSystem VirtualFileSystem { get; private set; }
  52. public ContentManager ContentManager { get; private set; }
  53. public AccountManager AccountManager { get; private set; }
  54. public LibHacHorizonManager LibHacHorizonManager { get; private set; }
  55. internal AppHost AppHost { get; private set; }
  56. public InputManager InputManager { get; private set; }
  57. internal RendererHost RendererControl { get; private set; }
  58. internal MainWindowViewModel ViewModel { get; private set; }
  59. public SettingsWindow SettingsWindow { get; set; }
  60. public bool CanUpdate
  61. {
  62. get => _canUpdate;
  63. set
  64. {
  65. _canUpdate = value;
  66. Dispatcher.UIThread.InvokeAsync(() => UpdateMenuItem.IsEnabled = _canUpdate);
  67. }
  68. }
  69. public static bool ShowKeyErrorOnLoad { get; set; }
  70. public ApplicationLibrary ApplicationLibrary { get; set; }
  71. public MainWindow()
  72. {
  73. ViewModel = new MainWindowViewModel(this);
  74. MainWindowViewModel = ViewModel;
  75. DataContext = ViewModel;
  76. InitializeComponent();
  77. Load();
  78. UiHandler = new AvaHostUiHandler(this);
  79. Title = $"Ryujinx {Program.Version}";
  80. // NOTE: Height of MenuBar and StatusBar is not usable here, since it would still be 0 at this point.
  81. double barHeight = MenuBar.MinHeight + StatusBar.MinHeight;
  82. Height = ((Height - barHeight) / Program.WindowScaleFactor) + barHeight;
  83. Width /= Program.WindowScaleFactor;
  84. if (Program.PreviewerDetached)
  85. {
  86. Initialize();
  87. ViewModel.Initialize();
  88. InputManager = new InputManager(new AvaloniaKeyboardDriver(this), new SDL2GamepadDriver());
  89. LoadGameList();
  90. }
  91. _rendererWaitEvent = new AutoResetEvent(false);
  92. }
  93. public void LoadGameList()
  94. {
  95. if (_isLoading)
  96. {
  97. return;
  98. }
  99. _isLoading = true;
  100. ViewModel.LoadApplications();
  101. _isLoading = false;
  102. }
  103. private void Update_StatusBar(object sender, StatusUpdatedEventArgs args)
  104. {
  105. if (ViewModel.ShowMenuAndStatusBar && !ViewModel.ShowLoadProgress)
  106. {
  107. Dispatcher.UIThread.InvokeAsync(() =>
  108. {
  109. if (args.VSyncEnabled)
  110. {
  111. ViewModel.VsyncColor = new SolidColorBrush(Color.Parse("#ff2eeac9"));
  112. }
  113. else
  114. {
  115. ViewModel.VsyncColor = new SolidColorBrush(Color.Parse("#ffff4554"));
  116. }
  117. ViewModel.DockedStatusText = args.DockedMode;
  118. ViewModel.AspectRatioStatusText = args.AspectRatio;
  119. ViewModel.GameStatusText = args.GameStatus;
  120. ViewModel.VolumeStatusText = args.VolumeStatus;
  121. ViewModel.FifoStatusText = args.FifoStatus;
  122. ViewModel.GpuNameText = args.GpuName;
  123. ViewModel.BackendText = args.GpuBackend;
  124. ViewModel.ShowStatusSeparator = true;
  125. });
  126. }
  127. }
  128. protected override void HandleScalingChanged(double scale)
  129. {
  130. Program.DesktopScaleFactor = scale;
  131. base.HandleScalingChanged(scale);
  132. }
  133. public void Application_Opened(object sender, ApplicationOpenedEventArgs args)
  134. {
  135. if (args.Application != null)
  136. {
  137. ViewModel.SelectedIcon = args.Application.Icon;
  138. string path = new FileInfo(args.Application.Path).FullName;
  139. LoadApplication(path);
  140. }
  141. args.Handled = true;
  142. }
  143. public async Task PerformanceCheck()
  144. {
  145. if (ConfigurationState.Instance.Logger.EnableTrace.Value)
  146. {
  147. string mainMessage = LocaleManager.Instance[LocaleKeys.DialogPerformanceCheckLoggingEnabledMessage];
  148. string secondaryMessage = LocaleManager.Instance[LocaleKeys.DialogPerformanceCheckLoggingEnabledConfirmMessage];
  149. UserResult result = await ContentDialogHelper.CreateConfirmationDialog(mainMessage, secondaryMessage,
  150. LocaleManager.Instance[LocaleKeys.InputDialogYes], LocaleManager.Instance[LocaleKeys.InputDialogNo],
  151. LocaleManager.Instance[LocaleKeys.RyujinxConfirm]);
  152. if (result != UserResult.Yes)
  153. {
  154. ConfigurationState.Instance.Logger.EnableTrace.Value = false;
  155. SaveConfig();
  156. }
  157. }
  158. if (!string.IsNullOrWhiteSpace(ConfigurationState.Instance.Graphics.ShadersDumpPath.Value))
  159. {
  160. string mainMessage = LocaleManager.Instance[LocaleKeys.DialogPerformanceCheckShaderDumpEnabledMessage];
  161. string secondaryMessage =
  162. LocaleManager.Instance[LocaleKeys.DialogPerformanceCheckShaderDumpEnabledConfirmMessage];
  163. UserResult result = await ContentDialogHelper.CreateConfirmationDialog(mainMessage, secondaryMessage,
  164. LocaleManager.Instance[LocaleKeys.InputDialogYes], LocaleManager.Instance[LocaleKeys.InputDialogNo],
  165. LocaleManager.Instance[LocaleKeys.RyujinxConfirm]);
  166. if (result != UserResult.Yes)
  167. {
  168. ConfigurationState.Instance.Graphics.ShadersDumpPath.Value = "";
  169. SaveConfig();
  170. }
  171. }
  172. }
  173. internal static void DeferLoadApplication(string launchPathArg, bool startFullscreenArg)
  174. {
  175. _deferLoad = true;
  176. _launchPath = launchPathArg;
  177. _startFullscreen = startFullscreenArg;
  178. }
  179. #pragma warning disable CS1998
  180. public async void LoadApplication(string path, bool startFullscreen = false, string titleName = "")
  181. #pragma warning restore CS1998
  182. {
  183. if (AppHost != null)
  184. {
  185. await ContentDialogHelper.CreateInfoDialog(
  186. LocaleManager.Instance[LocaleKeys.DialogLoadAppGameAlreadyLoadedMessage],
  187. LocaleManager.Instance[LocaleKeys.DialogLoadAppGameAlreadyLoadedSubMessage],
  188. LocaleManager.Instance[LocaleKeys.InputDialogOk],
  189. "",
  190. LocaleManager.Instance[LocaleKeys.RyujinxInfo]);
  191. return;
  192. }
  193. #if RELEASE
  194. await PerformanceCheck();
  195. #endif
  196. Logger.RestartTime();
  197. if (ViewModel.SelectedIcon == null)
  198. {
  199. ViewModel.SelectedIcon = ApplicationLibrary.GetApplicationIcon(path);
  200. }
  201. PrepareLoadScreen();
  202. _mainViewContent = MainContent.Content as Control;
  203. RendererControl = new RendererHost(ConfigurationState.Instance.Logger.GraphicsDebugLevel);
  204. if (ConfigurationState.Instance.Graphics.GraphicsBackend.Value == GraphicsBackend.OpenGl)
  205. {
  206. RendererControl.CreateOpenGL();
  207. }
  208. else
  209. {
  210. RendererControl.CreateVulkan();
  211. }
  212. AppHost = new AppHost(RendererControl, InputManager, path, VirtualFileSystem, ContentManager, AccountManager, _userChannelPersistence, this);
  213. Dispatcher.UIThread.Post(async () =>
  214. {
  215. if (!await AppHost.LoadGuestApplication())
  216. {
  217. AppHost.DisposeContext();
  218. AppHost = null;
  219. return;
  220. }
  221. CanUpdate = false;
  222. ViewModel.LoadHeading = string.IsNullOrWhiteSpace(titleName) ? string.Format(LocaleManager.Instance[LocaleKeys.LoadingHeading], AppHost.Device.Application.TitleName) : titleName;
  223. ViewModel.TitleName = string.IsNullOrWhiteSpace(titleName) ? AppHost.Device.Application.TitleName : titleName;
  224. SwitchToGameControl(startFullscreen);
  225. _currentEmulatedGamePath = path;
  226. Thread gameThread = new(InitializeGame)
  227. {
  228. Name = "GUI.WindowThread"
  229. };
  230. gameThread.Start();
  231. });
  232. }
  233. private void InitializeGame()
  234. {
  235. RendererControl.RendererInitialized += GlRenderer_Created;
  236. AppHost.StatusUpdatedEvent += Update_StatusBar;
  237. AppHost.AppExit += AppHost_AppExit;
  238. _rendererWaitEvent.WaitOne();
  239. AppHost?.Start();
  240. AppHost.DisposeContext();
  241. }
  242. private void HandleRelaunch()
  243. {
  244. if (_userChannelPersistence.PreviousIndex != -1 && _userChannelPersistence.ShouldRestart)
  245. {
  246. _userChannelPersistence.ShouldRestart = false;
  247. Dispatcher.UIThread.Post(() =>
  248. {
  249. LoadApplication(_currentEmulatedGamePath);
  250. });
  251. }
  252. else
  253. {
  254. // otherwise, clear state.
  255. _userChannelPersistence = new UserChannelPersistence();
  256. _currentEmulatedGamePath = null;
  257. }
  258. }
  259. public void SwitchToGameControl(bool startFullscreen = false)
  260. {
  261. ViewModel.ShowLoadProgress = false;
  262. ViewModel.ShowContent = true;
  263. ViewModel.IsLoadingIndeterminate = false;
  264. Dispatcher.UIThread.InvokeAsync(() =>
  265. {
  266. MainContent.Content = RendererControl;
  267. if (startFullscreen && WindowState != WindowState.FullScreen)
  268. {
  269. ViewModel.ToggleFullscreen();
  270. }
  271. RendererControl.Focus();
  272. });
  273. }
  274. public void ShowLoading(bool startFullscreen = false)
  275. {
  276. ViewModel.ShowContent = false;
  277. ViewModel.ShowLoadProgress = true;
  278. ViewModel.IsLoadingIndeterminate = true;
  279. Dispatcher.UIThread.InvokeAsync(() =>
  280. {
  281. if (startFullscreen && WindowState != WindowState.FullScreen)
  282. {
  283. ViewModel.ToggleFullscreen();
  284. }
  285. });
  286. }
  287. private void GlRenderer_Created(object sender, EventArgs e)
  288. {
  289. ShowLoading();
  290. _rendererWaitEvent.Set();
  291. }
  292. private void AppHost_AppExit(object sender, EventArgs e)
  293. {
  294. if (_isClosing)
  295. {
  296. return;
  297. }
  298. ViewModel.IsGameRunning = false;
  299. Dispatcher.UIThread.InvokeAsync(() =>
  300. {
  301. ViewModel.ShowMenuAndStatusBar = true;
  302. ViewModel.ShowContent = true;
  303. ViewModel.ShowLoadProgress = false;
  304. ViewModel.IsLoadingIndeterminate = false;
  305. CanUpdate = true;
  306. Cursor = Cursor.Default;
  307. if (MainContent.Content != _mainViewContent)
  308. {
  309. MainContent.Content = _mainViewContent;
  310. }
  311. AppHost = null;
  312. HandleRelaunch();
  313. });
  314. RendererControl.RendererInitialized -= GlRenderer_Created;
  315. RendererControl = null;
  316. ViewModel.SelectedIcon = null;
  317. Dispatcher.UIThread.InvokeAsync(() =>
  318. {
  319. Title = $"Ryujinx {Program.Version}";
  320. });
  321. }
  322. public void Sort_Checked(object sender, RoutedEventArgs args)
  323. {
  324. if (sender is RadioButton button)
  325. {
  326. var sort = Enum.Parse<ApplicationSort>(button.Tag.ToString());
  327. ViewModel.Sort(sort);
  328. }
  329. }
  330. protected override void HandleWindowStateChanged(WindowState state)
  331. {
  332. WindowState = state;
  333. if (state != WindowState.Minimized)
  334. {
  335. Renderer.Start();
  336. }
  337. }
  338. public void Order_Checked(object sender, RoutedEventArgs args)
  339. {
  340. if (sender is RadioButton button)
  341. {
  342. var tag = button.Tag.ToString();
  343. ViewModel.Sort(tag != "Descending");
  344. }
  345. }
  346. private void Initialize()
  347. {
  348. _userChannelPersistence = new UserChannelPersistence();
  349. VirtualFileSystem = VirtualFileSystem.CreateInstance();
  350. LibHacHorizonManager = new LibHacHorizonManager();
  351. ContentManager = new ContentManager(VirtualFileSystem);
  352. LibHacHorizonManager.InitializeFsServer(VirtualFileSystem);
  353. LibHacHorizonManager.InitializeArpServer();
  354. LibHacHorizonManager.InitializeBcatServer();
  355. LibHacHorizonManager.InitializeSystemClients();
  356. ApplicationLibrary = new ApplicationLibrary(VirtualFileSystem);
  357. // Save data created before we supported extra data in directory save data will not work properly if
  358. // given empty extra data. Luckily some of that extra data can be created using the data from the
  359. // save data indexer, which should be enough to check access permissions for user saves.
  360. // Every single save data's extra data will be checked and fixed if needed each time the emulator is opened.
  361. // Consider removing this at some point in the future when we don't need to worry about old saves.
  362. VirtualFileSystem.FixExtraData(LibHacHorizonManager.RyujinxClient);
  363. AccountManager = new AccountManager(LibHacHorizonManager.RyujinxClient, CommandLineState.Profile);
  364. VirtualFileSystem.ReloadKeySet();
  365. ApplicationHelper.Initialize(VirtualFileSystem, AccountManager, LibHacHorizonManager.RyujinxClient, this);
  366. RefreshFirmwareStatus();
  367. }
  368. protected void CheckLaunchState()
  369. {
  370. if (ShowKeyErrorOnLoad)
  371. {
  372. ShowKeyErrorOnLoad = false;
  373. Dispatcher.UIThread.Post(async () => await
  374. UserErrorDialog.ShowUserErrorDialog(UserError.NoKeys, this));
  375. }
  376. if (_deferLoad)
  377. {
  378. _deferLoad = false;
  379. LoadApplication(_launchPath, _startFullscreen);
  380. }
  381. if (ConfigurationState.Instance.CheckUpdatesOnStart.Value && Updater.CanUpdate(false, this))
  382. {
  383. Updater.BeginParse(this, false).ContinueWith(task =>
  384. {
  385. Logger.Error?.Print(LogClass.Application, $"Updater Error: {task.Exception}");
  386. }, TaskContinuationOptions.OnlyOnFaulted);
  387. }
  388. }
  389. public void RefreshFirmwareStatus()
  390. {
  391. SystemVersion version = null;
  392. try
  393. {
  394. version = ContentManager.GetCurrentFirmwareVersion();
  395. }
  396. catch (Exception) { }
  397. bool hasApplet = false;
  398. if (version != null)
  399. {
  400. LocaleManager.Instance.UpdateDynamicValue(LocaleKeys.StatusBarSystemVersion,
  401. version.VersionString);
  402. hasApplet = version.Major > 3;
  403. }
  404. else
  405. {
  406. LocaleManager.Instance.UpdateDynamicValue(LocaleKeys.StatusBarSystemVersion, "0.0");
  407. }
  408. ViewModel.IsAppletMenuActive = hasApplet;
  409. }
  410. private void Load()
  411. {
  412. VolumeStatus.Click += VolumeStatus_CheckedChanged;
  413. GameGrid.ApplicationOpened += Application_Opened;
  414. GameGrid.DataContext = ViewModel;
  415. GameList.ApplicationOpened += Application_Opened;
  416. GameList.DataContext = ViewModel;
  417. LoadHotKeys();
  418. }
  419. protected override void OnOpened(EventArgs e)
  420. {
  421. base.OnOpened(e);
  422. CheckLaunchState();
  423. }
  424. public static void UpdateGraphicsConfig()
  425. {
  426. GraphicsConfig.ResScale = ConfigurationState.Instance.Graphics.ResScale == -1 ? ConfigurationState.Instance.Graphics.ResScaleCustom : ConfigurationState.Instance.Graphics.ResScale;
  427. GraphicsConfig.MaxAnisotropy = ConfigurationState.Instance.Graphics.MaxAnisotropy;
  428. GraphicsConfig.ShadersDumpPath = ConfigurationState.Instance.Graphics.ShadersDumpPath;
  429. GraphicsConfig.EnableShaderCache = ConfigurationState.Instance.Graphics.EnableShaderCache;
  430. GraphicsConfig.EnableTextureRecompression = ConfigurationState.Instance.Graphics.EnableTextureRecompression;
  431. GraphicsConfig.EnableMacroHLE = ConfigurationState.Instance.Graphics.EnableMacroHLE;
  432. }
  433. public void LoadHotKeys()
  434. {
  435. HotKeyManager.SetHotKey(FullscreenHotKey, new KeyGesture(Key.Enter, KeyModifiers.Alt));
  436. HotKeyManager.SetHotKey(FullscreenHotKey2, new KeyGesture(Key.F11));
  437. HotKeyManager.SetHotKey(DockToggleHotKey, new KeyGesture(Key.F9));
  438. HotKeyManager.SetHotKey(ExitHotKey, new KeyGesture(Key.Escape));
  439. }
  440. public static void SaveConfig()
  441. {
  442. ConfigurationState.Instance.ToFileFormat().SaveConfig(Program.ConfigurationPath);
  443. }
  444. public void UpdateGameMetadata(string titleId)
  445. {
  446. ApplicationLibrary.LoadAndSaveMetaData(titleId, appMetadata =>
  447. {
  448. if (DateTime.TryParse(appMetadata.LastPlayed, out DateTime lastPlayedDateTime))
  449. {
  450. double sessionTimePlayed = DateTime.UtcNow.Subtract(lastPlayedDateTime).TotalSeconds;
  451. appMetadata.TimePlayed += Math.Round(sessionTimePlayed, MidpointRounding.AwayFromZero);
  452. }
  453. });
  454. }
  455. private void PrepareLoadScreen()
  456. {
  457. using MemoryStream stream = new MemoryStream(ViewModel.SelectedIcon);
  458. using var gameIconBmp = SixLabors.ImageSharp.Image.Load<Bgra32>(stream);
  459. var dominantColor = IconColorPicker.GetFilteredColor(gameIconBmp).ToPixel<Bgra32>();
  460. const int ColorDivisor = 4;
  461. Color progressFgColor = Color.FromRgb(dominantColor.R, dominantColor.G, dominantColor.B);
  462. Color progressBgColor = Color.FromRgb(
  463. (byte)(dominantColor.R / ColorDivisor),
  464. (byte)(dominantColor.G / ColorDivisor),
  465. (byte)(dominantColor.B / ColorDivisor));
  466. ViewModel.ProgressBarForegroundColor = new SolidColorBrush(progressFgColor);
  467. ViewModel.ProgressBarBackgroundColor = new SolidColorBrush(progressBgColor);
  468. }
  469. private void SearchBox_OnKeyUp(object sender, KeyEventArgs e)
  470. {
  471. ViewModel.SearchText = SearchBox.Text;
  472. }
  473. private async void StopEmulation_Click(object sender, RoutedEventArgs e)
  474. {
  475. if (AppHost != null)
  476. {
  477. await AppHost.ShowExitPrompt();
  478. }
  479. }
  480. private async void PauseEmulation_Click(object sender, RoutedEventArgs e)
  481. {
  482. await Task.Run(() =>
  483. {
  484. AppHost?.Pause();
  485. });
  486. }
  487. private async void ResumeEmulation_Click(object sender, RoutedEventArgs e)
  488. {
  489. await Task.Run(() =>
  490. {
  491. AppHost?.Resume();
  492. });
  493. }
  494. private void ScanAmiiboMenuItem_AttachedToVisualTree(object sender, VisualTreeAttachmentEventArgs e)
  495. {
  496. if (sender is MenuItem)
  497. {
  498. ViewModel.IsAmiiboRequested = AppHost.Device.System.SearchingForAmiibo(out _);
  499. }
  500. }
  501. private void VsyncStatus_PointerReleased(object sender, PointerReleasedEventArgs e)
  502. {
  503. AppHost.Device.EnableDeviceVsync = !AppHost.Device.EnableDeviceVsync;
  504. Logger.Info?.Print(LogClass.Application, $"VSync toggled to: {AppHost.Device.EnableDeviceVsync}");
  505. }
  506. private void DockedStatus_PointerReleased(object sender, PointerReleasedEventArgs e)
  507. {
  508. ConfigurationState.Instance.System.EnableDockedMode.Value = !ConfigurationState.Instance.System.EnableDockedMode.Value;
  509. }
  510. private void AspectRatioStatus_PointerReleased(object sender, PointerReleasedEventArgs e)
  511. {
  512. AspectRatio aspectRatio = ConfigurationState.Instance.Graphics.AspectRatio.Value;
  513. ConfigurationState.Instance.Graphics.AspectRatio.Value = (int)aspectRatio + 1 > Enum.GetNames(typeof(AspectRatio)).Length - 1 ? AspectRatio.Fixed4x3 : aspectRatio + 1;
  514. }
  515. private void VolumeStatus_CheckedChanged(object sender, SplitButtonClickEventArgs e)
  516. {
  517. var volumeSplitButton = sender as ToggleSplitButton;
  518. if (ViewModel.IsGameRunning)
  519. {
  520. if (!volumeSplitButton.IsChecked)
  521. {
  522. AppHost.Device.SetVolume(ConfigurationState.Instance.System.AudioVolume);
  523. }
  524. else
  525. {
  526. AppHost.Device.SetVolume(0);
  527. }
  528. ViewModel.Volume = AppHost.Device.GetVolume();
  529. }
  530. }
  531. protected override void OnClosing(CancelEventArgs e)
  532. {
  533. if (!_isClosing && AppHost != null && ConfigurationState.Instance.ShowConfirmExit)
  534. {
  535. e.Cancel = true;
  536. ConfirmExit();
  537. return;
  538. }
  539. _isClosing = true;
  540. if (AppHost != null)
  541. {
  542. AppHost.AppExit -= AppHost_AppExit;
  543. AppHost.AppExit += (sender, e) =>
  544. {
  545. AppHost = null;
  546. Dispatcher.UIThread.Post(() =>
  547. {
  548. MainContent = null;
  549. Close();
  550. });
  551. };
  552. AppHost?.Stop();
  553. e.Cancel = true;
  554. return;
  555. }
  556. ApplicationLibrary.CancelLoading();
  557. InputManager.Dispose();
  558. Program.Exit();
  559. base.OnClosing(e);
  560. }
  561. private void ConfirmExit()
  562. {
  563. Dispatcher.UIThread.InvokeAsync(async () =>
  564. {
  565. _isClosing = await ContentDialogHelper.CreateExitDialog();
  566. if (_isClosing)
  567. {
  568. Close();
  569. }
  570. });
  571. }
  572. }
  573. }