DimEditor.cs 4.6 KB

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