Adornments.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.Overlapped | 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!.SetScheme (
  40. new (color.SuperView.GetScheme ())
  41. {
  42. Normal = new (
  43. color.SuperView.GetAttributeForRole (VisualRole.Normal).Foreground,
  44. e.CurrentValue,
  45. color.SuperView.GetAttributeForRole (VisualRole.Normal).Style
  46. )
  47. });
  48. };
  49. var button = new Button { X = Pos.Center (), Y = Pos.Center (), Text = "Press me!" };
  50. button.Accepting += (s, e) =>
  51. MessageBox.Query (20, 7, "Hi", $"Am I a {window.GetType ().Name}?", "Yes", "No");
  52. var label = new TextView
  53. {
  54. X = Pos.Center (),
  55. Y = Pos.Bottom (button),
  56. Title = "Title",
  57. Text = "I have a 3 row top border.\nMy border inherits from the SuperView.",
  58. Width = 40,
  59. Height = 6 // TODO: Use Dim.Auto
  60. };
  61. label.Border.Thickness = new (1, 3, 1, 1);
  62. var btnButtonInWindow = new Button { X = Pos.AnchorEnd (), Y = Pos.AnchorEnd (), Text = "Button" };
  63. var labelAnchorEnd = new Label
  64. {
  65. Y = Pos.AnchorEnd (),
  66. Width = 40,
  67. Height = Dim.Percent (20),
  68. Text = "Label\nY=AnchorEnd(),Height=Dim.Percent(10)",
  69. SchemeName = "Dialog"
  70. };
  71. window.Margin.Data = "Margin";
  72. window.Margin.Text = "Margin Text";
  73. window.Margin.Thickness = new (0);
  74. window.Border.Data = "Border";
  75. window.Border.Text = "Border Text";
  76. window.Border.Thickness = new (0);
  77. window.Padding.Data = "Padding";
  78. window.Padding.Text = "Padding Text line 1\nPadding Text line 3\nPadding Text line 3\nPadding Text line 4\nPadding Text line 5";
  79. window.Padding.Thickness = new (3);
  80. window.Padding.SchemeName = "Error";
  81. window.Padding.CanFocus = true;
  82. var longLabel = new Label
  83. {
  84. X = 40, Y = 5, Title = "This is long text (in a label) that should clip."
  85. };
  86. longLabel.TextFormatter.WordWrap = true;
  87. window.Add (tf1, color, button, label, btnButtonInWindow, labelAnchorEnd, longLabel);
  88. window.Initialized += (s, e) =>
  89. {
  90. editor.ViewToEdit = window;
  91. editor.ShowViewIdentifier = true;
  92. var labelInPadding = new Label { X = 0, Y = 1, Title = "_Text:" };
  93. window.Padding.Add (labelInPadding);
  94. var textFieldInPadding = new TextField
  95. {
  96. X = Pos.Right (labelInPadding) + 1,
  97. Y = Pos.Top (labelInPadding), Width = 10,
  98. Text = "text (Y = 1)",
  99. CanFocus = true
  100. };
  101. textFieldInPadding.Accepting += (s, e) => MessageBox.Query (20, 7, "TextField", textFieldInPadding.Text, "Ok");
  102. window.Padding.Add (textFieldInPadding);
  103. var btnButtonInPadding = new Button
  104. {
  105. X = Pos.Center (),
  106. Y = 1,
  107. Text = "_Button in Padding Y = 1",
  108. CanFocus = true,
  109. HighlightStyle = HighlightStyle.None,
  110. };
  111. btnButtonInPadding.Accepting += (s, e) => MessageBox.Query (20, 7, "Hi", "Button in Padding Pressed!", "Ok");
  112. btnButtonInPadding.BorderStyle = LineStyle.Dashed;
  113. btnButtonInPadding.Border.Thickness = new (1, 1, 1, 1);
  114. window.Padding.Add (btnButtonInPadding);
  115. #if SUBVIEW_BASED_BORDER
  116. btnButtonInPadding.Border.CloseButton.Visible = true;
  117. view.Border.CloseButton.Visible = true;
  118. view.Border.CloseButton.Accept += (s, e) =>
  119. {
  120. MessageBox.Query (20, 7, "Hi", "Window Close Button Pressed!", "Ok");
  121. e.Handled = true;
  122. };
  123. view.Accept += (s, e) => MessageBox.Query (20, 7, "Hi", "Window Close Button Pressed!", "Ok");
  124. #endif
  125. };
  126. editor.AutoSelectViewToEdit = true;
  127. editor.AutoSelectSuperView = window;
  128. editor.AutoSelectAdornments = true;
  129. Application.Run (app);
  130. app.Dispose ();
  131. Application.Shutdown ();
  132. }
  133. }