Bläddra i källkod

ComboBox supports Dim.Fill() Dim.Percent()

Ross Ferguson 5 år sedan
förälder
incheckning
6f7285cfd4
1 ändrade filer med 25 tillägg och 11 borttagningar
  1. 25 11
      Terminal.Gui/Views/ComboBox.cs

+ 25 - 11
Terminal.Gui/Views/ComboBox.cs

@@ -6,10 +6,11 @@
 //
 //
 
 
 using System;
 using System;
-using System.Linq;
+using System.Collections;
 using System.Collections.Generic;
 using System.Collections.Generic;
+using System.Diagnostics;
+using System.Linq;
 using NStack;
 using NStack;
-using System.Collections;
 
 
 namespace Terminal.Gui {
 namespace Terminal.Gui {
 	/// <summary>
 	/// <summary>
@@ -17,6 +18,7 @@ namespace Terminal.Gui {
 	/// </summary>
 	/// </summary>
 	public class ComboBox : View {
 	public class ComboBox : View {
 
 
+
 		IListDataSource source;
 		IListDataSource source;
 		/// <summary>
 		/// <summary>
 		/// Gets or sets the <see cref="IListDataSource"/> backing this <see cref="ComboBox"/>, enabling custom rendering.
 		/// Gets or sets the <see cref="IListDataSource"/> backing this <see cref="ComboBox"/>, enabling custom rendering.
@@ -56,9 +58,8 @@ namespace Terminal.Gui {
 		///   Client code can hook up to this event, it is
 		///   Client code can hook up to this event, it is
 		///   raised when the selection has been confirmed.
 		///   raised when the selection has been confirmed.
 		/// </remarks>
 		/// </remarks>
-		public event EventHandler<ustring> Changed;
+		public event EventHandler<ustring> SelectedItemChanged;
 
 
-		//IList<string> listsource;
 		IList searchset;
 		IList searchset;
 		ustring text = "";
 		ustring text = "";
 		TextField search;
 		TextField search;
@@ -109,10 +110,11 @@ namespace Terminal.Gui {
 		{
 		{
 			search.TextChanged += Search_Changed;
 			search.TextChanged += Search_Changed;
 
 
+			// On resize
 			LayoutComplete += (LayoutEventArgs a) => {
 			LayoutComplete += (LayoutEventArgs a) => {
 
 
-				search.Width = Frame.Width;
-				listview.Width = Frame.Width - 1;
+				search.Width = Bounds.Width;
+				listview.Width = Bounds.Width - 1;
 			};
 			};
 
 
 			listview.SelectedItemChanged += (ListViewItemEventArgs e) => {
 			listview.SelectedItemChanged += (ListViewItemEventArgs e) => {
@@ -121,7 +123,6 @@ namespace Terminal.Gui {
 					SetValue ((string)searchset [listview.SelectedItem]);
 					SetValue ((string)searchset [listview.SelectedItem]);
 			};
 			};
 
 
-			// TODO: LayoutComplete event breaks cursor up/down. Revert to Application.Loaded 
 			Application.Loaded += (Application.ResizedEventArgs a) => {
 			Application.Loaded += (Application.ResizedEventArgs a) => {
 				// Determine if this view is hosted inside a dialog
 				// Determine if this view is hosted inside a dialog
 				for (View view = this.SuperView; view != null; view = view.SuperView) {
 				for (View view = this.SuperView; view != null; view = view.SuperView) {
@@ -184,7 +185,7 @@ namespace Terminal.Gui {
 			if (e.MouseEvent.Flags != MouseFlags.Button1Clicked)
 			if (e.MouseEvent.Flags != MouseFlags.Button1Clicked)
 				return;
 				return;
 
 
-			SuperView.SetFocus ((View)search);
+			SuperView.SetFocus (search);
 		}
 		}
 
 
 		///<inheritdoc/>
 		///<inheritdoc/>
@@ -198,6 +199,19 @@ namespace Terminal.Gui {
 			return true;
 			return true;
 		}
 		}
 
 
+		/// <summary>
+		/// Invokes the SelectedChanged event if it is defined.
+		/// </summary>
+		/// <returns></returns>
+		public virtual bool OnSelectedChanged ()
+		{
+			// Note: Cannot rely on "listview.SelectedItem != lastSelectedItem" because the list is dynamic. 
+			// So we cannot optimize. Ie: Don't call if not changed
+			SelectedItemChanged?.Invoke (this, search.Text);
+
+			return true;
+		}
+
 		///<inheritdoc/>
 		///<inheritdoc/>
 		public override bool ProcessKey(KeyEvent e)
 		public override bool ProcessKey(KeyEvent e)
 		{
 		{
@@ -215,7 +229,7 @@ namespace Terminal.Gui {
 				SetValue((string)searchset [listview.SelectedItem]);
 				SetValue((string)searchset [listview.SelectedItem]);
 				search.CursorPosition = search.Text.Length;
 				search.CursorPosition = search.Text.Length;
 				Search_Changed (search.Text);
 				Search_Changed (search.Text);
-				Changed?.Invoke (this, text);
+				OnSelectedChanged ();
 
 
 				searchset.Clear();
 				searchset.Clear();
 				listview.Clear ();
 				listview.Clear ();
@@ -244,7 +258,7 @@ namespace Terminal.Gui {
 			if (e.Key == Key.Esc) {
 			if (e.Key == Key.Esc) {
 				this.SetFocus (search);
 				this.SetFocus (search);
 				search.Text = text = "";
 				search.Text = text = "";
-				Changed?.Invoke (this, search.Text);
+				OnSelectedChanged ();
 				return true;
 				return true;
 			}
 			}
 
 
@@ -286,7 +300,7 @@ namespace Terminal.Gui {
 		private void Reset()
 		private void Reset()
 		{
 		{
 			search.Text = text = "";
 			search.Text = text = "";
-			Changed?.Invoke (this, search.Text);
+			OnSelectedChanged();
 
 
 			ResetSearchSet ();
 			ResetSearchSet ();