From b5fafb63943e92bdb98bd548badd387fafb103aa Mon Sep 17 00:00:00 2001 From: Evan Husted Date: Mon, 6 Jan 2025 23:52:20 -0600 Subject: [PATCH] UI: stop using async voids in MainMenuBarView; use RelayCommands --- .../UI/Views/Main/MainMenuBarView.axaml | 51 +++++------ .../UI/Views/Main/MainMenuBarView.axaml.cs | 89 ++++++++----------- .../CompatibilityContentDialog.axaml.cs | 26 +----- .../Compat/CompatibilityList.axaml.cs | 24 +++++ 4 files changed, 88 insertions(+), 102 deletions(-) diff --git a/src/Ryujinx/UI/Views/Main/MainMenuBarView.axaml b/src/Ryujinx/UI/Views/Main/MainMenuBarView.axaml index d2f050c5a..6bce21851 100644 --- a/src/Ryujinx/UI/Views/Main/MainMenuBarView.axaml +++ b/src/Ryujinx/UI/Views/Main/MainMenuBarView.axaml @@ -56,7 +56,7 @@ ToolTip.Tip="{ext:Locale LoadTitleUpdatesFromFolderTooltip}" /> @@ -72,7 +72,7 @@ ToolTip.Tip="{ext:Locale OpenRyujinxLogsTooltip}" /> @@ -167,7 +167,7 @@ Header="{ext:Locale MenuBarShowFileTypes}" /> @@ -277,56 +275,55 @@ - - + + - + - - - - + + + + diff --git a/src/Ryujinx/UI/Views/Main/MainMenuBarView.axaml.cs b/src/Ryujinx/UI/Views/Main/MainMenuBarView.axaml.cs index bf95667c9..07d5fd03f 100644 --- a/src/Ryujinx/UI/Views/Main/MainMenuBarView.axaml.cs +++ b/src/Ryujinx/UI/Views/Main/MainMenuBarView.axaml.cs @@ -2,6 +2,7 @@ using Avalonia; using Avalonia.Controls; using Avalonia.Interactivity; using Avalonia.Threading; +using CommunityToolkit.Mvvm.Input; using Gommon; using Ryujinx.Ava.Common.Locale; using Ryujinx.Ava.UI.Helpers; @@ -17,6 +18,7 @@ using Ryujinx.HLE.HOS.Services.Nfc.AmiiboDecryption; using System; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; namespace Ryujinx.Ava.UI.Views.Main { @@ -34,6 +36,34 @@ namespace Ryujinx.Ava.UI.Views.Main ToggleFileTypesMenuItem.ItemsSource = GenerateToggleFileTypeItems(); ChangeLanguageMenuItem.ItemsSource = GenerateLanguageMenuItems(); + + MiiAppletMenuItem.Command = new AsyncRelayCommand(OpenMiiApplet); + CloseRyujinxMenuItem.Command = new RelayCommand(CloseWindow); + OpenSettingsMenuItem.Command = new AsyncRelayCommand(OpenSettings); + PauseEmulationMenuItem.Command = new RelayCommand(() => ViewModel.AppHost?.Pause()); + ResumeEmulationMenuItem.Command = new RelayCommand(() => ViewModel.AppHost?.Resume()); + StopEmulationMenuItem.Command = new AsyncRelayCommand(() => ViewModel.AppHost?.ShowExitPrompt().OrCompleted()!); + CheatManagerMenuItem.Command = new AsyncRelayCommand(OpenCheatManagerForCurrentApp); + InstallFileTypesMenuItem.Command = new AsyncRelayCommand(InstallFileTypes); + UninstallFileTypesMenuItem.Command = new AsyncRelayCommand(UninstallFileTypes); + XciTrimmerMenuItem.Command = new AsyncRelayCommand(() => XCITrimmerWindow.Show(ViewModel)); + AboutWindowMenuItem.Command = new AsyncRelayCommand(AboutWindow.Show); + CompatibilityListMenuItem.Command = new AsyncRelayCommand(CompatibilityList.Show); + + UpdateMenuItem.Command = new AsyncRelayCommand(async () => + { + if (Updater.CanUpdate(true)) + await Updater.BeginUpdateAsync(true); + }); + + FaqMenuItem.Command = + SetupGuideMenuItem.Command = + LdnGuideMenuItem.Command = new RelayCommand(OpenHelper.OpenUrl); + + WindowSize720PMenuItem.Command = + WindowSize1080PMenuItem.Command = + WindowSize1440PMenuItem.Command = + WindowSize2160PMenuItem.Command = new RelayCommand(ChangeWindowSize); } private CheckBox[] GenerateToggleFileTypeItems() => @@ -96,22 +126,7 @@ namespace Ryujinx.Ava.UI.Views.Main } } - private async void StopEmulation_Click(object sender, RoutedEventArgs e) - { - await ViewModel.AppHost?.ShowExitPrompt().OrCompleted()!; - } - - private void PauseEmulation_Click(object sender, RoutedEventArgs e) - { - ViewModel.AppHost?.Pause(); - } - - private void ResumeEmulation_Click(object sender, RoutedEventArgs e) - { - ViewModel.AppHost?.Resume(); - } - - public async void OpenSettings(object sender, RoutedEventArgs e) + public async Task OpenSettings() { Window.SettingsWindow = new(Window.VirtualFileSystem, Window.ContentManager); @@ -124,7 +139,7 @@ namespace Ryujinx.Ava.UI.Views.Main public static readonly AppletMetadata MiiApplet = new("miiEdit", 0x0100000000001009); - public async void OpenMiiApplet(object sender, RoutedEventArgs e) + public async Task OpenMiiApplet() { if (MiiApplet.CanStart(ViewModel.ContentManager, out var appData, out var nacpData)) { @@ -132,13 +147,7 @@ namespace Ryujinx.Ava.UI.Views.Main } } - public async void OpenAmiiboWindow(object sender, RoutedEventArgs e) - => await ViewModel.OpenAmiiboWindow(); - - public async void OpenBinFile(object sender, RoutedEventArgs e) - => await ViewModel.OpenBinFile(); - - public async void OpenCheatManagerForCurrentApp(object sender, RoutedEventArgs e) + public async Task OpenCheatManagerForCurrentApp() { if (!ViewModel.IsGameRunning) return; @@ -166,7 +175,7 @@ namespace Ryujinx.Ava.UI.Views.Main ViewModel.IsAmiiboBinRequested = ViewModel.IsAmiiboRequested && AmiiboBinReader.HasAmiiboKeyFile; } - private async void InstallFileTypes_Click(object sender, RoutedEventArgs e) + private async Task InstallFileTypes() { ViewModel.AreMimeTypesRegistered = FileAssociationHelper.Install(); if (ViewModel.AreMimeTypesRegistered) @@ -175,7 +184,7 @@ namespace Ryujinx.Ava.UI.Views.Main await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance[LocaleKeys.DialogInstallFileTypesErrorMessage]); } - private async void UninstallFileTypes_Click(object sender, RoutedEventArgs e) + private async Task UninstallFileTypes() { ViewModel.AreMimeTypesRegistered = !FileAssociationHelper.Uninstall(); if (!ViewModel.AreMimeTypesRegistered) @@ -184,11 +193,8 @@ namespace Ryujinx.Ava.UI.Views.Main await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance[LocaleKeys.DialogUninstallFileTypesErrorMessage]); } - private async void ChangeWindowSize_Click(object sender, RoutedEventArgs e) + private void ChangeWindowSize(string resolution) { - if (sender is not MenuItem { Tag: string resolution }) - return; - (int resolutionWidth, int resolutionHeight) = resolution.Split(' ', 2) .Into(parts => (int.Parse(parts[0]), int.Parse(parts[1])) @@ -201,7 +207,7 @@ namespace Ryujinx.Ava.UI.Views.Main double windowWidthScaled = (resolutionWidth * Program.WindowScaleFactor); double windowHeightScaled = ((resolutionHeight + barsHeight) * Program.WindowScaleFactor); - await Dispatcher.UIThread.InvokeAsync(() => + Dispatcher.UIThread.Post(() => { ViewModel.WindowState = WindowState.Normal; @@ -209,24 +215,7 @@ namespace Ryujinx.Ava.UI.Views.Main }); } - public async void CheckForUpdates(object sender, RoutedEventArgs e) - { - if (Updater.CanUpdate(true)) - await Updater.BeginUpdateAsync(true); - } - - private void MenuItem_OnClick(object sender, RoutedEventArgs e) - { - if (sender is MenuItem { Tag: string url }) - OpenHelper.OpenUrl(url); - } - - public async void OpenXCITrimmerWindow(object sender, RoutedEventArgs e) => await XCITrimmerWindow.Show(ViewModel); - - public async void OpenAboutWindow(object sender, RoutedEventArgs e) => await AboutWindow.Show(); - - public void CloseWindow(object sender, RoutedEventArgs e) => Window.Close(); - - private async void OpenCompatibilityList(object sender, RoutedEventArgs e) => await CompatibilityContentDialog.Show(); + public void CloseWindow() => Window.Close(); + } } diff --git a/src/Ryujinx/Utilities/Compat/CompatibilityContentDialog.axaml.cs b/src/Ryujinx/Utilities/Compat/CompatibilityContentDialog.axaml.cs index 27c560d90..513600bc0 100644 --- a/src/Ryujinx/Utilities/Compat/CompatibilityContentDialog.axaml.cs +++ b/src/Ryujinx/Utilities/Compat/CompatibilityContentDialog.axaml.cs @@ -1,35 +1,11 @@ -using Avalonia.Styling; -using FluentAvalonia.UI.Controls; -using Ryujinx.Ava.UI.Helpers; +using FluentAvalonia.UI.Controls; using System; -using System.Threading.Tasks; namespace Ryujinx.Ava.Utilities.Compat { public partial class CompatibilityContentDialog : ContentDialog { protected override Type StyleKeyOverride => typeof(ContentDialog); - - public static async Task Show() - { - await CompatibilityHelper.InitAsync(); - - CompatibilityContentDialog contentDialog = new() - { - Content = new CompatibilityList { DataContext = new CompatibilityViewModel(RyujinxApp.MainWindow.ViewModel.ApplicationLibrary) } - }; - - Style closeButton = new(x => x.Name("CloseButton")); - closeButton.Setters.Add(new Setter(WidthProperty, 80d)); - - Style closeButtonParent = new(x => x.Name("CommandSpace")); - closeButtonParent.Setters.Add(new Setter(HorizontalAlignmentProperty, Avalonia.Layout.HorizontalAlignment.Right)); - - contentDialog.Styles.Add(closeButton); - contentDialog.Styles.Add(closeButtonParent); - - await ContentDialogHelper.ShowAsync(contentDialog); - } public CompatibilityContentDialog() => InitializeComponent(); } diff --git a/src/Ryujinx/Utilities/Compat/CompatibilityList.axaml.cs b/src/Ryujinx/Utilities/Compat/CompatibilityList.axaml.cs index 9860fbc27..f78eb2098 100644 --- a/src/Ryujinx/Utilities/Compat/CompatibilityList.axaml.cs +++ b/src/Ryujinx/Utilities/Compat/CompatibilityList.axaml.cs @@ -1,9 +1,33 @@ using Avalonia.Controls; +using Avalonia.Styling; +using Ryujinx.Ava.UI.Helpers; +using System.Threading.Tasks; namespace Ryujinx.Ava.Utilities.Compat { public partial class CompatibilityList : UserControl { + public static async Task Show() + { + await CompatibilityHelper.InitAsync(); + + CompatibilityContentDialog contentDialog = new() + { + Content = new CompatibilityList { DataContext = new CompatibilityViewModel(RyujinxApp.MainWindow.ViewModel.ApplicationLibrary) } + }; + + Style closeButton = new(x => x.Name("CloseButton")); + closeButton.Setters.Add(new Setter(WidthProperty, 80d)); + + Style closeButtonParent = new(x => x.Name("CommandSpace")); + closeButtonParent.Setters.Add(new Setter(HorizontalAlignmentProperty, Avalonia.Layout.HorizontalAlignment.Right)); + + contentDialog.Styles.Add(closeButton); + contentDialog.Styles.Add(closeButtonParent); + + await ContentDialogHelper.ShowAsync(contentDialog); + } + public CompatibilityList() { InitializeComponent();