mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-26 15:57:56 +01:00
126 lines
4.3 KiB
C#
126 lines
4.3 KiB
C#
#nullable enable
|
|
// A simple Terminal.Gui example in C# - using C# 9.0 Top-level statements
|
|
|
|
// This is a simple example application. For the full range of functionality
|
|
// see the UICatalog project
|
|
|
|
using Terminal.Gui.App;
|
|
using Terminal.Gui.Configuration;
|
|
using Terminal.Gui.Examples;
|
|
using Terminal.Gui.ViewBase;
|
|
using Terminal.Gui.Views;
|
|
|
|
[assembly: ExampleMetadata ("Simple Example", "A basic login form demonstrating Terminal.Gui fundamentals")]
|
|
[assembly: ExampleCategory ("Getting Started")]
|
|
[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["a", "d", "m", "i", "n", "Tab", "p", "a", "s", "s", "w", "o", "r", "d", "Enter"], DelayMs = 500, Order = 1)]
|
|
[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["Enter"], DelayMs = 500, Order = 2)]
|
|
[assembly: ExampleDemoKeyStrokes (KeyStrokes = ["Esc"], DelayMs = 100, Order = 3)]
|
|
|
|
// Override the default configuration for the application to use the Light theme
|
|
ConfigurationManager.RuntimeConfig = """{ "Theme": "Light" }""";
|
|
ConfigurationManager.Enable (ConfigLocations.All);
|
|
|
|
// Check for test context to determine driver
|
|
string? contextJson = Environment.GetEnvironmentVariable (ExampleContext.ENVIRONMENT_VARIABLE_NAME);
|
|
string? driverName = null;
|
|
|
|
if (!string.IsNullOrEmpty (contextJson))
|
|
{
|
|
ExampleContext? context = ExampleContext.FromJson (contextJson);
|
|
driverName = context?.DriverName;
|
|
}
|
|
|
|
IApplication app = Application.Create ();
|
|
|
|
// Setup automatic key injection for testing
|
|
ExampleContextInjector.SetupAutomaticInjection (app);
|
|
|
|
app.Init (driverName);
|
|
app.Run<ExampleWindow> ();
|
|
|
|
// Dispose the app to clean up and enable Console.WriteLine below
|
|
app.Dispose ();
|
|
|
|
// To see this output on the screen it must be done after shutdown,
|
|
// which restores the previous screen.
|
|
Console.WriteLine ($@"Username: {ExampleWindow.UserName}");
|
|
|
|
// Defines a top-level window with border and title
|
|
public sealed class ExampleWindow : Window
|
|
{
|
|
public static string UserName { get; set; }
|
|
|
|
public ExampleWindow ()
|
|
{
|
|
Title = $"Example App ({Application.QuitKey} to quit)";
|
|
|
|
// Create input components and labels
|
|
var usernameLabel = new Label { Text = "Username:" };
|
|
|
|
var userNameText = new TextField
|
|
{
|
|
// Position text field adjacent to the label
|
|
X = Pos.Right (usernameLabel) + 1,
|
|
|
|
// Fill remaining horizontal space
|
|
Width = Dim.Fill ()
|
|
};
|
|
|
|
var passwordLabel = new Label
|
|
{
|
|
Text = "Password:", X = Pos.Left (usernameLabel), Y = Pos.Bottom (usernameLabel) + 1
|
|
};
|
|
|
|
var passwordText = new TextField
|
|
{
|
|
Secret = true,
|
|
|
|
// align with the text box above
|
|
X = Pos.Left (userNameText),
|
|
Y = Pos.Top (passwordLabel),
|
|
Width = Dim.Fill ()
|
|
};
|
|
|
|
// Create login button
|
|
var btnLogin = new Button
|
|
{
|
|
Text = "Login",
|
|
Y = Pos.Bottom (passwordLabel) + 1,
|
|
|
|
// center the login button horizontally
|
|
X = Pos.Center (),
|
|
IsDefault = true
|
|
};
|
|
|
|
// When login button is clicked display a message popup
|
|
btnLogin.Accepting += (s, e) =>
|
|
{
|
|
if (userNameText.Text == "admin" && passwordText.Text == "password")
|
|
{
|
|
MessageBox.Query (App, "Logging In", "Login Successful", "Ok");
|
|
UserName = userNameText.Text;
|
|
Application.RequestStop ();
|
|
}
|
|
else
|
|
{
|
|
MessageBox.ErrorQuery (App, "Logging In", "Incorrect username or password", "Ok");
|
|
}
|
|
|
|
// When Accepting is handled, set e.Handled to true to prevent further processing.
|
|
e.Handled = true;
|
|
};
|
|
|
|
// Add the views to the Window
|
|
Add (usernameLabel, userNameText, passwordLabel, passwordText, btnLogin);
|
|
|
|
var lv = new ListView
|
|
{
|
|
Y = Pos.AnchorEnd (),
|
|
Height = Dim.Auto (),
|
|
Width = Dim.Auto ()
|
|
};
|
|
lv.SetSource (["One", "Two", "Three", "Four"]);
|
|
Add (lv);
|
|
}
|
|
}
|