Skip to content

Commit

Permalink
[Dev file previewer]Add option to set custom max file size and fix st…
Browse files Browse the repository at this point in the history
…yling issue (#23689)

* [Dev file previewer] Add option to set costum max file size and fix style issue
  • Loading branch information
Aaron-Junker committed Feb 13, 2023
1 parent bc4bde8 commit 00e10d3
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 4 deletions.
Expand Up @@ -212,6 +212,7 @@ public override void DoPreview<T>(T dataSource)
downloadLink.Top = TextRenderer.MeasureText(Resources.WebView2_Not_Installed_Message, errorMessage.Font).Height + 10;
downloadLink.Width = TextRenderer.MeasureText(Resources.Download_WebView2, errorMessage.Font).Width + 10;
downloadLink.Height = TextRenderer.MeasureText(Resources.Download_WebView2, errorMessage.Font).Height;
downloadLink.ForeColor = Settings.TextColor;
Controls.Add(downloadLink);
}
});
Expand All @@ -222,6 +223,7 @@ public override void DoPreview<T>(T dataSource)
Controls.Remove(_loadingBar);
Controls.Remove(_loadingBackground);
Label text = new Label();
text.ForeColor = Settings.TextColor;
text.Text = Resources.Exception_Occurred;
text.Text += e.Message;
text.Text += "\n" + e.Source;
Expand All @@ -244,6 +246,7 @@ public override void DoPreview<T>(T dataSource)
Controls.Remove(_loadingBackground);
Label errorMessage = new Label();
errorMessage.Text = Resources.Max_File_Size_Error.Replace("%1", (_settings.MaxFileSize / 1000).ToString(CultureInfo.CurrentCulture), StringComparison.InvariantCulture);
errorMessage.ForeColor = Settings.TextColor;
errorMessage.Width = 500;
errorMessage.Height = 50;
Controls.Add(errorMessage);
Expand Down
21 changes: 17 additions & 4 deletions src/modules/previewpane/MonacoPreviewHandler/Settings.cs
Expand Up @@ -58,11 +58,24 @@ public bool TryFormat
}

/// <summary>
/// Max file size for displaying (in bytes).
/// Gets Max file size for displaying (in bytes).
/// </summary>
private readonly long _maxFileSize = 50000;

public long MaxFileSize => _maxFileSize;
public double MaxFileSize
{
get
{
try
{
return moduleSettings.GetSettings<PowerPreviewSettings>(PowerPreviewSettings.ModuleName).Properties.MonacoPreviewMaxFileSize.Value * 1000;
}
catch (FileNotFoundException)
{
// Couldn't read the settings.
// Assume default of 50000.
return 50000;
}
}
}

/// <summary>
/// Gets the color of the window background.
Expand Down
5 changes: 5 additions & 0 deletions src/settings-ui/Settings.UI.Library/PowerPreviewProperties.cs
Expand Up @@ -13,6 +13,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
public class PowerPreviewProperties
{
public const string DefaultStlThumbnailColor = "#FFC924";
public const int DefaultMonacoMaxFileSize = 50;

private bool enableSvgPreview = true;

Expand Down Expand Up @@ -116,6 +117,9 @@ public bool MonacoPreviewTryFormat
}
}

[JsonPropertyName("monaco-previewer-max-file-size")]
public IntProperty MonacoPreviewMaxFileSize { get; set; }

private bool enablePdfPreview;

[JsonPropertyName("pdf-previewer-toggle-setting")]
Expand Down Expand Up @@ -207,6 +211,7 @@ public bool EnableStlThumbnail
public PowerPreviewProperties()
{
StlThumbnailColor = new StringProperty(DefaultStlThumbnailColor);
MonacoPreviewMaxFileSize = new IntProperty(DefaultMonacoMaxFileSize);
}

public override string ToString()
Expand Down
8 changes: 8 additions & 0 deletions src/settings-ui/Settings.UI/Strings/en-us/Resources.resw
Expand Up @@ -2934,4 +2934,12 @@ Activate by holding the key for the character you want to add an accent to, then
<data name="UpdateAvailable.Title" xml:space="preserve">
<value>Update available</value>
</data>
<data name="FileExplorerPreview_Toggle_Monaco_Max_File_Size.Header" xml:space="preserve">
<value>Maximum file size to preview</value>
<comment>Size refers to the disk space used by a file</comment>
</data>
<data name="FileExplorerPreview_Toggle_Monaco_Max_File_Size.Description" xml:space="preserve">
<value>The maximum size, in kilobytes, for files to be displayed. This is a safety mechanism to prevent loading large files into RAM.</value>
<comment>"RAM" refers to random access memory; "size" refers to disk space; "bytes" refer to the measurement unit</comment>
</data>
</root>
20 changes: 20 additions & 0 deletions src/settings-ui/Settings.UI/ViewModels/PowerPreviewViewModel.cs
Expand Up @@ -98,6 +98,7 @@ public PowerPreviewViewModel(ISettingsRepository<PowerPreviewSettings> moduleSet

_monacoWrapText = Settings.Properties.EnableMonacoPreviewWordWrap;
_monacoPreviewTryFormat = Settings.Properties.MonacoPreviewTryFormat;
_monacoMaxFileSize = Settings.Properties.MonacoPreviewMaxFileSize.Value;

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

private GpoRuleConfigured _pdfRenderEnabledGpoRuleConfiguration;
private bool _pdfRenderEnabledStateIsGPOConfigured;
Expand Down Expand Up @@ -353,6 +355,24 @@ public bool MonacoPreviewTryFormat
}
}

public int MonacoPreviewMaxFileSize
{
get
{
return _monacoMaxFileSize;
}

set
{
if (_monacoMaxFileSize != value)
{
_monacoMaxFileSize = value;
Settings.Properties.MonacoPreviewMaxFileSize.Value = value;
RaisePropertyChanged();
}
}
}

public bool PDFRenderIsEnabled
{
get
Expand Down
10 changes: 10 additions & 0 deletions src/settings-ui/Settings.UI/Views/PowerPreviewPage.xaml
Expand Up @@ -78,6 +78,16 @@
IsChecked="{x:Bind ViewModel.MonacoPreviewTryFormat, Mode=TwoWay}"
IsEnabled="{x:Bind ViewModel.MonacoRenderIsEnabled, Mode=OneWay}" />
</labs:SettingsCard>
<labs:SettingsCard
x:Uid="FileExplorerPreview_Toggle_Monaco_Max_File_Size"
IsEnabled="{x:Bind Mode=OneWay, Path=ViewModel.MonacoRenderIsEnabled}">
<NumberBox
MinWidth="{StaticResource SettingActionControlMinWidth}"
Maximum="100"
Minimum="2"
SpinButtonPlacementMode="Compact"
Value="{x:Bind Mode=TwoWay, Path=ViewModel.MonacoPreviewMaxFileSize}" />
</labs:SettingsCard>
</labs:SettingsExpander.Items>
</labs:SettingsExpander>

Expand Down

0 comments on commit 00e10d3

Please sign in to comment.