AdornmentEditor.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #nullable enable
  2. using System;
  3. namespace UICatalog.Scenarios;
  4. /// <summary>
  5. /// Provides a composable UI for editing the settings of an Adornment.
  6. /// </summary>
  7. public class AdornmentEditor : EditorBase
  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. };
  17. private readonly ColorPicker16 _foregroundColorPicker = new ()
  18. {
  19. Title = "_FG",
  20. BoxWidth = 1,
  21. BoxHeight = 1,
  22. BorderStyle = LineStyle.Single,
  23. SuperViewRendersLineCanvas = true
  24. };
  25. private CheckBox? _diagThicknessCheckBox;
  26. private CheckBox? _diagRulerCheckBox;
  27. private Adornment? _adornment;
  28. public Adornment? AdornmentToEdit
  29. {
  30. get => _adornment;
  31. set
  32. {
  33. Enabled = value is { };
  34. if (value == _adornment)
  35. {
  36. return;
  37. }
  38. _adornment = value;
  39. if (_adornment is null)
  40. {
  41. return;
  42. }
  43. if (IsInitialized)
  44. {
  45. _topEdit!.Value = _adornment.Thickness.Top;
  46. _leftEdit!.Value = _adornment.Thickness.Left;
  47. _bottomEdit!.Value = _adornment.Thickness.Bottom;
  48. _rightEdit!.Value = _adornment.Thickness.Right;
  49. _adornment.Initialized += (sender, args) =>
  50. {
  51. Scheme? cs = _adornment.GetScheme ();
  52. _foregroundColorPicker.SelectedColor = _adornment.GetAttributeForRole (VisualRole.Normal).Foreground.GetClosestNamedColor16 ();
  53. _backgroundColorPicker.SelectedColor = _adornment.GetAttributeForRole (VisualRole.Normal).Background.GetClosestNamedColor16 ();
  54. };
  55. }
  56. OnAdornmentChanged ();
  57. }
  58. }
  59. public event EventHandler<EventArgs>? AdornmentChanged;
  60. public void OnAdornmentChanged () { AdornmentChanged?.Invoke (this, EventArgs.Empty); }
  61. /// <inheritdoc/>
  62. protected override void OnViewToEditChanged () { AdornmentToEdit = ViewToEdit as Adornment; }
  63. private NumericUpDown<int>? _topEdit;
  64. private NumericUpDown<int>? _leftEdit;
  65. private NumericUpDown<int>? _bottomEdit;
  66. private NumericUpDown<int>? _rightEdit;
  67. public AdornmentEditor ()
  68. {
  69. CanFocus = true;
  70. Initialized += AdornmentEditor_Initialized;
  71. }
  72. private void AdornmentEditor_Initialized (object? sender, EventArgs e)
  73. {
  74. _topEdit = new ()
  75. {
  76. X = Pos.Center (), Y = 0,
  77. Format = "{0, 2}"
  78. };
  79. _topEdit.ValueChanging += Top_ValueChanging;
  80. Add (_topEdit);
  81. _leftEdit = new ()
  82. {
  83. X = Pos.Left (_topEdit) - Pos.Func (() => _topEdit.Text.Length) - 2, Y = Pos.Bottom (_topEdit),
  84. Format = _topEdit.Format
  85. };
  86. _leftEdit.ValueChanging += Left_ValueChanging;
  87. Add (_leftEdit);
  88. _rightEdit = new ()
  89. {
  90. X = Pos.Right (_leftEdit) + 5, Y = Pos.Bottom (_topEdit),
  91. Format = _topEdit.Format
  92. };
  93. _rightEdit.ValueChanging += Right_ValueChanging;
  94. Add (_rightEdit);
  95. _bottomEdit = new ()
  96. {
  97. X = Pos.Center (), Y = Pos.Bottom (_leftEdit),
  98. Format = _topEdit.Format
  99. };
  100. _bottomEdit.ValueChanging += Bottom_ValueChanging;
  101. Add (_bottomEdit);
  102. var copyTop = new Button
  103. {
  104. X = Pos.Center (), Y = Pos.Bottom (_bottomEdit), Text = "Cop_y Top"
  105. };
  106. copyTop.Accepting += (s, e) =>
  107. {
  108. AdornmentToEdit!.Thickness = new (_topEdit.Value);
  109. _leftEdit.Value = _rightEdit.Value = _bottomEdit.Value = _topEdit.Value;
  110. };
  111. Add (copyTop);
  112. // Foreground ColorPicker.
  113. _foregroundColorPicker.X = 0;
  114. _foregroundColorPicker.Y = Pos.Bottom (copyTop);
  115. _foregroundColorPicker.ColorChanged += ColorPickerColorChanged ();
  116. Add (_foregroundColorPicker);
  117. // Background ColorPicker.
  118. _backgroundColorPicker.X = Pos.Right (_foregroundColorPicker) - 1;
  119. _backgroundColorPicker.Y = Pos.Top (_foregroundColorPicker);
  120. _backgroundColorPicker.ColorChanged += ColorPickerColorChanged ();
  121. Add (_backgroundColorPicker);
  122. _topEdit.Value = AdornmentToEdit?.Thickness.Top ?? 0;
  123. _leftEdit.Value = AdornmentToEdit?.Thickness.Left ?? 0;
  124. _rightEdit.Value = AdornmentToEdit?.Thickness.Right ?? 0;
  125. _bottomEdit.Value = AdornmentToEdit?.Thickness.Bottom ?? 0;
  126. _diagThicknessCheckBox = new () { Text = "_Thickness Diag." };
  127. if (AdornmentToEdit is { })
  128. {
  129. _diagThicknessCheckBox.CheckedState =
  130. AdornmentToEdit.Diagnostics.FastHasFlags (ViewDiagnosticFlags.Thickness) ? CheckState.Checked : CheckState.UnChecked;
  131. }
  132. else
  133. {
  134. _diagThicknessCheckBox.CheckedState = Diagnostics.FastHasFlags (ViewDiagnosticFlags.Thickness) ? CheckState.Checked : CheckState.UnChecked;
  135. }
  136. _diagThicknessCheckBox.CheckedStateChanging += (s, e) =>
  137. {
  138. if (e.NewValue == CheckState.Checked)
  139. {
  140. AdornmentToEdit!.Diagnostics |= ViewDiagnosticFlags.Thickness;
  141. }
  142. else
  143. {
  144. AdornmentToEdit!.Diagnostics &= ~ViewDiagnosticFlags.Thickness;
  145. }
  146. };
  147. Add (_diagThicknessCheckBox);
  148. _diagThicknessCheckBox.Y = Pos.Bottom (_backgroundColorPicker);
  149. _diagRulerCheckBox = new () { Text = "_Ruler" };
  150. if (AdornmentToEdit is { })
  151. {
  152. _diagRulerCheckBox.CheckedState = AdornmentToEdit.Diagnostics.FastHasFlags (ViewDiagnosticFlags.Ruler) ? CheckState.Checked : CheckState.UnChecked;
  153. }
  154. else
  155. {
  156. _diagRulerCheckBox.CheckedState = Diagnostics.FastHasFlags (ViewDiagnosticFlags.Ruler) ? CheckState.Checked : CheckState.UnChecked;
  157. }
  158. _diagRulerCheckBox.CheckedStateChanging += (s, e) =>
  159. {
  160. if (e.NewValue == CheckState.Checked)
  161. {
  162. AdornmentToEdit!.Diagnostics |= ViewDiagnosticFlags.Ruler;
  163. }
  164. else
  165. {
  166. AdornmentToEdit!.Diagnostics &= ~ViewDiagnosticFlags.Ruler;
  167. }
  168. };
  169. Add (_diagRulerCheckBox);
  170. _diagRulerCheckBox.Y = Pos.Bottom (_diagThicknessCheckBox);
  171. }
  172. private EventHandler<ColorEventArgs> ColorPickerColorChanged ()
  173. {
  174. return (o, a) =>
  175. {
  176. if (AdornmentToEdit is null)
  177. {
  178. return;
  179. }
  180. AdornmentToEdit.SetScheme (new (AdornmentToEdit.GetScheme ())
  181. {
  182. Normal = new (_foregroundColorPicker.SelectedColor, _backgroundColorPicker.SelectedColor)
  183. })
  184. ;
  185. };
  186. }
  187. private void Top_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, e.NewValue, AdornmentToEdit.Thickness.Right, AdornmentToEdit.Thickness.Bottom);
  195. }
  196. private void Left_ValueChanging (object? sender, CancelEventArgs<int> e)
  197. {
  198. if (e.NewValue < 0 || AdornmentToEdit is null)
  199. {
  200. e.Cancel = true;
  201. return;
  202. }
  203. AdornmentToEdit.Thickness = new (e.NewValue, AdornmentToEdit.Thickness.Top, AdornmentToEdit.Thickness.Right, AdornmentToEdit.Thickness.Bottom);
  204. }
  205. private void Right_ValueChanging (object? sender, CancelEventArgs<int> e)
  206. {
  207. if (e.NewValue < 0 || AdornmentToEdit is null)
  208. {
  209. e.Cancel = true;
  210. return;
  211. }
  212. AdornmentToEdit.Thickness = new (AdornmentToEdit.Thickness.Left, AdornmentToEdit.Thickness.Top, e.NewValue, AdornmentToEdit.Thickness.Bottom);
  213. }
  214. private void Bottom_ValueChanging (object? sender, CancelEventArgs<int> e)
  215. {
  216. if (e.NewValue < 0 || AdornmentToEdit is null)
  217. {
  218. e.Cancel = true;
  219. return;
  220. }
  221. AdornmentToEdit.Thickness = new (AdornmentToEdit.Thickness.Left, AdornmentToEdit.Thickness.Top, AdornmentToEdit.Thickness.Right, e.NewValue);
  222. }
  223. }