mirror of
https://github.com/QL-Win/QuickLook.git
synced 2025-09-11 17:59:17 +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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user