1
0
mirror of synced 2025-02-11 00:03:05 +01:00
ImHex/plugins/builtin/source/content/views/view_settings.cpp

165 lines
6.8 KiB
C++
Raw Normal View History

2021-12-07 22:47:41 +01:00
#include "content/views/view_settings.hpp"
#include <hex/api/content_registry.hpp>
impr: Refactor and restructure Event Manager (#2082) ### Problem description This PR addresses issue #2013 that described a cluttered Event Manager. This is a DX issue and should not impact the users whatsoever. ### Implementation description The changes revolve around three main points: 1. the Event Manager (`event_manager.hpp`) was split into four categories: GUI, Interaction, Lifecycle, and Provider, and two types: Events, and Requests. This results in the following files: - `events_gui.hpp` - `events_interaction.hpp` - `events_lifecycle.hpp` - `events_provider.hpp` - `requests_gui.hpp` - `requests_interaction.hpp` - `requests_lifecycle.hpp` - `requests_provider.hpp` 2. Every event and request now has its own piece of documentation, with a `@brief`, accompanied by a longer comment if needed, and gets its `@param`s described. 3. The old `event_manager.hpp` import was removed and replaced by the correct imports wherever needed, as to reduce spread of those files only to where they are truly useful. ### Additional things The commits have been split into (chrono-)logical steps: - `feat`: split the Event Manager, and replace the imports - `refactor`, `chore`: make various small changes to match the required structure - `docs`: add documentation for events and requests Hopefully, this will help to review the PR. *Note: Beware of very long rebuild times in between the commits, use them sparingly! The Actions will ensure this PR builds anyways* Closes #2013 --------- Signed-off-by: BioTheWolff <47079795+BioTheWolff@users.noreply.github.com> Co-authored-by: Nik <werwolv98@gmail.com>
2025-01-25 16:32:07 +01:00
#include <hex/api/events/requests_gui.hpp>
#include <hex/helpers/logger.hpp>
#include <nlohmann/json.hpp>
#include <popups/popup_question.hpp>
2024-12-14 20:36:09 +01:00
#include <fonts/vscode_icons.hpp>
2021-12-07 22:47:41 +01:00
namespace hex::plugin::builtin {
ViewSettings::ViewSettings() : View::Modal("hex.builtin.view.settings.name") {
// Handle window open requests
RequestOpenWindow::subscribe(this, [this](const std::string &name) {
if (name == "Settings") {
TaskManager::doLater([this] {
this->getWindowOpenState() = true;
});
}
});
// Add the settings menu item to the Extras menu
ContentRegistry::Interface::addMenuItemSeparator({ "hex.builtin.menu.extras" }, 3000);
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.extras", "hex.builtin.view.settings.name" }, ICON_VS_SETTINGS_GEAR, 4000, CTRLCMD + Keys::Comma, [&, this] {
this->getWindowOpenState() = true;
});
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 {
auto defaultValue = widget->store();
widget->load(ContentRegistry::Settings::impl::getSetting(unlocalizedCategory, unlocalizedName, defaultValue));
} catch (const std::exception &e) {
log::error("Failed to load setting [{} / {}]: {}", unlocalizedCategory.get(), unlocalizedName.get(), e.what());
}
}
}
}
});
}
ViewSettings::~ViewSettings() {
RequestOpenWindow::unsubscribe(this);
EventImHexStartupFinished::unsubscribe(this);
}
void ViewSettings::drawContent() {
if (ImGui::BeginTable("Settings", 2, ImGuiTableFlags_BordersInner)) {
ImGui::TableSetupColumn("##category", ImGuiTableColumnFlags_WidthFixed, 120_scaled);
ImGui::TableSetupColumn("##settings", ImGuiTableColumnFlags_WidthStretch);
ImGui::TableNextRow();
ImGui::TableNextColumn();
{
auto &categories = ContentRegistry::Settings::impl::getSettings();
// Draw all categories
bool categorySet = false;
for (auto &category : categories) {
// Skip empty categories
if (category.subCategories.empty())
continue;
2024-08-03 23:49:47 +02:00
if (ImGui::Selectable(Lang(category.unlocalizedName), m_selectedCategory == &category, ImGuiSelectableFlags_NoAutoClosePopups) || m_selectedCategory == nullptr)
m_selectedCategory = &category;
if (m_selectedCategory == &category)
categorySet = true;
}
if (!categorySet)
m_selectedCategory = nullptr;
}
ImGui::TableNextColumn();
if (m_selectedCategory != nullptr) {
auto &category = *m_selectedCategory;
if (ImGui::BeginChild("scrolling")) {
// Draw the category description
if (!category.unlocalizedDescription.empty()) {
ImGuiExt::TextFormattedWrapped("{}", Lang(category.unlocalizedDescription));
ImGui::NewLine();
}
// Draw all settings of that category
u32 index = 0;
for (auto &subCategory : category.subCategories) {
ON_SCOPE_EXIT { index += 1; };
// Skip empty subcategories
if (subCategory.entries.empty())
continue;
if (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();
if (const auto &tooltip = setting.widget->getTooltip(); tooltip.has_value() && ImGui::IsItemHovered())
ImGuiExt::InfoTooltip(Lang(tooltip.value()));
auto &widget = setting.widget;
// 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;
}
}
}
}
ImGuiExt::EndSubWindow();
if (index != i64(category.subCategories.size()) - 1)
ImGui::NewLine();
}
}
ImGui::EndChild();
}
ImGui::EndTable();
}
2023-12-10 22:37:26 +01:00
}
2023-12-10 22:37:26 +01:00
void ViewSettings::drawAlwaysVisibleContent() {
// 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;
ui::PopupQuestion::open("hex.builtin.view.settings.restart_question"_lang,
ImHexApi::System::restartImHex,
[this]{
2023-12-19 13:10:25 +01:00
m_restartRequested = false;
}
);
}
}
2023-12-10 22:37:26 +01:00
}