Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #3229. TextFormatter should have a FillRemaining property. #3245

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Terminal.Gui/Drawing/Thickness.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public Rect Draw (Rect rect, string label = null)
Alignment = TextAlignment.Centered,
VerticalAlignment = VerticalTextAlignment.Bottom
};
tf.Draw (rect, Application.Driver.CurrentAttribute, Application.Driver.CurrentAttribute, rect, false);
tf.Draw (rect, Application.Driver.CurrentAttribute, Application.Driver.CurrentAttribute, rect);
}

return GetInside (rect);
Expand Down
11 changes: 8 additions & 3 deletions Terminal.Gui/Text/TextFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ public TextDirection Direction
}
}

/// <summary>
/// Determines if the bounds width will be used or only the text width will be used,
/// If <see langword="true"/> all the bounds area will be filled with whitespaces and the same background color
/// showing a perfect rectangle.
/// </summary>
public bool FillRemaining { get; set; }

/// <summary>Gets or sets the hot key. Fires the <see cref="HotKeyChanged"/> event.</summary>
public Key HotKey
{
Expand Down Expand Up @@ -297,15 +304,13 @@ public bool WordWrap
/// <param name="normalColor">The color to use for all text except the hotkey</param>
/// <param name="hotColor">The color to use to draw the hotkey</param>
/// <param name="containerBounds">Specifies the screen-relative location and maximum container size.</param>
/// <param name="fillRemaining">Determines if the bounds width will be used (default) or only the text width will be used.</param>
/// <param name="driver">The console driver currently used by the application.</param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public void Draw (
Rect bounds,
Attribute normalColor,
Attribute hotColor,
Rect containerBounds = default,
bool fillRemaining = true,
ConsoleDriver driver = null
)
{
Expand Down Expand Up @@ -509,7 +514,7 @@ public void Draw (
continue;
}

if (!fillRemaining && idx > runes.Length - 1)
if (!FillRemaining && idx > runes.Length - 1)
{
break;
}
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/View/Adornment/Adornment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public override void OnDrawContent (Rect contentArea)
}
}

TextFormatter?.Draw (screenBounds, normalAttr, normalAttr, Rect.Empty, false);
TextFormatter?.Draw (screenBounds, normalAttr, normalAttr, Rect.Empty);

//base.OnDrawContent (contentArea);
}
Expand Down
3 changes: 1 addition & 2 deletions Terminal.Gui/View/ViewDrawing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,7 @@ public virtual void OnDrawContent (Rect contentArea)
BoundsToScreen (contentArea),
HasFocus ? GetFocusColor () : GetNormalColor (),
HasFocus ? ColorScheme.HotFocus : GetHotNormalColor (),
Rect.Empty,
false
Rect.Empty
);
SetSubViewNeedsDisplay ();
}
Expand Down
3 changes: 1 addition & 2 deletions Terminal.Gui/Views/ProgressBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ public override void OnDrawContent (Rect contentArea)
BoundsToScreen (Bounds),
attr,
ColorScheme.Normal,
SuperView?.BoundsToScreen (SuperView.Bounds) ?? default (Rect),
false
SuperView?.BoundsToScreen (SuperView.Bounds) ?? default (Rect)
);
}
}
Expand Down
59 changes: 55 additions & 4 deletions UnitTests/Text/TextFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -419,14 +419,68 @@ public void Draw_With_Combining_Runes (int width, int height, TextDirection text
new Attribute (ColorName.White, ColorName.Black),
new Attribute (ColorName.Blue, ColorName.Black),
default (Rect),
true,
driver
);
TestHelpers.AssertDriverContentsWithFrameAre (expected, _output, driver);

driver.End ();
}

[Fact]
[SetupFakeDriver]
public void FillRemaining_True_False ()
{
((FakeDriver)Application.Driver).SetBufferSize (22, 5);

Attribute [] attrs =
{
Attribute.Default, new Attribute (ColorName.Green, ColorName.BrightMagenta),
new Attribute (ColorName.Blue, ColorName.Cyan)
};
var tf = new TextFormatter { Size = new Size (14, 3), Text = "Test\nTest long\nTest long long\n", MultiLine = true };

tf.Draw (
new Rect (1, 1, 19, 3),
attrs [1],
attrs [2]);

Assert.False (tf.FillRemaining);

TestHelpers.AssertDriverContentsWithFrameAre (
@"
Test
Test long
Test long long",
_output);

TestHelpers.AssertDriverAttributesAre (
@"
000000000000000000000
011110000000000000000
011111111100000000000
011111111111111000000
000000000000000000000",
null,
attrs);

tf.FillRemaining = true;

tf.Draw (
new Rect (1, 1, 19, 3),
attrs [1],
attrs [2]);

TestHelpers.AssertDriverAttributesAre (
@"
000000000000000000000
011111111111111111110
011111111111111111110
011111111111111111110
000000000000000000000",
null,
attrs);
}

[Theory]
[InlineData ("_k Before", true, 0, (KeyCode)'K')] // lower case should return uppercase Hotkey
[InlineData ("a_k Second", true, 1, (KeyCode)'K')]
Expand Down Expand Up @@ -2010,7 +2064,6 @@ string expected
new Attribute (ColorName.White, ColorName.Black),
new Attribute (ColorName.Blue, ColorName.Black),
default (Rect),
true,
driver
);
TestHelpers.AssertDriverContentsWithFrameAre (expected, _output, driver);
Expand Down Expand Up @@ -2049,7 +2102,6 @@ string expected
new Attribute (ColorName.White, ColorName.Black),
new Attribute (ColorName.Blue, ColorName.Black),
default (Rect),
true,
driver
);
TestHelpers.AssertDriverContentsWithFrameAre (expected, _output, driver);
Expand Down Expand Up @@ -2088,7 +2140,6 @@ string expected
new Attribute (ColorName.White, ColorName.Black),
new Attribute (ColorName.Blue, ColorName.Black),
default (Rect),
true,
driver
);
TestHelpers.AssertDriverContentsWithFrameAre (expected, _output, driver);
Expand Down
10 changes: 3 additions & 7 deletions UnitTests/Views/LabelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public void Label_Draw_Fill_Remaining_AutoSize_True ()
tf1.Text = "This TextFormatter (tf1) without fill will not be cleared on rewritten.";
Size tf1Size = tf1.Size;

var tf2 = new TextFormatter { Direction = TextDirection.LeftRight_TopBottom };
var tf2 = new TextFormatter { Direction = TextDirection.LeftRight_TopBottom, FillRemaining = true };
tf2.Text = "This TextFormatter (tf2) with fill will be cleared on rewritten.";
Size tf2Size = tf2.Size;

Expand All @@ -191,9 +191,7 @@ public void Label_Draw_Fill_Remaining_AutoSize_True ()
tf1.Draw (
new Rect (new Point (0, 1), tf1Size),
label.GetNormalColor (),
label.ColorScheme.HotNormal,
default (Rect),
false
label.ColorScheme.HotNormal
);

tf2.Draw (new Rect (new Point (0, 2), tf2Size), label.GetNormalColor (), label.ColorScheme.HotNormal);
Expand All @@ -215,9 +213,7 @@ This TextFormatter (tf2) with fill will be cleared on rewritten.
tf1.Draw (
new Rect (new Point (0, 1), tf1Size),
label.GetNormalColor (),
label.ColorScheme.HotNormal,
default (Rect),
false
label.ColorScheme.HotNormal
);

tf2.Text = "This TextFormatter (tf2) is rewritten.";
Expand Down
Loading