AdornmentsEditor.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 : View
  9. {
  10. public AdornmentsEditor ()
  11. {
  12. Title = "AdornmentsEditor";
  13. Width = Dim.Auto (DimAutoStyle.Content);
  14. Height = Dim.Auto (DimAutoStyle.Content);
  15. CanFocus = true;
  16. TabStop = TabBehavior.TabGroup;
  17. ExpandButton = new ()
  18. {
  19. Orientation = Orientation.Horizontal
  20. };
  21. Initialized += AdornmentsEditor_Initialized;
  22. }
  23. private View? _viewToEdit;
  24. private MarginEditor? _marginEditor;
  25. private BorderEditor? _borderEditor;
  26. private PaddingEditor? _paddingEditor;
  27. /// <summary>
  28. /// Gets or sets whether the AdornmentsEditor should automatically select the View to edit
  29. /// based on the values of <see cref="AutoSelectSuperView"/> and <see cref="AutoSelectAdornments"/>.
  30. /// </summary>
  31. public bool AutoSelectViewToEdit { get; set; }
  32. /// <summary>
  33. /// Gets or sets the View that will scope the behavior of <see cref="AutoSelectViewToEdit"/>.
  34. /// </summary>
  35. public View? AutoSelectSuperView { get; set; }
  36. /// <summary>
  37. /// Gets or sets whether auto select with the mouse will select Adornments or just Views.
  38. /// </summary>
  39. public bool AutoSelectAdornments { get; set; }
  40. public View? ViewToEdit
  41. {
  42. get => _viewToEdit;
  43. set
  44. {
  45. if (_viewToEdit == value)
  46. {
  47. return;
  48. }
  49. _viewToEdit = value;
  50. if (_marginEditor is { })
  51. {
  52. _marginEditor.AdornmentToEdit = _viewToEdit?.Margin ?? null;
  53. }
  54. if (_borderEditor is { })
  55. {
  56. _borderEditor.AdornmentToEdit = _viewToEdit?.Border ?? null;
  57. }
  58. if (_paddingEditor is { })
  59. {
  60. _paddingEditor.AdornmentToEdit = _viewToEdit?.Padding ?? null;
  61. }
  62. if (_viewToEdit is not Adornment)
  63. {
  64. Enabled = true;
  65. }
  66. else
  67. {
  68. Enabled = false;
  69. }
  70. Padding.Text = $"View: {GetIdentifyingString(_viewToEdit)}";
  71. }
  72. }
  73. private string GetIdentifyingString (View? view)
  74. {
  75. if (view is null)
  76. {
  77. return "null";
  78. }
  79. if (!string.IsNullOrEmpty (view.Id))
  80. {
  81. return view.Id;
  82. }
  83. if (!string.IsNullOrEmpty (view.Title))
  84. {
  85. return view.Title;
  86. }
  87. if (!string.IsNullOrEmpty (view.Text))
  88. {
  89. return view.Text;
  90. }
  91. return view.GetType ().Name;
  92. }
  93. private void NavigationOnFocusedChanged (object? sender, EventArgs e)
  94. {
  95. if (AutoSelectSuperView is null)
  96. {
  97. return;
  98. }
  99. if (ApplicationNavigation.IsInHierarchy (this, Application.Navigation!.GetFocused ()))
  100. {
  101. return;
  102. }
  103. if (!ApplicationNavigation.IsInHierarchy (AutoSelectSuperView, Application.Navigation!.GetFocused ()))
  104. {
  105. return;
  106. }
  107. ViewToEdit = Application.Navigation!.GetFocused ();
  108. }
  109. private void ApplicationOnMouseEvent (object? sender, MouseEventArgs e)
  110. {
  111. if (e.Flags != MouseFlags.Button1Clicked || !AutoSelectViewToEdit)
  112. {
  113. return;
  114. }
  115. if ((AutoSelectSuperView is { } && !AutoSelectSuperView.FrameToScreen ().Contains (e.Position))
  116. || FrameToScreen ().Contains (e.Position))
  117. {
  118. return;
  119. }
  120. View view = e.View;
  121. if (view is { })
  122. {
  123. if (view is Adornment adornment)
  124. {
  125. ViewToEdit = AutoSelectAdornments ? adornment : adornment.Parent;
  126. }
  127. else
  128. {
  129. ViewToEdit = view;
  130. }
  131. }
  132. }
  133. /// <inheritdoc/>
  134. protected override void Dispose (bool disposing) { base.Dispose (disposing); }
  135. public ExpanderButton? ExpandButton { get; }
  136. public bool ShowViewIdentifier
  137. {
  138. get => Padding.Thickness != Thickness.Empty;
  139. set
  140. {
  141. if (value)
  142. {
  143. Padding.Thickness = new (0, 2, 0, 0);
  144. }
  145. else
  146. {
  147. Padding.Thickness =Thickness.Empty;
  148. }
  149. }
  150. }
  151. private void AdornmentsEditor_Initialized (object? sender, EventArgs e)
  152. {
  153. BorderStyle = LineStyle.Dotted;
  154. Border.Add (ExpandButton!);
  155. _marginEditor = new ()
  156. {
  157. X = 0,
  158. Y = 0,
  159. SuperViewRendersLineCanvas = true
  160. };
  161. Add (_marginEditor);
  162. _borderEditor = new ()
  163. {
  164. X = Pos.Left (_marginEditor),
  165. Y = Pos.Bottom (_marginEditor),
  166. SuperViewRendersLineCanvas = true
  167. };
  168. Add (_borderEditor);
  169. _paddingEditor = new ()
  170. {
  171. X = Pos.Left (_borderEditor),
  172. Y = Pos.Bottom (_borderEditor),
  173. SuperViewRendersLineCanvas = true
  174. };
  175. Add (_paddingEditor);
  176. _marginEditor.AdornmentToEdit = _viewToEdit?.Margin ?? null;
  177. _borderEditor.AdornmentToEdit = _viewToEdit?.Border ?? null;
  178. _paddingEditor.AdornmentToEdit = _viewToEdit?.Padding ?? null;
  179. Application.MouseEvent += ApplicationOnMouseEvent;
  180. Application.Navigation!.FocusedChanged += NavigationOnFocusedChanged;
  181. }
  182. }