Adornments.cs 6.5 KB

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