From a06aeb10c1b40fa544ebf876b1e18b30e995e018 Mon Sep 17 00:00:00 2001 From: Luke Street Date: Thu, 30 Apr 2026 16:13:02 -0600 Subject: [PATCH] Extract TabBar component --- files.cmake | 8 +-- res/rml/popup.rml | 4 +- res/rml/window.rml | 5 +- src/dusk/ui/popup.cpp | 115 ++++++++-------------------------- src/dusk/ui/popup.hpp | 12 ++-- src/dusk/ui/tab_bar.cpp | 122 +++++++++++++++++++++++++++++++++++++ src/dusk/ui/tab_bar.hpp | 41 +++++++++++++ src/dusk/ui/tab_button.cpp | 24 -------- src/dusk/ui/tab_button.hpp | 14 ----- src/dusk/ui/window.cpp | 109 +++++++-------------------------- src/dusk/ui/window.hpp | 10 +-- 11 files changed, 227 insertions(+), 237 deletions(-) create mode 100644 src/dusk/ui/tab_bar.cpp create mode 100644 src/dusk/ui/tab_bar.hpp delete mode 100644 src/dusk/ui/tab_button.cpp delete mode 100644 src/dusk/ui/tab_button.hpp diff --git a/files.cmake b/files.cmake index c95bd904bd..798805a7c4 100644 --- a/files.cmake +++ b/files.cmake @@ -1485,12 +1485,12 @@ set(DUSK_FILES src/dusk/ui/settings.hpp src/dusk/ui/string_button.cpp src/dusk/ui/string_button.hpp - src/dusk/ui/tab_button.cpp - src/dusk/ui/tab_button.hpp - src/dusk/ui/ui.hpp + src/dusk/ui/tab_bar.cpp + src/dusk/ui/tab_bar.hpp src/dusk/ui/ui.cpp - src/dusk/ui/window.hpp + src/dusk/ui/ui.hpp src/dusk/ui/window.cpp + src/dusk/ui/window.hpp src/dusk/achievements.cpp src/dusk/iso_validate.cpp src/dusk/livesplit.cpp diff --git a/res/rml/popup.rml b/res/rml/popup.rml index 967c5d9fb3..0a9f4824e8 100644 --- a/res/rml/popup.rml +++ b/res/rml/popup.rml @@ -4,8 +4,6 @@ - + diff --git a/res/rml/window.rml b/res/rml/window.rml index a77728d00d..4947c281c7 100644 --- a/res/rml/window.rml +++ b/res/rml/window.rml @@ -4,9 +4,6 @@ -
-
-
-
+
diff --git a/src/dusk/ui/popup.cpp b/src/dusk/ui/popup.cpp index e78c615117..e2f5815e50 100644 --- a/src/dusk/ui/popup.cpp +++ b/src/dusk/ui/popup.cpp @@ -5,60 +5,36 @@ #include "aurora/rmlui.hpp" #include "editor.hpp" #include "settings.hpp" -#include "tab_button.hpp" #include "ui.hpp" #include "window.hpp" -#include -#include #include namespace dusk::ui { -Popup::Popup() : Document("res/rml/popup.rml") { - auto* tabBar = mDocument->GetElementById("tab-bar"); - if (tabBar == nullptr) { - return; - } - - const std::array tabLabels = { - "Settings", - "Warp", - "Editor", - "Reset", - "Exit", - }; - - // TODO: Make warp, reset, and exit buttons work - mTabActions = { - [this] { - hide(); - // TODO: make this better - auto& settingsWindow = add_document(std::make_unique()); - settingsWindow.show(); - set_selected_tab(0); - }, - [this] { set_selected_tab(1); }, - [this] { - hide(); - // TODO: make this better - auto& editorWindow = add_document(std::make_unique()); - editorWindow.show(); - set_selected_tab(2); - }, - [this] { set_selected_tab(3); }, - [this] { set_selected_tab(4); }, - }; - - mTabs.reserve(tabLabels.size()); - for (int i = 0; i < static_cast(tabLabels.size()); ++i) { - mTabs.push_back( - create_tab_button(tabBar, tabLabels[i], i == mSelectedTabIndex, [this, i](Rml::Event&) { - if (i >= 0 && i < static_cast(mTabActions.size())) { - mTabActions[i](); - } - })); - } +Popup::Popup() : Document("res/rml/popup.rml"), mRoot(mDocument->GetElementById("popup")) { + mTabBar = std::make_unique(mRoot, TabBar::Props{.autoSelect = false}); + mTabBar->add_tab("Settings", [this] { + hide(); + // TODO: make this better + auto& settingsWindow = add_document(std::make_unique()); + settingsWindow.show(); + }); + mTabBar->add_tab("Warp", [] { + // TODO + }); + mTabBar->add_tab("Editor", [this] { + hide(); + // TODO: make this better + auto& editorWindow = add_document(std::make_unique()); + editorWindow.show(); + }); + mTabBar->add_tab("Reset", [] { + // TODO + }); + mTabBar->add_tab("Exit", [] { + // TODO + }); } void Popup::show() { @@ -101,20 +77,6 @@ bool Popup::is_visible() const { } bool Popup::handle_nav_command(Rml::Event& event, NavCommand cmd) { - if (cmd == NavCommand::Left || cmd == NavCommand::Previous) { - focus_tab(std::max(0, mSelectedTabIndex - 1)); - return true; - } - if (cmd == NavCommand::Right || cmd == NavCommand::Next) { - focus_tab(std::min(static_cast(mTabs.size()) - 1, mSelectedTabIndex + 1)); - return true; - } - if (cmd == NavCommand::Confirm && mSelectedTabIndex >= 0 && - mSelectedTabIndex < static_cast(mTabActions.size())) - { - mTabActions[mSelectedTabIndex](); - return true; - } if (cmd == NavCommand::Cancel) { hide(); return true; @@ -130,36 +92,11 @@ void Popup::update() { mDocument->Hide(); mHideDeadline.reset(); } - if (mTabs.empty()) { - return; - } - std::vector tabs; - tabs.reserve(mTabs.size()); - for (const auto& tab : mTabs) { - tabs.push_back(tab.get()); - } - ui::set_selected_tab(tabs, mSelectedTabIndex); + Document::update(); } -void Popup::set_selected_tab(int index) { - if (index < 0 || index >= static_cast(mTabs.size())) { - return; - } - mSelectedTabIndex = index; - std::vector tabs; - tabs.reserve(mTabs.size()); - for (const auto& tab : mTabs) { - tabs.push_back(tab.get()); - } - ui::set_selected_tab(tabs, mSelectedTabIndex); -} - -bool Popup::focus_tab(int index) { - if (index < 0 || index >= static_cast(mTabs.size())) { - return false; - } - set_selected_tab(index); - return mTabs[index]->focus(); +bool Popup::focus() { + return mTabBar->focus(); } } // namespace dusk::ui diff --git a/src/dusk/ui/popup.hpp b/src/dusk/ui/popup.hpp index 7cd437d441..727b211eba 100644 --- a/src/dusk/ui/popup.hpp +++ b/src/dusk/ui/popup.hpp @@ -2,13 +2,14 @@ #include "button.hpp" #include "document.hpp" -#include "event.hpp" #include #include #include #include +#include "tab_bar.hpp" + namespace dusk::ui { class Popup : public Document { @@ -21,6 +22,7 @@ public: void show() override; void hide() override; void update() override; + bool focus() override; void toggle(); bool is_visible() const; @@ -29,13 +31,9 @@ protected: bool handle_nav_command(Rml::Event& event, NavCommand cmd) override; private: - void set_selected_tab(int index); - bool focus_tab(int index); - - std::vector > mTabs; - std::vector > mTabActions; + Rml::Element* mRoot; + std::unique_ptr mTabBar; std::unique_ptr