From 47c491271a2546bdb9451a5d1fdd5eaa649734d9 Mon Sep 17 00:00:00 2001 From: Isaac Marovitz Date: Fri, 19 Apr 2024 17:22:58 -0400 Subject: [PATCH] Migrate UI to SettingsUIViewModel --- .../Settings/SettingsUIViewModel.cs | 116 +++++++++++++++++ .../ViewModels/Settings/SettingsViewModel.cs | 117 ++---------------- .../UI/Views/Settings/SettingsUIView.axaml | 4 +- .../UI/Views/Settings/SettingsUIView.axaml.cs | 17 ++- .../UI/Windows/SettingsWindow.axaml.cs | 8 +- 5 files changed, 137 insertions(+), 125 deletions(-) create mode 100644 src/Ryujinx/UI/ViewModels/Settings/SettingsUIViewModel.cs diff --git a/src/Ryujinx/UI/ViewModels/Settings/SettingsUIViewModel.cs b/src/Ryujinx/UI/ViewModels/Settings/SettingsUIViewModel.cs new file mode 100644 index 000000000..f9241f267 --- /dev/null +++ b/src/Ryujinx/UI/ViewModels/Settings/SettingsUIViewModel.cs @@ -0,0 +1,116 @@ +using Avalonia.Collections; +using Ryujinx.Common.Configuration; +using Ryujinx.UI.Common.Configuration; +using System; +using System.Linq; + +namespace Ryujinx.Ava.UI.ViewModels.Settings +{ + public class SettingsUIViewModel : BaseModel + { + public event Action DirtyEvent; + + private bool _enableDiscordIntegration; + public bool EnableDiscordIntegration + { + get => _enableDiscordIntegration; + set + { + _enableDiscordIntegration = value; + DirtyEvent?.Invoke(); + } + } + + private bool _checkUpdatesOnStart; + public bool CheckUpdatesOnStart + { + get => _checkUpdatesOnStart; + set + { + _checkUpdatesOnStart = value; + DirtyEvent?.Invoke(); + } + } + + private bool _showConfirmExit; + public bool ShowConfirmExit + { + get => _showConfirmExit; + set + { + _showConfirmExit = value; + DirtyEvent?.Invoke(); + } + } + + private int _hideCursor; + public int HideCursor + { + get => _hideCursor; + set + { + _hideCursor = value; + DirtyEvent?.Invoke(); + } + } + + private int _baseStyleIndex; + public int BaseStyleIndex + { + get => _baseStyleIndex; + set + { + _baseStyleIndex = value; + DirtyEvent?.Invoke(); + } + } + + public bool DirsChanged; + + public AvaloniaList GameDirectories { get; set; } + + public SettingsUIViewModel() + { + ConfigurationState config = ConfigurationState.Instance; + + GameDirectories = new(); + + EnableDiscordIntegration = config.EnableDiscordIntegration; + CheckUpdatesOnStart = config.CheckUpdatesOnStart; + ShowConfirmExit = config.ShowConfirmExit; + HideCursor = (int)config.HideCursor.Value; + + GameDirectories.Clear(); + GameDirectories.AddRange(config.UI.GameDirs.Value); + GameDirectories.CollectionChanged += (_, _) => DirtyEvent?.Invoke(); + + BaseStyleIndex = config.UI.BaseStyle == "Light" ? 0 : 1; + } + + public bool CheckIfModified(ConfigurationState config) + { + bool isDirty = false; + + DirsChanged = !config.UI.GameDirs.Value.SequenceEqual(GameDirectories); + + isDirty |= config.EnableDiscordIntegration.Value != EnableDiscordIntegration; + isDirty |= config.CheckUpdatesOnStart.Value != CheckUpdatesOnStart; + isDirty |= config.ShowConfirmExit.Value != ShowConfirmExit; + isDirty |= config.HideCursor.Value != (HideCursorMode)HideCursor; + isDirty |= DirsChanged; + isDirty |= config.UI.BaseStyle.Value != (BaseStyleIndex == 0 ? "Light" : "Dark"); + + return isDirty; + } + + public void Save(ConfigurationState config) + { + config.EnableDiscordIntegration.Value = EnableDiscordIntegration; + config.CheckUpdatesOnStart.Value = CheckUpdatesOnStart; + config.ShowConfirmExit.Value = ShowConfirmExit; + config.HideCursor.Value = (HideCursorMode)HideCursor; + config.UI.GameDirs.Value = GameDirectories.ToList(); + config.UI.BaseStyle.Value = BaseStyleIndex == 0 ? "Light" : "Dark"; + } + } +} diff --git a/src/Ryujinx/UI/ViewModels/Settings/SettingsViewModel.cs b/src/Ryujinx/UI/ViewModels/Settings/SettingsViewModel.cs index fe54e8fdd..96678a330 100644 --- a/src/Ryujinx/UI/ViewModels/Settings/SettingsViewModel.cs +++ b/src/Ryujinx/UI/ViewModels/Settings/SettingsViewModel.cs @@ -1,16 +1,11 @@ -using Avalonia.Collections; using Ryujinx.Ava.UI.Windows; -using Ryujinx.Common.Configuration; using Ryujinx.UI.Common.Configuration; using System; -using System.Collections.Generic; namespace Ryujinx.Ava.UI.ViewModels.Settings { public class SettingsViewModel : BaseModel { - private bool _directoryChanged; - private bool _isModified; public bool IsModified @@ -27,65 +22,8 @@ namespace Ryujinx.Ava.UI.ViewModels.Settings public event Action DirtyEvent; public event Action ToggleButtons; - public bool DirectoryChanged - { - get => _directoryChanged; - set - { - _directoryChanged = value; - - OnPropertyChanged(); - } - } - public bool IsMacOS => OperatingSystem.IsMacOS(); - private bool _enableDiscordIntegration; - public bool EnableDiscordIntegration - { - get => _enableDiscordIntegration; - set - { - _enableDiscordIntegration = value; - CheckIfModified(); - } - } - - private bool _checkUpdatesOnStart; - public bool CheckUpdatesOnStart - { - get => _checkUpdatesOnStart; - set - { - _checkUpdatesOnStart = value; - CheckIfModified(); - } - } - - private bool _showConfirmExit; - public bool ShowConfirmExit - { - get => _showConfirmExit; - set - { - _showConfirmExit = value; - CheckIfModified(); - } - } - - private int _hideCursor; - public int HideCursor - { - get => _hideCursor; - set - { - _hideCursor = value; - CheckIfModified(); - } - } - - public int BaseStyleIndex { get; set; } - private readonly SettingsAudioViewModel _audioViewModel; private readonly SettingsCpuViewModel _cpuViewModel; private readonly SettingsGraphicsViewModel _graphicsViewModel; @@ -94,8 +32,7 @@ namespace Ryujinx.Ava.UI.ViewModels.Settings private readonly SettingsLoggingViewModel _loggingViewModel; private readonly SettingsNetworkViewModel _networkViewModel; private readonly SettingsSystemViewModel _systemViewModel; - - public AvaloniaList GameDirectories { get; set; } + private readonly SettingsUIViewModel _uiViewModel; public SettingsViewModel( SettingsAudioViewModel audioViewModel, @@ -105,7 +42,8 @@ namespace Ryujinx.Ava.UI.ViewModels.Settings SettingsInputViewModel inputViewModel, SettingsLoggingViewModel loggingViewModel, SettingsNetworkViewModel networkViewModel, - SettingsSystemViewModel systemViewModel) : this() + SettingsSystemViewModel systemViewModel, + SettingsUIViewModel uiViewModel) : this() { _audioViewModel = audioViewModel; _cpuViewModel = cpuViewModel; @@ -115,6 +53,7 @@ namespace Ryujinx.Ava.UI.ViewModels.Settings _loggingViewModel = loggingViewModel; _networkViewModel = networkViewModel; _systemViewModel = systemViewModel; + _uiViewModel = uiViewModel; _audioViewModel.DirtyEvent += CheckIfModified; _cpuViewModel.DirtyEvent += CheckIfModified; @@ -124,15 +63,14 @@ namespace Ryujinx.Ava.UI.ViewModels.Settings _loggingViewModel.DirtyEvent += CheckIfModified; _networkViewModel.DirtyEvent += CheckIfModified; _systemViewModel.DirtyEvent += CheckIfModified; + _uiViewModel.DirtyEvent += CheckIfModified; } public SettingsViewModel() { - GameDirectories = new AvaloniaList(); - if (Program.PreviewerDetached) { - LoadCurrentConfiguration(); + } } @@ -142,15 +80,6 @@ namespace Ryujinx.Ava.UI.ViewModels.Settings ConfigurationState config = ConfigurationState.Instance; - isDirty |= config.EnableDiscordIntegration.Value != EnableDiscordIntegration; - isDirty |= config.CheckUpdatesOnStart.Value != CheckUpdatesOnStart; - isDirty |= config.ShowConfirmExit.Value != ShowConfirmExit; - isDirty |= config.HideCursor.Value != (HideCursorMode)HideCursor; - - // isDirty |= config.UI.GameDirs.Value != GameDirectories.ToList(); - - isDirty |= config.UI.BaseStyle.Value != (BaseStyleIndex == 0 ? "Light" : "Dark"); - isDirty |= _audioViewModel?.CheckIfModified(config) ?? false; isDirty |= _cpuViewModel?.CheckIfModified(config) ?? false; isDirty |= _graphicsViewModel?.CheckIfModified(config) ?? false; @@ -160,44 +89,15 @@ namespace Ryujinx.Ava.UI.ViewModels.Settings isDirty |= _loggingViewModel?.CheckIfModified(config) ?? false; isDirty |= _networkViewModel?.CheckIfModified(config) ?? false; isDirty |= _systemViewModel?.CheckIfModified(config) ?? false; + isDirty |= _uiViewModel?.CheckIfModified(config) ?? false; IsModified = isDirty; } - public void LoadCurrentConfiguration() - { - ConfigurationState config = ConfigurationState.Instance; - - // User Interface - EnableDiscordIntegration = config.EnableDiscordIntegration; - CheckUpdatesOnStart = config.CheckUpdatesOnStart; - ShowConfirmExit = config.ShowConfirmExit; - HideCursor = (int)config.HideCursor.Value; - - GameDirectories.Clear(); - GameDirectories.AddRange(config.UI.GameDirs.Value); - - BaseStyleIndex = config.UI.BaseStyle == "Light" ? 0 : 1; - } - public void SaveSettings() { ConfigurationState config = ConfigurationState.Instance; - // User Interface - config.EnableDiscordIntegration.Value = EnableDiscordIntegration; - config.CheckUpdatesOnStart.Value = CheckUpdatesOnStart; - config.ShowConfirmExit.Value = ShowConfirmExit; - config.HideCursor.Value = (HideCursorMode)HideCursor; - - if (_directoryChanged) - { - List gameDirs = new(GameDirectories); - config.UI.GameDirs.Value = gameDirs; - } - - config.UI.BaseStyle.Value = BaseStyleIndex == 0 ? "Light" : "Dark"; - _audioViewModel?.Save(config); _cpuViewModel?.Save(config); _graphicsViewModel?.Save(config); @@ -206,12 +106,11 @@ namespace Ryujinx.Ava.UI.ViewModels.Settings _loggingViewModel?.Save(config); _networkViewModel?.Save(config); _systemViewModel?.Save(config); + _uiViewModel?.Save(config); config.ToFileFormat().SaveConfig(Program.ConfigurationPath); MainWindow.UpdateGraphicsConfig(); - - _directoryChanged = false; } public void ApplyButton() diff --git a/src/Ryujinx/UI/Views/Settings/SettingsUIView.axaml b/src/Ryujinx/UI/Views/Settings/SettingsUIView.axaml index 9e9424b10..9cd69e3a0 100644 --- a/src/Ryujinx/UI/Views/Settings/SettingsUIView.axaml +++ b/src/Ryujinx/UI/Views/Settings/SettingsUIView.axaml @@ -7,9 +7,9 @@ xmlns:locale="clr-namespace:Ryujinx.Ava.Common.Locale" xmlns:viewModels="clr-namespace:Ryujinx.Ava.UI.ViewModels.Settings" mc:Ignorable="d" - x:DataType="viewModels:SettingsViewModel"> + x:DataType="viewModels:SettingsUIViewModel"> - + 0) { - _viewModel.GameDirectories.Add(result[0].Path.LocalPath); - _viewModel.DirectoryChanged = true; + ViewModel.GameDirectories.Add(result[0].Path.LocalPath); } } } @@ -52,8 +50,7 @@ namespace Ryujinx.Ava.UI.Views.Settings foreach (string path in new List(GameList.SelectedItems.Cast())) { - _viewModel.GameDirectories.Remove(path); - _viewModel.DirectoryChanged = true; + ViewModel.GameDirectories.Remove(path); } if (GameList.ItemCount > 0) diff --git a/src/Ryujinx/UI/Windows/SettingsWindow.axaml.cs b/src/Ryujinx/UI/Windows/SettingsWindow.axaml.cs index 574b943b5..ca4fb3f4a 100644 --- a/src/Ryujinx/UI/Windows/SettingsWindow.axaml.cs +++ b/src/Ryujinx/UI/Windows/SettingsWindow.axaml.cs @@ -37,6 +37,7 @@ namespace Ryujinx.Ava.UI.Windows LoggingPage = new SettingsLoggingView(); NetworkPage = new SettingsNetworkView(); SystemPage = new SettingsSystemView(virtualFileSystem, contentManager); + UiPage = new SettingsUiView(); ViewModel = new SettingsViewModel( AudioPage.ViewModel, @@ -46,9 +47,8 @@ namespace Ryujinx.Ava.UI.Windows InputPage.ViewModel, LoggingPage.ViewModel, NetworkPage.ViewModel, - SystemPage.ViewModel); - - UiPage = new SettingsUiView(ViewModel); + SystemPage.ViewModel, + UiPage.ViewModel); DataContext = ViewModel; @@ -167,7 +167,7 @@ namespace Ryujinx.Ava.UI.Windows protected override void OnClosing(WindowClosingEventArgs e) { - if (Owner is MainWindow window && ViewModel.DirectoryChanged) + if (Owner is MainWindow window && UiPage.ViewModel.DirsChanged) { window.LoadApplications(); }