1 #include "BaseSettings.hpp"
2
3 #include <QDebug>
4
5 #include "util/Clamp.hpp"
6
7 namespace chatterino {
8
9 std::vector<std::weak_ptr<pajlada::Settings::SettingData>> _settings;
10
11 AB_SETTINGS_CLASS *AB_SETTINGS_CLASS::instance = nullptr;
12
_actuallyRegisterSetting(std::weak_ptr<pajlada::Settings::SettingData> setting)13 void _actuallyRegisterSetting(
14 std::weak_ptr<pajlada::Settings::SettingData> setting)
15 {
16 _settings.push_back(std::move(setting));
17 }
18
AB_SETTINGS_CLASS(const QString & settingsDirectory)19 AB_SETTINGS_CLASS::AB_SETTINGS_CLASS(const QString &settingsDirectory)
20 {
21 AB_SETTINGS_CLASS::instance = this;
22
23 QString settingsPath = settingsDirectory + "/settings.json";
24
25 // get global instance of the settings library
26 auto settingsInstance = pajlada::Settings::SettingManager::getInstance();
27
28 settingsInstance->load(qPrintable(settingsPath));
29
30 settingsInstance->setBackupEnabled(true);
31 settingsInstance->setBackupSlots(9);
32 settingsInstance->saveMethod =
33 pajlada::Settings::SettingManager::SaveMethod::SaveOnExit;
34 }
35
saveSnapshot()36 void AB_SETTINGS_CLASS::saveSnapshot()
37 {
38 rapidjson::Document *d = new rapidjson::Document(rapidjson::kObjectType);
39 rapidjson::Document::AllocatorType &a = d->GetAllocator();
40
41 for (const auto &weakSetting : _settings)
42 {
43 auto setting = weakSetting.lock();
44 if (!setting)
45 {
46 continue;
47 }
48
49 rapidjson::Value key(setting->getPath().c_str(), a);
50 auto curVal = setting->unmarshalJSON();
51 if (curVal == nullptr)
52 {
53 continue;
54 }
55
56 rapidjson::Value val;
57 val.CopyFrom(*curVal, a);
58 d->AddMember(key.Move(), val.Move(), a);
59 }
60
61 // log("Snapshot state: {}", rj::stringify(*d));
62
63 this->snapshot_.reset(d);
64 }
65
restoreSnapshot()66 void AB_SETTINGS_CLASS::restoreSnapshot()
67 {
68 if (!this->snapshot_)
69 {
70 return;
71 }
72
73 const auto &snapshot = *(this->snapshot_.get());
74
75 if (!snapshot.IsObject())
76 {
77 return;
78 }
79
80 for (const auto &weakSetting : _settings)
81 {
82 auto setting = weakSetting.lock();
83 if (!setting)
84 {
85 continue;
86 }
87
88 const char *path = setting->getPath().c_str();
89
90 if (!snapshot.HasMember(path))
91 {
92 continue;
93 }
94
95 setting->marshalJSON(snapshot[path]);
96 }
97 }
98
getClampedUiScale() const99 float AB_SETTINGS_CLASS::getClampedUiScale() const
100 {
101 return clamp<float>(this->uiScale.getValue(), 0.2f, 10);
102 }
103
setClampedUiScale(float value)104 void AB_SETTINGS_CLASS::setClampedUiScale(float value)
105 {
106 this->uiScale.setValue(clamp<float>(value, 0.2f, 10));
107 }
108
109 #ifndef AB_CUSTOM_SETTINGS
getSettings()110 Settings *getSettings()
111 {
112 static_assert(std::is_same_v<AB_SETTINGS_CLASS, Settings>,
113 "`AB_SETTINGS_CLASS` must be the same as `Settings`");
114
115 assert(AB_SETTINGS_CLASS::instance);
116
117 return AB_SETTINGS_CLASS::instance;
118 }
119 #endif
120
getABSettings()121 AB_SETTINGS_CLASS *getABSettings()
122 {
123 assert(AB_SETTINGS_CLASS::instance);
124
125 return AB_SETTINGS_CLASS::instance;
126 }
127
128 } // namespace chatterino
129