@@ -81,16 +81,14 @@ public interface ITextValidateProvider {
81
81
bool IsValid { get ; }
82
82
83
83
/// <summary>
84
- /// Set the input text, and get the formatted string for display .
84
+ /// Set the input text and get the current value .
85
85
/// </summary>
86
86
ustring Text { get ; set ; }
87
87
88
88
/// <summary>
89
- /// Mask used for validation.
90
- /// Not always a mask, can by a regex expression.
91
- /// TODO: Maybe we can change the name.
89
+ /// Gets the formatted string for display.
92
90
/// </summary>
93
- ustring Mask { get ; set ; }
91
+ ustring DisplayText { get ; }
94
92
}
95
93
96
94
//////////////////////////////////////////////////////////////////////////////
@@ -107,33 +105,37 @@ public interface ITextValidateProvider {
107
105
/// </summary>
108
106
public class NetMaskedTextProvider : ITextValidateProvider {
109
107
MaskedTextProvider provider ;
110
- string text ;
111
108
112
109
/// <summary>
113
110
/// Empty Constructor
114
111
/// </summary>
115
- public NetMaskedTextProvider ( ) { }
112
+ public NetMaskedTextProvider ( string mask )
113
+ {
114
+ Mask = mask ;
115
+ }
116
116
117
- ///<inheritdoc/>
117
+ /// <summary>
118
+ /// Mask property
119
+ /// </summary>
118
120
public ustring Mask {
119
121
get {
120
122
return provider ? . Mask ;
121
123
}
122
124
set {
125
+ var current = provider != null ? provider . ToString ( false , false ) : string . Empty ;
123
126
provider = new MaskedTextProvider ( value == ustring . Empty ? "&&&&&&" : value . ToString ( ) ) ;
124
- if ( string . IsNullOrEmpty ( text ) == false ) {
125
- provider . Set ( text ) ;
127
+ if ( string . IsNullOrEmpty ( current ) == false ) {
128
+ provider . Set ( current ) ;
126
129
}
127
130
}
128
131
}
129
132
130
133
///<inheritdoc/>
131
134
public ustring Text {
132
135
get {
133
- return provider . ToDisplayString ( ) ;
136
+ return provider . ToString ( ) ;
134
137
}
135
138
set {
136
- text = value . ToString ( ) ;
137
139
provider . Set ( value . ToString ( ) ) ;
138
140
}
139
141
}
@@ -144,6 +146,9 @@ public ustring Text {
144
146
///<inheritdoc/>
145
147
public bool Fixed => true ;
146
148
149
+ ///<inheritdoc/>
150
+ public ustring DisplayText => provider . ToDisplayString ( ) ;
151
+
147
152
///<inheritdoc/>
148
153
public int Cursor ( int pos )
149
154
{
@@ -212,20 +217,25 @@ public bool InsertAt (char ch, int pos)
212
217
public class TextRegexProvider : ITextValidateProvider {
213
218
Regex regex ;
214
219
List < Rune > text ;
215
- List < Rune > mask ;
220
+ List < Rune > pattern ;
216
221
217
222
/// <summary>
218
- /// Empty Constructor
223
+ /// Empty Constructor.
219
224
/// </summary>
220
- public TextRegexProvider ( ) { }
225
+ public TextRegexProvider ( string pattern )
226
+ {
227
+ Pattern = pattern ;
228
+ }
221
229
222
- ///<inheritdoc/>
223
- public ustring Mask {
230
+ /// <summary>
231
+ /// Regex pattern property.
232
+ /// </summary>
233
+ public ustring Pattern {
224
234
get {
225
- return ustring . Make ( mask ) ;
235
+ return ustring . Make ( pattern ) ;
226
236
}
227
237
set {
228
- mask = value . ToRuneList ( ) ;
238
+ pattern = value . ToRuneList ( ) ;
229
239
CompileMask ( ) ;
230
240
SetupText ( ) ;
231
241
}
@@ -242,6 +252,9 @@ public ustring Text {
242
252
}
243
253
}
244
254
255
+ ///<inheritdoc/>
256
+ public ustring DisplayText => Text ;
257
+
245
258
///<inheritdoc/>
246
259
public bool IsValid {
247
260
get {
@@ -257,6 +270,7 @@ public bool IsValid {
257
270
/// </summary>
258
271
public bool ValidateOnInput { get ; set ; } = true ;
259
272
273
+
260
274
bool Validate ( List < Rune > text )
261
275
{
262
276
var match = regex . Match ( ustring . Make ( text ) . ToString ( ) ) ;
@@ -340,7 +354,7 @@ void SetupText ()
340
354
/// </summary>
341
355
private void CompileMask ( )
342
356
{
343
- regex = new Regex ( ustring . Make ( mask ) . ToString ( ) , RegexOptions . Compiled ) ;
357
+ regex = new Regex ( ustring . Make ( pattern ) . ToString ( ) , RegexOptions . Compiled ) ;
344
358
}
345
359
}
346
360
#endregion
@@ -349,58 +363,66 @@ private void CompileMask ()
349
363
/// <summary>
350
364
/// Text field that validates input through a <see cref="ITextValidateProvider"/>
351
365
/// </summary>
352
- /// <typeparam name="T"></typeparam>
353
- public class TextValidateField < T > : View , ITextValidateProvider where T : class {
366
+ public class TextValidateField : View {
354
367
355
368
ITextValidateProvider provider ;
356
369
int cursorPosition = 0 ;
357
370
358
371
/// <summary>
359
- /// Initializes a new instance of the <see cref="TextValidateField{T} "/> class using <see cref="LayoutStyle.Computed"/> positioning.
372
+ /// Initializes a new instance of the <see cref="TextValidateField"/> class using <see cref="LayoutStyle.Computed"/> positioning.
360
373
/// </summary>
361
- public TextValidateField ( )
374
+ public TextValidateField ( ) : this ( null )
362
375
{
363
376
}
364
377
365
378
/// <summary>
366
- /// Initializes a new instance of the <see cref="TextValidateField{T}"></see> class using <see cref="LayoutStyle.Computed"/> positioning.
379
+ /// Initializes a new instance of the <see cref="TextValidateField"/> class using <see cref="LayoutStyle.Computed"/> positioning.
367
380
/// </summary>
368
- /// <param name="mask">Mask</param>
369
- public TextValidateField ( ustring mask ) : this ( mask , ustring . Empty ) { }
370
-
371
- /// <summary>
372
- /// Initializes a new instance of the <see cref="TextValidateField{T}"/> class using <see cref="LayoutStyle.Computed"/> positioning.
373
- /// </summary>
374
- /// <param name="mask"></param>
375
- /// <param name="text">Initial Value</param>
376
- public TextValidateField ( ustring mask , ustring text )
381
+ public TextValidateField ( ITextValidateProvider provider )
377
382
{
378
- provider = Activator . CreateInstance ( typeof ( T ) ) as ITextValidateProvider ;
383
+ if ( provider != null ) {
384
+ Provider = provider ;
385
+ }
379
386
380
- Mask = mask ;
381
- Text = text ;
387
+ Initialize ( ) ;
388
+ }
382
389
383
- this . Width = text == ustring . Empty ? 20 : Text . Length ;
384
- this . Height = 1 ;
385
- this . CanFocus = true ;
390
+ void Initialize ( )
391
+ {
392
+ Height = 1 ;
393
+ CanFocus = true ;
386
394
}
387
395
388
396
/// <summary>
389
- /// Get the Provider
397
+ /// Provider
390
398
/// </summary>
391
- public T Provider => ( T ) provider ;
399
+ public ITextValidateProvider Provider {
400
+ get => provider ;
401
+ set {
402
+ provider = value ;
403
+ if ( provider . Fixed == true ) {
404
+ this . Width = provider . DisplayText == ustring . Empty ? 10 : Text . Length ;
405
+ }
406
+ HomeKeyHandler ( ) ;
407
+ SetNeedsDisplay ( ) ;
408
+ }
409
+ }
392
410
393
411
///<inheritdoc/>
394
412
public override bool MouseEvent ( MouseEvent mouseEvent )
395
413
{
396
- var c = provider . Cursor ( mouseEvent . X - GetMargins ( Frame . Width ) . left ) ;
397
- if ( provider . Fixed == false && TextAlignment == TextAlignment . Right && Text . Length > 0 ) {
398
- c += 1 ;
414
+ if ( mouseEvent . Flags . HasFlag ( MouseFlags . Button1Pressed ) ) {
415
+
416
+ var c = provider . Cursor ( mouseEvent . X - GetMargins ( Frame . Width ) . left ) ;
417
+ if ( provider . Fixed == false && TextAlignment == TextAlignment . Right && Text . Length > 0 ) {
418
+ c += 1 ;
419
+ }
420
+ cursorPosition = c ;
421
+ SetFocus ( ) ;
422
+ SetNeedsDisplay ( ) ;
423
+ return true ;
399
424
}
400
- cursorPosition = c ;
401
- SetFocus ( ) ;
402
- SetNeedsDisplay ( ) ;
403
- return true ;
425
+ return false ;
404
426
}
405
427
406
428
/// <summary>
@@ -424,22 +446,6 @@ public override bool MouseEvent (MouseEvent mouseEvent)
424
446
}
425
447
}
426
448
427
- /// <summary>
428
- /// Mask
429
- /// </summary>
430
- public ustring Mask {
431
- get {
432
- return provider . Mask ;
433
- }
434
- set {
435
- provider . Mask = value ;
436
-
437
- cursorPosition = provider . CursorStart ( ) ;
438
-
439
- SetNeedsDisplay ( ) ;
440
- }
441
- }
442
-
443
449
///inheritdoc/>
444
450
public override void PositionCursor ( )
445
451
{
@@ -501,8 +507,8 @@ public override void Redraw (Rect bounds)
501
507
// Content
502
508
Driver . SetAttribute ( textColor ) ;
503
509
// Content
504
- for ( int i = 0 ; i < provider . Text . Length ; i ++ ) {
505
- Driver . AddRune ( provider . Text [ i ] ) ;
510
+ for ( int i = 0 ; i < provider . DisplayText . Length ; i ++ ) {
511
+ Driver . AddRune ( provider . DisplayText [ i ] ) ;
506
512
}
507
513
508
514
// Right Margin
@@ -615,75 +621,6 @@ public override bool ProcessKey (KeyEvent kb)
615
621
return true ;
616
622
}
617
623
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
-
687
624
/// <summary>
688
625
/// This property returns true if the input is valid.
689
626
/// </summary>
@@ -696,7 +633,5 @@ public virtual bool IsValid {
696
633
return provider . IsValid ;
697
634
}
698
635
}
699
-
700
- public bool Fixed => throw new NotImplementedException ( ) ;
701
636
}
702
637
}
0 commit comments