2021-12-07 22:47:41 +01:00
|
|
|
#include "content/views/view_settings.hpp"
|
2021-01-11 20:31:40 +01:00
|
|
|
|
2021-01-13 17:28:27 +01:00
|
|
|
#include <hex/api/content_registry.hpp>
|
2022-02-21 21:46:25 +01:00
|
|
|
#include <hex/helpers/logger.hpp>
|
|
|
|
|
2021-08-29 14:18:45 +02:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
2023-04-08 00:58:53 +02:00
|
|
|
#include <content/popups/popup_question.hpp>
|
|
|
|
|
2021-12-07 22:47:41 +01:00
|
|
|
namespace hex::plugin::builtin {
|
2021-01-11 20:31:40 +01:00
|
|
|
|
2023-12-04 22:17:43 +01:00
|
|
|
ViewSettings::ViewSettings() : View::Modal("hex.builtin.view.settings.name") {
|
2023-08-29 12:14:12 +02:00
|
|
|
// Handle window open requests
|
2023-12-08 10:29:44 +01:00
|
|
|
RequestOpenWindow::subscribe(this, [this](const std::string &name) {
|
2021-04-18 20:24:42 +02:00
|
|
|
if (name == "Settings") {
|
2022-09-06 10:50:17 +02:00
|
|
|
TaskManager::doLater([this] {
|
|
|
|
this->getWindowOpenState() = true;
|
|
|
|
});
|
2021-01-27 14:26:24 +01:00
|
|
|
}
|
|
|
|
});
|
2022-01-23 02:28:38 +01:00
|
|
|
|
2023-08-29 12:14:12 +02:00
|
|
|
// Add the settings menu item to the Extras menu
|
2023-05-12 08:38:32 +02:00
|
|
|
ContentRegistry::Interface::addMenuItemSeparator({ "hex.builtin.menu.extras" }, 3000);
|
|
|
|
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.extras", "hex.builtin.view.settings.name"_lang }, 4000, Shortcut::None, [&, this] {
|
2023-03-20 13:11:43 +01:00
|
|
|
this->getWindowOpenState() = true;
|
2022-01-23 02:28:38 +01:00
|
|
|
});
|
2021-01-11 20:31:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ViewSettings::~ViewSettings() {
|
2023-12-08 10:29:44 +01:00
|
|
|
RequestOpenWindow::unsubscribe(this);
|
2021-01-11 20:31:40 +01:00
|
|
|
}
|
|
|
|
|
2023-12-04 22:17:43 +01:00
|
|
|
void ViewSettings::drawContent() {
|
|
|
|
if (ImGui::BeginTabBar("settings")) {
|
|
|
|
auto &categories = ContentRegistry::Settings::impl::getSettings();
|
2022-02-18 22:34:54 +01:00
|
|
|
|
2023-12-04 22:17:43 +01:00
|
|
|
// Draw all categories
|
|
|
|
for (auto &category : categories) {
|
2022-02-18 22:34:54 +01:00
|
|
|
|
2023-12-04 22:17:43 +01:00
|
|
|
// Skip empty categories
|
|
|
|
if (category.subCategories.empty())
|
|
|
|
continue;
|
2022-02-18 22:34:54 +01:00
|
|
|
|
2023-12-04 22:17:43 +01:00
|
|
|
// For each category, create a new tab
|
|
|
|
if (ImGui::BeginTabItem(Lang(category.unlocalizedName))) {
|
|
|
|
if (ImGui::BeginChild("scrolling")) {
|
2023-11-17 16:05:45 +01:00
|
|
|
|
2023-12-04 22:17:43 +01:00
|
|
|
// Draw the category description
|
|
|
|
if (!category.unlocalizedDescription.empty()) {
|
|
|
|
ImGuiExt::TextFormattedWrapped("{}", Lang(category.unlocalizedDescription));
|
|
|
|
ImGui::NewLine();
|
|
|
|
}
|
2023-08-29 12:14:12 +02:00
|
|
|
|
2023-12-04 22:17:43 +01:00
|
|
|
// Draw all settings of that category
|
|
|
|
for (auto &subCategory : category.subCategories) {
|
2022-02-18 22:34:54 +01:00
|
|
|
|
2023-12-04 22:17:43 +01:00
|
|
|
// Skip empty subcategories
|
|
|
|
if (subCategory.entries.empty())
|
|
|
|
continue;
|
2023-08-29 12:14:12 +02:00
|
|
|
|
2023-12-04 22:17:43 +01:00
|
|
|
ImGuiExt::BeginSubWindow(Lang(subCategory.unlocalizedName));
|
|
|
|
{
|
|
|
|
for (auto &setting : subCategory.entries) {
|
|
|
|
ImGui::BeginDisabled(!setting.widget->isEnabled());
|
|
|
|
ImGui::PushItemWidth(-200_scaled);
|
|
|
|
bool settingChanged = setting.widget->draw(Lang(setting.unlocalizedName));
|
|
|
|
ImGui::PopItemWidth();
|
|
|
|
ImGui::EndDisabled();
|
2023-10-21 23:07:33 +02:00
|
|
|
|
2023-12-04 22:17:43 +01:00
|
|
|
if (auto tooltip = setting.widget->getTooltip(); tooltip.has_value() && ImGui::IsItemHovered())
|
|
|
|
ImGuiExt::InfoTooltip(Lang(tooltip.value()));
|
2023-10-21 23:07:33 +02:00
|
|
|
|
2023-12-04 22:17:43 +01:00
|
|
|
auto &widget = setting.widget;
|
2023-10-21 23:07:33 +02:00
|
|
|
|
2023-12-04 22:17:43 +01:00
|
|
|
// Handle a setting being changed
|
|
|
|
if (settingChanged) {
|
|
|
|
auto newValue = widget->store();
|
2023-10-21 23:07:33 +02:00
|
|
|
|
2023-12-04 22:17:43 +01:00
|
|
|
// Write new value to settings
|
|
|
|
ContentRegistry::Settings::write(category.unlocalizedName, setting.unlocalizedName, newValue);
|
2023-10-21 23:07:33 +02:00
|
|
|
|
2023-12-04 22:17:43 +01:00
|
|
|
// Print a debug message
|
|
|
|
log::debug("Setting [{} / {}]: Value was changed to {}", category.unlocalizedName, setting.unlocalizedName, nlohmann::to_string(newValue));
|
2023-10-21 23:07:33 +02:00
|
|
|
|
2023-12-04 22:17:43 +01:00
|
|
|
// Signal that the setting was changed
|
2023-12-08 10:29:44 +01:00
|
|
|
EventSettingsChanged::post();
|
2023-12-04 22:17:43 +01:00
|
|
|
widget->onChanged();
|
2023-10-21 23:07:33 +02:00
|
|
|
|
2023-12-04 22:17:43 +01:00
|
|
|
// Request a restart if the setting requires it
|
|
|
|
if (widget->doesRequireRestart())
|
|
|
|
this->m_restartRequested = true;
|
2023-10-21 23:07:33 +02:00
|
|
|
}
|
|
|
|
}
|
2022-02-21 21:46:25 +01:00
|
|
|
}
|
2023-12-04 22:17:43 +01:00
|
|
|
ImGuiExt::EndSubWindow();
|
2021-09-20 23:40:36 +02:00
|
|
|
}
|
|
|
|
}
|
2023-12-04 22:17:43 +01:00
|
|
|
ImGui::EndChild();
|
2021-09-20 23:40:36 +02:00
|
|
|
|
2023-12-04 22:17:43 +01:00
|
|
|
ImGui::EndTabItem();
|
|
|
|
}
|
2021-01-11 20:31:40 +01:00
|
|
|
}
|
2023-12-04 22:17:43 +01:00
|
|
|
|
|
|
|
ImGui::EndTabBar();
|
|
|
|
}
|
2022-02-21 21:46:25 +01:00
|
|
|
|
2023-08-29 12:14:12 +02:00
|
|
|
// If a restart is required, ask the user if they want to restart
|
2022-10-02 17:30:26 +02:00
|
|
|
if (!this->getWindowOpenState() && this->m_restartRequested) {
|
2023-11-24 11:29:05 +01:00
|
|
|
PopupQuestion::open("hex.builtin.view.settings.restart_question"_lang,
|
|
|
|
ImHexApi::System::restartImHex,
|
|
|
|
[this]{
|
|
|
|
this->m_restartRequested = false;
|
|
|
|
}
|
|
|
|
);
|
2022-02-21 21:46:25 +01:00
|
|
|
}
|
2021-01-11 20:31:40 +01:00
|
|
|
}
|
|
|
|
|
2022-02-18 22:34:54 +01:00
|
|
|
}
|