Browse Source

Color scheme has been added

Miguel de Icaza 7 years ago
parent
commit
21586ae168
11 changed files with 85 additions and 35 deletions
  1. 34 3
      Core.cs
  2. 3 1
      Driver.cs
  3. 2 2
      Views/Button.cs
  4. 2 2
      Views/Checkbox.cs
  5. 2 0
      Views/Dialog.cs
  6. 1 1
      Views/Label.cs
  7. 8 6
      Views/Menu.cs
  8. 23 1
      Views/MessageBox.cs
  9. 2 2
      Views/RadioGroup.cs
  10. 1 15
      Views/TextField.cs
  11. 7 2
      demo.cs

+ 34 - 3
Core.cs

@@ -132,6 +132,19 @@ namespace Terminal {
 	///    Developers can call the SetNeedsDisplay method on the view to flag a region or the entire view
 	///    as requiring to be redrawn.
 	/// </para>
+	/// <para>
+	///    Views have a ColorScheme property that defines the default colors that subviews
+	///    should use for rendering.   This ensures that the views fit in the context where
+	///    they are being used, and allows for themes to be plugged in.   For example, the
+	///    default colors for windows and toplevels uses a blue background, while it uses 
+	///    a white background for dialog boxes and a red background for errors.
+	/// </para>
+	/// <para>
+	///    If a ColorScheme is not set on a view, the result of the ColorScheme is the
+	///    value of the SuperView and the value might only be valid once a view has been
+	///    added to a SuperView, so your subclasses should not rely on ColorScheme being
+	///    set at construction time.
+	/// </para>
 	/// </remarks>
 	public class View : Responder, IEnumerable {
 		string id = "";
@@ -530,6 +543,23 @@ namespace Terminal {
 			}
 		}
 
+		/// <summary>
+		/// The color scheme for this view, if it is not defined, it returns the parent's
+		/// color scheme.
+		/// </summary>
+		public ColorScheme ColorScheme {
+			get {
+				if (colorScheme == null)
+					return SuperView?.ColorScheme;
+				return colorScheme;
+			}
+			set {
+				colorScheme = value;
+			}
+		}
+
+		ColorScheme colorScheme;
+
 		/// <summary>
 		/// Displays the specified character in the specified column and row.
 		/// </summary>
@@ -808,6 +838,7 @@ namespace Terminal {
 		/// <param name="frame">Frame.</param>
 		public Toplevel (Rect frame) : base (frame)
 		{
+			ColorScheme = Colors.Base;
 		}
 
 		/// <summary>
@@ -930,10 +961,10 @@ namespace Terminal {
 		public override void Redraw (Rect bounds)
 		{
 			if (!NeedDisplay.IsEmpty) {
-				Driver.SetAttribute (Colors.Base.Normal);
+				Driver.SetAttribute (ColorScheme.Normal);
 				DrawFrame ();
 				if (HasFocus)
-					Driver.SetAttribute (Colors.Dialog.Normal);
+					Driver.SetAttribute (ColorScheme.Normal);
 				var width = Frame.Width;
 				if (Title != null && width > 4) {
 					Move (1, 0);
@@ -942,7 +973,7 @@ namespace Terminal {
 					Driver.AddStr (str);
 					Driver.AddCh (' ');
 				}
-				Driver.SetAttribute (Colors.Dialog.Normal);
+				Driver.SetAttribute (ColorScheme.Normal);
 			}
 			contentView.Redraw (contentView.Bounds);
 			ClearNeedsDisplay ();

+ 3 - 1
Driver.cs

@@ -54,7 +54,9 @@ namespace Terminal {
 	}
 
 	/// <summary>
-	/// Color scheme definitions
+	/// Color scheme definitions, they cover some common scenarios and are used
+	/// typically in toplevel containers to set the scheme that is used by all the
+	/// views contained inside.
 	/// </summary>
 	public class ColorScheme {
 		public Attribute Normal;

+ 2 - 2
Views/Button.cs

@@ -133,13 +133,13 @@ namespace Terminal {
 
 		public override void Redraw (Rect region)
 		{
-			Driver.SetAttribute (HasFocus ? Colors.Base.Focus : Colors.Base.Normal);
+			Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
 			Move (0, 0);
 			Driver.AddStr (shown_text);
 
 			if (hot_pos != -1) {
 				Move (hot_pos, 0);
-				Driver.SetAttribute (HasFocus ? Colors.Base.HotFocus : Colors.Base.HotNormal);
+				Driver.SetAttribute (HasFocus ? ColorScheme.HotFocus : ColorScheme.HotNormal);
 				Driver.AddCh (hot_key);
 			}
 		}

+ 2 - 2
Views/Checkbox.cs

@@ -82,14 +82,14 @@ namespace Terminal {
 
 		public override void Redraw (Rect region)
 		{
-			Driver.SetAttribute (HasFocus ? Colors.Base.Focus : Colors.Base.Normal);
+			Driver.SetAttribute (HasFocus ? ColorScheme.Focus : ColorScheme.Normal);
 			Move (0, 0);
 			Driver.AddStr (Checked ? "[x] " : "[ ] ");
 			Move (4, 0);
 			Driver.AddStr (Text);
 			if (hot_pos != -1) {
 				Move (4 + hot_pos, 0);
-				Driver.SetAttribute (HasFocus ? Colors.Base.HotFocus : Colors.Base.HotNormal);
+				Driver.SetAttribute (HasFocus ? ColorScheme.HotFocus : ColorScheme.HotNormal);
 				Driver.AddCh (hot_key);
 			}
 		}

+ 2 - 0
Views/Dialog.cs

@@ -25,6 +25,8 @@ namespace Terminal {
 		/// <param name="buttons">Optional buttons to lay out at the bottom of the dialog.</param>
 		public Dialog (string title, int width, int height, params Button [] buttons) : base (Application.MakeCenteredRect (new Size (width, height)), title)
 		{
+			ColorScheme = Colors.Dialog;
+
 			foreach (var b in buttons) {
 				this.buttons.Add (b);
 				Add (b);

+ 1 - 1
Views/Label.cs

@@ -127,7 +127,7 @@ namespace Terminal {
 			if (TextColor != -1)
 				Driver.SetAttribute (TextColor);
 			else
-				Driver.SetAttribute (Colors.Base.Normal);
+				Driver.SetAttribute (ColorScheme.Normal);
 
 			Clear ();
 			Move (Frame.X, Frame.Y);

+ 8 - 6
Views/Menu.cs

@@ -127,18 +127,19 @@ namespace Terminal {
 		{
 			this.barItems = barItems;
 			this.host = host;
+			ColorScheme = Colors.Menu;
 			CanFocus = true;
 		}
 
 		public override void Redraw (Rect region)
 		{
-			Driver.SetAttribute (Colors.Menu.Normal);
+			Driver.SetAttribute (ColorScheme.Normal);
 			DrawFrame (region, true);
 
 			for (int i = 0; i < barItems.Children.Length; i++){
 				var item = barItems.Children [i];
 				Move (1, i+1);
-				Driver.SetAttribute (item == null ? Colors.Base.Focus : i == current ? Colors.Menu.Focus : Colors.Menu.Normal);
+				Driver.SetAttribute (item == null ? Colors.Base.Focus : i == current ? ColorScheme.Focus : ColorScheme.Normal);
 				for (int p = 0; p < Frame.Width-2; p++)
 					if (item == null)
 						Driver.AddSpecial (SpecialChar.HLine);
@@ -150,8 +151,8 @@ namespace Terminal {
 
 				Move (2, i + 1);
 				DrawHotString (item.Title,
-				               i == current? Colors.Menu.HotFocus : Colors.Menu.HotNormal,
-				               i == current ? Colors.Menu.Focus : Colors.Menu.Normal);
+				               i == current? ColorScheme.HotFocus : ColorScheme.HotNormal,
+				               i == current ? ColorScheme.Focus : ColorScheme.Normal);
 
 				// The help string
 				var l = item.Help.Length;
@@ -269,6 +270,7 @@ namespace Terminal {
 			Menus = menus;
 			CanFocus = false;
 			selected = -1;
+			ColorScheme = Colors.Menu;
 		}
 
 		public override void Redraw (Rect region)
@@ -286,8 +288,8 @@ namespace Terminal {
 				Move (pos, 0);
 				Attribute hotColor, normalColor;
 				if (i == selected){
-					hotColor = i == selected ? Colors.Menu.HotFocus : Colors.Menu.HotNormal;
-					normalColor = i == selected ? Colors.Menu.Focus : Colors.Menu.Normal;
+					hotColor = i == selected ? ColorScheme.HotFocus : ColorScheme.HotNormal;
+					normalColor = i == selected ? ColorScheme.Focus : ColorScheme.Normal;
 				} else {
 					hotColor = Colors.Base.Focus;
 					normalColor = Colors.Base.Focus;

+ 23 - 1
Views/MessageBox.cs

@@ -6,7 +6,7 @@ namespace Terminal {
 	/// </summary>
 	public class MessageBox {
 		/// <summary>
-		/// Runs the dialog bo 
+		/// Presents a message with the specified title and message and a list of buttons to show to the user.
 		/// </summary>
 		/// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
 		/// <param name="width">Width for the window.</param>
@@ -15,11 +15,33 @@ namespace Terminal {
 		/// <param name="message">Message to display, might contain multiple lines..</param>
 		/// <param name="buttons">Array of buttons to add.</param>
 		public static int Query (int width, int height, string title, string message, params string [] buttons)
+		{
+			return QueryFull (false, width, height, title, message, buttons);
+		}
+
+		/// <summary>
+		/// Presents an error message box with the specified title and message and a list of buttons to show to the user.
+		/// </summary>
+		/// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
+		/// <param name="width">Width for the window.</param>
+		/// <param name="height">Height for the window.</param>
+		/// <param name="title">Title for the query.</param>
+		/// <param name="message">Message to display, might contain multiple lines..</param>
+		/// <param name="buttons">Array of buttons to add.</param>
+		public static int ErrorQuery (int width, int height, string title, string message, params string [] buttons)
+		{
+			return QueryFull (true, width, height, title, message, buttons);
+		}
+
+		static int QueryFull (bool useErrorColors, int width, int height, string title, string message, params string [] buttons)
 		{
 			int lines = Label.MeasureLines (message, width);
 			int clicked = -1, count = 0;
 
 			var d = new Dialog (title, width, height);
+			if (useErrorColors)
+				d.ColorScheme = Colors.Error;
+			
 			foreach (var s in buttons) {
 				int n = count++;
 				var b = new Button (s);

+ 2 - 2
Views/RadioGroup.cs

@@ -63,9 +63,9 @@ namespace Terminal {
 			base.Redraw (region);
 			for (int i = 0; i < radioLabels.Length; i++) {
 				Move (0, i);
-				Driver.SetAttribute (Colors.Base.Normal);
+				Driver.SetAttribute (ColorScheme.Normal);
 				Driver.AddStr (i == selected ? "(o) " : "( ) ");
-				DrawHotString (radioLabels [i], HasFocus && i == cursor, Colors.Base);
+				DrawHotString (radioLabels [i], HasFocus && i == cursor, ColorScheme);
 			}
 		}
 

+ 1 - 15
Views/TextField.cs

@@ -45,7 +45,6 @@ namespace Terminal {
 			point = s.Length;
 			first = point > w ? point - w : 0;
 			CanFocus = true;
-			Color = Colors.Dialog.Focus;
 		}
 
 		/// <summary>
@@ -75,19 +74,6 @@ namespace Terminal {
 		/// </remarks>
 		public bool Secret { get; set; }
 
-		Attribute color;
-		/// <summary>
-		/// Sets the color attribute to use (includes foreground and background).
-		/// </summary>
-		/// <value>The color.</value>
-		public Attribute Color {
-			get => color;
-			set {
-				color = value;
-				SetNeedsDisplay ();
-			}
-		}
-
 		/// <summary>
 		///    The current cursor position.
 		/// </summary>
@@ -103,7 +89,7 @@ namespace Terminal {
 
 		public override void Redraw (Rect region)
 		{
-			Driver.SetAttribute (Color);
+			Driver.SetAttribute (ColorScheme.Focus);
 			Move (0, 0);
 
 			for (int i = 0; i < Frame.Width; i++) {

+ 7 - 2
demo.cs

@@ -31,7 +31,7 @@ class Demo {
 	{
 		var d = new Dialog (
 			"New File", 50, 20,
-			new Button ("Ok", is_default: true ) { Clicked = () => { Application.RequestStop (); } },
+			new Button ("Ok", is_default: true) { Clicked = () => { Application.RequestStop (); } },
 			new Button ("Cancel") { Clicked = () => { Application.RequestStop (); } });
 		ml2 = new Label (1, 1, "Mouse Debug Line");
 		d.Add (ml2);
@@ -44,6 +44,11 @@ class Demo {
 		return n == 0;
 	}
 
+	static void Close ()
+	{
+		MessageBox.ErrorQuery (50, 5, "Error", "There is nothing to close", "Ok");
+	}
+
 	public static Label ml;
 	static void Main ()
 	{
@@ -56,7 +61,7 @@ class Demo {
 			new MenuBarItem ("_File", new MenuItem [] {
 				new MenuItem ("_New", "Creates new file", NewFile),
 				new MenuItem ("_Open", "", null),
-				new MenuItem ("_Close", "", null),
+				new MenuItem ("_Close", "", () => Close ()),
 				new MenuItem ("_Quit", "", () => { if (Quit ()) top.Running = false; })
 			}),
 			new MenuBarItem ("_Edit", new MenuItem [] {