Browse Source

Merge pull request #1031 from BDisp/gets-colorscheme-attributes

Fixes #1030. Getting colors from the Attributes of a ColorScheme.
Charlie Kindel 4 years ago
parent
commit
173146e66c

+ 19 - 3
Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs

@@ -40,7 +40,7 @@ namespace Terminal.Gui {
 		}
 
 		static bool sync = false;
-		public override void AddRune (Rune rune) 
+		public override void AddRune (Rune rune)
 		{
 			if (Clip.Contains (ccol, crow)) {
 				if (needMove) {
@@ -91,7 +91,14 @@ namespace Terminal.Gui {
 		}
 
 		public override void UpdateScreen () => window.redrawwin ();
-		public override void SetAttribute (Attribute c) => Curses.attrset (c.value);
+
+		int currentAttribute;
+
+		public override void SetAttribute (Attribute c) {
+			currentAttribute = c.Value;
+			Curses.attrset (currentAttribute);
+		}
+
 		public Curses.Window window;
 
 		static short last_color_pair = 16;
@@ -105,7 +112,11 @@ namespace Terminal.Gui {
 		public static Attribute MakeColor (short foreground, short background)
 		{
 			Curses.InitColorPair (++last_color_pair, foreground, background);
-			return new Attribute () { value = Curses.ColorPair (last_color_pair) };
+			return new Attribute (
+				value: Curses.ColorPair (last_color_pair),
+				foreground: (Color)foreground,
+				background: (Color)background
+				);
 		}
 
 		int [,] colorPairs = new int [16, 16];
@@ -848,6 +859,11 @@ namespace Terminal.Gui {
 			//mouseGrabbed = false;
 			//Curses.mouseinterval (lastMouseInterval);
 		}
+
+		public override Attribute GetAttribute ()
+		{
+			return currentAttribute;
+		}
 	}
 
 	internal static class Platform {

+ 13 - 3
Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs

@@ -113,7 +113,11 @@ namespace Terminal.Gui {
 		static Attribute MakeColor (ConsoleColor f, ConsoleColor b)
 		{
 			// Encode the colors into the int value.
-			return new Attribute () { value = ((((int)f) & 0xffff) << 16) | (((int)b) & 0xffff) };
+			return new Attribute (
+				value: ((((int)f) & 0xffff) << 16) | (((int)b) & 0xffff),
+				foreground: (Color)f,
+				background: (Color)b
+				);
 		}
 
 		public override void Init (Action terminalResized)
@@ -249,7 +253,7 @@ namespace Terminal.Gui {
 		int currentAttribute;
 		public override void SetAttribute (Attribute c)
 		{
-			currentAttribute = c.value;
+			currentAttribute = c.Value;
 		}
 
 		Key MapKey (ConsoleKeyInfo keyInfo)
@@ -390,9 +394,14 @@ namespace Terminal.Gui {
 			};
 		}
 
+		public override Attribute GetAttribute ()
+		{
+			return currentAttribute;
+		}
+
+		#region Unused
 		public override void SetColors (ConsoleColor foreground, ConsoleColor background)
 		{
-			throw new NotImplementedException ();
 		}
 
 		public override void SetColors (short foregroundColorId, short backgroundColorId)
@@ -407,6 +416,7 @@ namespace Terminal.Gui {
 		public override void UncookMouse ()
 		{
 		}
+		#endregion
 #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
 	}
 }

+ 13 - 2
Terminal.Gui/ConsoleDrivers/NetDriver.cs

@@ -95,7 +95,11 @@ namespace Terminal.Gui {
 		static Attribute MakeColor (ConsoleColor f, ConsoleColor b)
 		{
 			// Encode the colors into the int value.
-			return new Attribute () { value = ((((int)f) & 0xffff) << 16) | (((int)b) & 0xffff) };
+			return new Attribute (
+				value: ((((int)f) & 0xffff) << 16) | (((int)b) & 0xffff),
+				foreground: (Color)f,
+				background: (Color)b
+				);
 		}
 
 		bool isWinPlatform;
@@ -319,7 +323,7 @@ namespace Terminal.Gui {
 		int currentAttribute;
 		public override void SetAttribute (Attribute c)
 		{
-			currentAttribute = c.value;
+			currentAttribute = c.Value;
 		}
 
 		Key MapKey (ConsoleKeyInfo keyInfo)
@@ -492,6 +496,12 @@ namespace Terminal.Gui {
 			};
 		}
 
+		public override Attribute GetAttribute ()
+		{
+			return currentAttribute;
+		}
+
+		#region Unused
 		public override void SetColors (ConsoleColor foreground, ConsoleColor background)
 		{
 		}
@@ -507,6 +517,7 @@ namespace Terminal.Gui {
 		public override void UncookMouse ()
 		{
 		}
+		#endregion
 
 		//
 		// These are for the .NET driver, but running natively on Windows, wont run

+ 11 - 6
Terminal.Gui/ConsoleDrivers/WindowsDriver.cs

@@ -1236,17 +1236,17 @@ namespace Terminal.Gui {
 
 		public override void SetAttribute (Attribute c)
 		{
-			currentAttribute = c.value;
+			currentAttribute = c.Value;
 		}
 
 		Attribute MakeColor (ConsoleColor f, ConsoleColor b)
 		{
 			// Encode the colors into the int value.
-			return new Attribute () {
-				value = ((int)f | (int)b << 4),
-				foreground = (Color)f,
-				background = (Color)b
-			};
+			return new Attribute (
+				value: ((int)f | (int)b << 4),
+				foreground: (Color)f,
+				background: (Color)b
+			);
 		}
 
 		public override Attribute MakeAttribute (Color fore, Color back)
@@ -1312,6 +1312,11 @@ namespace Terminal.Gui {
 			winConsole.Cleanup ();
 		}
 
+		public override Attribute GetAttribute ()
+		{
+			return currentAttribute;
+		}
+
 		#region Unused
 		public override void SetColors (ConsoleColor foreground, ConsoleColor background)
 		{

+ 87 - 63
Terminal.Gui/Core/ConsoleDriver.cs

@@ -61,7 +61,7 @@ namespace Terminal.Gui {
 		/// </summary>
 		BrightGreen,
 		/// <summary>
-		/// The brigh cyan color.
+		/// The bright cyan color.
 		/// </summary>
 		BrighCyan,
 		/// <summary>
@@ -91,9 +91,18 @@ namespace Terminal.Gui {
 	///   class to define color schemes that can be used in your application.
 	/// </remarks>
 	public struct Attribute {
-		internal int value;
-		internal Color foreground;
-		internal Color background;
+		/// <summary>
+		/// The color attribute value.
+		/// </summary>
+		public int Value { get; }
+		/// <summary>
+		/// The foreground color.
+		/// </summary>
+		public Color Foreground { get; }
+		/// <summary>
+		/// The background color.
+		/// </summary>
+		public Color Background { get; }
 
 		/// <summary>
 		/// Initializes a new instance of the <see cref="Attribute"/> struct.
@@ -103,9 +112,9 @@ namespace Terminal.Gui {
 		/// <param name="background">Background</param>
 		public Attribute (int value, Color foreground = new Color (), Color background = new Color ())
 		{
-			this.value = value;
-			this.foreground = foreground;
-			this.background = background;
+			Value = value;
+			Foreground = foreground;
+			Background = background;
 		}
 
 		/// <summary>
@@ -115,9 +124,9 @@ namespace Terminal.Gui {
 		/// <param name="background">Background</param>
 		public Attribute (Color foreground = new Color (), Color background = new Color ())
 		{
-			this.value = value = ((int)foreground | (int)background << 4);
-			this.foreground = foreground;
-			this.background = background;
+			Value = (int)foreground | ((int)background << 4);
+			Foreground = foreground;
+			Background = background;
 		}
 
 		/// <summary>
@@ -125,7 +134,7 @@ namespace Terminal.Gui {
 		/// </summary>
 		/// <returns>The integer value stored in the attribute.</returns>
 		/// <param name="c">The attribute to convert</param>
-		public static implicit operator int (Attribute c) => c.value;
+		public static implicit operator int (Attribute c) => c.Value;
 
 		/// <summary>
 		/// Implicitly convert an integer value into an <see cref="Attribute"/>
@@ -146,6 +155,17 @@ namespace Terminal.Gui {
 				throw new InvalidOperationException ("The Application has not been initialized");
 			return Application.Driver.MakeAttribute (foreground, background);
 		}
+
+		/// <summary>
+		/// Gets the current <see cref="Attribute"/> from the driver.
+		/// </summary>
+		/// <returns>The current attribute.</returns>
+		public static Attribute Get ()
+		{
+			if (Application.Driver == null)
+				throw new InvalidOperationException ("The Application has not been initialized");
+			return Application.Driver.GetAttribute ();
+		}
 	}
 
 	/// <summary>
@@ -201,18 +221,18 @@ namespace Terminal.Gui {
 			case "TopLevel":
 				switch (callerMemberName) {
 				case "Normal":
-					HotNormal = Application.Driver.MakeAttribute (HotNormal.foreground, attribute.background);
+					HotNormal = Application.Driver.MakeAttribute (HotNormal.Foreground, attribute.Background);
 					break;
 				case "Focus":
-					HotFocus = Application.Driver.MakeAttribute (HotFocus.foreground, attribute.background);
+					HotFocus = Application.Driver.MakeAttribute (HotFocus.Foreground, attribute.Background);
 					break;
 				case "HotNormal":
-					HotFocus = Application.Driver.MakeAttribute (attribute.foreground, HotFocus.background);
+					HotFocus = Application.Driver.MakeAttribute (attribute.Foreground, HotFocus.Background);
 					break;
 				case "HotFocus":
-					HotNormal = Application.Driver.MakeAttribute (attribute.foreground, HotNormal.background);
-					if (Focus.foreground != attribute.background)
-						Focus = Application.Driver.MakeAttribute (Focus.foreground, attribute.background);
+					HotNormal = Application.Driver.MakeAttribute (attribute.Foreground, HotNormal.Background);
+					if (Focus.Foreground != attribute.Background)
+						Focus = Application.Driver.MakeAttribute (Focus.Foreground, attribute.Background);
 					break;
 				}
 				break;
@@ -220,19 +240,19 @@ namespace Terminal.Gui {
 			case "Base":
 				switch (callerMemberName) {
 				case "Normal":
-					HotNormal = Application.Driver.MakeAttribute (HotNormal.foreground, attribute.background);
+					HotNormal = Application.Driver.MakeAttribute (HotNormal.Foreground, attribute.Background);
 					break;
 				case "Focus":
-					HotFocus = Application.Driver.MakeAttribute (HotFocus.foreground, attribute.background);
+					HotFocus = Application.Driver.MakeAttribute (HotFocus.Foreground, attribute.Background);
 					break;
 				case "HotNormal":
-					HotFocus = Application.Driver.MakeAttribute (attribute.foreground, HotFocus.background);
-					Normal = Application.Driver.MakeAttribute (Normal.foreground, attribute.background);
+					HotFocus = Application.Driver.MakeAttribute (attribute.Foreground, HotFocus.Background);
+					Normal = Application.Driver.MakeAttribute (Normal.Foreground, attribute.Background);
 					break;
 				case "HotFocus":
-					HotNormal = Application.Driver.MakeAttribute (attribute.foreground, HotNormal.background);
-					if (Focus.foreground != attribute.background)
-						Focus = Application.Driver.MakeAttribute (Focus.foreground, attribute.background);
+					HotNormal = Application.Driver.MakeAttribute (attribute.Foreground, HotNormal.Background);
+					if (Focus.Foreground != attribute.Background)
+						Focus = Application.Driver.MakeAttribute (Focus.Foreground, attribute.Background);
 					break;
 				}
 				break;
@@ -240,57 +260,56 @@ namespace Terminal.Gui {
 			case "Menu":
 				switch (callerMemberName) {
 				case "Normal":
-					if (Focus.background != attribute.background)
-						Focus = Application.Driver.MakeAttribute (attribute.foreground, Focus.background);
-					HotNormal = Application.Driver.MakeAttribute (HotNormal.foreground, attribute.background);
-					Disabled = Application.Driver.MakeAttribute (Disabled.foreground, attribute.background);
+					if (Focus.Background != attribute.Background)
+						Focus = Application.Driver.MakeAttribute (attribute.Foreground, Focus.Background);
+					HotNormal = Application.Driver.MakeAttribute (HotNormal.Foreground, attribute.Background);
+					Disabled = Application.Driver.MakeAttribute (Disabled.Foreground, attribute.Background);
 					break;
 				case "Focus":
-					Normal = Application.Driver.MakeAttribute (attribute.foreground, Normal.background);
-					HotFocus = Application.Driver.MakeAttribute (HotFocus.foreground, attribute.background);
+					Normal = Application.Driver.MakeAttribute (attribute.Foreground, Normal.Background);
+					HotFocus = Application.Driver.MakeAttribute (HotFocus.Foreground, attribute.Background);
 					break;
 				case "HotNormal":
-					if (Focus.background != attribute.background)
-						HotFocus = Application.Driver.MakeAttribute (attribute.foreground, HotFocus.background);
-					Normal = Application.Driver.MakeAttribute (Normal.foreground, attribute.background);
-					Disabled = Application.Driver.MakeAttribute (Disabled.foreground, attribute.background);
+					if (Focus.Background != attribute.Background)
+						HotFocus = Application.Driver.MakeAttribute (attribute.Foreground, HotFocus.Background);
+					Normal = Application.Driver.MakeAttribute (Normal.Foreground, attribute.Background);
+					Disabled = Application.Driver.MakeAttribute (Disabled.Foreground, attribute.Background);
 					break;
 				case "HotFocus":
-					HotNormal = Application.Driver.MakeAttribute (attribute.foreground, HotNormal.background);
-					if (Focus.foreground != attribute.background)
-						Focus = Application.Driver.MakeAttribute (Focus.foreground, attribute.background);
+					HotNormal = Application.Driver.MakeAttribute (attribute.Foreground, HotNormal.Background);
+					if (Focus.Foreground != attribute.Background)
+						Focus = Application.Driver.MakeAttribute (Focus.Foreground, attribute.Background);
 					break;
 				case "Disabled":
-					if (Focus.background != attribute.background)
-						HotFocus = Application.Driver.MakeAttribute (attribute.foreground, HotFocus.background);
-					Normal = Application.Driver.MakeAttribute (Normal.foreground, attribute.background);
-					HotNormal = Application.Driver.MakeAttribute (HotNormal.foreground, attribute.background);
+					if (Focus.Background != attribute.Background)
+						HotFocus = Application.Driver.MakeAttribute (attribute.Foreground, HotFocus.Background);
+					Normal = Application.Driver.MakeAttribute (Normal.Foreground, attribute.Background);
+					HotNormal = Application.Driver.MakeAttribute (HotNormal.Foreground, attribute.Background);
 					break;
-
 				}
 				break;
 
 			case "Dialog":
 				switch (callerMemberName) {
 				case "Normal":
-					if (Focus.background != attribute.background)
-						Focus = Application.Driver.MakeAttribute (attribute.foreground, Focus.background);
-					HotNormal = Application.Driver.MakeAttribute (HotNormal.foreground, attribute.background);
+					if (Focus.Background != attribute.Background)
+						Focus = Application.Driver.MakeAttribute (attribute.Foreground, Focus.Background);
+					HotNormal = Application.Driver.MakeAttribute (HotNormal.Foreground, attribute.Background);
 					break;
 				case "Focus":
-					Normal = Application.Driver.MakeAttribute (attribute.foreground, Normal.background);
-					HotFocus = Application.Driver.MakeAttribute (HotFocus.foreground, attribute.background);
+					Normal = Application.Driver.MakeAttribute (attribute.Foreground, Normal.Background);
+					HotFocus = Application.Driver.MakeAttribute (HotFocus.Foreground, attribute.Background);
 					break;
 				case "HotNormal":
-					if (Focus.background != attribute.background)
-						HotFocus = Application.Driver.MakeAttribute (attribute.foreground, HotFocus.background);
-					if (Normal.foreground != attribute.background)
-						Normal = Application.Driver.MakeAttribute (Normal.foreground, attribute.background);
+					if (Focus.Background != attribute.Background)
+						HotFocus = Application.Driver.MakeAttribute (attribute.Foreground, HotFocus.Background);
+					if (Normal.Foreground != attribute.Background)
+						Normal = Application.Driver.MakeAttribute (Normal.Foreground, attribute.Background);
 					break;
 				case "HotFocus":
-					HotNormal = Application.Driver.MakeAttribute (attribute.foreground, HotNormal.background);
-					if (Focus.foreground != attribute.background)
-						Focus = Application.Driver.MakeAttribute (Focus.foreground, attribute.background);
+					HotNormal = Application.Driver.MakeAttribute (attribute.Foreground, HotNormal.Background);
+					if (Focus.Foreground != attribute.Background)
+						Focus = Application.Driver.MakeAttribute (Focus.Foreground, attribute.Background);
 					break;
 				}
 				break;
@@ -298,17 +317,16 @@ namespace Terminal.Gui {
 			case "Error":
 				switch (callerMemberName) {
 				case "Normal":
-					HotNormal = Application.Driver.MakeAttribute (HotNormal.foreground, attribute.background);
-					HotFocus = Application.Driver.MakeAttribute (HotFocus.foreground, attribute.background);
+					HotNormal = Application.Driver.MakeAttribute (HotNormal.Foreground, attribute.Background);
+					HotFocus = Application.Driver.MakeAttribute (HotFocus.Foreground, attribute.Background);
 					break;
 				case "HotNormal":
 				case "HotFocus":
-					HotFocus = Application.Driver.MakeAttribute (attribute.foreground, attribute.background);
-					Normal = Application.Driver.MakeAttribute (Normal.foreground, attribute.background);
+					HotFocus = Application.Driver.MakeAttribute (attribute.Foreground, attribute.Background);
+					Normal = Application.Driver.MakeAttribute (Normal.Foreground, attribute.Background);
 					break;
 				}
 				break;
-
 			}
 			preparingScheme = false;
 			return attribute;
@@ -383,7 +401,7 @@ namespace Terminal.Gui {
 	public static class Colors {
 		static Colors ()
 		{
-			// Use reflection to dynamically create the default set of ColorSchemes from the list defiined 
+			// Use reflection to dynamically create the default set of ColorSchemes from the list defined 
 			// by the class. 
 			ColorSchemes = typeof (Colors).GetProperties ()
 				.Where (p => p.PropertyType == typeof (ColorScheme))
@@ -661,7 +679,7 @@ namespace Terminal.Gui {
 		/// <param name="paddingTop">Number of rows to pad on the top (if 0 the border and title will not appear on the top).</param>
 		/// <param name="paddingRight">Number of columns to pad on the right (if 0 the border will not appear on the right).</param>
 		/// <param name="paddingBottom">Number of rows to pad on the bottom (if 0 the border will not appear on the bottom).</param>
-		/// <param name="textAlignment">Not yet immplemented.</param>
+		/// <param name="textAlignment">Not yet implemented.</param>
 		/// <remarks></remarks>
 		public virtual void DrawWindowTitle (Rect region, ustring title, int paddingLeft, int paddingTop, int paddingRight, int paddingBottom, TextAlignment textAlignment = TextAlignment.Left)
 		{
@@ -676,7 +694,7 @@ namespace Terminal.Gui {
 		}
 
 		/// <summary>
-		/// Enables diagnostic funcions
+		/// Enables diagnostic functions
 		/// </summary>
 		[Flags]
 		public enum DiagnosticFlags : uint {
@@ -867,7 +885,7 @@ namespace Terminal.Gui {
 		/// <param name="region">Screen relative region where the frame will be drawn.</param>
 		/// <param name="padding">Padding to add on the sides.</param>
 		/// <param name="fill">If set to <c>true</c> it will clear the contents with the current color, otherwise the contents will be left untouched.</param>
-		/// <remarks>This API has been superceded by <see cref="DrawWindowFrame(Rect, int, int, int, int, bool, bool)"/>.</remarks>
+		/// <remarks>This API has been superseded by <see cref="DrawWindowFrame(Rect, int, int, int, int, bool, bool)"/>.</remarks>
 		/// <remarks>This API is equivalent to calling <c>DrawWindowFrame(Rect, p - 1, p - 1, p - 1, p - 1)</c>. In other words,
 		/// A padding value of 0 means there is actually a one cell border.
 		/// </remarks>
@@ -1053,5 +1071,11 @@ namespace Terminal.Gui {
 		/// <param name="back">Background.</param>
 		/// <returns></returns>
 		public abstract Attribute MakeAttribute (Color fore, Color back);
+
+		/// <summary>
+		/// Gets the current <see cref="Attribute"/>.
+		/// </summary>
+		/// <returns>The current attribute.</returns>
+		public abstract Attribute GetAttribute ();
 	}
 }