Example.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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"], DelayMs = 500, 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 ();
  27. // Setup automatic key injection for testing
  28. ExampleContextInjector.SetupAutomaticInjection (app);
  29. app.Init (driverName);
  30. app.Run<ExampleWindow> ();
  31. // Dispose the app to clean up and enable Console.WriteLine below
  32. app.Dispose ();
  33. // To see this output on the screen it must be done after shutdown,
  34. // which restores the previous screen.
  35. Console.WriteLine ($@"Username: {ExampleWindow.UserName}");
  36. // Defines a top-level window with border and title
  37. public sealed class ExampleWindow : Window
  38. {
  39. public static string UserName { get; set; }
  40. public ExampleWindow ()
  41. {
  42. Title = $"Example App ({Application.QuitKey} to quit)";
  43. // Create input components and labels
  44. var usernameLabel = new Label { Text = "Username:" };
  45. var userNameText = new TextField
  46. {
  47. // Position text field adjacent to the label
  48. X = Pos.Right (usernameLabel) + 1,
  49. // Fill remaining horizontal space
  50. Width = Dim.Fill ()
  51. };
  52. var passwordLabel = new Label
  53. {
  54. Text = "Password:", X = Pos.Left (usernameLabel), Y = Pos.Bottom (usernameLabel) + 1
  55. };
  56. var passwordText = new TextField
  57. {
  58. Secret = true,
  59. // align with the text box above
  60. X = Pos.Left (userNameText),
  61. Y = Pos.Top (passwordLabel),
  62. Width = Dim.Fill ()
  63. };
  64. // Create login button
  65. var btnLogin = new Button
  66. {
  67. Text = "Login",
  68. Y = Pos.Bottom (passwordLabel) + 1,
  69. // center the login button horizontally
  70. X = Pos.Center (),
  71. IsDefault = true
  72. };
  73. // When login button is clicked display a message popup
  74. btnLogin.Accepting += (s, e) =>
  75. {
  76. if (userNameText.Text == "admin" && passwordText.Text == "password")
  77. {
  78. MessageBox.Query (App, "Logging In", "Login Successful", "Ok");
  79. UserName = userNameText.Text;
  80. Application.RequestStop ();
  81. }
  82. else
  83. {
  84. MessageBox.ErrorQuery (App, "Logging In", "Incorrect username or password", "Ok");
  85. }
  86. // When Accepting is handled, set e.Handled to true to prevent further processing.
  87. e.Handled = true;
  88. };
  89. // Add the views to the Window
  90. Add (usernameLabel, userNameText, passwordLabel, passwordText, btnLogin);
  91. var lv = new ListView
  92. {
  93. Y = Pos.AnchorEnd (),
  94. Height = Dim.Auto (),
  95. Width = Dim.Auto ()
  96. };
  97. lv.SetSource (["One", "Two", "Three", "Four"]);
  98. Add (lv);
  99. }
  100. }