AdornmentsEditor.cs 3.5 KB

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