AdornmentsEditor.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #nullable enable
  2. using System;
  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 : EditorBase
  9. {
  10. public AdornmentsEditor ()
  11. {
  12. Title = "AdornmentsEditor";
  13. TabStop = TabBehavior.TabGroup;
  14. ExpanderButton!.Orientation = Orientation.Horizontal;
  15. Initialized += AdornmentsEditor_Initialized;
  16. SchemeName = "Dialog";
  17. }
  18. public MarginEditor? MarginEditor { get; set; }
  19. public BorderEditor? BorderEditor { get; private set; }
  20. public PaddingEditor? PaddingEditor { get; private set; }
  21. /// <inheritdoc/>
  22. protected override void OnViewToEditChanged ()
  23. {
  24. //Enabled = ViewToEdit is not Adornment;
  25. if (MarginEditor is { })
  26. {
  27. MarginEditor.AdornmentToEdit = ViewToEdit?.Margin ?? null;
  28. }
  29. if (BorderEditor is { })
  30. {
  31. BorderEditor.AdornmentToEdit = ViewToEdit?.Border ?? null;
  32. }
  33. if (PaddingEditor is { })
  34. {
  35. PaddingEditor.AdornmentToEdit = ViewToEdit?.Padding ?? null;
  36. }
  37. if (Padding is { })
  38. {
  39. Padding.Text = $"View: {GetIdentifyingString (ViewToEdit)}";
  40. }
  41. }
  42. private string GetIdentifyingString (View? view)
  43. {
  44. if (view is null)
  45. {
  46. return "null";
  47. }
  48. if (!string.IsNullOrEmpty (view.Id))
  49. {
  50. return view.Id;
  51. }
  52. if (!string.IsNullOrEmpty (view.Title))
  53. {
  54. return view.Title;
  55. }
  56. if (!string.IsNullOrEmpty (view.Text))
  57. {
  58. return view.Text;
  59. }
  60. return view.GetType ().Name;
  61. }
  62. public bool ShowViewIdentifier
  63. {
  64. get => Padding is { } && Padding.Thickness != Thickness.Empty;
  65. set
  66. {
  67. if (Padding is null)
  68. {
  69. return;
  70. }
  71. Padding.Thickness = value ? new (0, 2, 0, 0) : Thickness.Empty;
  72. }
  73. }
  74. private void AdornmentsEditor_Initialized (object? sender, EventArgs e)
  75. {
  76. MarginEditor = new ()
  77. {
  78. X = -1,
  79. Y = 0,
  80. SuperViewRendersLineCanvas = true,
  81. BorderStyle = LineStyle.Single
  82. };
  83. MarginEditor.Border!.Thickness = MarginEditor.Border.Thickness with { Bottom = 0 };
  84. Add (MarginEditor);
  85. BorderEditor = new ()
  86. {
  87. X = Pos.Left (MarginEditor),
  88. Y = Pos.Bottom (MarginEditor),
  89. SuperViewRendersLineCanvas = true,
  90. BorderStyle = LineStyle.Single
  91. };
  92. BorderEditor.Border!.Thickness = BorderEditor.Border.Thickness with { Bottom = 0 };
  93. Add (BorderEditor);
  94. PaddingEditor = new ()
  95. {
  96. X = Pos.Left (BorderEditor),
  97. Y = Pos.Bottom (BorderEditor),
  98. SuperViewRendersLineCanvas = true,
  99. BorderStyle = LineStyle.Single
  100. };
  101. PaddingEditor.Border!.Thickness = PaddingEditor.Border.Thickness with { Bottom = 0 };
  102. Add (PaddingEditor);
  103. Width = Dim.Auto (maximumContentDim: Dim.Func (() => MarginEditor.Frame.Width - 2));
  104. MarginEditor.ExpanderButton!.Collapsed = true;
  105. BorderEditor.ExpanderButton!.Collapsed = true;
  106. PaddingEditor.ExpanderButton!.Collapsed = true;
  107. MarginEditor.AdornmentToEdit = ViewToEdit?.Margin ?? null;
  108. BorderEditor.AdornmentToEdit = ViewToEdit?.Border ?? null;
  109. PaddingEditor.AdornmentToEdit = ViewToEdit?.Padding ?? null;
  110. }
  111. }