mirror of
https://github.com/QL-Win/QuickLook.git
synced 2025-09-10 09:19:06 +00:00
#1080: dark theme support for the text viewer
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
@@ -25,6 +26,7 @@ using System.Xml;
|
||||
using ICSharpCode.AvalonEdit;
|
||||
using ICSharpCode.AvalonEdit.Highlighting;
|
||||
using ICSharpCode.AvalonEdit.Highlighting.Xshd;
|
||||
using QuickLook.Common.Helpers;
|
||||
using QuickLook.Common.Plugin;
|
||||
|
||||
namespace QuickLook.Plugin.TextViewer
|
||||
@@ -33,33 +35,18 @@ namespace QuickLook.Plugin.TextViewer
|
||||
{
|
||||
private TextViewerPanel _tvp;
|
||||
|
||||
private static HighlightingManager _hlmLight;
|
||||
private static HighlightingManager _hlmDark;
|
||||
|
||||
public int Priority => -5;
|
||||
|
||||
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
|
||||
var _ = new TextEditor();
|
||||
|
||||
_hlmLight = getHighlightingManager(Themes.Light, "Light");
|
||||
_hlmDark = getHighlightingManager(Themes.Dark, "Dark");
|
||||
}
|
||||
|
||||
public bool CanHandle(string path)
|
||||
@@ -87,8 +74,6 @@ namespace QuickLook.Plugin.TextViewer
|
||||
|
||||
public void Prepare(string path, ContextObject context)
|
||||
{
|
||||
//context.Theme = Themes.Light;
|
||||
|
||||
context.PreferredSize = new Size {Width = 800, Height = 600};
|
||||
}
|
||||
|
||||
@@ -109,6 +94,8 @@ namespace QuickLook.Plugin.TextViewer
|
||||
else
|
||||
{
|
||||
_tvp = new TextViewerPanel(path, context);
|
||||
AssignHighlightingManager(_tvp, context);
|
||||
|
||||
context.ViewerContent = _tvp;
|
||||
}
|
||||
context.Title = $"{Path.GetFileName(path)}";
|
||||
@@ -128,5 +115,39 @@ namespace QuickLook.Plugin.TextViewer
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@@ -57,8 +57,8 @@
|
||||
</AssemblyOriginatorKeyFile>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="ICSharpCode.AvalonEdit, Version=6.1.2.30, Culture=neutral, PublicKeyToken=9cc39be672370310, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\packages\AvalonEdit.6.1.2.30\lib\net45\ICSharpCode.AvalonEdit.dll</HintPath>
|
||||
<Reference Include="ICSharpCode.AvalonEdit, Version=6.1.3.50, Culture=neutral, PublicKeyToken=9cc39be672370310, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\packages\AvalonEdit.6.1.3.50\lib\net45\ICSharpCode.AvalonEdit.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="PresentationCore" />
|
||||
<Reference Include="PresentationFramework" />
|
||||
@@ -89,8 +89,9 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Syntax\**">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
<CopyToOutputDirectory>None</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<None Include="app.config" />
|
||||
<None Include="packages.config" />
|
||||
<None Include="Translations.config">
|
||||
<SubType>Designer</SubType>
|
||||
|
@@ -39,12 +39,14 @@ namespace QuickLook.Plugin.TextViewer
|
||||
{
|
||||
private readonly ContextObject _context;
|
||||
private bool _disposed;
|
||||
private HighlightingManager highlightingManager = HighlightingManager.Instance;
|
||||
|
||||
public TextViewerPanel(string path, ContextObject context)
|
||||
{
|
||||
_context = context;
|
||||
|
||||
Background = new SolidColorBrush(Color.FromArgb(0xAA, 255, 255, 255));
|
||||
SetResourceReference(Control.ForegroundProperty, "WindowTextForeground");
|
||||
Background = Brushes.Transparent;
|
||||
FontSize = 14;
|
||||
ShowLineNumbers = true;
|
||||
WordWrap = true;
|
||||
@@ -82,6 +84,12 @@ namespace QuickLook.Plugin.TextViewer
|
||||
LoadFileAsync(path);
|
||||
}
|
||||
|
||||
public HighlightingManager HighlightingManager
|
||||
{
|
||||
get => highlightingManager;
|
||||
set => highlightingManager = value;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_disposed = true;
|
||||
@@ -185,7 +193,7 @@ namespace QuickLook.Plugin.TextViewer
|
||||
Encoding = encoding;
|
||||
SyntaxHighlighting = bufferCopy.Length > maxHighlightingLength
|
||||
? null
|
||||
: HighlightingManager.Instance.GetDefinitionByExtension(Path.GetExtension(path));
|
||||
: HighlightingManager?.GetDefinitionByExtension(Path.GetExtension(path));
|
||||
Document = doc;
|
||||
|
||||
_context.IsBusy = false;
|
||||
|
11
QuickLook.Plugin/QuickLook.Plugin.TextViewer/app.config
Normal file
11
QuickLook.Plugin/QuickLook.Plugin.TextViewer/app.config
Normal 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>
|
@@ -1,5 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<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" />
|
||||
</packages>
|
Reference in New Issue
Block a user