Browse Source

Remove Initalized stuff - With TrueColor and the map of color names to RGB values now in Color, it's not needed.

Tigger Kindel 1 year ago
parent
commit
6f1e3e9a06

+ 0 - 8
Terminal.Gui/Configuration/ThemeScope.cs

@@ -44,12 +44,4 @@ namespace Terminal.Gui;
 /// </code></example> 
 [JsonConverter (typeof (ScopeJsonConverter<ThemeScope>))]
 public class ThemeScope : Scope<ThemeScope> {
-
-	/// <inheritdoc/>
-	internal override bool Apply ()
-	{
-		var ret = base.Apply ();
-		Application.Driver?.InitializeColorSchemes ();
-		return ret;
-	}
 }

+ 3 - 17
Terminal.Gui/ConsoleDrivers/ConsoleDriver.cs

@@ -377,9 +377,7 @@ public abstract class ConsoleDriver {
 	/// </remarks>
 	internal virtual bool Force16Colors {
 		get => Application.Force16Colors || !SupportsTrueColor;
-		set {
-			Application.Force16Colors = (value || !SupportsTrueColor);
-		}
+		set => Application.Force16Colors = (value || !SupportsTrueColor);
 	}
 
 	Attribute _currentAttribute;
@@ -390,11 +388,10 @@ public abstract class ConsoleDriver {
 	public Attribute CurrentAttribute {
 		get => _currentAttribute;
 		set {
-			if (value is { Initialized: false } && Application.Driver != null) {
+			if (Application.Driver != null) {
 				_currentAttribute = new Attribute (value.Foreground, value.Background);
 				return;
 			}
-			if (!value.Initialized) Debug.WriteLine ("ConsoleDriver.CurrentAttribute: Attributes must be initialized before use.");
 
 			_currentAttribute = value;
 		}
@@ -441,23 +438,12 @@ public abstract class ConsoleDriver {
 	{
 		// Encode the colors into the int value.
 		return new Attribute (
-			platformColor: 0, // Not used anymore by anyting but cursesdriver!
+			platformColor: 0, // only used by cursesdriver!
 			foreground: foreground,
 			background: background
 		);
 	}
 
-	/// <summary>
-	/// Ensures all <see cref="Attribute"/>s in <see cref="Colors.ColorSchemes"/> are correctly 
-	/// initialized by the driver.
-	/// </summary>
-	public void InitializeColorSchemes ()
-	{
-		// Ensure all Attributes are initialized by the driver
-		foreach (var s in Colors.ColorSchemes) {
-			s.Value.Initialize ();
-		}
-	}
 
 	#endregion
 

+ 0 - 1
Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs

@@ -676,7 +676,6 @@ internal class CursesDriver : ConsoleDriver {
 		}
 
 		CurrentAttribute = MakeColor (ColorNames.White, ColorNames.Black);
-		InitializeColorSchemes ();
 
 		TerminalResized = terminalResized;
 

+ 1 - 1
Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs

@@ -82,7 +82,7 @@ public class FakeDriver : ConsoleDriver {
 		ResizeScreen ();
 		// Call InitializeColorSchemes before UpdateOffScreen as it references Colors
 		CurrentAttribute = new Attribute (Color.White, Color.Black);
-		InitializeColorSchemes ();
+		//InitializeColorSchemes ();
 		ClearContents ();
 	}
 

+ 0 - 1
Terminal.Gui/ConsoleDrivers/NetDriver.cs

@@ -696,7 +696,6 @@ internal class NetDriver : ConsoleDriver {
 		ResizeScreen ();
 		ClearContents ();
 		CurrentAttribute = new Attribute (Color.White, Color.Black);
-		InitializeColorSchemes ();
 
 		StartReportingMouseMoves ();
 	}

+ 0 - 1
Terminal.Gui/ConsoleDrivers/WindowsDriver.cs

@@ -1543,7 +1543,6 @@ internal class WindowsDriver : ConsoleDriver {
 		}
 
 		CurrentAttribute = new Attribute (Color.White, Color.Black);
-		InitializeColorSchemes ();
 
 		_outputBuffer = new WindowsConsole.ExtendedCharInfo [Rows * Cols];
 		Clip = new Rect (0, 0, Cols, Rows);

+ 4 - 44
Terminal.Gui/Drawing/Color.cs

@@ -84,7 +84,7 @@ namespace Terminal.Gui {
 	}
 
 	/// <summary>
-	/// Represents a color in the console. This is used with <see cref="Attribute"/>. 
+	/// Represents a 24-bit color. This is used with <see cref="Attribute"/>. 
 	/// </summary>
 	[JsonConverter (typeof (ColorJsonConverter))]
 	public class Color : IEquatable<Color> {
@@ -591,7 +591,6 @@ namespace Terminal.Gui {
 		}
 	}
 
-	// TODO: Get rid of all the Initialized crap - it's not needed once Curses Driver supports TrueColor
 	/// <summary>
 	/// Attributes represent how text is styled when displayed in the terminal. 
 	/// </summary>
@@ -601,7 +600,7 @@ namespace Terminal.Gui {
 	///   class to define color schemes that can be used in an application.
 	/// </remarks>
 	[JsonConverter (typeof (AttributeJsonConverter))]
-	public struct Attribute : IEquatable<Attribute> {
+	public readonly struct Attribute : IEquatable<Attribute> {
 
 		/// <summary>
 		/// Default empty attribute.
@@ -609,9 +608,7 @@ namespace Terminal.Gui {
 		public static readonly Attribute Default = new Attribute (Color.White, Color.Black);
 
 		/// <summary>
-		/// The <see cref="ConsoleDriver"/>-specific color value. If <see cref="Initialized"/> is <see langword="false"/> 
-		/// the value of this property is invalid (typically because the Attribute was created before a driver was loaded)
-		/// and the attribute should be re-made before it is used.
+		/// The <see cref="ConsoleDriver"/>-specific color value.
 		/// </summary>
 		[JsonIgnore (Condition = JsonIgnoreCondition.Always)]
 		internal int PlatformColor { get; }
@@ -656,7 +653,6 @@ namespace Terminal.Gui {
 			Foreground = foreground;
 			Background = background;
 			PlatformColor = platformColor;
-			Initialized = true;
 		}
 
 		/// <summary>
@@ -678,14 +674,11 @@ namespace Terminal.Gui {
 			Background = background;
 
 			if (Application.Driver == null) {
-				// Create the attribute, but show it's not been initialized
-				Initialized = false;
 				PlatformColor = -1;
 				return;
 			}
 
 			var make = Application.Driver.MakeAttribute (foreground, background);
-			Initialized = make.Initialized;
 			PlatformColor = make.PlatformColor;
 		}
 
@@ -758,19 +751,7 @@ namespace Terminal.Gui {
 
 		/// <inheritdoc />
 		public override int GetHashCode () => HashCode.Combine (PlatformColor, Foreground, Background);
-
-		/// <summary>
-		/// If <see langword="true"/> the attribute has been initialized by a <see cref="ConsoleDriver"/> and 
-		/// thus has <see cref="PlatformColor"/> that is valid for that driver. If <see langword="false"/> the <see cref="Foreground"/>
-		/// and <see cref="Background"/> colors may have been set '-1' but
-		/// the attribute has not been mapped to a <see cref="ConsoleDriver"/> specific color value.
-		/// </summary>
-		/// <remarks>
-		/// Attributes that have not been initialized must eventually be initialized before being passed to a driver.
-		/// </remarks>
-		[JsonIgnore]
-		public bool Initialized { get; internal set; }
-
+		
 		/// <inheritdoc />
 		public override string ToString ()
 		{
@@ -935,27 +916,6 @@ namespace Terminal.Gui {
 		{
 			return !(left == right);
 		}
-
-		internal void Initialize ()
-		{
-			// If the new scheme was created before a driver was loaded, we need to re-make
-			// the attributes
-			if (!_normal.Initialized) {
-				_normal = new Attribute (_normal.Foreground, _normal.Background);
-			}
-			if (!_focus.Initialized) {
-				_focus = new Attribute (_focus.Foreground, _focus.Background);
-			}
-			if (!_hotNormal.Initialized) {
-				_hotNormal = new Attribute (_hotNormal.Foreground, _hotNormal.Background);
-			}
-			if (!_hotFocus.Initialized) {
-				_hotFocus = new Attribute (_hotFocus.Foreground, _hotFocus.Background);
-			}
-			if (!_disabled.Initialized) {
-				_disabled = new Attribute (_disabled.Foreground, _disabled.Background);
-			}
-		}
 	}
 
 	/// <summary>

+ 2 - 2
UICatalog/UICatalog.cs

@@ -721,7 +721,7 @@ namespace UICatalog {
 
 			public void ConfigChanged ()
 			{
-				miForce16Colors.Checked = Application.Force16Colors;
+				miForce16Colors!.Checked = Application.Force16Colors;
 
 				if (_topLevelColorScheme == null || !Colors.ColorSchemes.ContainsKey (_topLevelColorScheme)) {
 					_topLevelColorScheme = "Base";
@@ -730,7 +730,7 @@ namespace UICatalog {
 				_themeMenuItems = ((UICatalogTopLevel)Application.Top).CreateThemeMenuItems ();
 				_themeMenuBarItem!.Children = _themeMenuItems;
 				foreach (var mi in _themeMenuItems!) {
-					if (mi != null && mi.Parent == null) {
+					if (mi is { Parent: null }) {
 						mi.Parent = _themeMenuBarItem;
 					}
 				}

+ 8 - 8
UnitTests/Drawing/AttributeTests.cs

@@ -16,7 +16,7 @@ public class AttributeTests {
 		var attribute = new Attribute ();
 
 		// Assert
-		Assert.False (attribute.Initialized);
+		//Assert.False (attribute.Initialized);
 		Assert.Equal (-1, attribute.PlatformColor);
 		Assert.Equal ((Color)Color.White, attribute.Foreground);
 		Assert.Equal ((Color)Color.Black, attribute.Background);
@@ -30,7 +30,7 @@ public class AttributeTests {
 		var attribute = new Attribute (42);
 
 		// Assert
-		Assert.True (attribute.Initialized);
+		//Assert.True (attribute.Initialized);
 		Assert.Equal (42, attribute.PlatformColor);
 		Assert.Equal ((Color)Color.White, attribute.Foreground);
 		Assert.Equal ((Color)Color.Black, attribute.Background);
@@ -109,19 +109,19 @@ public class AttributeTests {
 
 		attr = new Attribute (fg, bg);
 
-		Assert.True (attr.Initialized);
+		//Assert.True (attr.Initialized);
 		//Assert.True (attr.HasValidColors);
 		Assert.Equal (fg, attr.Foreground);
 		Assert.Equal (bg, attr.Background);
 
 		attr = new Attribute (fg);
-		Assert.True (attr.Initialized);
+		//Assert.True (attr.Initialized);
 		//Assert.True (attr.HasValidColors);
 		Assert.Equal (fg, attr.Foreground);
 		Assert.Equal (fg, attr.Background);
 
 		attr = new Attribute (bg);
-		Assert.True (attr.Initialized);
+		//Assert.True (attr.Initialized);
 		//Assert.True (attr.HasValidColors);
 		Assert.Equal (bg, attr.Foreground);
 		Assert.Equal (bg, attr.Background);
@@ -230,7 +230,7 @@ public class AttributeTests {
 
 		var a = new Attribute (fg, bg);
 
-		Assert.False (a.Initialized);
+		//Assert.False (a.Initialized);
 	}
 
 	[Fact]
@@ -247,7 +247,7 @@ public class AttributeTests {
 		bg = (Color)Color.Blue;
 
 		var attr = new Attribute (fg, bg);
-		Assert.True (attr.Initialized);
+		//Assert.True (attr.Initialized);
 		Assert.Equal (fg, attr.Foreground);
 		Assert.Equal (bg, attr.Background);
 
@@ -266,7 +266,7 @@ public class AttributeTests {
 		bg = (Color)Color.Blue;
 
 		var attr = new Attribute (fg, bg);
-		Assert.False (attr.Initialized);
+		//Assert.False (attr.Initialized);
 		Assert.Equal (fg, attr.Foreground);
 		Assert.Equal (bg, attr.Background);
 	}