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-12-23 21:09:41 +01:00
|
|
|
#include <popups/popup_question.hpp>
|
2024-01-28 22:14:59 +01:00
|
|
|
#include <fonts/codicons_font.h>
|
2023-04-08 00:58:53 +02:00
|
|
|
|
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);
|
2024-01-08 21:51:48 +01:00
|
|
|
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.extras", "hex.builtin.view.settings.name" }, ICON_VS_SETTINGS_GEAR, 4000, Shortcut::None, [&, this] {
|
2023-03-20 13:11:43 +01:00
|
|
|
this->getWindowOpenState() = true;
|
2022-01-23 02:28:38 +01:00
|
|
|
});
|
2024-01-21 18:39:32 +01:00
|
|
|
|
|
|
|
EventImHexStartupFinished::subscribe(this, [] {
|
|
|
|
for (const auto &[unlocalizedCategory, unlocalizedDescription, subCategories] : ContentRegistry::Settings::impl::getSettings()) {
|
|
|
|
for (const auto &[unlocalizedSubCategory, entries] : subCategories) {
|
|
|
|
for (const auto &[unlocalizedName, widget] : entries) {
|
|
|
|
try {
|
2024-03-25 20:41:05 +01:00
|
|
|
auto defaultValue = widget->store();
|
|
|
|
widget->load(ContentRegistry::Settings::impl::getSetting(unlocalizedCategory, unlocalizedName, defaultValue));
|
2024-01-21 18:39:32 +01:00
|
|
|
widget->onChanged();
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
log::error("Failed to load setting [{} / {}]: {}", unlocalizedCategory.get(), unlocalizedName.get(), e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-01-11 20:31:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ViewSettings::~ViewSettings() {
|
2023-12-08 10:29:44 +01:00
|
|
|
RequestOpenWindow::unsubscribe(this);
|
2024-01-21 18:39:32 +01:00
|
|
|
EventImHexStartupFinished::unsubscribe(this);
|
2021-01-11 20:31:40 +01:00
|
|
|
}
|
|
|
|
|
2023-12-04 22:17:43 +01:00
|
|
|
void ViewSettings::drawContent() {
|
2024-04-10 19:55:37 +02:00
|
|
|
if (ImGui::BeginTable("Settings", 2, ImGuiTableFlags_BordersInner)) {
|
|
|
|
ImGui::TableSetupColumn("##category", ImGuiTableColumnFlags_WidthFixed, 120_scaled);
|
|
|
|
ImGui::TableSetupColumn("##settings", ImGuiTableColumnFlags_WidthStretch);
|
2022-02-18 22:34:54 +01:00
|
|
|
|
2024-04-10 19:55:37 +02:00
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
2022-02-18 22:34:54 +01:00
|
|
|
|
2024-04-10 19:55:37 +02:00
|
|
|
{
|
|
|
|
auto &categories = ContentRegistry::Settings::impl::getSettings();
|
2022-02-18 22:34:54 +01:00
|
|
|
|
2024-04-10 19:55:37 +02:00
|
|
|
// Draw all categories
|
|
|
|
bool categorySet = false;
|
|
|
|
for (auto &category : categories) {
|
|
|
|
// Skip empty categories
|
|
|
|
if (category.subCategories.empty())
|
|
|
|
continue;
|
2023-11-17 16:05:45 +01:00
|
|
|
|
2024-08-03 23:49:47 +02:00
|
|
|
if (ImGui::Selectable(Lang(category.unlocalizedName), m_selectedCategory == &category, ImGuiSelectableFlags_NoAutoClosePopups) || m_selectedCategory == nullptr)
|
2024-04-10 19:55:37 +02:00
|
|
|
m_selectedCategory = &category;
|
|
|
|
|
|
|
|
if (m_selectedCategory == &category)
|
|
|
|
categorySet = true;
|
|
|
|
}
|
2023-08-29 12:14:12 +02:00
|
|
|
|
2024-04-10 19:55:37 +02:00
|
|
|
if (!categorySet)
|
|
|
|
m_selectedCategory = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::TableNextColumn();
|
2022-02-18 22:34:54 +01:00
|
|
|
|
2024-04-10 19:55:37 +02:00
|
|
|
if (m_selectedCategory != nullptr) {
|
|
|
|
auto &category = *m_selectedCategory;
|
2023-08-29 12:14:12 +02:00
|
|
|
|
2024-04-10 19:55:37 +02:00
|
|
|
if (ImGui::BeginChild("scrolling")) {
|
2023-10-21 23:07:33 +02:00
|
|
|
|
2024-04-10 19:55:37 +02:00
|
|
|
// Draw the category description
|
|
|
|
if (!category.unlocalizedDescription.empty()) {
|
|
|
|
ImGuiExt::TextFormattedWrapped("{}", Lang(category.unlocalizedDescription));
|
|
|
|
ImGui::NewLine();
|
|
|
|
}
|
2023-10-21 23:07:33 +02:00
|
|
|
|
2024-04-10 19:55:37 +02:00
|
|
|
// Draw all settings of that category
|
|
|
|
for (auto &subCategory : category.subCategories) {
|
2023-10-21 23:07:33 +02:00
|
|
|
|
2024-04-10 19:55:37 +02:00
|
|
|
// Skip empty subcategories
|
|
|
|
if (subCategory.entries.empty())
|
|
|
|
continue;
|
2023-10-21 23:07:33 +02:00
|
|
|
|
2024-06-25 13:54:29 +02:00
|
|
|
if (ImGuiExt::BeginSubWindow(Lang(subCategory.unlocalizedName))) {
|
2024-04-10 19:55:37 +02:00
|
|
|
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
|
|
|
|
2024-05-19 15:10:22 +02:00
|
|
|
if (const auto &tooltip = setting.widget->getTooltip(); tooltip.has_value() && ImGui::IsItemHovered())
|
2024-04-10 19:55:37 +02:00
|
|
|
ImGuiExt::InfoTooltip(Lang(tooltip.value()));
|
2023-10-21 23:07:33 +02:00
|
|
|
|
2024-04-10 19:55:37 +02:00
|
|
|
auto &widget = setting.widget;
|
2023-10-21 23:07:33 +02:00
|
|
|
|
2024-04-10 19:55:37 +02:00
|
|
|
// Handle a setting being changed
|
|
|
|
if (settingChanged) {
|
|
|
|
auto newValue = widget->store();
|
|
|
|
|
|
|
|
// Write new value to settings
|
|
|
|
ContentRegistry::Settings::write<nlohmann::json>(category.unlocalizedName, setting.unlocalizedName, newValue);
|
|
|
|
|
|
|
|
// Print a debug message
|
|
|
|
log::debug("Setting [{} / {}]: Value was changed to {}", category.unlocalizedName.get(), setting.unlocalizedName.get(), nlohmann::to_string(newValue));
|
|
|
|
|
|
|
|
// Signal that the setting was changed
|
|
|
|
widget->onChanged();
|
|
|
|
|
|
|
|
// Request a restart if the setting requires it
|
|
|
|
if (widget->doesRequireRestart()) {
|
|
|
|
m_restartRequested = true;
|
|
|
|
m_triggerPopup = true;
|
2023-10-21 23:07:33 +02:00
|
|
|
}
|
|
|
|
}
|
2022-02-21 21:46:25 +01:00
|
|
|
}
|
2024-06-25 13:54:29 +02:00
|
|
|
|
2021-09-20 23:40:36 +02:00
|
|
|
}
|
2024-06-26 19:11:31 +02:00
|
|
|
ImGuiExt::EndSubWindow();
|
2024-04-10 19:55:37 +02:00
|
|
|
ImGui::NewLine();
|
2021-09-20 23:40:36 +02:00
|
|
|
}
|
2023-12-04 22:17:43 +01:00
|
|
|
}
|
2024-04-10 19:55:37 +02:00
|
|
|
ImGui::EndChild();
|
2021-01-11 20:31:40 +01:00
|
|
|
}
|
2023-12-04 22:17:43 +01:00
|
|
|
|
2024-04-10 19:55:37 +02:00
|
|
|
ImGui::EndTable();
|
2023-12-04 22:17:43 +01:00
|
|
|
}
|
2023-12-10 22:37:26 +01:00
|
|
|
}
|
2022-02-21 21:46:25 +01:00
|
|
|
|
2023-12-10 22:37:26 +01:00
|
|
|
void ViewSettings::drawAlwaysVisibleContent() {
|
2023-08-29 12:14:12 +02:00
|
|
|
// If a restart is required, ask the user if they want to restart
|
2023-12-19 13:10:25 +01:00
|
|
|
if (!this->getWindowOpenState() && m_triggerPopup) {
|
|
|
|
m_triggerPopup = false;
|
2023-12-23 21:09:41 +01:00
|
|
|
ui::PopupQuestion::open("hex.builtin.view.settings.restart_question"_lang,
|
2023-11-24 11:29:05 +01:00
|
|
|
ImHexApi::System::restartImHex,
|
|
|
|
[this]{
|
2023-12-19 13:10:25 +01:00
|
|
|
m_restartRequested = false;
|
2023-11-24 11:29:05 +01:00
|
|
|
}
|
|
|
|
);
|
2022-02-21 21:46:25 +01:00
|
|
|
}
|
2021-01-11 20:31:40 +01:00
|
|
|
}
|
|
|
|
|
2023-12-10 22:37:26 +01:00
|
|
|
|
2022-02-18 22:34:54 +01:00
|
|
|
}
|