#1080: dark theme support for the text viewer

This commit is contained in:
Paddy
2022-02-20 18:55:28 +01:00
parent c93be2cb51
commit 660dc88001
92 changed files with 71 additions and 30 deletions

View File

@@ -16,6 +16,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
@@ -25,6 +26,7 @@ using System.Xml;
using ICSharpCode.AvalonEdit; using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Highlighting; using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Highlighting.Xshd; using ICSharpCode.AvalonEdit.Highlighting.Xshd;
using QuickLook.Common.Helpers;
using QuickLook.Common.Plugin; using QuickLook.Common.Plugin;
namespace QuickLook.Plugin.TextViewer namespace QuickLook.Plugin.TextViewer
@@ -33,33 +35,18 @@ namespace QuickLook.Plugin.TextViewer
{ {
private TextViewerPanel _tvp; private TextViewerPanel _tvp;
private static HighlightingManager _hlmLight;
private static HighlightingManager _hlmDark;
public int Priority => -5; public int Priority => -5;
public void Init() public void Init()
{ {
var hlm = HighlightingManager.Instance;
var assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (string.IsNullOrEmpty(assemblyPath)) return;
var syntaxPath = Path.Combine(assemblyPath, "Syntax");
if (!Directory.Exists(syntaxPath)) return;
foreach (var file in Directory.EnumerateFiles(syntaxPath, "*.xshd"))
{
var ext = Path.GetFileNameWithoutExtension(file);
using (Stream s = File.OpenRead(Path.GetFullPath(file)))
using (var reader = new XmlTextReader(s))
{
var xshd = HighlightingLoader.LoadXshd(reader);
var highlightingDefinition = HighlightingLoader.Load(xshd, hlm);
if (xshd.Extensions.Count > 0)
hlm.RegisterHighlighting(ext, xshd.Extensions.ToArray(), highlightingDefinition);
}
}
// pre-load // pre-load
var _ = new TextEditor(); var _ = new TextEditor();
_hlmLight = getHighlightingManager(Themes.Light, "Light");
_hlmDark = getHighlightingManager(Themes.Dark, "Dark");
} }
public bool CanHandle(string path) public bool CanHandle(string path)
@@ -87,8 +74,6 @@ namespace QuickLook.Plugin.TextViewer
public void Prepare(string path, ContextObject context) public void Prepare(string path, ContextObject context)
{ {
//context.Theme = Themes.Light;
context.PreferredSize = new Size {Width = 800, Height = 600}; context.PreferredSize = new Size {Width = 800, Height = 600};
} }
@@ -109,6 +94,8 @@ namespace QuickLook.Plugin.TextViewer
else else
{ {
_tvp = new TextViewerPanel(path, context); _tvp = new TextViewerPanel(path, context);
AssignHighlightingManager(_tvp, context);
context.ViewerContent = _tvp; context.ViewerContent = _tvp;
} }
context.Title = $"{Path.GetFileName(path)}"; context.Title = $"{Path.GetFileName(path)}";
@@ -128,5 +115,39 @@ namespace QuickLook.Plugin.TextViewer
return true; return true;
} }
private HighlightingManager getHighlightingManager(Themes theme, string dirName)
{
var hlm = new HighlightingManager();
var assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (string.IsNullOrEmpty(assemblyPath))
return hlm;
var syntaxPath = Path.Combine(assemblyPath, "Syntax", dirName);
if (!Directory.Exists(syntaxPath))
return hlm;
foreach (var file in Directory.EnumerateFiles(syntaxPath, "*.xshd"))
{
Debug.WriteLine(file);
var ext = Path.GetFileNameWithoutExtension(file);
using (Stream s = File.OpenRead(Path.GetFullPath(file)))
using (var reader = new XmlTextReader(s))
{
var xshd = HighlightingLoader.LoadXshd(reader);
var highlightingDefinition = HighlightingLoader.Load(xshd, hlm);
if (xshd.Extensions.Count > 0)
hlm.RegisterHighlighting(ext, xshd.Extensions.ToArray(), highlightingDefinition);
}
}
return hlm;
}
private void AssignHighlightingManager(TextViewerPanel tvp, ContextObject context)
{
var isDark = (context.Theme == Themes.Dark) | OSThemeHelper.AppsUseDarkTheme() | false;
tvp.HighlightingManager = isDark ? _hlmDark : _hlmLight;
}
} }
} }

View File

@@ -57,8 +57,8 @@
</AssemblyOriginatorKeyFile> </AssemblyOriginatorKeyFile>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="ICSharpCode.AvalonEdit, Version=6.1.2.30, Culture=neutral, PublicKeyToken=9cc39be672370310, processorArchitecture=MSIL"> <Reference Include="ICSharpCode.AvalonEdit, Version=6.1.3.50, Culture=neutral, PublicKeyToken=9cc39be672370310, processorArchitecture=MSIL">
<HintPath>..\..\packages\AvalonEdit.6.1.2.30\lib\net45\ICSharpCode.AvalonEdit.dll</HintPath> <HintPath>..\..\packages\AvalonEdit.6.1.3.50\lib\net45\ICSharpCode.AvalonEdit.dll</HintPath>
</Reference> </Reference>
<Reference Include="PresentationCore" /> <Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" /> <Reference Include="PresentationFramework" />
@@ -89,8 +89,9 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="Syntax\**"> <Content Include="Syntax\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>None</CopyToOutputDirectory>
</Content> </Content>
<None Include="app.config" />
<None Include="packages.config" /> <None Include="packages.config" />
<None Include="Translations.config"> <None Include="Translations.config">
<SubType>Designer</SubType> <SubType>Designer</SubType>

View File

@@ -39,12 +39,14 @@ namespace QuickLook.Plugin.TextViewer
{ {
private readonly ContextObject _context; private readonly ContextObject _context;
private bool _disposed; private bool _disposed;
private HighlightingManager highlightingManager = HighlightingManager.Instance;
public TextViewerPanel(string path, ContextObject context) public TextViewerPanel(string path, ContextObject context)
{ {
_context = context; _context = context;
Background = new SolidColorBrush(Color.FromArgb(0xAA, 255, 255, 255)); SetResourceReference(Control.ForegroundProperty, "WindowTextForeground");
Background = Brushes.Transparent;
FontSize = 14; FontSize = 14;
ShowLineNumbers = true; ShowLineNumbers = true;
WordWrap = true; WordWrap = true;
@@ -82,6 +84,12 @@ namespace QuickLook.Plugin.TextViewer
LoadFileAsync(path); LoadFileAsync(path);
} }
public HighlightingManager HighlightingManager
{
get => highlightingManager;
set => highlightingManager = value;
}
public void Dispose() public void Dispose()
{ {
_disposed = true; _disposed = true;
@@ -144,7 +152,7 @@ namespace QuickLook.Plugin.TextViewer
Task.Run(() => Task.Run(() =>
{ {
const int maxLength = 5 * 1024 * 1024; const int maxLength = 5 * 1024 * 1024;
const int maxHighlightingLength = (int) (0.5 * 1024 * 1024); const int maxHighlightingLength = (int)(0.5 * 1024 * 1024);
var buffer = new MemoryStream(); var buffer = new MemoryStream();
bool fileTooLong; bool fileTooLong;
@@ -185,7 +193,7 @@ namespace QuickLook.Plugin.TextViewer
Encoding = encoding; Encoding = encoding;
SyntaxHighlighting = bufferCopy.Length > maxHighlightingLength SyntaxHighlighting = bufferCopy.Length > maxHighlightingLength
? null ? null
: HighlightingManager.Instance.GetDefinitionByExtension(Path.GetExtension(path)); : HighlightingManager?.GetDefinitionByExtension(Path.GetExtension(path));
Document = doc; Document = doc;
_context.IsBusy = false; _context.IsBusy = false;

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="ICSharpCode.AvalonEdit" publicKeyToken="9cc39be672370310" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.1.3.50" newVersion="6.1.3.50" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="AvalonEdit" version="6.1.2.30" targetFramework="net462" /> <package id="AvalonEdit" version="6.1.3.50" targetFramework="net462" />
<package id="UTF.Unknown" version="2.5.0" targetFramework="net462" /> <package id="UTF.Unknown" version="2.5.0" targetFramework="net462" />
</packages> </packages>