ViewPropertiesEditor.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 += (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. _orientationOptionSelector = new ()
  44. {
  45. X = Pos.Right (label) + 1,
  46. Y = Pos.Top (label),
  47. Orientation = Orientation.Horizontal
  48. };
  49. _orientationOptionSelector.ValueChanged += (s, selected) =>
  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 += (s, e) =>
  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
  79. {
  80. if (_text is null)
  81. {
  82. return string.Empty;
  83. }
  84. return _text!.Text;
  85. }
  86. set => _text!.Text = value;
  87. }
  88. protected override void OnViewToEditChanged ()
  89. {
  90. Enabled = ViewToEdit is not Adornment;
  91. if (ViewToEdit is { } and not Adornment)
  92. {
  93. _canFocusCheckBox!.CheckedState = ViewToEdit.CanFocus ? CheckState.Checked : CheckState.UnChecked;
  94. _enabledCheckBox!.CheckedState = ViewToEdit.Enabled ? CheckState.Checked : CheckState.UnChecked;
  95. if (ViewToEdit is IOrientation orientatedView)
  96. {
  97. _orientationOptionSelector!.Value = orientatedView.Orientation;
  98. _orientationOptionSelector.Enabled = true;
  99. }
  100. else
  101. {
  102. _orientationOptionSelector!.Enabled = false;
  103. }
  104. }
  105. }
  106. }