diff --git a/Terminal.Gui/Text/TextFormatter.cs b/Terminal.Gui/Text/TextFormatter.cs index 77876fbe5..95b317038 100644 --- a/Terminal.Gui/Text/TextFormatter.cs +++ b/Terminal.Gui/Text/TextFormatter.cs @@ -29,6 +29,13 @@ public class TextFormatter private Alignment _textVerticalAlignment = Alignment.Start; private bool _wordWrap = true; + /// + /// Gets or sets whether to use the new architecture for drawing. + /// When true, the Draw method will use the new separated formatter/renderer architecture. + /// This provides better performance and addresses Format/Draw coupling issues. + /// + public bool UseNewArchitecture { get; set; } = false; + /// /// Initializes a new instance of the class. /// @@ -154,18 +161,26 @@ public class TextFormatter IConsoleDriver? driver = null ) { - // Use the new architecture - this addresses @tig's feedback that the new architecture wasn't being used - // Sync properties with the new formatter - SyncFormatterProperties(); - - // Format the text using the new architecture - FormattedText formattedText = _formatter.Format(); - - // Render using the new renderer - _renderer.Draw(formattedText, screen, normalColor, hotColor, FillRemaining, maximum, driver); - } + // If using new architecture, delegate to the improved implementation + if (UseNewArchitecture) + { + DrawWithNewArchitecture(screen, normalColor, hotColor, maximum, driver); + return; + } - /// + // Original implementation follows... + // With this check, we protect against subclasses with overrides of Text (like Button) + if (string.IsNullOrEmpty (Text)) + { + return; + } + + if (driver is null) + { + driver = Application.Driver; + } + + driver?.SetAttribute (normalColor); List linesFormatted = GetLines (); diff --git a/Tests/UnitTestsParallelizable/Text/TextFormatterNewArchitectureTests.cs b/Tests/UnitTestsParallelizable/Text/TextFormatterNewArchitectureTests.cs index 90a837d1c..c902c343c 100644 --- a/Tests/UnitTestsParallelizable/Text/TextFormatterNewArchitectureTests.cs +++ b/Tests/UnitTestsParallelizable/Text/TextFormatterNewArchitectureTests.cs @@ -148,4 +148,26 @@ public class TextFormatterNewArchitectureTests Application.Shutdown(); } + + [Fact] + public void TextFormatter_UseNewArchitecture_Flag_Works() + { + Application.Init(new FakeDriver()); + + var tf = new TextFormatter + { + Text = "Hello World", + UseNewArchitecture = true // Enable new architecture + }; + + // This should now use the new architecture via the Draw method + tf.Draw(new Rectangle(0, 0, 10, 1), Attribute.Default, Attribute.Default); + + // Test that the new architecture produces results + Size size = tf.GetFormattedSizeWithNewArchitecture(); + Assert.True(size.Width > 0); + Assert.True(size.Height > 0); + + Application.Shutdown(); + } } \ No newline at end of file