Console improvements, info providers
This commit is contained in:
@@ -5,38 +5,40 @@ public static class Color256Colors
|
||||
public static class Backgrounds
|
||||
{
|
||||
public static readonly Color256 Black = new(0, ColorType.Background);
|
||||
public static readonly Color256 Blue = new(9, ColorType.Background);
|
||||
public static readonly Color256 Cyan = new(11, ColorType.Background);
|
||||
public static readonly Color256 DarkBlue = new(1, ColorType.Background);
|
||||
public static readonly Color256 DarkCyan = new(3, ColorType.Background);
|
||||
public static readonly Color256 Blue = new(12, ColorType.Background);
|
||||
public static readonly Color256 Cyan = new(14, ColorType.Background);
|
||||
public static readonly Color256 DarkBlue = new(4, ColorType.Background);
|
||||
public static readonly Color256 DarkCyan = new(6, ColorType.Background);
|
||||
public static readonly Color256 DarkGray = new(8, ColorType.Background);
|
||||
public static readonly Color256 DarkGreen = new(2, ColorType.Background);
|
||||
public static readonly Color256 DarkMagenta = new(5, ColorType.Background);
|
||||
public static readonly Color256 DarkRed = new(4, ColorType.Background);
|
||||
public static readonly Color256 DarkYellow = new(6, ColorType.Background);
|
||||
public static readonly Color256 Gray = new(7, ColorType.Background);
|
||||
public static readonly Color256 DarkRed = new(1, ColorType.Background);
|
||||
public static readonly Color256 DarkYellow = new(3, ColorType.Background);
|
||||
public static readonly Color256 Gray = new(15, ColorType.Background);
|
||||
public static readonly Color256 Green = new(10, ColorType.Background);
|
||||
public static readonly Color256 Magenta = new(13, ColorType.Background);
|
||||
public static readonly Color256 Red = new(12, ColorType.Background);
|
||||
public static readonly Color256 White = new(15, ColorType.Background);
|
||||
public static readonly Color256 Red = new(9, ColorType.Background);
|
||||
public static readonly Color256 White = new(7, ColorType.Background);
|
||||
public static readonly Color256 Yellow = new(11, ColorType.Background);
|
||||
}
|
||||
public static class Foregrounds
|
||||
{
|
||||
public static readonly Color256 Black = new(0, ColorType.Foreground);
|
||||
public static readonly Color256 Blue = new(9, ColorType.Foreground);
|
||||
public static readonly Color256 Cyan = new(11, ColorType.Foreground);
|
||||
public static readonly Color256 DarkBlue = new(1, ColorType.Foreground);
|
||||
public static readonly Color256 DarkCyan = new(3, ColorType.Foreground);
|
||||
public static readonly Color256 Blue = new(12, ColorType.Foreground);
|
||||
public static readonly Color256 Cyan = new(14, ColorType.Foreground);
|
||||
public static readonly Color256 DarkBlue = new(4, ColorType.Foreground);
|
||||
public static readonly Color256 DarkCyan = new(6, ColorType.Foreground);
|
||||
public static readonly Color256 DarkGray = new(8, ColorType.Foreground);
|
||||
public static readonly Color256 DarkGreen = new(2, ColorType.Foreground);
|
||||
public static readonly Color256 DarkMagenta = new(5, ColorType.Foreground);
|
||||
public static readonly Color256 DarkRed = new(4, ColorType.Foreground);
|
||||
public static readonly Color256 DarkYellow = new(6, ColorType.Foreground);
|
||||
public static readonly Color256 Gray = new(7, ColorType.Foreground);
|
||||
public static readonly Color256 DarkRed = new(1, ColorType.Foreground);
|
||||
public static readonly Color256 DarkYellow = new(3, ColorType.Foreground);
|
||||
public static readonly Color256 Gray = new(15, ColorType.Foreground);
|
||||
public static readonly Color256 Green = new(10, ColorType.Foreground);
|
||||
public static readonly Color256 Magenta = new(13, ColorType.Foreground);
|
||||
public static readonly Color256 Red = new(12, ColorType.Foreground);
|
||||
public static readonly Color256 White = new(15, ColorType.Foreground);
|
||||
public static readonly Color256 Red = new(9, ColorType.Foreground);
|
||||
public static readonly Color256 White = new(7, ColorType.Foreground);
|
||||
public static readonly Color256 Yellow = new(11, ColorType.Background);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TerminalUI.Extensions;
|
||||
using TerminalUI.Models;
|
||||
using TerminalUI.ViewExtensions;
|
||||
@@ -7,6 +8,8 @@ namespace TerminalUI.Controls;
|
||||
|
||||
public class Grid<T> : ChildContainerView<T>
|
||||
{
|
||||
private ILogger<Grid<T>>? Logger => ApplicationContext?.LoggerFactory?.CreateLogger<Grid<T>>();
|
||||
|
||||
private delegate void WithSizes(Span<int> widths, Span<int> heights);
|
||||
|
||||
private delegate TResult WithSizes<TResult>(Span<int> widths, Span<int> heights);
|
||||
@@ -91,6 +94,18 @@ public class Grid<T> : ChildContainerView<T>
|
||||
var x = positionExtension?.Column ?? 0;
|
||||
var y = positionExtension?.Row ?? 0;
|
||||
|
||||
if (x > columnWidths.Length)
|
||||
{
|
||||
Logger?.LogWarning("Child {Child} is out of bounds, x: {X}, y: {Y}", child, x, y);
|
||||
x = 0;
|
||||
}
|
||||
|
||||
if (y > rowHeights.Length)
|
||||
{
|
||||
Logger?.LogWarning("Child {Child} is out of bounds, x: {X}, y: {Y}", child, x, y);
|
||||
y = 0;
|
||||
}
|
||||
|
||||
var width = columnWidths[x];
|
||||
var height = rowHeights[y];
|
||||
|
||||
|
||||
@@ -126,13 +126,103 @@ public partial class ListView<TDataContext, TItem> : View<TDataContext>
|
||||
if (_listViewItems is null || _listViewItemLength == 0)
|
||||
return new Size(0, 0);
|
||||
|
||||
var itemSize = _listViewItems[0].GetRequestedSize();
|
||||
_requestedItemSize = itemSize;
|
||||
return itemSize with {Height = itemSize.Height * _listViewItemLength};
|
||||
if (Orientation == Orientation.Vertical)
|
||||
{
|
||||
var itemSize = _listViewItems[0].GetRequestedSize();
|
||||
_requestedItemSize = itemSize;
|
||||
return itemSize with {Height = itemSize.Height * _listViewItemLength};
|
||||
}
|
||||
else
|
||||
{
|
||||
var width = 0;
|
||||
var height = 0;
|
||||
for (var i = 0; i < _listViewItemLength; i++)
|
||||
{
|
||||
var item = _listViewItems[i];
|
||||
width += item.GetRequestedSize().Width;
|
||||
height = Math.Max(height, item.GetRequestedSize().Height);
|
||||
}
|
||||
|
||||
return new Size(width, height);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void DefaultRenderer(Position position, Size size)
|
||||
{
|
||||
if (Orientation == Orientation.Vertical)
|
||||
RenderVertical(position, size);
|
||||
else
|
||||
RenderHorizontal(position, size);
|
||||
}
|
||||
|
||||
private void RenderHorizontal(Position position, Size size)
|
||||
{
|
||||
//Note: no support for same width elements
|
||||
var listViewItems = InstantiateItemViews();
|
||||
if (listViewItems.Length == 0) return;
|
||||
|
||||
Span<Size> requestedSizes = stackalloc Size[_listViewItemLength];
|
||||
|
||||
var totalRequestedWidth = 0;
|
||||
Span<int> widthSumUpToIndex = stackalloc int[_listViewItemLength];
|
||||
for (var i = 0; i < listViewItems.Length; i++)
|
||||
{
|
||||
widthSumUpToIndex[i] = totalRequestedWidth;
|
||||
var item = listViewItems[i];
|
||||
var requestedItemSize = item.GetRequestedSize();
|
||||
totalRequestedWidth += requestedItemSize.Width;
|
||||
requestedSizes[i] = requestedItemSize;
|
||||
}
|
||||
|
||||
var renderStartIndex = _renderStartIndex;
|
||||
var lastItemIndex = _listViewItemLength;
|
||||
|
||||
if (totalRequestedWidth > size.Width)
|
||||
{
|
||||
//Moving the render "window" to the right
|
||||
//Until the selected item's end is in it
|
||||
//So when RenderStartPosition (ie all the widths up to RenderStartIndex) + size.Width >= SelectedItemEnd
|
||||
var selectedIndexEnd = widthSumUpToIndex[SelectedIndex] + requestedSizes[SelectedIndex].Width;
|
||||
var startXOfRenderStartItem = widthSumUpToIndex[renderStartIndex];
|
||||
|
||||
while (selectedIndexEnd > startXOfRenderStartItem + size.Width)
|
||||
{
|
||||
startXOfRenderStartItem += requestedSizes[renderStartIndex].Width;
|
||||
renderStartIndex++;
|
||||
}
|
||||
|
||||
//Moving the render "window" to the left
|
||||
//Until the selected item's start is in it
|
||||
//So when RenderStartPosition (ie all the widths up to RenderStartIndex) <= SelectedItemStart
|
||||
var selectedIndexStart = widthSumUpToIndex[SelectedIndex];
|
||||
startXOfRenderStartItem = widthSumUpToIndex[renderStartIndex];
|
||||
while (selectedIndexStart < startXOfRenderStartItem)
|
||||
{
|
||||
renderStartIndex--;
|
||||
startXOfRenderStartItem -= requestedSizes[renderStartIndex].Width;
|
||||
}
|
||||
}
|
||||
|
||||
var deltaX = 0;
|
||||
for (var i = renderStartIndex; i < _listViewItemLength; i++)
|
||||
{
|
||||
var item = listViewItems[i];
|
||||
var requestedItemSize = requestedSizes[i];
|
||||
var width = requestedItemSize.Width;
|
||||
var nextDeltaX = deltaX + requestedItemSize.Width;
|
||||
if (nextDeltaX > size.Width)
|
||||
{
|
||||
width = size.Width - deltaX;
|
||||
}
|
||||
|
||||
item.Render(position with {X = position.X + deltaX}, size with {Width = width});
|
||||
deltaX = nextDeltaX;
|
||||
}
|
||||
}
|
||||
|
||||
private void RenderVertical(Position position, Size size)
|
||||
{
|
||||
//Note: only same height is supported
|
||||
var requestedItemSize = _requestedItemSize;
|
||||
if (requestedItemSize.Height == 0 || requestedItemSize.Width == 0)
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user