Skip to content

Commit de5a3a9

Browse files
committed
Adding more styling properties
1 parent 8509121 commit de5a3a9

File tree

16 files changed

+234
-25
lines changed

16 files changed

+234
-25
lines changed

components/MarkdownTextBlock/samples/MarkdownTextBlockExampleSample.xaml.cs

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
using CommunityToolkit.WinUI.Controls;
66
using Microsoft.UI;
7+
using Microsoft.UI.Text;
78
using System.Diagnostics;
9+
using Windows.UI;
810

911
namespace MarkdownTextBlockExperiment.Samples;
1012

@@ -604,7 +606,102 @@ public MarkdownTextBlockExampleSample()
604606

605607
var themes = new MarkdownThemes
606608
{
607-
609+
// Headings
610+
H1FontSize = 28,
611+
H2FontSize = 24,
612+
H3FontSize = 20,
613+
H4FontSize = 18,
614+
H5FontSize = 16,
615+
H6FontSize = 14,
616+
617+
H1FontWeight = FontWeights.Bold,
618+
H2FontWeight = FontWeights.SemiBold,
619+
H3FontWeight = FontWeights.Medium,
620+
H4FontWeight = FontWeights.Normal,
621+
H5FontWeight = FontWeights.Normal,
622+
H6FontWeight = FontWeights.Normal,
623+
624+
H1Foreground = new SolidColorBrush(Colors.Crimson),
625+
H2Foreground = new SolidColorBrush(Colors.DarkOrange),
626+
H3Foreground = new SolidColorBrush(Colors.Goldenrod),
627+
H4Foreground = new SolidColorBrush(Colors.ForestGreen),
628+
H5Foreground = new SolidColorBrush(Colors.SteelBlue),
629+
H6Foreground = new SolidColorBrush(Colors.MediumPurple),
630+
631+
H1Margin = new Thickness(0, 20, 0, 4),
632+
H2Margin = new Thickness(0, 18, 0, 4),
633+
H3Margin = new Thickness(0, 16, 0, 4),
634+
H4Margin = new Thickness(0, 14, 0, 4),
635+
H5Margin = new Thickness(0, 12, 0, 2),
636+
H6Margin = new Thickness(0, 10, 0, 2),
637+
638+
// General container
639+
Padding = new Thickness(12),
640+
InternalMargin = new Thickness(6),
641+
CornerRadius = new CornerRadius(6),
642+
643+
// Paragraph / lists
644+
ParagraphMargin = new Thickness(0, 10, 0, 10),
645+
ParagraphLineHeight = 22,
646+
ListGutterWidth = 28,
647+
ListBulletSpacing = 6,
648+
ListMargin = new Thickness(0, 6, 0, 6),
649+
650+
// Horizontal rule
651+
HorizontalRuleBrush = new SolidColorBrush(Colors.Gray),
652+
HorizontalRuleThickness = 2,
653+
HorizontalRuleMargin = new Thickness(0, 18, 0, 18),
654+
655+
// Links
656+
LinkForeground = new SolidColorBrush(Colors.DodgerBlue),
657+
658+
// Inline code
659+
InlineCodeBackground = new SolidColorBrush(Color.FromArgb(255, 40, 44, 52)),
660+
InlineCodeForeground = new SolidColorBrush(Colors.White),
661+
InlineCodeBorderBrush = new SolidColorBrush(Color.FromArgb(255, 70, 75, 85)),
662+
InlineCodeBorderThickness = new Thickness(1),
663+
InlineCodeCornerRadius = new CornerRadius(3),
664+
InlineCodePadding = new Thickness(4, 0, 4, 0),
665+
InlineCodeFontSize = 13,
666+
InlineCodeFontWeight = FontWeights.SemiBold,
667+
668+
// Code blocks
669+
CodeBlockBackground = new SolidColorBrush(Color.FromArgb(255, 30, 34, 40)),
670+
CodeBlockForeground = new SolidColorBrush(Colors.Gainsboro),
671+
CodeBlockBorderBrush = new SolidColorBrush(Color.FromArgb(255, 60, 65, 75)),
672+
CodeBlockBorderThickness = new Thickness(1),
673+
CodeBlockPadding = new Thickness(12),
674+
CodeBlockMargin = new Thickness(0, 14, 0, 16),
675+
CodeBlockFontFamily = new FontFamily("Consolas"),
676+
CodeBlockCornerRadius = new CornerRadius(8),
677+
678+
// Quotes
679+
QuoteBackground = new SolidColorBrush(Color.FromArgb(30, 255, 200, 0)),
680+
QuoteBorderBrush = new SolidColorBrush(Colors.Orange),
681+
QuoteBorderThickness = new Thickness(5, 0, 0, 0),
682+
QuoteForeground = new SolidColorBrush(Colors.DarkSlateGray),
683+
QuoteMargin = new Thickness(0, 10, 0, 10),
684+
QuotePadding = new Thickness(10),
685+
QuoteCornerRadius = new CornerRadius(6),
686+
687+
// Images
688+
ImageMaxWidth = 420,
689+
ImageMaxHeight = 260,
690+
ImageStretch = Stretch.UniformToFill,
691+
692+
// Tables
693+
TableBorderBrush = new SolidColorBrush(Colors.DimGray),
694+
TableBorderThickness = 1,
695+
TableCellPadding = new Thickness(8, 4, 8, 4),
696+
TableMargin = new Thickness(0, 18, 0, 18),
697+
TableHeadingBackground = new SolidColorBrush(Color.FromArgb(255, 50, 70, 95)),
698+
699+
// Other / border defaults
700+
BorderBrush = new SolidColorBrush(Colors.DarkGray),
701+
702+
// (YAML placeholders if ever used)
703+
YamlBorderBrush = new SolidColorBrush(Colors.DarkGoldenrod),
704+
YamlBorderThickness = new Thickness(1)
608705
};
609706
_config = new MarkdownConfig { Themes = themes };
610707
_text = _markdown;

components/MarkdownTextBlock/src/HtmlWriter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public static void WriteHtml(WinUIRenderer renderer, HtmlNodeCollection nodes)
4343
else
4444
{
4545
var myHyperlink = new MyHyperlink(node, renderer.Config.BaseUrl);
46+
myHyperlink.TextElement.Foreground = renderer.Config.Themes.LinkForeground;
4647
myHyperlink.ClickEvent += (sender, e) =>
4748
{
4849
renderer.MarkdownTextBlock.RaiseLinkClickedEvent(sender.NavigateUri);

components/MarkdownTextBlock/src/MarkdownTextBlock.Properties.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,23 @@ public partial class MarkdownTextBlock
108108
typeof(MarkdownTextBlock),
109109
new PropertyMetadata(null));
110110

111+
/// <summary>
112+
/// Identifies the <see cref="IsTextSelectionEnabled"/> dependency property.
113+
/// </summary>
114+
private static readonly DependencyProperty IsTextSelectionEnabledProperty = DependencyProperty.Register(
115+
nameof(IsTextSelectionEnabled),
116+
typeof(bool),
117+
typeof(MarkdownTextBlock),
118+
new PropertyMetadata(false, OnIsTextSelectionEnabledChanged));
119+
120+
private static void OnIsTextSelectionEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
121+
{
122+
if (d is MarkdownTextBlock mtb && mtb._document != null)
123+
{
124+
mtb._document.RichTextBlock.IsTextSelectionEnabled = (bool)e.NewValue;
125+
}
126+
}
127+
111128
public MarkdownConfig Config
112129
{
113130
get => (MarkdownConfig)GetValue(ConfigProperty);
@@ -203,4 +220,13 @@ public MarkdownDocument? MarkdownDocument
203220
get => (MarkdownDocument)GetValue(MarkdownDocumentProperty);
204221
private set => SetValue(MarkdownDocumentProperty, value);
205222
}
223+
224+
/// <summary>
225+
/// Gets or sets a value indicating whether text selection is enabled.
226+
/// </summary>
227+
public bool IsTextSelectionEnabled
228+
{
229+
get => (bool)GetValue(IsTextSelectionEnabledProperty);
230+
set => SetValue(IsTextSelectionEnabledProperty, value);
231+
}
206232
}

components/MarkdownTextBlock/src/MarkdownThemes.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,54 @@ public sealed class MarkdownThemes : DependencyObject
7878
public double InlineCodeFontSize { get; set; } = 10;
7979

8080
public FontWeight InlineCodeFontWeight { get; set; } = FontWeights.Normal;
81+
82+
// Legacy parity properties (new)
83+
// Code block styling
84+
public Brush CodeBlockBackground { get; set; } = (Brush)Application.Current.Resources["ExpanderHeaderBackground"];
85+
public Brush CodeBlockBorderBrush { get; set; } = new SolidColorBrush(Colors.Gray);
86+
public Thickness CodeBlockBorderThickness { get; set; } = new Thickness(1);
87+
public Thickness CodeBlockPadding { get; set; } = new Thickness(8);
88+
public Thickness CodeBlockMargin { get; set; } = new Thickness(0, 8, 0, 8);
89+
public FontFamily CodeBlockFontFamily { get; set; } = new FontFamily("Consolas");
90+
public Brush CodeBlockForeground { get; set; } = (Brush)Application.Current.Resources["TextFillColorPrimaryBrush"];
91+
public CornerRadius CodeBlockCornerRadius { get; set; } = new CornerRadius(4);
92+
93+
// Horizontal rule
94+
public Brush HorizontalRuleBrush { get; set; } = new SolidColorBrush(Colors.Gray);
95+
public double HorizontalRuleThickness { get; set; } = 1.0;
96+
public Thickness HorizontalRuleMargin { get; set; } = new Thickness(0, 12, 0, 12);
97+
98+
// Link styling
99+
public Brush LinkForeground { get; set; } = (Brush)Application.Current.Resources["AccentTextFillColorPrimaryBrush"] ?? new SolidColorBrush(Colors.DodgerBlue);
100+
101+
// Paragraph / list
102+
public Thickness ParagraphMargin { get; set; } = new Thickness(0, 8, 0, 8);
103+
public double ParagraphLineHeight { get; set; } = 0; // 0 = default
104+
public double ListBulletSpacing { get; set; } = 4; // spaces after bullet
105+
public double ListGutterWidth { get; set; } = 30; // indent delta per level
106+
public Thickness ListMargin { get; set; } = new Thickness(0, 4, 0, 4);
107+
108+
// Quote styling
109+
public Brush QuoteBackground { get; set; } = new SolidColorBrush(Colors.Transparent);
110+
public Brush QuoteBorderBrush { get; set; } = new SolidColorBrush(Colors.Gray);
111+
public Thickness QuoteBorderThickness { get; set; } = new Thickness(4, 0, 0, 0);
112+
public Brush QuoteForeground { get; set; } = (Brush)Application.Current.Resources["TextFillColorPrimaryBrush"];
113+
public Thickness QuoteMargin { get; set; } = new Thickness(0, 4, 0, 4);
114+
public Thickness QuotePadding { get; set; } = new Thickness(4);
115+
public CornerRadius QuoteCornerRadius { get; set; } = new CornerRadius(4);
116+
117+
// Image styling
118+
public double ImageMaxWidth { get; set; } = 0; // 0 = no constraint
119+
public double ImageMaxHeight { get; set; } = 0;
120+
public Stretch ImageStretch { get; set; } = Stretch.Uniform;
121+
122+
// Table styling
123+
public Brush TableBorderBrush { get; set; } = new SolidColorBrush(Colors.Gray);
124+
public double TableBorderThickness { get; set; } = 1;
125+
public Thickness TableCellPadding { get; set; } = new Thickness(4);
126+
public Thickness TableMargin { get; set; } = new Thickness(0, 10, 0, 10);
127+
128+
// YAML / not currently used - placeholders for parity
129+
public Brush YamlBorderBrush { get; set; } = new SolidColorBrush(Colors.Gray);
130+
public Thickness YamlBorderThickness { get; set; } = new Thickness(1);
81131
}

components/MarkdownTextBlock/src/Renderers/ObjectRenderers/Inlines/LinkInlineRenderer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@ protected override void Write(WinUIRenderer renderer, LinkInline link)
3535
{
3636
renderer.MarkdownTextBlock.RaiseLinkClickedEvent(((HyperlinkButton)sender).NavigateUri);
3737
};
38+
// Apply link foreground to nested RichTextBlock content
39+
// (Handled in MyHyperlinkButton initialization via MarkdownConfig.Default for now)
3840
renderer.Push(myHyperlinkButton);
3941
}
4042
else
4143
{
4244
var hyperlink = new MyHyperlink(link, renderer.Config.BaseUrl);
45+
hyperlink.TextElement.Foreground = renderer.Config.Themes.LinkForeground;
4346
hyperlink.ClickEvent += (sender, e) =>
4447
{
4548
renderer.MarkdownTextBlock.RaiseLinkClickedEvent(sender.NavigateUri);

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

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

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

1919
renderer.Push(quote);
2020
renderer.WriteChildren(obj);

components/MarkdownTextBlock/src/TextElements/MyCodeBlock.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ public MyCodeBlock(CodeBlock codeBlock, MarkdownConfig config)
2424
_paragraph = new Paragraph();
2525
var container = new InlineUIContainer();
2626
var border = new Border();
27-
border.Background = (Brush)Application.Current.Resources["ExpanderHeaderBackground"];
28-
border.Padding = _config.Themes.Padding;
29-
border.Margin = _config.Themes.InternalMargin;
30-
border.CornerRadius = _config.Themes.CornerRadius;
27+
border.Background = _config.Themes.CodeBlockBackground;
28+
border.BorderBrush = _config.Themes.CodeBlockBorderBrush;
29+
border.BorderThickness = _config.Themes.CodeBlockBorderThickness;
30+
border.Padding = _config.Themes.CodeBlockPadding;
31+
border.Margin = _config.Themes.CodeBlockMargin;
32+
border.CornerRadius = _config.Themes.CodeBlockCornerRadius;
3133
var richTextBlock = new RichTextBlock();
34+
richTextBlock.FontFamily = _config.Themes.CodeBlockFontFamily;
35+
richTextBlock.Foreground = _config.Themes.CodeBlockForeground;
3236

3337
#if false
3438
if (codeBlock is FencedCodeBlock fencedCodeBlock)

components/MarkdownTextBlock/src/TextElements/MyHyperlink.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public MyHyperlink(LinkInline linkInline, string? baseUrl)
4141
_hyperlink = new Hyperlink()
4242
{
4343
NavigateUri = Extensions.GetUri(url, baseUrl),
44+
Foreground = MarkdownConfig.Default.Themes.LinkForeground
4445
};
4546
}
4647

@@ -52,6 +53,7 @@ public MyHyperlink(HtmlNode htmlNode, string? baseUrl)
5253
_hyperlink = new Hyperlink()
5354
{
5455
NavigateUri = Extensions.GetUri(url, baseUrl),
56+
Foreground = MarkdownConfig.Default.Themes.LinkForeground
5557
};
5658
}
5759

components/MarkdownTextBlock/src/TextElements/MyHyperlinkButton.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ private MyHyperlinkButton(string? url, string? baseUrl, HtmlNode? htmlNode, Link
6565
_flowDoc = new MyFlowDocument(_linkInline!);
6666
}
6767
_inlineUIContainer.Child = _hyperLinkButton;
68-
_hyperLinkButton.Content = _flowDoc.RichTextBlock;
68+
_flowDoc.RichTextBlock.Foreground = MarkdownConfig.Default.Themes.LinkForeground;
69+
_hyperLinkButton.Content = _flowDoc.RichTextBlock;
6970
}
7071

7172
public void AddChild(IAddChild child)

components/MarkdownTextBlock/src/TextElements/MyImage.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,18 @@ private async void LoadImage(object sender, RoutedEventArgs e)
150150
{
151151
_image.Height = _precedentHeight;
152152
}
153+
154+
// Apply theme constraints if provided
155+
var themes = MarkdownConfig.Default.Themes;
156+
if (themes.ImageMaxWidth > 0)
157+
{
158+
_image.MaxWidth = themes.ImageMaxWidth;
159+
}
160+
if (themes.ImageMaxHeight > 0)
161+
{
162+
_image.MaxHeight = themes.ImageMaxHeight;
163+
}
164+
_image.Stretch = themes.ImageStretch;
153165
}
154166
catch (Exception) { }
155167
}

0 commit comments

Comments
 (0)