Przeglądaj źródła

Timefield format with bounds values (#303)

* Implemented lower and upper bounds to TimeField

* Passing old text to the Changed event handler

* Change sepChar from char to string in TimeField

* Changing comparison from ':' to sepChar.ToCharArray () [0]
BDisp 5 lat temu
rodzic
commit
8a502e5f1b
2 zmienionych plików z 61 dodań i 14 usunięć
  1. 4 3
      Terminal.Gui/Views/TextField.cs
  2. 57 11
      Terminal.Gui/Views/TimeField.cs

+ 4 - 3
Terminal.Gui/Views/TextField.cs

@@ -35,7 +35,7 @@ namespace Terminal.Gui {
 		///   Client code can hook up to this event, it is
 		///   raised when the text in the entry changes.
 		/// </remarks>
-		public event EventHandler Changed;
+		public event EventHandler<ustring> Changed;
 
 		/// <summary>
 		///    Public constructor that creates a text field, with layout controlled with X, Y, Width and Height.
@@ -98,7 +98,10 @@ namespace Terminal.Gui {
 			}
 
 			set {
+				ustring oldText = ustring.Make(text);
 				text = TextModel.ToRunes (value);
+				Changed?.Invoke(this, oldText);
+
 				if (point > text.Count)
 					point = Math.Max (text.Count-1, 0);
 
@@ -193,8 +196,6 @@ namespace Terminal.Gui {
 		void SetText (List<Rune> newText)
 		{
 			text = newText;
-			if (Changed != null)
-				Changed (this, EventArgs.Empty);
 		}
 
 		void SetText (IEnumerable<Rune> newText)

+ 57 - 11
Terminal.Gui/Views/TimeField.cs

@@ -1,4 +1,4 @@
-//
+	//
 // TimeField.cs: text entry for time
 //
 // Author: Jörg Preiß
@@ -25,9 +25,9 @@ namespace Terminal.Gui {
 		int longFieldLen = 8;
 		int shortFieldLen = 5;
 		int FieldLen { get { return isShort ? shortFieldLen : longFieldLen; } }
-
-		string longFormat = " hh:mm:ss";
-		string shortFormat = " hh:mm";
+		string sepChar;
+		string longFormat;
+		string shortFormat;
 		string Format { get { return isShort ? shortFormat : longFormat; } }
 
 
@@ -40,9 +40,20 @@ namespace Terminal.Gui {
 		/// <param name="isShort">If true, the seconds are hidden.</param>
 		public TimeField (int x, int y, DateTime time, bool isShort = false) : base (x, y, isShort ? 7 : 10, "")
 		{
+			CultureInfo cultureInfo = CultureInfo.CurrentCulture;
+			sepChar = cultureInfo.DateTimeFormat.TimeSeparator;
+			longFormat = $" HH{sepChar}mm{sepChar}ss";
+			shortFormat = $" HH{sepChar}mm";
 			this.isShort = isShort;
 			CursorPosition = 1;
 			Time = time;
+			Changed += TimeField_Changed;
+		}
+
+		private void TimeField_Changed (object sender, ustring e)
+		{
+			if (!DateTime.TryParseExact (Text.ToString (), Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result))
+				Text = e;
 		}
 
 		/// <summary>
@@ -72,31 +83,66 @@ namespace Terminal.Gui {
 
 		bool SetText (ustring text)
 		{
-			if (!DateTime.TryParseExact (text.ToString (), Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result)) 
+			ustring [] vals = text.Split (ustring.Make (sepChar));
+			bool isValidTime = true;
+			int hour = Int32.Parse (vals [0].ToString ());
+			int minute = Int32.Parse (vals [1].ToString ());
+			int second = isShort ? 0 : Int32.Parse (vals [2].ToString ());
+			if (hour < 0) {
+				isValidTime = false;
+				hour = 0;
+				vals [0] = "0";
+			} else if (hour > 23) {
+				isValidTime = false;
+				hour = 23;
+				vals [0] = "23";
+			}
+			if (minute < 0) {
+				isValidTime = false;
+				minute = 0;
+				vals [1] = "0";
+			} else if (minute > 59) {
+				isValidTime = false;
+				minute = 59;
+				vals [1] = "59";
+			}
+			if (second < 0) {
+				isValidTime = false;
+				second = 0;
+				vals [2] = "0";
+			} else if (second > 59) {
+				isValidTime = false;
+				second = 59;
+				vals [2] = "59";
+			}
+			string time = isShort ? $" {hour,2:00}{sepChar}{minute,2:00}" : $" {hour,2:00}{sepChar}{minute,2:00}{sepChar}{second,2:00}";
+			Text = time;
+
+			if (!DateTime.TryParseExact (text.ToString (), Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result) ||
+				!isValidTime) 
 				return false;
-			Text = text;
 			return true;
 		}
 
 		void IncCursorPosition ()
 		{
-			if (CursorPosition == FieldLen) 
+			if (CursorPosition == FieldLen)
 				return;
-			if (Text [++CursorPosition] == ':') 
+			if (Text [++CursorPosition] == sepChar.ToCharArray () [0])
 				CursorPosition++;
 		}
 
 		void DecCursorPosition ()
 		{
-			if (CursorPosition == 1) 
+			if (CursorPosition == 1)
 				return;
-			if (Text [--CursorPosition] == ':') 
+			if (Text [--CursorPosition] == sepChar.ToCharArray () [0])
 				CursorPosition--;
 		}
 
 		void AdjCursorPosition ()
 		{
-			if (Text [CursorPosition] == ':') 
+			if (Text [CursorPosition] == sepChar.ToCharArray () [0])
 				CursorPosition++;
 		}