Bläddra i källkod

Fixed color issues

Tigger Kindel 1 år sedan
förälder
incheckning
5e32c4616b

+ 16 - 1
Terminal.Gui/Configuration/ConfigProperty.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Reflection;
+using System.Text.Json;
 using System.Text.Json.Serialization;
 
 #nullable enable
@@ -89,7 +90,21 @@ public class ConfigProperty {
 	public bool Apply ()
 	{
 		if (PropertyValue != null) {
-			PropertyInfo?.SetValue (null, ConfigurationManager.DeepMemberwiseCopy (PropertyValue, PropertyInfo?.GetValue (null)));
+			try {
+				PropertyInfo?.SetValue (null, ConfigurationManager.DeepMemberwiseCopy (PropertyValue, PropertyInfo?.GetValue (null)));
+			} catch (TargetInvocationException tie) {
+				// Check if there is an inner exception
+				if (tie.InnerException != null) {
+					// Handle the inner exception separately without catching the outer exception
+					Exception innerException = tie.InnerException;
+
+					// Handle the inner exception here
+					throw new JsonException ($"Error Applying Configuration Change: {innerException.Message}", innerException);
+				}
+
+				// Handle the outer exception or rethrow it if needed
+				throw new JsonException ($"Error Applying Configuration Change: {tie.Message}", tie);
+			}
 		}
 		return PropertyValue != null;
 	}

+ 23 - 5
Terminal.Gui/Configuration/ConfigurationManager.cs

@@ -11,6 +11,8 @@ using System.Reflection;
 using System.Text;
 using System.Text.Json;
 using System.Text.Json.Serialization;
+using static Terminal.Gui.SpinnerStyle;
+
 
 #nullable enable
 
@@ -311,11 +313,24 @@ public static partial class ConfigurationManager {
 	/// </summary>
 	public static void Apply ()
 	{
-		bool settings = Settings?.Apply () ?? false;
-		bool themes = !string.IsNullOrEmpty (ThemeManager.SelectedTheme) && (ThemeManager.Themes? [ThemeManager.SelectedTheme]?.Apply () ?? false);
-		bool appsettings = AppSettings?.Apply () ?? false;
-		if (settings || themes || appsettings) {
-			OnApplied ();
+		var settings = false;
+		var themes = false;
+		var appSettings = false;
+		try {
+			settings = Settings?.Apply () ?? false;
+			themes = !string.IsNullOrEmpty (ThemeManager.SelectedTheme) && (ThemeManager.Themes? [ThemeManager.SelectedTheme]?.Apply () ?? false);
+			appSettings = AppSettings?.Apply () ?? false;
+
+		} catch (JsonException e) {
+			if (ThrowOnJsonErrors ?? false) {
+				throw;
+			} else {
+				AddJsonError ($"Error applying Configuration Change: {e.Message}");
+			}
+		} finally {
+			if (settings || themes || appSettings) {
+				OnApplied ();
+			}
 		}
 	}
 
@@ -465,6 +480,9 @@ public static partial class ConfigurationManager {
 		// Dictionary
 		if (source.GetType ().IsGenericType && source.GetType ().GetGenericTypeDefinition ().IsAssignableFrom (typeof (Dictionary<,>))) {
 			foreach (var srcKey in ((IDictionary)source).Keys) {
+				if (srcKey is string) {
+
+				}
 				if (((IDictionary)destination).Contains (srcKey))
 					((IDictionary)destination) [srcKey] = DeepMemberwiseCopy (((IDictionary)source) [srcKey], ((IDictionary)destination) [srcKey]);
 				else {

+ 4 - 1
Terminal.Gui/Configuration/SettingsScope.cs

@@ -76,7 +76,10 @@ public class SettingsScope : Scope<SettingsScope> {
 		}
 
 		var stream = File.OpenRead (realPath);
-		return Update (stream, filePath);
+		var s = Update (stream, filePath);
+		stream.Close ();
+		stream.Dispose ();
+		return s;
 	}
 
 	/// <summary>

+ 7 - 6
Terminal.Gui/Drawing/Color.cs

@@ -224,15 +224,16 @@ namespace Terminal.Gui {
 		}.ToImmutableDictionary ();
 
 		[SerializableConfigurationProperty (Scope = typeof (SettingsScope), OmitClassName = true)]
-		public static Dictionary<string, string> Colors {
+		//[JsonConverter (typeof (DictionaryJsonConverter<string>))]
+		public static Dictionary<ColorNames, string> Colors {
 			get {
-				// Transform _colorToNameMap into a Dictionary<string,string>
-				return _colorToNameMap.ToDictionary (kvp => kvp.Value.ToString (), kvp => $"#{kvp.Key.R:X2}{kvp.Key.G:X2}{kvp.Key.B:X2}");
+				// Transform _colorToNameMap into a Dictionary<ColorNames,string>
+				return _colorToNameMap.ToDictionary (kvp => kvp.Value, kvp => $"#{kvp.Key.R:X2}{kvp.Key.G:X2}{kvp.Key.B:X2}");
 			}
 			set {
-				// Transform Dictionary<string,string> into _colorToNameMap
+				// Transform Dictionary<ColorNames,string> into _colorToNameMap
 				var newMap = value.ToDictionary (kvp => new Color (kvp.Value), kvp => {
-					if (Enum.TryParse<ColorNames> (kvp.Key, out var colorName)) {
+					if (Enum.TryParse<ColorNames> (kvp.Key.ToString(), ignoreCase: true, out ColorNames colorName)) {
 						return colorName;
 					}
 					throw new ArgumentException ($"Invalid color name: {kvp.Key}");
@@ -448,7 +449,7 @@ namespace Terminal.Gui {
 				return true;
 			}
 
-			if (Enum.TryParse<ColorNames> (text, true, out ColorNames colorName)) {
+			if (Enum.TryParse<ColorNames> (text, ignoreCase: true, out ColorNames colorName)) {
 				color = new Color (colorName);
 				return true;
 			}

+ 1 - 1
Terminal.Gui/Resources/config.json

@@ -200,7 +200,7 @@
                 "Background": "Black"
               },
               "Focus": {
-                "Foreground": "White",
+                "Foreground": "BrightYellow",
                 "Background": "DarkGray"
               },
               "HotNormal": {

+ 2 - 2
Terminal.Gui/Views/TableView/TableView.cs

@@ -572,7 +572,7 @@ namespace Terminal.Gui {
 			Attribute color;
 
 			if (FullRowSelect && IsSelected (0, rowToRender)) {
-				color = focused ? rowScheme.HotFocus : rowScheme.HotNormal;
+				color = focused ? rowScheme.Focus : rowScheme.HotNormal;
 			} else {
 				color = Enabled ? rowScheme.Normal : rowScheme.Disabled;
 			}
@@ -618,7 +618,7 @@ namespace Terminal.Gui {
 
 				Attribute cellColor;
 				if (isSelectedCell) {
-					cellColor = focused ? scheme.HotFocus : scheme.HotNormal;
+					cellColor = focused ? scheme.Focus : scheme.HotNormal;
 				} else {
 					cellColor = Enabled ? scheme.Normal : scheme.Disabled;
 				}

+ 1 - 1
UICatalog/UICatalog.cs

@@ -183,7 +183,7 @@ namespace UICatalog {
 			}
 
 			// TODO: This is a hack. Figure out how to ensure that the file is fully written before reading it.
-			Thread.Sleep (500);
+			//Thread.Sleep (500);
 			CM.Load ();
 			CM.Apply ();
 		}