NetMaskedTextProvider.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System.ComponentModel;
  2. namespace Terminal.Gui.Views;
  3. /// <summary>
  4. /// .Net MaskedTextProvider Provider for TextValidateField.
  5. /// <para></para>
  6. /// <para>
  7. /// <a
  8. /// href="https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.maskedtextprovider?view=net-5.0">
  9. /// Wrapper around MaskedTextProvider
  10. /// </a>
  11. /// </para>
  12. /// <para>
  13. /// <a
  14. /// href="https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.maskedtextbox.mask?view=net-5.0">
  15. /// Masking elements
  16. /// </a>
  17. /// </para>
  18. /// </summary>
  19. public class NetMaskedTextProvider : ITextValidateProvider
  20. {
  21. private MaskedTextProvider? _provider;
  22. /// <summary>Empty Constructor</summary>
  23. public NetMaskedTextProvider (string mask) { Mask = mask; }
  24. /// <summary>Mask property</summary>
  25. public string Mask
  26. {
  27. get => _provider!.Mask;
  28. set
  29. {
  30. string current = _provider != null
  31. ? _provider.ToString (false, false)
  32. : string.Empty;
  33. _provider = new (value == string.Empty ? "&&&&&&" : value);
  34. if (!string.IsNullOrEmpty (current))
  35. {
  36. _provider.Set (current);
  37. }
  38. }
  39. }
  40. /// <inheritdoc/>
  41. public event EventHandler<EventArgs<string>>? TextChanged;
  42. /// <inheritdoc/>
  43. public string Text
  44. {
  45. get => _provider!.ToString ();
  46. set => _provider!.Set (value);
  47. }
  48. /// <inheritdoc/>
  49. public bool IsValid => _provider!.MaskCompleted;
  50. /// <inheritdoc/>
  51. public bool Fixed => true;
  52. /// <inheritdoc/>
  53. public string DisplayText => _provider!.ToDisplayString ();
  54. /// <inheritdoc/>
  55. public int Cursor (int pos)
  56. {
  57. if (pos < 0)
  58. {
  59. return CursorStart ();
  60. }
  61. if (pos > _provider!.Length)
  62. {
  63. return CursorEnd ();
  64. }
  65. int p = _provider.FindEditPositionFrom (pos, false);
  66. if (p == -1)
  67. {
  68. p = _provider.FindEditPositionFrom (pos, true);
  69. }
  70. return p;
  71. }
  72. /// <inheritdoc/>
  73. public int CursorStart ()
  74. {
  75. return _provider!.IsEditPosition (0)
  76. ? 0
  77. : _provider.FindEditPositionFrom (0, true);
  78. }
  79. /// <inheritdoc/>
  80. public int CursorEnd ()
  81. {
  82. return _provider!.IsEditPosition (_provider.Length - 1)
  83. ? _provider.Length - 1
  84. : _provider.FindEditPositionFrom (_provider.Length, false);
  85. }
  86. /// <inheritdoc/>
  87. public int CursorLeft (int pos)
  88. {
  89. int c = _provider!.FindEditPositionFrom (pos - 1, false);
  90. return c == -1 ? pos : c;
  91. }
  92. /// <inheritdoc/>
  93. public int CursorRight (int pos)
  94. {
  95. int c = _provider!.FindEditPositionFrom (pos + 1, true);
  96. return c == -1 ? pos : c;
  97. }
  98. /// <inheritdoc/>
  99. public bool Delete (int pos)
  100. {
  101. string oldValue = Text;
  102. bool result = _provider!.Replace (' ', pos); // .RemoveAt (pos);
  103. if (result)
  104. {
  105. OnTextChanged (new (in oldValue));
  106. }
  107. return result;
  108. }
  109. /// <inheritdoc/>
  110. public bool InsertAt (char ch, int pos)
  111. {
  112. string oldValue = Text;
  113. bool result = _provider!.Replace (ch, pos);
  114. if (result)
  115. {
  116. OnTextChanged (new (in oldValue));
  117. }
  118. return result;
  119. }
  120. /// <inheritdoc/>
  121. public void OnTextChanged (EventArgs<string> args) { TextChanged?.Invoke (this, args); }
  122. }