ViewPropertiesEditor.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #nullable enable
  2. namespace UICatalog.Scenarios;
  3. public class ViewPropertiesEditor : EditorBase
  4. {
  5. private CheckBox? _canFocusCheckBox;
  6. private CheckBox? _enabledCheckBox;
  7. private RadioGroup? _orientation;
  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 += (s, args) =>
  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 += (s, args) =>
  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. _orientation = new ()
  44. {
  45. X = Pos.Right (label) + 1,
  46. Y = Pos.Top (label),
  47. RadioLabels = new [] { "Horizontal", "Vertical" },
  48. Orientation = Orientation.Horizontal
  49. };
  50. _orientation.SelectedItemChanged += (s, selected) =>
  51. {
  52. if (ViewToEdit is IOrientation orientatedView)
  53. {
  54. orientatedView.Orientation = (Orientation)_orientation.SelectedItem!;
  55. }
  56. };
  57. Add (label, _orientation);
  58. label = new () { X = 0, Y = Pos.Bottom (_orientation), Text = "Text:" };
  59. _text = new ()
  60. {
  61. X = Pos.Right (label) + 1,
  62. Y = Pos.Top (label),
  63. Width = Dim.Fill (),
  64. Height = Dim.Auto (minimumContentDim: 2),
  65. Text = "This is demo text"
  66. };
  67. _text.ContentsChanged += (s, e) =>
  68. {
  69. if (ViewToEdit is { })
  70. {
  71. ViewToEdit.Text = _text.Text;
  72. }
  73. };
  74. Add (label, _text);
  75. base.EndInit ();
  76. }
  77. public string DemoText
  78. {
  79. get
  80. {
  81. if (_text is null)
  82. {
  83. return string.Empty;
  84. }
  85. return _text!.Text;
  86. }
  87. set => _text!.Text = value;
  88. }
  89. protected override void OnViewToEditChanged ()
  90. {
  91. Enabled = ViewToEdit is not Adornment;
  92. if (ViewToEdit is { } and not Adornment)
  93. {
  94. _canFocusCheckBox!.CheckedState = ViewToEdit.CanFocus ? CheckState.Checked : CheckState.UnChecked;
  95. _enabledCheckBox!.CheckedState = ViewToEdit.Enabled ? CheckState.Checked : CheckState.UnChecked;
  96. if (ViewToEdit is IOrientation orientatedView)
  97. {
  98. _orientation!.SelectedItem = (int)orientatedView.Orientation;
  99. _orientation.Enabled = true;
  100. }
  101. else
  102. {
  103. _orientation!.Enabled = false;
  104. }
  105. }
  106. }
  107. }