-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
143 lines (116 loc) · 4.62 KB
/
Copy pathProgram.cs
File metadata and controls
143 lines (116 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// This is a test application for a self-contained single file.
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using Terminal.Gui.App;
using Terminal.Gui.Configuration;
using Terminal.Gui.Drawing;
using Terminal.Gui.Input;
using Terminal.Gui.ViewBase;
using Terminal.Gui.Views;
namespace SelfContained;
public static class Program
{
private static async Task Main (string[] args)
{
var smokeTest = args.Length > 0 && args [0] == "--smoke-test";
if (smokeTest)
{
ConfigurationManager.Enable (ConfigLocations.All);
Application.AppModel = AppModel.Inline;
IApplication app = Application.Create ();
app.Init ();
using CancellationTokenSource cts = new (TimeSpan.FromSeconds (2));
using ExampleWindow smokeWindow = new ();
await app.RunAsync (smokeWindow, cts.Token);
Console.WriteLine ("Smoke test passed.");
app.Dispose ();
return;
}
#pragma warning disable IL2026 // Run() has attributes for AOT compatibility
Run ();
#pragma warning restore IL2026
}
[RequiresUnreferencedCode ("Calls Terminal.Gui.Application.Run<T>(Func<Exception, Boolean>, IDriver)")]
private static void Run ()
{
ConfigurationManager.Enable (ConfigLocations.All);
// Use Inline mode — renders below the shell prompt without alternate screen buffer
Application.AppModel = AppModel.Inline;
IApplication app = Application.Create ();
app.Init ();
#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
if (Equals (Thread.CurrentThread.CurrentUICulture, CultureInfo.InvariantCulture) &&
Application.SupportedCultures?.Count == 0)
{
// Only happens if the project has <InvariantGlobalization>true</InvariantGlobalization>
Debug.Assert (Application.SupportedCultures.Count == 0);
}
else
{
Debug.Assert (Application.SupportedCultures?.Count > 0);
Debug.Assert (Equals (CultureInfo.CurrentCulture, Thread.CurrentThread.CurrentUICulture));
}
#endregion
using ExampleWindow exampleWindow = new ();
var userName = app.Run (exampleWindow) as string;
// Shutdown the application in order to free resources and clean up the terminal
app.Dispose ();
// To see this output on the screen it must be done after shutdown,
// which restores the previous screen.
Console.WriteLine ($@"Username: {userName}");
}
}
// Defines a top-level window with border and title
public class ExampleWindow : Runnable<string>
{
public ExampleWindow ()
{
BorderStyle = LineStyle.Single;
Title = $"Example App ({Application.GetDefaultKey (Command.Quit)} to quit)";
// Create input components and labels
Label usernameLabel = new () { Text = "Username:" };
TextField userNameText = new ()
{
// Position text field adjacent to the label
X = Pos.Right (usernameLabel) + 1,
// Fill remaining horizontal space
Width = Dim.Fill ()
};
Label passwordLabel = new ()
{ Text = "Password:", X = Pos.Left (usernameLabel), Y = Pos.Bottom (usernameLabel) + 1 };
TextField passwordText = new ()
{
Secret = true,
// align with the text box above
X = Pos.Left (userNameText),
Y = Pos.Top (passwordLabel),
Width = Dim.Fill ()
};
// Create login button
Button btnLogin = new ()
{
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.Accepted += (_, _) =>
{
if (userNameText.Text == "admin" && passwordText.Text == "password")
{
MessageBox.Query (App!, "Logging In", "Login Successful", "Ok");
Result = userNameText.Text;
App?.RequestStop ();
}
else
{
MessageBox.ErrorQuery (App!, "Logging In", "Incorrect username or password", "Ok");
}
};
// Add the views to the Window
Add (usernameLabel, userNameText, passwordLabel, passwordText, btnLogin);
}
}