Skip to content

Commit 1718c3f

Browse files
committed
Merge branch 'main' of tig:migueldeicaza/gui.cs
2 parents 734c854 + 761224c commit 1718c3f

2 files changed

Lines changed: 99 additions & 9 deletions

File tree

‎Terminal.Gui/Views/TextValidateField.cs‎

Lines changed: 95 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,15 +350,15 @@ private void CompileMask ()
350350
/// Text field that validates input through a <see cref="ITextValidateProvider"/>
351351
/// </summary>
352352
/// <typeparam name="T"></typeparam>
353-
public class TextValidateField<T> : View where T : ITextValidateProvider {
353+
public class TextValidateField<T> : View, ITextValidateProvider where T : class {
354354

355355
ITextValidateProvider provider;
356356
int cursorPosition = 0;
357357

358358
/// <summary>
359359
/// Initializes a new instance of the <see cref="TextValidateField{T}"/> class using <see cref="LayoutStyle.Computed"/> positioning.
360360
/// </summary>
361-
public TextValidateField () : this (ustring.Empty)
361+
public TextValidateField ()
362362
{
363363
}
364364

@@ -373,7 +373,7 @@ public TextValidateField (ustring mask) : this (mask, ustring.Empty) { }
373373
/// </summary>
374374
/// <param name="mask"></param>
375375
/// <param name="text">Initial Value</param>
376-
public TextValidateField (ustring mask, ustring text) : base ()
376+
public TextValidateField (ustring mask, ustring text)
377377
{
378378
provider = Activator.CreateInstance (typeof (T)) as ITextValidateProvider;
379379

@@ -408,9 +408,16 @@ public override bool MouseEvent (MouseEvent mouseEvent)
408408
/// </summary>
409409
public new ustring Text {
410410
get {
411+
if (provider == null) {
412+
return ustring.Empty;
413+
}
414+
411415
return provider.Text;
412416
}
413417
set {
418+
if (provider == null) {
419+
return;
420+
}
414421
provider.Text = value;
415422

416423
SetNeedsDisplay ();
@@ -472,6 +479,12 @@ public override void PositionCursor ()
472479
///<inheritdoc/>
473480
public override void Redraw (Rect bounds)
474481
{
482+
if (provider == null) {
483+
Move (0, 0);
484+
Driver.AddStr ("Error: ITextValidateProvider not set!");
485+
return;
486+
}
487+
475488
var bgcolor = !IsValid ? Color.BrightRed : ColorScheme.Focus.Background;
476489
var textColor = new Attribute (ColorScheme.Focus.Foreground, bgcolor);
477490

@@ -571,6 +584,10 @@ bool EndKeyHandler ()
571584
///<inheritdoc/>
572585
public override bool ProcessKey (KeyEvent kb)
573586
{
587+
if (provider == null) {
588+
return true;
589+
}
590+
574591
switch (kb.Key) {
575592
case Key.Home: HomeKeyHandler (); break;
576593
case Key.End: EndKeyHandler (); break;
@@ -598,13 +615,88 @@ public override bool ProcessKey (KeyEvent kb)
598615
return true;
599616
}
600617

618+
/// <summary>
619+
/// Set Cursor position to <paramref name="pos" />.
620+
/// </summary>
621+
/// <param name="pos"></param>
622+
/// <returns>Return first valid position.</returns>
623+
public int Cursor (int pos)
624+
{
625+
return provider.Cursor (pos);
626+
}
627+
628+
/// <summary>
629+
/// First valid position before <paramref name="pos" />.
630+
/// </summary>
631+
/// <param name="pos"></param>
632+
/// <returns>New cursor position if any, otherwise returns <paramref name="pos" /></returns>
633+
public int CursorLeft (int pos)
634+
{
635+
return provider.CursorLeft (pos);
636+
}
637+
638+
/// <summary>
639+
/// First valid position after <paramref name="pos" />.
640+
/// </summary>
641+
/// <param name="pos">Current position.</param>
642+
/// <returns>New cursor position if any, otherwise returns <paramref name="pos" /></returns>
643+
public int CursorRight (int pos)
644+
{
645+
return provider.CursorRight (pos);
646+
}
647+
648+
/// <summary>
649+
/// Find the first valid character position.
650+
/// </summary>
651+
/// <returns>New cursor position.</returns>
652+
public int CursorStart ()
653+
{
654+
return provider.CursorStart ();
655+
}
656+
657+
/// <summary>
658+
/// Find the last valid character position.
659+
/// </summary>
660+
/// <returns>New cursor position.</returns>
661+
public int CursorEnd ()
662+
{
663+
return provider.CursorEnd ();
664+
}
665+
666+
/// <summary>
667+
/// Deletes the current character in <paramref name="pos" />.
668+
/// </summary>
669+
/// <param name="pos"></param>
670+
/// <returns>true if the character was successfully removed, otherwise false.</returns>
671+
public bool Delete (int pos)
672+
{
673+
return provider.Delete (pos);
674+
}
675+
676+
/// <summary>
677+
/// Insert character <paramref name="ch" /> in position <paramref name="pos" />.
678+
/// </summary>
679+
/// <param name="ch"></param>
680+
/// <param name="pos"></param>
681+
/// <returns>true if the character was successfully inserted, otherwise false.</returns>
682+
public bool InsertAt (char ch, int pos)
683+
{
684+
return provider.InsertAt (ch, pos);
685+
}
686+
601687
/// <summary>
602688
/// This property returns true if the input is valid.
603689
/// </summary>
604690
public virtual bool IsValid {
605691
get {
692+
if (provider == null) {
693+
return false;
694+
}
695+
606696
return provider.IsValid;
607697
}
608698
}
699+
700+
public bool Fixed => throw new NotImplementedException ();
609701
}
610702
}

‎UnitTests/TextValidateFieldTests.cs‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -392,17 +392,15 @@ public void Input_With_Validate_On_Input_Set_Text ()
392392
}
393393

394394
[Fact]
395-
public void Empty_Mask_Validates_Everything ()
395+
public void Empty_Mask_Does_Not_Validate ()
396396
{
397-
// Maybe it's not the right behaviour.
398-
399397
var field = new TextValidateField<TextRegexProvider> () {
400398
Width = 20
401399
};
402400

403-
field.ProcessKey (new KeyEvent (Key.D1, new KeyModifiers { }));
404-
Assert.Equal ("1", field.Text);
405-
Assert.True (field.IsValid);
401+
field.ProcessKey (new KeyEvent (Key.D1, new KeyModifiers ()));
402+
Assert.Equal ("", field.Text);
403+
Assert.False (field.IsValid);
406404
}
407405

408406
[Fact]

0 commit comments

Comments
 (0)