-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
122 lines (103 loc) · 3.23 KB
/
Program.cs
File metadata and controls
122 lines (103 loc) · 3.23 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
// Fluent API example demonstrating IRunnable with automatic disposal and result extraction
using Terminal.Gui.App;
using Terminal.Gui.Drawing;
using Terminal.Gui.Resources;
using Terminal.Gui.ViewBase;
using Terminal.Gui.Views;
var smokeTest = args.Length > 0 && args [0] == "--smoke-test";
IApplication app = Application.Create ().Init ();
if (smokeTest)
{
using CancellationTokenSource cts = new (TimeSpan.FromSeconds (2));
await app.RunAsync<ColorPickerView> (cts.Token);
Console.WriteLine ("Smoke test passed.");
app.Dispose ();
return;
}
app.Run<ColorPickerView> ();
// Run the application with fluent API - automatically creates, runs, and disposes the runnable
Color? result = app.GetResult () as Color?;
// Shut down the app with Dispose before we can use Console.WriteLine
app.Dispose ();
if (result is not null)
{
Console.WriteLine (@$"Selected Color: {result}");
}
else
{
Console.WriteLine (@"No color selected");
}
/// <summary>
/// A runnable view that allows the user to select a color.
/// Demonstrates the Runnable with type pattern with automatic disposal.
/// </summary>
public class ColorPickerView : Runnable<Color?>
{
public ColorPickerView ()
{
Title = "Select a Color (Esc to quit)";
BorderStyle = LineStyle.Single;
Height = Dim.Auto ();
Width = Dim.Auto ();
// Add instructions
Label instructions = new ()
{
Text = "Use arrow keys to select a color, Enter to accept",
X = Pos.Center (),
Y = 0
};
// Create color picker
ColorPicker colorPicker = new ()
{
X = Pos.Center (),
Y = Pos.Bottom (instructions),
Style = new ColorPickerStyle
{
ShowColorName = true,
ShowTextFields = true
}
};
colorPicker.ApplyStyleChanges ();
// Create OK button
Button okButton = new ()
{
Title = "_OK",
X = Pos.Align (Alignment.Center),
Y = Pos.AnchorEnd (),
IsDefault = true
};
okButton.Accepting += (s, e) =>
{
// Extract result before stopping
Result = colorPicker.Value;
RequestStop ();
e.Handled = true;
};
// Create Cancel button
Button cancelButton = new ()
{
Title = Strings.btnCancel,
X = Pos.Align (Alignment.Center),
Y = Pos.AnchorEnd ()
};
cancelButton.Accepting += (s, e) =>
{
// Don't set result - leave as null
RequestStop ();
e.Handled = true;
};
// Add views
Add (instructions, colorPicker, okButton, cancelButton);
}
protected override bool OnIsRunningChanging (bool oldIsRunning, bool newIsRunning)
{
// Alternative place to extract result before stopping
// This is called before the view is removed from the stack
if (!newIsRunning && Result is null)
{
// User pressed Esc - could extract current selection here
//Result = SelectedColor;
}
return base.OnIsRunningChanging (oldIsRunning, newIsRunning);
}
}