DimEditor.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #nullable enable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using Terminal.Gui;
  7. namespace UICatalog.Scenarios;
  8. /// <summary>
  9. /// Provides an editor UI for the Margin, Border, and Padding of a View.
  10. /// </summary>
  11. public class DimEditor : EditorBase
  12. {
  13. public DimEditor ()
  14. {
  15. Title = "Dim";
  16. Initialized += DimEditor_Initialized;
  17. }
  18. private int _value;
  19. private RadioGroup? _dimRadioGroup;
  20. private TextField? _valueEdit;
  21. /// <inheritdoc />
  22. protected override void OnViewToEditChanged ()
  23. {
  24. if (ViewToEdit is { })
  25. {
  26. ViewToEdit.SubViewsLaidOut += (_, _) => { OnUpdateLayoutSettings (); };
  27. }
  28. }
  29. protected override void OnUpdateLayoutSettings ()
  30. {
  31. Enabled = ViewToEdit is not Adornment;
  32. if (ViewToEdit is null)
  33. {
  34. return;
  35. }
  36. Dim? dim;
  37. dim = Dimension == Dimension.Width ? ViewToEdit.Width : ViewToEdit.Height;
  38. try
  39. {
  40. _dimRadioGroup!.SelectedItem = _dimNames.IndexOf (_dimNames.First (s => dim!.ToString ().StartsWith (s)));
  41. }
  42. catch (InvalidOperationException e)
  43. {
  44. // This is a hack to work around the fact that the Pos enum doesn't have an "Align" value yet
  45. Debug.WriteLine ($"{e}");
  46. }
  47. _valueEdit!.Enabled = false;
  48. switch (dim)
  49. {
  50. case DimAbsolute absolute:
  51. _valueEdit.Enabled = true;
  52. _value = absolute.Size;
  53. _valueEdit!.Text = _value.ToString ();
  54. break;
  55. case DimFill fill:
  56. var margin = fill.Margin as DimAbsolute;
  57. _valueEdit.Enabled = margin is { };
  58. _value = margin?.Size ?? 0;
  59. _valueEdit!.Text = _value.ToString ();
  60. break;
  61. case DimFunc func:
  62. _valueEdit.Enabled = true;
  63. _value = func.Fn ();
  64. _valueEdit!.Text = _value.ToString ();
  65. break;
  66. case DimPercent percent:
  67. _valueEdit.Enabled = true;
  68. _value = percent.Percentage;
  69. _valueEdit!.Text = _value.ToString ();
  70. break;
  71. default:
  72. _valueEdit!.Text = dim!.ToString ();
  73. break;
  74. }
  75. }
  76. public Dimension Dimension { get; set; }
  77. private void DimEditor_Initialized (object? sender, EventArgs e)
  78. {
  79. var label = new Label
  80. {
  81. X = 0, Y = 0,
  82. Text = $"{Title}:"
  83. };
  84. Add (label);
  85. _dimRadioGroup = new () { X = 0, Y = Pos.Bottom (label), RadioLabels = _radioItems };
  86. _dimRadioGroup.SelectedItemChanged += OnRadioGroupOnSelectedItemChanged;
  87. _valueEdit = new ()
  88. {
  89. X = Pos.Right (label) + 1,
  90. Y = 0,
  91. Width = Dim.Func (() => _radioItems.Max (i => i.GetColumns ()) - label.Frame.Width + 1),
  92. Text = $"{_value}"
  93. };
  94. _valueEdit.Accepting += (s, args) =>
  95. {
  96. try
  97. {
  98. _value = int.Parse (_valueEdit.Text);
  99. DimChanged ();
  100. }
  101. catch
  102. {
  103. // ignored
  104. }
  105. args.Handled = true;
  106. };
  107. Add (_valueEdit);
  108. Add (_dimRadioGroup);
  109. }
  110. private void OnRadioGroupOnSelectedItemChanged (object? s, SelectedItemChangedArgs selected) { DimChanged (); }
  111. // These need to have same order
  112. private readonly List<string> _dimNames = ["Absolute", "Auto", "Fill", "Func", "Percent",];
  113. private readonly string [] _radioItems = ["Absolute(n)", "Auto", "Fill(n)", "Func(()=>n)", "Percent(n)",];
  114. private void DimChanged ()
  115. {
  116. if (ViewToEdit == null || UpdatingLayoutSettings)
  117. {
  118. return;
  119. }
  120. try
  121. {
  122. Dim? dim = _dimRadioGroup!.SelectedItem switch
  123. {
  124. 0 => Dim.Absolute (_value),
  125. 1 => Dim.Auto (),
  126. 2 => Dim.Fill (_value),
  127. 3 => Dim.Func (() => _value),
  128. 4 => Dim.Percent (_value),
  129. _ => Dimension == Dimension.Width ? ViewToEdit.Width : ViewToEdit.Height
  130. };
  131. if (Dimension == Dimension.Width)
  132. {
  133. ViewToEdit.Width = dim;
  134. }
  135. else
  136. {
  137. ViewToEdit.Height = dim;
  138. }
  139. }
  140. catch (Exception e)
  141. {
  142. MessageBox.ErrorQuery ("Exception", e.Message, "Ok");
  143. }
  144. }
  145. }