PowerToys/src/runner/general_settings.cpp

110 lines
3.5 KiB
C++
Raw Normal View History

#include "pch.h"
#include "general_settings.h"
#include "auto_start_helper.h"
#include <common/settings_helpers.h>
#include "powertoy_module.h"
#include <common/windows_colors.h>
using namespace web;
static std::wstring settings_theme;
web::json::value load_general_settings() {
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;
}
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;
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");
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();
}
}
}
}
}
if (general_configs.has_string_field(L"theme")) {
settings_theme = general_configs.at(L"theme").as_string();
}
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;
}
catch (std::exception&) {
// 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();
}
}
}