AdornmentsEditor.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Text;
  3. using Terminal.Gui;
  4. namespace UICatalog.Scenarios;
  5. /// <summary>
  6. /// Provides an editor UI for the Margin, Border, and Padding of a View.
  7. /// </summary>
  8. public class AdornmentsEditor : View
  9. {
  10. private View _viewToEdit;
  11. private Label _lblView; // Text describing the vi
  12. private MarginEditor _marginEditor;
  13. private BorderEditor _borderEditor;
  14. private PaddingEditor _paddingEditor;
  15. // TODO: Move Diagnostics to a separate Editor class (DiagnosticsEditor?).
  16. private CheckBox _diagPaddingCheckBox;
  17. private CheckBox _diagRulerCheckBox;
  18. private readonly ViewDiagnosticFlags _savedDiagnosticFlags = Diagnostics;
  19. public AdornmentsEditor ()
  20. {
  21. //ColorScheme = Colors.ColorSchemes ["Dialog"];
  22. Title = $"AdornmentsEditor";
  23. Width = Dim.Auto (DimAutoStyle.Content);
  24. Height = Dim.Auto (DimAutoStyle.Content);
  25. //SuperViewRendersLineCanvas = true;
  26. TabStop = TabBehavior.TabGroup;
  27. Application.MouseEvent += Application_MouseEvent;
  28. Initialized += AdornmentsEditor_Initialized;
  29. }
  30. /// <summary>
  31. /// Gets or sets whether the AdornmentsEditor should automatically select the View to edit when the mouse is clicked
  32. /// anywhere outside the editor.
  33. /// </summary>
  34. public bool AutoSelectViewToEdit { get; set; }
  35. private void AdornmentsEditor_Initialized (object sender, EventArgs e)
  36. {
  37. BorderStyle = LineStyle.Dotted;
  38. ExpanderButton expandButton = new ExpanderButton ()
  39. {
  40. Orientation = Orientation.Horizontal
  41. };
  42. Border.Add (expandButton);
  43. _lblView = new ()
  44. {
  45. X = 0,
  46. Y = 0,
  47. Height = 2,
  48. };
  49. _lblView.TextFormatter.WordWrap = true;
  50. _lblView.TextFormatter.MultiLine = true;
  51. _lblView.HotKeySpecifier = (Rune)'\uffff';
  52. Add (_lblView);
  53. _marginEditor = new ()
  54. {
  55. X = 0,
  56. Y = Pos.Bottom (_lblView),
  57. SuperViewRendersLineCanvas = true
  58. };
  59. Add (_marginEditor);
  60. _lblView.Width = Dim.Width (_marginEditor);
  61. _borderEditor = new ()
  62. {
  63. X = Pos.Left (_marginEditor),
  64. Y = Pos.Bottom (_marginEditor),
  65. SuperViewRendersLineCanvas = true
  66. };
  67. Add (_borderEditor);
  68. _paddingEditor = new ()
  69. {
  70. X = Pos.Left (_borderEditor),
  71. Y = Pos.Bottom (_borderEditor),
  72. SuperViewRendersLineCanvas = true
  73. };
  74. Add (_paddingEditor);
  75. _diagPaddingCheckBox = new () { Text = "_Diagnostic Padding" };
  76. _diagPaddingCheckBox.State = Diagnostics.FastHasFlags (ViewDiagnosticFlags.Padding) ? CheckState.Checked : CheckState.UnChecked;
  77. _diagPaddingCheckBox.Toggle += (s, e) =>
  78. {
  79. if (e.NewValue == CheckState.Checked)
  80. {
  81. Diagnostics |= ViewDiagnosticFlags.Padding;
  82. }
  83. else
  84. {
  85. Diagnostics &= ~ViewDiagnosticFlags.Padding;
  86. }
  87. };
  88. Add (_diagPaddingCheckBox);
  89. _diagPaddingCheckBox.Y = Pos.Bottom (_paddingEditor);
  90. _diagRulerCheckBox = new () { Text = "_Diagnostic Ruler" };
  91. _diagRulerCheckBox.State = Diagnostics.FastHasFlags(ViewDiagnosticFlags.Ruler) ? CheckState.Checked : CheckState.UnChecked;
  92. _diagRulerCheckBox.Toggle += (s, e) =>
  93. {
  94. if (e.NewValue == CheckState.Checked)
  95. {
  96. Diagnostics |= ViewDiagnosticFlags.Ruler;
  97. }
  98. else
  99. {
  100. Diagnostics &= ~ViewDiagnosticFlags.Ruler;
  101. }
  102. };
  103. Add (_diagRulerCheckBox);
  104. _diagRulerCheckBox.Y = Pos.Bottom (_diagPaddingCheckBox);
  105. }
  106. private void Application_MouseEvent (object sender, MouseEvent e)
  107. {
  108. if (!AutoSelectViewToEdit || FrameToScreen ().Contains (e.Position))
  109. {
  110. return;
  111. }
  112. // TODO: Add a setting (property) so only subviews of a specified view are considered.
  113. var view = e.View;
  114. if (view is { } && e.Flags == MouseFlags.Button1Clicked)
  115. {
  116. if (view is Adornment adornment)
  117. {
  118. ViewToEdit = adornment.Parent;
  119. }
  120. else
  121. {
  122. ViewToEdit = view;
  123. }
  124. }
  125. }
  126. /// <inheritdoc />
  127. protected override void Dispose (bool disposing)
  128. {
  129. View.Diagnostics = _savedDiagnosticFlags;
  130. base.Dispose (disposing);
  131. }
  132. public View ViewToEdit
  133. {
  134. get => _viewToEdit;
  135. set
  136. {
  137. if (_viewToEdit == value)
  138. {
  139. return;
  140. }
  141. _viewToEdit = value;
  142. _marginEditor.AdornmentToEdit = _viewToEdit.Margin ?? null;
  143. _borderEditor.AdornmentToEdit = _viewToEdit.Border ?? null;
  144. _paddingEditor.AdornmentToEdit = _viewToEdit.Padding ?? null;
  145. _lblView.Text = _viewToEdit.ToString ();
  146. return;
  147. }
  148. }
  149. }