Skip to content

Commit

Permalink
Add setting to enable/disable it and add Peek support
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron-Junker committed Apr 11, 2024
1 parent 9d5e56e commit 4d98eeb
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/common/FilePreviewCommon/Assets/Monaco/index.html
Expand Up @@ -15,6 +15,8 @@
var wrap = ([[PT_WRAP]] == 1) ? true : false;

var base64code = "[[PT_CODE]]";

var stickyScroll = ([[PT_STICKY_SCROLL]] == 1) ? true : false;

// Code taken from https://stackoverflow.com/a/30106551/14774889
var code = decodeURIComponent(atob(base64code).split('').map(function(c) {
Expand Down Expand Up @@ -77,6 +79,7 @@
horizontal: 'auto',

},
stickyScroll: {enabled: stickyScroll},
wordWrap: (wrap?"on":"off") // Word wraps
});
window.onresize = function (){
Expand Down
Expand Up @@ -9,5 +9,7 @@ public interface IPreviewSettings
public bool SourceCodeWrapText { get; }

public bool SourceCodeTryFormat { get; }

public bool SourceCodeStickyScroll { get; }
}
}
4 changes: 4 additions & 0 deletions src/modules/peek/Peek.FilePreviewer/Models/PreviewSettings.cs
Expand Up @@ -25,11 +25,14 @@ public class PreviewSettings : IPreviewSettings

public bool SourceCodeTryFormat { get; private set; }

public bool SourceCodeStickyScroll { get; private set; }

public PreviewSettings()
{
_settingsUtils = new SettingsUtils();
SourceCodeWrapText = false;
SourceCodeTryFormat = false;
SourceCodeStickyScroll = true;

LoadSettingsFromJson();

Expand Down Expand Up @@ -61,6 +64,7 @@ private void LoadSettingsFromJson()
{
SourceCodeWrapText = settings.SourceCodeWrapText.Value;
SourceCodeTryFormat = settings.SourceCodeTryFormat.Value;
SourceCodeStickyScroll = settings.SourceCodeStickyScroll.Value;
}

retry = false;
Expand Down
Expand Up @@ -45,13 +45,13 @@ public static HashSet<string> GetExtensions()
/// <summary>
/// Prepares temp html for the previewing
/// </summary>
public static string PreviewTempFile(string fileText, string extension, string tempFolder, bool tryFormat, bool wrapText)
public static string PreviewTempFile(string fileText, string extension, string tempFolder, bool tryFormat, bool wrapText, bool stickyScroll)
{
// TODO: check if file is too big, add MaxFileSize to settings
return InitializeIndexFileAndSelectedFile(fileText, extension, tempFolder, tryFormat, wrapText);
return InitializeIndexFileAndSelectedFile(fileText, extension, tempFolder, tryFormat, wrapText, stickyScroll);
}

private static string InitializeIndexFileAndSelectedFile(string fileContent, string extension, string tempFolder, bool tryFormat, bool wrapText)
private static string InitializeIndexFileAndSelectedFile(string fileContent, string extension, string tempFolder, bool tryFormat, bool wrapText, bool stickyScroll)
{
string vsCodeLangSet = Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.GetLanguage(extension);

Expand Down Expand Up @@ -79,6 +79,7 @@ private static string InitializeIndexFileAndSelectedFile(string fileContent, str

html = html.Replace("[[PT_LANG]]", vsCodeLangSet, StringComparison.InvariantCulture);
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_CODE]]", base64FileCode, StringComparison.InvariantCulture);
html = html.Replace("[[PT_URL]]", Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.VirtualHostName, StringComparison.InvariantCulture);
Expand Down
Expand Up @@ -112,7 +112,7 @@ public Task<bool> LoadDisplayInfoAsync(CancellationToken cancellationToken)
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));
Preview = new Uri(MonacoHelper.PreviewTempFile(raw, File.Extension, TempFolderPath.Path, _previewSettings.SourceCodeTryFormat, _previewSettings.SourceCodeWrapText, _previewSettings.SourceCodeStickyScroll));
}
else if (isMarkdown)
{
Expand Down
Expand Up @@ -398,6 +398,7 @@ private void InitializeIndexFileAndSelectedFile(string filePath)
_html = _html.Replace("[[PT_WRAP]]", _settings.Wrap ? "1" : "0", StringComparison.InvariantCulture);
_html = _html.Replace("[[PT_THEME]]", Settings.GetTheme(), 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);
}

Expand Down
20 changes: 20 additions & 0 deletions src/modules/previewpane/MonacoPreviewHandler/Settings.cs
Expand Up @@ -77,6 +77,26 @@ public double MaxFileSize
}
}

/// <summary>
/// Gets a value indicating whether sticky scroll should be enabled. Set by PT settings.
/// </summary>
public bool StickyScroll
{
get
{
try
{
return moduleSettings.GetSettings<PowerPreviewSettings>(PowerPreviewSettings.ModuleName).Properties.MonacoPreviewStickyScroll;
}
catch (FileNotFoundException)
{
// Couldn't read the settings.
// Assume default of true.
return true;
}
}
}

/// <summary>
/// Gets the color of the window background.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions src/settings-ui/Settings.UI.Library/PeekPreviewSettings.cs
Expand Up @@ -16,10 +16,13 @@ public class PeekPreviewSettings : ISettingsConfig

public BoolProperty SourceCodeTryFormat { get; set; }

public BoolProperty SourceCodeStickyScroll { get; set; }

public PeekPreviewSettings()
{
SourceCodeWrapText = new BoolProperty(false);
SourceCodeTryFormat = new BoolProperty(false);
SourceCodeStickyScroll = new BoolProperty(true);
}

public string ToJsonString()
Expand Down
17 changes: 17 additions & 0 deletions src/settings-ui/Settings.UI.Library/PowerPreviewProperties.cs
Expand Up @@ -133,6 +133,23 @@ public bool MonacoPreviewTryFormat
[JsonPropertyName("monaco-previewer-max-file-size")]
public IntProperty MonacoPreviewMaxFileSize { get; set; }

private bool monacoPreviewStickyScroll = true;

[JsonPropertyName("monaco-previewer-sticky-scroll")]
[JsonConverter(typeof(BoolPropertyJsonConverter))]
public bool MonacoPreviewStickyScroll
{
get => monacoPreviewStickyScroll;
set
{
if (value != monacoPreviewStickyScroll)
{
LogTelemetryEvent(value);
monacoPreviewStickyScroll = value;
}
}
}

private bool enablePdfPreview;

[JsonPropertyName("pdf-previewer-toggle-setting")]
Expand Down
3 changes: 3 additions & 0 deletions src/settings-ui/Settings.UI/SettingsXAML/Views/PeekPage.xaml
Expand Up @@ -55,6 +55,9 @@
IsChecked="{x:Bind ViewModel.SourceCodeTryFormat, Mode=TwoWay}"
IsEnabled="{x:Bind ViewModel.IsEnabled, Mode=OneWay}" />
</tkcontrols:SettingsCard>
<tkcontrols:SettingsCard ContentAlignment="Left" IsEnabled="{x:Bind ViewModel.IsEnabled, Mode=OneWay}">
<CheckBox x:Uid="Peek_SourceCode_StickyScroll" IsChecked="{x:Bind ViewModel.SourceCodeStickyScroll, Mode=TwoWay}" />
</tkcontrols:SettingsCard>
</tkcontrols:SettingsExpander.Items>
</tkcontrols:SettingsExpander>
</controls:SettingsGroup>
Expand Down
Expand Up @@ -99,6 +99,11 @@
SpinButtonPlacementMode="Compact"
Value="{x:Bind ViewModel.MonacoPreviewMaxFileSize, Mode=TwoWay}" />
</tkcontrols:SettingsCard>
<tkcontrols:SettingsCard ContentAlignment="Left" IsEnabled="{x:Bind ViewModel.MonacoRenderIsEnabled, Mode=OneWay}">
<CheckBox
x:Uid="FileExplorerPreview_ToggleSwitch_Monaco_Sticky_Scroll"
IsChecked="{x:Bind ViewModel.MonacoPreviewStickyScroll, Mode=TwoWay}" IsEnabled="{x:Bind ViewModel.MonacoRenderIsEnabled, Mode=OneWay}" />
</tkcontrols:SettingsCard>
</tkcontrols:SettingsExpander.Items>
</tkcontrols:SettingsExpander>

Expand Down
6 changes: 6 additions & 0 deletions src/settings-ui/Settings.UI/Strings/en-us/Resources.resw
Expand Up @@ -4056,4 +4056,10 @@ Activate by holding the key for the character you want to add an accent to, then
<data name="GPO_SomeThumbnailProvidersAreManaged.Title" xml:space="preserve">
<value>The enabled state of some thumbnail handlers is managed by your organization.</value>
</data>
<data name="FileExplorerPreview_ToggleSwitch_Monaco_Sticky_Scroll.Content" xml:space="preserve">
<value>Enable sticky scroll</value>
</data>
<data name="Peek_SourceCode_StickyScroll.Content" xml:space="preserve">
<value>Enable sticky scroll</value>
</data>
</root>
14 changes: 14 additions & 0 deletions src/settings-ui/Settings.UI/ViewModels/PeekViewModel.cs
Expand Up @@ -173,6 +173,20 @@ public bool SourceCodeTryFormat
}
}

public bool SourceCodeStickyScroll
{
get => _peekPreviewSettings.SourceCodeStickyScroll.Value;
set
{
if (_peekPreviewSettings.SourceCodeStickyScroll.Value != value)
{
_peekPreviewSettings.SourceCodeStickyScroll.Value = value;
OnPropertyChanged(nameof(SourceCodeStickyScroll));
SavePreviewSettings();
}
}
}

private void NotifySettingsChanged()
{
// Using InvariantCulture as this is an IPC message
Expand Down
20 changes: 20 additions & 0 deletions src/settings-ui/Settings.UI/ViewModels/PowerPreviewViewModel.cs
Expand Up @@ -92,6 +92,7 @@ public PowerPreviewViewModel(ISettingsRepository<PowerPreviewSettings> moduleSet
_monacoWrapText = Settings.Properties.EnableMonacoPreviewWordWrap;
_monacoPreviewTryFormat = Settings.Properties.MonacoPreviewTryFormat;
_monacoMaxFileSize = Settings.Properties.MonacoPreviewMaxFileSize.Value;
_monacoStickyScroll = Settings.Properties.MonacoPreviewStickyScroll;

_pdfRenderEnabledGpoRuleConfiguration = GPOWrapper.GetConfiguredPdfPreviewEnabledValue();
if (_pdfRenderEnabledGpoRuleConfiguration == GpoRuleConfigured.Disabled || _pdfRenderEnabledGpoRuleConfiguration == GpoRuleConfigured.Enabled)
Expand Down Expand Up @@ -231,6 +232,7 @@ public PowerPreviewViewModel(ISettingsRepository<PowerPreviewSettings> moduleSet
private bool _monacoWrapText;
private bool _monacoPreviewTryFormat;
private int _monacoMaxFileSize;
private bool _monacoStickyScroll;

private GpoRuleConfigured _pdfRenderEnabledGpoRuleConfiguration;
private bool _pdfRenderEnabledStateIsGPOConfigured;
Expand Down Expand Up @@ -595,6 +597,24 @@ public int MonacoPreviewMaxFileSize
}
}

public bool MonacoPreviewStickyScroll
{
get
{
return _monacoStickyScroll;
}

set
{
if (_monacoStickyScroll != value)
{
_monacoStickyScroll = value;
Settings.Properties.MonacoPreviewStickyScroll = value;
RaisePropertyChanged();
}
}
}

public bool PDFRenderIsEnabled
{
get
Expand Down

0 comments on commit 4d98eeb

Please sign in to comment.