Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion MarkdownView.slnx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Solution>
<Project Path="src/WinUI.Markdown/MarkdownView.csproj" />
<Project Path="samples/WinUI.Markdown.Sample/MarkdownViewTest.csproj">
<Platform Project="x64" />
<Deploy />
</Project>
<Project Path="tests/WinUI.Markdown.Tests/WinUI.Markdown.Tests.csproj" />
Expand Down
5 changes: 5 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"msbuild-sdks": {
"Uno.Sdk": "6.5.31"
}
}
45 changes: 28 additions & 17 deletions samples/WinUI.Markdown.Sample/MarkdownViewTest.csproj
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Uno.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net10.0-windows10.0.19041.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<TargetFrameworks>net10.0-desktop</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">$(TargetFrameworks);net10.0-windows10.0.19041.0</TargetFrameworks>
<OutputType>Exe</OutputType>
<RootNamespace>WinUI.Markdown.Sample</RootNamespace>
<RuntimeIdentifiers>win-x64;win-arm64;linux-x64;linux-arm64;osx-arm64;osx-x64</RuntimeIdentifiers>
<WinUISDKReferences>false</WinUISDKReferences>
<UnoSingleProject>true</UnoSingleProject>
<ApplicationTitle>WinUI.Markdown.Sample</ApplicationTitle>
<ApplicationId>com.aadhil-kh.winui-markdown-sample</ApplicationId>
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
<ApplicationVersion>1</ApplicationVersion>
<ApplicationPublisher>LeXtudio</ApplicationPublisher>
<UnoFeatures>SkiaRenderer;</UnoFeatures>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net10.0-windows10.0.19041.0'">
<UseWinUI>true</UseWinUI>
<EnableWindowsTargeting>true</EnableWindowsTargeting>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<WindowsPackageType>None</WindowsPackageType>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Platforms>x86;x64;ARM64</Platforms>
<Platform Condition="'$(Platform)' == ''">x64</Platform>
<RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == ''">win-x64</RuntimeIdentifier>
<RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
<PublishProfile Condition="'$(Platform)' != 'AnyCPU'">win-$(Platform).pubxml</PublishProfile>
<UseWinUI>true</UseWinUI>
<WinUISDKReferences>false</WinUISDKReferences>
<EnableMsixTooling>true</EnableMsixTooling>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
Expand All @@ -27,7 +37,7 @@
<Content Include="Assets\Wide310x150Logo.scale-200.png" />
</ItemGroup>

<ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net10.0-windows10.0.19041.0'">
<Manifest Include="$(ApplicationManifest)" />
</ItemGroup>

Expand All @@ -36,23 +46,24 @@
Tools extension to be activated for this project even if the Windows App SDK Nuget
package has not yet been restored.
-->
<ItemGroup Condition="'$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
<ItemGroup Condition="'$(TargetFramework)' == 'net10.0-windows10.0.19041.0' and '$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
<ProjectCapability Include="Msix" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\WinUI.Markdown\MarkdownView.csproj" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net10.0-windows10.0.19041.0'">
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.28000.1839" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="2.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\WinUI.Markdown\MarkdownView.csproj" />
<Compile Remove="Platforms\**" />
</ItemGroup>

<!--
Defining the "HasPackageAndPublishMenuAddedByProject" property here allows the Solution
Explorer "Package and Publish" context menu entry to be enabled for this project even if
the Windows App SDK Nuget package has not yet been restored.
-->
<PropertyGroup Condition="'$(DisableHasPackageAndPublishMenuAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
<PropertyGroup Condition="'$(TargetFramework)' == 'net10.0-windows10.0.19041.0' and '$(DisableHasPackageAndPublishMenuAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
<HasPackageAndPublishMenu>true</HasPackageAndPublishMenu>
</PropertyGroup>

Expand Down
20 changes: 20 additions & 0 deletions samples/WinUI.Markdown.Sample/Platforms/Desktop/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Uno.UI.Hosting;

namespace MarkdownViewTest;

internal static class Program
{
[STAThread]
public static void Main(string[] args)
{
var host = UnoPlatformHostBuilder.Create()
.App(() => new App())
.UseX11()
.UseLinuxFrameBuffer()
.UseMacOS()
.UseWin32()
.Build();

host.Run();
}
}
15 changes: 15 additions & 0 deletions src/WinUI.Markdown/Aliases.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// On Uno, bridge the Microsoft.UI.Xaml.Documents/Controls namespaces to our
// UnoRichText implementations via global using aliases.
// On Windows (WINDOWS_APP_SDK) the native WinUI types are used as-is.
#if !WINDOWS_APP_SDK
global using RichTextBlock = LeXtudio.UI.Xaml.Controls.RichTextBlock;
global using InlineCollection = LeXtudio.UI.Xaml.Documents.InlineCollection;
global using Paragraph = LeXtudio.UI.Xaml.Documents.Paragraph;
global using Run = LeXtudio.UI.Xaml.Documents.Run;
global using Bold = LeXtudio.UI.Xaml.Documents.Bold;
global using Italic = LeXtudio.UI.Xaml.Documents.Italic;
global using Span = LeXtudio.UI.Xaml.Documents.Span;
global using LineBreak = LeXtudio.UI.Xaml.Documents.LineBreak;
global using InlineUIContainer = LeXtudio.UI.Xaml.Documents.InlineUIContainer;
global using Hyperlink = LeXtudio.UI.Xaml.Documents.Hyperlink;
#endif
4 changes: 4 additions & 0 deletions src/WinUI.Markdown/Controls/MarkdownView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,13 @@ private IMarkdownRenderer EnsureRenderer(RenderMode effectiveRenderMode)

ResetRenderer();
_activeRenderMode = effectiveRenderMode;
#if WINDOWS_APP_SDK
_renderer = effectiveRenderMode == RenderMode.WebView2
? new WebView2Renderer()
: new NativeRenderer();
#else
_renderer = new NativeRenderer();
#endif
_renderer.LinkClicked += _linkClickedHandler;

PART_Root.Children.Clear();
Expand Down
24 changes: 19 additions & 5 deletions src/WinUI.Markdown/MarkdownView.csproj
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Uno.Sdk">
<PropertyGroup>
<TargetFramework>net10.0-windows10.0.19041.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<TargetFrameworks>net10.0-desktop</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">$(TargetFrameworks);net10.0-windows10.0.19041.0</TargetFrameworks>
<RootNamespace>WinUI.Markdown</RootNamespace>
<AssemblyName>WinUI.Markdown</AssemblyName>
<UseWinUI>true</UseWinUI>
<WinUISDKReferences>false</WinUISDKReferences>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<PackageId>WinUI.Markdown</PackageId>
<Description>A WinUI 3 MarkdownView control with native and WebView2 renderers.</Description>
<PackageSummary>WinUI 3 Markdown control with native rendering, WebView2 rendering, Auto fallback, and theme support.</PackageSummary>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net10.0-windows10.0.19041.0'">
<UseWinUI>true</UseWinUI>
<EnableWindowsTargeting>true</EnableWindowsTargeting>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<DefineConstants>$(DefineConstants);WINDOWS_APP_SDK</DefineConstants>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' != 'net10.0-windows10.0.19041.0'">
<PackageReference Include="LeXtudio.RichText" Version="0.1.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Markdig" Version="0.41.3" />
<PackageReference Include="ColorCode.Core" Version="2.0.15" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net10.0-windows10.0.19041.0'">
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.3719.77" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.28000.1839" PrivateAssets="All" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="2.0.1" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
Expand Down
10 changes: 10 additions & 0 deletions src/WinUI.Markdown/Renderers/MarkdownRenderPlanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ internal static class MarkdownRenderPlanner
{
public static MarkdownRenderPlan Resolve(RenderMode requestedMode, MarkdownDocument document, bool allowWebView2Fallback)
{
#if WINDOWS_APP_SDK
if (requestedMode is RenderMode.Native or RenderMode.WebView2)
{
return new MarkdownRenderPlan(requestedMode, string.Empty);
Expand All @@ -22,6 +23,15 @@ public static MarkdownRenderPlan Resolve(RenderMode requestedMode, MarkdownDocum
return allowWebView2Fallback
? new MarkdownRenderPlan(RenderMode.WebView2, fallbackReason)
: new MarkdownRenderPlan(RenderMode.Native, fallbackReason);
#else
if (requestedMode is RenderMode.Native or RenderMode.WebView2)
{
return new MarkdownRenderPlan(RenderMode.Native, string.Empty);
}

var fallbackReason = GetFallbackReason(document);
return new MarkdownRenderPlan(RenderMode.Native, fallbackReason);
#endif
}

private static string GetFallbackReason(MarkdownDocument document)
Expand Down
2 changes: 2 additions & 0 deletions src/WinUI.Markdown/Renderers/WebView2Renderer.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if WINDOWS_APP_SDK
using System.Net;
using Markdig;
using Microsoft.UI.Xaml;
Expand Down Expand Up @@ -78,3 +79,4 @@ private static string BuildHtmlShell(string body, MarkdownTheme theme)
""";
}
}
#endif
21 changes: 18 additions & 3 deletions src/WinUI.Markdown/Visitors/NativeMarkdownVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ private FrameworkElement RenderBlock(MarkdigBlock block, int nestingLevel)
};
}

#if WINDOWS_APP_SDK
private TextBlock RenderHeading(HeadingBlock heading)
{
var sizeIndex = Math.Clamp(heading.Level - 1, 0, _theme.HeadingSizes.Length - 1);
Expand All @@ -76,11 +77,25 @@ private TextBlock RenderHeading(HeadingBlock heading)
Margin = new Thickness(0, heading.Level == 1 ? 0 : _theme.HeadingSpacingBefore, 0, _theme.HeadingSpacingAfter)
};
}
#else
private RichTextBlock RenderHeading(HeadingBlock heading)
{
var sizeIndex = Math.Clamp(heading.Level - 1, 0, _theme.HeadingSizes.Length - 1);
var block = CreateRichTextBlock();
block.FontSize = _theme.HeadingSizes[sizeIndex];
block.FontWeight = HeadingWeight(sizeIndex);
block.Margin = new Thickness(0, heading.Level == 1 ? 0 : _theme.HeadingSpacingBefore, 0, _theme.HeadingSpacingAfter);

var paragraph = new Paragraph();
AppendInlines(paragraph.Inlines, heading.Inline);
block.Blocks.Add(paragraph);
return block;
}
#endif
private RichTextBlock RenderParagraph(ParagraphBlock paragraph, int skipLeadingCharacters = 0)
{
var block = CreateRichTextBlock();
var xamlParagraph = new Microsoft.UI.Xaml.Documents.Paragraph();
var xamlParagraph = new Paragraph();
AppendInlines(xamlParagraph.Inlines, paragraph.Inline, skipLeadingCharacters);
block.Blocks.Add(xamlParagraph);
return block;
Expand Down Expand Up @@ -145,7 +160,7 @@ private ScrollViewer RenderCodeBlock(CodeBlock code)
{
var text = code.Lines.ToString();
var block = CreateRichTextBlock();
var paragraph = new Microsoft.UI.Xaml.Documents.Paragraph();
var paragraph = new Paragraph();
_syntaxHighlighter.Append(paragraph.Inlines, text, code is FencedCodeBlock fenced ? fenced.Info : null);
block.Blocks.Add(paragraph);
block.FontFamily = _theme.CodeFont;
Expand Down Expand Up @@ -225,7 +240,7 @@ private Grid RenderTable(Table table, int nestingLevel)
private RichTextBlock RenderRawText(string text)
{
var block = CreateRichTextBlock();
block.Blocks.Add(new Microsoft.UI.Xaml.Documents.Paragraph { Inlines = { new Run { Text = text } } });
block.Blocks.Add(new Paragraph { Inlines = { new Run { Text = text } } });
return block;
}

Expand Down
30 changes: 24 additions & 6 deletions tests/WinUI.Markdown.Tests/WinUI.Markdown.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Uno.Sdk">
<PropertyGroup>
<TargetFramework>net10.0-windows10.0.19041.0</TargetFramework>
<TargetFrameworks>net10.0-desktop</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">$(TargetFrameworks);net10.0-windows10.0.19041.0</TargetFrameworks>
<OutputType>Exe</OutputType>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<UseWinUI>true</UseWinUI>
<WinUISDKReferences>false</WinUISDKReferences>
<UnoSingleProject>true</UnoSingleProject>
<UnoFeatures>SkiaRenderer;</UnoFeatures>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net10.0-windows10.0.19041.0'">
<UseWinUI>true</UseWinUI>
<EnableWindowsTargeting>true</EnableWindowsTargeting>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
</PropertyGroup>

<!-- Uno.SingleProject.WinAppSdk.targets sets OutputType=WinExe for IsUnoHead projects after static evaluation.
Restore it to Exe before xunit validation and compilation. -->
<Target Name="_RestoreOutputTypeForTests"
BeforeTargets="_XunitValidateBuild;BeforeBuild"
Condition="'$(TargetFramework)' == 'net10.0-windows10.0.19041.0'">
<PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>
</Target>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.0" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.28000.1839" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="2.0.1" />
<PackageReference Include="xunit.v3" Version="3.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net10.0-windows10.0.19041.0'">
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.28000.1839" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="2.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\WinUI.Markdown\MarkdownView.csproj" />
</ItemGroup>
Expand Down
19 changes: 19 additions & 0 deletions tests/WinUI.Markdown.Tests/app.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="MarkdownViewTest.app"/>

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- The ID below informs the system that this application is compatible with OS features first introduced in Windows 10.
It is necessary to support features in unpackaged applications, for example the custom titlebar implementation.
For more info see https://docs.microsoft.com/windows/apps/windows-app-sdk/use-windows-app-sdk-run-time#declare-os-compatibility-in-your-application-manifest -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>

<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
</windowsSettings>
</application>
</assembly>