MainWindow.axaml.cs 23 KB

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