mirror of
https://github.com/microsoft/PowerToys
synced 2025-08-31 22:45:32 +00:00
[PowerRename]Save data from the last run outside of the settings file (#28861)
* [PowerRename] Sync settings before saving last window size * address review comments
This commit is contained in:
@@ -70,4 +70,40 @@ namespace json
|
|||||||
{
|
{
|
||||||
return value; // identity function overload for convenience
|
return value; // identity function overload for convenience
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T, typename D = std::optional<T>>
|
||||||
|
requires std::constructible_from<std::optional<T>, D>
|
||||||
|
void get(const json::JsonObject& o, const wchar_t* name, T& destination, D default_value = std::nullopt)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if constexpr (std::is_same_v<T, bool>)
|
||||||
|
{
|
||||||
|
destination = o.GetNamedBoolean(name);
|
||||||
|
}
|
||||||
|
else if constexpr (std::is_arithmetic_v<T>)
|
||||||
|
{
|
||||||
|
destination = static_cast<T>(o.GetNamedNumber(name));
|
||||||
|
}
|
||||||
|
else if constexpr (std::is_same_v<T, std::wstring>)
|
||||||
|
{
|
||||||
|
destination = o.GetNamedString(name);
|
||||||
|
}
|
||||||
|
else if constexpr (std::is_same_v<T, json::JsonObject>)
|
||||||
|
{
|
||||||
|
destination = o.GetNamedObject(name);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
static_assert(std::bool_constant<std::is_same_v<T, T&>>::value, "Unsupported type");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
std::optional<T> maybe_default{ std::move(default_value) };
|
||||||
|
if (maybe_default.has_value())
|
||||||
|
destination = std::move(*maybe_default);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -134,7 +134,7 @@ namespace winrt::PowerRenameUI::implementation
|
|||||||
GetDpiForMonitor(hMonitor, MONITOR_DPI_TYPE::MDT_EFFECTIVE_DPI, &x_dpi, &x_dpi);
|
GetDpiForMonitor(hMonitor, MONITOR_DPI_TYPE::MDT_EFFECTIVE_DPI, &x_dpi, &x_dpi);
|
||||||
UINT window_dpi = GetDpiForWindow(m_window);
|
UINT window_dpi = GetDpiForWindow(m_window);
|
||||||
|
|
||||||
const auto& [width, height] = CSettingsInstance().GetLastWindowSize();
|
const auto& [width, height] = LastRunSettingsInstance().GetLastWindowSize();
|
||||||
|
|
||||||
winrt::Windows::Graphics::RectInt32 rect;
|
winrt::Windows::Graphics::RectInt32 rect;
|
||||||
// Scale window size
|
// Scale window size
|
||||||
@@ -297,12 +297,15 @@ namespace winrt::PowerRenameUI::implementation
|
|||||||
Microsoft::UI::Windowing::AppWindow::GetFromWindowId(Microsoft::UI::GetWindowIdFromWindow(m_window));
|
Microsoft::UI::Windowing::AppWindow::GetFromWindowId(Microsoft::UI::GetWindowIdFromWindow(m_window));
|
||||||
const auto [width, height] = appWindow.Size();
|
const auto [width, height] = appWindow.Size();
|
||||||
|
|
||||||
CSettingsInstance().UpdateLastWindowSize(static_cast<int>(width), static_cast<int>(height));
|
m_updatedWindowSize.emplace(std::make_pair(width, height));
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::OnClosed(winrt::Windows::Foundation::IInspectable const&, winrt::Microsoft::UI::Xaml::WindowEventArgs const&)
|
void MainWindow::OnClosed(winrt::Windows::Foundation::IInspectable const&, winrt::Microsoft::UI::Xaml::WindowEventArgs const&)
|
||||||
{
|
{
|
||||||
CSettingsInstance().Save();
|
if (m_updatedWindowSize)
|
||||||
|
{
|
||||||
|
LastRunSettingsInstance().UpdateLastWindowSize(m_updatedWindowSize->first, m_updatedWindowSize->second);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::InvalidateItemListViewState()
|
void MainWindow::InvalidateItemListViewState()
|
||||||
@@ -820,8 +823,8 @@ namespace winrt::PowerRenameUI::implementation
|
|||||||
{
|
{
|
||||||
flags = CSettingsInstance().GetFlags();
|
flags = CSettingsInstance().GetFlags();
|
||||||
|
|
||||||
textBox_search().Text(CSettingsInstance().GetSearchText().c_str());
|
textBox_search().Text(LastRunSettingsInstance().GetSearchText().c_str());
|
||||||
textBox_replace().Text(CSettingsInstance().GetReplaceText().c_str());
|
textBox_replace().Text(LastRunSettingsInstance().GetReplaceText().c_str());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -846,7 +849,7 @@ namespace winrt::PowerRenameUI::implementation
|
|||||||
CSettingsInstance().SetFlags(flags);
|
CSettingsInstance().SetFlags(flags);
|
||||||
|
|
||||||
winrt::hstring searchTerm = textBox_search().Text();
|
winrt::hstring searchTerm = textBox_search().Text();
|
||||||
CSettingsInstance().SetSearchText(std::wstring{ searchTerm });
|
LastRunSettingsInstance().SetSearchText(std::wstring{ searchTerm });
|
||||||
|
|
||||||
if (CSettingsInstance().GetMRUEnabled() && m_searchMRU)
|
if (CSettingsInstance().GetMRUEnabled() && m_searchMRU)
|
||||||
{
|
{
|
||||||
@@ -858,7 +861,7 @@ namespace winrt::PowerRenameUI::implementation
|
|||||||
}
|
}
|
||||||
|
|
||||||
winrt::hstring replaceTerm = textBox_replace().Text();
|
winrt::hstring replaceTerm = textBox_replace().Text();
|
||||||
CSettingsInstance().SetReplaceText(std::wstring{ replaceTerm });
|
LastRunSettingsInstance().SetReplaceText(std::wstring{ replaceTerm });
|
||||||
|
|
||||||
if (CSettingsInstance().GetMRUEnabled() && m_replaceMRU)
|
if (CSettingsInstance().GetMRUEnabled() && m_replaceMRU)
|
||||||
{
|
{
|
||||||
|
@@ -153,6 +153,8 @@ namespace winrt::PowerRenameUI::implementation
|
|||||||
UINT m_selectedCount = 0;
|
UINT m_selectedCount = 0;
|
||||||
UINT m_renamingCount = 0;
|
UINT m_renamingCount = 0;
|
||||||
winrt::event<Microsoft::UI::Xaml::Data::PropertyChangedEventHandler> m_propertyChanged;
|
winrt::event<Microsoft::UI::Xaml::Data::PropertyChangedEventHandler> m_propertyChanged;
|
||||||
|
std::optional<std::pair<int,int>> m_updatedWindowSize;
|
||||||
|
|
||||||
|
|
||||||
bool m_flagValidationInProgress = false;
|
bool m_flagValidationInProgress = false;
|
||||||
|
|
||||||
|
@@ -13,6 +13,7 @@
|
|||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
const wchar_t c_powerRenameDataFilePath[] = L"\\power-rename-settings.json";
|
const wchar_t c_powerRenameDataFilePath[] = L"\\power-rename-settings.json";
|
||||||
|
const wchar_t c_powerRenameLastRunFilePath[] = L"\\power-rename-last-run-data.json";
|
||||||
const wchar_t c_powerRenameUIFlagsFilePath[] = L"\\power-rename-ui-flags";
|
const wchar_t c_powerRenameUIFlagsFilePath[] = L"\\power-rename-ui-flags";
|
||||||
|
|
||||||
const wchar_t c_enabled[] = L"Enabled";
|
const wchar_t c_enabled[] = L"Enabled";
|
||||||
@@ -48,11 +49,7 @@ void CSettings::Save()
|
|||||||
jsonData.SetNamedValue(c_persistState, json::value(settings.persistState));
|
jsonData.SetNamedValue(c_persistState, json::value(settings.persistState));
|
||||||
jsonData.SetNamedValue(c_mruEnabled, json::value(settings.MRUEnabled));
|
jsonData.SetNamedValue(c_mruEnabled, json::value(settings.MRUEnabled));
|
||||||
jsonData.SetNamedValue(c_maxMRUSize, json::value(settings.maxMRUSize));
|
jsonData.SetNamedValue(c_maxMRUSize, json::value(settings.maxMRUSize));
|
||||||
jsonData.SetNamedValue(c_searchText, json::value(settings.searchText));
|
|
||||||
jsonData.SetNamedValue(c_replaceText, json::value(settings.replaceText));
|
|
||||||
jsonData.SetNamedValue(c_useBoostLib, json::value(settings.useBoostLib));
|
jsonData.SetNamedValue(c_useBoostLib, json::value(settings.useBoostLib));
|
||||||
jsonData.SetNamedValue(c_lastWindowWidth, json::value(settings.lastWindowWidth));
|
|
||||||
jsonData.SetNamedValue(c_lastWindowHeight, json::value(settings.lastWindowHeight));
|
|
||||||
|
|
||||||
json::to_file(jsonFilePath, jsonData);
|
json::to_file(jsonFilePath, jsonData);
|
||||||
GetSystemTimeAsFileTime(&lastLoadedTime);
|
GetSystemTimeAsFileTime(&lastLoadedTime);
|
||||||
@@ -94,8 +91,10 @@ void CSettings::MigrateFromRegistry()
|
|||||||
settings.MRUEnabled = GetRegBoolean(c_mruEnabled, true);
|
settings.MRUEnabled = GetRegBoolean(c_mruEnabled, true);
|
||||||
settings.maxMRUSize = GetRegNumber(c_maxMRUSize, 10);
|
settings.maxMRUSize = GetRegNumber(c_maxMRUSize, 10);
|
||||||
settings.flags = GetRegNumber(c_flags, 0);
|
settings.flags = GetRegNumber(c_flags, 0);
|
||||||
settings.searchText = GetRegString(c_searchText, L"");
|
|
||||||
settings.replaceText = GetRegString(c_replaceText, L"");
|
LastRunSettingsInstance().SetSearchText(GetRegString(c_searchText, L""));
|
||||||
|
LastRunSettingsInstance().SetReplaceText(GetRegString(c_replaceText, L""));
|
||||||
|
|
||||||
settings.useBoostLib = false; // Never existed in registry, disabled by default.
|
settings.useBoostLib = false; // Never existed in registry, disabled by default.
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,21 +130,10 @@ void CSettings::ParseJson()
|
|||||||
{
|
{
|
||||||
settings.maxMRUSize = static_cast<unsigned int>(jsonSettings.GetNamedNumber(c_maxMRUSize));
|
settings.maxMRUSize = static_cast<unsigned int>(jsonSettings.GetNamedNumber(c_maxMRUSize));
|
||||||
}
|
}
|
||||||
if (json::has(jsonSettings, c_searchText, json::JsonValueType::String))
|
|
||||||
{
|
|
||||||
settings.searchText = jsonSettings.GetNamedString(c_searchText);
|
|
||||||
}
|
|
||||||
if (json::has(jsonSettings, c_replaceText, json::JsonValueType::String))
|
|
||||||
{
|
|
||||||
settings.replaceText = jsonSettings.GetNamedString(c_replaceText);
|
|
||||||
}
|
|
||||||
if (json::has(jsonSettings, c_useBoostLib, json::JsonValueType::Boolean))
|
if (json::has(jsonSettings, c_useBoostLib, json::JsonValueType::Boolean))
|
||||||
{
|
{
|
||||||
settings.useBoostLib = jsonSettings.GetNamedBoolean(c_useBoostLib);
|
settings.useBoostLib = jsonSettings.GetNamedBoolean(c_useBoostLib);
|
||||||
}
|
}
|
||||||
|
|
||||||
settings.lastWindowWidth = static_cast<int>(jsonSettings.GetNamedNumber(c_lastWindowWidth, DEFAULT_WINDOW_WIDTH));
|
|
||||||
settings.lastWindowHeight = static_cast<int>(jsonSettings.GetNamedNumber(c_lastWindowHeight, DEFAULT_WINDOW_HEIGHT));
|
|
||||||
}
|
}
|
||||||
catch (const winrt::hresult_error&)
|
catch (const winrt::hresult_error&)
|
||||||
{
|
{
|
||||||
@@ -177,3 +165,35 @@ CSettings& CSettingsInstance()
|
|||||||
static CSettings instance;
|
static CSettings instance;
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LastRunSettings& LastRunSettingsInstance()
|
||||||
|
{
|
||||||
|
static LastRunSettings instance;
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
void LastRunSettings::Load()
|
||||||
|
{
|
||||||
|
const auto lastRunSettingsFilePath = PTSettingsHelper::get_module_save_folder_location(PowerRenameConstants::ModuleKey) + c_powerRenameLastRunFilePath;
|
||||||
|
auto json = json::from_file(lastRunSettingsFilePath);
|
||||||
|
if (!json)
|
||||||
|
return;
|
||||||
|
|
||||||
|
json::get(*json, c_searchText, searchText, L"");
|
||||||
|
json::get(*json, c_replaceText, replaceText, L"");
|
||||||
|
json::get(*json, c_lastWindowWidth, lastWindowWidth, DEFAULT_WINDOW_WIDTH);
|
||||||
|
json::get(*json, c_lastWindowHeight, lastWindowHeight, DEFAULT_WINDOW_HEIGHT);
|
||||||
|
}
|
||||||
|
|
||||||
|
void LastRunSettings::Save()
|
||||||
|
{
|
||||||
|
json::JsonObject json;
|
||||||
|
|
||||||
|
json.SetNamedValue(c_searchText, json::value(searchText));
|
||||||
|
json.SetNamedValue(c_replaceText, json::value(replaceText));
|
||||||
|
json.SetNamedValue(c_lastWindowWidth, json::value(lastWindowWidth));
|
||||||
|
json.SetNamedValue(c_lastWindowHeight, json::value(lastWindowHeight));
|
||||||
|
|
||||||
|
const auto lastRunSettingsFilePath = PTSettingsHelper::get_module_save_folder_location(PowerRenameConstants::ModuleKey) + c_powerRenameLastRunFilePath;
|
||||||
|
json::to_file(lastRunSettingsFilePath, json);
|
||||||
|
}
|
||||||
|
@@ -6,9 +6,6 @@
|
|||||||
class CSettings
|
class CSettings
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static constexpr inline int DEFAULT_WINDOW_WIDTH = 1400;
|
|
||||||
static constexpr inline int DEFAULT_WINDOW_HEIGHT = 800;
|
|
||||||
|
|
||||||
CSettings();
|
CSettings();
|
||||||
|
|
||||||
inline bool GetEnabled()
|
inline bool GetEnabled()
|
||||||
@@ -28,17 +25,6 @@ public:
|
|||||||
Save();
|
Save();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::tuple<int, int> GetLastWindowSize() const
|
|
||||||
{
|
|
||||||
return std::make_tuple(settings.lastWindowWidth, settings.lastWindowHeight);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void UpdateLastWindowSize(const int width, const int height)
|
|
||||||
{
|
|
||||||
settings.lastWindowWidth = std::max(width, DEFAULT_WINDOW_WIDTH);
|
|
||||||
settings.lastWindowHeight = std::max(height, DEFAULT_WINDOW_HEIGHT);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool GetShowIconOnMenu() const
|
inline bool GetShowIconOnMenu() const
|
||||||
{
|
{
|
||||||
return settings.showIconOnMenu;
|
return settings.showIconOnMenu;
|
||||||
@@ -110,28 +96,6 @@ public:
|
|||||||
WriteFlags();
|
WriteFlags();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline const std::wstring& GetSearchText() const
|
|
||||||
{
|
|
||||||
return settings.searchText;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void SetSearchText(const std::wstring& text)
|
|
||||||
{
|
|
||||||
settings.searchText = text;
|
|
||||||
Save();
|
|
||||||
}
|
|
||||||
|
|
||||||
inline const std::wstring& GetReplaceText() const
|
|
||||||
{
|
|
||||||
return settings.replaceText;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void SetReplaceText(const std::wstring& text)
|
|
||||||
{
|
|
||||||
settings.replaceText = text;
|
|
||||||
Save();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Save();
|
void Save();
|
||||||
void Load();
|
void Load();
|
||||||
|
|
||||||
@@ -146,10 +110,6 @@ private:
|
|||||||
bool MRUEnabled{ true };
|
bool MRUEnabled{ true };
|
||||||
unsigned int maxMRUSize{ 10 };
|
unsigned int maxMRUSize{ 10 };
|
||||||
unsigned int flags{ 0 };
|
unsigned int flags{ 0 };
|
||||||
std::wstring searchText{};
|
|
||||||
std::wstring replaceText{};
|
|
||||||
int lastWindowWidth{ DEFAULT_WINDOW_WIDTH };
|
|
||||||
int lastWindowHeight{ DEFAULT_WINDOW_HEIGHT };
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void Reload();
|
void Reload();
|
||||||
@@ -166,3 +126,60 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
CSettings& CSettingsInstance();
|
CSettings& CSettingsInstance();
|
||||||
|
|
||||||
|
class LastRunSettings
|
||||||
|
{
|
||||||
|
static constexpr inline int DEFAULT_WINDOW_WIDTH = 1400;
|
||||||
|
static constexpr inline int DEFAULT_WINDOW_HEIGHT = 800;
|
||||||
|
|
||||||
|
int lastWindowWidth{ DEFAULT_WINDOW_WIDTH };
|
||||||
|
int lastWindowHeight{ DEFAULT_WINDOW_HEIGHT };
|
||||||
|
|
||||||
|
std::wstring searchText{};
|
||||||
|
std::wstring replaceText{};
|
||||||
|
|
||||||
|
public:
|
||||||
|
inline LastRunSettings()
|
||||||
|
{
|
||||||
|
Load();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::tuple<int, int> GetLastWindowSize() const
|
||||||
|
{
|
||||||
|
return std::make_tuple(lastWindowWidth, lastWindowHeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void UpdateLastWindowSize(const int width, const int height)
|
||||||
|
{
|
||||||
|
lastWindowWidth = std::max(width, DEFAULT_WINDOW_WIDTH);
|
||||||
|
lastWindowHeight = std::max(height, DEFAULT_WINDOW_HEIGHT);
|
||||||
|
Save();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const std::wstring& GetSearchText() const
|
||||||
|
{
|
||||||
|
return searchText;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void SetSearchText(const std::wstring& text)
|
||||||
|
{
|
||||||
|
searchText = text;
|
||||||
|
Save();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const std::wstring& GetReplaceText() const
|
||||||
|
{
|
||||||
|
return replaceText;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void SetReplaceText(const std::wstring& text)
|
||||||
|
{
|
||||||
|
replaceText = text;
|
||||||
|
Save();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Load();
|
||||||
|
void Save();
|
||||||
|
};
|
||||||
|
|
||||||
|
LastRunSettings& LastRunSettingsInstance();
|
||||||
|
Reference in New Issue
Block a user