[Monaco]Make minimap toggleable (#33742)

This commit is contained in:
PesBandi 2025-01-14 12:47:53 +01:00 коммит произвёл GitHub
Родитель f11c885184
Коммит 80461c0241
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
16 изменённых файлов: 144 добавлений и 34 удалений

Просмотреть файл

@ -9,17 +9,19 @@
// `theme` can be "vs" for light theme or "vs-dark" for dark theme // `theme` can be "vs" for light theme or "vs-dark" for dark theme
// `lang` is the language of the file // `lang` is the language of the file
// `wrap` if the editor is wrapping or not // `wrap` if the editor is wrapping or not
// `minimap` if the minimap is shown
// `contextMenu` whether to use the Monaco context menu. The built-in context menu
// doesn't work in Peek, so we set this to false and create a custom one
var theme = ("[[PT_THEME]]" == "dark") ? "vs-dark" : "vs"; var theme = ("[[PT_THEME]]" == "dark") ? "vs-dark" : "vs";
var lang = "[[PT_LANG]]"; var wrap = [[PT_WRAP]];
var wrap = ([[PT_WRAP]] == 1) ? true : false; var minimap = [[PT_MINIMAP]];
var stickyScroll = [[PT_STICKY_SCROLL]];
var base64code = "[[PT_CODE]]";
var stickyScroll = ([[PT_STICKY_SCROLL]] == 1) ? true : false;
var fontSize = [[PT_FONT_SIZE]]; var fontSize = [[PT_FONT_SIZE]];
var contextMenu = ([[PT_CONTEXTMENU]] == 1) ? true : false;
var lang = "[[PT_LANG]]";
var base64code = "[[PT_CODE]]";
var contextMenu = [[PT_CONTEXTMENU]];
var editor; var editor;
@ -29,12 +31,13 @@
}).join('')); }).join(''));
function runToggleTextWrapCommand() { function runToggleTextWrapCommand() {
if (wrap) {
editor.updateOptions({ wordWrap: 'off' })
} else {
editor.updateOptions({ wordWrap: 'on' })
}
wrap = !wrap; wrap = !wrap;
editor.updateOptions({ wordWrap: wrap ? 'on' : 'off' });
}
function runToggleMinimap() {
minimap = !minimap;
editor.updateOptions({minimap: {enabled: minimap}});
} }
function runCopyCommand() { function runCopyCommand() {
@ -99,8 +102,8 @@
language: lang, // Sets language of the code language: lang, // Sets language of the code
readOnly: true, // Sets to readonly readOnly: true, // Sets to readonly
theme: 'theme', // Sets editor theme theme: 'theme', // Sets editor theme
minimap: { enabled: false }, // Disables minimap minimap: { enabled: minimap }, // Controls if minimap is shown
lineNumbersMinChars: '3', // Width of the line numbers lineNumbersMinChars: 3, // Width of the line numbers
contextmenu: contextMenu, contextmenu: contextMenu,
scrollbar: { scrollbar: {
// Deactivate shadows // Deactivate shadows
@ -135,10 +138,20 @@
contextMenuOrder: 100, contextMenuOrder: 100,
// Method that will be executed when the action is triggered. // Method that will be executed when the action is triggered.
// @param editor The editor instance is passed in as a convenience run: runToggleTextWrapCommand
run: function (ed) { });
runToggleTextWrapCommand();
} editor.addAction({
id: 'toggle-minimap',
label: 'Toggle minimap',
contextMenuGroupId: 'cutcopypaste',
contextMenuOrder: 100,
// Method that will be executed when the action is triggered.
run: runToggleMinimap
}); });
onContextMenu(); onContextMenu();
@ -166,4 +179,4 @@
} }
</script> </script>
</body> </body>
</html> </html>

Просмотреть файл

@ -231,10 +231,11 @@ namespace Peek.FilePreviewer.Controls
// When using Monaco, we show menu items that call the appropriate JS functions - // When using Monaco, we show menu items that call the appropriate JS functions -
// WebView2 isn't able to show a "Copy" menu item of its own. // WebView2 isn't able to show a "Copy" menu item of its own.
return [ return [
CreateCommandMenuItem("ContextMenu_Copy", "runCopyCommand"), CreateCommandMenuItem("ContextMenu_Copy", "runCopyCommand"),
new Separator(), new Separator(),
CreateCommandMenuItem("ContextMenu_ToggleTextWrapping", "runToggleTextWrapCommand"), CreateCommandMenuItem("ContextMenu_ToggleTextWrapping", "runToggleTextWrapCommand"),
]; CreateCommandMenuItem("ContextMenu_ToggleMinimap", "runToggleMinimap")
];
} }
else else
{ {

Просмотреть файл

@ -13,5 +13,7 @@ namespace Peek.FilePreviewer.Models
public int SourceCodeFontSize { get; } public int SourceCodeFontSize { get; }
public bool SourceCodeStickyScroll { get; } public bool SourceCodeStickyScroll { get; }
public bool SourceCodeMinimap { get; }
} }
} }

Просмотреть файл

@ -30,6 +30,8 @@ namespace Peek.FilePreviewer.Models
public bool SourceCodeStickyScroll { get; private set; } public bool SourceCodeStickyScroll { get; private set; }
public bool SourceCodeMinimap { get; private set; }
public PreviewSettings() public PreviewSettings()
{ {
_settingsUtils = new SettingsUtils(); _settingsUtils = new SettingsUtils();
@ -37,6 +39,7 @@ namespace Peek.FilePreviewer.Models
SourceCodeTryFormat = false; SourceCodeTryFormat = false;
SourceCodeFontSize = 14; SourceCodeFontSize = 14;
SourceCodeStickyScroll = true; SourceCodeStickyScroll = true;
SourceCodeMinimap = false;
LoadSettingsFromJson(); LoadSettingsFromJson();
@ -70,6 +73,7 @@ namespace Peek.FilePreviewer.Models
SourceCodeTryFormat = settings.SourceCodeTryFormat.Value; SourceCodeTryFormat = settings.SourceCodeTryFormat.Value;
SourceCodeFontSize = settings.SourceCodeFontSize.Value; SourceCodeFontSize = settings.SourceCodeFontSize.Value;
SourceCodeStickyScroll = settings.SourceCodeStickyScroll.Value; SourceCodeStickyScroll = settings.SourceCodeStickyScroll.Value;
SourceCodeMinimap = settings.SourceCodeMinimap.Value;
} }
retry = false; retry = false;

Просмотреть файл

@ -48,13 +48,13 @@ namespace Peek.FilePreviewer.Previewers
/// <summary> /// <summary>
/// Prepares temp html for the previewing /// Prepares temp html for the previewing
/// </summary> /// </summary>
public static string PreviewTempFile(string fileText, string extension, string tempFolder, bool tryFormat, bool wrapText, bool stickyScroll, int fontSize) public static string PreviewTempFile(string fileText, string extension, string tempFolder, bool tryFormat, bool wrapText, bool stickyScroll, int fontSize, bool minimap)
{ {
// TODO: check if file is too big, add MaxFileSize to settings // TODO: check if file is too big, add MaxFileSize to settings
return InitializeIndexFileAndSelectedFile(fileText, extension, tempFolder, tryFormat, wrapText, stickyScroll, fontSize); return InitializeIndexFileAndSelectedFile(fileText, extension, tempFolder, tryFormat, wrapText, stickyScroll, fontSize, minimap);
} }
private static string InitializeIndexFileAndSelectedFile(string fileContent, string extension, string tempFolder, bool tryFormat, bool wrapText, bool stickyScroll, int fontSize) private static string InitializeIndexFileAndSelectedFile(string fileContent, string extension, string tempFolder, bool tryFormat, bool wrapText, bool stickyScroll, int fontSize, bool minimap)
{ {
string vsCodeLangSet = Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.GetLanguage(extension); string vsCodeLangSet = Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.GetLanguage(extension);
@ -81,13 +81,14 @@ namespace Peek.FilePreviewer.Previewers
string html = Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.ReadIndexHtml(); string html = Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.ReadIndexHtml();
html = html.Replace("[[PT_LANG]]", vsCodeLangSet, StringComparison.InvariantCulture); html = html.Replace("[[PT_LANG]]", vsCodeLangSet, StringComparison.InvariantCulture);
html = html.Replace("[[PT_WRAP]]", wrapText ? "1" : "0", StringComparison.InvariantCulture); html = html.Replace("[[PT_WRAP]]", wrapText ? "true" : "false", StringComparison.InvariantCulture);
html = html.Replace("[[PT_CONTEXTMENU]]", "0", StringComparison.InvariantCulture); html = html.Replace("[[PT_CONTEXTMENU]]", "false", StringComparison.InvariantCulture);
html = html.Replace("[[PT_STICKY_SCROLL]]", stickyScroll ? "1" : "0", StringComparison.InvariantCulture); html = html.Replace("[[PT_STICKY_SCROLL]]", stickyScroll ? "true" : "false", StringComparison.InvariantCulture);
html = html.Replace("[[PT_THEME]]", theme, 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_FONT_SIZE]]", fontSize.ToString(CultureInfo.InvariantCulture), StringComparison.InvariantCulture);
html = html.Replace("[[PT_CODE]]", base64FileCode, StringComparison.InvariantCulture); html = html.Replace("[[PT_CODE]]", base64FileCode, StringComparison.InvariantCulture);
html = html.Replace("[[PT_URL]]", Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.VirtualHostName, StringComparison.InvariantCulture); html = html.Replace("[[PT_URL]]", Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.VirtualHostName, StringComparison.InvariantCulture);
html = html.Replace("[[PT_MINIMAP]]", minimap ? "true" : "false", StringComparison.InvariantCulture);
string filename = tempFolder + "\\" + Guid.NewGuid().ToString() + ".html"; string filename = tempFolder + "\\" + Guid.NewGuid().ToString() + ".html";
File.WriteAllText(filename, html); File.WriteAllText(filename, html);

Просмотреть файл

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

Просмотреть файл

@ -326,4 +326,7 @@
<value>Toggle text wrapping</value> <value>Toggle text wrapping</value>
<comment>Toggle whether text in pane is word-wrapped</comment> <comment>Toggle whether text in pane is word-wrapped</comment>
</data> </data>
<data name="ContextMenu_ToggleMinimap" xml:space="preserve">
<value>Toggle minimap</value>
</data>
</root> </root>

Просмотреть файл

@ -396,13 +396,14 @@ namespace Microsoft.PowerToys.PreviewHandler.Monaco
// prepping index html to load in // prepping index html to load in
_html = FilePreviewCommon.MonacoHelper.ReadIndexHtml(); _html = FilePreviewCommon.MonacoHelper.ReadIndexHtml();
_html = _html.Replace("[[PT_LANG]]", _vsCodeLangSet, StringComparison.InvariantCulture); _html = _html.Replace("[[PT_LANG]]", _vsCodeLangSet, StringComparison.InvariantCulture);
_html = _html.Replace("[[PT_WRAP]]", _settings.Wrap ? "1" : "0", StringComparison.InvariantCulture); _html = _html.Replace("[[PT_WRAP]]", _settings.Wrap ? "true" : "false", StringComparison.InvariantCulture);
_html = _html.Replace("[[PT_CONTEXTMENU]]", "1", StringComparison.InvariantCulture); _html = _html.Replace("[[PT_CONTEXTMENU]]", "true", StringComparison.InvariantCulture);
_html = _html.Replace("[[PT_THEME]]", Settings.GetTheme(), 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_STICKY_SCROLL]]", _settings.StickyScroll ? "true" : "false", StringComparison.InvariantCulture);
_html = _html.Replace("[[PT_FONT_SIZE]]", _settings.FontSize.ToString(CultureInfo.InvariantCulture), 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_CODE]]", _base64FileCode, StringComparison.InvariantCulture);
_html = _html.Replace("[[PT_URL]]", FilePreviewCommon.MonacoHelper.VirtualHostName, StringComparison.InvariantCulture); _html = _html.Replace("[[PT_URL]]", FilePreviewCommon.MonacoHelper.VirtualHostName, StringComparison.InvariantCulture);
_html = _html.Replace("[[PT_MINIMAP]]", _settings.Minimap ? "true" : "false", StringComparison.InvariantCulture);
} }
private async void DownloadLink_Click(object sender, EventArgs e) private async void DownloadLink_Click(object sender, EventArgs e)

Просмотреть файл

@ -118,6 +118,26 @@ namespace Microsoft.PowerToys.PreviewHandler.Monaco
} }
} }
/// <summary>
/// Gets a value indicating whether the minimap should be enabled. Set by PT settings.
/// </summary>
public bool Minimap
{
get
{
try
{
return moduleSettings.GetSettings<PowerPreviewSettings>(PowerPreviewSettings.ModuleName).Properties.MonacoPreviewMinimap;
}
catch (FileNotFoundException)
{
// Couldn't read the settings
// Assume default of false
return false;
}
}
}
/// <summary> /// <summary>
/// Gets the color of the window background. /// Gets the color of the window background.
/// </summary> /// </summary>

Просмотреть файл

@ -21,12 +21,15 @@ namespace Settings.UI.Library
public BoolProperty SourceCodeStickyScroll { get; set; } public BoolProperty SourceCodeStickyScroll { get; set; }
public BoolProperty SourceCodeMinimap { get; set; }
public PeekPreviewSettings() public PeekPreviewSettings()
{ {
SourceCodeWrapText = new BoolProperty(false); SourceCodeWrapText = new BoolProperty(false);
SourceCodeTryFormat = new BoolProperty(false); SourceCodeTryFormat = new BoolProperty(false);
SourceCodeFontSize = new IntProperty(14); SourceCodeFontSize = new IntProperty(14);
SourceCodeStickyScroll = new BoolProperty(true); SourceCodeStickyScroll = new BoolProperty(true);
SourceCodeMinimap = new BoolProperty(false);
} }
public string ToJsonString() public string ToJsonString()

Просмотреть файл

@ -155,6 +155,23 @@ namespace Microsoft.PowerToys.Settings.UI.Library
} }
} }
private bool monacoPreviewMinimap;
[JsonPropertyName("monaco-previewer-minimap")]
[JsonConverter(typeof(BoolPropertyJsonConverter))]
public bool MonacoPreviewMinimap
{
get => monacoPreviewMinimap;
set
{
if (value != monacoPreviewMinimap)
{
LogTelemetryEvent(value);
monacoPreviewMinimap = value;
}
}
}
private bool enablePdfPreview; private bool enablePdfPreview;
[JsonPropertyName("pdf-previewer-toggle-setting")] [JsonPropertyName("pdf-previewer-toggle-setting")]

Просмотреть файл

@ -66,6 +66,9 @@
<tkcontrols:SettingsCard ContentAlignment="Left" IsEnabled="{x:Bind ViewModel.IsEnabled, Mode=OneWay}"> <tkcontrols:SettingsCard ContentAlignment="Left" IsEnabled="{x:Bind ViewModel.IsEnabled, Mode=OneWay}">
<CheckBox x:Uid="Peek_SourceCode_StickyScroll" IsChecked="{x:Bind ViewModel.SourceCodeStickyScroll, Mode=TwoWay}" /> <CheckBox x:Uid="Peek_SourceCode_StickyScroll" IsChecked="{x:Bind ViewModel.SourceCodeStickyScroll, Mode=TwoWay}" />
</tkcontrols:SettingsCard> </tkcontrols:SettingsCard>
<tkcontrols:SettingsCard ContentAlignment="Left" IsEnabled="{x:Bind ViewModel.IsEnabled, Mode=OneWay}">
<CheckBox x:Uid="Peek_SourceCode_Minimap" IsChecked="{x:Bind ViewModel.SourceCodeMinimap, Mode=TwoWay}" />
</tkcontrols:SettingsCard>
</tkcontrols:SettingsExpander.Items> </tkcontrols:SettingsExpander.Items>
</tkcontrols:SettingsExpander> </tkcontrols:SettingsExpander>
</controls:SettingsGroup> </controls:SettingsGroup>

Просмотреть файл

@ -103,6 +103,12 @@
IsChecked="{x:Bind ViewModel.MonacoPreviewStickyScroll, Mode=TwoWay}" IsChecked="{x:Bind ViewModel.MonacoPreviewStickyScroll, Mode=TwoWay}"
IsEnabled="{x:Bind ViewModel.MonacoRenderIsEnabled, Mode=OneWay}" /> IsEnabled="{x:Bind ViewModel.MonacoRenderIsEnabled, Mode=OneWay}" />
</tkcontrols:SettingsCard> </tkcontrols:SettingsCard>
<tkcontrols:SettingsCard ContentAlignment="Left" IsEnabled="{x:Bind ViewModel.MonacoRenderIsEnabled, Mode=OneWay}">
<CheckBox
x:Uid="FileExplorerPreview_ToggleSwitch_Monaco_Minimap"
IsChecked="{x:Bind ViewModel.MonacoPreviewMinimap, Mode=TwoWay}"
IsEnabled="{x:Bind ViewModel.MonacoRenderIsEnabled, Mode=OneWay}" />
</tkcontrols:SettingsCard>
</tkcontrols:SettingsExpander.Items> </tkcontrols:SettingsExpander.Items>
</tkcontrols:SettingsExpander> </tkcontrols:SettingsExpander>

Просмотреть файл

@ -4405,6 +4405,9 @@ Activate by holding the key for the character you want to add an accent to, then
<data name="Peek_SourceCode_WrapText.Content" xml:space="preserve"> <data name="Peek_SourceCode_WrapText.Content" xml:space="preserve">
<value>Wrap text</value> <value>Wrap text</value>
</data> </data>
<data name="Peek_SourceCode_Minimap.Content" xml:space="preserve">
<value>Show minimap</value>
</data>
<data name="ShowPluginsOverview_All.Content" xml:space="preserve"> <data name="ShowPluginsOverview_All.Content" xml:space="preserve">
<value>All</value> <value>All</value>
</data> </data>
@ -4451,6 +4454,9 @@ Activate by holding the key for the character you want to add an accent to, then
<data name="Peek_SourceCode_StickyScroll.Content" xml:space="preserve"> <data name="Peek_SourceCode_StickyScroll.Content" xml:space="preserve">
<value>Enable sticky scroll</value> <value>Enable sticky scroll</value>
</data> </data>
<data name="FileExplorerPreview_ToggleSwitch_Monaco_Minimap.Content" xml:space="preserve">
<value>Show minimap</value>
</data>
<data name="PrivacyLink.Text" xml:space="preserve"> <data name="PrivacyLink.Text" xml:space="preserve">
<value>OpenAI Privacy</value> <value>OpenAI Privacy</value>
</data> </data>

Просмотреть файл

@ -202,6 +202,20 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
} }
} }
public bool SourceCodeMinimap
{
get => _peekPreviewSettings.SourceCodeMinimap.Value;
set
{
if (_peekPreviewSettings.SourceCodeMinimap.Value != value)
{
_peekPreviewSettings.SourceCodeMinimap.Value = value;
OnPropertyChanged(nameof(SourceCodeMinimap));
SavePreviewSettings();
}
}
}
private void NotifySettingsChanged() private void NotifySettingsChanged()
{ {
// Using InvariantCulture as this is an IPC message // Using InvariantCulture as this is an IPC message

Просмотреть файл

@ -95,6 +95,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
_monacoMaxFileSize = Settings.Properties.MonacoPreviewMaxFileSize.Value; _monacoMaxFileSize = Settings.Properties.MonacoPreviewMaxFileSize.Value;
_monacoFontSize = Settings.Properties.MonacoPreviewFontSize.Value; _monacoFontSize = Settings.Properties.MonacoPreviewFontSize.Value;
_monacoStickyScroll = Settings.Properties.MonacoPreviewStickyScroll; _monacoStickyScroll = Settings.Properties.MonacoPreviewStickyScroll;
_monacoMinimap = Settings.Properties.MonacoPreviewMinimap;
_pdfRenderEnabledGpoRuleConfiguration = GPOWrapper.GetConfiguredPdfPreviewEnabledValue(); _pdfRenderEnabledGpoRuleConfiguration = GPOWrapper.GetConfiguredPdfPreviewEnabledValue();
if (_pdfRenderEnabledGpoRuleConfiguration == GpoRuleConfigured.Disabled || _pdfRenderEnabledGpoRuleConfiguration == GpoRuleConfigured.Enabled) if (_pdfRenderEnabledGpoRuleConfiguration == GpoRuleConfigured.Disabled || _pdfRenderEnabledGpoRuleConfiguration == GpoRuleConfigured.Enabled)
@ -236,6 +237,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
private int _monacoMaxFileSize; private int _monacoMaxFileSize;
private bool _monacoStickyScroll; private bool _monacoStickyScroll;
private int _monacoFontSize; private int _monacoFontSize;
private bool _monacoMinimap;
private GpoRuleConfigured _pdfRenderEnabledGpoRuleConfiguration; private GpoRuleConfigured _pdfRenderEnabledGpoRuleConfiguration;
private bool _pdfRenderEnabledStateIsGPOConfigured; private bool _pdfRenderEnabledStateIsGPOConfigured;
@ -618,6 +620,20 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
} }
} }
public bool MonacoPreviewMinimap
{
get => _monacoMinimap;
set
{
if (_monacoMinimap != value)
{
_monacoMinimap = value;
Settings.Properties.MonacoPreviewMinimap = value;
RaisePropertyChanged();
}
}
}
public int MonacoPreviewFontSize public int MonacoPreviewFontSize
{ {
get get