2
0

Program.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // This is a test application for a self-contained single file.
  2. using System.Diagnostics;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Globalization;
  5. using Terminal.Gui.Configuration;
  6. using Terminal.Gui.App;
  7. using Terminal.Gui.Drawing;
  8. using Terminal.Gui.ViewBase;
  9. using Terminal.Gui.Views;
  10. namespace SelfContained;
  11. public static class Program
  12. {
  13. [RequiresUnreferencedCode ("Calls Terminal.Gui.Application.Run<T>(Func<Exception, Boolean>, IDriver)")]
  14. private static void Main (string [] args)
  15. {
  16. ConfigurationManager.Enable (ConfigLocations.All);
  17. IApplication app = Application.Create ();
  18. app.Init ();
  19. #region The code in this region is not intended for use in a self-contained single-file. It's just here to make sure there is no functionality break with localization in Terminal.Gui using single-file
  20. if (Equals (Thread.CurrentThread.CurrentUICulture, CultureInfo.InvariantCulture) && Application.SupportedCultures?.Count == 0)
  21. {
  22. // Only happens if the project has <InvariantGlobalization>true</InvariantGlobalization>
  23. Debug.Assert (Application.SupportedCultures.Count == 0);
  24. }
  25. else
  26. {
  27. Debug.Assert (Application.SupportedCultures?.Count > 0);
  28. Debug.Assert (Equals (CultureInfo.CurrentCulture, Thread.CurrentThread.CurrentUICulture));
  29. }
  30. #endregion
  31. using ExampleWindow exampleWindow = new ();
  32. string? userName = app.Run (exampleWindow) as string;
  33. // Shutdown the application in order to free resources and clean up the terminal
  34. app.Dispose ();
  35. // To see this output on the screen it must be done after shutdown,
  36. // which restores the previous screen.
  37. Console.WriteLine ($@"Username: {userName}");
  38. }
  39. }
  40. // Defines a top-level window with border and title
  41. public class ExampleWindow : Runnable<string>
  42. {
  43. public ExampleWindow ()
  44. {
  45. BorderStyle = LineStyle.Single;
  46. Title = $"Example App ({Application.QuitKey} to quit)";
  47. // Create input components and labels
  48. var usernameLabel = new Label { Text = "Username:" };
  49. var userNameText = new TextField
  50. {
  51. // Position text field adjacent to the label
  52. X = Pos.Right (usernameLabel) + 1,
  53. // Fill remaining horizontal space
  54. Width = Dim.Fill ()
  55. };
  56. var passwordLabel = new Label
  57. {
  58. Text = "Password:", X = Pos.Left (usernameLabel), Y = Pos.Bottom (usernameLabel) + 1
  59. };
  60. var passwordText = new TextField
  61. {
  62. Secret = true,
  63. // align with the text box above
  64. X = Pos.Left (userNameText),
  65. Y = Pos.Top (passwordLabel),
  66. Width = Dim.Fill ()
  67. };
  68. // Create login button
  69. var btnLogin = new Button
  70. {
  71. Text = "Login",
  72. Y = Pos.Bottom (passwordLabel) + 1,
  73. // center the login button horizontally
  74. X = Pos.Center (),
  75. IsDefault = true
  76. };
  77. // When login button is clicked display a message popup
  78. btnLogin.Accepting += (s, e) =>
  79. {
  80. if (userNameText.Text == "admin" && passwordText.Text == "password")
  81. {
  82. MessageBox.Query (App, "Logging In", "Login Successful", "Ok");
  83. Result = userNameText.Text;
  84. App?.RequestStop ();
  85. }
  86. else
  87. {
  88. MessageBox.ErrorQuery (App, "Logging In", "Incorrect username or password", "Ok");
  89. }
  90. // When Accepting is handled, set e.Handled to true to prevent further processing.
  91. e.Handled = true;
  92. };
  93. // Add the views to the Window
  94. Add (usernameLabel, userNameText, passwordLabel, passwordText, btnLogin);
  95. }
  96. }