瀏覽代碼

Merge pull request #1005 from BDisp/textchanging-event

Fixes #998. Added a cancelable TextChanging event to prevent the TextChanged event being called if the changing is canceled.
Charlie Kindel 4 年之前
父節點
當前提交
9791ab2f8d
共有 2 個文件被更改,包括 46 次插入2 次删除
  1. 1 1
      Terminal.Gui/Core/View.cs
  2. 45 1
      Terminal.Gui/Views/TextField.cs

+ 1 - 1
Terminal.Gui/Core/View.cs

@@ -1908,7 +1908,7 @@ namespace Terminal.Gui {
 
 		/// <summary>
 		/// Used by <see cref="Text"/> to resize the view's <see cref="Bounds"/> with the <see cref="TextFormatter.Size"/>.
-		/// Setting <see cref="Auto"/> to true only work if the <see cref="Width"/> and <see cref="Height"/> are null or
+		/// Setting <see cref="AutoSize"/> to true only work if the <see cref="Width"/> and <see cref="Height"/> are null or
 		///   <see cref="LayoutStyle.Absolute"/> values and doesn't work with <see cref="LayoutStyle.Computed"/> layout,
 		///   to avoid breaking the <see cref="Pos"/> and <see cref="Dim"/> settings.
 		/// </summary>

+ 45 - 1
Terminal.Gui/Views/TextField.cs

@@ -31,6 +31,11 @@ namespace Terminal.Gui {
 		/// </summary>
 		public bool ReadOnly { get; set; } = false;
 
+		/// <summary>
+		/// Changing event, raised before the <see cref="Text"/> changes and can be canceled or changing the new text.
+		/// </summary>
+		public event Action<TextChangingEventArgs> TextChanging;
+
 		/// <summary>
 		///   Changed event, raised when the text has changed.
 		/// </summary>
@@ -136,7 +141,11 @@ namespace Terminal.Gui {
 				if (oldText == value)
 					return;
 
-				text = TextModel.ToRunes (value);
+				var newText = OnTextChanging (value);
+				if (newText.Cancel) {
+					return;
+				}
+				text = TextModel.ToRunes (newText.NewText);
 				if (!Secret && !isFromHistory) {
 					if (historyText == null)
 						historyText = new List<ustring> () { oldText };
@@ -921,5 +930,40 @@ namespace Terminal.Gui {
 			ClearAllSelection ();
 			SetNeedsDisplay ();
 		}
+
+		/// <summary>
+		/// Virtual method that invoke the <see cref="TextChanging"/> event if it's defined.
+		/// </summary>
+		/// <param name="newText">The new text to be replaced.</param>
+		/// <returns>Returns the <see cref="TextChangingEventArgs"/></returns>
+		public virtual TextChangingEventArgs OnTextChanging (ustring newText)
+		{
+			var ev = new TextChangingEventArgs (newText);
+			TextChanging?.Invoke (ev);
+			return ev;
+		}
+	}
+
+	/// <summary>
+	/// An <see cref="EventArgs"/> which allows passing a cancelable new text value event.
+	/// </summary>
+	public class TextChangingEventArgs : EventArgs {
+		/// <summary>
+		/// The new text to be replaced.
+		/// </summary>
+		public ustring NewText { get; set; }
+		/// <summary>
+		/// Flag which allows to cancel the new text value.
+		/// </summary>
+		public bool Cancel { get; set; }
+
+		/// <summary>
+		/// Initializes a new instance of <see cref="TextChangingEventArgs"/>
+		/// </summary>
+		/// <param name="newText">The new <see cref="TextField.Text"/> to be replaced.</param>
+		public TextChangingEventArgs (ustring newText)
+		{
+			NewText = newText;
+		}
 	}
 }