Skip to content

Commit 186f3d8

Browse files
authored
Merge pull request #615 from jcoc611-microsoft/user/juosori/Markdown_Tables_Style
MarkdownTextBlock: Tables style improvements
2 parents 519358d + dc7bf03 commit 186f3d8

File tree

8 files changed

+51
-19
lines changed

8 files changed

+51
-19
lines changed

components/MarkdownTextBlock/src/Extensions.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -710,13 +710,13 @@ public static Size GetMarkdownImageSize(LinkInline link)
710710
return new(0, 0);
711711
}
712712

713-
public static SolidColorBrush GetAccentColorBrush()
713+
public static SolidColorBrush GetAccentColorBrush(UIColorType colorType = UIColorType.Accent)
714714
{
715715
// Create a UISettings object to get the accent color
716716
var uiSettings = new UISettings();
717717

718718
// Get the accent color as a Color value
719-
var accentColor = uiSettings.GetColorValue(UIColorType.Accent);
719+
var accentColor = uiSettings.GetColorValue(colorType);
720720

721721
// Create a SolidColorBrush from the accent color
722722
var accentBrush = new SolidColorBrush(accentColor);

components/MarkdownTextBlock/src/MarkdownTextBlock.xaml.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ private void ApplyText(bool rerender)
8888

8989
if (!string.IsNullOrEmpty(Text))
9090
{
91-
this.MarkdownDocument = Markdown.Parse(Text, _pipeline);
92-
_renderer.Render(this.MarkdownDocument);
91+
var parsedMarkdown = Markdown.Parse(Text, _pipeline);
92+
this.MarkdownDocument = parsedMarkdown;
93+
_renderer.Render(parsedMarkdown);
9394
}
9495
}
9596
}

components/MarkdownTextBlock/src/MarkdownThemes.cs

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public sealed class MarkdownThemes : DependencyObject
5656
public Thickness H5Margin { get; set; } = new(left: 0, top: 8, right: 0, bottom: 0);
5757
public Thickness H6Margin { get; set; } = new(left: 0, top: 8, right: 0, bottom: 0);
5858

59+
public Brush BorderBrush { get; set; } = new SolidColorBrush(Colors.Gray);
60+
61+
public Brush TableHeadingBackground { get; set; } = Extensions.GetAccentColorBrush(Windows.UI.ViewManagement.UIColorType.AccentLight3);
62+
5963
public Brush InlineCodeBackground { get; set; } = (Brush)Application.Current.Resources["ExpanderHeaderBackground"];
6064
public Brush InlineCodeForeground { get; set; } = (Brush)Application.Current.Resources["TextFillColorPrimaryBrush"];
6165

components/MarkdownTextBlock/src/Renderers/ObjectRenderers/Extensions/TableRenderer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ protected override void Write(WinUIRenderer renderer, Table table)
1414
if (renderer == null) throw new ArgumentNullException(nameof(renderer));
1515
if (table == null) throw new ArgumentNullException(nameof(table));
1616

17-
var myTable = new MyTable(table);
17+
var myTable = new MyTable(table, renderer.Config.Themes);
1818

1919
renderer.Push(myTable);
2020

components/MarkdownTextBlock/src/Renderers/ObjectRenderers/ThematicBreakRenderer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ protected override void Write(WinUIRenderer renderer, ThematicBreakBlock obj)
1414
if (renderer == null) throw new ArgumentNullException(nameof(renderer));
1515
if (obj == null) throw new ArgumentNullException(nameof(obj));
1616

17-
var thematicBreak = new MyThematicBreak(obj);
17+
var thematicBreak = new MyThematicBreak(obj, renderer.Config.Themes);
1818

1919
renderer.WriteBlock(thematicBreak);
2020
}

components/MarkdownTextBlock/src/TextElements/MyTable.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using CommunityToolkit.WinUI.Controls.MarkdownTextBlockRns;
56
using Markdig.Extensions.Tables;
67

78
namespace CommunityToolkit.Labs.WinUI.MarkdownTextBlock.TextElements;
@@ -17,7 +18,7 @@ public TextElement TextElement
1718
get => _paragraph;
1819
}
1920

20-
public MyTable(Table table)
21+
public MyTable(Table table, MarkdownThemes themes)
2122
{
2223
_table = table;
2324
_paragraph = new Paragraph();
@@ -28,8 +29,10 @@ public MyTable(Table table)
2829
(
2930
column,
3031
table.Count,
31-
1,
32-
new SolidColorBrush(Colors.Gray)
32+
borderThickness: 1,
33+
themes.BorderBrush,
34+
themes.TableHeadingBackground,
35+
themes.CornerRadius
3336
);
3437

3538
var inlineUIContainer = new InlineUIContainer();

components/MarkdownTextBlock/src/TextElements/MyTableUIElement.cs

+31-8
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,44 @@ namespace CommunityToolkit.Labs.WinUI.MarkdownTextBlock.TextElements;
66

77
internal partial class MyTableUIElement : Panel
88
{
9+
// Children[0] = Border
10+
// Children[1] = Header Background
11+
// Children[2..columnCount] = Vertical lines
12+
// Children[columnCount+1..columnCount + rowCount - 1] = Horizontal lines
13+
// Children[columnCount + rowCount..] = Content
14+
915
private readonly int _columnCount;
1016
private readonly int _rowCount;
1117
private readonly double _borderThickness;
1218
private double[]? _columnWidths;
1319
private double[]? _rowHeights;
1420

15-
public MyTableUIElement(int columnCount, int rowCount, double borderThickness, Brush borderBrush)
21+
public MyTableUIElement(int columnCount, int rowCount, double borderThickness, Brush borderBrush, Brush headingBrush, CornerRadius cornerRadius)
1622
{
1723
_columnCount = columnCount;
1824
_rowCount = rowCount;
1925
_borderThickness = borderThickness;
20-
for (int col = 0; col < columnCount + 1; col++)
26+
27+
Margin = new Thickness(left: 0, top: 10, right: 0, bottom: 10);
28+
29+
Children.Add(new Border
30+
{
31+
Background = headingBrush,
32+
CornerRadius = new CornerRadius(topLeft: cornerRadius.TopLeft, topRight: cornerRadius.TopRight, 0, 0)
33+
});
34+
Children.Add(new Border
35+
{
36+
BorderThickness = new Thickness(_borderThickness),
37+
CornerRadius = cornerRadius,
38+
BorderBrush = borderBrush
39+
});
40+
41+
for (int col = 1; col < columnCount; col++)
2142
{
2243
Children.Add(new Rectangle { Fill = borderBrush });
2344
}
2445

25-
for (int row = 0; row < rowCount + 1; row++)
46+
for (int row = 1; row < rowCount; row++)
2647
{
2748
Children.Add(new Rectangle { Fill = borderBrush });
2849
}
@@ -33,7 +54,7 @@ private IEnumerable<FrameworkElement> ContentChildren
3354
{
3455
get
3556
{
36-
for (int i = _columnCount + _rowCount + 2; i < Children.Count; i++)
57+
for (int i = _columnCount + _rowCount; i < Children.Count; i++)
3758
{
3859
yield return (FrameworkElement)Children[i];
3960
}
@@ -45,7 +66,7 @@ private IEnumerable<Rectangle> VerticalLines
4566
{
4667
get
4768
{
48-
for (int i = 0; i < _columnCount + 1; i++)
69+
for (int i = 2; i < _columnCount + 1; i++)
4970
{
5071
yield return (Rectangle)Children[i];
5172
}
@@ -57,7 +78,7 @@ private IEnumerable<Rectangle> HorizontalLines
5778
{
5879
get
5980
{
60-
for (int i = _columnCount + 1; i < _columnCount + _rowCount + 2; i++)
81+
for (int i = _columnCount + 1; i < _columnCount + _rowCount; i++)
6182
{
6283
yield return (Rectangle)Children[i];
6384
}
@@ -167,30 +188,32 @@ protected override Size ArrangeOverride(Size finalSize)
167188
double x = 0;
168189
foreach (var borderLine in VerticalLines)
169190
{
191+
x += _borderThickness + _columnWidths[colIndex];
170192
borderLine.Arrange(new Rect(x, 0, _borderThickness, finalSize.Height));
171193
if (colIndex >= _columnWidths.Length)
172194
{
173195
break;
174196
}
175197

176-
x += _borderThickness + _columnWidths[colIndex];
177198
colIndex++;
178199
}
179200
}
180201

181202
// Arrange horizontal border elements.
182203
{
204+
Children[0].Arrange(new Rect(0, 0, finalSize.Width, _rowHeights[0] + (_borderThickness * 2)));
205+
Children[1].Arrange(new Rect(0, 0, finalSize.Width, finalSize.Height));
183206
int rowIndex = 0;
184207
double y = 0;
185208
foreach (var borderLine in HorizontalLines)
186209
{
210+
y += _borderThickness + _rowHeights[rowIndex];
187211
borderLine.Arrange(new Rect(0, y, finalSize.Width, _borderThickness));
188212
if (rowIndex >= _rowHeights.Length)
189213
{
190214
break;
191215
}
192216

193-
y += _borderThickness + _rowHeights[rowIndex];
194217
rowIndex++;
195218
}
196219
}

components/MarkdownTextBlock/src/TextElements/MyThematicBreak.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using CommunityToolkit.WinUI.Controls.MarkdownTextBlockRns;
56
using Markdig.Syntax;
67

78
namespace CommunityToolkit.Labs.WinUI.MarkdownTextBlock.TextElements;
@@ -16,7 +17,7 @@ public TextElement TextElement
1617
get => _paragraph;
1718
}
1819

19-
public MyThematicBreak(ThematicBreakBlock thematicBreakBlock)
20+
public MyThematicBreak(ThematicBreakBlock thematicBreakBlock, MarkdownThemes themes)
2021
{
2122
_thematicBreakBlock = thematicBreakBlock;
2223
_paragraph = new Paragraph();
@@ -25,7 +26,7 @@ public MyThematicBreak(ThematicBreakBlock thematicBreakBlock)
2526
Line line = new Line
2627
{
2728
Stretch = Stretch.Fill,
28-
Stroke = new SolidColorBrush(Colors.Gray),
29+
Stroke = themes.BorderBrush,
2930
X2 = 1,
3031
Margin = new Thickness(0, 12, 0, 12)
3132
};

0 commit comments

Comments
 (0)