Program.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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;
  6. namespace SelfContained;
  7. public static class Program
  8. {
  9. [RequiresUnreferencedCode ("Calls Terminal.Gui.Application.Run<T>(Func<Exception, Boolean>, IConsoleDriver)")]
  10. private static void Main (string [] args)
  11. {
  12. Application.Init ();
  13. #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
  14. if (Equals (Thread.CurrentThread.CurrentUICulture, CultureInfo.InvariantCulture) && Application.SupportedCultures?.Count == 0)
  15. {
  16. // Only happens if the project has <InvariantGlobalization>true</InvariantGlobalization>
  17. Debug.Assert (Application.SupportedCultures.Count == 0);
  18. }
  19. else
  20. {
  21. Debug.Assert (Application.SupportedCultures?.Count > 0);
  22. Debug.Assert (Equals (CultureInfo.CurrentCulture, Thread.CurrentThread.CurrentUICulture));
  23. }
  24. #endregion
  25. ExampleWindow app = new ();
  26. Application.Run (app);
  27. // Dispose the app object before shutdown
  28. app.Dispose ();
  29. // Before the application exits, reset Terminal.Gui for clean shutdown
  30. Application.Shutdown ();
  31. // To see this output on the screen it must be done after shutdown,
  32. // which restores the previous screen.
  33. Console.WriteLine ($@"Username: {ExampleWindow.UserName}");
  34. }
  35. }
  36. // Defines a top-level window with border and title
  37. public class ExampleWindow : Window
  38. {
  39. public static string? UserName;
  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 ("Logging In", "Login Successful", "Ok");
  79. UserName = userNameText.Text;
  80. Application.RequestStop ();
  81. }
  82. else
  83. {
  84. MessageBox.ErrorQuery ("Logging In", "Incorrect username or password", "Ok");
  85. }
  86. };
  87. // Add the views to the Window
  88. Add (usernameLabel, userNameText, passwordLabel, passwordText, btnLogin);
  89. }
  90. }