AdornmentEditor.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 ColorPicker _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 ColorPicker _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 Buttons.NumericUpDown<int> _topEdit;
  68. private Buttons.NumericUpDown<int> _leftEdit;
  69. private Buttons.NumericUpDown<int> _bottomEdit;
  70. private Buttons.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. TabStop = TabBehavior.TabGroup;
  78. }
  79. private void AdornmentEditor_Initialized (object sender, EventArgs e)
  80. {
  81. ExpanderButton expandButton;
  82. Border.Add (expandButton = new ExpanderButton ());
  83. _topEdit = new ()
  84. {
  85. X = Pos.Center (), Y = 0,
  86. Enabled = false
  87. };
  88. _topEdit.ValueChanging += Top_ValueChanging;
  89. Add (_topEdit);
  90. _leftEdit = new ()
  91. {
  92. X = Pos.Left (_topEdit) - Pos.Func (() => _topEdit.Digits) - 2, Y = Pos.Bottom (_topEdit),
  93. Enabled = false
  94. };
  95. _leftEdit.ValueChanging += Left_ValueChanging;
  96. Add (_leftEdit);
  97. _rightEdit = new ()
  98. {
  99. X = Pos.Right (_leftEdit) + 5, Y = Pos.Bottom (_topEdit),
  100. Enabled = false
  101. };
  102. _rightEdit.ValueChanging += Right_ValueChanging;
  103. Add (_rightEdit);
  104. _bottomEdit = new ()
  105. {
  106. X = Pos.Center (), Y = Pos.Bottom (_leftEdit),
  107. Enabled = false
  108. };
  109. _bottomEdit.ValueChanging += Bottom_ValueChanging;
  110. Add (_bottomEdit);
  111. var copyTop = new Button
  112. {
  113. X = Pos.Center (), Y = Pos.Bottom (_bottomEdit), Text = "Cop_y Top",
  114. Enabled = false
  115. };
  116. copyTop.Accept += (s, e) =>
  117. {
  118. AdornmentToEdit.Thickness = new (_topEdit.Value);
  119. _leftEdit.Value = _rightEdit.Value = _bottomEdit.Value = _topEdit.Value;
  120. };
  121. Add (copyTop);
  122. // Foreground ColorPicker.
  123. _foregroundColorPicker.X = 0;
  124. _foregroundColorPicker.Y = Pos.Bottom (copyTop);
  125. _foregroundColorPicker.ColorChanged += ColorPickerColorChanged ();
  126. Add (_foregroundColorPicker);
  127. // Background ColorPicker.
  128. _backgroundColorPicker.X = Pos.Right (_foregroundColorPicker) - 1;
  129. _backgroundColorPicker.Y = Pos.Top (_foregroundColorPicker);
  130. _backgroundColorPicker.ColorChanged += ColorPickerColorChanged ();
  131. Add (_backgroundColorPicker);
  132. _topEdit.Value = AdornmentToEdit?.Thickness.Top ?? 0;
  133. _leftEdit.Value = AdornmentToEdit?.Thickness.Left ?? 0;
  134. _rightEdit.Value = AdornmentToEdit?.Thickness.Right ?? 0;
  135. _bottomEdit.Value = AdornmentToEdit?.Thickness.Bottom ?? 0;
  136. foreach (var subview in Subviews)
  137. {
  138. subview.Enabled = AdornmentToEdit is { };
  139. }
  140. }
  141. private EventHandler<ColorEventArgs> ColorPickerColorChanged ()
  142. {
  143. return (o, a) =>
  144. {
  145. if (AdornmentToEdit is null)
  146. {
  147. return;
  148. }
  149. AdornmentToEdit.ColorScheme = new (AdornmentToEdit.ColorScheme)
  150. {
  151. Normal = new (_foregroundColorPicker.SelectedColor, _backgroundColorPicker.SelectedColor)
  152. };
  153. };
  154. }
  155. private void Top_ValueChanging (object sender, CancelEventArgs<int> e)
  156. {
  157. if (e.NewValue < 0 || AdornmentToEdit is null)
  158. {
  159. e.Cancel = true;
  160. return;
  161. }
  162. AdornmentToEdit.Thickness = new (AdornmentToEdit.Thickness.Left, e.NewValue, AdornmentToEdit.Thickness.Right, AdornmentToEdit.Thickness.Bottom);
  163. }
  164. private void Left_ValueChanging (object sender, CancelEventArgs<int> e)
  165. {
  166. if (e.NewValue < 0 || AdornmentToEdit is null)
  167. {
  168. e.Cancel = true;
  169. return;
  170. }
  171. AdornmentToEdit.Thickness = new (e.NewValue, AdornmentToEdit.Thickness.Top, AdornmentToEdit.Thickness.Right, AdornmentToEdit.Thickness.Bottom);
  172. }
  173. private void Right_ValueChanging (object sender, CancelEventArgs<int> e)
  174. {
  175. if (e.NewValue < 0 || AdornmentToEdit is null)
  176. {
  177. e.Cancel = true;
  178. return;
  179. }
  180. AdornmentToEdit.Thickness = new (AdornmentToEdit.Thickness.Left, AdornmentToEdit.Thickness.Top, e.NewValue, AdornmentToEdit.Thickness.Bottom);
  181. }
  182. private void Bottom_ValueChanging (object sender, CancelEventArgs<int> e)
  183. {
  184. if (e.NewValue < 0 || AdornmentToEdit is null)
  185. {
  186. e.Cancel = true;
  187. return;
  188. }
  189. AdornmentToEdit.Thickness = new (AdornmentToEdit.Thickness.Left, AdornmentToEdit.Thickness.Top, AdornmentToEdit.Thickness.Right, e.NewValue);
  190. }
  191. }