Skip to content

Commit

Permalink
[PTRun]Add setting to disable thumbnails (microsoft#24600)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaimecbernardo authored Mar 6, 2023
1 parent 07029ff commit 6537820
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 22 deletions.
5 changes: 5 additions & 0 deletions src/modules/launcher/PowerLauncher/SettingsReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ public void ReadSettings()
_settings.StartupPosition = overloadSettings.Properties.Position;
}

if (_settings.GenerateThumbnailsFromFiles != overloadSettings.Properties.GenerateThumbnailsFromFiles)
{
_settings.GenerateThumbnailsFromFiles = overloadSettings.Properties.GenerateThumbnailsFromFiles;
}

retry = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using PowerLauncher.Helper;
using PowerLauncher.Plugin;
using Wox.Infrastructure.Image;
using Wox.Infrastructure.UserSettings;
using Wox.Plugin;
using Wox.Plugin.Logger;

Expand All @@ -23,6 +24,8 @@ public enum ActivationType
Hover,
}

private readonly PowerToysRunSettings _settings;

public ObservableCollection<ContextMenuItemViewModel> ContextMenuItems { get; } = new ObservableCollection<ContextMenuItemViewModel>();

public ICommand ActivateContextButtonsHoverCommand { get; }
Expand Down Expand Up @@ -65,13 +68,15 @@ public int ContextMenuSelectedIndex

public const int NoSelectionIndex = -1;

public ResultViewModel(Result result, IMainViewModel mainViewModel)
public ResultViewModel(Result result, IMainViewModel mainViewModel, PowerToysRunSettings settings)
{
if (result != null)
{
Result = result;
}

_settings = settings;

ContextMenuSelectedIndex = NoSelectionIndex;
LoadContextMenu();

Expand Down Expand Up @@ -201,7 +206,7 @@ public ImageSource Image
}

// will get here either when icoPath has value\icon delegate is null\when had exception in delegate
return ImageLoader.Load(imagePath);
return ImageLoader.Load(imagePath, _settings.GenerateThumbnailsFromFiles);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public void AddResults(List<Result> newRawResults, CancellationToken ct)
List<ResultViewModel> newResults = new List<ResultViewModel>(newRawResults.Count);
foreach (Result r in newRawResults)
{
newResults.Add(new ResultViewModel(r, _mainViewModel));
newResults.Add(new ResultViewModel(r, _mainViewModel, _settings));
ct.ThrowIfCancellationRequested();
}

Expand Down
32 changes: 21 additions & 11 deletions src/modules/launcher/Wox.Infrastructure/Image/ImageLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Windows.Media;
using System.Windows.Media.Imaging;
using ManagedCommon;
using Wox.Infrastructure.UserSettings;
using Wox.Plugin;
using Wox.Plugin.Logger;

Expand Down Expand Up @@ -67,7 +68,7 @@ public static void Initialize(Theme theme)
{
ImageCache.Usage.AsParallel().ForAll(x =>
{
Load(x.Key);
Load(x.Key, true);
});
});
Expand Down Expand Up @@ -119,7 +120,7 @@ private enum ImageType
Cache,
}

private static ImageResult LoadInternal(string path, bool loadFullImage = false)
private static ImageResult LoadInternal(string path, bool generateThumbnailsFromFiles, bool loadFullImage = false)
{
ImageSource image;
ImageType type = ImageType.Error;
Expand Down Expand Up @@ -172,18 +173,27 @@ private static ImageResult LoadInternal(string path, bool loadFullImage = false)
}
else
{
/* Although the documentation for GetImage on MSDN indicates that
* if a thumbnail is available it will return one, this has proved to not
* be the case in many situations while testing.
* - Solution: explicitly pass the ThumbnailOnly flag
*/
image = WindowsThumbnailProvider.GetThumbnail(path, Constant.ThumbnailSize, Constant.ThumbnailSize, ThumbnailOptions.ThumbnailOnly);
// PowerToys Run internal images are png, so we make this exception
if (extension == ".png" || generateThumbnailsFromFiles)
{
/* Although the documentation for GetImage on MSDN indicates that
* if a thumbnail is available it will return one, this has proved to not
* be the case in many situations while testing.
* - Solution: explicitly pass the ThumbnailOnly flag
*/
image = WindowsThumbnailProvider.GetThumbnail(path, Constant.ThumbnailSize, Constant.ThumbnailSize, ThumbnailOptions.ThumbnailOnly);
}
else
{
image = WindowsThumbnailProvider.GetThumbnail(path, Constant.ThumbnailSize, Constant.ThumbnailSize, ThumbnailOptions.IconOnly);
}
}
}
else if (extension == ".pdf" && WindowsThumbnailProvider.DoesPdfUseAcrobatAsProvider())
else if (!generateThumbnailsFromFiles || (extension == ".pdf" && WindowsThumbnailProvider.DoesPdfUseAcrobatAsProvider()))
{
// The PDF thumbnail provider from Adobe Reader and Acrobat Pro lets crash PT Run with an Dispatcher exception. (https://github.com/microsoft/PowerToys/issues/18166)
// To not run into the crash, we only request the icon of PDF files if the PDF thumbnail handler is set to Adobe Reader/Acrobat Pro.
// Also don't get thumbnail if the GenerateThumbnailsFromFiles option is off.
type = ImageType.File;
image = WindowsThumbnailProvider.GetThumbnail(path, Constant.ThumbnailSize, Constant.ThumbnailSize, ThumbnailOptions.IconOnly);
}
Expand Down Expand Up @@ -217,9 +227,9 @@ private static ImageResult LoadInternal(string path, bool loadFullImage = false)

private const bool _enableImageHash = true;

public static ImageSource Load(string path, bool loadFullImage = false)
public static ImageSource Load(string path, bool generateThumbnailsFromFiles, bool loadFullImage = false)
{
var imageResult = LoadInternal(path, loadFullImage);
var imageResult = LoadInternal(path, generateThumbnailsFromFiles, loadFullImage);

var img = imageResult.ImageSource;
if (imageResult.ImageType != ImageType.Error && imageResult.ImageType != ImageType.Cache)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ public bool HideNotifyIcon

public bool StartedFromPowerToysRunner { get; set; }

public bool GenerateThumbnailsFromFiles { get; set; } = true;

public HttpProxy Proxy { get; set; } = new HttpProxy();

[JsonConverter(typeof(JsonStringEnumConverter))]
Expand Down
2 changes: 1 addition & 1 deletion src/modules/launcher/Wox.Test/ResultViewModelTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void Setup()
var result = new Result();
contextMenuResult = new ContextMenuResult();
mainViewModelMock = new Mock<IMainViewModel>();
resultViewModel = new ResultViewModel(result, mainViewModelMock.Object);
resultViewModel = new ResultViewModel(result, mainViewModelMock.Object, null);

var pluginMock = new Mock<IPlugin>();
pluginMock.As<IContextMenu>().Setup(x => x.LoadContextMenus(result)).Returns(new List<ContextMenuResult> { contextMenuResult });
Expand Down
14 changes: 7 additions & 7 deletions src/modules/launcher/Wox.Test/ResultsViewModelTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void ContextMenuSelectedIndexShouldEqualNoSelectionIndexWhenInitialized()
// Arrange
ResultsViewModel rvm = new ResultsViewModel();
Result result = new Result();
ResultViewModel selectedItem = new ResultViewModel(result, null);
ResultViewModel selectedItem = new ResultViewModel(result, null, null);
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel(null, null, null, null, Key.None, ModifierKeys.None, null));
rvm.SelectedItem = selectedItem;

Expand All @@ -32,7 +32,7 @@ public void SelectNextContextMenuItemIncrementsContextMenuSelectedIndexWhenCalle
// Arrange
ResultsViewModel rvm = new ResultsViewModel();
Result result = new Result();
ResultViewModel selectedItem = new ResultViewModel(result, null);
ResultViewModel selectedItem = new ResultViewModel(result, null, null);
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel(null, null, null, null, Key.None, ModifierKeys.None, null));
rvm.SelectedItem = selectedItem;

Expand All @@ -49,7 +49,7 @@ public void SelectNextContextMenuItemDoesnNotIncrementContextMenuSelectedIndexWh
// Arrange
ResultsViewModel rvm = new ResultsViewModel();
Result result = new Result();
ResultViewModel selectedItem = new ResultViewModel(result, null);
ResultViewModel selectedItem = new ResultViewModel(result, null, null);
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel(null, null, null, null, Key.None, ModifierKeys.None, null));
rvm.SelectedItem = selectedItem;

Expand All @@ -66,7 +66,7 @@ public void SelectPreviousContextMenuItemDecrementsContextMenuSelectedIndexWhenC
// Arrange
ResultsViewModel rvm = new ResultsViewModel();
Result result = new Result();
ResultViewModel selectedItem = new ResultViewModel(result, null);
ResultViewModel selectedItem = new ResultViewModel(result, null, null);
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel(null, null, null, null, Key.None, ModifierKeys.None, null));
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel(null, null, null, null, Key.None, ModifierKeys.None, null));
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel(null, null, null, null, Key.None, ModifierKeys.None, null));
Expand All @@ -88,7 +88,7 @@ public void SelectPreviousContextMenuItemResetsContextMenuSelectedIndexWhenCalle
// Arrange
ResultsViewModel rvm = new ResultsViewModel();
Result result = new Result();
ResultViewModel selectedItem = new ResultViewModel(result, null);
ResultViewModel selectedItem = new ResultViewModel(result, null, null);
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel(null, null, null, null, Key.None, ModifierKeys.None, null));
rvm.SelectedItem = selectedItem;

Expand All @@ -106,7 +106,7 @@ public void IsContextMenuItemSelectedReturnsTrueWhenContextMenuItemIsSelected()
// Arrange
ResultsViewModel rvm = new ResultsViewModel();
Result result = new Result();
ResultViewModel selectedItem = new ResultViewModel(result, null);
ResultViewModel selectedItem = new ResultViewModel(result, null, null);
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel(null, null, null, null, Key.None, ModifierKeys.None, null));
rvm.SelectedItem = selectedItem;

Expand All @@ -124,7 +124,7 @@ public void IsContextMenuItemSelectedReturnsFalseWhenContextMenuItemIsNotSelecte
// Arrange
ResultsViewModel rvm = new ResultsViewModel();
Result result = new Result();
ResultViewModel selectedItem = new ResultViewModel(result, null);
ResultViewModel selectedItem = new ResultViewModel(result, null, null);
selectedItem.ContextMenuItems.Add(new ContextMenuItemViewModel(null, null, null, null, Key.None, ModifierKeys.None, null));
rvm.SelectedItem = selectedItem;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public class PowerLauncherProperties
[JsonPropertyName("search_wait_for_slow_results")]
public bool SearchWaitForSlowResults { get; set; }

[JsonPropertyName("generate_thumbnails_from_files")]
public bool GenerateThumbnailsFromFiles { get; set; }

public PowerLauncherProperties()
{
OpenPowerLauncher = new HotkeySettings(false, false, true, false, 32);
Expand All @@ -92,6 +95,7 @@ public PowerLauncherProperties()
SearchClickedItemWeight = 5;
SearchQueryTuningEnabled = false;
SearchWaitForSlowResults = false;
GenerateThumbnailsFromFiles = true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public void OriginalFilesModificationTest(string version, string fileName)
Assert.AreEqual(originalSettings.Properties.OverrideWinkeyS, viewModel.OverrideWinSKey);
Assert.AreEqual(originalSettings.Properties.SearchResultPreference, viewModel.SearchResultPreference);
Assert.AreEqual(originalSettings.Properties.SearchTypePreference, viewModel.SearchTypePreference);
Assert.AreEqual(originalSettings.Properties.GenerateThumbnailsFromFiles, viewModel.GenerateThumbnailsFromFiles);

// Verify that the stub file was used
var expectedCallCount = 2; // once via the view model, and once by the test (GetSettings<T>)
Expand Down
6 changes: 6 additions & 0 deletions src/settings-ui/Settings.UI/Strings/en-us/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,12 @@
<data name="PowerLauncher_TabSelectsContextButtons.Description" xml:space="preserve">
<value>Pressing tab will first select through the available context buttons of the current selection before moving onto the next result</value>
</data>
<data name="PowerLauncher_GenerateThumbnailsFromFiles.Header" xml:space="preserve">
<value>Generate thumbnails from files</value>
</data>
<data name="PowerLauncher_GenerateThumbnailsFromFiles.Description" xml:space="preserve">
<value>Results will try to generate thumbnails for files. Disabling this setting may increase stability and speed</value>
</data>
<data name="PowerLauncher_SearchQueryResultsWithDelay.Header" xml:space="preserve">
<value>Input Smoothing</value>
<comment>This is about adding a delay to wait for more input before executing a search</comment>
Expand Down
17 changes: 17 additions & 0 deletions src/settings-ui/Settings.UI/ViewModels/PowerLauncherViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,23 @@ public bool TabSelectsContextButtons
}
}

public bool GenerateThumbnailsFromFiles
{
get
{
return settings.Properties.GenerateThumbnailsFromFiles;
}

set
{
if (settings.Properties.GenerateThumbnailsFromFiles != value)
{
settings.Properties.GenerateThumbnailsFromFiles = value;
UpdateSettings();
}
}
}

private ObservableCollection<PowerLauncherPluginViewModel> _plugins;

public ObservableCollection<PowerLauncherPluginViewModel> Plugins
Expand Down
8 changes: 8 additions & 0 deletions src/settings-ui/Settings.UI/Views/PowerLauncherPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@
IsOn="{x:Bind ViewModel.TabSelectsContextButtons, Mode=TwoWay}" />
</labs:SettingsCard>

<labs:SettingsCard
x:Uid="PowerLauncher_GenerateThumbnailsFromFiles"
>
<ToggleSwitch
x:Uid="ToggleSwitch"
IsOn="{x:Bind ViewModel.GenerateThumbnailsFromFiles, Mode=TwoWay}" />
</labs:SettingsCard>

</controls:SettingsGroup>

<!--<ComboBox x:Uid="PowerLauncher_SearchResultPreference"
Expand Down

0 comments on commit 6537820

Please sign in to comment.