AdornmentEditor.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using System;
  2. using Terminal.Gui;
  3. namespace UICatalog.Scenarios;
  4. /// <summary>
  5. /// Provides a composable UI for editing the settings of an Adornment.
  6. /// </summary>
  7. public class AdornmentEditor : View
  8. {
  9. private readonly ColorPicker16 _backgroundColorPicker = new ()
  10. {
  11. Title = "_BG",
  12. BoxWidth = 1,
  13. BoxHeight = 1,
  14. BorderStyle = LineStyle.Single,
  15. SuperViewRendersLineCanvas = true,
  16. Enabled = false
  17. };
  18. private readonly ColorPicker16 _foregroundColorPicker = new ()
  19. {
  20. Title = "_FG",
  21. BoxWidth = 1,
  22. BoxHeight = 1,
  23. BorderStyle = LineStyle.Single,
  24. SuperViewRendersLineCanvas = true,
  25. Enabled = false
  26. };
  27. private Adornment _adornment;
  28. public Adornment AdornmentToEdit
  29. {
  30. get => _adornment;
  31. set
  32. {
  33. if (value == _adornment)
  34. {
  35. return;
  36. }
  37. _adornment = value;
  38. foreach (var subview in Subviews)
  39. {
  40. subview.Enabled = _adornment is { };
  41. }
  42. if (_adornment is null)
  43. {
  44. return;
  45. }
  46. if (IsInitialized)
  47. {
  48. _topEdit.Value = _adornment.Thickness.Top;
  49. _leftEdit.Value = _adornment.Thickness.Left;
  50. _bottomEdit.Value = _adornment.Thickness.Bottom;
  51. _rightEdit.Value = _adornment.Thickness.Right;
  52. _adornment.Initialized += (sender, args) =>
  53. {
  54. var cs = _adornment.ColorScheme;
  55. _foregroundColorPicker.SelectedColor = cs.Normal.Foreground.GetClosestNamedColor ();
  56. _backgroundColorPicker.SelectedColor = cs.Normal.Background.GetClosestNamedColor ();
  57. };
  58. }
  59. OnAdornmentChanged ();
  60. }
  61. }
  62. public event EventHandler<EventArgs> AdornmentChanged;
  63. public void OnAdornmentChanged ()
  64. {
  65. AdornmentChanged?.Invoke (this, EventArgs.Empty);
  66. }
  67. private NumericUpDown<int> _topEdit;
  68. private NumericUpDown<int> _leftEdit;
  69. private NumericUpDown<int> _bottomEdit;
  70. private NumericUpDown<int> _rightEdit;
  71. public AdornmentEditor ()
  72. {
  73. Width = Dim.Auto (DimAutoStyle.Content);
  74. Height = Dim.Auto (DimAutoStyle.Content);
  75. BorderStyle = LineStyle.Dashed;
  76. Initialized += AdornmentEditor_Initialized;
  77. CanFocus = true;
  78. TabStop = TabBehavior.TabStop;
  79. }
  80. private void AdornmentEditor_Initialized (object sender, EventArgs e)
  81. {
  82. ExpanderButton expandButton;
  83. Border.Add (expandButton = new ExpanderButton ());
  84. _topEdit = new ()
  85. {
  86. X = Pos.Center (), Y = 0,
  87. Format = "{0, 2}",
  88. Enabled = false
  89. };
  90. _topEdit.ValueChanging += Top_ValueChanging;
  91. Add (_topEdit);
  92. _leftEdit = new ()
  93. {
  94. X = Pos.Left (_topEdit) - Pos.Func (() => _topEdit.Text.Length) - 2, Y = Pos.Bottom (_topEdit),
  95. Format = _topEdit.Format,
  96. Enabled = false
  97. };
  98. _leftEdit.ValueChanging += Left_ValueChanging;
  99. Add (_leftEdit);
  100. _rightEdit = new ()
  101. {
  102. X = Pos.Right (_leftEdit) + 5, Y = Pos.Bottom (_topEdit),
  103. Format = _topEdit.Format,
  104. Enabled = false
  105. };
  106. _rightEdit.ValueChanging += Right_ValueChanging;
  107. Add (_rightEdit);
  108. _bottomEdit = new ()
  109. {
  110. X = Pos.Center (), Y = Pos.Bottom (_leftEdit),
  111. Format = _topEdit.Format,
  112. Enabled = false
  113. };
  114. _bottomEdit.ValueChanging += Bottom_ValueChanging;
  115. Add (_bottomEdit);
  116. var copyTop = new Button
  117. {
  118. X = Pos.Center (), Y = Pos.Bottom (_bottomEdit), Text = "Cop_y Top",
  119. Enabled = false
  120. };
  121. copyTop.Accept += (s, e) =>
  122. {
  123. AdornmentToEdit.Thickness = new (_topEdit.Value);
  124. _leftEdit.Value = _rightEdit.Value = _bottomEdit.Value = _topEdit.Value;
  125. };
  126. Add (copyTop);
  127. // Foreground ColorPicker.
  128. _foregroundColorPicker.X = 0;
  129. _foregroundColorPicker.Y = Pos.Bottom (copyTop);
  130. _foregroundColorPicker.ColorChanged += ColorPickerColorChanged ();
  131. Add (_foregroundColorPicker);
  132. // Background ColorPicker.
  133. _backgroundColorPicker.X = Pos.Right (_foregroundColorPicker) - 1;
  134. _backgroundColorPicker.Y = Pos.Top (_foregroundColorPicker);
  135. _backgroundColorPicker.ColorChanged += ColorPickerColorChanged ();
  136. Add (_backgroundColorPicker);
  137. _topEdit.Value = AdornmentToEdit?.Thickness.Top ?? 0;
  138. _leftEdit.Value = AdornmentToEdit?.Thickness.Left ?? 0;
  139. _rightEdit.Value = AdornmentToEdit?.Thickness.Right ?? 0;
  140. _bottomEdit.Value = AdornmentToEdit?.Thickness.Bottom ?? 0;
  141. foreach (var subview in Subviews)
  142. {
  143. subview.Enabled = AdornmentToEdit is { };
  144. }
  145. }
  146. private EventHandler<ColorEventArgs> ColorPickerColorChanged ()
  147. {
  148. return (o, a) =>
  149. {
  150. if (AdornmentToEdit is null)
  151. {
  152. return;
  153. }
  154. AdornmentToEdit.ColorScheme = new (AdornmentToEdit.ColorScheme)
  155. {
  156. Normal = new (_foregroundColorPicker.SelectedColor, _backgroundColorPicker.SelectedColor)
  157. };
  158. };
  159. }
  160. private void Top_ValueChanging (object sender, CancelEventArgs<int> e)
  161. {
  162. if (e.NewValue < 0 || AdornmentToEdit is null)
  163. {
  164. e.Cancel = true;
  165. return;
  166. }
  167. AdornmentToEdit.Thickness = new (AdornmentToEdit.Thickness.Left, e.NewValue, AdornmentToEdit.Thickness.Right, AdornmentToEdit.Thickness.Bottom);
  168. }
  169. private void Left_ValueChanging (object sender, CancelEventArgs<int> e)
  170. {
  171. if (e.NewValue < 0 || AdornmentToEdit is null)
  172. {
  173. e.Cancel = true;
  174. return;
  175. }
  176. AdornmentToEdit.Thickness = new (e.NewValue, AdornmentToEdit.Thickness.Top, AdornmentToEdit.Thickness.Right, AdornmentToEdit.Thickness.Bottom);
  177. }
  178. private void Right_ValueChanging (object sender, CancelEventArgs<int> e)
  179. {
  180. if (e.NewValue < 0 || AdornmentToEdit is null)
  181. {
  182. e.Cancel = true;
  183. return;
  184. }
  185. AdornmentToEdit.Thickness = new (AdornmentToEdit.Thickness.Left, AdornmentToEdit.Thickness.Top, e.NewValue, AdornmentToEdit.Thickness.Bottom);
  186. }
  187. private void Bottom_ValueChanging (object sender, CancelEventArgs<int> e)
  188. {
  189. if (e.NewValue < 0 || AdornmentToEdit is null)
  190. {
  191. e.Cancel = true;
  192. return;
  193. }
  194. AdornmentToEdit.Thickness = new (AdornmentToEdit.Thickness.Left, AdornmentToEdit.Thickness.Top, AdornmentToEdit.Thickness.Right, e.NewValue);
  195. }
  196. }