misc: Code cleanup

This commit is contained in:
Evan Husted 2024-10-25 08:34:50 -05:00
parent c69904afc6
commit 281be7ca62
19 changed files with 136 additions and 156 deletions

View file

@ -31,15 +31,11 @@ namespace Ryujinx.Ava.UI.Applet
public bool DisplayMessageDialog(ControllerAppletUIArgs args)
{
ManualResetEvent dialogCloseEvent = new(false);
bool ignoreApplet = ConfigurationState.Instance.IgnoreApplet;
bool okPressed = false;
if (ignoreApplet)
{
if (ConfigurationState.Instance.IgnoreApplet)
return false;
}
Dispatcher.UIThread.InvokeAsync(async () =>
{
@ -74,9 +70,9 @@ namespace Ryujinx.Ava.UI.Applet
UserResult response = await ContentDialogHelper.ShowDeferredContentDialog(_parent,
title,
message,
"",
string.Empty,
LocaleManager.Instance[LocaleKeys.DialogOpenSettingsWindowLabel],
"",
string.Empty,
LocaleManager.Instance[LocaleKeys.SettingsButtonClose],
(int)Symbol.Important,
deferEvent,
@ -179,12 +175,12 @@ namespace Ryujinx.Ava.UI.Applet
{
Title = title,
WindowStartupLocation = WindowStartupLocation.CenterScreen,
Width = 400,
Width = 400
};
object response = await msgDialog.Run();
if (response != null && buttons != null && buttons.Length > 1 && (int)response != buttons.Length - 1)
if (response != null && buttons is { Length: > 1 } && (int)response != buttons.Length - 1)
{
showDetails = true;
}

View file

@ -24,10 +24,13 @@ namespace Ryujinx.Ava.UI.Applet
public AvaloniaDynamicTextInputHandler(MainWindow parent)
{
_parent = parent;
(_parent.InputManager.KeyboardDriver as AvaloniaKeyboardDriver).KeyPressed += AvaloniaDynamicTextInputHandler_KeyPressed;
(_parent.InputManager.KeyboardDriver as AvaloniaKeyboardDriver).KeyRelease += AvaloniaDynamicTextInputHandler_KeyRelease;
(_parent.InputManager.KeyboardDriver as AvaloniaKeyboardDriver).TextInput += AvaloniaDynamicTextInputHandler_TextInput;
if (_parent.InputManager.KeyboardDriver is AvaloniaKeyboardDriver avaloniaKeyboardDriver)
{
avaloniaKeyboardDriver.KeyPressed += AvaloniaDynamicTextInputHandler_KeyPressed;
avaloniaKeyboardDriver.KeyRelease += AvaloniaDynamicTextInputHandler_KeyRelease;
avaloniaKeyboardDriver.TextInput += AvaloniaDynamicTextInputHandler_TextInput;
}
_hiddenTextBox = _parent.HiddenTextBox;
@ -44,7 +47,7 @@ namespace Ryujinx.Ava.UI.Applet
TextChangedEvent?.Invoke(text ?? string.Empty, _hiddenTextBox.SelectionStart, _hiddenTextBox.SelectionEnd, false);
}
private void SelectionChanged(int selection)
private void SelectionChanged(int _)
{
TextChangedEvent?.Invoke(_hiddenTextBox.Text ?? string.Empty, _hiddenTextBox.SelectionStart, _hiddenTextBox.SelectionEnd, false);
}
@ -112,10 +115,13 @@ namespace Ryujinx.Ava.UI.Applet
public void Dispose()
{
(_parent.InputManager.KeyboardDriver as AvaloniaKeyboardDriver).KeyPressed -= AvaloniaDynamicTextInputHandler_KeyPressed;
(_parent.InputManager.KeyboardDriver as AvaloniaKeyboardDriver).KeyRelease -= AvaloniaDynamicTextInputHandler_KeyRelease;
(_parent.InputManager.KeyboardDriver as AvaloniaKeyboardDriver).TextInput -= AvaloniaDynamicTextInputHandler_TextInput;
if (_parent.InputManager.KeyboardDriver is AvaloniaKeyboardDriver avaloniaKeyboardDriver)
{
avaloniaKeyboardDriver.KeyPressed -= AvaloniaDynamicTextInputHandler_KeyPressed;
avaloniaKeyboardDriver.KeyRelease -= AvaloniaDynamicTextInputHandler_KeyRelease;
avaloniaKeyboardDriver.TextInput -= AvaloniaDynamicTextInputHandler_TextInput;
}
_textChangedSubscription?.Dispose();
_selectionStartChangedSubscription?.Dispose();
_selectionEndtextChangedSubscription?.Dispose();

View file

@ -47,42 +47,37 @@ namespace Ryujinx.Ava.UI.Controls
LoadProfiles();
if (contentManager.GetCurrentFirmwareVersion() != null)
{
Task.Run(() =>
{
UserFirmwareAvatarSelectorViewModel.PreloadAvatars(contentManager, virtualFileSystem);
});
}
Task.Run(() => UserFirmwareAvatarSelectorViewModel.PreloadAvatars(contentManager, virtualFileSystem));
InitializeComponent();
}
public void GoBack()
{
if (ContentFrame.BackStack.Count > 0)
{
ContentFrame.GoBack();
}
LoadProfiles();
}
public void Navigate(Type sourcePageType, object parameter)
{
ContentFrame.Navigate(sourcePageType, parameter);
}
public void Navigate(Type sourcePageType, object parameter)
=> ContentFrame.Navigate(sourcePageType, parameter);
public static async Task Show(AccountManager ownerAccountManager, ContentManager ownerContentManager,
VirtualFileSystem ownerVirtualFileSystem, HorizonClient ownerHorizonClient)
public static async Task Show(
AccountManager ownerAccountManager,
ContentManager ownerContentManager,
VirtualFileSystem ownerVirtualFileSystem,
HorizonClient ownerHorizonClient)
{
var content = new NavigationDialogHost(ownerAccountManager, ownerContentManager, ownerVirtualFileSystem, ownerHorizonClient);
ContentDialog contentDialog = new()
{
Title = LocaleManager.Instance[LocaleKeys.UserProfileWindowTitle],
PrimaryButtonText = "",
SecondaryButtonText = "",
CloseButtonText = "",
PrimaryButtonText = string.Empty,
SecondaryButtonText = string.Empty,
CloseButtonText = string.Empty,
Content = content,
Padding = new Thickness(0),
Padding = new Thickness(0)
};
contentDialog.Closed += (_, _) => content.ViewModel.Dispose();
@ -160,14 +155,14 @@ namespace Ryujinx.Ava.UI.Controls
if (profile == null)
{
Dispatcher.UIThread.Post(Action);
return;
static async void Action()
{
await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance[LocaleKeys.DialogUserProfileDeletionWarningMessage]);
}
Dispatcher.UIThread.Post(Action);
return;
}
AccountManager.OpenUser(profile.UserId);

View file

@ -42,7 +42,7 @@ namespace Ryujinx.Ava.UI.Helpers
PrimaryButtonCommand = MiniCommand.Create(() =>
{
result = primaryButtonResult;
}),
})
};
contentDialog.SecondaryButtonCommand = MiniCommand.Create(() =>

View file

@ -1,5 +1,6 @@
using Avalonia.Logging;
using Avalonia.Utilities;
using Gommon;
using Ryujinx.Common.Logging;
using System;
using System.Text;
@ -90,7 +91,7 @@ namespace Ryujinx.Ava.UI.Helpers
if (source != null)
{
result.Append(" (");
result.Append(source.GetType().Name);
result.Append(source.GetType().AsFullNamePrettyString());
result.Append(" #");
result.Append(source.GetHashCode());
result.Append(')');

View file

@ -35,7 +35,7 @@ namespace Ryujinx.Ava.UI.Helpers
{
Text = text,
Source = this,
RoutedEvent = TextInputEvent,
RoutedEvent = TextInputEvent
});
}
}

View file

@ -9,20 +9,12 @@ namespace Ryujinx.Ava.UI.Helpers
{
public static TimeZoneConverter Instance = new();
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
{
return null;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
=> value is TimeZone timeZone
? $"{timeZone.UtcDifference} {timeZone.Location} {timeZone.Abbreviation}"
: null;
var timeZone = (TimeZone)value;
return string.Format("{0} {1} {2}", timeZone.UtcDifference, timeZone.Location, timeZone.Abbreviation);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
=> throw new NotImplementedException();
}
}

View file

@ -3,12 +3,12 @@ using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Input;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using Avalonia.Platform.Storage;
using Avalonia.Threading;
using DynamicData;
using DynamicData.Binding;
using FluentAvalonia.UI.Controls;
using Gommon;
using LibHac.Common;
using Ryujinx.Ava.Common;
using Ryujinx.Ava.Common.Locale;
@ -41,6 +41,7 @@ using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Key = Ryujinx.Input.Key;
@ -113,6 +114,9 @@ namespace Ryujinx.Ava.UI.ViewModels
public ApplicationData ListSelectedApplication;
public ApplicationData GridSelectedApplication;
public static readonly Bitmap IconBitmap =
new(Assembly.GetAssembly(typeof(ConfigurationState))!.GetManifestResourceStream("Ryujinx.UI.Common.Resources.Logo_Ryujinx.png")!);
public MainWindow Window { get; init; }
internal AppHost AppHost { get; set; }
@ -179,18 +183,16 @@ namespace Ryujinx.Ava.UI.ViewModels
_searchTimer?.Dispose();
_searchTimer = new Timer(TimerCallback, null, 1000, 0);
_searchTimer = new Timer(_ =>
{
RefreshView();
_searchTimer.Dispose();
_searchTimer = null;
}, null, 1000, 0);
}
}
private void TimerCallback(object obj)
{
RefreshView();
_searchTimer.Dispose();
_searchTimer = null;
}
public bool CanUpdate
{
get => _canUpdate && EnableNonGameRunningControls && Updater.CanUpdate(false);
@ -978,29 +980,26 @@ namespace Ryujinx.Ava.UI.ViewModels
#region PrivateMethods
private IComparer<ApplicationData> GetComparer()
{
return SortMode switch
private static IComparer<ApplicationData> CreateComparer(bool ascending, Func<ApplicationData, IComparable> selector) =>
ascending
? SortExpressionComparer<ApplicationData>.Ascending(selector)
: SortExpressionComparer<ApplicationData>.Descending(selector);
private IComparer<ApplicationData> GetComparer()
=> SortMode switch
{
#pragma warning disable IDE0055 // Disable formatting
ApplicationSort.Title => IsAscending ? SortExpressionComparer<ApplicationData>.Ascending(app => app.Name)
: SortExpressionComparer<ApplicationData>.Descending(app => app.Name),
ApplicationSort.Developer => IsAscending ? SortExpressionComparer<ApplicationData>.Ascending(app => app.Developer)
: SortExpressionComparer<ApplicationData>.Descending(app => app.Developer),
ApplicationSort.Title => CreateComparer(IsAscending, app => app.Name),
ApplicationSort.Developer => CreateComparer(IsAscending, app => app.Developer),
ApplicationSort.LastPlayed => new LastPlayedSortComparer(IsAscending),
ApplicationSort.TotalTimePlayed => new TimePlayedSortComparer(IsAscending),
ApplicationSort.FileType => IsAscending ? SortExpressionComparer<ApplicationData>.Ascending(app => app.FileExtension)
: SortExpressionComparer<ApplicationData>.Descending(app => app.FileExtension),
ApplicationSort.FileSize => IsAscending ? SortExpressionComparer<ApplicationData>.Ascending(app => app.FileSize)
: SortExpressionComparer<ApplicationData>.Descending(app => app.FileSize),
ApplicationSort.Path => IsAscending ? SortExpressionComparer<ApplicationData>.Ascending(app => app.Path)
: SortExpressionComparer<ApplicationData>.Descending(app => app.Path),
ApplicationSort.Favorite => IsAscending ? SortExpressionComparer<ApplicationData>.Ascending(app => new AppListFavoriteComparable(app))
: SortExpressionComparer<ApplicationData>.Descending(app => new AppListFavoriteComparable(app)),
ApplicationSort.FileType => CreateComparer(IsAscending, app => app.FileExtension),
ApplicationSort.FileSize => CreateComparer(IsAscending, app => app.FileSize),
ApplicationSort.Path => CreateComparer(IsAscending, app => app.Path),
ApplicationSort.Favorite => CreateComparer(IsAscending, app => new AppListFavoriteComparable(app)),
_ => null,
#pragma warning restore IDE0055
};
}
public void RefreshView()
{
@ -1125,7 +1124,7 @@ namespace Ryujinx.Ava.UI.ViewModels
}
catch (MissingKeyException ex)
{
if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime)
{
Logger.Error?.Print(LogClass.Application, ex.ToString());
@ -1260,8 +1259,10 @@ namespace Ryujinx.Ava.UI.ViewModels
GameStatusText = args.GameStatus;
VolumeStatusText = args.VolumeStatus;
FifoStatusText = args.FifoStatus;
ShaderCountText = args.ShaderCount > 0 ? $"Compiling shaders: {args.ShaderCount}" : string.Empty;
ShowRightmostSeparator = !ShaderCountText.IsNullOrEmpty();
ShaderCountText = (ShowRightmostSeparator = args.ShaderCount > 0)
? $"{LocaleManager.Instance[LocaleKeys.CompilingShaders]}: {args.ShaderCount}"
: string.Empty;
ShowStatusSeparator = true;
});
@ -1641,8 +1642,7 @@ namespace Ryujinx.Ava.UI.ViewModels
gameThread.Start();
}
public void SwitchToRenderer(bool startFullscreen)
{
public void SwitchToRenderer(bool startFullscreen) =>
Dispatcher.UIThread.Post(() =>
{
SwitchToGameControl(startFullscreen);
@ -1651,15 +1651,9 @@ namespace Ryujinx.Ava.UI.ViewModels
RendererHostControl.Focus();
});
}
public static void UpdateGameMetadata(string titleId)
{
ApplicationLibrary.LoadAndSaveMetaData(titleId, appMetadata =>
{
appMetadata.UpdatePostGame();
});
}
public static void UpdateGameMetadata(string titleId)
=> ApplicationLibrary.LoadAndSaveMetaData(titleId, appMetadata => appMetadata.UpdatePostGame());
public void RefreshFirmwareStatus()
{

View file

@ -287,11 +287,11 @@ namespace Ryujinx.Ava.UI.ViewModels
public SettingsViewModel()
{
GameDirectories = new AvaloniaList<string>();
AutoloadDirectories = new AvaloniaList<string>();
TimeZones = new AvaloniaList<TimeZone>();
AvailableGpus = new ObservableCollection<ComboBoxItem>();
_validTzRegions = new List<string>();
GameDirectories = [];
AutoloadDirectories = [];
TimeZones = [];
AvailableGpus = [];
_validTzRegions = [];
_networkInterfaces = new Dictionary<string, string>();
Task.Run(CheckSoundBackends);

View file

@ -21,10 +21,10 @@ namespace Ryujinx.Ava.UI.Windows
private readonly struct PaletteColor(int qck, byte r, byte g, byte b)
{
public int Qck { get; } = qck;
public byte R { get; } = r;
public byte G { get; } = g;
public byte B { get; } = b;
public int Qck => qck;
public byte R => r;
public byte G => g;
public byte B => b;
}
public static SKColor GetFilteredColor(SKBitmap image)
@ -54,15 +54,6 @@ namespace Ryujinx.Ava.UI.Windows
var buffer = GetBuffer(image);
int w = image.Width;
int w8 = w << 8;
int h8 = image.Height << 8;
#pragma warning disable IDE0059 // Unnecessary assignment
int xStep = w8 / ColorsPerLine;
int yStep = h8 / ColorsPerLine;
#pragma warning restore IDE0059
int i = 0;
int maxHitCount = 0;