mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 02:36:19 +02: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:
@@ -13,6 +13,7 @@
|
||||
namespace
|
||||
{
|
||||
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_enabled[] = L"Enabled";
|
||||
@@ -48,11 +49,7 @@ void CSettings::Save()
|
||||
jsonData.SetNamedValue(c_persistState, json::value(settings.persistState));
|
||||
jsonData.SetNamedValue(c_mruEnabled, json::value(settings.MRUEnabled));
|
||||
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_lastWindowWidth, json::value(settings.lastWindowWidth));
|
||||
jsonData.SetNamedValue(c_lastWindowHeight, json::value(settings.lastWindowHeight));
|
||||
|
||||
json::to_file(jsonFilePath, jsonData);
|
||||
GetSystemTimeAsFileTime(&lastLoadedTime);
|
||||
@@ -94,8 +91,10 @@ void CSettings::MigrateFromRegistry()
|
||||
settings.MRUEnabled = GetRegBoolean(c_mruEnabled, true);
|
||||
settings.maxMRUSize = GetRegNumber(c_maxMRUSize, 10);
|
||||
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.
|
||||
}
|
||||
|
||||
@@ -131,21 +130,10 @@ void CSettings::ParseJson()
|
||||
{
|
||||
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))
|
||||
{
|
||||
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&)
|
||||
{
|
||||
@@ -177,3 +165,35 @@ CSettings& CSettingsInstance()
|
||||
static CSettings 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
|
||||
{
|
||||
public:
|
||||
static constexpr inline int DEFAULT_WINDOW_WIDTH = 1400;
|
||||
static constexpr inline int DEFAULT_WINDOW_HEIGHT = 800;
|
||||
|
||||
CSettings();
|
||||
|
||||
inline bool GetEnabled()
|
||||
@@ -28,17 +25,6 @@ public:
|
||||
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
|
||||
{
|
||||
return settings.showIconOnMenu;
|
||||
@@ -110,28 +96,6 @@ public:
|
||||
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 Load();
|
||||
|
||||
@@ -146,10 +110,6 @@ private:
|
||||
bool MRUEnabled{ true };
|
||||
unsigned int maxMRUSize{ 10 };
|
||||
unsigned int flags{ 0 };
|
||||
std::wstring searchText{};
|
||||
std::wstring replaceText{};
|
||||
int lastWindowWidth{ DEFAULT_WINDOW_WIDTH };
|
||||
int lastWindowHeight{ DEFAULT_WINDOW_HEIGHT };
|
||||
};
|
||||
|
||||
void Reload();
|
||||
@@ -166,3 +126,60 @@ private:
|
||||
};
|
||||
|
||||
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