Prechádzať zdrojové kódy

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

Ross Ferguson 5 rokov pred
rodič
commit
89e2790a1c

+ 22 - 21
Example/demo.cs

@@ -421,26 +421,27 @@ static class Demo {
 		MessageBox.Query (60, 10, "Selected Animals", result == "" ? "No animals selected" : result, "Ok");
 	}
 
-	//static void ComboBoxDemo ()
-	//{
-	//	IList<string> items = new List<string> ();
-	//	foreach (var dir in new [] { "/etc", @"\windows\System32" }) {
-	//		if (Directory.Exists (dir)) {
-	//			items = Directory.GetFiles (dir)
-	//			.Select (Path.GetFileName)
-	//			.Where (x => char.IsLetterOrDigit (x [0]))
-	//			.Distinct ()
-	//			.OrderBy (x => x).ToList ();
-	//		}
-	//	}
-	//	var list = new ComboBox (0, 0, 36, 7, items);
-	//	list.Changed += (object sender, ustring text) => { Application.RequestStop (); };
-
-	//	var d = new Dialog ("Select source file", 40, 12) { list };
-	//	Application.Run (d);
-
-	//	MessageBox.Query (60, 10, "Selected file", list.Text.ToString() == "" ? "Nothing selected" : list.Text.ToString(), "Ok");
-	//}
+	static void ComboBoxDemo ()
+	{
+		IList<string> items = new List<string> ();
+		foreach (var dir in new [] { "/etc", @"\windows\System32" }) {
+			if (Directory.Exists (dir)) {
+				items = Directory.GetFiles (dir)
+				.Select (Path.GetFileName)
+				.Where (x => char.IsLetterOrDigit (x [0]))
+				.Distinct ()
+				.OrderBy (x => x).ToList ();
+			}
+		}
+		var list = new ComboBox () { X = 0, Y = 0, Width = 36, Height = 7 };
+		list.SetSource(items);
+		list.Changed += (object sender, ustring text) => { Application.RequestStop (); };
+
+		var d = new Dialog ("Select source file", 40, 12) { list };
+		Application.Run (d);
+
+		MessageBox.Query (60, 10, "Selected file", list.Text.ToString() == "" ? "Nothing selected" : list.Text.ToString(), "Ok");
+	}
 	#endregion
 
 
@@ -571,7 +572,7 @@ static class Demo {
 			new MenuBarItem ("_List Demos", new MenuItem [] {
 				new MenuItem ("Select _Multiple Items", "", () => ListSelectionDemo (true)),
 				new MenuItem ("Select _Single Item", "", () => ListSelectionDemo (false)),
-//				new MenuItem ("Search Single Item", "", ComboBoxDemo)
+				new MenuItem ("Search Single Item", "", ComboBoxDemo)
 			}),
 			new MenuBarItem ("A_ssorted", new MenuItem [] {
 				new MenuItem ("_Show text alignments", "", () => ShowTextAlignments ()),

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

@@ -361,7 +361,7 @@ namespace Terminal.Gui {
 			return 0;
 		}
 
-		class DimFactor : Dim {
+		internal class DimFactor : Dim {
 			float factor;
 
 			public DimFactor (float n)

+ 0 - 6
Terminal.Gui/Terminal.Gui.csproj

@@ -183,12 +183,6 @@
     <PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="true" />
     <PackageReference Include="NStack.Core" Version="0.14.0" />
   </ItemGroup>
-  <ItemGroup Condition="'$(Configuration)'!='Debug'">
-    <PackageReference Include="SauceControl.InheritDoc" Version="1.0.0" PrivateAssets="all" />
-  </ItemGroup>
-  <ItemGroup>
-    <Compile Remove="Views\ComboBox.cs" />
-  </ItemGroup>
   <!--<ItemGroup>
     <Reference Include="NStack">
       <HintPath>..\..\..\Users\miguel\.nuget\packages\nstack.core\0.14.0\lib\netstandard2.0\NStack.dll</HintPath>

+ 29 - 23
Terminal.Gui/Views/ComboBox.cs

@@ -40,8 +40,8 @@ namespace Terminal.Gui {
 		/// </summary>
 		public ComboBox () : base()
 		{
-			search = new TextField ("") { LayoutStyle = LayoutStyle.Computed };
-			listview = new ListView () { LayoutStyle = LayoutStyle.Computed, CanFocus = true /* why? */ };
+			search = new TextField ("");
+			listview = new ListView () { LayoutStyle = LayoutStyle.Computed, CanFocus = true };
 
 			Initialize ();
 		}
@@ -62,7 +62,7 @@ namespace Terminal.Gui {
 			height = h;
 			width = w;
 
-			search = new TextField (x, y, w, "");
+			search = new TextField ("") { X = x, Y = y, Width = w };
 
 			listview = new ListView (new Rect (x, y + 1, w, 0), listsource.ToList ()) {
 				LayoutStyle = LayoutStyle.Computed,
@@ -81,7 +81,7 @@ namespace Terminal.Gui {
 			};
 
 			// TODO: LayoutComplete event breaks cursor up/down. Revert to Application.Loaded 
-			Application.Loaded += (sender, a) => {
+			Application.Loaded += (object sender, Application.ResizedEventArgs a) => {
 				// Determine if this view is hosted inside a dialog
 				for (View view = this.SuperView; view != null; view = view.SuperView) {
 					if (view is Dialog) {
@@ -102,18 +102,25 @@ namespace Terminal.Gui {
 				else
 					listview.Y = Pos.Bottom (search);
 
-				if (Width == null)
+				if (Width == null) {
 					listview.Width = CalculateWidth ();
-				else {
-					width = GetDimAsInt (Width);
+					search.Width = width;
+				} else {
+					width = GetDimAsInt (Width, a, vertical: false);
+					search.Width = width;
 					listview.Width = CalculateWidth ();
 				}
 
-				if (Height == null)
-					listview.Height = CalculatetHeight ();
-				else {
-					height = GetDimAsInt (Height);
+				if (Height == null) {
+					var h = CalculatetHeight ();
+					listview.Height = h;
+					this.Height = h + 1; // adjust view to account for search box
+				} else {
+					if (height == 0)
+						height = GetDimAsInt (Height, a, vertical: true);
+
 					listview.Height = CalculatetHeight ();
+					this.Height = height + 1; // adjust view to account for search box
 				}
 
 				if (this.Text != null)
@@ -161,8 +168,7 @@ namespace Terminal.Gui {
 		///<inheritdoc/>
 		public override bool ProcessKey(KeyEvent e)
 		{
-			if (e.Key == Key.Tab)
-			{
+			if (e.Key == Key.Tab) {
 				base.ProcessKey(e);
 				return false; // allow tab-out to next control
 			}
@@ -295,18 +301,18 @@ namespace Terminal.Gui {
 		/// Get DimAbsolute as integer value
 		/// </summary>
 		/// <param name="dim"></param>
+		/// <param name="a"></param>
+		/// <param name="vertical"></param>
 		/// <returns></returns>
-		private int GetDimAsInt(Dim dim)
+		private int GetDimAsInt (Dim dim, Application.ResizedEventArgs a, bool vertical)
 		{
-			if (!(dim is Dim.DimAbsolute))
-				throw new ArgumentException ("Dim must be an absolute value");
-
-			// Anchor in the case of DimAbsolute returns absolute value. No documentation on what Anchor() does so not sure if this will change in the future.
-			//
-			// TODO: Does Dim need:- 
-			//		public static implicit operator int (Dim d)
-			//
-			return dim.Anchor (0);
+			if (dim is Dim.DimAbsolute)
+				return dim.Anchor (0);
+
+			if (dim is Dim.DimFill || dim is Dim.DimFactor)
+				return vertical ? dim.Anchor (a.Rows) : dim.Anchor (a.Cols);
+
+			return 0;
 		}
 	}
 }

+ 11 - 8
UICatalog/Scenarios/ListsAndCombos.cs

@@ -22,17 +22,20 @@ namespace UICatalog.Scenarios {
 				}
 			}
 
+			Dim width = 30;
+
 			// ListView
 			var lbListView = new Label ("Listview") {
 				ColorScheme = Colors.TopLevel,
 				X = 0,
-				Width = 30
+				Width = width
 			};
 
 			var listview = new ListView (items) {
 				X = 0,
 				Y = Pos.Bottom (lbListView) + 1,
-				Width = 30
+				Height = Dim.Fill(2),
+				Width = width
 			};
 			listview.OpenSelectedItem += (object sender, ListViewItemEventArgs e) => lbListView.Text = items [listview.SelectedItem];
 			Win.Add (lbListView, listview);
@@ -41,14 +44,14 @@ namespace UICatalog.Scenarios {
 			var lbComboBox = new Label ("ComboBox") {
 				ColorScheme = Colors.TopLevel,
 				X = Pos.Right (lbListView) + 1,
-				Width = 30
+				Width = width
 			};
 
-			var comboBox = new ComboBox() {
-				X = Pos.Right(listview) + 1 , 
-				Y = Pos.Bottom (lbListView) +1,
-				Height = 10,
-				Width = 30
+			var comboBox = new ComboBox () {
+				X = Pos.Right (listview) + 1,
+				Y = Pos.Bottom (lbListView) + 1,
+				Height = Dim.Fill (2),
+				Width = width
 			};
 			comboBox.SetSource (items);
 

+ 12 - 10
UICatalog/Scenarios/Unicode.cs

@@ -35,16 +35,18 @@ namespace UICatalog {
 			var checkBox = new CheckBox (" ~  s  gui.cs   master ↑10") { X = 15, Y = Pos.Y (label), Width = Dim.Percent (50) };
 			Win.Add (checkBox);
 
-			//label = new Label ("ComboBox:") { X = Pos.X (label), Y = Pos.Bottom (label) + 1 };
-			//Win.Add (label);
-			//var comboBox = new ComboBox (1, 1, 30, 5, new List<string> () { "item #1", " ~  s  gui.cs   master ↑10", "Со_хранить" }) {
-			//	X = 15,
-			//	Y = Pos.Y (label),
-			//	Width = 30,
-			//	ColorScheme = Colors.Error
-			//};
-			//Win.Add (comboBox);
-			//comboBox.Text = " ~  s  gui.cs   master ↑10";
+			label = new Label ("ComboBox:") { X = Pos.X (label), Y = Pos.Bottom (label) + 1 };
+			Win.Add (label);
+			var comboBox = new ComboBox () {
+				X = 15,
+				Y = Pos.Y (label),
+				Width = Dim.Percent (50),
+				ColorScheme = Colors.Error
+			};
+			comboBox.SetSource (new List<string> () { "item #1", " ~  s  gui.cs   master ↑10", "Со_хранить" });
+
+			Win.Add (comboBox);
+			comboBox.Text = " ~  s  gui.cs   master ↑10";
 
 			label = new Label ("HexView:") { X = Pos.X (label), Y = Pos.Bottom (label) + 2 };
 			Win.Add (label);

+ 0 - 4
UICatalog/UICatalog.csproj

@@ -8,10 +8,6 @@
     <LangVersion>8.0</LangVersion>
   </PropertyGroup>
 
-  <ItemGroup>
-    <Compile Remove="Scenarios\ListsAndCombos.cs" />
-  </ItemGroup>
-
   <ItemGroup>
     <ProjectReference Include="..\Terminal.Gui\Terminal.Gui.csproj" />
   </ItemGroup>