Parcourir la source

More on #4058 - Adds `TextStyle` Scenario (#4079)

* Fixed Generic.cs.
Added TextStyles Scenario.

* Code cleanup
Tig il y a 4 mois
Parent
commit
18cc7a935c
2 fichiers modifiés avec 125 ajouts et 13 suppressions
  1. 2 13
      Examples/UICatalog/Scenarios/Generic.cs
  2. 123 0
      Examples/UICatalog/Scenarios/TextStyles.cs

+ 2 - 13
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) =>
                             {

+ 123 - 0
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<TextStyle> ()
+                                         .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<string> 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;
+        }
+    }
+}