#include "window.hpp" #include #include "aurora/lib/logging.hpp" #include "aurora/rmlui.hpp" #include "button.hpp" #include "magic_enum.hpp" #include "ui.hpp" namespace dusk::ui { static aurora::Module Log{"dusk::ui::window"}; Window::Window() { auto* context = aurora::rmlui::get_context(); if (context == nullptr) { return; } mDocument = context->LoadDocument("res/rml/window.rml"); if (mDocument == nullptr) { return; } mKeyListener = std::make_unique( mDocument, Rml::EventId::Keydown, [this](Rml::Event& event) { // 1-9 for quick switching tabs const auto key = static_cast( event.GetParameter("key_identifier", Rml::Input::KI_UNKNOWN)); if (key >= Rml::Input::KeyIdentifier::KI_1 && key <= Rml::Input::KeyIdentifier::KI_9) { if (set_active_tab(key - Rml::Input::KeyIdentifier::KI_1)) { if (!mContentComponents.empty()) { mContentComponents.front()->focus(); } event.StopPropagation(); return; } } const auto cmd = map_nav_event(event); if (cmd == NavCommand::None) { return; } auto* target = event.GetTargetElement(); if (target->Closest(".tab-bar")) { if (handle_tab_bar_nav(event, cmd)) { event.StopPropagation(); } } else if (target->Closest(".content")) { if (handle_content_nav(event, cmd)) { event.StopPropagation(); } } }); } Window::~Window() { auto* context = aurora::rmlui::get_context(); if (context != nullptr && mDocument != nullptr) { context->UnloadDocument(mDocument); mDocument = nullptr; } } void Window::show() { if (mDocument != nullptr) { mDocument->Show(); } } void Window::hide() { if (mDocument != nullptr) { mDocument->Hide(); } } void Window::update() { for (const auto& component : mContentComponents) { component->update(); } } bool Window::set_active_tab(int index) { if (index < 0 || index >= mTabs.size() || index == mSelectedTabIndex) { return false; } const auto& tab = mTabs[index]; if (tab.button->focus()) { clear_content(); for (int i = 0; i < mTabs.size(); i++) { mTabs[i].button->set_selected(i == index); } mSelectedTabIndex = index; if (tab.builder) { tab.builder(mDocument->GetElementById("content")); } return true; } return false; } void Window::add_tab(const Rml::String& title, TabBuilder builder) { const int index = static_cast(mTabs.size()); auto* tabBar = mDocument->GetElementById("tab-bar"); mTabs.emplace_back(Tab{ .title = title, .button = std::make_unique