Skip to content

Commit eea13c2

Browse files
authored
More on #4058 - Adds TextStyle Scenario (#4079)
* Fixed Generic.cs. Added TextStyles Scenario. * Code cleanup
1 parent 41a5314 commit eea13c2

File tree

2 files changed

+125
-13
lines changed

2 files changed

+125
-13
lines changed

Examples/UICatalog/Scenarios/Generic.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,12 @@ public override void Main ()
1919
BorderStyle = LineStyle.None
2020
};
2121

22-
var button = new Shortcut()
22+
var button = new Button ()
2323
{
24-
CanFocus = true,
25-
Id = "button",
2624
X = Pos.Center (),
2725
Y = 1,
28-
ShadowStyle = ShadowStyle.None,
29-
Text = "HelpText",
30-
Title = "Command",
31-
Key = Key.F10,
32-
HighlightStyle = HighlightStyle.None
26+
Title = "_Button",
3327
};
34-
button.ColorScheme = Colors.ColorSchemes ["Error"];
35-
36-
button.Padding!.Thickness = new (1);
37-
button.Padding.ColorScheme = Colors.ColorSchemes ["Toplevel"];
38-
button.Margin!.Thickness = new (1);
3928

4029
button.Accepting += (s, e) =>
4130
{
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#nullable enable
2+
using Terminal.Gui;
3+
4+
namespace UICatalog.Scenarios;
5+
6+
[ScenarioMetadata ("Text Styles", "Shows Attribute.TextStyles including bold, italic, etc...")]
7+
[ScenarioCategory ("Text and Formatting")]
8+
[ScenarioCategory ("Colors")]
9+
public sealed class TestStyles : Scenario
10+
{
11+
public override void Main ()
12+
{
13+
// Init
14+
Application.Init ();
15+
16+
// Setup - Create a top-level application window and configure it.
17+
Window appWindow = new ()
18+
{
19+
Title = GetQuitKeyAndName (),
20+
BorderStyle = LineStyle.None
21+
};
22+
23+
appWindow.DrawingContent += OnAppWindowOnDrawingContent;
24+
25+
// Run - Start the application.
26+
Application.Run (appWindow);
27+
appWindow.Dispose ();
28+
29+
// Shutdown - Calling Application.Shutdown is required.
30+
Application.Shutdown ();
31+
}
32+
33+
private void OnAppWindowOnDrawingContent (object? sender, DrawEventArgs args)
34+
{
35+
if (sender is View { } sendingView)
36+
{
37+
var y = 0;
38+
var x = 0;
39+
int maxWidth = sendingView.Viewport.Width; // Get the available width of the view
40+
41+
TextStyle [] allStyles = Enum.GetValues (typeof (TextStyle))
42+
.Cast<TextStyle> ()
43+
.Where (style => style != TextStyle.None)
44+
.ToArray ();
45+
46+
// Draw individual flags on the first line
47+
foreach (TextStyle style in allStyles)
48+
{
49+
string text = Enum.GetName (typeof (TextStyle), style)!;
50+
int textWidth = text.Length;
51+
52+
// Check if the text fits in the current line
53+
if (x + textWidth >= maxWidth)
54+
{
55+
x = 0; // Move to the next line
56+
y++;
57+
}
58+
59+
sendingView.Move (x, y);
60+
61+
var attr = new Attribute (sendingView.GetNormalColor ())
62+
{
63+
TextStyle = style
64+
};
65+
sendingView.SetAttribute (attr);
66+
sendingView.AddStr (text);
67+
68+
x += textWidth + 2; // Add spacing between entries
69+
}
70+
71+
// Add a blank line
72+
y += 2;
73+
x = 0;
74+
75+
// Generate all combinations of TextStyle (excluding individual flags)
76+
int totalCombinations = 1 << allStyles.Length; // 2^n combinations
77+
78+
for (var i = 1; i < totalCombinations; i++) // Start from 1 to skip "None"
79+
{
80+
var combination = (TextStyle)0;
81+
List<string> styleNames = new ();
82+
83+
for (var bit = 0; bit < allStyles.Length; bit++)
84+
{
85+
if ((i & (1 << bit)) != 0)
86+
{
87+
combination |= allStyles [bit];
88+
styleNames.Add (Enum.GetName (typeof (TextStyle), allStyles [bit])!);
89+
}
90+
}
91+
92+
// Skip individual flags
93+
if (styleNames.Count == 1)
94+
{
95+
continue;
96+
}
97+
98+
string text = $"[{string.Join (" | ", styleNames)}]";
99+
int textWidth = text.Length;
100+
101+
// Check if the text fits in the current line
102+
if (x + textWidth >= maxWidth)
103+
{
104+
x = 0; // Move to the next line
105+
y++;
106+
}
107+
108+
sendingView.Move (x, y);
109+
110+
var attr = new Attribute (sendingView.GetNormalColor ())
111+
{
112+
TextStyle = combination
113+
};
114+
sendingView.SetAttribute (attr);
115+
sendingView.AddStr (text);
116+
117+
x += textWidth + 2; // Add spacing between entries
118+
}
119+
120+
args.Cancel = true;
121+
}
122+
}
123+
}

0 commit comments

Comments
 (0)