Migrate UI to SettingsUIViewModel

This commit is contained in:
Isaac Marovitz 2024-04-19 17:22:58 -04:00
parent 9b6241985f
commit 47c491271a
No known key found for this signature in database
GPG Key ID: 97250B2B09A132E1
5 changed files with 137 additions and 125 deletions

View File

@ -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<string> 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";
}
}
}

View File

@ -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<bool> DirtyEvent;
public event Action<bool> 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<string> 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<string>();
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<string> 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()

View File

@ -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">
<Design.DataContext>
<viewModels:SettingsViewModel />
<viewModels:SettingsUIViewModel />
</Design.DataContext>
<ScrollViewer
Name="UiPage"

View File

@ -11,11 +11,11 @@ namespace Ryujinx.Ava.UI.Views.Settings
{
public partial class SettingsUiView : UserControl
{
private readonly SettingsViewModel _viewModel;
public SettingsUIViewModel ViewModel;
public SettingsUiView(SettingsViewModel viewModel)
public SettingsUiView()
{
_viewModel = viewModel;
DataContext = ViewModel = new SettingsUIViewModel();
InitializeComponent();
}
@ -23,10 +23,9 @@ namespace Ryujinx.Ava.UI.Views.Settings
{
string path = PathBox.Text;
if (!string.IsNullOrWhiteSpace(path) && Directory.Exists(path) && !_viewModel.GameDirectories.Contains(path))
if (!string.IsNullOrWhiteSpace(path) && Directory.Exists(path) && !ViewModel.GameDirectories.Contains(path))
{
_viewModel.GameDirectories.Add(path);
_viewModel.DirectoryChanged = true;
ViewModel.GameDirectories.Add(path);
}
else
{
@ -39,8 +38,7 @@ namespace Ryujinx.Ava.UI.Views.Settings
if (result.Count > 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<string>(GameList.SelectedItems.Cast<string>()))
{
_viewModel.GameDirectories.Remove(path);
_viewModel.DirectoryChanged = true;
ViewModel.GameDirectories.Remove(path);
}
if (GameList.ItemCount > 0)

View File

@ -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();
}