AdornmentsEditor.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. public AdornmentsEditor ()
  11. {
  12. //ColorScheme = Colors.ColorSchemes ["Dialog"];
  13. Title = "AdornmentsEditor";
  14. Width = Dim.Auto (DimAutoStyle.Content);
  15. Height = Dim.Auto (DimAutoStyle.Content);
  16. //SuperViewRendersLineCanvas = true;
  17. TabStop = TabBehavior.TabGroup;
  18. Initialized += AdornmentsEditor_Initialized;
  19. }
  20. private readonly ViewDiagnosticFlags _savedDiagnosticFlags = Diagnostics;
  21. private View _viewToEdit;
  22. private Label _lblView; // Text describing the vi
  23. private MarginEditor _marginEditor;
  24. private BorderEditor _borderEditor;
  25. private PaddingEditor _paddingEditor;
  26. // TODO: Move Diagnostics to a separate Editor class (DiagnosticsEditor?).
  27. private CheckBox _diagPaddingCheckBox;
  28. private CheckBox _diagRulerCheckBox;
  29. /// <summary>
  30. /// Gets or sets whether the AdornmentsEditor should automatically select the View to edit when the mouse is clicked
  31. /// anywhere outside the editor.
  32. /// </summary>
  33. public bool AutoSelectViewToEdit { get; set; }
  34. public View ViewToEdit
  35. {
  36. get => _viewToEdit;
  37. set
  38. {
  39. if (_viewToEdit == value)
  40. {
  41. return;
  42. }
  43. _viewToEdit = value;
  44. _marginEditor.AdornmentToEdit = _viewToEdit?.Margin ?? null;
  45. _borderEditor.AdornmentToEdit = _viewToEdit?.Border ?? null;
  46. _paddingEditor.AdornmentToEdit = _viewToEdit?.Padding ?? null;
  47. _lblView.Text = $"{_viewToEdit?.GetType ().Name}: {_viewToEdit?.Id}" ?? string.Empty;
  48. }
  49. }
  50. /// <inheritdoc/>
  51. protected override void Dispose (bool disposing)
  52. {
  53. Diagnostics = _savedDiagnosticFlags;
  54. base.Dispose (disposing);
  55. }
  56. private void AdornmentsEditor_Initialized (object sender, EventArgs e)
  57. {
  58. BorderStyle = LineStyle.Dotted;
  59. var expandButton = new ExpanderButton
  60. {
  61. Orientation = Orientation.Horizontal
  62. };
  63. Border.Add (expandButton);
  64. _lblView = new ()
  65. {
  66. X = 0,
  67. Y = 0,
  68. Height = 2
  69. };
  70. _lblView.TextFormatter.WordWrap = true;
  71. _lblView.TextFormatter.MultiLine = true;
  72. _lblView.HotKeySpecifier = (Rune)'\uffff';
  73. Add (_lblView);
  74. _marginEditor = new ()
  75. {
  76. X = 0,
  77. Y = Pos.Bottom (_lblView),
  78. SuperViewRendersLineCanvas = true
  79. };
  80. Add (_marginEditor);
  81. _lblView.Width = Dim.Width (_marginEditor);
  82. _borderEditor = new ()
  83. {
  84. X = Pos.Left (_marginEditor),
  85. Y = Pos.Bottom (_marginEditor),
  86. SuperViewRendersLineCanvas = true
  87. };
  88. Add (_borderEditor);
  89. _paddingEditor = new ()
  90. {
  91. X = Pos.Left (_borderEditor),
  92. Y = Pos.Bottom (_borderEditor),
  93. SuperViewRendersLineCanvas = true
  94. };
  95. Add (_paddingEditor);
  96. _diagPaddingCheckBox = new () { Text = "_Diagnostic Padding" };
  97. _diagPaddingCheckBox.CheckedState = Diagnostics.FastHasFlags (ViewDiagnosticFlags.Padding) ? CheckState.Checked : CheckState.UnChecked;
  98. _diagPaddingCheckBox.CheckedStateChanging += (s, e) =>
  99. {
  100. if (e.NewValue == CheckState.Checked)
  101. {
  102. Diagnostics |= ViewDiagnosticFlags.Padding;
  103. }
  104. else
  105. {
  106. Diagnostics &= ~ViewDiagnosticFlags.Padding;
  107. }
  108. };
  109. Add (_diagPaddingCheckBox);
  110. _diagPaddingCheckBox.Y = Pos.Bottom (_paddingEditor);
  111. _diagRulerCheckBox = new () { Text = "_Diagnostic Ruler" };
  112. _diagRulerCheckBox.CheckedState = Diagnostics.FastHasFlags (ViewDiagnosticFlags.Ruler) ? CheckState.Checked : CheckState.UnChecked;
  113. _diagRulerCheckBox.CheckedStateChanging += (s, e) =>
  114. {
  115. if (e.NewValue == CheckState.Checked)
  116. {
  117. Diagnostics |= ViewDiagnosticFlags.Ruler;
  118. }
  119. else
  120. {
  121. Diagnostics &= ~ViewDiagnosticFlags.Ruler;
  122. }
  123. };
  124. Add (_diagRulerCheckBox);
  125. _diagRulerCheckBox.Y = Pos.Bottom (_diagPaddingCheckBox);
  126. }
  127. }