From 18cc7a935c6e8dea99f78727eae388f5a079892c Mon Sep 17 00:00:00 2001 From: Tig Date: Mon, 12 May 2025 17:39:31 -0600 Subject: [PATCH] More on #4058 - Adds `TextStyle` Scenario (#4079) * Fixed Generic.cs. Added TextStyles Scenario. * Code cleanup --- Examples/UICatalog/Scenarios/Generic.cs | 15 +-- Examples/UICatalog/Scenarios/TextStyles.cs | 123 +++++++++++++++++++++ 2 files changed, 125 insertions(+), 13 deletions(-) create mode 100644 Examples/UICatalog/Scenarios/TextStyles.cs diff --git a/Examples/UICatalog/Scenarios/Generic.cs b/Examples/UICatalog/Scenarios/Generic.cs index be11ce4bf..91c60a0f9 100644 --- a/Examples/UICatalog/Scenarios/Generic.cs +++ b/Examples/UICatalog/Scenarios/Generic.cs @@ -19,23 +19,12 @@ public sealed class Generic : Scenario BorderStyle = LineStyle.None }; - var button = new Shortcut() + var button = new Button () { - CanFocus = true, - Id = "button", X = Pos.Center (), Y = 1, - ShadowStyle = ShadowStyle.None, - Text = "HelpText", - Title = "Command", - Key = Key.F10, - HighlightStyle = HighlightStyle.None + Title = "_Button", }; - button.ColorScheme = Colors.ColorSchemes ["Error"]; - - button.Padding!.Thickness = new (1); - button.Padding.ColorScheme = Colors.ColorSchemes ["Toplevel"]; - button.Margin!.Thickness = new (1); button.Accepting += (s, e) => { diff --git a/Examples/UICatalog/Scenarios/TextStyles.cs b/Examples/UICatalog/Scenarios/TextStyles.cs new file mode 100644 index 000000000..c30bffdc5 --- /dev/null +++ b/Examples/UICatalog/Scenarios/TextStyles.cs @@ -0,0 +1,123 @@ +#nullable enable +using Terminal.Gui; + +namespace UICatalog.Scenarios; + +[ScenarioMetadata ("Text Styles", "Shows Attribute.TextStyles including bold, italic, etc...")] +[ScenarioCategory ("Text and Formatting")] +[ScenarioCategory ("Colors")] +public sealed class TestStyles : Scenario +{ + public override void Main () + { + // Init + Application.Init (); + + // Setup - Create a top-level application window and configure it. + Window appWindow = new () + { + Title = GetQuitKeyAndName (), + BorderStyle = LineStyle.None + }; + + appWindow.DrawingContent += OnAppWindowOnDrawingContent; + + // Run - Start the application. + Application.Run (appWindow); + appWindow.Dispose (); + + // Shutdown - Calling Application.Shutdown is required. + Application.Shutdown (); + } + + private void OnAppWindowOnDrawingContent (object? sender, DrawEventArgs args) + { + if (sender is View { } sendingView) + { + var y = 0; + var x = 0; + int maxWidth = sendingView.Viewport.Width; // Get the available width of the view + + TextStyle [] allStyles = Enum.GetValues (typeof (TextStyle)) + .Cast () + .Where (style => style != TextStyle.None) + .ToArray (); + + // Draw individual flags on the first line + foreach (TextStyle style in allStyles) + { + string text = Enum.GetName (typeof (TextStyle), style)!; + int textWidth = text.Length; + + // Check if the text fits in the current line + if (x + textWidth >= maxWidth) + { + x = 0; // Move to the next line + y++; + } + + sendingView.Move (x, y); + + var attr = new Attribute (sendingView.GetNormalColor ()) + { + TextStyle = style + }; + sendingView.SetAttribute (attr); + sendingView.AddStr (text); + + x += textWidth + 2; // Add spacing between entries + } + + // Add a blank line + y += 2; + x = 0; + + // Generate all combinations of TextStyle (excluding individual flags) + int totalCombinations = 1 << allStyles.Length; // 2^n combinations + + for (var i = 1; i < totalCombinations; i++) // Start from 1 to skip "None" + { + var combination = (TextStyle)0; + List styleNames = new (); + + for (var bit = 0; bit < allStyles.Length; bit++) + { + if ((i & (1 << bit)) != 0) + { + combination |= allStyles [bit]; + styleNames.Add (Enum.GetName (typeof (TextStyle), allStyles [bit])!); + } + } + + // Skip individual flags + if (styleNames.Count == 1) + { + continue; + } + + string text = $"[{string.Join (" | ", styleNames)}]"; + int textWidth = text.Length; + + // Check if the text fits in the current line + if (x + textWidth >= maxWidth) + { + x = 0; // Move to the next line + y++; + } + + sendingView.Move (x, y); + + var attr = new Attribute (sendingView.GetNormalColor ()) + { + TextStyle = combination + }; + sendingView.SetAttribute (attr); + sendingView.AddStr (text); + + x += textWidth + 2; // Add spacing between entries + } + + args.Cancel = true; + } + } +}