AdornmentsEditor.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #nullable enable
  2. using System;
  3. using System.Text;
  4. using Terminal.Gui;
  5. namespace UICatalog.Scenarios;
  6. /// <summary>
  7. /// Provides an editor UI for the Margin, Border, and Padding of a View.
  8. /// </summary>
  9. public class AdornmentsEditor : View
  10. {
  11. public AdornmentsEditor ()
  12. {
  13. //ColorScheme = Colors.ColorSchemes ["Dialog"];
  14. Title = "AdornmentsEditor";
  15. Width = Dim.Auto (DimAutoStyle.Content);
  16. Height = Dim.Auto (DimAutoStyle.Content);
  17. //SuperViewRendersLineCanvas = true;
  18. CanFocus = true;
  19. TabStop = TabBehavior.TabGroup;
  20. _expandButton = new ()
  21. {
  22. Orientation = Orientation.Horizontal
  23. };
  24. Initialized += AdornmentsEditor_Initialized;
  25. }
  26. private readonly ViewDiagnosticFlags _savedDiagnosticFlags = Diagnostics;
  27. private View? _viewToEdit;
  28. private Label? _lblView; // Text describing the vi
  29. private MarginEditor? _marginEditor;
  30. private BorderEditor? _borderEditor;
  31. private PaddingEditor? _paddingEditor;
  32. // TODO: Move Diagnostics to a separate Editor class (DiagnosticsEditor?).
  33. private CheckBox? _diagPaddingCheckBox;
  34. private CheckBox? _diagRulerCheckBox;
  35. /// <summary>
  36. /// Gets or sets whether the AdornmentsEditor should automatically select the View to edit
  37. /// based on the values of <see cref="AutoSelectSuperView"/> and <see cref="AutoSelectAdornments"/>.
  38. /// </summary>
  39. public bool AutoSelectViewToEdit { get; set; }
  40. /// <summary>
  41. /// Gets or sets the View that will scope the behavior of <see cref="AutoSelectViewToEdit"/>.
  42. /// </summary>
  43. public View? AutoSelectSuperView { get; set; }
  44. /// <summary>
  45. /// Gets or sets whether auto select with the mouse will select Adornments or just Views.
  46. /// </summary>
  47. public bool AutoSelectAdornments { get; set; }
  48. public View? ViewToEdit
  49. {
  50. get => _viewToEdit;
  51. set
  52. {
  53. if (_viewToEdit == value)
  54. {
  55. return;
  56. }
  57. _viewToEdit = value;
  58. if (_viewToEdit is not Adornment)
  59. {
  60. _marginEditor!.AdornmentToEdit = _viewToEdit?.Margin ?? null;
  61. _borderEditor!.AdornmentToEdit = _viewToEdit?.Border ?? null;
  62. _paddingEditor!.AdornmentToEdit = _viewToEdit?.Padding ?? null;
  63. }
  64. if (_lblView is { })
  65. {
  66. _lblView.Text = $"{_viewToEdit?.GetType ().Name}: {_viewToEdit?.Id}" ?? string.Empty;
  67. }
  68. }
  69. }
  70. private void NavigationOnFocusedChanged (object? sender, EventArgs e)
  71. {
  72. if (AutoSelectSuperView is null)
  73. {
  74. return;
  75. }
  76. if (ApplicationNavigation.IsInHierarchy (this, Application.Navigation!.GetFocused ()))
  77. {
  78. return;
  79. }
  80. if (!ApplicationNavigation.IsInHierarchy (AutoSelectSuperView, Application.Navigation!.GetFocused ()))
  81. {
  82. return;
  83. }
  84. ViewToEdit = Application.Navigation!.GetFocused ();
  85. }
  86. private void ApplicationOnMouseEvent (object? sender, MouseEvent e)
  87. {
  88. if (e.Flags != MouseFlags.Button1Clicked || !AutoSelectViewToEdit)
  89. {
  90. return;
  91. }
  92. if ((AutoSelectSuperView is { } && !AutoSelectSuperView.FrameToScreen ().Contains (e.Position))
  93. || FrameToScreen ().Contains (e.Position))
  94. {
  95. return;
  96. }
  97. View view = e.View;
  98. if (view is { })
  99. {
  100. if (view is Adornment adornment)
  101. {
  102. ViewToEdit = AutoSelectAdornments ? adornment : adornment.Parent;
  103. }
  104. else
  105. {
  106. ViewToEdit = view;
  107. }
  108. }
  109. }
  110. /// <inheritdoc/>
  111. protected override void Dispose (bool disposing)
  112. {
  113. Diagnostics = _savedDiagnosticFlags;
  114. base.Dispose (disposing);
  115. }
  116. private readonly ExpanderButton? _expandButton;
  117. public ExpanderButton? ExpandButton => _expandButton;
  118. private void AdornmentsEditor_Initialized (object? sender, EventArgs e)
  119. {
  120. BorderStyle = LineStyle.Dotted;
  121. Border.Add (_expandButton!);
  122. _lblView = new ()
  123. {
  124. X = 0,
  125. Y = 0,
  126. Height = 2
  127. };
  128. _lblView.TextFormatter.WordWrap = true;
  129. _lblView.TextFormatter.MultiLine = true;
  130. _lblView.HotKeySpecifier = (Rune)'\uffff';
  131. Add (_lblView);
  132. _marginEditor = new ()
  133. {
  134. X = 0,
  135. Y = Pos.Bottom (_lblView),
  136. SuperViewRendersLineCanvas = true
  137. };
  138. Add (_marginEditor);
  139. _lblView.Width = Dim.Width (_marginEditor);
  140. _borderEditor = new ()
  141. {
  142. X = Pos.Left (_marginEditor),
  143. Y = Pos.Bottom (_marginEditor),
  144. SuperViewRendersLineCanvas = true
  145. };
  146. Add (_borderEditor);
  147. _paddingEditor = new ()
  148. {
  149. X = Pos.Left (_borderEditor),
  150. Y = Pos.Bottom (_borderEditor),
  151. SuperViewRendersLineCanvas = true
  152. };
  153. Add (_paddingEditor);
  154. _diagPaddingCheckBox = new () { Text = "_Diagnostic Padding" };
  155. _diagPaddingCheckBox.CheckedState = Diagnostics.FastHasFlags (ViewDiagnosticFlags.Padding) ? CheckState.Checked : CheckState.UnChecked;
  156. _diagPaddingCheckBox.CheckedStateChanging += (s, e) =>
  157. {
  158. if (e.NewValue == CheckState.Checked)
  159. {
  160. Diagnostics |= ViewDiagnosticFlags.Padding;
  161. }
  162. else
  163. {
  164. Diagnostics &= ~ViewDiagnosticFlags.Padding;
  165. }
  166. };
  167. Add (_diagPaddingCheckBox);
  168. _diagPaddingCheckBox.Y = Pos.Bottom (_paddingEditor);
  169. _diagRulerCheckBox = new () { Text = "_Diagnostic Ruler" };
  170. _diagRulerCheckBox.CheckedState = Diagnostics.FastHasFlags (ViewDiagnosticFlags.Ruler) ? CheckState.Checked : CheckState.UnChecked;
  171. _diagRulerCheckBox.CheckedStateChanging += (s, e) =>
  172. {
  173. if (e.NewValue == CheckState.Checked)
  174. {
  175. Diagnostics |= ViewDiagnosticFlags.Ruler;
  176. }
  177. else
  178. {
  179. Diagnostics &= ~ViewDiagnosticFlags.Ruler;
  180. }
  181. };
  182. Add (_diagRulerCheckBox);
  183. _diagRulerCheckBox.Y = Pos.Bottom (_diagPaddingCheckBox);
  184. Application.MouseEvent += ApplicationOnMouseEvent;
  185. Application.Navigation!.FocusedChanged += NavigationOnFocusedChanged;
  186. }
  187. }