From 828e89d414f15e55bb929261f147a2e5c33edde7 Mon Sep 17 00:00:00 2001 From: Niels Laute Date: Tue, 24 Feb 2026 15:21:06 +0100 Subject: [PATCH 1/6] Using DPs --- .../MarkdownTextBlockCustomThemeSample.xaml | 32 +- ...MarkdownTextBlockCustomThemeSample.xaml.cs | 34 +- .../MarkdownTextBlockCustomThemeSampleBase.cs | 65 +- .../MarkdownTextBlockExampleSample.xaml | 1 - .../MarkdownTextBlockExampleSample.xaml.cs | 8 - .../samples/ThemeOptionsPane.xaml | 3 - .../samples/ThemeOptionsPane.xaml.cs | 8 - .../MarkdownTextBlock/src/HtmlWriter.cs | 8 +- .../MarkdownTextBlock/src/MarkdownConfig.cs | 15 - .../src/MarkdownTextBlock.Properties.cs | 656 +++++++++++++++++- .../src/MarkdownTextBlock.xaml | 229 +++++- .../src/MarkdownTextBlock.xaml.cs | 95 ++- .../MarkdownTextBlock/src/MarkdownThemes.cs | 133 ---- .../ObjectRenderers/CodeBlockRenderer.cs | 2 +- .../Extensions/TableRenderer.cs | 6 +- .../ObjectRenderers/HeadingRenderer.cs | 2 +- .../Inlines/AutoLinkInlineRenderer.cs | 2 +- .../Inlines/CodeInlineRenderer.cs | 2 +- .../Inlines/EmphasisInlineRenderer.cs | 2 +- .../Inlines/LinkInlineRenderer.cs | 6 +- .../ObjectRenderers/QuoteBlockRenderer.cs | 2 +- .../ObjectRenderers/ThematicBreakRenderer.cs | 2 +- .../src/Renderers/WinUIRenderer.cs | 9 +- .../src/TextElements/MyAutolinkInline.cs | 9 +- .../src/TextElements/MyCodeBlock.cs | 23 +- .../src/TextElements/MyFlowDocument.cs | 12 +- .../src/TextElements/MyHeading.cs | 59 +- .../src/TextElements/MyHyperlink.cs | 23 +- .../src/TextElements/MyHyperlinkButton.cs | 17 +- .../src/TextElements/MyImage.cs | 33 +- .../src/TextElements/MyInlineCode.cs | 23 +- .../src/TextElements/MyParagraph.cs | 14 +- .../src/TextElements/MyQuote.cs | 23 +- .../src/TextElements/MyTable.cs | 12 +- .../src/TextElements/MyTableCell.cs | 5 +- .../src/TextElements/MyThematicBreak.cs | 10 +- .../tests/ImageProviderConstraintTest.cs | 45 +- 37 files changed, 1141 insertions(+), 489 deletions(-) delete mode 100644 components/MarkdownTextBlock/src/MarkdownConfig.cs delete mode 100644 components/MarkdownTextBlock/src/MarkdownThemes.cs diff --git a/components/MarkdownTextBlock/samples/MarkdownTextBlockCustomThemeSample.xaml b/components/MarkdownTextBlock/samples/MarkdownTextBlockCustomThemeSample.xaml index 326987686..e5455d3a8 100644 --- a/components/MarkdownTextBlock/samples/MarkdownTextBlockCustomThemeSample.xaml +++ b/components/MarkdownTextBlock/samples/MarkdownTextBlockCustomThemeSample.xaml @@ -11,7 +11,37 @@ diff --git a/components/MarkdownTextBlock/samples/MarkdownTextBlockCustomThemeSample.xaml.cs b/components/MarkdownTextBlock/samples/MarkdownTextBlockCustomThemeSample.xaml.cs index 127efbee0..eb83d7024 100644 --- a/components/MarkdownTextBlock/samples/MarkdownTextBlockCustomThemeSample.xaml.cs +++ b/components/MarkdownTextBlock/samples/MarkdownTextBlockCustomThemeSample.xaml.cs @@ -2,8 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using CommunityToolkit.WinUI.Controls; - namespace MarkdownTextBlockExperiment.Samples; /// @@ -12,8 +10,6 @@ namespace MarkdownTextBlockExperiment.Samples; [ToolkitSample(id: nameof(MarkdownTextBlockCustomThemeSample), "Custom Theme", description: "A sample showcasing custom theming options with live editing for headings, code blocks, quotes, tables, and more.")] public sealed partial class MarkdownTextBlockCustomThemeSample : MarkdownTextBlockCustomThemeSampleBase { - public MarkdownConfig MarkdownConfig { get; private set; } - public string MarkdownText { get; } = @" # Custom Theme Demo @@ -96,7 +92,7 @@ Try adjusting the **Bullet Spacing** and **Gutter Width** settings to see how li - Adjust the theme settings in the options panel - The gutter width controls how much each level is indented - The bullet spacing controls space after the bullet character -- Click **Apply Changes** to see updates +- Changes apply live via bindings - Use **Reset to Defaults** to start over Numbered lists work too: @@ -109,20 +105,24 @@ 1. Deep nesting works 3. Third item "; + // Converter methods for x:Bind function bindings (must be on the x:Class type, not the base) + public Brush GetHeadingBrush(int index) => HeadingColors[Math.Clamp(index, 0, HeadingColors.Length - 1)]; + public Brush GetInlineCodeBrush(int index) => InlineCodeColors[Math.Clamp(index, 0, InlineCodeColors.Length - 1)]; + public Brush GetCodeBackgroundBrush(int index) => CodeBackgroundColors[Math.Clamp(index, 0, CodeBackgroundColors.Length - 1)]; + public Brush GetCodeBorderBrush(int index) => CodeBorderColors[Math.Clamp(index, 0, CodeBorderColors.Length - 1)]; + public Brush GetQuoteBrush(int index) => QuoteColors[Math.Clamp(index, 0, QuoteColors.Length - 1)]; + public FontFamily GetCodeFont(int index) => CodeFonts[Math.Clamp(index, 0, CodeFonts.Length - 1)]; + public Stretch GetImageStretch(int index) => ImageStretchOptions[Math.Clamp(index, 0, ImageStretchOptions.Length - 1)]; + public Windows.UI.Text.FontWeight GetBoldFontWeight(int index) => BoldFontWeights[Math.Clamp(index, 0, BoldFontWeights.Length - 1)]; + public Thickness GetUniformThickness(double value) => new Thickness(value); + public CornerRadius GetUniformCornerRadius(double value) => new CornerRadius(value); + public Thickness GetHorizontalPadding(double value) => new Thickness(value, value / 2, value, value / 2); + public Thickness GetLeftBorderThickness(double value) => new Thickness(value, 0, 0, 0); + public CornerRadius GetRightCornerRadius(double value) => new CornerRadius(0, value, value, 0); + public Thickness GetVerticalMargin(double value) => new Thickness(0, value, 0, value); + public MarkdownTextBlockCustomThemeSample() { - MarkdownConfig = new MarkdownConfig { Themes = CreateThemes() }; this.InitializeComponent(); } - - public override void ApplyTheme() - { - MarkdownConfig = new MarkdownConfig { Themes = CreateThemes() }; - - // Force re-render by toggling text - MarkdownTextBlock.Config = MarkdownConfig; - var text = MarkdownTextBlock.Text; - MarkdownTextBlock.Text = ""; - MarkdownTextBlock.Text = text; - } } diff --git a/components/MarkdownTextBlock/samples/MarkdownTextBlockCustomThemeSampleBase.cs b/components/MarkdownTextBlock/samples/MarkdownTextBlockCustomThemeSampleBase.cs index c1c49e5d2..4768e7a90 100644 --- a/components/MarkdownTextBlock/samples/MarkdownTextBlockCustomThemeSampleBase.cs +++ b/components/MarkdownTextBlock/samples/MarkdownTextBlockCustomThemeSampleBase.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using CommunityToolkit.WinUI.Controls; using Microsoft.UI; using Windows.UI; @@ -14,7 +13,7 @@ namespace MarkdownTextBlockExperiment.Samples; -public abstract partial class MarkdownTextBlockCustomThemeSampleBase : Page +public partial class MarkdownTextBlockCustomThemeSampleBase : Page { // Dependency properties for theme customization public static readonly DependencyProperty H1FontSizeProperty = @@ -372,67 +371,7 @@ public double ListGutterWidth FontWeights.ExtraBold }; - public MarkdownThemes CreateThemes() - { - return new MarkdownThemes - { - H1FontSize = H1FontSize, - H1FontWeight = FontWeights.Bold, - H1Foreground = HeadingColors[H1ColorIndex], - H1Margin = new Thickness(0, 20, 0, 10), - - H2FontSize = H2FontSize, - H2FontWeight = FontWeights.SemiBold, - H2Foreground = new SolidColorBrush(Colors.MediumSlateBlue), - - H3FontSize = H3FontSize, - H3FontWeight = FontWeights.SemiBold, - H3Foreground = new SolidColorBrush(Colors.MediumPurple), - - InlineCodeFontSize = InlineCodeFontSize, - InlineCodePadding = new Thickness(InlineCodePadding, InlineCodePadding / 2, InlineCodePadding, InlineCodePadding / 2), - InlineCodeCornerRadius = new CornerRadius(InlineCodeCornerRadius), - InlineCodeBorderThickness = new Thickness(InlineCodeBorderThickness), - InlineCodeForeground = InlineCodeColors[InlineCodeColorIndex], - InlineCodeBackground = CodeBackgroundColors[InlineCodeBackgroundColorIndex], - InlineCodeBorderBrush = CodeBorderColors[InlineCodeBorderColorIndex], - - CodeBlockPadding = new Thickness(CodeBlockPadding), - CodeBlockCornerRadius = new CornerRadius(CodeBlockCornerRadius), - CodeBlockBorderThickness = new Thickness(CodeBlockBorderThickness), - CodeBlockFontFamily = CodeFonts[CodeBlockFontIndex], - CodeBlockBackground = CodeBackgroundColors[CodeBlockBackgroundColorIndex], - CodeBlockForeground = new SolidColorBrush(Colors.LightGreen), - CodeBlockBorderBrush = CodeBorderColors[CodeBlockBorderColorIndex], - - QuoteBorderThickness = new Thickness(QuoteBorderWidth, 0, 0, 0), - QuotePadding = new Thickness(QuotePadding, QuotePadding / 2, QuotePadding, QuotePadding / 2), - QuoteCornerRadius = new CornerRadius(0, QuoteCornerRadius, QuoteCornerRadius, 0), - QuoteBorderBrush = QuoteColors[QuoteColorIndex], - QuoteBackground = new SolidColorBrush(Color.FromArgb(20, 100, 149, 237)), - QuoteForeground = new SolidColorBrush(Colors.CornflowerBlue), - - TableCellPadding = new Thickness(TableCellPadding, TableCellPadding / 2, TableCellPadding, TableCellPadding / 2), - TableBorderThickness = TableBorderThickness, - TableBorderBrush = new SolidColorBrush(Colors.SlateGray), - TableHeadingBackground = new SolidColorBrush(Color.FromArgb(40, 100, 149, 237)), - HorizontalRuleThickness = HorizontalRuleThickness, - HorizontalRuleMargin = new Thickness(0, HorizontalRuleMargin, 0, HorizontalRuleMargin), - HorizontalRuleBrush = new SolidColorBrush(Colors.MediumSlateBlue), - - LinkForeground = new SolidColorBrush(Colors.DeepSkyBlue), - - ImageMaxWidth = ImageMaxWidth, - ImageMaxHeight = ImageMaxHeight, - ImageStretch = ImageStretchOptions[ImageStretchIndex], - - BoldFontWeight = BoldFontWeights[BoldFontWeightIndex], - - ListBulletSpacing = ListBulletSpacing, - ListGutterWidth = ListGutterWidth, - }; - } public void ResetToDefaults() { @@ -476,6 +415,4 @@ public void ResetToDefaults() ListBulletSpacing = 4; ListGutterWidth = 30; } - - public abstract void ApplyTheme(); } diff --git a/components/MarkdownTextBlock/samples/MarkdownTextBlockExampleSample.xaml b/components/MarkdownTextBlock/samples/MarkdownTextBlockExampleSample.xaml index 043e52c46..ad89fdf6f 100644 --- a/components/MarkdownTextBlock/samples/MarkdownTextBlockExampleSample.xaml +++ b/components/MarkdownTextBlock/samples/MarkdownTextBlockExampleSample.xaml @@ -11,7 +11,6 @@ _config; - set => _config = value; - } - public string Text { get => _text; @@ -600,7 +593,6 @@ public string Text public MarkdownTextBlockExampleSample() { this.InitializeComponent(); - _config = new MarkdownConfig(); _text = _markdown; MarkdownTextBlock.OnLinkClicked += MarkdownTextBlock_OnLinkClicked; } diff --git a/components/MarkdownTextBlock/samples/ThemeOptionsPane.xaml b/components/MarkdownTextBlock/samples/ThemeOptionsPane.xaml index 4946ef686..99f2f1c44 100644 --- a/components/MarkdownTextBlock/samples/ThemeOptionsPane.xaml +++ b/components/MarkdownTextBlock/samples/ThemeOptionsPane.xaml @@ -15,9 +15,6 @@ Spacing="8">