Browse Source

Merge pull request #134 from tig/BDisp-v2_layout-improvements

Fixes merge issues?
BDisp 2 years ago
parent
commit
ef9124c472

+ 2 - 2
Terminal.Gui/Configuration/ConfigurationManager.cs

@@ -223,13 +223,13 @@ namespace Terminal.Gui.Configuration {
 		public static ThemeManager? Themes => ThemeManager.Instance;
 
 		/// <summary>
-		/// Aplication-specific configuration settings scope.
+		/// Application-specific configuration settings scope.
 		/// </summary>
 		[SerializableConfigurationProperty (Scope = typeof (SettingsScope), OmitClassName = true), JsonPropertyName ("AppSettings")]
 		public static AppScope? AppSettings { get; set; }
 
 		/// <summary>
-		/// Initializes the internal state of ConfiguraitonManager. Nominally called once as part of application
+		/// Initializes the internal state of ConfigurationManager. Nominally called once as part of application
 		/// startup to initialize global state. Also called from some Unit Tests to ensure correctness (e.g. Reset()).
 		/// </summary>
 		internal static void Initialize ()

+ 5 - 1
Terminal.Gui/Configuration/Scope.cs

@@ -129,7 +129,11 @@ namespace Terminal.Gui.Configuration {
 								}
 							}
 							var readHelper = Activator.CreateInstance ((Type?)typeof (ReadHelper<>).MakeGenericType (typeof (scopeT), propertyType!)!, converter) as ReadHelper;
-							scope! [propertyName].PropertyValue = readHelper?.Read (ref reader, propertyType!, options);
+							try {
+								scope! [propertyName].PropertyValue = readHelper?.Read (ref reader, propertyType!, options);
+							} catch (NotSupportedException e) {
+								throw new JsonException ($"Error reading property \"{propertyName}\" of type \"{propertyType.Name}\".", e);
+							}
 						} else {
 							try {
 								scope! [propertyName].PropertyValue = JsonSerializer.Deserialize (ref reader, propertyType!, options);

+ 7 - 7
Terminal.Gui/Configuration/ThemeScope.cs

@@ -120,21 +120,21 @@ namespace Terminal.Gui.Configuration {
 			/// </summary>
 			public static ThemeManager Instance { get { return _instance; } }
 
-			private static string theme = string.Empty;
+			private static string _theme = string.Empty;
 
 			/// <summary>
 			/// The currently selected theme. This is the internal version; see <see cref="Theme"/>.
 			/// </summary>
 			[JsonInclude, SerializableConfigurationProperty (Scope = typeof (SettingsScope), OmitClassName = true), JsonPropertyName ("Theme")]
 			internal static string SelectedTheme {
-				get => theme;
+				get => _theme;
 				set {
-					var oldTheme = theme;
-					theme = value;
-					if (oldTheme != theme &&
+					var oldTheme = _theme;
+					_theme = value;
+					if (oldTheme != _theme &&
 						ConfigurationManager.Settings! ["Themes"]?.PropertyValue is Dictionary<string, ThemeScope> themes &&
-						themes.ContainsKey (theme)) {
-						ConfigurationManager.Settings! ["Theme"].PropertyValue = theme;
+						themes.ContainsKey (_theme)) {
+						ConfigurationManager.Settings! ["Theme"].PropertyValue = _theme;
 						Instance.OnThemeChanged (oldTheme);
 					}
 				}

+ 1 - 0
Terminal.Gui/Core/Border.cs

@@ -448,6 +448,7 @@ namespace Terminal.Gui {
 		/// <summary>
 		/// Gets or sets the single child element of a <see cref="View"/>.
 		/// </summary>
+		[JsonIgnore]
 		public View Child {
 			get => child;
 			set {

+ 1 - 1
Terminal.Gui/Terminal.Gui.csproj

@@ -30,7 +30,7 @@
     <PackageReference Include="System.Text.Json" Version="7.0.1" />
     <PackageReference Include="System.Management" Version="7.0.0" />
     <InternalsVisibleTo Include="UnitTests" />
-  </ItemGroup>
+   </ItemGroup>
   <!-- Uncomment the RestoreSources element to have dotnet restore pull NStack from a local dir for testing -->
   <PropertyGroup>
     <!-- See https://stackoverflow.com/a/44463578/297526 -->

+ 4 - 4
UnitTests/Configuration/ConfigurationMangerTests.cs

@@ -182,12 +182,13 @@ namespace Terminal.Gui.ConfigurationTests {
 		/// Save the `config.json` file; this can be used to update the file in `Terminal.Gui.Resources.config.json'.
 		/// </summary>
 		/// <remarks>
-		/// IMPORTANT: For the file generated to be valid, this must be the ONLY test run. Conifg Properties
-		/// are all satic and thus can be overwritten by other tests.</remarks>
+		/// IMPORTANT: For the file generated to be valid, this must be the ONLY test run. Config Properties
+		/// are all static and thus can be overwritten by other tests.</remarks>
 		[Fact]
 		public void SaveDefaults ()
 		{
 			ConfigurationManager.Initialize ();
+			ConfigurationManager.Reset ();
 
 			// Get the hard coded settings
 			ConfigurationManager.GetHardCodedDefaults ();
@@ -316,12 +317,11 @@ namespace Terminal.Gui.ConfigurationTests {
 		[Fact, AutoInitShutdown]
 		public void TestConfigurationManagerToJson ()
 		{
+			ConfigurationManager.Reset ();
 			ConfigurationManager.GetHardCodedDefaults ();
 			var stream = ConfigurationManager.ToStream ();
-
 			
 			ConfigurationManager.Settings.Update (stream, "TestConfigurationManagerToJson");
-
 		}
 
 		[Fact, AutoInitShutdown (configLocation: ConfigLocations.None)]

+ 1 - 201
UnitTests/Core/LayoutTests.cs

@@ -35,53 +35,6 @@ namespace Terminal.Gui.CoreTests {
 			Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
 		}
 
-		[Fact]
-		public void TopologicalSort_Does_Never_Throws_If_Root_Is_Not_Null ()
-		{
-			var root = new View () { Id = "root", Width = 20, Height = 20 };
-			var sub1 = new View () {
-				Id = "sub1",
-				X = Pos.Left (root) + 1,
-				Y = Pos.Top (root) + 1,
-				Width = Dim.Width (root) - 2,
-				Height = Dim.Height (root) - 2
-			};
-			var sub2 = new View () {
-				Id = "sub2",
-				X = Pos.Left (root) + 1,
-				Y = Pos.Top (root) + 1,
-				Width = Dim.Width (root) - 2,
-				Height = Dim.Height (root) - 2
-			};
-			var sub3 = new View () {
-				Id = "sub3",
-				X = Pos.Left (root) + 1,
-				Y = Pos.Top (root) + 1,
-				Width = Dim.Width (root) - 2,
-				Height = Dim.Height (root) - 2
-			};
-			sub2.Add (sub3);
-			sub1.Add (sub2);
-			root.Add (sub1);
-
-			var exception = Record.Exception (root.LayoutSubviews);
-			Assert.Null (exception);
-			Assert.Equal (new Rect (0, 0, 20, 20), root.Frame);
-			Assert.Equal (new Rect (1, 1, 18, 18), sub1.Frame);
-			Assert.Equal (new Rect (1, 1, 18, 18), sub2.Frame);
-			Assert.Equal (new Rect (1, 1, 18, 18), sub3.Frame);
-
-			sub2.Width = Dim.Width (root);
-			exception = Record.Exception (root.LayoutSubviews);
-			Assert.Null (exception);
-			Assert.Equal (new Rect (1, 1, 20, 18), sub2.Frame);
-
-			sub3.Width = Dim.Width (root);
-			exception = Record.Exception (root.LayoutSubviews);
-			Assert.Null (exception);
-			Assert.Equal (new Rect (1, 1, 20, 18), sub3.Frame);
-		}
-
 		[Fact]
 		public void TopologicalSort_Recursive_Ref ()
 		{
@@ -1446,7 +1399,7 @@ Y
 
 			testView = new View () {
 				AutoSize = false,
-				X = Pos.Left (testView),
+				X = Pos.Left(testView),
 				Y = Pos.Left (testView),
 				Width = 1,
 				Height = 1
@@ -1468,158 +1421,5 @@ Y
 			Assert.Equal (6, testView.Frame.X);
 			Assert.Equal (6, testView.Frame.Y);
 		}
-
-		[Fact]
-		[AutoInitShutdown]
-		public void Y_Center_Minus_Absolute_Inside_Window_Height_Ten ()
-		{
-			var win = new Window ();
-
-			var label = new Label ("This should be the first line.") {
-				TextAlignment = Terminal.Gui.TextAlignment.Centered,
-				ColorScheme = Colors.Menu,
-				Width = Dim.Fill (),
-				X = Pos.Center (),
-				Y = Pos.Center () - 3  // center minus 3 minus two lines top and bottom borders equal to zero (5-3-2=0)
-			};
-
-			var button = new Button ("Press me!") {
-				X = Pos.Center (),
-				Y = Pos.Center ()
-			};
-
-			win.Add (label, button);
-
-			var top = Application.Top;
-			top.Add (win);
-			Application.Begin (top);
-			((FakeDriver)Application.Driver).SetBufferSize (40, 10);
-
-			Assert.True (label.AutoSize);
-			Assert.Equal (new Rect (0, 0, 40, 10), top.Frame);
-			Assert.Equal (new Rect (0, 0, 40, 10), win.Frame);
-			Assert.Equal (new Rect (1, 1, 38, 8), win.Subviews [0].Frame);
-			Assert.Equal ("ContentView()({X=1,Y=1,Width=38,Height=8})", win.Subviews [0].ToString ());
-			Assert.Equal (new Rect (0, 0, 40, 10), new Rect (
-				win.Frame.Left, win.Frame.Top,
-				win.Frame.Right, win.Frame.Bottom));
-			Assert.Equal (new Rect (0, 0, 38, 1), label.Frame);
-			Assert.Equal (new Rect (12, 3, 13, 1), button.Frame);
-			var expected = @"
-┌──────────────────────────────────────┐
-│    This should be the first line.    │
-│                                      │
-│                                      │
-│            [ Press me! ]             │
-│                                      │
-│                                      │
-│                                      │
-│                                      │
-└──────────────────────────────────────┘
-";
-
-			TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
-		}
-
-		[Fact]
-		[AutoInitShutdown]
-		public void Y_Center_Minus_Absolute_Inside_Window_Height_Nine ()
-		{
-			var win = new Window ();
-
-			var label = new Label ("This should be the first line.") {
-				TextAlignment = Terminal.Gui.TextAlignment.Centered,
-				ColorScheme = Colors.Menu,
-				Width = Dim.Fill (),
-				X = Pos.Center (),
-				Y = Pos.Center () - 3  // center minus 2 minus two lines top and bottom borders equal to zero (5-3-2=0)
-			};
-
-			var button = new Button ("Press me!") {
-				X = Pos.Center (),
-				Y = Pos.Center ()
-			};
-
-			win.Add (label, button);
-
-			var top = Application.Top;
-			top.Add (win);
-			Application.Begin (top);
-			((FakeDriver)Application.Driver).SetBufferSize (40, 9);
-
-			Assert.True (label.AutoSize);
-			Assert.Equal (new Rect (0, 0, 40, 9), top.Frame);
-			Assert.Equal (new Rect (0, 0, 40, 9), win.Frame);
-			Assert.Equal (new Rect (1, 1, 38, 7), win.Subviews [0].Frame);
-			Assert.Equal ("ContentView()({X=1,Y=1,Width=38,Height=7})", win.Subviews [0].ToString ());
-			Assert.Equal (new Rect (0, 0, 40, 9), new Rect (
-				win.Frame.Left, win.Frame.Top,
-				win.Frame.Right, win.Frame.Bottom));
-			Assert.Equal (new Rect (0, 0, 38, 1), label.Frame);
-			Assert.Equal (new Rect (12, 3, 13, 1), button.Frame);
-			var expected = @"
-┌──────────────────────────────────────┐
-│    This should be the first line.    │
-│                                      │
-│                                      │
-│            [ Press me! ]             │
-│                                      │
-│                                      │
-│                                      │
-└──────────────────────────────────────┘
-";
-
-			TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
-		}
-
-		[Fact]
-		[AutoInitShutdown]
-		public void Y_Center_Minus_Absolute_Inside_Window_Height_Eight ()
-		{
-			var win = new Window ();
-
-			var label = new Label ("This should be the first line.") {
-				TextAlignment = Terminal.Gui.TextAlignment.Centered,
-				ColorScheme = Colors.Menu,
-				Width = Dim.Fill (),
-				X = Pos.Center (),
-				Y = Pos.Center () - 2  // center minus 2 minus two lines top and bottom borders equal to zero (4-2-2=0)
-			};
-
-			var button = new Button ("Press me!") {
-				X = Pos.Center (),
-				Y = Pos.Center ()
-			};
-
-			win.Add (label, button);
-
-			var top = Application.Top;
-			top.Add (win);
-			Application.Begin (top);
-			((FakeDriver)Application.Driver).SetBufferSize (40, 8);
-
-			Assert.True (label.AutoSize);
-			Assert.Equal (new Rect (0, 0, 40, 8), top.Frame);
-			Assert.Equal (new Rect (0, 0, 40, 8), win.Frame);
-			Assert.Equal (new Rect (1, 1, 38, 6), win.Subviews [0].Frame);
-			Assert.Equal ("ContentView()({X=1,Y=1,Width=38,Height=6})", win.Subviews [0].ToString ());
-			Assert.Equal (new Rect (0, 0, 40, 8), new Rect (
-				win.Frame.Left, win.Frame.Top,
-				win.Frame.Right, win.Frame.Bottom));
-			Assert.Equal (new Rect (0, 0, 38, 1), label.Frame);
-			Assert.Equal (new Rect (12, 2, 13, 1), button.Frame);
-			var expected = @"
-┌──────────────────────────────────────┐
-│    This should be the first line.    │
-│                                      │
-│            [ Press me! ]             │
-│                                      │
-│                                      │
-│                                      │
-└──────────────────────────────────────┘
-";
-
-			TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
-		}
 	}
 }

+ 2 - 2
UnitTests/Types/DimTests.cs

@@ -520,14 +520,14 @@ namespace Terminal.Gui.TypeTests {
 				v5.Text = "Button5";
 				Assert.Equal ("Combine(View(Width,Button()({X=2,Y=7,Width=97,Height=189}))-View(Width,Button()({X=0,Y=0,Width=19,Height=19})))", v5.Width.ToString ());
 				Assert.Equal ("Combine(View(Height,Button()({X=2,Y=7,Width=97,Height=189}))-View(Height,Button()({X=0,Y=0,Width=19,Height=19})))", v5.Height.ToString ());
-				Assert.Equal (78, v5.Frame.Width); // 97-19=78
+				Assert.Equal (78, v5.Frame.Width); // 97-9=78
 				Assert.Equal (170, v5.Frame.Height); // 189-19=170
 
 				v6.Text = "Button6";
 				Assert.Equal ("Factor(0.2,True)", v6.Width.ToString ());
 				Assert.Equal ("Factor(0.2,True)", v6.Height.ToString ());
 				Assert.Equal (19, v6.Frame.Width); // 99*20%=19
-				Assert.Equal (38, v6.Frame.Height); // 198-7*20=38
+				Assert.Equal (38, v6.Frame.Height); // 198-7*20=18
 			};
 
 			Application.Iteration += () => Application.RequestStop ();