TextFormat

This commit is contained in:
2023-08-16 19:21:50 +02:00
parent cbbf7b3704
commit 3c996f0c20
31 changed files with 304 additions and 94 deletions

View File

@@ -71,7 +71,7 @@ public sealed partial class Border<T> : ContentView<Border<T>, T>, IDisplayView
if (contentRendered)
{
var driver = renderContext.ConsoleDriver;
driver.ResetColor();
driver.ResetStyle();
SetColorsForDriver(renderContext);
RenderTopBorder(renderContext, position, size);

View File

@@ -170,13 +170,11 @@ public sealed class Grid<T> : ChildContainerView<Grid<T>, T>, IVisibilityChangeH
_forceRerenderChildren.Clear();
}
var childContext = new RenderContext(
context.ConsoleDriver,
context.ForceRerender,
Foreground ?? context.Foreground,
Background ?? context.Background,
context.Statistics
);
var childContext = context with
{
Foreground = Foreground ?? context.Foreground,
Background = Background ?? context.Background,
};
var viewsByPosition = GroupViewsByPosition(columnWidths.Length, rowHeights.Length);
var anyRendered = false;
@@ -214,7 +212,6 @@ public sealed class Grid<T> : ChildContainerView<Grid<T>, T>, IVisibilityChangeH
int row,
IReadOnlyList<IView> forceRerenderChildren)
{
var width = columnWidths[column];
var height = rowHeights[row];
var renderSize = new Size(width, height);
@@ -238,7 +235,7 @@ public sealed class Grid<T> : ChildContainerView<Grid<T>, T>, IVisibilityChangeH
}
if (renderSize.Width == 0 || renderSize.Height == 0) return false;
if (!viewsByPosition.TryGetValue((column, row), out var children))
{
RenderEmpty(context, renderPosition, renderSize);
@@ -253,7 +250,8 @@ public sealed class Grid<T> : ChildContainerView<Grid<T>, T>, IVisibilityChangeH
true,
context.Foreground,
context.Background,
context.Statistics
context.Statistics,
context.TextFormat
);
RenderEmpty(context, renderPosition, renderSize);
}
@@ -270,7 +268,8 @@ public sealed class Grid<T> : ChildContainerView<Grid<T>, T>, IVisibilityChangeH
true,
context.Foreground,
context.Background,
context.Statistics
context.Statistics,
context.TextFormat
);
}
}

View File

@@ -85,7 +85,8 @@ public sealed partial class ItemsControl<TDataContext, TItem>
double width = 0;
double height = 0;
foreach (var child in _children)
var children = _children.ToList();
foreach (var child in children)
{
if (!child.IsVisible) continue;
@@ -149,13 +150,7 @@ public sealed partial class ItemsControl<TDataContext, TItem>
if (forceRerenderChildren.Contains(child))
{
var rerenderContext = new RenderContext(
renderContext.ConsoleDriver,
true,
renderContext.Foreground,
renderContext.Background,
renderContext.Statistics
);
var rerenderContext = renderContext with {ForceRerender = true};
neededRerender = child.Render(rerenderContext, childPosition, childSize) || neededRerender;
}
else

View File

@@ -286,7 +286,7 @@ public sealed partial class ListView<TDataContext, TItem> : View<ListView<TDataC
var driver = ApplicationContext!.ConsoleDriver;
var placeholder = new string(' ', size.Width);
driver.ResetColor();
driver.ResetStyle();
for (var i = deltaY; i < size.Height; i++)
{
driver.SetCursorPosition(position with {Y = position.Y + i});

View File

@@ -111,7 +111,7 @@ public partial class ProgressBar<T> : View<ProgressBar<T>, T>
};
}
SetColor(driver, foreground, background);
SetStyleColor(renderContext, foreground, background);
// Left border
var textStartPosition = position;
@@ -132,7 +132,7 @@ public partial class ProgressBar<T> : View<ProgressBar<T>, T>
// Transient character
if (progressQuotientWidth < progressAvailableSpace)
{
SetColor(driver, foreground, unfilledBackground);
SetStyleColor(renderContext, foreground, unfilledBackground);
RenderText(
transientChar,
driver,
@@ -147,7 +147,7 @@ public partial class ProgressBar<T> : View<ProgressBar<T>, T>
Span<char> unfilledText = stackalloc char[progressRemainderWidth];
unfilledText.Fill(unfilledCharacter);
SetColor(driver, unfilledForeground, unfilledBackground);
SetStyleColor(renderContext, unfilledForeground, unfilledBackground);
RenderText(
unfilledText,
driver,
@@ -159,7 +159,7 @@ public partial class ProgressBar<T> : View<ProgressBar<T>, T>
// Right border
if (rightCap.HasValue)
{
SetColor(driver, foreground, background);
SetStyleColor(renderContext, foreground, background);
RenderText(
rightCap.Value,
driver,

View File

@@ -25,7 +25,7 @@ public sealed partial class Rectangle<T> : View<Rectangle<T>, T>, IDisplayView
var driver = renderContext.ConsoleDriver;
var s = new string(' ', size.Width);
driver.ResetColor();
driver.ResetStyle();
if (color is not null)
{
driver.SetBackgroundColor(color);

View File

@@ -80,13 +80,7 @@ public sealed partial class StackPanel<T> : ChildContainerView<StackPanel<T>, T>
if (forceRerenderChildren.Contains(child))
{
var rerenderContext = new RenderContext(
renderContext.ConsoleDriver,
true,
renderContext.Foreground,
renderContext.Background,
renderContext.Statistics
);
var rerenderContext = renderContext with {ForceRerender = true};
neededRerender = child.Render(rerenderContext, childPosition, childSize) || neededRerender;
}
else

View File

@@ -2,8 +2,8 @@
using System.Diagnostics;
using PropertyChanged.SourceGenerator;
using TerminalUI.Color;
using TerminalUI.Extensions;
using TerminalUI.Models;
using TerminalUI.TextFormat;
using TerminalUI.Traits;
namespace TerminalUI.Controls;
@@ -16,7 +16,8 @@ public sealed partial class TextBlock<T> : View<TextBlock<T>, T>, IDisplayView
Size Size,
string? Text,
IColor? Foreground,
IColor? Background);
IColor? Background,
ITextFormat? TextFormat);
private RenderState? _lastRenderState;
private string[]? _textLines;
@@ -24,17 +25,13 @@ public sealed partial class TextBlock<T> : View<TextBlock<T>, T>, IDisplayView
[Notify] private string? _text = string.Empty;
[Notify] private TextAlignment _textAlignment = TextAlignment.Left;
[Notify] private ITextFormat? _textFormat;
public TextBlock()
{
/*this.Bind(
this,
dc => dc == null ? string.Empty : dc.ToString(),
tb => tb.Text
);*/
RerenderProperties.Add(nameof(Text));
RerenderProperties.Add(nameof(TextAlignment));
RerenderProperties.Add(nameof(TextFormat));
((INotifyPropertyChanged) this).PropertyChanged += (o, e) =>
{
@@ -58,7 +55,8 @@ public sealed partial class TextBlock<T> : View<TextBlock<T>, T>, IDisplayView
size,
Text,
foreground,
background);
background,
_textFormat);
if (!renderContext.ForceRerender && !NeedsRerender(renderState)) return false;
@@ -78,7 +76,7 @@ public sealed partial class TextBlock<T> : View<TextBlock<T>, T>, IDisplayView
_placeholderRenderDone = false;
var driver = renderContext.ConsoleDriver;
SetColor(driver, foreground, background);
SetStyleColor(renderContext, foreground, background, _textFormat);
RenderText(_textLines, driver, position, size, TransformText);

View File

@@ -104,7 +104,7 @@ public sealed partial class TextBox<T> : View<TextBox<T>, T>, IFocusable, IDispl
_lastRenderState = renderStatus;
var driver = renderContext.ConsoleDriver;
SetColor(driver, foreground, background);
SetStyleColor(renderContext, foreground, background);
RenderEmpty(renderContext, position, size);

View File

@@ -8,6 +8,7 @@ using PropertyChanged.SourceGenerator;
using TerminalUI.Color;
using TerminalUI.ConsoleDrivers;
using TerminalUI.Models;
using TerminalUI.TextFormat;
using TerminalUI.Traits;
namespace TerminalUI.Controls;
@@ -175,12 +176,12 @@ public abstract partial class View<TConcrete, T> : IView<T> where TConcrete : Vi
bool renderResult;
if (Background != null || Foreground != null)
{
var newRenderContext = new RenderContext(
renderContext.ConsoleDriver,
renderContext.ForceRerender,
Foreground ?? renderContext.Foreground,
Background ?? renderContext.Background,
renderContext.Statistics);
var newRenderContext = renderContext with
{
Foreground = Foreground ?? renderContext.Foreground,
Background = Background ?? renderContext.Background
};
renderResult = RenderMethod(newRenderContext, position, size);
}
else
@@ -202,7 +203,7 @@ public abstract partial class View<TConcrete, T> : IView<T> where TConcrete : Vi
protected void RenderEmpty(in RenderContext renderContext, Position position, Size size)
{
var driver = renderContext.ConsoleDriver;
driver.ResetColor();
driver.ResetStyle();
var placeHolder = new string(ApplicationContext!.EmptyCharacter, size.Width);
for (var i = 0; i < size.Height; i++)
@@ -309,14 +310,25 @@ public abstract partial class View<TConcrete, T> : IView<T> where TConcrete : Vi
driver.Write(contentString);
}
}
protected void SetColor(IConsoleDriver driver, IColor? foreground, IColor? background)
protected void SetStyleColor(
in RenderContext renderContext,
IColor? foreground = null,
IColor? background = null,
ITextFormat? textFormat = null)
{
var driver = renderContext.ConsoleDriver;
driver.ResetColor();
if (textFormat is { } t)
{
t.ApplyFormat(driver, renderContext.TextFormat);
}
if (foreground is not null)
{
driver.SetForegroundColor(foreground);
}
if(background is not null)
if (background is not null)
{
driver.SetBackgroundColor(background);
}
@@ -324,11 +336,9 @@ public abstract partial class View<TConcrete, T> : IView<T> where TConcrete : Vi
protected void SetColorsForDriver(in RenderContext renderContext)
{
var driver = renderContext.ConsoleDriver;
var foreground = Foreground ?? renderContext.Foreground;
var background = Background ?? renderContext.Background;
SetColor(driver, foreground, background);
SetStyleColor(renderContext, foreground, background);
}
public TChild CreateChild<TChild>() where TChild : IView<T>, new()