Browse Source

Fixed ColorPicker

Tig Kindel 1 year ago
parent
commit
c819b6ef17
2 changed files with 228 additions and 234 deletions
  1. 227 233
      Terminal.Gui/Views/ColorPicker.cs
  2. 1 1
      UICatalog/Scenarios/ColorPicker.cs

+ 227 - 233
Terminal.Gui/Views/ColorPicker.cs

@@ -1,281 +1,275 @@
 using System;
 using System.Text;
 
-namespace Terminal.Gui {
+namespace Terminal.Gui; 
 
+/// <summary>
+/// Event arguments for the <see cref="Color"/> events.
+/// </summary>
+public class ColorEventArgs : EventArgs {
 	/// <summary>
-	/// Event arguments for the <see cref="Color"/> events.
+	/// Initializes a new instance of <see cref="ColorEventArgs"/>
 	/// </summary>
-	public class ColorEventArgs : EventArgs {
+	public ColorEventArgs () { }
 
-		/// <summary>
-		/// Initializes a new instance of <see cref="ColorEventArgs"/>
-		/// </summary>
-		public ColorEventArgs ()
-		{
-		}
+	/// <summary>
+	/// The new Thickness.
+	/// </summary>
+	public Color Color { get; set; }
+
+	/// <summary>
+	/// The previous Thickness.
+	/// </summary>
+	public Color PreviousColor { get; set; }
+}
 
-		/// <summary>
-		/// The new Thickness.
-		/// </summary>
-		public Color Color { get; set; }
+/// <summary>
+/// The <see cref="ColorPicker"/> <see cref="View"/> Color picker.
+/// </summary>
+public class ColorPicker : View {
+	int _boxHeight = 2;
+	int _boxWidth = 4;
 
-		/// <summary>
-		/// The previous Thickness.
-		/// </summary>
-		public Color PreviousColor { get; set; }
-	}
 
 	/// <summary>
-	/// The <see cref="ColorPicker"/> <see cref="View"/> Color picker.
+	/// Columns of color boxes
 	/// </summary>
-	public class ColorPicker : View {
-		private int _selectColorIndex = (int)Color.Black;
-
-
-		/// <summary>
-		/// Columns of color boxes
-		/// </summary>
-		private int _cols = 8;
-
-		/// <summary>
-		/// Rows of color boxes
-		/// </summary>
-		private int _rows = 2;
-
-		/// <summary>
-		/// Width of a color box
-		/// </summary>
-		public int BoxWidth {
-			get => _boxWidth;
-			set {
-				if (_boxWidth != value) {
-					_boxWidth = value;
-					if (IsInitialized) {
-						Bounds = new Rect (Bounds.Location, new Size (_cols * BoxWidth, _rows * BoxHeight));
-					}
-				}
-			}
-		}
-		private int _boxWidth = 4;
-
-		/// <summary>
-		/// Height of a color box
-		/// </summary>
-		public int BoxHeight {
-			get => _boxHeight;
-			set {
-				if (_boxHeight != value) {
-					_boxHeight = value;
-					if (IsInitialized) {
-						Bounds = new Rect (Bounds.Location, new Size (_cols * BoxWidth, _rows * BoxHeight));
-					}
-				}
-			}
-		}
-		int _boxHeight = 2;
-
-		/// <summary>
-		/// Cursor for the selected color.
-		/// </summary>
-		public Point Cursor {
-			get {
-				return new Point (_selectColorIndex % _cols, _selectColorIndex / _cols);
-			}
+	readonly int _cols = 8;
 
-			set {
-				var colorIndex = value.Y * _cols + value.X;
-				SelectedColor = (ColorName)colorIndex;
-			}
-		}
+	/// <summary>
+	/// Rows of color boxes
+	/// </summary>
+	readonly int _rows = 2;
 
-		/// <summary>
-		/// Fired when a color is picked.
-		/// </summary>
-		public event EventHandler<ColorEventArgs> ColorChanged;
-
-		/// <summary>
-		/// Selected color.
-		/// </summary>
-		public ColorName SelectedColor {
-			get {
-				return (ColorName)_selectColorIndex;
-			}
+	int _selectColorIndex = (int)Color.Black;
+
+	/// <summary>
+	/// Initializes a new instance of <see cref="ColorPicker"/>.
+	/// </summary>
+	public ColorPicker () => SetInitialProperties ();
 
-			set {
-				ColorName prev = (ColorName)_selectColorIndex;
-				_selectColorIndex = (int)value;
-				ColorChanged?.Invoke (this, new ColorEventArgs () {
-					PreviousColor = new Color (prev),
-					Color = new Color (value),
-				});
-				SetNeedsDisplay ();
+	/// <summary>
+	/// Width of a color box
+	/// </summary>
+	public int BoxWidth {
+		get => _boxWidth;
+		set {
+			if (_boxWidth != value) {
+				_boxWidth = value;
+				SetNeedsLayout ();
 			}
 		}
+	}
 
-		/// <summary>
-		/// Initializes a new instance of <see cref="ColorPicker"/>.
-		/// </summary>
-		public ColorPicker ()
-		{
-			SetInitialProperties ();
+	/// <summary>
+	/// Height of a color box
+	/// </summary>
+	public int BoxHeight {
+		get => _boxHeight;
+		set {
+			if (_boxHeight != value) {
+				_boxHeight = value;
+				SetNeedsLayout ();
+			}
 		}
+	}
 
-		private void SetInitialProperties ()
-		{
-			CanFocus = true;
-			AddCommands ();
-			AddKeyBindings ();
-			LayoutStarted += (o, a) => {
-				Bounds = new Rect (Bounds.Location, new Size (_cols * BoxWidth, _rows * BoxHeight));
-			};
+	/// <summary>
+	/// Cursor for the selected color.
+	/// </summary>
+	public Point Cursor {
+		get => new (_selectColorIndex % _cols, _selectColorIndex / _cols);
+		set {
+			var colorIndex = value.Y * _cols + value.X;
+			SelectedColor = (ColorName)colorIndex;
 		}
+	}
 
-		/// <summary>
-		/// Add the commands.
-		/// </summary>
-		private void AddCommands ()
-		{
-			AddCommand (Command.Left, () => MoveLeft ());
-			AddCommand (Command.Right, () => MoveRight ());
-			AddCommand (Command.LineUp, () => MoveUp ());
-			AddCommand (Command.LineDown, () => MoveDown ());
+	/// <summary>
+	/// Selected color.
+	/// </summary>
+	public ColorName SelectedColor {
+		get => (ColorName)_selectColorIndex;
+		set {
+			var prev = (ColorName)_selectColorIndex;
+			_selectColorIndex = (int)value;
+			ColorChanged?.Invoke (this, new ColorEventArgs {
+									       PreviousColor = new Color (prev),
+									       Color         = new Color (value)
+								       });
+			SetNeedsDisplay ();
 		}
+	}
 
-		/// <summary>
-		/// Add the KeyBindinds.
-		/// </summary>
-		private void AddKeyBindings ()
-		{
-			KeyBindings.Add (KeyCode.CursorLeft, Command.Left);
-			KeyBindings.Add (KeyCode.CursorRight, Command.Right);
-			KeyBindings.Add (KeyCode.CursorUp, Command.LineUp);
-			KeyBindings.Add (KeyCode.CursorDown, Command.LineDown);
-		}
+	/// <summary>
+	/// Fired when a color is picked.
+	/// </summary>
+	public event EventHandler<ColorEventArgs> ColorChanged;
+
+	void SetInitialProperties ()
+	{
+		CanFocus = true;
+		AddCommands ();
+		AddKeyBindings ();
+		LayoutStarted += (o, a) => {
+			var thickness = GetFramesThickness ();
+			Width  = _cols * BoxWidth + thickness.Vertical;
+			Height = _rows * BoxHeight + thickness.Horizontal;
+		};
+	}
 
-		///<inheritdoc/>
-		public override void OnDrawContent (Rect contentArea)
-		{
-			base.OnDrawContent (contentArea);
-
-			Driver.SetAttribute (HasFocus ? ColorScheme.Focus : GetNormalColor ());
-			var colorIndex = 0;
-
-			for (var y = 0; y < (Bounds.Height / BoxHeight); y++) {
-				for (var x = 0; x < (Bounds.Width / BoxWidth); x++) {
-					var foregroundColorIndex = y == 0 ? colorIndex + _cols : colorIndex - _cols;
-					Driver.SetAttribute (new Attribute ((ColorName)foregroundColorIndex, (ColorName)colorIndex));
-					var selected = x == Cursor.X && y == Cursor.Y;
-					DrawColorBox (x, y, selected);
-					colorIndex++;
-				}
-			}
-		}
+	/// <summary>
+	/// Add the commands.
+	/// </summary>
+	void AddCommands ()
+	{
+		AddCommand (Command.Left,     () => MoveLeft ());
+		AddCommand (Command.Right,    () => MoveRight ());
+		AddCommand (Command.LineUp,   () => MoveUp ());
+		AddCommand (Command.LineDown, () => MoveDown ());
+	}
 
-		/// <summary>
-		/// Draw a box for one color.
-		/// </summary>
-		/// <param name="x">X location.</param>
-		/// <param name="y">Y location</param>
-		/// <param name="selected"></param>
-		private void DrawColorBox (int x, int y, bool selected)
-		{
-			var index = 0;
-
-			for (var zoomedY = 0; zoomedY < BoxHeight; zoomedY++) {
-				for (var zoomedX = 0; zoomedX < BoxWidth; zoomedX++) {
-					Move (x * BoxWidth + zoomedX, y * BoxHeight + zoomedY);
-					Driver.AddRune ((Rune)' ');
-					index++;
-				}
-			}
+	/// <summary>
+	/// Add the KeyBindinds.
+	/// </summary>
+	void AddKeyBindings ()
+	{
+		KeyBindings.Add (KeyCode.CursorLeft,  Command.Left);
+		KeyBindings.Add (KeyCode.CursorRight, Command.Right);
+		KeyBindings.Add (KeyCode.CursorUp,    Command.LineUp);
+		KeyBindings.Add (KeyCode.CursorDown,  Command.LineDown);
+	}
 
-			if (selected) {
-				DrawFocusRect (new Rect (x * BoxWidth, y * BoxHeight, BoxWidth, BoxHeight));
+	///<inheritdoc/>
+	public override void OnDrawContent (Rect contentArea)
+	{
+		base.OnDrawContent (contentArea);
+
+		Driver.SetAttribute (HasFocus ? ColorScheme.Focus : GetNormalColor ());
+		var colorIndex = 0;
+
+		for (var y = 0; y < Bounds.Height / BoxHeight; y++) {
+			for (var x = 0; x < Bounds.Width / BoxWidth; x++) {
+				var foregroundColorIndex = y == 0 ? colorIndex + _cols : colorIndex - _cols;
+				Driver.SetAttribute (new Attribute ((ColorName)foregroundColorIndex, (ColorName)colorIndex));
+				var selected = x == Cursor.X && y == Cursor.Y;
+				DrawColorBox (x, y, selected);
+				colorIndex++;
 			}
 		}
+	}
 
-		private void DrawFocusRect (Rect rect)
-		{
-			var lc = new LineCanvas ();
-			if (rect.Width == 1) {
-				lc.AddLine (rect.Location, rect.Height, Orientation.Vertical, LineStyle.Dotted);
-			} else if (rect.Height == 1) {
-				lc.AddLine (rect.Location, rect.Width, Orientation.Horizontal, LineStyle.Dotted);
-			} else {
-				lc.AddLine (rect.Location, rect.Width, Orientation.Horizontal, LineStyle.Dotted);
-				lc.AddLine (new Point (rect.Location.X, rect.Location.Y + rect.Height - 1), rect.Width, Orientation.Horizontal, LineStyle.Dotted);
-
-				lc.AddLine (rect.Location, rect.Height, Orientation.Vertical, LineStyle.Dotted);
-				lc.AddLine (new Point (rect.Location.X + rect.Width - 1, rect.Location.Y), rect.Height, Orientation.Vertical, LineStyle.Dotted);
-			}
-			foreach (var p in lc.GetMap ()) {
-				AddRune (p.Key.X, p.Key.Y, p.Value);
+	/// <summary>
+	/// Draw a box for one color.
+	/// </summary>
+	/// <param name="x">X location.</param>
+	/// <param name="y">Y location</param>
+	/// <param name="selected"></param>
+	void DrawColorBox (int x, int y, bool selected)
+	{
+		var index = 0;
+
+		for (var zoomedY = 0; zoomedY < BoxHeight; zoomedY++) {
+			for (var zoomedX = 0; zoomedX < BoxWidth; zoomedX++) {
+				Move (x * BoxWidth + zoomedX, y * BoxHeight + zoomedY);
+				Driver.AddRune ((Rune)' ');
+				index++;
 			}
 		}
 
-		/// <summary>
-		/// Moves the selected item index to the previous column.
-		/// </summary>
-		/// <returns></returns>
-		public virtual bool MoveLeft ()
-		{
-			if (Cursor.X > 0) SelectedColor--;
-			return true;
+		if (selected) {
+			DrawFocusRect (new Rect (x * BoxWidth, y * BoxHeight, BoxWidth, BoxHeight));
 		}
+	}
 
-		/// <summary>
-		/// Moves the selected item index to the next column.
-		/// </summary>
-		/// <returns></returns>
-		public virtual bool MoveRight ()
-		{
-			if (Cursor.X < _cols - 1) SelectedColor++;
-			return true;
+	void DrawFocusRect (Rect rect)
+	{
+		var lc = new LineCanvas ();
+		if (rect.Width == 1) {
+			lc.AddLine (rect.Location, rect.Height, Orientation.Vertical, LineStyle.Dotted);
+		} else if (rect.Height == 1) {
+			lc.AddLine (rect.Location, rect.Width, Orientation.Horizontal, LineStyle.Dotted);
+		} else {
+			lc.AddLine (rect.Location,                                                  rect.Width, Orientation.Horizontal, LineStyle.Dotted);
+			lc.AddLine (new Point (rect.Location.X, rect.Location.Y + rect.Height - 1), rect.Width, Orientation.Horizontal, LineStyle.Dotted);
+
+			lc.AddLine (rect.Location,                                                 rect.Height, Orientation.Vertical, LineStyle.Dotted);
+			lc.AddLine (new Point (rect.Location.X + rect.Width - 1, rect.Location.Y), rect.Height, Orientation.Vertical, LineStyle.Dotted);
+		}
+		foreach (var p in lc.GetMap ()) {
+			AddRune (p.Key.X, p.Key.Y, p.Value);
 		}
+	}
 
-		/// <summary>
-		/// Moves the selected item index to the previous row.
-		/// </summary>
-		/// <returns></returns>
-		public virtual bool MoveUp ()
-		{
-			if (Cursor.Y > 0) SelectedColor -= _cols;
-			return true;
+	/// <summary>
+	/// Moves the selected item index to the previous column.
+	/// </summary>
+	/// <returns></returns>
+	public virtual bool MoveLeft ()
+	{
+		if (Cursor.X > 0) {
+			SelectedColor--;
+		}
+		return true;
+	}
+
+	/// <summary>
+	/// Moves the selected item index to the next column.
+	/// </summary>
+	/// <returns></returns>
+	public virtual bool MoveRight ()
+	{
+		if (Cursor.X < _cols - 1) {
+			SelectedColor++;
 		}
+		return true;
+	}
 
-		/// <summary>
-		/// Moves the selected item index to the next row.
-		/// </summary>
-		/// <returns></returns>
-		public virtual bool MoveDown ()
-		{
-			if (Cursor.Y < _rows - 1) SelectedColor += _cols;
-			return true;
+	/// <summary>
+	/// Moves the selected item index to the previous row.
+	/// </summary>
+	/// <returns></returns>
+	public virtual bool MoveUp ()
+	{
+		if (Cursor.Y > 0) {
+			SelectedColor -= _cols;
 		}
+		return true;
+	}
 
-		///<inheritdoc/>
-		public override bool MouseEvent (MouseEvent me)
-		{
-			if (!me.Flags.HasFlag (MouseFlags.Button1Clicked) || !CanFocus) {
-				return false;
-			}
+	/// <summary>
+	/// Moves the selected item index to the next row.
+	/// </summary>
+	/// <returns></returns>
+	public virtual bool MoveDown ()
+	{
+		if (Cursor.Y < _rows - 1) {
+			SelectedColor += _cols;
+		}
+		return true;
+	}
 
-			SetFocus ();
-			if (me.X > Bounds.Width || me.Y > Bounds.Height) {
-				return true;
-			}
-			Cursor = new Point ((me.X ) / _boxWidth, (me.Y) / _boxHeight);
+	///<inheritdoc/>
+	public override bool MouseEvent (MouseEvent me)
+	{
+		if (!me.Flags.HasFlag (MouseFlags.Button1Clicked) || !CanFocus) {
+			return false;
+		}
 
+		SetFocus ();
+		if (me.X > Bounds.Width || me.Y > Bounds.Height) {
 			return true;
 		}
+		Cursor = new Point (me.X / _boxWidth, me.Y / _boxHeight);
 
-		///<inheritdoc/>
-		public override bool OnEnter (View view)
-		{
-			Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
+		return true;
+	}
 
-			return base.OnEnter (view);
-		}
+	///<inheritdoc/>
+	public override bool OnEnter (View view)
+	{
+		Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
+
+		return base.OnEnter (view);
 	}
-}
+}

+ 1 - 1
UICatalog/Scenarios/ColorPicker.cs

@@ -58,7 +58,7 @@ namespace UICatalog.Scenarios {
 			Win.Add (_foregroundColorLabel);
 
 			// Background ColorPicker.
-			backgroundColorPicker = new ColorPicker () { 
+			backgroundColorPicker = new ColorPicker () {
 				Title = "Background Color",
 				Y = 0,
 				X = 0,