ColorPicker.16.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #nullable enable
  2. namespace Terminal.Gui.Views;
  3. /// <summary>A sinple color picker that supports the legacy 16 ANSI colors</summary>
  4. public class ColorPicker16 : View
  5. {
  6. /// <summary>Initializes a new instance of <see cref="ColorPicker16"/>.</summary>
  7. public ColorPicker16 () { SetInitialProperties (); }
  8. /// <summary>Columns of color boxes</summary>
  9. private const int COLS = 8;
  10. /// <summary>Rows of color boxes</summary>
  11. private const int ROWS = 2;
  12. private int _boxHeight = 2;
  13. private int _boxWidth = 4;
  14. private int _selectColorIndex = (int)Color.Black;
  15. /// <summary>Height of a color box</summary>
  16. public int BoxHeight
  17. {
  18. get => _boxHeight;
  19. set
  20. {
  21. if (_boxHeight != value)
  22. {
  23. _boxHeight = value;
  24. Width = Dim.Auto (minimumContentDim: _boxWidth * COLS);
  25. Height = Dim.Auto (minimumContentDim: _boxHeight * ROWS);
  26. SetContentSize (new (_boxWidth * COLS, _boxHeight * ROWS));
  27. SetNeedsLayout ();
  28. }
  29. }
  30. }
  31. /// <summary>Width of a color box</summary>
  32. public int BoxWidth
  33. {
  34. get => _boxWidth;
  35. set
  36. {
  37. if (_boxWidth != value)
  38. {
  39. _boxWidth = value;
  40. Width = Dim.Auto (minimumContentDim: _boxWidth * COLS);
  41. Height = Dim.Auto (minimumContentDim: _boxHeight * ROWS);
  42. SetContentSize (new (_boxWidth * COLS, _boxHeight * ROWS));
  43. SetNeedsLayout ();
  44. }
  45. }
  46. }
  47. /// <summary>Fired when a color is picked.</summary>
  48. public event EventHandler<ResultEventArgs<Color>>? ColorChanged;
  49. /// <summary>Cursor for the selected color.</summary>
  50. public Point Cursor
  51. {
  52. get => new (_selectColorIndex % COLS, _selectColorIndex / COLS);
  53. set
  54. {
  55. int colorIndex = value.Y * COLS + value.X;
  56. SelectedColor = (ColorName16)colorIndex;
  57. }
  58. }
  59. /// <summary>Moves the selected item index to the next row.</summary>
  60. /// <returns></returns>
  61. private bool MoveDown (ICommandContext? commandContext)
  62. {
  63. if (RaiseSelecting (commandContext) == true)
  64. {
  65. return true;
  66. }
  67. if (Cursor.Y < ROWS - 1)
  68. {
  69. SelectedColor += COLS;
  70. }
  71. return true;
  72. }
  73. /// <summary>Moves the selected item index to the previous column.</summary>
  74. /// <returns></returns>
  75. private bool MoveLeft (ICommandContext? commandContext)
  76. {
  77. if (RaiseSelecting (commandContext) == true)
  78. {
  79. return true;
  80. }
  81. if (Cursor.X > 0)
  82. {
  83. SelectedColor--;
  84. }
  85. return true;
  86. }
  87. /// <summary>Moves the selected item index to the next column.</summary>
  88. /// <returns></returns>
  89. private bool MoveRight (ICommandContext? commandContext)
  90. {
  91. if (RaiseSelecting (commandContext) == true)
  92. {
  93. return true;
  94. }
  95. if (Cursor.X < COLS - 1)
  96. {
  97. SelectedColor++;
  98. }
  99. return true;
  100. }
  101. /// <summary>Moves the selected item index to the previous row.</summary>
  102. /// <returns></returns>
  103. private bool MoveUp (ICommandContext? commandContext)
  104. {
  105. if (RaiseSelecting (commandContext) == true)
  106. {
  107. return true;
  108. }
  109. if (Cursor.Y > 0)
  110. {
  111. SelectedColor -= COLS;
  112. }
  113. return true;
  114. }
  115. ///<inheritdoc/>
  116. protected override bool OnDrawingContent ()
  117. {
  118. SetAttribute (HasFocus ? GetAttributeForRole (VisualRole.Focus) : GetAttributeForRole (VisualRole.Normal));
  119. var colorIndex = 0;
  120. for (var y = 0; y < Math.Max (2, Viewport.Height / BoxHeight); y++)
  121. {
  122. for (var x = 0; x < Math.Max (8, Viewport.Width / BoxWidth); x++)
  123. {
  124. int foregroundColorIndex = y == 0 ? colorIndex + COLS : colorIndex - COLS;
  125. if (foregroundColorIndex > 15 || colorIndex > 15)
  126. {
  127. continue;
  128. }
  129. if (Enabled)
  130. {
  131. SetAttribute (new ((ColorName16)foregroundColorIndex, (ColorName16)colorIndex));
  132. }
  133. else
  134. {
  135. SetAttribute (new ((ColorName16)foregroundColorIndex, ((Color)(ColorName16)colorIndex).GetDimColor (), TextStyle.Faint));
  136. }
  137. bool selected = x == Cursor.X && y == Cursor.Y;
  138. DrawColorBox (x, y, selected);
  139. colorIndex++;
  140. }
  141. }
  142. return true;
  143. }
  144. /// <summary>Selected color.</summary>
  145. public ColorName16 SelectedColor
  146. {
  147. get => (ColorName16)_selectColorIndex;
  148. set
  149. {
  150. if (value == (ColorName16)_selectColorIndex)
  151. {
  152. return;
  153. }
  154. _selectColorIndex = (int)value;
  155. ColorChanged?.Invoke (
  156. this,
  157. new (value)
  158. );
  159. SetNeedsDraw ();
  160. }
  161. }
  162. /// <summary>Add the commands.</summary>
  163. private void AddCommands ()
  164. {
  165. AddCommand (Command.Left, (ctx) => MoveLeft (ctx));
  166. AddCommand (Command.Right, (ctx) => MoveRight (ctx));
  167. AddCommand (Command.Up, (ctx) => MoveUp (ctx));
  168. AddCommand (Command.Down, (ctx) => MoveDown (ctx));
  169. AddCommand (Command.Select, (ctx) =>
  170. {
  171. var set = false;
  172. if (ctx is CommandContext<MouseBinding> { Binding.MouseEventArgs: { } } mouseCommandContext)
  173. {
  174. Cursor = new (mouseCommandContext.Binding.MouseEventArgs.Position.X / _boxWidth, mouseCommandContext.Binding.MouseEventArgs.Position.Y / _boxHeight);
  175. set = true;
  176. }
  177. return RaiseAccepting (ctx) == true || set;
  178. });
  179. }
  180. /// <summary>Add the KeyBindings.</summary>
  181. private void AddKeyBindings ()
  182. {
  183. KeyBindings.Add (Key.CursorLeft, Command.Left);
  184. KeyBindings.Add (Key.CursorRight, Command.Right);
  185. KeyBindings.Add (Key.CursorUp, Command.Up);
  186. KeyBindings.Add (Key.CursorDown, Command.Down);
  187. }
  188. // TODO: Decouple Cursor from SelectedColor so that mouse press-and-hold can show the color under the cursor.
  189. /// <summary>Draw a box for one color.</summary>
  190. /// <param name="x">X location.</param>
  191. /// <param name="y">Y location</param>
  192. /// <param name="selected"></param>
  193. private void DrawColorBox (int x, int y, bool selected)
  194. {
  195. for (var zoomedY = 0; zoomedY < BoxHeight; zoomedY++)
  196. {
  197. for (var zoomedX = 0; zoomedX < BoxWidth; zoomedX++)
  198. {
  199. AddRune (x * BoxWidth + zoomedX, y * BoxHeight + zoomedY, (Rune)' ');
  200. }
  201. }
  202. if (selected)
  203. {
  204. DrawFocusRect (new (x * BoxWidth, y * BoxHeight, BoxWidth, BoxHeight));
  205. }
  206. }
  207. private void DrawFocusRect (Rectangle rect)
  208. {
  209. var lc = new LineCanvas ();
  210. if (rect.Width == 1)
  211. {
  212. lc.AddLine (rect.Location, rect.Height, Orientation.Vertical, LineStyle.Dotted);
  213. }
  214. else if (rect.Height == 1)
  215. {
  216. lc.AddLine (rect.Location, rect.Width, Orientation.Horizontal, LineStyle.Dotted);
  217. }
  218. else
  219. {
  220. lc.AddLine (rect.Location, rect.Width, Orientation.Horizontal, LineStyle.Dotted);
  221. lc.AddLine (
  222. rect.Location with { Y = rect.Location.Y + rect.Height - 1 },
  223. rect.Width,
  224. Orientation.Horizontal,
  225. LineStyle.Dotted
  226. );
  227. lc.AddLine (rect.Location, rect.Height, Orientation.Vertical, LineStyle.Dotted);
  228. lc.AddLine (
  229. rect.Location with { X = rect.Location.X + rect.Width - 1 },
  230. rect.Height,
  231. Orientation.Vertical,
  232. LineStyle.Dotted
  233. );
  234. }
  235. foreach (KeyValuePair<Point, Rune> p in lc.GetMap ())
  236. {
  237. AddRune (p.Key.X, p.Key.Y, p.Value);
  238. }
  239. }
  240. private void SetInitialProperties ()
  241. {
  242. HighlightStates = ViewBase.MouseState.PressedOutside | ViewBase.MouseState.Pressed;
  243. CanFocus = true;
  244. AddCommands ();
  245. AddKeyBindings ();
  246. Width = Dim.Auto (minimumContentDim: _boxWidth * COLS);
  247. Height = Dim.Auto (minimumContentDim: _boxHeight * ROWS);
  248. SetContentSize (new (_boxWidth * COLS, _boxHeight * ROWS));
  249. }
  250. }