ryujinx/Ryujinx.Ava/Ui/Applet/SwkbdAppletDialog.axaml.cs
Emmanuel Hansen 594246ea47
UI - Avalonia Part 2 (#3351)
* add settings windows and children views

* Expose hotkeys configuration on the UI

* Remove double spacing from locale JSON

* simplify button assigner

* add cemuhook buttons and title to locale

* move common button assigner to own class

* cancel button assigner when window is closed

* remove unused setting

* address review. fix controller profile not loading default when switching devices

* fix updater file name

* Input cleanup (#37)

* addressed review

* add device type to controller device checks

* change accessibility modifier of public classes to internal

* Update Ryujinx.Ava/Ui/ViewModels/ControllerSettingsViewModel.cs

Co-authored-by: gdkchan <gab.dark.100@gmail.com>

* Update de_DE.json

* Update de_DE.json

* Update tr_TR.json

Translated newly added lines

* Update it_IT.json

* fix rebase

* update avalonia

* fix wrong key used for button text

* Align settings window elements

* Tabs to spaces

* Update brazilian portuguese translation

* Minor improvement on brazilian portuguese translation

* fix turkish translation

* remove unused text

* change view related classes to public

* unsubscribe from deferred event if dialog is closed

* Load the default language before loading any other when switching languages

* Make controller settings more compact

* increase default width of settings window, reduce profile buttons width

Co-authored-by: gdk <gab.dark.100@gmail.com>
Co-authored-by: MutantAura <44103205+MutantAura@users.noreply.github.com>
Co-authored-by: Niwu34 <67392333+Niwu34@users.noreply.github.com>
Co-authored-by: aegiff <99728970+aegiff@users.noreply.github.com>
Co-authored-by: Antonio Brugnolo <36473846+AntoSkate@users.noreply.github.com>
2022-07-05 20:06:31 +02:00

152 lines
5.0 KiB
C#

using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using FluentAvalonia.Core;
using FluentAvalonia.UI.Controls;
using Ryujinx.Ava.Common.Locale;
using Ryujinx.Ava.Ui.Windows;
using Ryujinx.HLE.HOS.Applets;
using System;
using System.Threading.Tasks;
namespace Ryujinx.Ava.Ui.Controls
{
internal class SwkbdAppletDialog : UserControl
{
private Predicate<int> _checkLength;
private int _inputMax;
private int _inputMin;
private string _placeholder;
private ContentDialog _host;
public SwkbdAppletDialog(string mainText, string secondaryText, string placeholder)
{
MainText = mainText;
SecondaryText = secondaryText;
DataContext = this;
_placeholder = placeholder;
InitializeComponent();
SetInputLengthValidation(0, int.MaxValue); // Disable by default.
}
public SwkbdAppletDialog()
{
DataContext = this;
InitializeComponent();
}
public string Message { get; set; } = "";
public string MainText { get; set; } = "";
public string SecondaryText { get; set; } = "";
public TextBlock Error { get; private set; }
public TextBox Input { get; set; }
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
Error = this.FindControl<TextBlock>("Error");
Input = this.FindControl<TextBox>("Input");
Input.Watermark = _placeholder;
Input.AddHandler(TextInputEvent, Message_TextInput, RoutingStrategies.Tunnel, true);
}
public static async Task<(UserResult Result, string Input)> ShowInputDialog(StyleableWindow window, string title, SoftwareKeyboardUiArgs args)
{
ContentDialog contentDialog = window.ContentDialog;
UserResult result = UserResult.Cancel;
SwkbdAppletDialog content = new SwkbdAppletDialog(args.HeaderText, args.SubtitleText, args.GuideText)
{
Message = args.InitialText ?? ""
};
string input = string.Empty;
content.SetInputLengthValidation(args.StringLengthMin, args.StringLengthMax);
if (contentDialog != null)
{
content._host = contentDialog;
contentDialog.Title = title;
contentDialog.PrimaryButtonText = args.SubmitText;
contentDialog.IsPrimaryButtonEnabled = content._checkLength(content.Message.Length);
contentDialog.SecondaryButtonText = "";
contentDialog.CloseButtonText = LocaleManager.Instance["InputDialogCancel"];
contentDialog.Content = content;
TypedEventHandler<ContentDialog, ContentDialogClosedEventArgs> handler = (sender, eventArgs) =>
{
if (eventArgs.Result == ContentDialogResult.Primary)
{
result = UserResult.Ok;
input = content.Input.Text;
}
};
contentDialog.Closed += handler;
await contentDialog.ShowAsync();
contentDialog.Closed -= handler;
}
return (result, input);
}
public void SetInputLengthValidation(int min, int max)
{
_inputMin = Math.Min(min, max);
_inputMax = Math.Max(min, max);
Error.IsVisible = false;
Error.FontStyle = FontStyle.Italic;
if (_inputMin <= 0 && _inputMax == int.MaxValue) // Disable.
{
Error.IsVisible = false;
_checkLength = length => true;
}
else if (_inputMin > 0 && _inputMax == int.MaxValue)
{
Error.IsVisible = true;
Error.Text = string.Format(LocaleManager.Instance["SwkbdMinCharacters"], _inputMin);
_checkLength = length => _inputMin <= length;
}
else
{
Error.IsVisible = true;
Error.Text = string.Format(LocaleManager.Instance["SwkbdMinRangeCharacters"], _inputMin, _inputMax);
_checkLength = length => _inputMin <= length && length <= _inputMax;
}
Message_TextInput(this, new TextInputEventArgs());
}
private void Message_TextInput(object sender, TextInputEventArgs e)
{
if (_host != null)
{
_host.IsPrimaryButtonEnabled = _checkLength(Message.Length);
}
}
private void Message_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter && _host.IsPrimaryButtonEnabled)
{
_host.Hide(ContentDialogResult.Primary);
}
else
{
_host.IsPrimaryButtonEnabled = _checkLength(Message.Length);
}
}
}
}