2022-09-19 18:05:26 +00:00
|
|
|
using Avalonia;
|
2022-05-15 11:30:15 +00:00
|
|
|
using Avalonia.Controls;
|
|
|
|
using Avalonia.Input;
|
|
|
|
using Avalonia.Interactivity;
|
|
|
|
using Avalonia.Media;
|
|
|
|
using FluentAvalonia.Core;
|
|
|
|
using FluentAvalonia.UI.Controls;
|
|
|
|
using Ryujinx.Ava.Common.Locale;
|
2022-12-29 14:24:05 +00:00
|
|
|
using Ryujinx.Ava.UI.Helpers;
|
|
|
|
using Ryujinx.Ava.UI.Windows;
|
2022-05-15 11:30:15 +00:00
|
|
|
using Ryujinx.HLE.HOS.Applets;
|
|
|
|
using System;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
2022-12-29 14:24:05 +00:00
|
|
|
namespace Ryujinx.Ava.UI.Controls
|
2022-05-15 11:30:15 +00:00
|
|
|
{
|
2022-07-24 17:38:38 +00:00
|
|
|
internal partial class SwkbdAppletDialog : UserControl
|
2022-05-15 11:30:15 +00:00
|
|
|
{
|
|
|
|
private Predicate<int> _checkLength;
|
|
|
|
private int _inputMax;
|
|
|
|
private int _inputMin;
|
|
|
|
private string _placeholder;
|
|
|
|
|
|
|
|
private ContentDialog _host;
|
|
|
|
|
2023-04-12 19:18:40 +00:00
|
|
|
public SwkbdAppletDialog(string mainText, string secondaryText, string placeholder, string message)
|
2022-05-15 11:30:15 +00:00
|
|
|
{
|
|
|
|
MainText = mainText;
|
|
|
|
SecondaryText = secondaryText;
|
2023-04-12 19:18:40 +00:00
|
|
|
Message = message ?? "";
|
2022-05-15 11:30:15 +00:00
|
|
|
DataContext = this;
|
|
|
|
_placeholder = placeholder;
|
|
|
|
InitializeComponent();
|
|
|
|
|
2022-07-24 17:38:38 +00:00
|
|
|
Input.Watermark = _placeholder;
|
|
|
|
|
|
|
|
Input.AddHandler(TextInputEvent, Message_TextInput, RoutingStrategies.Tunnel, true);
|
|
|
|
|
2022-05-15 11:30:15 +00:00
|
|
|
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 static async Task<(UserResult Result, string Input)> ShowInputDialog(StyleableWindow window, string title, SoftwareKeyboardUiArgs args)
|
|
|
|
{
|
2022-07-24 17:38:38 +00:00
|
|
|
ContentDialog contentDialog = new ContentDialog();
|
2022-05-15 11:30:15 +00:00
|
|
|
|
|
|
|
UserResult result = UserResult.Cancel;
|
|
|
|
|
2023-04-12 19:18:40 +00:00
|
|
|
SwkbdAppletDialog content = new SwkbdAppletDialog(args.HeaderText, args.SubtitleText, args.GuideText, args.InitialText);
|
2022-05-15 11:30:15 +00:00
|
|
|
|
|
|
|
string input = string.Empty;
|
|
|
|
|
2022-09-19 18:05:26 +00:00
|
|
|
var overlay = new ContentDialogOverlayWindow()
|
|
|
|
{
|
|
|
|
Height = window.Bounds.Height,
|
|
|
|
Width = window.Bounds.Width,
|
|
|
|
Position = window.PointToScreen(new Point())
|
|
|
|
};
|
|
|
|
|
|
|
|
window.PositionChanged += OverlayOnPositionChanged;
|
|
|
|
|
|
|
|
void OverlayOnPositionChanged(object sender, PixelPointEventArgs e)
|
|
|
|
{
|
|
|
|
overlay.Position = window.PointToScreen(new Point());
|
|
|
|
}
|
|
|
|
|
|
|
|
contentDialog = overlay.ContentDialog;
|
|
|
|
|
|
|
|
bool opened = false;
|
|
|
|
|
2022-05-15 11:30:15 +00:00
|
|
|
content.SetInputLengthValidation(args.StringLengthMin, args.StringLengthMax);
|
|
|
|
|
2022-09-19 18:05:26 +00:00
|
|
|
content._host = contentDialog;
|
|
|
|
contentDialog.Title = title;
|
|
|
|
contentDialog.PrimaryButtonText = args.SubmitText;
|
|
|
|
contentDialog.IsPrimaryButtonEnabled = content._checkLength(content.Message.Length);
|
|
|
|
contentDialog.SecondaryButtonText = "";
|
2023-01-03 18:45:08 +00:00
|
|
|
contentDialog.CloseButtonText = LocaleManager.Instance[LocaleKeys.InputDialogCancel];
|
2022-09-19 18:05:26 +00:00
|
|
|
contentDialog.Content = content;
|
|
|
|
|
|
|
|
TypedEventHandler<ContentDialog, ContentDialogClosedEventArgs> handler = (sender, eventArgs) =>
|
2022-05-15 11:30:15 +00:00
|
|
|
{
|
2022-09-19 18:05:26 +00:00
|
|
|
if (eventArgs.Result == ContentDialogResult.Primary)
|
2022-05-15 11:30:15 +00:00
|
|
|
{
|
2022-09-19 18:05:26 +00:00
|
|
|
result = UserResult.Ok;
|
|
|
|
input = content.Input.Text;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
contentDialog.Closed += handler;
|
|
|
|
|
|
|
|
overlay.Opened += OverlayOnActivated;
|
|
|
|
|
|
|
|
async void OverlayOnActivated(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
if (opened)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
opened = true;
|
|
|
|
|
|
|
|
overlay.Position = window.PointToScreen(new Point());
|
|
|
|
|
2022-12-16 01:40:55 +00:00
|
|
|
await contentDialog.ShowAsync(overlay);
|
2022-05-15 11:30:15 +00:00
|
|
|
contentDialog.Closed -= handler;
|
2022-09-19 18:05:26 +00:00
|
|
|
overlay.Close();
|
|
|
|
};
|
|
|
|
|
|
|
|
await overlay.ShowDialog(window);
|
2022-05-15 11:30:15 +00:00
|
|
|
|
|
|
|
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;
|
2023-01-21 01:06:19 +00:00
|
|
|
|
|
|
|
Error.Text = LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.SwkbdMinCharacters, _inputMin);
|
2022-05-15 11:30:15 +00:00
|
|
|
|
|
|
|
_checkLength = length => _inputMin <= length;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Error.IsVisible = true;
|
2023-01-21 01:06:19 +00:00
|
|
|
|
|
|
|
Error.Text = LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.SwkbdMinRangeCharacters, _inputMin, _inputMax);
|
2022-05-15 11:30:15 +00:00
|
|
|
|
|
|
|
_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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|