ViewPropertiesEditor.cs 4.4 KB

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