static DefaultStyleSheet() { styles = new ColorCode.Styling.StyleDictionary { new ColorCode.Styling.Style(ColorCode.Common.ScopeName.PlainText) { Foreground = "black", Background = "white", ReferenceName = "plainText" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.HtmlServerSideScript) { Background = "yellow", ReferenceName = "htmlServerSideScript" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.HtmlComment) { Foreground = "green", ReferenceName = "htmlComment" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.HtmlTagDelimiter) { Foreground = "blue", ReferenceName = "htmlTagDelimiter" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.HtmlElementName) { Foreground = "dullred", ReferenceName = "htmlElementName" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.HtmlAttributeName) { Foreground = "red", ReferenceName = "htmlAttributeName" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.HtmlAttributeValue) { Foreground = "blue", ReferenceName = "htmlAttributeValue" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.HtmlOperator) { Foreground = "blue", ReferenceName = "htmlOperator" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.Comment) { Foreground = "green", ReferenceName = "comment" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.XmlDocTag) { Foreground = "gray", ReferenceName = "xmlDocTag" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.XmlDocComment) { Foreground = "green", ReferenceName = "xmlDocComment" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.String) { Foreground = "dullred", ReferenceName = "string" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.StringCSharpVerbatim) { Foreground = "dullred", ReferenceName = "stringCSharpVerbatim" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.Keyword) { Foreground = "blue", ReferenceName = "keyword" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.PreprocessorKeyword) { Foreground = "blue", ReferenceName = "preprocessorKeyword" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.HtmlEntity) { Foreground = "red", ReferenceName = "htmlEntity" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.XmlAttribute) { Foreground = "red", ReferenceName = "xmlAttribute" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.XmlAttributeQuotes) { Foreground = "black", ReferenceName = "xmlAttributeQuotes" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.XmlAttributeValue) { Foreground = "blue", ReferenceName = "xmlAttributeValue" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.XmlCDataSection) { Foreground = "gray", ReferenceName = "xmlCDataSection" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.XmlComment) { Foreground = "green", ReferenceName = "xmlComment" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.XmlDelimiter) { Foreground = "blue", ReferenceName = "xmlDelimiter" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.XmlName) { Foreground = "dullred", ReferenceName = "xmlName" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.ClassName) { Foreground = "mediumturquoise", ReferenceName = "className" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.CssSelector) { Foreground = "dullred", ReferenceName = "cssSelector" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.CssPropertyName) { Foreground = "red", ReferenceName = "cssPropertyName" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.CssPropertyValue) { Foreground = "blue", ReferenceName = "cssPropertyValue" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.SqlSystemFunction) { Foreground = "magenta", ReferenceName = "sqlSystemFunction" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.PowerShellAttribute) { Foreground = "powderblue", ReferenceName = "powershellAttribute" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.PowerShellOperator) { Foreground = "gray", ReferenceName = "powershellOperator" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.PowerShellType) { Foreground = "teal", ReferenceName = "powershellType" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.PowerShellVariable) { Foreground = "orangered", ReferenceName = "powershellVariable" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.Type) { Foreground = "teal", ReferenceName = "type" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.TypeVariable) { Foreground = "teal", Italic = true, ReferenceName = "typeVariable" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.NameSpace) { Foreground = "navy", ReferenceName = "namespace" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.Constructor) { Foreground = "purple", ReferenceName = "constructor" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.Predefined) { Foreground = "navy", ReferenceName = "predefined" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.PseudoKeyword) { Foreground = "navy", ReferenceName = "pseudoKeyword" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.StringEscape) { Foreground = "gray", ReferenceName = "stringEscape" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.ControlKeyword) { Foreground = "blue", ReferenceName = "controlKeyword" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.Number) { ReferenceName = "number" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.Operator) { ReferenceName = "operator" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.Delimiter) { ReferenceName = "delimiter" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.MarkdownHeader) { // Foreground = "Blue, Bold = true, ReferenceName = "markdownHeader" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.MarkdownCode) { Foreground = "teal", ReferenceName = "markdownCode" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.MarkdownListItem) { Bold = true, ReferenceName = "markdownListItem" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.MarkdownEmph) { Italic = true, ReferenceName = "italic" }, new ColorCode.Styling.Style(ColorCode.Common.ScopeName.MarkdownBold) { Bold = true, ReferenceName = "bold" }, }; }
/// <summary> /// Start the preview on the Control. /// </summary> /// <param name="dataSource">Path to the file.</param> public override void DoPreview<T>(T dataSource) { _infoBarDisplayed = false; try { if (!(dataSource is string filePath)) { throw new ArgumentException($"{nameof(dataSource)} for {nameof(CodeFilePreviewHandler)} must be a string but was a '{typeof(T)}'"); } string ext = Path.GetExtension(filePath); string fileText = File.ReadAllText(filePath); string codeFileHTML = string.Empty; if (ext.Equals(".xml", StringComparison.OrdinalIgnoreCase)) { codeFileHTML = fileText; } else { StyleDic theme = StyleDic.DefaultLight; string background = "{background:#FFFFFF;}"; string baseColor = ControlzEx.Theming.WindowsThemeHelper.GetWindowsBaseColor(); if (baseColor == "Dark") { theme = StyleDic.DefaultDark; background = "{background:#202020;}"; } string header = string.Format(System.Globalization.CultureInfo.CurrentCulture, htmlHeader, background); HtmlFormatter formatter = new HtmlFormatter(theme); codeFileHTML = formatter.GetHtmlString(fileText, GetLanguageByExtension(ext)); codeFileHTML = $"{header}{codeFileHTML}{htmlFooter}"; } /* not work, why? the result of "codeFileHTML" is right. render fail. string lang = GetLangByExtension(ext); string js = File.ReadAllText(@"C:\res\shjs\sh_main.min.js"); string jsLang = File.ReadAllText($"C:\\res\\shjs\\lang\\sh_{lang}.min.js"); string css = File.ReadAllText(@"C:\res\shjs\css\default.css"); string header = $"<!doctype html><html><head><style>{css}</style><script>{js}{jsLang}</script></head><body onload=\"sh_highlightDocument();\">"; string footer = "</body></html>"; string codeFileHTML = $"{header}<pre class=\"sh_{lang}\">{HttpUtility.HtmlEncode(fileText)}</pre>{footer}"; */ InvokeOnControlThread(() => { _browser = new WebBrowserExt { DocumentText = codeFileHTML, Dock = DockStyle.Fill, IsWebBrowserContextMenuEnabled = false, ScriptErrorsSuppressed = true, ScrollBarsEnabled = true, AllowNavigation = false, }; Controls.Add(_browser); if (_infoBarDisplayed) { _infoBar = GetTextBoxControl(codeFileHTML); Resize += FormResized; Controls.Add(_infoBar); } }); PowerToysTelemetry.Log.WriteEvent(new CodeFilePreviewed()); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) #pragma warning restore CA1031 // Do not catch general exception types { PowerToysTelemetry.Log.WriteEvent(new CodeFilePreviewError { Message = ex.Message }); InvokeOnControlThread(() => { Controls.Clear(); _infoBarDisplayed = true; _infoBar = GetTextBoxControl(Resources.CodeFileNotPreviewedError); Resize += FormResized; Controls.Add(_infoBar); }); } finally { base.DoPreview(dataSource); } }