2019-09-05 00:26:26 +08:00
|
|
|
#include "pch.h"
|
|
|
|
#include "general_settings.h"
|
|
|
|
#include "auto_start_helper.h"
|
|
|
|
#include <common/settings_helpers.h>
|
|
|
|
#include "powertoy_module.h"
|
2019-10-16 16:21:44 +08:00
|
|
|
#include <common/windows_colors.h>
|
2019-09-05 00:26:26 +08:00
|
|
|
|
2019-10-29 04:04:37 +08:00
|
|
|
static std::wstring settings_theme = L"system";
|
2019-10-16 16:21:44 +08:00
|
|
|
|
2019-12-06 16:40:23 +08:00
|
|
|
json::JsonObject load_general_settings() {
|
2019-10-16 16:21:44 +08:00
|
|
|
auto loaded = PTSettingsHelper::load_general_settings();
|
2019-12-06 16:40:23 +08:00
|
|
|
settings_theme = loaded.GetNamedString(L"theme", L"system");
|
|
|
|
if (settings_theme != L"dark" && settings_theme != L"light") {
|
2019-10-16 16:21:44 +08:00
|
|
|
settings_theme = L"system";
|
|
|
|
}
|
|
|
|
return loaded;
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
2019-12-06 16:40:23 +08:00
|
|
|
json::JsonObject get_general_settings() {
|
|
|
|
json::JsonObject result;
|
|
|
|
const bool startup = is_auto_start_task_active_for_this_user();
|
|
|
|
result.SetNamedValue(L"startup", json::value(startup));
|
2019-09-05 00:26:26 +08:00
|
|
|
|
2019-12-06 16:40:23 +08:00
|
|
|
json::JsonObject enabled;
|
2019-09-05 00:26:26 +08:00
|
|
|
for (auto&[name, powertoy] : modules()) {
|
2019-12-06 16:40:23 +08:00
|
|
|
enabled.SetNamedValue(name, json::value(powertoy.is_enabled()));
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
2019-12-06 16:40:23 +08:00
|
|
|
result.SetNamedValue(L"enabled", std::move(enabled));
|
2019-10-16 16:21:44 +08:00
|
|
|
|
2019-12-06 16:40:23 +08:00
|
|
|
result.SetNamedValue(L"theme", json::value(settings_theme));
|
|
|
|
result.SetNamedValue(L"system_theme", json::value(WindowsColors::is_dark_mode() ? L"dark" : L"light"));
|
|
|
|
result.SetNamedValue(L"powertoys_version", json::value(get_product_version()));
|
2019-09-05 00:26:26 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-12-06 16:40:23 +08:00
|
|
|
void apply_general_settings(const json::JsonObject& general_configs) {
|
|
|
|
if (json::has(general_configs, L"startup", json::JsonValueType::Boolean)) {
|
|
|
|
const bool startup = general_configs.GetNamedBoolean(L"startup");
|
|
|
|
const bool current_startup = is_auto_start_task_active_for_this_user();
|
2019-09-05 00:26:26 +08:00
|
|
|
if (current_startup != startup) {
|
|
|
|
if (startup) {
|
|
|
|
enable_auto_start_task_for_this_user();
|
|
|
|
} else {
|
|
|
|
disable_auto_start_task_for_this_user();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-06 16:40:23 +08:00
|
|
|
if (json::has(general_configs, L"enabled")) {
|
|
|
|
for (const auto& enabled_element : general_configs.GetNamedObject(L"enabled")) {
|
|
|
|
const auto value = enabled_element.Value();
|
|
|
|
if (value.ValueType() != json::JsonValueType::Boolean) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const std::wstring name{enabled_element.Key().c_str()};
|
|
|
|
const bool found = modules().find(name) != modules().end();
|
|
|
|
if (!found) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const bool module_inst_enabled = modules().at(name).is_enabled();
|
|
|
|
const bool target_enabled = value.GetBoolean();
|
|
|
|
if (module_inst_enabled == target_enabled) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (target_enabled) {
|
|
|
|
modules().at(name).enable();
|
|
|
|
} else {
|
|
|
|
modules().at(name).disable();
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-06 16:40:23 +08:00
|
|
|
if (json::has(general_configs, L"theme", json::JsonValueType::String)) {
|
|
|
|
settings_theme = general_configs.GetNamedString(L"theme");
|
2019-10-16 16:21:44 +08:00
|
|
|
}
|
2019-12-06 16:40:23 +08:00
|
|
|
json::JsonObject save_settings = get_general_settings();
|
2019-09-05 00:26:26 +08:00
|
|
|
PTSettingsHelper::save_general_settings(save_settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
void start_initial_powertoys() {
|
|
|
|
bool only_enable_some_powertoys = false;
|
|
|
|
|
|
|
|
std::unordered_set<std::wstring> powertoys_to_enable;
|
|
|
|
|
2019-12-06 16:40:23 +08:00
|
|
|
json::JsonObject general_settings;
|
2019-09-05 00:26:26 +08:00
|
|
|
try {
|
|
|
|
general_settings = load_general_settings();
|
2019-12-06 16:40:23 +08:00
|
|
|
json::JsonObject enabled = general_settings.GetNamedObject(L"enabled");
|
|
|
|
for (const auto & enabled_element : enabled) {
|
|
|
|
if (enabled_element.Value().GetBoolean()) {
|
2019-09-05 00:26:26 +08:00
|
|
|
// Enable this powertoy.
|
2019-12-06 16:40:23 +08:00
|
|
|
powertoys_to_enable.emplace(enabled_element.Key());
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
only_enable_some_powertoys = true;
|
|
|
|
}
|
2019-12-06 16:40:23 +08:00
|
|
|
catch (...) {
|
2019-09-05 00:26:26 +08:00
|
|
|
// Couldn't read the general settings correctly.
|
|
|
|
// Load all powertoys.
|
2019-12-06 16:40:23 +08:00
|
|
|
// TODO: notify user about invalid json config
|
2019-09-05 00:26:26 +08:00
|
|
|
only_enable_some_powertoys = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto&[name, powertoy] : modules()) {
|
|
|
|
if (only_enable_some_powertoys) {
|
|
|
|
if (powertoys_to_enable.find(name)!=powertoys_to_enable.end()) {
|
|
|
|
powertoy.enable();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
powertoy.enable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|