mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-04 18:26:39 +02:00
[File Explorer] Fix enable/disable for File Explorer PowerToy (#6883)
* Commented out enable/disable for File Explorer * Revert UI changes * Disable the toggles if PT is not running elevated * Fixed compilation errors in tests * Cleaned up preview pane code to separate thumbnail and preview panes as separate classes * Fixed broken settings format and added elevation check and registry updated required logic. Preview Handler tested manually working, Thumbnail Enable/Disable needs to be fixed * Updated Thumbnail enable/disable logic and added warning messages * Update tests for File Explorer * Fixed RegGetValue failing in Release config * Renamed new classes * Split wrappers for disable to work * Modified enabled flag check to also check if user is on new settings. Fixed casing issue in powerpreview.h that caused a dialog prompt on first launch after install * Update fontweight and margin * Fixed release build not working * Move UseNewSettings usage to powerpreview.cpp to avoid tests breaking. For new settings the enable check is done in constructor and for old settings it is done in enable * Update src/core/Microsoft.PowerToys.Settings.UI/Strings/en-us/Resources.resw Co-authored-by: htcfreek <61519853+htcfreek@users.noreply.github.com> * Update src/core/Microsoft.PowerToys.Settings.UI/Strings/en-us/Resources.resw Co-authored-by: htcfreek <61519853+htcfreek@users.noreply.github.com> * Update src/core/Microsoft.PowerToys.Settings.UI/Strings/en-us/Resources.resw Co-authored-by: htcfreek <61519853+htcfreek@users.noreply.github.com> * Moved dup code to method * Use correct versions of general settings for backwards compat test Co-authored-by: htcfreek <61519853+htcfreek@users.noreply.github.com>
This commit is contained in:
@@ -5,39 +5,53 @@
|
||||
#include "trace.h"
|
||||
#include "settings.h"
|
||||
#include "Generated Files/resource.h"
|
||||
#include <common\os-detect.h>
|
||||
|
||||
// Constructor
|
||||
PowerPreviewModule::PowerPreviewModule() :
|
||||
m_moduleName(GET_RESOURCE_STRING(IDS_MODULE_NAME)),
|
||||
m_fileExplorerModules(
|
||||
{ // SVG Preview Handler settings object.
|
||||
new PreviewHandlerSettings(
|
||||
true,
|
||||
L"svg-previewer-toggle-setting",
|
||||
GET_RESOURCE_STRING(IDS_PREVPANE_SVG_SETTINGS_DESCRIPTION),
|
||||
L"{ddee2b8a-6807-48a6-bb20-2338174ff779}",
|
||||
L"Svg Preview Handler",
|
||||
new RegistryWrapper()),
|
||||
|
||||
// MarkDown Preview Handler Settings Object.
|
||||
new PreviewHandlerSettings(
|
||||
true,
|
||||
L"md-previewer-toggle-setting",
|
||||
GET_RESOURCE_STRING(IDS_PREVPANE_MD_SETTINGS_DESCRIPTION),
|
||||
L"{45769bcc-e8fd-42d0-947e-02beef77a1f5}",
|
||||
L"Markdown Preview Handler",
|
||||
new RegistryWrapper()),
|
||||
//SVG Thumbnail Provider settings object.
|
||||
new ThumbnailProviderSettings(
|
||||
true,
|
||||
L"svg-thumbnail-toggle-setting",
|
||||
GET_RESOURCE_STRING(IDS_SVG_THUMBNAIL_PROVIDER_SETTINGS_DESCRIPTION),
|
||||
L"{36B27788-A8BB-4698-A756-DF9F11F64F84}",
|
||||
L"Svg Thumbnail Provider",
|
||||
new RegistryWrapper(),
|
||||
L".svg\\shellex\\{E357FCCD-A995-4576-B01F-234630154E96}") })
|
||||
{
|
||||
// Initialize the toggle states for each module
|
||||
init_settings();
|
||||
|
||||
// If the user is on the new settings interface, File Explorer might be disabled if they updated from old to new settings, so initialize the registry state in the constructor as PowerPreviewModule::enable/disable will not be called on startup
|
||||
if (UseNewSettings())
|
||||
{
|
||||
update_registry_to_match_toggles();
|
||||
}
|
||||
}
|
||||
|
||||
// Destroy the powertoy and free memory.
|
||||
void PowerPreviewModule::destroy()
|
||||
{
|
||||
Trace::Destroyed();
|
||||
for (auto previewHandler : this->m_previewHandlers)
|
||||
{
|
||||
if (previewHandler != NULL)
|
||||
{
|
||||
// Disable all the active preview handlers.
|
||||
if (this->m_enabled && previewHandler->GetToggleSettingState())
|
||||
{
|
||||
previewHandler->DisablePreview();
|
||||
}
|
||||
|
||||
delete previewHandler;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto thumbnailProvider : this->m_thumbnailProviders)
|
||||
{
|
||||
if (thumbnailProvider != NULL)
|
||||
{
|
||||
// Disable all the active thumbnail providers.
|
||||
if (this->m_enabled && thumbnailProvider->GetToggleSettingState())
|
||||
{
|
||||
thumbnailProvider->DisablePreview();
|
||||
}
|
||||
|
||||
delete thumbnailProvider;
|
||||
}
|
||||
}
|
||||
|
||||
delete this;
|
||||
}
|
||||
|
||||
@@ -66,20 +80,12 @@ bool PowerPreviewModule::get_config(_Out_ wchar_t* buffer, _Out_ int* buffer_siz
|
||||
GET_RESOURCE_STRING(IDS_PRVPANE_FILE_PREV_STTNGS_GROUP_DESC),
|
||||
GET_RESOURCE_STRING(IDS_PRVPANE_FILE_PREV_STTNGS_GROUP_TEXT));
|
||||
|
||||
for (auto previewHandler : this->m_previewHandlers)
|
||||
for (auto fileExplorerModule : this->m_fileExplorerModules)
|
||||
{
|
||||
settings.add_bool_toggle(
|
||||
previewHandler->GetToggleSettingName(),
|
||||
previewHandler->GetToggleSettingDescription(),
|
||||
previewHandler->GetToggleSettingState());
|
||||
}
|
||||
|
||||
for (auto thumbnailProvider : this->m_thumbnailProviders)
|
||||
{
|
||||
settings.add_bool_toggle(
|
||||
thumbnailProvider->GetToggleSettingName(),
|
||||
thumbnailProvider->GetToggleSettingDescription(),
|
||||
thumbnailProvider->GetToggleSettingState());
|
||||
fileExplorerModule->GetToggleSettingName(),
|
||||
fileExplorerModule->GetToggleSettingDescription(),
|
||||
fileExplorerModule->GetToggleSettingState());
|
||||
}
|
||||
|
||||
return settings.serialize_to_buffer(buffer, buffer_size);
|
||||
@@ -92,14 +98,17 @@ void PowerPreviewModule::set_config(const wchar_t* config)
|
||||
{
|
||||
PowerToysSettings::PowerToyValues settings = PowerToysSettings::PowerToyValues::from_json_string(config);
|
||||
|
||||
for (auto previewHandler : this->m_previewHandlers)
|
||||
bool updateSuccess = true;
|
||||
bool isElevated = is_process_elevated(false);
|
||||
for (auto fileExplorerModule : this->m_fileExplorerModules)
|
||||
{
|
||||
previewHandler->UpdateState(settings, this->m_enabled);
|
||||
// If the user is using the new settings interface, as it does not have a toggle to modify enabled consider File Explorer to always be enabled
|
||||
updateSuccess = updateSuccess && fileExplorerModule->UpdateState(settings, this->m_enabled || UseNewSettings(), isElevated);
|
||||
}
|
||||
|
||||
for (auto thumbnailProvider : this->m_thumbnailProviders)
|
||||
if (!updateSuccess)
|
||||
{
|
||||
thumbnailProvider->UpdateState(settings, this->m_enabled);
|
||||
show_update_warning_message();
|
||||
}
|
||||
|
||||
settings.save_to_settings_file();
|
||||
@@ -113,30 +122,10 @@ void PowerPreviewModule::set_config(const wchar_t* config)
|
||||
// Enable preview handlers.
|
||||
void PowerPreviewModule::enable()
|
||||
{
|
||||
for (auto previewHandler : this->m_previewHandlers)
|
||||
// Should only be done for old settings as it is already done for new settings in the constructor.
|
||||
if (!UseNewSettings())
|
||||
{
|
||||
if (previewHandler->GetToggleSettingState())
|
||||
{
|
||||
// Enable all the previews with initial state set as true.
|
||||
previewHandler->EnablePreview();
|
||||
}
|
||||
else
|
||||
{
|
||||
previewHandler->DisablePreview();
|
||||
}
|
||||
}
|
||||
|
||||
for (auto thumbnailProvider : this->m_thumbnailProviders)
|
||||
{
|
||||
if (thumbnailProvider->GetToggleSettingState())
|
||||
{
|
||||
// Enable all the thumbnail providers with initial state set as true.
|
||||
thumbnailProvider->EnableThumbnailProvider();
|
||||
}
|
||||
else
|
||||
{
|
||||
thumbnailProvider->DisableThumbnailProvider();
|
||||
}
|
||||
update_registry_to_match_toggles();
|
||||
}
|
||||
|
||||
if (!this->m_enabled)
|
||||
@@ -150,15 +139,12 @@ void PowerPreviewModule::enable()
|
||||
// Disable active preview handlers.
|
||||
void PowerPreviewModule::disable()
|
||||
{
|
||||
for (auto previewHandler : this->m_previewHandlers)
|
||||
{
|
||||
previewHandler->DisablePreview();
|
||||
}
|
||||
|
||||
for (auto thumbnailProvider : this->m_thumbnailProviders)
|
||||
{
|
||||
thumbnailProvider->DisableThumbnailProvider();
|
||||
}
|
||||
elevation_check_wrapper([this]() {
|
||||
for (auto fileExplorerModule : this->m_fileExplorerModules)
|
||||
{
|
||||
fileExplorerModule->Disable();
|
||||
}
|
||||
});
|
||||
|
||||
if (this->m_enabled)
|
||||
{
|
||||
@@ -184,18 +170,81 @@ void PowerPreviewModule::init_settings()
|
||||
PowerToysSettings::PowerToyValues::load_from_settings_file(PowerPreviewModule::get_name());
|
||||
|
||||
// Load settings states.
|
||||
for (auto previewHandler : this->m_previewHandlers)
|
||||
for (auto fileExplorerModule : this->m_fileExplorerModules)
|
||||
{
|
||||
previewHandler->LoadState(settings);
|
||||
}
|
||||
|
||||
for (auto thumbnailProvider : this->m_thumbnailProviders)
|
||||
{
|
||||
thumbnailProvider->LoadState(settings);
|
||||
fileExplorerModule->LoadState(settings);
|
||||
}
|
||||
}
|
||||
catch (std::exception const& e)
|
||||
{
|
||||
Trace::InitSetErrorLoadingFile(e.what());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Function to check if the registry states need to be updated
|
||||
bool PowerPreviewModule::is_registry_update_required()
|
||||
{
|
||||
for (auto fileExplorerModule : this->m_fileExplorerModules)
|
||||
{
|
||||
if (fileExplorerModule->GetToggleSettingState() != fileExplorerModule->CheckRegistryState())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Function to warn the user that PowerToys needs to run as administrator for changes to take effect
|
||||
void PowerPreviewModule::show_update_warning_message()
|
||||
{
|
||||
// Show warning message if update is required
|
||||
MessageBoxW(NULL,
|
||||
GET_RESOURCE_STRING(IDS_FILEEXPLORER_ADMIN_RESTART_WARNING_DESCRIPTION).c_str(),
|
||||
GET_RESOURCE_STRING(IDS_FILEEXPLORER_ADMIN_RESTART_WARNING_TITLE).c_str(),
|
||||
MB_OK | MB_ICONWARNING);
|
||||
}
|
||||
|
||||
// Function that checks if a registry method is required and if so checks if the process is elevated and accordingly executes the method or shows a warning
|
||||
void PowerPreviewModule::registry_and_elevation_check_wrapper(std::function<void()> method)
|
||||
{
|
||||
// Check if a registry update is required
|
||||
if (is_registry_update_required())
|
||||
{
|
||||
elevation_check_wrapper(method);
|
||||
}
|
||||
}
|
||||
|
||||
// Function that checks if the process is elevated and accordingly executes the method or shows a warning
|
||||
void PowerPreviewModule::elevation_check_wrapper(std::function<void()> method)
|
||||
{
|
||||
// Check if the process is elevated in order to have permissions to modify HKLM registry
|
||||
if (is_process_elevated(false))
|
||||
{
|
||||
method();
|
||||
}
|
||||
// Show a warning if it doesn't have permissions
|
||||
else
|
||||
{
|
||||
show_update_warning_message();
|
||||
}
|
||||
}
|
||||
|
||||
// Function that updates the registry state to match the toggle states
|
||||
void PowerPreviewModule::update_registry_to_match_toggles()
|
||||
{
|
||||
registry_and_elevation_check_wrapper([this]() {
|
||||
for (auto fileExplorerModule : this->m_fileExplorerModules)
|
||||
{
|
||||
if (fileExplorerModule->GetToggleSettingState())
|
||||
{
|
||||
// Enable all the modules with initial state set as true.
|
||||
fileExplorerModule->Enable();
|
||||
}
|
||||
else
|
||||
{
|
||||
fileExplorerModule->Disable();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user