Adornments.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using Terminal.Gui;
  2. namespace UICatalog.Scenarios;
  3. [ScenarioMetadata ("Adornments Demo", "Demonstrates Margin, Border, and Padding on Views.")]
  4. [ScenarioCategory ("Layout")]
  5. [ScenarioCategory ("Adornments")]
  6. public class Adornments : Scenario
  7. {
  8. public override void Main ()
  9. {
  10. Application.Init ();
  11. Window app = new ()
  12. {
  13. Title = GetQuitKeyAndName ()
  14. };
  15. var editor = new AdornmentsEditor
  16. {
  17. AutoSelectViewToEdit = true,
  18. // This is for giggles, to show that the editor can be moved around.
  19. Arrangement = ViewArrangement.Movable,
  20. X = Pos.AnchorEnd ()
  21. };
  22. editor.Border.Thickness = new (1, 2, 1, 1);
  23. app.Add (editor);
  24. var window = new Window
  25. {
  26. Title = "The _Window",
  27. Arrangement = ViewArrangement.Movable,
  28. // X = Pos.Center (),
  29. Width = Dim.Percent (60),
  30. Height = Dim.Percent (90)
  31. };
  32. app.Add (window);
  33. var tf1 = new TextField { Width = 10, Text = "TextField" };
  34. var color = new ColorPicker16 { Title = "BG", BoxHeight = 1, BoxWidth = 1, X = Pos.AnchorEnd () };
  35. color.BorderStyle = LineStyle.RoundedDotted;
  36. color.ColorChanged += (s, e) =>
  37. {
  38. color.SuperView.ColorScheme = new (color.SuperView.ColorScheme)
  39. {
  40. Normal = new (
  41. color.SuperView.ColorScheme.Normal.Foreground,
  42. e.CurrentValue
  43. )
  44. };
  45. };
  46. var button = new Button { X = Pos.Center (), Y = Pos.Center (), Text = "Press me!" };
  47. button.Accepting += (s, e) =>
  48. MessageBox.Query (20, 7, "Hi", $"Am I a {window.GetType ().Name}?", "Yes", "No");
  49. var label = new TextView
  50. {
  51. X = Pos.Center (),
  52. Y = Pos.Bottom (button),
  53. Title = "Title",
  54. Text = "I have a 3 row top border.\nMy border inherits from the SuperView.",
  55. Width = 40,
  56. Height = 6 // TODO: Use Dim.Auto
  57. };
  58. label.Border.Thickness = new (1, 3, 1, 1);
  59. var btnButtonInWindow = new Button { X = Pos.AnchorEnd (), Y = Pos.AnchorEnd (), Text = "Button" };
  60. var labelAnchorEnd = new Label
  61. {
  62. Y = Pos.AnchorEnd (),
  63. Width = 40,
  64. Height = Dim.Percent (20),
  65. Text = "Label\nY=AnchorEnd(),Height=Dim.Percent(10)",
  66. ColorScheme = Colors.ColorSchemes ["Error"]
  67. };
  68. window.Margin.Data = "Margin";
  69. window.Margin.Thickness = new (3);
  70. window.Border.Data = "Border";
  71. window.Border.Thickness = new (3);
  72. window.Padding.Data = "Padding";
  73. window.Padding.Thickness = new (3);
  74. window.Padding.CanFocus = true;
  75. var longLabel = new Label
  76. {
  77. X = 40, Y = 5, Title = "This is long text (in a label) that should clip."
  78. };
  79. longLabel.TextFormatter.WordWrap = true;
  80. window.Add (tf1, color, button, label, btnButtonInWindow, labelAnchorEnd, longLabel);
  81. editor.Initialized += (s, e) => { editor.ViewToEdit = window; };
  82. window.Initialized += (s, e) =>
  83. {
  84. var labelInPadding = new Label { X = 1, Y = 0, Title = "_Text:" };
  85. window.Padding.Add (labelInPadding);
  86. var textFieldInPadding = new TextField
  87. {
  88. X = Pos.Right (labelInPadding) + 1,
  89. Y = Pos.Top (labelInPadding), Width = 15,
  90. Text = "some text",
  91. CanFocus = true
  92. };
  93. textFieldInPadding.Accepting += (s, e) => MessageBox.Query (20, 7, "TextField", textFieldInPadding.Text, "Ok");
  94. window.Padding.Add (textFieldInPadding);
  95. var btnButtonInPadding = new Button
  96. {
  97. X = Pos.Center (),
  98. Y = 0,
  99. Text = "_Button in Padding",
  100. CanFocus = true
  101. };
  102. btnButtonInPadding.Accepting += (s, e) => MessageBox.Query (20, 7, "Hi", "Button in Padding Pressed!", "Ok");
  103. btnButtonInPadding.BorderStyle = LineStyle.Dashed;
  104. btnButtonInPadding.Border.Thickness = new (1, 1, 1, 1);
  105. window.Padding.Add (btnButtonInPadding);
  106. #if SUBVIEW_BASED_BORDER
  107. btnButtonInPadding.Border.CloseButton.Visible = true;
  108. view.Border.CloseButton.Visible = true;
  109. view.Border.CloseButton.Accept += (s, e) =>
  110. {
  111. MessageBox.Query (20, 7, "Hi", "Window Close Button Pressed!", "Ok");
  112. e.Cancel = true;
  113. };
  114. view.Accept += (s, e) => MessageBox.Query (20, 7, "Hi", "Window Close Button Pressed!", "Ok");
  115. #endif
  116. };
  117. editor.AutoSelectViewToEdit = true;
  118. editor.AutoSelectSuperView = window;
  119. editor.AutoSelectAdornments = true;
  120. Application.Run (app);
  121. app.Dispose ();
  122. Application.Shutdown ();
  123. }
  124. }