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
|
|
|
|
|
|
|
using namespace web;
|
|
|
|
|
2019-10-16 16:21:44 +08:00
|
|
|
static std::wstring settings_theme;
|
|
|
|
|
2019-09-05 00:26:26 +08:00
|
|
|
web::json::value load_general_settings() {
|
2019-10-16 16:21:44 +08:00
|
|
|
auto loaded = PTSettingsHelper::load_general_settings();
|
|
|
|
if (loaded.has_string_field(L"theme")) {
|
|
|
|
settings_theme = loaded.as_object()[L"theme"].as_string();
|
|
|
|
if (settings_theme != L"dark" && settings_theme != L"light") {
|
|
|
|
settings_theme = L"system";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
settings_theme = L"system";
|
|
|
|
}
|
|
|
|
return loaded;
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
web::json::value get_general_settings() {
|
|
|
|
json::value result = json::value::object();
|
|
|
|
bool startup = is_auto_start_task_active_for_this_user();
|
|
|
|
result.as_object()[L"startup"] = json::value::boolean(startup);
|
|
|
|
|
|
|
|
json::value enabled = json::value::object();
|
|
|
|
for (auto&[name, powertoy] : modules()) {
|
|
|
|
enabled.as_object()[name] = json::value::boolean(powertoy.is_enabled());
|
|
|
|
}
|
|
|
|
result.as_object()[L"enabled"] = enabled;
|
2019-10-16 16:21:44 +08:00
|
|
|
|
|
|
|
result.as_object()[L"theme"] = json::value::string(settings_theme);
|
|
|
|
result.as_object()[L"system_theme"] = json::value::string(WindowsColors::is_dark_mode() ? L"dark" : L"light");
|
2019-09-05 00:26:26 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void apply_general_settings(const json::value& general_configs) {
|
|
|
|
bool contains_startup = general_configs.has_boolean_field(L"startup");
|
|
|
|
if (contains_startup) {
|
|
|
|
bool startup = general_configs.at(L"startup").as_bool();
|
|
|
|
bool current_startup = is_auto_start_task_active_for_this_user();
|
|
|
|
if (current_startup != startup) {
|
|
|
|
if (startup) {
|
|
|
|
enable_auto_start_task_for_this_user();
|
|
|
|
} else {
|
|
|
|
disable_auto_start_task_for_this_user();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool contains_enabled = general_configs.has_object_field(L"enabled");
|
|
|
|
if (contains_enabled) {
|
|
|
|
for (auto enabled_element : general_configs.at(L"enabled").as_object()) {
|
|
|
|
if (enabled_element.second.is_boolean() && modules().find(enabled_element.first) != modules().end()) {
|
|
|
|
bool module_inst_enabled = modules().at(enabled_element.first).is_enabled();
|
|
|
|
bool target_enabled = enabled_element.second.as_bool();
|
|
|
|
if (module_inst_enabled != target_enabled) {
|
|
|
|
if (target_enabled) {
|
|
|
|
modules().at(enabled_element.first).enable();
|
|
|
|
} else {
|
|
|
|
modules().at(enabled_element.first).disable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-16 16:21:44 +08:00
|
|
|
if (general_configs.has_string_field(L"theme")) {
|
|
|
|
settings_theme = general_configs.at(L"theme").as_string();
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
json::value save_settings = get_general_settings();
|
|
|
|
PTSettingsHelper::save_general_settings(save_settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
void start_initial_powertoys() {
|
|
|
|
bool only_enable_some_powertoys = false;
|
|
|
|
|
|
|
|
std::unordered_set<std::wstring> powertoys_to_enable;
|
|
|
|
|
|
|
|
json::value general_settings;
|
|
|
|
try {
|
|
|
|
general_settings = load_general_settings();
|
|
|
|
json::value enabled = general_settings[L"enabled"];
|
|
|
|
for (auto enabled_element : enabled.as_object()) {
|
|
|
|
if (enabled_element.second.as_bool()) {
|
|
|
|
// Enable this powertoy.
|
|
|
|
powertoys_to_enable.emplace(enabled_element.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
only_enable_some_powertoys = true;
|
|
|
|
}
|
2019-09-11 18:38:20 +08:00
|
|
|
catch (std::exception&) {
|
2019-09-05 00:26:26 +08:00
|
|
|
// Couldn't read the general settings correctly.
|
|
|
|
// Load all powertoys.
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|