浏览代码

AutoHide option. List and text view synced.

Ross Ferguson 5 年之前
父节点
当前提交
282cf64500
共有 2 个文件被更改,包括 34 次插入33 次删除
  1. 3 1
      Example/demo.cs
  2. 31 32
      Terminal.Gui/Views/TextFieldAutoComplete.cs

+ 3 - 1
Example/demo.cs

@@ -415,7 +415,9 @@ static class Demo {
 
 	static void TextFieldAutoCompleteDemo ()
 	{
-		var items = Directory.GetFiles (@"..\..\..\Terminal.Gui", "*.cs", SearchOption.AllDirectories).Select (x => Path.GetFileName (x)).ToList ();
+		var items = Directory.GetFiles (@"..\..\..\Terminal.Gui", "*.cs", SearchOption.AllDirectories)
+						.Select (x => Path.GetFileName (x)).Where(x => !x.StartsWith(".")).Distinct().OrderBy(x => x).ToList ();
+
 		var list = new TextFieldAutoComplete (0, 0, 36, 7, items);
 		list.Changed += (object sender, ustring text) => { Application.RequestStop (); };
 

+ 31 - 32
Terminal.Gui/Views/TextFieldAutoComplete.cs

@@ -4,9 +4,6 @@
 // Author:
 //   Ross Ferguson ([email protected])
 //
-// TODO:
-//   * Completion list auto appears when hosted directly in a Window as opposed to a dialog
-//
 
 using System;
 using System.Linq;
@@ -34,6 +31,7 @@ namespace Terminal.Gui {
 		readonly ListView listview;
 		readonly int height;
 		readonly int width;
+		readonly bool isAutoHide;
 
 		/// <summary>
 		/// Public constructor
@@ -43,11 +41,15 @@ namespace Terminal.Gui {
 		/// <param name="w">The width</param>		
 		/// <param name="h">The height</param>
 		/// <param name="source">Auto completetion source</param>
-		public TextFieldAutoComplete(int x, int y, int w, int h, IList<string> source)
+		/// <param name="autoHide">Completetion list hidden until start of typing. Use when hosting in Window as opposed to a Dialog</param>
+		public TextFieldAutoComplete(int x, int y, int w, int h, IList<string> source, bool autoHide = false)
 		{
-			listsource = searchset = source;
+			listsource = source;
+			isAutoHide = autoHide;
+			searchset = isAutoHide ? new List<string> () : listsource;
 			height = h;
 			width = w;
+			isAutoHide = autoHide;
 			search = new TextField(x, y, w, "");
 			search.Changed += Search_Changed;
 
@@ -56,6 +58,10 @@ namespace Terminal.Gui {
 				LayoutStyle = LayoutStyle.Computed,
 				ColorScheme = Colors.Dialog
 			};
+			listview.SelectedChanged += () => {
+				SetValue (searchset [listview.SelectedItem]);
+			};
+
 
 			// Needs to be re-applied for LayoutStyle.Computed
 			listview.X = x;
@@ -92,27 +98,28 @@ namespace Terminal.Gui {
 					return true;
 				}
 
-				search.Text = text =  searchset [listview.SelectedItem];
+				SetValue( searchset [listview.SelectedItem]);
 				search.CursorPosition = search.Text.Length;
 				Changed?.Invoke (this, text);
 
 				searchset.Clear();
-				listview.Clear();
+				listview.SetSource(new List<string> ());
+				listview.Height = 0;
 				this.SetFocus(search);
 
 				return true;
 			}
 
-			if (e.Key == Key.CursorDown && search.HasFocus) // jump to list
-			{
-				this.SetFocus(listview);
-				listview.SelectedItem = 0;
+			if (e.Key == Key.CursorDown && search.HasFocus && listview.SelectedItem == 0) { // jump to list
+				this.SetFocus (listview);
+				SetValue (searchset [listview.SelectedItem]);
 				return true;
 			}
 
-			if(e.Key == Key.CursorUp && listview.SelectedItem == 0 && listview.HasFocus) // jump back to search
+			if (e.Key == Key.CursorUp && listview.HasFocus && listview.SelectedItem == 0) // jump back to search
 			{
-				this.SetFocus(search);
+				search.CursorPosition = search.Text.Length;
+				this.SetFocus (search);
 				return true;
 			}
 
@@ -147,6 +154,14 @@ namespace Terminal.Gui {
 			}
 		}
 
+		private void SetValue(ustring text)
+		{
+			search.Changed -= Search_Changed;
+			this.text = search.Text = text;
+			search.CursorPosition = 0;
+			search.Changed += Search_Changed;
+		}
+
 		/// <summary>
 		/// Reset to full original list
 		/// </summary>
@@ -154,27 +169,25 @@ namespace Terminal.Gui {
 		{
 			search.Text = text = "";
 			Changed?.Invoke (this, search.Text);
-			searchset = listsource;
+			searchset = isAutoHide ? new List<string> () : listsource;
 
 			listview.SetSource(searchset.ToList());
 			listview.Height = CalculatetHeight ();
-			listview.Redraw (new Rect (0, 0, width, height));
 
 			this.SetFocus(search);
 		}
 
 		private void Search_Changed (object sender, ustring text)
 		{
-			// Cannot use text argument as its old value (pre-change)
 			if (string.IsNullOrEmpty (search.Text.ToString())) {
-				searchset = listsource;
+				searchset = isAutoHide ? new List<string> () : listsource;
 			} 
 			else
 				searchset = listsource.Where (x => x.StartsWith (search.Text.ToString (), StringComparison.CurrentCultureIgnoreCase)).ToList ();
 
 			listview.SetSource (searchset.ToList ());
 			listview.Height = CalculatetHeight ();
-			listview.Redraw (new Rect (0, 0, width, height));
+			listview.Redraw (new Rect (0, 0, width, height)); // for any view behind this
 		}
 
 		/// <summary>
@@ -185,19 +198,5 @@ namespace Terminal.Gui {
 		{
 			return Math.Min (height, searchset.Count);
 		}
-
-		/// <summary>
-		/// Determine if this view is hosted inside a dialog
-		/// </summary>
-		/// <returns></returns>
-		private bool IsDialogHosted()
-		{
-			for (View v = this.SuperView; v != null; v = v.SuperView) {
-
-				if (v.GetType () == typeof (Dialog))
-					return true;
-			}
-			return false;
-		}
 	}
 }