Detect if all Drives are indexed and show a warning if they aren't (#5015)

* opens new settings search options

* Catching file not found exception

* removed unnecessary header files

* Added display strings and cleaned up code

* reduced the number of max results to 30

* added log statement for exception

* Added drive detection to settings ui but still doesn't reflect on toggling it

* added getter setter for DriveDetectionWarning

* Got UI and backend to work as expected

* Reading value from registry working as expected

* Added test for settings

* Added tests for drive detection

* rename drive detection

* Localized indexer string

* formatting

* resolving merge conflict

* Added theme aware warning icon

* changed text for the warning

* Added the warning images to the installer
This commit is contained in:
Alekhya 2020-07-20 17:50:42 -07:00 committed by GitHub
parent c85cd4ac24
commit 96cd135f82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 289 additions and 68 deletions

View file

@ -954,7 +954,7 @@
<?endforeach?>
</Component>
<Component Id="IndexerImagesComponent" Directory="IndexerImagesFolder" Guid="DB2E8D49-D104-425B-9616-952AC8CAB676">
<?foreach File in indexer.dark.png;indexer.light.png?>
<?foreach File in indexer.dark.png;indexer.light.png;Warning.light.png;Warning.dark.png?>
<File Id="Indexer_$(var.File)" Source="$(var.BinX64Dir)modules\launcher\Plugins\Microsoft.Plugin.Indexer\Images\$(var.File)" />
<?endforeach?>
</Component>

View file

@ -26,6 +26,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib
public bool ignore_hotkeys_in_fullscreen { get; set; }
public bool disable_drive_detection_warning { get; set; }
public bool clear_input_on_launch { get; set; }
public PowerLauncherProperties()
@ -37,6 +38,7 @@ namespace Microsoft.PowerToys.Settings.UI.Lib
search_result_preference = "most_recently_used";
search_type_preference = "application_name";
ignore_hotkeys_in_fullscreen = false;
disable_drive_detection_warning = false;
clear_input_on_launch = false;
}
}

View file

@ -272,6 +272,9 @@
<data name="PowerLauncher_IgnoreHotkeysInFullScreen.Content" xml:space="preserve">
<value>Ignore hotkeys in fullscreen mode</value>
</data>
<data name="PowerLauncher_DisableDriveDetectionWarning.Content" xml:space="preserve">
<value>Disable drive detection warning for the indexer plugin</value>
</data>
<data name="PowerLauncher_ClearInputOnLaunch.Content" xml:space="preserve">
<value>Clear the previous query on launch</value>
</data>

View file

@ -252,5 +252,22 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
}
}
}
public bool DisableDriveDetectionWarning
{
get
{
return settings.properties.disable_drive_detection_warning;
}
set
{
if (settings.properties.disable_drive_detection_warning != value)
{
settings.properties.disable_drive_detection_warning = value;
UpdateSettings();
}
}
}
}
}

View file

@ -1,4 +1,4 @@
<Page
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Microsoft.PowerToys.Settings.UI.Views"
@ -10,7 +10,7 @@
xmlns:CustomControls="using:Microsoft.PowerToys.Settings.UI.Controls"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid ColumnSpacing="{StaticResource DefaultColumnSpacing}" RowSpacing="{StaticResource DefaultRowSpacing}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="LayoutVisualStates">
@ -141,6 +141,13 @@
IsChecked="{x:Bind Mode=TwoWay, Path=ViewModel.ClearInputOnLaunch}"
IsEnabled="{x:Bind Mode=OneWay, Path=ViewModel.EnablePowerLauncher}"
/>
<CheckBox x:Uid="PowerLauncher_DisableDriveDetectionWarning"
Margin="{StaticResource SmallTopMargin}"
IsChecked="{x:Bind Mode=TwoWay, Path=ViewModel.DisableDriveDetectionWarning}"
IsEnabled="{x:Bind Mode=OneWay, Path=ViewModel.EnablePowerLauncher}"
/>
</StackPanel>
<StackPanel
x:Name="SidePanel"

View file

@ -131,6 +131,15 @@ namespace ViewModelTests
Assert.IsFalse(mockSettings.properties.override_win_s_key);
}
[TestMethod]
public void DriveDetectionViewModel_WhenSet_MustUpdateOverrides()
{
// Act
viewModel.DisableDriveDetectionWarning = true;
// Assert
Assert.AreEqual(1, sendCallbackMock.TimesSent);
Assert.IsTrue(mockSettings.properties.disable_drive_detection_warning);
}
}
}

View file

@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Plugin.Indexer.DriveDetection;
using Microsoft.Plugin.Indexer.Interface;
namespace Microsoft.Plugin.Indexer.DriveDetection
{
public class IndexerDriveDetection
{
private bool IsEnhancedModeEnabled { get; set; } = false;
private IRegistryWrapper _registryHelper;
public bool IsDriveDetectionWarningCheckBoxSelected { get; set; } = false;
public IndexerDriveDetection(IRegistryWrapper registryHelper)
{
_registryHelper = registryHelper;
GetEnhancedModeStatus();
}
// To display the results if either enhanced mode is on or if the disable drive detection checkbox is checked
public bool DisplayResults()
{
return IsDriveDetectionWarningCheckBoxSelected || IsEnhancedModeEnabled;
}
// To look up the registry entry for
private void GetEnhancedModeStatus()
{
string registryLocation = @"Software\Microsoft\Windows Search\Gather\Windows\SystemIndex";
string valueName = "EnableFindMyFiles";
IsEnhancedModeEnabled = _registryHelper.GetHKLMRegistryValue(registryLocation, valueName) == 0 ? false : true;
}
}
}

View file

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;
using Microsoft.Plugin.Indexer.Interface;
using Microsoft.Win32;
namespace Microsoft.Plugin.Indexer.DriveDetection
{
public class RegistryWrapper : IRegistryWrapper
{
// Given the registrypath and the name of the value, to retrieve the data corresponding to that registry key
public int GetHKLMRegistryValue(string registryLocation, string valueName)
{
using (RegistryKey regKey = Registry.LocalMachine.OpenSubKey(registryLocation))
{
if(regKey != null)
{
Object value = regKey.GetValue(valueName);
if(value != null)
{
return (int)value;
}
}
}
return 0;
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 566 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 580 B

View file

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Microsoft.Plugin.Indexer.Interface
{
public interface IRegistryWrapper
{
int GetHKLMRegistryValue(string registryLocation, string valueName);
}
}

View file

@ -8,5 +8,7 @@
<system:String x:Key="Microsoft_plugin_indexer_open_in_console">Open path in console (Ctrl+Shift+C)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_name">Name</system:String>
<system:String x:Key="Microsoft_plugin_indexer_path">Path</system:String>
<system:String x:Key="Microsoft_plugin_indexer_drivedetectionwarning">Warning: Detected that not all drives are indexed.</system:String>
<system:String x:Key="Microsoft_plugin_indexer_disable_warning_in_settings">Go to Windows Search settings to fix.</system:String>
</ResourceDictionary>

View file

@ -8,5 +8,7 @@
<system:String x:Key="Microsoft_plugin_indexer_open_in_console">Open path in console (Ctrl+Shift+C)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_name">Name</system:String>
<system:String x:Key="Microsoft_plugin_indexer_path">Path</system:String>
<system:String x:Key="Microsoft_plugin_indexer_drivedetectionwarning">Warning: Detected that not all drives are indexed.</system:String>
<system:String x:Key="Microsoft_plugin_indexer_disable_warning_in_settings">Go to Windows Search settings to fix.</system:String>
</ResourceDictionary>

View file

@ -8,5 +8,7 @@
<system:String x:Key="Microsoft_plugin_indexer_open_in_console">Open path in console (Ctrl+Shift+C)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_name">Name</system:String>
<system:String x:Key="Microsoft_plugin_indexer_path">Path</system:String>
<system:String x:Key="Microsoft_plugin_indexer_drivedetectionwarning">Warning: Detected that not all drives are indexed.</system:String>
<system:String x:Key="Microsoft_plugin_indexer_disable_warning_in_settings">Go to Windows Search settings to fix.</system:String>
</ResourceDictionary>

View file

@ -8,5 +8,7 @@
<system:String x:Key="Microsoft_plugin_indexer_open_in_console">Open path in console (Ctrl+Shift+C)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_name">Name</system:String>
<system:String x:Key="Microsoft_plugin_indexer_path">Path</system:String>
<system:String x:Key="Microsoft_plugin_indexer_drivedetectionwarning">Warning: Detected that not all drives are indexed.</system:String>
<system:String x:Key="Microsoft_plugin_indexer_disable_warning_in_settings">Go to Windows Search settings to fix.</system:String>
</ResourceDictionary>

View file

@ -8,5 +8,7 @@
<system:String x:Key="Microsoft_plugin_indexer_open_in_console">Open path in console (Ctrl+Shift+C)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_name">Name</system:String>
<system:String x:Key="Microsoft_plugin_indexer_path">Path</system:String>
<system:String x:Key="Microsoft_plugin_indexer_drivedetectionwarning">Warning: Detected that not all drives are indexed.</system:String>
<system:String x:Key="Microsoft_plugin_indexer_disable_warning_in_settings">Go to Windows Search settings to fix.</system:String>
</ResourceDictionary>

View file

@ -8,5 +8,7 @@
<system:String x:Key="Microsoft_plugin_indexer_open_in_console">Open path in console (Ctrl+Shift+C)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_name">Name</system:String>
<system:String x:Key="Microsoft_plugin_indexer_path">Path</system:String>
<system:String x:Key="Microsoft_plugin_indexer_drivedetectionwarning">Warning: Detected that not all drives are indexed.</system:String>
<system:String x:Key="Microsoft_plugin_indexer_disable_warning_in_settings">Go to Windows Search settings to fix.</system:String>
</ResourceDictionary>

View file

@ -8,5 +8,7 @@
<system:String x:Key="Microsoft_plugin_indexer_open_in_console">Open path in console (Ctrl+Shift+C)</system:String>
<system:String x:Key="Microsoft_plugin_indexer_name">Name</system:String>
<system:String x:Key="Microsoft_plugin_indexer_path">Path</system:String>
<system:String x:Key="Microsoft_plugin_indexer_drivedetectionwarning">Warning: Detected that not all drives are indexed.</system:String>
<system:String x:Key="Microsoft_plugin_indexer_disable_warning_in_settings">Go to Windows Search settings to fix.</system:String>
</ResourceDictionary>

View file

@ -14,6 +14,7 @@ using Microsoft.PowerToys.Settings.UI.Lib;
using System.Windows.Controls;
using Wox.Infrastructure.Logger;
using System.Text.RegularExpressions;
using Microsoft.Plugin.Indexer.DriveDetection;
namespace Microsoft.Plugin.Indexer
{
@ -32,9 +33,12 @@ namespace Microsoft.Plugin.Indexer
// To access Windows Search functionalities
private readonly WindowsSearchAPI _api = new WindowsSearchAPI(new OleDBSearch());
// To obtain information regarding the drives that are indexed
private readonly IndexerDriveDetection _driveDetection = new IndexerDriveDetection(new RegistryWrapper());
// Reserved keywords in oleDB
private string ReservedStringPattern = @"^[\/\\\$\%]+$";
private string WarningIconPath { get; set; }
private IContextMenu _contextMenuLoader;
// To save the configurations of plugins
@ -47,78 +51,103 @@ namespace Microsoft.Plugin.Indexer
public List<Result> Query(Query query)
{
var results = new List<Result>();
if (!string.IsNullOrEmpty(query.Search))
{
var searchQuery = query.Search;
if (_settings.MaxSearchCount <= 0)
{
_settings.MaxSearchCount = 50;
}
var regexMatch = Regex.Match(searchQuery, ReservedStringPattern);
if (!regexMatch.Success)
if (_driveDetection.DisplayResults())
{
if (!string.IsNullOrEmpty(query.Search))
{
try
var searchQuery = query.Search;
if (_settings.MaxSearchCount <= 0)
{
var searchResultsList = _api.Search(searchQuery, maxCount: _settings.MaxSearchCount).ToList();
foreach (var searchResult in searchResultsList)
_settings.MaxSearchCount = 50;
}
var regexMatch = Regex.Match(searchQuery, ReservedStringPattern);
if (!regexMatch.Success)
{
try
{
var path = searchResult.Path;
var toolTipTitle = string.Format("{0} : {1}", _context.API.GetTranslation("Microsoft_plugin_indexer_name"), searchResult.Title);
var toolTipText = string.Format("{0} : {1}", _context.API.GetTranslation("Microsoft_plugin_indexer_path"), path);
string workingDir = null;
if (_settings.UseLocationAsWorkingDir)
workingDir = Path.GetDirectoryName(path);
Result r = new Result();
r.Title = searchResult.Title;
r.SubTitle = "Search: " + path;
r.IcoPath = path;
r.ToolTipData = new ToolTipData(toolTipTitle, toolTipText);
r.Action = c =>
var searchResultsList = _api.Search(searchQuery, maxCount: _settings.MaxSearchCount).ToList();
foreach (var searchResult in searchResultsList)
{
bool hide;
try
var path = searchResult.Path;
var toolTipTitle = string.Format("{0} : {1}", _context.API.GetTranslation("Microsoft_plugin_indexer_name"), searchResult.Title);
var toolTipText = string.Format("{0} : {1}", _context.API.GetTranslation("Microsoft_plugin_indexer_path"), path);
string workingDir = null;
if (_settings.UseLocationAsWorkingDir)
workingDir = Path.GetDirectoryName(path);
Result r = new Result();
r.Title = searchResult.Title;
r.SubTitle = "Search: " + path;
r.IcoPath = path;
r.ToolTipData = new ToolTipData(toolTipTitle, toolTipText);
r.Action = c =>
{
Process.Start(new ProcessStartInfo
bool hide;
try
{
FileName = path,
UseShellExecute = true,
WorkingDirectory = workingDir
});
hide = true;
}
catch (Win32Exception)
Process.Start(new ProcessStartInfo
{
FileName = path,
UseShellExecute = true,
WorkingDirectory = workingDir
});
hide = true;
}
catch (Win32Exception)
{
var name = $"Plugin: {_context.CurrentPluginMetadata.Name}";
var msg = "Can't Open this file";
_context.API.ShowMsg(name, msg, string.Empty);
hide = false;
}
return hide;
};
r.ContextData = searchResult;
//If the result is a directory, then it's display should show a directory.
if (Directory.Exists(path))
{
var name = $"Plugin: {_context.CurrentPluginMetadata.Name}";
var msg = "Can't Open this file";
_context.API.ShowMsg(name, msg, string.Empty);
hide = false;
r.QueryTextDisplay = path;
}
return hide;
};
r.ContextData = searchResult;
//If the result is a directory, then it's display should show a directory.
if (Directory.Exists(path))
{
r.QueryTextDisplay = path;
results.Add(r);
}
results.Add(r);
}
catch (InvalidOperationException)
{
//The connection has closed, internal error of ExecuteReader()
//Not showing this exception to the users
}
catch (Exception ex)
{
Log.Info(ex.ToString());
}
}
catch (InvalidOperationException)
}
}
else
{
results.Add(new Result
{
Title = _context.API.GetTranslation("Microsoft_plugin_indexer_drivedetectionwarning"),
SubTitle = _context.API.GetTranslation("Microsoft_plugin_indexer_disable_warning_in_settings"),
IcoPath = WarningIconPath,
Action = e =>
{
//The connection has closed, internal error of ExecuteReader()
//Not showing this exception to the users
try
{
Process.Start(GetWindowsSearchSettingsProcessInfo());
}
catch (Exception ex)
{
Log.Exception("Microsoft.Plugin.Indexer", $"Unable to launch Windows Search Settings: {ex.Message}", ex, "Query");
}
return true;
}
catch (Exception ex)
{
Log.Info(ex.ToString());
}
}
});
}
return results;
@ -131,6 +160,26 @@ namespace Microsoft.Plugin.Indexer
_contextMenuLoader = new ContextMenuLoader(context);
_storage = new PluginJsonStorage<Settings>();
_settings = _storage.Load();
_context.API.ThemeChanged += OnThemeChanged;
UpdateIconPath(_context.API.GetCurrentTheme());
}
// Todo : Update with theme based IconPath
private void UpdateIconPath(Theme theme)
{
if (theme == Theme.Light || theme == Theme.HighContrastWhite)
{
WarningIconPath = "Images/Warning.light.png";
}
else
{
WarningIconPath = "Images/Warning.dark.png";
}
}
private void OnThemeChanged(Theme _, Theme newTheme)
{
UpdateIconPath(newTheme);
}
// TODO: Localize the strings
@ -154,10 +203,23 @@ namespace Microsoft.Plugin.Indexer
public void UpdateSettings(PowerLauncherSettings settings)
{
_settings.MaxSearchCount = settings.properties.maximum_number_of_results;
_driveDetection.IsDriveDetectionWarningCheckBoxSelected = settings.properties.disable_drive_detection_warning;
}
public Control CreateSettingPanel()
{
throw new NotImplementedException();
}
// Returns the Process Start Information for the new Windows Search Settings
public ProcessStartInfo GetWindowsSearchSettingsProcessInfo()
{
var ps = new ProcessStartInfo("ms-settings:cortana-windowssearch")
{
UseShellExecute = true,
Verb = "open"
};
return ps;
}
}

View file

@ -56,6 +56,12 @@
<None Update="Images\indexer.light.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Images\Warning.dark.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Images\Warning.light.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="plugin.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>

View file

@ -103,7 +103,7 @@ namespace Microsoft.Plugin.Indexer.SearchHelper
queryHelper.QuerySorting = "System.DateModified DESC";
}
public IEnumerable<SearchResult> Search(string keyword, string pattern = "*", int maxCount = 100)
public IEnumerable<SearchResult> Search(string keyword, string pattern = "*", int maxCount = 30)
{
lock(_lock){
ISearchQueryHelper queryHelper;

View file

@ -66,6 +66,13 @@ namespace PowerLauncher
_settings.IgnoreHotkeysOnFullscreen = overloadSettings.properties.ignore_hotkeys_in_fullscreen;
}
var indexer = PluginManager.AllPlugins.Find(p => p.Metadata.Name.Equals("Windows Indexer Plugin", StringComparison.OrdinalIgnoreCase));
if (indexer != null)
{
var indexerSettings = indexer.Plugin as ISettingProvider;
indexerSettings.UpdateSettings(overloadSettings);
}
if (_settings.ClearInputOnLaunch != overloadSettings.properties.clear_input_on_launch)
{
_settings.ClearInputOnLaunch = overloadSettings.properties.clear_input_on_launch;

View file

@ -8,6 +8,7 @@ using Microsoft.Plugin.Indexer;
using Moq;
using Wox.Plugin;
using System.Linq;
using Microsoft.Plugin.Indexer.DriveDetection;
namespace Wox.Test.Plugins
{
@ -297,5 +298,23 @@ namespace Wox.Test.Plugins
mockapi.Verify(m => m.GetTranslation("Microsoft_plugin_indexer_open_containing_folder"), Times.Never());
mockapi.Verify(m => m.GetTranslation("Microsoft_plugin_indexer_open_in_console"), Times.Once());
}
[TestCase(0, false, ExpectedResult = false)]
[TestCase(0, true, ExpectedResult = true)]
[TestCase(1, false, ExpectedResult = true)]
[TestCase(1, true, ExpectedResult = true)]
public bool DriveDetection_MustDisplayResults_WhenEnhancedModeIsOnOrWhenWarningIsDisabled(int enhancedModeStatus, bool disableWarningCheckBoxStatus)
{
// Arrange
var mockRegistry = new Mock<IRegistryWrapper>();
mockRegistry.Setup(r => r.GetHKLMRegistryValue(It.IsAny<string>(), It.IsAny<string>())).Returns(enhancedModeStatus); // Enhanced mode is disabled
IndexerDriveDetection _driveDetection = new IndexerDriveDetection(mockRegistry.Object);
_driveDetection.IsDriveDetectionWarningCheckBoxSelected = disableWarningCheckBoxStatus;
// Act & Assert
return _driveDetection.DisplayResults();
}
}
}