Skip to content

Commit 86db77d

Browse files
niels9001michael-hawkerArlodotexe
authored
[Gallery] Colorcoding for code snippets (#103)
* Adding colorcode support * WASM support * Removing dependencies * Making it work for WASM * Strip header comments (and namespace) from code examples * Use not_win instead of xamarin * Ran XAML styler --------- Co-authored-by: michael-hawker <[email protected]> Co-authored-by: Arlo Godfrey <[email protected]>
1 parent 097f7c5 commit 86db77d

File tree

2 files changed

+89
-20
lines changed

2 files changed

+89
-20
lines changed

CommunityToolkit.App.Shared/Renderers/ToolkitSampleRenderer.xaml

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
<!-- 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. -->
1+
<!-- 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. -->
22
<Page x:Class="CommunityToolkit.App.Shared.Renderers.ToolkitSampleRenderer"
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
66
xmlns:local="using:CommunityToolkit.App.Shared"
77
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
88
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
9+
xmlns:not_win="http://uno.ui/not_win"
910
xmlns:renderer="using:CommunityToolkit.App.Shared.Renderers"
1011
xmlns:wasm="http://uno.ui/wasm"
1112
xmlns:win="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
13+
ActualThemeChanged="ToolkitSampleRenderer_ActualThemeChanged"
1214
Loaded="ToolkitSampleRenderer_Loaded"
13-
mc:Ignorable="d wasm">
15+
mc:Ignorable="d wasm not_win">
1416

1517
<Grid CornerRadius="0">
1618
<Grid.RowDefinitions>
@@ -62,7 +64,6 @@
6264
<Setter Target="FixedOptionsBar.ColumnSpacing" Value="8" />
6365
<Setter Target="FixedOptionsBar.RowSpacing" Value="0" />
6466
<Setter Target="FixedOptionsBar.Margin" Value="12" />
65-
6667
</VisualState.Setters>
6768
</VisualState>
6869
</VisualStateGroup>
@@ -150,7 +151,6 @@
150151
Click="FlowDirectionBtn_OnClick"
151152
Style="{StaticResource SubtleButtonStyle}"
152153
ToolTipService.ToolTip="Toggle right-to-left">
153-
154154
<Button.Content>
155155
<FontIcon win:AutomationProperties.AccessibilityView="Raw"
156156
FontSize="16"
@@ -175,7 +175,6 @@
175175
</Button.Content>
176176
</Button>
177177
</Grid>
178-
179178
</Grid>
180179

181180
<Grid x:Name="ContentPageHolder"
@@ -217,25 +216,41 @@
217216
<Pivot x:Name="CodePivot"
218217
MaxHeight="400"
219218
Margin="16,0,16,0"
220-
HorizontalAlignment="Stretch">
219+
HorizontalAlignment="Stretch"
220+
HorizontalContentAlignment="Stretch">
221221
<Pivot.Resources>
222222
<x:Double x:Key="PivotHeaderItemFontSize">14</x:Double>
223223
</Pivot.Resources>
224224

225225
<PivotItem Header="XAML">
226226
<ScrollViewer>
227-
<TextBlock wasm:IsTextSelectionEnabled="True"
228-
win:IsTextSelectionEnabled="True"
229-
Style="{StaticResource CaptionTextBlockStyle}"
230-
Text="{x:Bind XamlCode, Mode=OneWay}" />
227+
<StackPanel Orientation="Vertical">
228+
<not_win:TextBlock x:Name="XAMLCodeRenderer"
229+
Margin="0,12,0,12"
230+
wasm:IsTextSelectionEnabled="True"
231+
FontFamily="Consolas"
232+
FontSize="12"
233+
TextWrapping="WrapWholeWords" />
234+
<win:RichTextBlock x:Name="XAMLCodeRenderer"
235+
FontFamily="Consolas"
236+
FontSize="12"
237+
IsTextSelectionEnabled="True" />
238+
</StackPanel>
231239
</ScrollViewer>
232240
</PivotItem>
233241
<PivotItem Header="C#">
234242
<ScrollViewer>
235-
<TextBlock wasm:IsTextSelectionEnabled="True"
236-
win:IsTextSelectionEnabled="True"
237-
Style="{StaticResource CaptionTextBlockStyle}"
238-
Text="{x:Bind CSharpCode, Mode=OneWay}" />
243+
<StackPanel Orientation="Vertical">
244+
<not_win:TextBlock x:Name="CSharpCodeRenderer"
245+
Margin="0,12,0,12"
246+
wasm:IsTextSelectionEnabled="True"
247+
FontFamily="Consolas"
248+
FontSize="12"
249+
TextWrapping="WrapWholeWords" />
250+
<win:RichTextBlock x:Name="CSharpCodeRenderer"
251+
wasm:IsTextSelectionEnabled="True"
252+
win:IsTextSelectionEnabled="True" />
253+
</StackPanel>
239254
</ScrollViewer>
240255
</PivotItem>
241256
</Pivot>

CommunityToolkit.App.Shared/Renderers/ToolkitSampleRenderer.xaml.cs

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
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.Tooling.SampleGen.Attributes;
5+
#if !HAS_UNO
6+
using ColorCode;
7+
#endif
68
using CommunityToolkit.Tooling.SampleGen.Metadata;
7-
using System.Runtime.InteropServices.WindowsRuntime;
8-
using Windows.Foundation.Collections;
99
using Windows.Storage;
1010

1111
#if WINAPPSDK
@@ -152,8 +152,31 @@ private async Task LoadData()
152152
return;
153153
}
154154

155-
XamlCode = await GetMetadataFileContents(Metadata, "xaml");
156-
CSharpCode = await GetMetadataFileContents(Metadata, "xaml.cs");
155+
XamlCode = (await GetMetadataFileContents(Metadata, "xaml"))?.Trim();
156+
CSharpCode = (await GetMetadataFileContents(Metadata, "xaml.cs"))?.Trim();
157+
158+
// Remove Header License Comments from code samples for space
159+
if (XamlCode?.StartsWith("<!-- Licensed") == true ||
160+
XamlCode?.StartsWith("<!-- Licensed") == true)
161+
{
162+
var lines = XamlCode.Split(Environment.NewLine).Skip(1);
163+
XamlCode = string.Join(Environment.NewLine, lines).Trim();
164+
}
165+
166+
if (CSharpCode?.StartsWith("// Licensed") == true)
167+
{
168+
var lines = CSharpCode.Split(Environment.NewLine).Skip(3);
169+
CSharpCode = string.Join(Environment.NewLine, lines).Trim();
170+
}
171+
172+
// Remove namespace line as not relevant to sample
173+
if (CSharpCode?.StartsWith("namespace") == true)
174+
{
175+
var lines = CSharpCode.Split(Environment.NewLine).Skip(1);
176+
CSharpCode = string.Join(Environment.NewLine, lines).Trim();
177+
}
178+
179+
RenderCode();
157180

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

@@ -287,7 +310,6 @@ private void ThemeBtn_OnClick(object sender, RoutedEventArgs e)
287310
{
288311
ThemeBG.Visibility = Visibility.Collapsed;
289312
}
290-
291313
}
292314

293315
private void FlowDirectionBtn_OnClick(object sender, RoutedEventArgs e)
@@ -308,4 +330,36 @@ private void CodeBtn_OnClick(object sender, RoutedEventArgs e)
308330
{
309331
SourcecodeExpander.IsExpanded = !SourcecodeExpander.IsExpanded;
310332
}
333+
334+
private void RenderCode()
335+
{
336+
// Uno doesn't support RichTextBlock, so we are using a normal TextBlock instead on WASM
337+
#if !HAS_UNO
338+
RichTextBlockFormatter codeFormatter = new RichTextBlockFormatter(ActualTheme);
339+
#endif
340+
if (XamlCode is not null)
341+
{
342+
#if HAS_UNO
343+
XAMLCodeRenderer.Text = XamlCode;
344+
#else
345+
XAMLCodeRenderer.Blocks?.Clear();
346+
codeFormatter.FormatRichTextBlock(XamlCode, Languages.FindById("xaml"), XAMLCodeRenderer);
347+
#endif
348+
}
349+
350+
if (CSharpCode is not null)
351+
{
352+
#if HAS_UNO
353+
CSharpCodeRenderer.Text = CSharpCode;
354+
#else
355+
CSharpCodeRenderer.Blocks?.Clear();
356+
codeFormatter.FormatRichTextBlock(CSharpCode, Languages.CSharp, CSharpCodeRenderer);
357+
#endif
358+
}
359+
}
360+
361+
private void ToolkitSampleRenderer_ActualThemeChanged(FrameworkElement sender, object args)
362+
{
363+
RenderCode();
364+
}
311365
}

0 commit comments

Comments
 (0)