Skip to content

[Gallery] Colorcoding for code snippets #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 30, 2023
Merged
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
43 changes: 29 additions & 14 deletions CommunityToolkit.App.Shared/Renderers/ToolkitSampleRenderer.xaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. -->
<Page x:Class="CommunityToolkit.App.Shared.Renderers.ToolkitSampleRenderer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:CommunityToolkit.App.Shared"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
xmlns:not_win="http://uno.ui/not_win"
xmlns:renderer="using:CommunityToolkit.App.Shared.Renderers"
xmlns:wasm="http://uno.ui/wasm"
xmlns:win="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
ActualThemeChanged="ToolkitSampleRenderer_ActualThemeChanged"
Loaded="ToolkitSampleRenderer_Loaded"
mc:Ignorable="d wasm">
mc:Ignorable="d wasm not_win">

<Grid CornerRadius="0">
<Grid.RowDefinitions>
Expand Down Expand Up @@ -62,7 +64,6 @@
<Setter Target="FixedOptionsBar.ColumnSpacing" Value="8" />
<Setter Target="FixedOptionsBar.RowSpacing" Value="0" />
<Setter Target="FixedOptionsBar.Margin" Value="12" />

</VisualState.Setters>
</VisualState>
</VisualStateGroup>
Expand Down Expand Up @@ -150,7 +151,6 @@
Click="FlowDirectionBtn_OnClick"
Style="{StaticResource SubtleButtonStyle}"
ToolTipService.ToolTip="Toggle right-to-left">

<Button.Content>
<FontIcon win:AutomationProperties.AccessibilityView="Raw"
FontSize="16"
Expand All @@ -175,7 +175,6 @@
</Button.Content>
</Button>
</Grid>

</Grid>

<Grid x:Name="ContentPageHolder"
Expand Down Expand Up @@ -217,25 +216,41 @@
<Pivot x:Name="CodePivot"
MaxHeight="400"
Margin="16,0,16,0"
HorizontalAlignment="Stretch">
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch">
<Pivot.Resources>
<x:Double x:Key="PivotHeaderItemFontSize">14</x:Double>
</Pivot.Resources>

<PivotItem Header="XAML">
<ScrollViewer>
<TextBlock wasm:IsTextSelectionEnabled="True"
win:IsTextSelectionEnabled="True"
Style="{StaticResource CaptionTextBlockStyle}"
Text="{x:Bind XamlCode, Mode=OneWay}" />
<StackPanel Orientation="Vertical">
<not_win:TextBlock x:Name="XAMLCodeRenderer"
Margin="0,12,0,12"
wasm:IsTextSelectionEnabled="True"
FontFamily="Consolas"
FontSize="12"
TextWrapping="WrapWholeWords" />
<win:RichTextBlock x:Name="XAMLCodeRenderer"
FontFamily="Consolas"
FontSize="12"
IsTextSelectionEnabled="True" />
</StackPanel>
</ScrollViewer>
</PivotItem>
<PivotItem Header="C#">
<ScrollViewer>
<TextBlock wasm:IsTextSelectionEnabled="True"
win:IsTextSelectionEnabled="True"
Style="{StaticResource CaptionTextBlockStyle}"
Text="{x:Bind CSharpCode, Mode=OneWay}" />
<StackPanel Orientation="Vertical">
<not_win:TextBlock x:Name="CSharpCodeRenderer"
Margin="0,12,0,12"
wasm:IsTextSelectionEnabled="True"
FontFamily="Consolas"
FontSize="12"
TextWrapping="WrapWholeWords" />
<win:RichTextBlock x:Name="CSharpCodeRenderer"
wasm:IsTextSelectionEnabled="True"
win:IsTextSelectionEnabled="True" />
</StackPanel>
</ScrollViewer>
</PivotItem>
</Pivot>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// 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.Tooling.SampleGen.Attributes;
#if !HAS_UNO
using ColorCode;
#endif
using CommunityToolkit.Tooling.SampleGen.Metadata;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation.Collections;
using Windows.Storage;

#if WINAPPSDK
Expand Down Expand Up @@ -152,8 +152,31 @@ private async Task LoadData()
return;
}

XamlCode = await GetMetadataFileContents(Metadata, "xaml");
CSharpCode = await GetMetadataFileContents(Metadata, "xaml.cs");
XamlCode = (await GetMetadataFileContents(Metadata, "xaml"))?.Trim();
CSharpCode = (await GetMetadataFileContents(Metadata, "xaml.cs"))?.Trim();

// Remove Header License Comments from code samples for space
if (XamlCode?.StartsWith("<!-- Licensed") == true ||
XamlCode?.StartsWith("<!-- Licensed") == true)
{
var lines = XamlCode.Split(Environment.NewLine).Skip(1);
XamlCode = string.Join(Environment.NewLine, lines).Trim();
}

if (CSharpCode?.StartsWith("// Licensed") == true)
{
var lines = CSharpCode.Split(Environment.NewLine).Skip(3);
CSharpCode = string.Join(Environment.NewLine, lines).Trim();
}

// Remove namespace line as not relevant to sample
if (CSharpCode?.StartsWith("namespace") == true)
{
var lines = CSharpCode.Split(Environment.NewLine).Skip(1);
CSharpCode = string.Join(Environment.NewLine, lines).Trim();
}

RenderCode();

var sampleControlInstance = (UIElement)Metadata.SampleControlFactory();

Expand Down Expand Up @@ -287,7 +310,6 @@ private void ThemeBtn_OnClick(object sender, RoutedEventArgs e)
{
ThemeBG.Visibility = Visibility.Collapsed;
}

}

private void FlowDirectionBtn_OnClick(object sender, RoutedEventArgs e)
Expand All @@ -308,4 +330,36 @@ private void CodeBtn_OnClick(object sender, RoutedEventArgs e)
{
SourcecodeExpander.IsExpanded = !SourcecodeExpander.IsExpanded;
}

private void RenderCode()
{
// Uno doesn't support RichTextBlock, so we are using a normal TextBlock instead on WASM
#if !HAS_UNO
RichTextBlockFormatter codeFormatter = new RichTextBlockFormatter(ActualTheme);
#endif
if (XamlCode is not null)
{
#if HAS_UNO
XAMLCodeRenderer.Text = XamlCode;
#else
XAMLCodeRenderer.Blocks?.Clear();
codeFormatter.FormatRichTextBlock(XamlCode, Languages.FindById("xaml"), XAMLCodeRenderer);
#endif
}

if (CSharpCode is not null)
{
#if HAS_UNO
CSharpCodeRenderer.Text = CSharpCode;
#else
CSharpCodeRenderer.Blocks?.Clear();
codeFormatter.FormatRichTextBlock(CSharpCode, Languages.CSharp, CSharpCodeRenderer);
#endif
}
}

private void ToolkitSampleRenderer_ActualThemeChanged(FrameworkElement sender, object args)
{
RenderCode();
}
}