ryujinx/Ryujinx/Ui/InputDialog.cs
mageven c11855565e
Implement Software Keyboard GTK frontend (#1434)
* Implement SwKbd GUI

* Relocate UI handler to Emu Context from Config

Also create a common interface for UI handlers in the context and specialize for Gtk

Add basic input length validation in InputDialog

* Add Transfer Memory support to AppletCreator

Read Initial Text for SwKbd using Transfer Memory

* Improve InputDialog widget

Improve length validation
Has extra label to show validition info
Handle potential errors and log them

* Misc improvements

* Improve string validation
* Improve error handling
* Remove tuple in struct
* Address formatting nits

* Add proper Cancel functionality

Also handle GUI errors in UI handler

* Address jD's comments

* Fix _uiHandler init

* Address AcK's comments
2020-08-03 03:30:58 +02:00

69 lines
2.4 KiB
C#

using Gtk;
using System;
namespace Ryujinx.Ui
{
public class InputDialog : MessageDialog
{
private int _inputMin, _inputMax;
private Predicate<int> _checkLength;
private Label _validationInfo;
public Entry InputEntry { get; }
public Button OkButton { get; }
public Button CancelButton { get; }
public InputDialog(Window parent)
: base(parent, DialogFlags.Modal | DialogFlags.DestroyWithParent, MessageType.Question, ButtonsType.None, null)
{
SetDefaultSize(300, 0);
_validationInfo = new Label() { Visible = false };
InputEntry = new Entry() { Visible = true };
InputEntry.Activated += (object sender, EventArgs e) => { if (OkButton.IsSensitive) Respond(ResponseType.Ok); };
InputEntry.Changed += OnInputChanged;
OkButton = (Button)AddButton("OK", ResponseType.Ok);
CancelButton = (Button)AddButton("Cancel", ResponseType.Cancel);
((Box)MessageArea).PackEnd(_validationInfo, true, true, 0);
((Box)MessageArea).PackEnd(InputEntry, true, true, 4);
SetInputLengthValidation(0, int.MaxValue); // disable by default
}
public void SetInputLengthValidation(int min, int max)
{
_inputMin = Math.Min(min, max);
_inputMax = Math.Max(min, max);
_validationInfo.Visible = false;
if (_inputMin <= 0 && _inputMax == int.MaxValue) // disable
{
_validationInfo.Visible = false;
_checkLength = (length) => true;
}
else if (_inputMin > 0 && _inputMax == int.MaxValue)
{
_validationInfo.Visible = true;
_validationInfo.Markup = $"<i>Must be at least {_inputMin} characters long</i>";
_checkLength = (length) => _inputMin <= length;
}
else
{
_validationInfo.Visible = true;
_validationInfo.Markup = $"<i>Must be {_inputMin}-{_inputMax} characters long</i>";
_checkLength = (length) => _inputMin <= length && length <= _inputMax;
}
OnInputChanged(this, EventArgs.Empty);
}
private void OnInputChanged(object sender, EventArgs e)
{
OkButton.Sensitive = _checkLength(InputEntry.Text.Length);
}
}
}