ViewPropertiesEditor.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #nullable enable
  2. namespace UICatalog.Scenarios;
  3. public class ViewPropertiesEditor : EditorBase
  4. {
  5. private CheckBox? _canFocusCheckBox;
  6. private CheckBox? _enabledCheckBox;
  7. private OptionSelector<Orientation>? _orientationOptionSelector;
  8. private TextView? _text;
  9. /// <inheritdoc/>
  10. public override void EndInit ()
  11. {
  12. _canFocusCheckBox = new ()
  13. {
  14. Title = "CanFocus",
  15. X = 0,
  16. Y = 0,
  17. CheckedState = ViewToEdit is { } ? ViewToEdit.CanFocus ? CheckState.Checked : CheckState.UnChecked : CheckState.UnChecked
  18. };
  19. _canFocusCheckBox.CheckedStateChanged += (_, _) =>
  20. {
  21. if (ViewToEdit is { })
  22. {
  23. ViewToEdit.CanFocus = _canFocusCheckBox.CheckedState == CheckState.Checked;
  24. }
  25. };
  26. base.Add (_canFocusCheckBox);
  27. _enabledCheckBox = new ()
  28. {
  29. Title = "Enabled",
  30. X = Pos.Right (_canFocusCheckBox) + 1,
  31. Y = Pos.Top (_canFocusCheckBox),
  32. CheckedState = ViewToEdit is { } ? ViewToEdit.Enabled ? CheckState.Checked : CheckState.UnChecked : CheckState.UnChecked
  33. };
  34. _enabledCheckBox.CheckedStateChanged += (_, _) =>
  35. {
  36. if (ViewToEdit is { })
  37. {
  38. ViewToEdit.Enabled = _enabledCheckBox.CheckedState == CheckState.Checked;
  39. }
  40. };
  41. base.Add (_enabledCheckBox);
  42. Label label = new () { X = Pos.Right (_enabledCheckBox) + 1, Y = Pos.Top (_enabledCheckBox), Text = "Orientation:" };
  43. _orientationOptionSelector = new ()
  44. {
  45. X = Pos.Right (label) + 1,
  46. Y = Pos.Top (label),
  47. Orientation = Orientation.Horizontal
  48. };
  49. _orientationOptionSelector.ValueChanged += (_, _) =>
  50. {
  51. if (ViewToEdit is IOrientation orientatedView)
  52. {
  53. orientatedView.Orientation = _orientationOptionSelector.Value!.Value;
  54. }
  55. };
  56. Add (label, _orientationOptionSelector);
  57. label = new () { X = 0, Y = Pos.Bottom (_orientationOptionSelector), Text = "Text:" };
  58. _text = new ()
  59. {
  60. X = Pos.Right (label) + 1,
  61. Y = Pos.Top (label),
  62. Width = Dim.Fill (),
  63. Height = Dim.Auto (minimumContentDim: 2),
  64. Text = "This is demo text"
  65. };
  66. _text.ContentsChanged += (_, _) =>
  67. {
  68. if (ViewToEdit is { })
  69. {
  70. ViewToEdit.Text = _text.Text;
  71. }
  72. };
  73. Add (label, _text);
  74. base.EndInit ();
  75. }
  76. public string DemoText
  77. {
  78. get => _text is null ? string.Empty : _text!.Text;
  79. set => _text!.Text = value;
  80. }
  81. protected override void OnViewToEditChanged ()
  82. {
  83. Enabled = ViewToEdit is not Adornment;
  84. if (ViewToEdit is { } and not Adornment)
  85. {
  86. _canFocusCheckBox!.CheckedState = ViewToEdit.CanFocus ? CheckState.Checked : CheckState.UnChecked;
  87. _enabledCheckBox!.CheckedState = ViewToEdit.Enabled ? CheckState.Checked : CheckState.UnChecked;
  88. if (ViewToEdit is IOrientation orientatedView)
  89. {
  90. _orientationOptionSelector!.Value = orientatedView.Orientation;
  91. _orientationOptionSelector.Enabled = true;
  92. }
  93. else
  94. {
  95. _orientationOptionSelector!.Enabled = false;
  96. }
  97. }
  98. }
  99. }