[Monaco]Add option to set font size (#32559)

* Add setting to enable/disable it and add Peek support

* Add ability to set font size
This commit is contained in:
Aaron Junker-Wildi
2024-04-24 14:17:29 +02:00
committed by GitHub
parent 5b4da37a43
commit d9008186cf
14 changed files with 109 additions and 4 deletions

View File

@@ -10,6 +10,8 @@ namespace Peek.FilePreviewer.Models
public bool SourceCodeTryFormat { get; }
public int SourceCodeFontSize { get; }
public bool SourceCodeStickyScroll { get; }
}
}

View File

@@ -25,6 +25,8 @@ namespace Peek.FilePreviewer.Models
public bool SourceCodeTryFormat { get; private set; }
public int SourceCodeFontSize { get; private set; }
public bool SourceCodeStickyScroll { get; private set; }
public PreviewSettings()
@@ -32,6 +34,7 @@ namespace Peek.FilePreviewer.Models
_settingsUtils = new SettingsUtils();
SourceCodeWrapText = false;
SourceCodeTryFormat = false;
SourceCodeFontSize = 14;
SourceCodeStickyScroll = true;
LoadSettingsFromJson();
@@ -64,6 +67,7 @@ namespace Peek.FilePreviewer.Models
{
SourceCodeWrapText = settings.SourceCodeWrapText.Value;
SourceCodeTryFormat = settings.SourceCodeTryFormat.Value;
SourceCodeFontSize = settings.SourceCodeFontSize.Value;
SourceCodeStickyScroll = settings.SourceCodeStickyScroll.Value;
}

View File

@@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.Json;
@@ -45,13 +46,13 @@ namespace Peek.FilePreviewer.Previewers
/// <summary>
/// Prepares temp html for the previewing
/// </summary>
public static string PreviewTempFile(string fileText, string extension, string tempFolder, bool tryFormat, bool wrapText, bool stickyScroll)
public static string PreviewTempFile(string fileText, string extension, string tempFolder, bool tryFormat, bool wrapText, bool stickyScroll, int fontSize)
{
// TODO: check if file is too big, add MaxFileSize to settings
return InitializeIndexFileAndSelectedFile(fileText, extension, tempFolder, tryFormat, wrapText, stickyScroll);
return InitializeIndexFileAndSelectedFile(fileText, extension, tempFolder, tryFormat, wrapText, stickyScroll, fontSize);
}
private static string InitializeIndexFileAndSelectedFile(string fileContent, string extension, string tempFolder, bool tryFormat, bool wrapText, bool stickyScroll)
private static string InitializeIndexFileAndSelectedFile(string fileContent, string extension, string tempFolder, bool tryFormat, bool wrapText, bool stickyScroll, int fontSize)
{
string vsCodeLangSet = Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.GetLanguage(extension);
@@ -81,6 +82,7 @@ namespace Peek.FilePreviewer.Previewers
html = html.Replace("[[PT_WRAP]]", wrapText ? "1" : "0", StringComparison.InvariantCulture);
html = html.Replace("[[PT_STICKY_SCROLL]]", stickyScroll ? "1" : "0", StringComparison.InvariantCulture);
html = html.Replace("[[PT_THEME]]", theme, StringComparison.InvariantCulture);
html = html.Replace("[[PT_FONT_SIZE]]", fontSize.ToString(CultureInfo.InvariantCulture), StringComparison.InvariantCulture);
html = html.Replace("[[PT_CODE]]", base64FileCode, StringComparison.InvariantCulture);
html = html.Replace("[[PT_URL]]", Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.VirtualHostName, StringComparison.InvariantCulture);

View File

@@ -112,7 +112,7 @@ namespace Peek.FilePreviewer.Previewers
if (IsDevFilePreview && !isHtml && !isMarkdown)
{
var raw = await ReadHelper.Read(File.Path.ToString());
Preview = new Uri(MonacoHelper.PreviewTempFile(raw, File.Extension, TempFolderPath.Path, _previewSettings.SourceCodeTryFormat, _previewSettings.SourceCodeWrapText, _previewSettings.SourceCodeStickyScroll));
Preview = new Uri(MonacoHelper.PreviewTempFile(raw, File.Extension, TempFolderPath.Path, _previewSettings.SourceCodeTryFormat, _previewSettings.SourceCodeWrapText, _previewSettings.SourceCodeStickyScroll, _previewSettings.SourceCodeFontSize));
}
else if (isMarkdown)
{

View File

@@ -397,6 +397,8 @@ namespace Microsoft.PowerToys.PreviewHandler.Monaco
_html = _html.Replace("[[PT_LANG]]", _vsCodeLangSet, StringComparison.InvariantCulture);
_html = _html.Replace("[[PT_WRAP]]", _settings.Wrap ? "1" : "0", StringComparison.InvariantCulture);
_html = _html.Replace("[[PT_THEME]]", Settings.GetTheme(), StringComparison.InvariantCulture);
_html = _html.Replace("[[PT_STICKY_SCROLL]]", _settings.StickyScroll ? "1" : "0", StringComparison.InvariantCulture);
_html = _html.Replace("[[PT_FONT_SIZE]]", _settings.FontSize.ToString(CultureInfo.InvariantCulture), StringComparison.InvariantCulture);
_html = _html.Replace("[[PT_CODE]]", _base64FileCode, StringComparison.InvariantCulture);
_html = _html.Replace("[[PT_STICKY_SCROLL]]", _settings.StickyScroll ? "1" : "0", StringComparison.InvariantCulture);
_html = _html.Replace("[[PT_URL]]", FilePreviewCommon.MonacoHelper.VirtualHostName, StringComparison.InvariantCulture);

View File

@@ -77,6 +77,26 @@ namespace Microsoft.PowerToys.PreviewHandler.Monaco
}
}
/// <summary>
/// Gets the font size for the previewer. Set by PT settings.
/// </summary>
public double FontSize
{
get
{
try
{
return moduleSettings.GetSettings<PowerPreviewSettings>(PowerPreviewSettings.ModuleName).Properties.MonacoPreviewFontSize.Value;
}
catch (FileNotFoundException)
{
// Couldn't read the settings.
// Assume default of 14.
return 14;
}
}
}
/// <summary>
/// Gets a value indicating whether sticky scroll should be enabled. Set by PT settings.
/// </summary>