Skip to content

Commit

Permalink
impr: Use sidebar for settings categories instead of tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
WerWolv committed Apr 10, 2024
1 parent 4115184 commit 5d08499
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 50 deletions.
3 changes: 3 additions & 0 deletions plugins/builtin/include/content/views/view_settings.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <hex/api/content_registry.hpp>
#include <hex/ui/view.hpp>

namespace hex::plugin::builtin {
Expand All @@ -21,6 +22,8 @@ namespace hex::plugin::builtin {
private:
bool m_restartRequested = false;
bool m_triggerPopup = false;

const ContentRegistry::Settings::impl::Category *m_selectedCategory = nullptr;
};

}
119 changes: 69 additions & 50 deletions plugins/builtin/source/content/views/view_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,78 +49,97 @@ namespace hex::plugin::builtin {
}

void ViewSettings::drawContent() {
if (ImGui::BeginTabBar("settings")) {
auto &categories = ContentRegistry::Settings::impl::getSettings();
if (ImGui::BeginTable("Settings", 2, ImGuiTableFlags_BordersInner)) {
ImGui::TableSetupColumn("##category", ImGuiTableColumnFlags_WidthFixed, 120_scaled);
ImGui::TableSetupColumn("##settings", ImGuiTableColumnFlags_WidthStretch);

// Draw all categories
for (auto &category : categories) {
ImGui::TableNextRow();
ImGui::TableNextColumn();

// Skip empty categories
if (category.subCategories.empty())
continue;
{
auto &categories = ContentRegistry::Settings::impl::getSettings();

// For each category, create a new tab
if (ImGui::BeginTabItem(Lang(category.unlocalizedName))) {
if (ImGui::BeginChild("scrolling")) {
// Draw all categories
bool categorySet = false;
for (auto &category : categories) {
// Skip empty categories
if (category.subCategories.empty())
continue;

// Draw the category description
if (!category.unlocalizedDescription.empty()) {
ImGuiExt::TextFormattedWrapped("{}", Lang(category.unlocalizedDescription));
ImGui::NewLine();
}
if (ImGui::Selectable(Lang(category.unlocalizedName), m_selectedCategory == &category, ImGuiSelectableFlags_DontClosePopups) || m_selectedCategory == nullptr)
m_selectedCategory = &category;

if (m_selectedCategory == &category)
categorySet = true;
}

// Draw all settings of that category
for (auto &subCategory : category.subCategories) {
if (!categorySet)
m_selectedCategory = nullptr;
}

ImGui::TableNextColumn();

// Skip empty subcategories
if (subCategory.entries.empty())
continue;
if (m_selectedCategory != nullptr) {
auto &category = *m_selectedCategory;

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 (ImGui::BeginChild("scrolling")) {

if (auto tooltip = setting.widget->getTooltip(); tooltip.has_value() && ImGui::IsItemHovered())
ImGuiExt::InfoTooltip(Lang(tooltip.value()));
// Draw the category description
if (!category.unlocalizedDescription.empty()) {
ImGuiExt::TextFormattedWrapped("{}", Lang(category.unlocalizedDescription));
ImGui::NewLine();
}

auto &widget = setting.widget;
// Draw all settings of that category
for (auto &subCategory : category.subCategories) {

// Handle a setting being changed
if (settingChanged) {
auto newValue = widget->store();
// Skip empty subcategories
if (subCategory.entries.empty())
continue;

// Write new value to settings
ContentRegistry::Settings::write<nlohmann::json>(category.unlocalizedName, setting.unlocalizedName, newValue);
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();

// Print a debug message
log::debug("Setting [{} / {}]: Value was changed to {}", category.unlocalizedName.get(), setting.unlocalizedName.get(), nlohmann::to_string(newValue));
if (auto tooltip = setting.widget->getTooltip(); tooltip.has_value() && ImGui::IsItemHovered())
ImGuiExt::InfoTooltip(Lang(tooltip.value()));

// Signal that the setting was changed
widget->onChanged();
auto &widget = setting.widget;

// Request a restart if the setting requires it
if (widget->doesRequireRestart()) {
m_restartRequested = true;
m_triggerPopup = true;
}
// 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();
}
ImGuiExt::EndSubWindow();
ImGui::NewLine();
}
ImGui::EndChild();

ImGui::EndTabItem();
}
ImGui::EndChild();
}

ImGui::EndTabBar();
ImGui::EndTable();
}
}

Expand Down

0 comments on commit 5d08499

Please sign in to comment.