MainWindow.axaml.cs 25 KB

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