Example.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #nullable enable
  2. // A simple Terminal.Gui example in C# - using C# 9.0 Top-level statements
  3. // This is a simple example application. For the full range of functionality
  4. // see the UICatalog project
  5. using Terminal.Gui.App;
  6. using Terminal.Gui.Configuration;
  7. using Terminal.Gui.Examples;
  8. using Terminal.Gui.ViewBase;
  9. using Terminal.Gui.Views;
  10. [assembly: ExampleMetadata ("Simple Example", "A basic login form demonstrating Terminal.Gui fundamentals")]
  11. [assembly: ExampleCategory ("Getting Started")]
  12. [assembly: ExampleDemoKeyStrokes (KeyStrokes = ["a", "d", "m", "i", "n", "Tab", "p", "a", "s", "s", "w", "o", "r", "d", "Enter"], Order = 1)]
  13. [assembly: ExampleDemoKeyStrokes (KeyStrokes = ["Enter"], DelayMs = 500, Order = 2)]
  14. [assembly: ExampleDemoKeyStrokes (KeyStrokes = ["Esc"], DelayMs = 100, Order = 3)]
  15. // Override the default configuration for the application to use the Light theme
  16. ConfigurationManager.RuntimeConfig = """{ "Theme": "Light" }""";
  17. ConfigurationManager.Enable (ConfigLocations.All);
  18. // Check for test context to determine driver
  19. string? contextJson = Environment.GetEnvironmentVariable (ExampleContext.ENVIRONMENT_VARIABLE_NAME);
  20. string? driverName = null;
  21. if (!string.IsNullOrEmpty (contextJson))
  22. {
  23. ExampleContext? context = ExampleContext.FromJson (contextJson);
  24. driverName = context?.DriverName;
  25. }
  26. IApplication app = Application.Create ().Init (driverName);
  27. app.Run<ExampleWindow> ();
  28. // Dispose the app to clean up and enable Console.WriteLine below
  29. app.Dispose ();
  30. // To see this output on the screen it must be done after shutdown,
  31. // which restores the previous screen.
  32. Console.WriteLine ($@"Username: {ExampleWindow.UserName}");
  33. // Defines a top-level window with border and title
  34. public sealed class ExampleWindow : Window
  35. {
  36. public static string UserName { get; set; }
  37. public ExampleWindow ()
  38. {
  39. Title = $"Example App ({Application.QuitKey} to quit)";
  40. // Create input components and labels
  41. var usernameLabel = new Label { Text = "Username:" };
  42. var userNameText = new TextField
  43. {
  44. // Position text field adjacent to the label
  45. X = Pos.Right (usernameLabel) + 1,
  46. // Fill remaining horizontal space
  47. Width = Dim.Fill ()
  48. };
  49. var passwordLabel = new Label
  50. {
  51. Text = "Password:", X = Pos.Left (usernameLabel), Y = Pos.Bottom (usernameLabel) + 1
  52. };
  53. var passwordText = new TextField
  54. {
  55. Secret = true,
  56. // align with the text box above
  57. X = Pos.Left (userNameText),
  58. Y = Pos.Top (passwordLabel),
  59. Width = Dim.Fill ()
  60. };
  61. // Create login button
  62. var btnLogin = new Button
  63. {
  64. Text = "Login",
  65. Y = Pos.Bottom (passwordLabel) + 1,
  66. // center the login button horizontally
  67. X = Pos.Center (),
  68. IsDefault = true
  69. };
  70. // When login button is clicked display a message popup
  71. btnLogin.Accepting += (s, e) =>
  72. {
  73. if (userNameText.Text == "admin" && passwordText.Text == "password")
  74. {
  75. MessageBox.Query (App, "Logging In", "Login Successful", "Ok");
  76. UserName = userNameText.Text;
  77. Application.RequestStop ();
  78. }
  79. else
  80. {
  81. MessageBox.ErrorQuery (App, "Logging In", "Incorrect username or password", "Ok");
  82. }
  83. // When Accepting is handled, set e.Handled to true to prevent further processing.
  84. e.Handled = true;
  85. };
  86. // Add the views to the Window
  87. Add (usernameLabel, userNameText, passwordLabel, passwordText, btnLogin);
  88. var lv = new ListView
  89. {
  90. Y = Pos.AnchorEnd (),
  91. Height = Dim.Auto (),
  92. Width = Dim.Auto ()
  93. };
  94. lv.SetSource (["One", "Two", "Three", "Four"]);
  95. Add (lv);
  96. }
  97. }