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>
|
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>
|
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-12-14 20:36:09 +01:00
|
|
|
#include <fonts/vscode_icons.hpp>
|
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);
|
2025-01-26 22:41:20 +01:00
|
|
|
ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.extras", "hex.builtin.view.settings.name" }, ICON_VS_SETTINGS_GEAR, 4000, CTRLCMD + Keys::Comma, [&, 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
|
|
|
|
2024-12-20 21:23:19 +01:00
|
|
|
EventImHexStartupFinished::subscribe(this, []{
|
2024-01-21 18:39:32 +01:00
|
|
|
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
|
|
|
} 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
|
2025-01-19 17:54:18 +01:00
|
|
|
u32 index = 0;
|
|
|
|
for (auto &subCategory : category.subCategories) {
|
|
|
|
ON_SCOPE_EXIT { index += 1; };
|
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();
|
2025-01-19 10:56:00 +01:00
|
|
|
|
|
|
|
if (index != i64(category.subCategories.size()) - 1)
|
|
|
|
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
|
|
|
}
|