From d9008186cff3ce3d56cde63655167ee8fd2f8601 Mon Sep 17 00:00:00 2001 From: Aaron Junker-Wildi Date: Wed, 24 Apr 2024 14:17:29 +0200 Subject: [PATCH] [Monaco]Add option to set font size (#32559) * Add setting to enable/disable it and add Peek support * Add ability to set font size --- .../Assets/Monaco/index.html | 3 +++ .../Models/IPreviewSettings.cs | 2 ++ .../Models/PreviewSettings.cs | 4 ++++ .../Helpers/MonacoHelper.cs | 8 +++++--- .../WebBrowserPreviewer.cs | 2 +- .../MonacoPreviewHandlerControl.cs | 2 ++ .../MonacoPreviewHandler/Settings.cs | 20 +++++++++++++++++++ .../PeekPreviewSettings.cs | 3 +++ .../PowerPreviewProperties.cs | 5 +++++ .../SettingsXAML/Views/PeekPage.xaml | 8 ++++++++ .../SettingsXAML/Views/PowerPreviewPage.xaml | 8 ++++++++ .../Settings.UI/Strings/en-us/Resources.resw | 14 +++++++++++++ .../Settings.UI/ViewModels/PeekViewModel.cs | 14 +++++++++++++ .../ViewModels/PowerPreviewViewModel.cs | 20 +++++++++++++++++++ 14 files changed, 109 insertions(+), 4 deletions(-) diff --git a/src/common/FilePreviewCommon/Assets/Monaco/index.html b/src/common/FilePreviewCommon/Assets/Monaco/index.html index 7b0bfd3c1b..d452475233 100644 --- a/src/common/FilePreviewCommon/Assets/Monaco/index.html +++ b/src/common/FilePreviewCommon/Assets/Monaco/index.html @@ -17,6 +17,8 @@ var base64code = "[[PT_CODE]]"; var stickyScroll = ([[PT_STICKY_SCROLL]] == 1) ? true : false; + + var fontSize = [[PT_FONT_SIZE]]; // Code taken from https://stackoverflow.com/a/30106551/14774889 var code = decodeURIComponent(atob(base64code).split('').map(function(c) { @@ -80,6 +82,7 @@ }, stickyScroll: {enabled: stickyScroll}, + fontSize: fontSize, wordWrap: (wrap?"on":"off") // Word wraps }); window.onresize = function (){ diff --git a/src/modules/peek/Peek.FilePreviewer/Models/IPreviewSettings.cs b/src/modules/peek/Peek.FilePreviewer/Models/IPreviewSettings.cs index ca67baf40a..9a89d9a4aa 100644 --- a/src/modules/peek/Peek.FilePreviewer/Models/IPreviewSettings.cs +++ b/src/modules/peek/Peek.FilePreviewer/Models/IPreviewSettings.cs @@ -10,6 +10,8 @@ namespace Peek.FilePreviewer.Models public bool SourceCodeTryFormat { get; } + public int SourceCodeFontSize { get; } + public bool SourceCodeStickyScroll { get; } } } diff --git a/src/modules/peek/Peek.FilePreviewer/Models/PreviewSettings.cs b/src/modules/peek/Peek.FilePreviewer/Models/PreviewSettings.cs index 5409aebb8b..1be17f3689 100644 --- a/src/modules/peek/Peek.FilePreviewer/Models/PreviewSettings.cs +++ b/src/modules/peek/Peek.FilePreviewer/Models/PreviewSettings.cs @@ -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; } diff --git a/src/modules/peek/Peek.FilePreviewer/Previewers/WebBrowserPreviewer/Helpers/MonacoHelper.cs b/src/modules/peek/Peek.FilePreviewer/Previewers/WebBrowserPreviewer/Helpers/MonacoHelper.cs index 17c0074bd0..3364997695 100644 --- a/src/modules/peek/Peek.FilePreviewer/Previewers/WebBrowserPreviewer/Helpers/MonacoHelper.cs +++ b/src/modules/peek/Peek.FilePreviewer/Previewers/WebBrowserPreviewer/Helpers/MonacoHelper.cs @@ -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 /// /// Prepares temp html for the previewing /// - 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); diff --git a/src/modules/peek/Peek.FilePreviewer/Previewers/WebBrowserPreviewer/WebBrowserPreviewer.cs b/src/modules/peek/Peek.FilePreviewer/Previewers/WebBrowserPreviewer/WebBrowserPreviewer.cs index 92808da0e0..1c35093ddb 100644 --- a/src/modules/peek/Peek.FilePreviewer/Previewers/WebBrowserPreviewer/WebBrowserPreviewer.cs +++ b/src/modules/peek/Peek.FilePreviewer/Previewers/WebBrowserPreviewer/WebBrowserPreviewer.cs @@ -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) { diff --git a/src/modules/previewpane/MonacoPreviewHandler/MonacoPreviewHandlerControl.cs b/src/modules/previewpane/MonacoPreviewHandler/MonacoPreviewHandlerControl.cs index 9c0db9ca18..8bf05690f5 100644 --- a/src/modules/previewpane/MonacoPreviewHandler/MonacoPreviewHandlerControl.cs +++ b/src/modules/previewpane/MonacoPreviewHandler/MonacoPreviewHandlerControl.cs @@ -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); diff --git a/src/modules/previewpane/MonacoPreviewHandler/Settings.cs b/src/modules/previewpane/MonacoPreviewHandler/Settings.cs index bf8571bbb9..03dd2148fe 100644 --- a/src/modules/previewpane/MonacoPreviewHandler/Settings.cs +++ b/src/modules/previewpane/MonacoPreviewHandler/Settings.cs @@ -77,6 +77,26 @@ namespace Microsoft.PowerToys.PreviewHandler.Monaco } } + /// + /// Gets the font size for the previewer. Set by PT settings. + /// + public double FontSize + { + get + { + try + { + return moduleSettings.GetSettings(PowerPreviewSettings.ModuleName).Properties.MonacoPreviewFontSize.Value; + } + catch (FileNotFoundException) + { + // Couldn't read the settings. + // Assume default of 14. + return 14; + } + } + } + /// /// Gets a value indicating whether sticky scroll should be enabled. Set by PT settings. /// diff --git a/src/settings-ui/Settings.UI.Library/PeekPreviewSettings.cs b/src/settings-ui/Settings.UI.Library/PeekPreviewSettings.cs index 4c0fe2d0fe..75562ca9a2 100644 --- a/src/settings-ui/Settings.UI.Library/PeekPreviewSettings.cs +++ b/src/settings-ui/Settings.UI.Library/PeekPreviewSettings.cs @@ -16,12 +16,15 @@ namespace Settings.UI.Library public BoolProperty SourceCodeTryFormat { get; set; } + public IntProperty SourceCodeFontSize { get; set; } + public BoolProperty SourceCodeStickyScroll { get; set; } public PeekPreviewSettings() { SourceCodeWrapText = new BoolProperty(false); SourceCodeTryFormat = new BoolProperty(false); + SourceCodeFontSize = new IntProperty(14); SourceCodeStickyScroll = new BoolProperty(true); } diff --git a/src/settings-ui/Settings.UI.Library/PowerPreviewProperties.cs b/src/settings-ui/Settings.UI.Library/PowerPreviewProperties.cs index e06814e3b5..1c09438b61 100644 --- a/src/settings-ui/Settings.UI.Library/PowerPreviewProperties.cs +++ b/src/settings-ui/Settings.UI.Library/PowerPreviewProperties.cs @@ -15,6 +15,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library { public const string DefaultStlThumbnailColor = "#FFC924"; public const int DefaultMonacoMaxFileSize = 50; + public const int DefaultMonacoFontSize = 14; public const int DefaultSvgBackgroundColorMode = (int)SvgPreviewColorMode.Default; public const string DefaultSvgBackgroundSolidColor = "#FFFFFF"; public const int DefaultSvgBackgroundCheckeredShade = (int)SvgPreviewCheckeredShade.Light; @@ -133,6 +134,9 @@ namespace Microsoft.PowerToys.Settings.UI.Library [JsonPropertyName("monaco-previewer-max-file-size")] public IntProperty MonacoPreviewMaxFileSize { get; set; } + [JsonPropertyName("monaco-previewer-font-size")] + public IntProperty MonacoPreviewFontSize { get; set; } + private bool monacoPreviewStickyScroll = true; [JsonPropertyName("monaco-previewer-sticky-scroll")] @@ -279,6 +283,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library SvgBackgroundCheckeredShade = new IntProperty(DefaultSvgBackgroundCheckeredShade); StlThumbnailColor = new StringProperty(DefaultStlThumbnailColor); MonacoPreviewMaxFileSize = new IntProperty(DefaultMonacoMaxFileSize); + MonacoPreviewFontSize = new IntProperty(DefaultMonacoFontSize); } public override string ToString() diff --git a/src/settings-ui/Settings.UI/SettingsXAML/Views/PeekPage.xaml b/src/settings-ui/Settings.UI/SettingsXAML/Views/PeekPage.xaml index b0069841af..e7ec1c3610 100644 --- a/src/settings-ui/Settings.UI/SettingsXAML/Views/PeekPage.xaml +++ b/src/settings-ui/Settings.UI/SettingsXAML/Views/PeekPage.xaml @@ -55,6 +55,14 @@ IsChecked="{x:Bind ViewModel.SourceCodeTryFormat, Mode=TwoWay}" IsEnabled="{x:Bind ViewModel.IsEnabled, Mode=OneWay}" /> + + + diff --git a/src/settings-ui/Settings.UI/SettingsXAML/Views/PowerPreviewPage.xaml b/src/settings-ui/Settings.UI/SettingsXAML/Views/PowerPreviewPage.xaml index ac618bd901..f483695820 100644 --- a/src/settings-ui/Settings.UI/SettingsXAML/Views/PowerPreviewPage.xaml +++ b/src/settings-ui/Settings.UI/SettingsXAML/Views/PowerPreviewPage.xaml @@ -89,6 +89,14 @@ SpinButtonPlacementMode="Compact" Value="{x:Bind ViewModel.MonacoPreviewMaxFileSize, Mode=TwoWay}" /> + + + Enable sticky scroll + + Font size of the editor in pt. Recommended: 14pt + {Locked="pt"} + + + Font size + + + Font size of the editor in pt. Recommended: 14pt + {Locked="pt"} + + + Font size + \ No newline at end of file diff --git a/src/settings-ui/Settings.UI/ViewModels/PeekViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/PeekViewModel.cs index b4fb5a22de..4672d0dabf 100644 --- a/src/settings-ui/Settings.UI/ViewModels/PeekViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/PeekViewModel.cs @@ -173,6 +173,20 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels } } + public int SourceCodeFontSize + { + get => _peekPreviewSettings.SourceCodeFontSize.Value; + set + { + if (_peekPreviewSettings.SourceCodeFontSize.Value != value) + { + _peekPreviewSettings.SourceCodeFontSize.Value = value; + OnPropertyChanged(nameof(SourceCodeFontSize)); + SavePreviewSettings(); + } + } + } + public bool SourceCodeStickyScroll { get => _peekPreviewSettings.SourceCodeStickyScroll.Value; diff --git a/src/settings-ui/Settings.UI/ViewModels/PowerPreviewViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/PowerPreviewViewModel.cs index d38415d192..fa494f6cd5 100644 --- a/src/settings-ui/Settings.UI/ViewModels/PowerPreviewViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/PowerPreviewViewModel.cs @@ -92,6 +92,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels _monacoWrapText = Settings.Properties.EnableMonacoPreviewWordWrap; _monacoPreviewTryFormat = Settings.Properties.MonacoPreviewTryFormat; _monacoMaxFileSize = Settings.Properties.MonacoPreviewMaxFileSize.Value; + _monacoFontSize = Settings.Properties.MonacoPreviewFontSize.Value; _monacoStickyScroll = Settings.Properties.MonacoPreviewStickyScroll; _pdfRenderEnabledGpoRuleConfiguration = GPOWrapper.GetConfiguredPdfPreviewEnabledValue(); @@ -233,6 +234,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private bool _monacoPreviewTryFormat; private int _monacoMaxFileSize; private bool _monacoStickyScroll; + private int _monacoFontSize; private GpoRuleConfigured _pdfRenderEnabledGpoRuleConfiguration; private bool _pdfRenderEnabledStateIsGPOConfigured; @@ -615,6 +617,24 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels } } + public int MonacoPreviewFontSize + { + get + { + return _monacoFontSize; + } + + set + { + if (_monacoFontSize != value) + { + _monacoFontSize = value; + Settings.Properties.MonacoPreviewFontSize.Value = value; + RaisePropertyChanged(); + } + } + } + public bool PDFRenderIsEnabled { get