diff --git a/Terminal.Gui/Application.cs b/Terminal.Gui/Application.cs
index 2dbdf993f..5ea981b7b 100644
--- a/Terminal.Gui/Application.cs
+++ b/Terminal.Gui/Application.cs
@@ -43,9 +43,9 @@ namespace Terminal.Gui {
public static partial class Application {
///
- /// The current in use.
+ /// Gets the that has been selected. See also .
///
- public static ConsoleDriver Driver;
+ public static ConsoleDriver Driver { get; internal set; }
///
/// If , forces the use of the System.Console-based (see ) driver. The default is .
@@ -53,6 +53,14 @@ namespace Terminal.Gui {
[SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
public static bool UseSystemConsole { get; set; } = false;
+ ///
+ /// Gets or sets whether will be forced to output only the 16 colors defined in .
+ /// The default is , meaning 24-bit (TrueColor) colors will be output as long as the selected
+ /// supports TrueColor.
+ ///
+ [SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
+ public static bool Force16Colors { get; set; } = false;
+
// For Unit testing - ignores UseSystemConsole
internal static bool _forceFakeConsole;
@@ -78,7 +86,6 @@ namespace Terminal.Gui {
// Return all culture for which satellite folder found with culture code.
return culture.Where (cultureInfo =>
- assemblyLocation != null &&
Directory.Exists (Path.Combine (assemblyLocation, cultureInfo.Name)) &&
File.Exists (Path.Combine (assemblyLocation, cultureInfo.Name, resourceFilename))
).ToList ();
@@ -103,7 +110,7 @@ namespace Terminal.Gui {
///
/// The function
/// combines and
- /// into a single call. An applciation cam use
+ /// into a single call. An application cam use
/// without explicitly calling .
///
///
@@ -402,10 +409,7 @@ namespace Terminal.Gui {
///
/// See for more details.
///
- public static void Run (Func errorHandler = null)
- {
- Run (Top, errorHandler);
- }
+ public static void Run (Func errorHandler = null) => Run (Top, errorHandler);
///
/// Runs the application by calling
@@ -442,7 +446,7 @@ namespace Terminal.Gui {
}
Run (top, errorHandler);
} else {
- // This codepath should be impossible because Init(null, null) will select the platform default driver
+ // This code path should be impossible because Init(null, null) will select the platform default driver
throw new InvalidOperationException ("Init() completed without a driver being set (this should be impossible); Run() cannot be called.");
}
} else {
@@ -558,11 +562,11 @@ namespace Terminal.Gui {
// users use async/await on their code
//
class MainLoopSyncContext : SynchronizationContext {
- readonly MainLoop mainLoop;
+ readonly MainLoop _mainLoop;
public MainLoopSyncContext (MainLoop mainLoop)
{
- this.mainLoop = mainLoop;
+ this._mainLoop = mainLoop;
}
public override SynchronizationContext CreateCopy ()
@@ -572,11 +576,11 @@ namespace Terminal.Gui {
public override void Post (SendOrPostCallback d, object state)
{
- mainLoop.AddIdle (() => {
+ _mainLoop.AddIdle (() => {
d (state);
return false;
});
- //mainLoop.Driver.Wakeup ();
+ //_mainLoop.Driver.Wakeup ();
}
public override void Send (SendOrPostCallback d, object state)
@@ -585,7 +589,7 @@ namespace Terminal.Gui {
d (state);
} else {
var wasExecuted = false;
- mainLoop.Invoke (() => {
+ _mainLoop.Invoke (() => {
d (state);
wasExecuted = true;
});
@@ -602,12 +606,14 @@ namespace Terminal.Gui {
/// The state returned by the method.
public static void RunLoop (RunState state)
{
- if (state == null)
+ if (state == null) {
throw new ArgumentNullException (nameof (state));
- if (state.Toplevel == null)
+ }
+ if (state.Toplevel == null) {
throw new ObjectDisposedException ("state");
+ }
- bool firstIteration = true;
+ var firstIteration = true;
for (state.Toplevel.Running = true; state.Toplevel.Running;) {
if (ExitRunLoopAfterFirstIteration && !firstIteration) {
return;
@@ -689,14 +695,6 @@ namespace Terminal.Gui {
}
}
- /////
- ///// Wakes up the that might be waiting on input; must be thread safe.
- /////
- //public static void DoEvents ()
- //{
- // MainLoop.MainLoopDriver.Wakeup ();
- //}
-
///
/// Stops running the most recent or the if provided.
///
@@ -1085,7 +1083,7 @@ namespace Terminal.Gui {
///
/// Merely a debugging aid to see the raw mouse events
///
- public static Action RootMouseEvent;
+ public static Action RootMouseEvent { get; set; }
static View _lastMouseOwnerView;
@@ -1312,7 +1310,7 @@ namespace Terminal.Gui {
///
/// Return true to suppress the KeyPress event
///
- public static Func RootKeyEvent;
+ public static Func RootKeyEvent { get; set; }
#endregion Keyboard handling
}
diff --git a/Terminal.Gui/Configuration/AttributeJsonConverter.cs b/Terminal.Gui/Configuration/AttributeJsonConverter.cs
index b6680f4c1..66d085baf 100644
--- a/Terminal.Gui/Configuration/AttributeJsonConverter.cs
+++ b/Terminal.Gui/Configuration/AttributeJsonConverter.cs
@@ -30,14 +30,14 @@ namespace Terminal.Gui {
}
Attribute attribute = new Attribute ();
- Color foreground = (Color)(-1);
- Color background = (Color)(-1);
+ Color foreground = null;
+ Color background = null;
while (reader.Read ()) {
if (reader.TokenType == JsonTokenType.EndObject) {
- if (foreground == (Color)(-1) || background == (Color)(-1)) {
+ if (foreground == null || background == null) {
throw new JsonException ($"Both Foreground and Background colors must be provided.");
- }
- return attribute;
+ }
+ return new Attribute (foreground, background);
}
if (reader.TokenType != JsonTokenType.PropertyName) {
@@ -80,10 +80,6 @@ namespace Terminal.Gui {
default:
throw new JsonException ($"Unknown Attribute property {propertyName}.");
}
-
- if (foreground != (Color)(-1) && background != (Color)(-1)) {
- attribute = new Attribute (foreground, background);
- }
}
throw new JsonException ();
}
diff --git a/Terminal.Gui/ConsoleDrivers/ConsoleDriver.cs b/Terminal.Gui/ConsoleDrivers/ConsoleDriver.cs
index e60ea40bf..2a00b7a58 100644
--- a/Terminal.Gui/ConsoleDrivers/ConsoleDriver.cs
+++ b/Terminal.Gui/ConsoleDrivers/ConsoleDriver.cs
@@ -365,20 +365,20 @@ public abstract class ConsoleDriver {
///
public virtual bool SupportsTrueColor { get => true; }
- bool _force16Colors = false;
-
- // TODO: Make this a ConfiguationManager setting on Application
///
- /// Gets or sets whether the should use 16 colors instead of the default TrueColors.
+ /// Gets or sets whether the should use 16 colors instead of the default TrueColors. See
+ /// to change this setting via .
///
///
+ ///
/// Will be forced to if is , indicating
/// that the cannot support TrueColor.
+ ///
///
- public virtual bool Force16Colors {
- get => _force16Colors || !SupportsTrueColor;
+ internal virtual bool Force16Colors {
+ get => Application.Force16Colors || !SupportsTrueColor;
set {
- _force16Colors = (value || !SupportsTrueColor);
+ Application.Force16Colors = (value || !SupportsTrueColor);
}
}
@@ -430,22 +430,22 @@ public abstract class ConsoleDriver {
///
/// The current attribute.
public Attribute GetAttribute () => CurrentAttribute;
-
- ///
- /// Makes an .
- ///
- /// The foreground color.
- /// The background color.
- /// The attribute for the foreground and background colors.
- public abstract Attribute MakeColor (ColorNames foregroundName, ColorNames backgroundName);
-
+
///
/// Makes an .
///
/// The foreground color.
/// The background color.
/// The attribute for the foreground and background colors.
- public abstract Attribute MakeColor (Color foreground, Color background);
+ public virtual Attribute MakeColor (Color foreground, Color background)
+ {
+ // Encode the colors into the int value.
+ return new Attribute (
+ platformColor: 0, // Not used anymore by anyting but cursesdriver!
+ foreground: foreground,
+ background: background
+ );
+ }
///
/// Ensures all s in are correctly
diff --git a/Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs b/Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs
index 37389be39..819934999 100644
--- a/Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs
+++ b/Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs
@@ -88,7 +88,7 @@ internal class CursesDriver : ConsoleDriver {
/// and the background color is stored in the least significant 4 bits.
/// The Terminal.GUi Color values are converted to curses color encoding before being encoded.
///
- public override Attribute MakeColor (ColorNames foregroundName, ColorNames backgroundName)
+ private Attribute MakeColor (ColorNames foregroundName, ColorNames backgroundName)
{
if (!RunningUnitTests) {
return MakeColor (ColorNameToCursesColorNumber (foregroundName), ColorNameToCursesColorNumber (backgroundName));
@@ -244,7 +244,7 @@ internal class CursesDriver : ConsoleDriver {
// In unit tests, we don't want to actually write to the screen.
continue;
}
- Curses.attrset (Contents [row, col].Attribute.GetValueOrDefault ().Value);
+ Curses.attrset (Contents [row, col].Attribute.GetValueOrDefault ().PlatformColor);
var rune = Contents [row, col].Runes [0];
if (rune.IsBmp) {
diff --git a/Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs b/Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs
index d00b85f4c..b7770d619 100644
--- a/Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs
+++ b/Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs
@@ -81,7 +81,7 @@ public class FakeDriver : ConsoleDriver {
FakeConsole.Clear ();
ResizeScreen ();
// Call InitializeColorSchemes before UpdateOffScreen as it references Colors
- CurrentAttribute = MakeColor (Color.White, Color.Black);
+ CurrentAttribute = new Attribute (Color.White, Color.Black);
InitializeColorSchemes ();
ClearContents ();
}
@@ -134,8 +134,8 @@ public class FakeDriver : ConsoleDriver {
// Performance: Only send the escape sequence if the attribute has changed.
if (attr != redrawAttr) {
redrawAttr = attr;
- FakeConsole.ForegroundColor = (ConsoleColor)attr.Foreground.Value;
- FakeConsole.BackgroundColor = (ConsoleColor)attr.Background.Value;
+ FakeConsole.ForegroundColor = (ConsoleColor)attr.Foreground.ColorName;
+ FakeConsole.BackgroundColor = (ConsoleColor)attr.Background.ColorName;
}
outputWidth++;
var rune = (Rune)Contents [row, col].Runes [0];
@@ -187,25 +187,21 @@ public class FakeDriver : ConsoleDriver {
#region Color Handling
- ///
- /// In the FakeDriver, colors are encoded as an int; same as NetDriver
- /// However, the foreground color is stored in the most significant 16 bits,
- /// and the background color is stored in the least significant 16 bits.
- ///
- public override Attribute MakeColor (Color foreground, Color background)
- {
- // Encode the colors into the int value.
- return new Attribute (
- platformColor: ((((int)foreground.ColorName) & 0xffff) << 16) | (((int)background.ColorName) & 0xffff),
- foreground: foreground,
- background: background
- );
- }
+ /////
+ ///// In the FakeDriver, colors are encoded as an int; same as NetDriver
+ ///// However, the foreground color is stored in the most significant 16 bits,
+ ///// and the background color is stored in the least significant 16 bits.
+ /////
+ //public override Attribute MakeColor (Color foreground, Color background)
+ //{
+ // // Encode the colors into the int value.
+ // return new Attribute (
+ // platformColor: 0,//((((int)foreground.ColorName) & 0xffff) << 16) | (((int)background.ColorName) & 0xffff),
+ // foreground: foreground,
+ // background: background
+ // );
+ //}
- public override Attribute MakeColor (ColorNames foreground, ColorNames background)
- {
- return MakeColor (new Color (foreground), new Color (background));
- }
#endregion
public ConsoleKeyInfo FromVKPacketToKConsoleKeyInfo (ConsoleKeyInfo consoleKeyInfo)
diff --git a/Terminal.Gui/ConsoleDrivers/NetDriver.cs b/Terminal.Gui/ConsoleDrivers/NetDriver.cs
index 08f6dc9fa..3297763fb 100644
--- a/Terminal.Gui/ConsoleDrivers/NetDriver.cs
+++ b/Terminal.Gui/ConsoleDrivers/NetDriver.cs
@@ -695,7 +695,7 @@ internal class NetDriver : ConsoleDriver {
ResizeScreen ();
ClearContents ();
- CurrentAttribute = MakeColor (Color.White, Color.Black);
+ CurrentAttribute = new Attribute (Color.White, Color.Black);
InitializeColorSchemes ();
StartReportingMouseMoves ();
@@ -793,7 +793,7 @@ internal class NetDriver : ConsoleDriver {
if (Force16Colors) {
output.Append (EscSeqUtils.CSI_SetGraphicsRendition (
- MapColors ((ConsoleColor)attr.Background.Value, false), MapColors ((ConsoleColor)attr.Foreground.Value, true)));
+ MapColors ((ConsoleColor)attr.Background.ColorName, false), MapColors ((ConsoleColor)attr.Foreground.ColorName, true)));
} else {
output.Append (EscSeqUtils.CSI_SetForegroundColorRGB (attr.Foreground.R, attr.Foreground.G, attr.Foreground.B));
output.Append (EscSeqUtils.CSI_SetBackgroundColorRGB (attr.Background.R, attr.Background.G, attr.Background.B));
@@ -862,25 +862,20 @@ internal class NetDriver : ConsoleDriver {
return colorMap.TryGetValue (color, out var colorValue) ? colorValue + (isForeground ? 0 : 10) : 0;
}
- ///
- /// In the NetDriver, colors are encoded as an int.
- /// However, the foreground color is stored in the most significant 16 bits,
- /// and the background color is stored in the least significant 16 bits.
- ///
- public override Attribute MakeColor (Color foreground, Color background)
- {
- // Encode the colors into the int value.
- return new Attribute (
- platformColor: ((((int)foreground.ColorName) & 0xffff) << 16) | (((int)background.ColorName) & 0xffff),
- foreground: foreground,
- background: background
- );
- }
-
- public override Attribute MakeColor (ColorNames foreground, ColorNames background)
- {
- return MakeColor (new Color (foreground), new Color (background));
- }
+ /////
+ ///// In the NetDriver, colors are encoded as an int.
+ ///// However, the foreground color is stored in the most significant 16 bits,
+ ///// and the background color is stored in the least significant 16 bits.
+ /////
+ //public override Attribute MakeColor (Color foreground, Color background)
+ //{
+ // // Encode the colors into the int value.
+ // return new Attribute (
+ // platformColor: ((((int)foreground.ColorName) & 0xffff) << 16) | (((int)background.ColorName) & 0xffff),
+ // foreground: foreground,
+ // background: background
+ // );
+ //}
#endregion
#region Cursor Handling
diff --git a/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs b/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs
index cb78c4c4a..931a08a7f 100644
--- a/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs
+++ b/Terminal.Gui/ConsoleDrivers/WindowsDriver.cs
@@ -67,7 +67,7 @@ internal class WindowsConsole {
foreach (ExtendedCharInfo info in charInfoBuffer) {
ci [i++] = new CharInfo () {
Char = new CharUnion () { UnicodeChar = info.Char },
- Attributes = (ushort)info.Attribute.Value
+ Attributes = (ushort)(((int)info.Attribute.Foreground.ColorName) | ((int)info.Attribute.Background.ColorName << 4))
};
}
@@ -1542,7 +1542,7 @@ internal class WindowsDriver : ConsoleDriver {
WinConsole = null;
}
- CurrentAttribute = MakeColor (Color.White, Color.Black);
+ CurrentAttribute = new Attribute (Color.White, Color.Black);
InitializeColorSchemes ();
_outputBuffer = new WindowsConsole.ExtendedCharInfo [Rows * Cols];
@@ -1649,15 +1649,11 @@ internal class WindowsDriver : ConsoleDriver {
{
// Encode the colors into the int value.
return new Attribute (
- platformColor: (((int)foreground.ColorName) | ((int)background.ColorName << 4)),
+ platformColor: 0, // Not used anymore! (((int)foreground.ColorName) | ((int)background.ColorName << 4)),
foreground: foreground,
background: background
);
}
- public override Attribute MakeColor (ColorNames foreground, ColorNames background)
- {
- return MakeColor (new Color (foreground), new Color (background));
- }
#endregion
diff --git a/Terminal.Gui/Drawing/Color.cs b/Terminal.Gui/Drawing/Color.cs
index 37dfb69b2..47d0884f3 100644
--- a/Terminal.Gui/Drawing/Color.cs
+++ b/Terminal.Gui/Drawing/Color.cs
@@ -256,7 +256,7 @@ namespace Terminal.Gui {
/// Get returns the of the closest 24-bit color value. Set sets the RGB value using a hard-coded map.
///
public ColorNames ColorName {
- get => FindClosestColor (this.Value);
+ get => FindClosestColor (this);
set {
var c = FromColorName (value);
@@ -393,7 +393,7 @@ namespace Terminal.Gui {
return true;
}
- // rgb(XX,YY,ZZ)
+ // rgb(r,g,b)
var match = Regex.Match (text, @"rgb\((\d+),(\d+),(\d+)\)");
if (match.Success) {
var r = int.Parse (match.Groups [1].Value);
@@ -403,7 +403,7 @@ namespace Terminal.Gui {
return true;
}
- // rgb(AA,XX,YY,ZZ)
+ // rgb(a,r,g,b)
match = Regex.Match (text, @"rgb\((\d+),(\d+),(\d+),(\d+)\)");
if (match.Success) {
var a = int.Parse (match.Groups [1].Value);
@@ -464,9 +464,16 @@ namespace Terminal.Gui {
///
public static bool operator == (Color left, Color right)
{
+ if (left is null && right is null)
+ return true;
+
+ if (left is null || right is null)
+ return false;
+
return left.Equals (right);
}
+
///
/// Inequality operator for two objects.
///
@@ -475,6 +482,12 @@ namespace Terminal.Gui {
///
public static bool operator != (Color left, Color right)
{
+ if (left is null && right is null)
+ return false;
+
+ if (left is null || right is null)
+ return true;
+
return !left.Equals (right);
}
@@ -587,7 +600,7 @@ namespace Terminal.Gui {
/// and the attribute should be re-made before it is used.
///
[JsonIgnore (Condition = JsonIgnoreCondition.Always)]
- internal int Value { get; }
+ internal int PlatformColor { get; }
///
/// The foreground color.
@@ -607,7 +620,7 @@ namespace Terminal.Gui {
public Attribute ()
{
var d = Default;
- Value = -1;
+ PlatformColor = -1;
Foreground = d.Foreground;
Background = d.Background;
}
@@ -628,7 +641,7 @@ namespace Terminal.Gui {
{
Foreground = foreground;
Background = background;
- Value = platformColor;
+ PlatformColor = platformColor;
Initialized = true;
}
@@ -653,13 +666,13 @@ namespace Terminal.Gui {
if (Application.Driver == null) {
// Create the attribute, but show it's not been initialized
Initialized = false;
- Value = -1;
+ PlatformColor = -1;
return;
}
var make = Application.Driver.MakeAttribute (foreground, background);
Initialized = make.Initialized;
- Value = make.Value;
+ PlatformColor = make.PlatformColor;
}
///
@@ -724,17 +737,17 @@ namespace Terminal.Gui {
///
public bool Equals (Attribute other)
{
- return Value == other.Value &&
+ return PlatformColor == other.PlatformColor &&
Foreground == other.Foreground &&
Background == other.Background;
}
///
- public override int GetHashCode () => HashCode.Combine (Value, Foreground, Background);
+ public override int GetHashCode () => HashCode.Combine (PlatformColor, Foreground, Background);
///
/// If the attribute has been initialized by a and
- /// thus has that is valid for that driver. If the
+ /// thus has that is valid for that driver. If the
/// and colors may have been set '-1' but
/// the attribute has not been mapped to a specific color value.
///
diff --git a/UICatalog/Scenarios/Images.cs b/UICatalog/Scenarios/Images.cs
index 370cefc42..e4896c241 100644
--- a/UICatalog/Scenarios/Images.cs
+++ b/UICatalog/Scenarios/Images.cs
@@ -34,10 +34,10 @@ namespace UICatalog.Scenarios {
var cbUseTrueColor = new CheckBox ("Use true color") {
X = Pos.Right(cbSupportsTrueColor) + 2,
Y = 0,
- Checked = !Application.Driver.Force16Colors,
+ Checked = !Application.Force16Colors,
Enabled = canTrueColor,
};
- cbUseTrueColor.Toggled += (_, evt) => Application.Driver.Force16Colors = !evt.NewValue ?? false;
+ cbUseTrueColor.Toggled += (_, evt) => Application.Force16Colors = !evt.NewValue ?? false;
Win.Add (cbUseTrueColor);
var btnOpenImage = new Button ("Open Image") {
diff --git a/UICatalog/Scenarios/TrueColors.cs b/UICatalog/Scenarios/TrueColors.cs
index 9ecb8b125..c2f15d5e9 100644
--- a/UICatalog/Scenarios/TrueColors.cs
+++ b/UICatalog/Scenarios/TrueColors.cs
@@ -32,11 +32,11 @@ namespace UICatalog.Scenarios {
var cbUseTrueColor = new CheckBox ("Force 16 colors") {
X = x,
Y = y++,
- Checked = Application.Driver.Force16Colors,
+ Checked = Application.Force16Colors,
Enabled = canTrueColor,
};
cbUseTrueColor.Toggled += (_, evt) => {
- Application.Driver.Force16Colors = evt.NewValue ?? false;
+ Application.Force16Colors = evt.NewValue ?? false;
};
Win.Add (cbUseTrueColor);
diff --git a/UICatalog/UICatalog.cs b/UICatalog/UICatalog.cs
index e4948aacf..94fca7da4 100644
--- a/UICatalog/UICatalog.cs
+++ b/UICatalog/UICatalog.cs
@@ -117,7 +117,6 @@ namespace UICatalog {
scenario.Theme = _cachedTheme;
scenario.TopLevelColorScheme = _topLevelColorScheme;
scenario.Init ();
- Application.Driver.Force16Colors = _force16Colors;
scenario.Setup ();
scenario.Run ();
scenario.Dispose ();
@@ -232,7 +231,6 @@ namespace UICatalog {
static Scenario? _selectedScenario = null;
static bool _useSystemConsole = false;
- static bool _force16Colors = false;
static ConsoleDriver.DiagnosticFlags _diagnosticFlags;
static bool _isFirstRunning = true;
static string _topLevelColorScheme = string.Empty;
@@ -483,8 +481,6 @@ namespace UICatalog {
List