MainWindow.axaml.cs 25 KB

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