This repository was archived by the owner on Feb 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathCodeRenderer.cs
More file actions
189 lines (158 loc) · 5.76 KB
/
CodeRenderer.cs
File metadata and controls
189 lines (158 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// 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.
using System;
using ColorCode;
using Microsoft.Toolkit.Uwp.Helpers;
using Windows.ApplicationModel.DataTransfer;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace Microsoft.Toolkit.Uwp.SampleApp.Controls
{
public class CodeRenderer : Control
{
private RichTextBlockFormatter _formatter;
private RichTextBlock _codeView;
private Button _printButton;
private Button _copyButton;
private PrintHelper _printHelper;
private Grid _container;
private string _displayedText;
private ILanguage _language;
private bool _rendered;
private ElementTheme _theme;
public CodeRenderer()
{
DefaultStyleKey = typeof(CodeRenderer);
_theme = SystemTheme();
SampleController.Current.ThemeChanged += Current_ThemeChanged;
}
/// <summary>
/// Renders the Code with Syntax Highlighting of the provided Language Alias.
/// </summary>
/// <param name="code">Code to Render</param>
/// <param name="language">Language Alias</param>
public void SetCode(string code, string language)
{
_rendered = false;
_language = Languages.FindById(language);
_displayedText = code;
if (_codeView != null)
{
RenderDocument();
}
}
protected override void OnApplyTemplate()
{
if (_printButton != null)
{
_printButton.Click -= PrintButton_Click;
}
if (_copyButton != null)
{
_copyButton.Click -= CopyButton_Click;
}
_codeView = GetTemplateChild("codeView") as RichTextBlock;
_printButton = GetTemplateChild("PrintButton") as Button;
_copyButton = GetTemplateChild("CopyButton") as Button;
_container = GetTemplateChild("Container") as Grid;
if (_printButton != null)
{
_printButton.Click += PrintButton_Click;
}
if (_copyButton != null)
{
_copyButton.Click += CopyButton_Click;
}
if (!_rendered)
{
RenderDocument();
}
base.OnApplyTemplate();
}
private void RenderDocument()
{
if (_codeView != null)
{
_codeView.Blocks?.Clear();
_formatter = new RichTextBlockFormatter(_theme);
_formatter.FormatRichTextBlock(_displayedText, _language, _codeView);
_rendered = true;
}
}
private void CopyButton_Click(object sender, RoutedEventArgs e)
{
TrackingManager.TrackEvent("Copy", _displayedText);
var content = new DataPackage();
content.SetText(_displayedText);
Clipboard.SetContent(content);
}
private async void PrintButton_Click(object sender, RoutedEventArgs e)
{
SampleController.Current.DisplayWaitRing = true;
var printBlock = new RichTextBlock
{
FontFamily = _codeView.FontFamily,
RequestedTheme = ElementTheme.Light
};
var printFormatter = new RichTextBlockFormatter(ElementTheme.Light);
printFormatter.FormatRichTextBlock(_displayedText, _language, printBlock);
_printHelper = new PrintHelper(_container);
_printHelper.AddFrameworkElementToPrint(printBlock);
_printHelper.OnPrintFailed += PrintHelper_OnPrintFailed;
_printHelper.OnPrintSucceeded += PrintHelper_OnPrintSucceeded;
_printHelper.OnPrintCanceled += PrintHelper_OnPrintCanceled;
await _printHelper.ShowPrintUIAsync("Windows Community Toolkit Sample App");
}
private void ReleasePrintHelper()
{
_printHelper.OnPrintFailed -= PrintHelper_OnPrintFailed;
_printHelper.OnPrintSucceeded -= PrintHelper_OnPrintSucceeded;
_printHelper.OnPrintCanceled -= PrintHelper_OnPrintCanceled;
_printHelper.Dispose();
SampleController.Current.DisplayWaitRing = false;
}
private async void PrintHelper_OnPrintSucceeded()
{
ReleasePrintHelper();
var dialog = new MessageDialog("Printing done.");
await dialog.ShowAsync();
}
private async void PrintHelper_OnPrintFailed()
{
ReleasePrintHelper();
var dialog = new MessageDialog("Printing failed.");
await dialog.ShowAsync();
}
private void PrintHelper_OnPrintCanceled()
{
ReleasePrintHelper();
}
private void Current_ThemeChanged(object sender, Models.ThemeChangedArgs e)
{
// Only Handle System Theme Changes.
if (e.CustomSet)
{
return;
}
_theme = SystemTheme();
try
{
_rendered = false;
RenderDocument();
}
catch
{
}
}
/// <summary>
/// Casts Application Theme to Element Theme, as Formatter accepts ElementTheme.
/// </summary>
/// <returns>Element Theme</returns>
private ElementTheme SystemTheme()
{
return SampleController.Current.GetSystemTheme() == ApplicationTheme.Dark ? ElementTheme.Dark : ElementTheme.Light;
}
}
}