瀏覽代碼

Changed from DateTime to TimeSpan. Changed from EventHandler to Action.

BDisp 5 年之前
父節點
當前提交
d32c631dfd

+ 2 - 2
Example/demo.cs

@@ -183,8 +183,8 @@ static class Demo {
 			scrollView2,
 			tf,
 			new Button (10, 19, "Cancel"),
-			new TimeField (3, 20, DateTime.Now),
-			new TimeField (23, 20, DateTime.Now, true),
+			new TimeField (3, 20, DateTime.Now.TimeOfDay),
+			new TimeField (23, 20, DateTime.Now.TimeOfDay, true),
 			new DateField (3, 22, DateTime.Now),
 			new DateField (23, 22, DateTime.Now, true),
 			progress,

+ 9 - 10
Terminal.Gui/Views/DateField.cs

@@ -38,7 +38,7 @@ namespace Terminal.Gui {
 		/// <remarks>
 		///   The passed <see cref="EventArgs"/> is a <see cref="DateTimeEventArgs"/> containing the old, new value and format.
 		/// </remarks>
-		public event EventHandler<DateTimeEventArgs> DateChanged;
+		public event Action<DateTimeEventArgs<DateTime>> DateChanged;
 
 		/// <summary>
 		///    Initializes a new instance of <see cref="DateField"/> using <see cref="LayoutStyle.Absolute"/> layout.
@@ -76,8 +76,7 @@ namespace Terminal.Gui {
 			longFormat = GetLongFormat (cultureInfo.DateTimeFormat.ShortDatePattern);
 			shortFormat = GetShortFormat (longFormat);
 			CursorPosition = 1;
-			this.date = date;
-			Text = date.ToString (Format);
+			Date = date;
 			Changed += DateField_Changed;
 		}
 
@@ -131,7 +130,7 @@ namespace Terminal.Gui {
 				var oldData = date;
 				date = value;
 				this.Text = value.ToString (Format);
-				var args = new DateTimeEventArgs (oldData, value, Format);
+				var args = new DateTimeEventArgs<DateTime> (oldData, value, Format);
 				if (oldData != value) {
 					OnDateChanged (args);
 				}
@@ -374,25 +373,25 @@ namespace Terminal.Gui {
 		/// Virtual method that will invoke the <see cref="DateChanged"/>  with a <see cref="DateTimeEventArgs"/>.
 		/// </summary>
 		/// <param name="args">The arguments of the <see cref="DateTimeEventArgs"/></param>
-		public virtual void OnDateChanged (DateTimeEventArgs args)
+		public virtual void OnDateChanged (DateTimeEventArgs<DateTime> args)
 		{
-			DateChanged?.Invoke (this, args);
+			DateChanged?.Invoke (args);
 		}
 	}
 
 	/// <summary>
 	/// Handled the <see cref="EventArgs"/> for <see cref="DateField"/> or <see cref="TimeField"/> events.
 	/// </summary>
-	public class DateTimeEventArgs : EventArgs {
+	public class DateTimeEventArgs<T> : EventArgs {
 		/// <summary>
 		/// The old <see cref="DateField"/> or <see cref="TimeField"/> value.
 		/// </summary>
-		public DateTime OldValue {get;}
+		public T OldValue {get;}
 
 		/// <summary>
 		/// The new <see cref="DateField"/> or <see cref="TimeField"/> value.
 		/// </summary>
-		public DateTime NewValue { get; }
+		public T NewValue { get; }
 
 		/// <summary>
 		/// The <see cref="DateField"/> or <see cref="TimeField"/> format.
@@ -405,7 +404,7 @@ namespace Terminal.Gui {
 		/// <param name="oldValue">The old <see cref="DateField"/> or <see cref="TimeField"/> value.</param>
 		/// <param name="newValue">The new <see cref="DateField"/> or <see cref="TimeField"/> value.</param>
 		/// <param name="format">The <see cref="DateField"/> or <see cref="TimeField"/> format.</param>
-		public DateTimeEventArgs (DateTime oldValue, DateTime newValue, string format)
+		public DateTimeEventArgs (T oldValue, T newValue, string format)
 		{
 			OldValue = oldValue;
 			NewValue = newValue;

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

@@ -132,6 +132,10 @@ namespace Terminal.Gui {
 					return;
 
 				var oldText = ustring.Make (text);
+
+				if (oldText == value)
+					return;
+
 				text = TextModel.ToRunes (value);
 				if (!Secret && !isFromHistory) {
 					if (historyText == null)

+ 16 - 17
Terminal.Gui/Views/TimeField.cs

@@ -17,7 +17,7 @@ namespace Terminal.Gui {
 	///   The <see cref="TimeField"/> <see cref="View"/> provides time editing functionality with mouse support.
 	/// </remarks>
 	public class TimeField : TextField {
-		DateTime time;
+		TimeSpan time;
 		bool isShort;
 
 		int longFieldLen = 8;
@@ -38,7 +38,7 @@ namespace Terminal.Gui {
 		/// <remarks>
 		///   The passed <see cref="EventArgs"/> is a <see cref="DateTimeEventArgs"/> containing the old, new value and format.
 		/// </remarks>
-		public event EventHandler<DateTimeEventArgs> TimeChanged;
+		public event Action<DateTimeEventArgs<TimeSpan>> TimeChanged;
 
 		/// <summary>
 		///    Initializes a new instance of <see cref="TimeField"/> using <see cref="LayoutStyle.Absolute"/> positioning.
@@ -47,7 +47,7 @@ namespace Terminal.Gui {
 		/// <param name="y">The y coordinate.</param>
 		/// <param name="time">Initial time.</param>
 		/// <param name="isShort">If true, the seconds are hidden. Sets the <see cref="IsShortFormat"/> property.</param>
-		public TimeField (int x, int y, DateTime time, bool isShort = false) : base (x, y, isShort ? 7 : 10, "")
+		public TimeField (int x, int y, TimeSpan time, bool isShort = false) : base (x, y, isShort ? 7 : 10, "")
 		{
 			this.isShort = isShort;
 			Initialize (time);
@@ -57,7 +57,7 @@ namespace Terminal.Gui {
 		///    Initializes a new instance of <see cref="TimeField"/> using <see cref="LayoutStyle.Computed"/> positioning.
 		/// </summary>
 		/// <param name="time">Initial time</param>
-		public TimeField (DateTime time) : base (string.Empty)
+		public TimeField (TimeSpan time) : base (string.Empty)
 		{
 			this.isShort = true;
 			Width = FieldLen + 2;
@@ -67,24 +67,23 @@ namespace Terminal.Gui {
 		/// <summary>
 		///    Initializes a new instance of <see cref="TimeField"/> using <see cref="LayoutStyle.Computed"/> positioning.
 		/// </summary>
-		public TimeField () : this (time: DateTime.MinValue) { }
+		public TimeField () : this (time: TimeSpan.MinValue) { }
 
-		void Initialize (DateTime time)
+		void Initialize (TimeSpan time)
 		{
 			CultureInfo cultureInfo = CultureInfo.CurrentCulture;
 			sepChar = cultureInfo.DateTimeFormat.TimeSeparator;
-			longFormat = $" HH{sepChar}mm{sepChar}ss";
-			shortFormat = $" HH{sepChar}mm";
+			longFormat = $" hh\\{sepChar}mm\\{sepChar}ss";
+			shortFormat = $" hh\\{sepChar}mm";
 			CursorPosition = 1;
-			this.time = time;
-			Text = time.ToString (Format);
+			Time = time;
 			Changed += TimeField_Changed;
 		}
 
 		void TimeField_Changed (object sender, ustring e)
 		{
 			try {
-				if (!DateTime.TryParseExact (Text.ToString (), Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result))
+				if (!TimeSpan.TryParseExact (Text.ToString ().Trim (), Format.Trim (), CultureInfo.CurrentCulture, TimeSpanStyles.None, out TimeSpan result))
 					Text = e;
 			} catch (Exception) {
 				Text = e;
@@ -96,7 +95,7 @@ namespace Terminal.Gui {
 		/// </summary>
 		/// <remarks>
 		/// </remarks>
-		public DateTime Time {
+		public TimeSpan Time {
 			get {
 				return time;
 			}
@@ -106,8 +105,8 @@ namespace Terminal.Gui {
 
 				var oldTime = time;
 				time = value;
-				this.Text = value.ToString (Format);
-				var args = new DateTimeEventArgs (oldTime, value, Format);
+				this.Text = " " + value.ToString (Format.Trim ());
+				var args = new DateTimeEventArgs<TimeSpan> (oldTime, value, Format);
 				if (oldTime != value) {
 					OnTimeChanged (args);
 				}
@@ -184,7 +183,7 @@ namespace Terminal.Gui {
 			}
 			string t = isShort ? $" {hour,2:00}{sepChar}{minute,2:00}" : $" {hour,2:00}{sepChar}{minute,2:00}{sepChar}{second,2:00}";
 
-			if (!DateTime.TryParseExact (t, Format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime result) ||
+			if (!TimeSpan.TryParseExact (t.Trim (), Format.Trim (), CultureInfo.CurrentCulture, TimeSpanStyles.None, out TimeSpan result) ||
 				!isValidTime)
 				return false;
 			Time = result;
@@ -292,9 +291,9 @@ namespace Terminal.Gui {
 		/// Virtual method that will invoke the <see cref="TimeChanged"/>  with a <see cref="DateTimeEventArgs"/>.
 		/// </summary>
 		/// <param name="args">The arguments of the <see cref="DateTimeEventArgs"/></param>
-		public virtual void OnTimeChanged (DateTimeEventArgs args)
+		public virtual void OnTimeChanged (DateTimeEventArgs<TimeSpan> args)
 		{
-			TimeChanged?.Invoke (this, args);
+			TimeChanged?.Invoke (args);
 		}
 	}
 }

+ 4 - 3
UICatalog/Scenarios/Text.cs

@@ -1,4 +1,5 @@
-using System.Text;
+using System;
+using System.Text;
 using Terminal.Gui;
 
 namespace UICatalog {
@@ -45,8 +46,8 @@ namespace UICatalog {
 			};
 			Win.Add (dateField);
 
-			var timeField = new TimeField (System.DateTime.Now) {
-				X = Pos.Right(dateField) + 5,
+			var timeField = new TimeField (DateTime.Now.TimeOfDay) {
+				X = Pos.Right (dateField) + 5,
 				Y = Pos.Bottom (hexView) + 1,
 				Width = Dim.Percent (40),
 				ColorScheme = Colors.Dialog,

+ 5 - 5
UICatalog/Scenarios/TimeAndDate.cs

@@ -15,7 +15,7 @@ namespace UICatalog {
 
 		public override void Setup ()
 		{
-			var longTime = new TimeField (DateTime.Now) {
+			var longTime = new TimeField (DateTime.Now.TimeOfDay) {
 				X = Pos.Center (),
 				Y = 2,
 				IsShortFormat = false,
@@ -24,9 +24,9 @@ namespace UICatalog {
 			longTime.TimeChanged += TimeChanged;
 			Win.Add (longTime);
 
-			var shortTime = new TimeField (DateTime.Now) {
+			var shortTime = new TimeField (DateTime.Now.TimeOfDay) {
 				X = Pos.Center (),
-				Y = Pos.Bottom(longTime) + 1,
+				Y = Pos.Bottom (longTime) + 1,
 				IsShortFormat = true,
 				ReadOnly = false,
 			};
@@ -106,14 +106,14 @@ namespace UICatalog {
 			});
 		}
 
-		private void TimeChanged (object sender, DateTimeEventArgs e)
+		private void TimeChanged (DateTimeEventArgs<TimeSpan> e)
 		{
 			lblOldTime.Text = $"Old Time: {e.OldValue}";
 			lblNewTime.Text = $"New Time: {e.NewValue}";
 			lblTimeFmt.Text = $"Time Format: {e.Format}";
 		}
 
-		private void DateChanged (object sender, DateTimeEventArgs e)
+		private void DateChanged (DateTimeEventArgs<DateTime> e)
 		{
 			lblOldDate.Text = $"Old Date: {e.OldValue}";
 			lblNewDate.Text = $"New Date: {e.NewValue}";