[Peek]Add wrap and formatting options for Monaco previewer (#29378)

* add options for monaco previewer

* fix formatting
This commit is contained in:
Davide Giacometti
2023-10-24 15:32:35 +02:00
committed by GitHub
parent 9693fd7035
commit 5a06bcb473
14 changed files with 313 additions and 24 deletions

View File

@@ -0,0 +1,40 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Text.Json;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
namespace Settings.UI.Library
{
public class PeekPreviewSettings : ISettingsConfig
{
public const string FileName = "preview-settings.json";
public BoolProperty SourceCodeWrapText { get; set; }
public BoolProperty SourceCodeTryFormat { get; set; }
public PeekPreviewSettings()
{
SourceCodeWrapText = new BoolProperty(false);
SourceCodeTryFormat = new BoolProperty(false);
}
public string ToJsonString()
{
return JsonSerializer.Serialize(this);
}
public string GetModuleName()
{
return PeekSettings.ModuleName;
}
public bool UpgradeSettingsConfiguration()
{
return false;
}
}
}

View File

@@ -40,6 +40,25 @@
</controls:SettingsCard>
</custom:SettingsGroup>
<custom:SettingsGroup x:Uid="Peek_Preview_GroupSettings" IsEnabled="{x:Bind Mode=OneWay, Path=ViewModel.IsEnabled}">
<controls:SettingsExpander
x:Uid="Peek_SourceCode_Header"
HeaderIcon="{ui:FontIcon Glyph=&#xE99A;}"
IsEnabled="{x:Bind Mode=OneWay, Path=ViewModel.IsEnabled}">
<controls:SettingsExpander.Items>
<controls:SettingsCard ContentAlignment="Left" IsEnabled="{x:Bind ViewModel.IsEnabled, Mode=OneWay}">
<CheckBox x:Uid="Peek_SourceCode_WrapText" IsChecked="{x:Bind ViewModel.SourceCodeWrapText, Mode=TwoWay}" />
</controls:SettingsCard>
<controls:SettingsCard ContentAlignment="Left" IsEnabled="{x:Bind ViewModel.IsEnabled, Mode=OneWay}">
<custom:CheckBoxWithDescriptionControl
x:Uid="Peek_SourceCode_TryFormat"
IsChecked="{x:Bind ViewModel.SourceCodeTryFormat, Mode=TwoWay}"
IsEnabled="{x:Bind ViewModel.IsEnabled, Mode=OneWay}" />
</controls:SettingsCard>
</controls:SettingsExpander.Items>
</controls:SettingsExpander>
</custom:SettingsGroup>
</StackPanel>
</custom:SettingsPageControl.ModuleContent>
<custom:SettingsPageControl.PrimaryLinks>

View File

@@ -3838,4 +3838,22 @@ Activate by holding the key for the character you want to add an accent to, then
<data name="EnabledModules.Text" xml:space="preserve">
<value>Enabled modules</value>
</data>
<data name="Peek_Preview_GroupSettings.Header" xml:space="preserve">
<value>Preview</value>
</data>
<data name="Peek_SourceCode_Header.Description" xml:space="preserve">
<value>.cpp, .py, .json, .xml, .csproj, ...</value>
</data>
<data name="Peek_SourceCode_Header.Header" xml:space="preserve">
<value>Source code files (Monaco)</value>
</data>
<data name="Peek_SourceCode_TryFormat.Description" xml:space="preserve">
<value>Applies to json and xml. Files remain unchanged.</value>
</data>
<data name="Peek_SourceCode_TryFormat.Header" xml:space="preserve">
<value>Try to format the source for preview</value>
</data>
<data name="Peek_SourceCode_WrapText.Content" xml:space="preserve">
<value>Wrap text</value>
</data>
</root>

View File

@@ -9,6 +9,7 @@ using global::PowerToys.GPOWrapper;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
using Settings.UI.Library;
namespace Microsoft.PowerToys.Settings.UI.ViewModels
{
@@ -20,6 +21,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
private readonly ISettingsUtils _settingsUtils;
private readonly PeekSettings _peekSettings;
private readonly PeekPreviewSettings _peekPreviewSettings;
private GpoRuleConfigured _enabledGpoRuleConfiguration;
private bool _enabledStateIsGPOConfigured;
@@ -46,6 +48,15 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
_peekSettings = new PeekSettings();
}
if (_settingsUtils.SettingsExists(PeekSettings.ModuleName, PeekPreviewSettings.FileName))
{
_peekPreviewSettings = _settingsUtils.GetSettingsOrDefault<PeekPreviewSettings>(PeekSettings.ModuleName, PeekPreviewSettings.FileName);
}
else
{
_peekPreviewSettings = new PeekPreviewSettings();
}
InitializeEnabledValue();
SendConfigMSG = ipcMSGCallBackFunc;
@@ -137,15 +148,48 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
}
}
public bool SourceCodeWrapText
{
get => _peekPreviewSettings.SourceCodeWrapText.Value;
set
{
if (_peekPreviewSettings.SourceCodeWrapText.Value != value)
{
_peekPreviewSettings.SourceCodeWrapText.Value = value;
OnPropertyChanged(nameof(SourceCodeWrapText));
SavePreviewSettings();
}
}
}
public bool SourceCodeTryFormat
{
get => _peekPreviewSettings.SourceCodeTryFormat.Value;
set
{
if (_peekPreviewSettings.SourceCodeTryFormat.Value != value)
{
_peekPreviewSettings.SourceCodeTryFormat.Value = value;
OnPropertyChanged(nameof(SourceCodeTryFormat));
SavePreviewSettings();
}
}
}
private void NotifySettingsChanged()
{
// Using InvariantCulture as this is an IPC message
SendConfigMSG(
string.Format(
CultureInfo.InvariantCulture,
"{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
PeekSettings.ModuleName,
JsonSerializer.Serialize(_peekSettings)));
string.Format(
CultureInfo.InvariantCulture,
"{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
PeekSettings.ModuleName,
JsonSerializer.Serialize(_peekSettings)));
}
private void SavePreviewSettings()
{
_settingsUtils.SaveSettings(_peekPreviewSettings.ToJsonString(), PeekSettings.ModuleName, PeekPreviewSettings.FileName);
}
public void RefreshEnabledState()