From 520bd07413990c1f3a6a1b3f8039a7da2b97164d Mon Sep 17 00:00:00 2001 From: Xein Date: Mon, 23 Nov 2026 11:29:14 +0100 Subject: [PATCH] multi-file ui support --- engine/src/toast/ui/document_preprocess.cpp | 83 ++++++++++++++++++--- engine/src/toast/ui/document_preprocess.hpp | 10 +++ engine/src/toast/ui/nodes/panel.cpp | 2 + engine/src/toast/ui/nodes/panel_3d.cpp | 2 + engine/src/toast/ui/ui_system.cpp | 24 ++++++ engine/src/toast/ui/ui_system.hpp | 5 ++ 6 files changed, 116 insertions(+), 10 deletions(-) diff --git a/engine/src/toast/ui/document_preprocess.cpp b/engine/src/toast/ui/document_preprocess.cpp index 3cc3d3bd8..209296f28 100644 --- a/engine/src/toast/ui/document_preprocess.cpp +++ b/engine/src/toast/ui/document_preprocess.cpp @@ -225,13 +225,16 @@ auto resolveColorReferences(std::string_view source, const ColorResolver& resolv return result; } -auto preprocessDocument(std::string_view rml, const PreprocessContext& ctx) -> DocumentScan { - ZoneScoped; - DocumentScan scan; +namespace { + +struct ScanState { + DocumentScan& scan; std::vector aliases; // data-for loop variables, locals of the document std::vector member_roots; // identifiers read through . or [ std::vector array_candidates; // plain containers of data-for +}; +void scanMarkup(std::string_view rml, ScanState& state, std::vector& template_links) { // can attributes and {{ }} expressions size_t i = 0; while (i < rml.size()) { @@ -246,7 +249,7 @@ auto preprocessDocument(std::string_view rml, const PreprocessContext& ctx) -> D if (end == std::string_view::npos) { break; } - collectIdentifiers(rml.substr(i + 2, end - i - 2), scan.binds, &member_roots); + collectIdentifiers(rml.substr(i + 2, end - i - 2), state.scan.binds, &state.member_roots); i = end + 2; continue; } @@ -256,6 +259,14 @@ auto preprocessDocument(std::string_view rml, const PreprocessContext& ctx) -> D const size_t tag_end = rml.find('>', i); const std::string_view tag = rml.substr(i, tag_end == std::string_view::npos ? rml.size() - i : tag_end - i); + if (tag.size() > 5 && findCaseInsensitive(tag.substr(0, 5), "empty() && *type == "text/template") { + template_links.emplace_back(*href); + } + } + size_t a = tag.find_first_of(" \t\r\n"); if (a == std::string_view::npos) { i = tag_end == std::string_view::npos ? rml.size() : tag_end + 1; @@ -302,21 +313,21 @@ auto preprocessDocument(std::string_view rml, const PreprocessContext& ctx) -> D } if (name.starts_with("data-event-") || (name.starts_with("on") && value.contains('('))) { - collectEventName(value, scan.events); + collectEventName(value, state.scan.events); } else if (name == "data-for") { // "item : items" or "item, index : items" binds the container std::string_view container; - parseDataFor(value, aliases, container); + parseDataFor(value, state.aliases, container); if (!container.empty()) { - collectIdentifiers(container, scan.binds, &member_roots); + collectIdentifiers(container, state.scan.binds, &state.member_roots); if (const auto plain = asPlainIdentifier(container); !plain.empty()) { - pushUnique(array_candidates, std::string(plain)); + pushUnique(state.array_candidates, std::string(plain)); } } } else if (name.starts_with("data-") && name != "data-model" && !name.starts_with("data-loc-")) { - collectIdentifiers(value, scan.binds, &member_roots); + collectIdentifiers(value, state.scan.binds, &state.member_roots); if (name == "data-checked") { - collectIdentifiers(value, scan.bool_binds); + collectIdentifiers(value, state.scan.bool_binds); } } } @@ -327,6 +338,58 @@ auto preprocessDocument(std::string_view rml, const PreprocessContext& ctx) -> D i++; } +} + +void scanTemplates(const PreprocessContext& ctx, ScanState& state, const std::vector& document_links) { + if (!ctx.template_loader) { + return; + } + + struct Pending { + std::string referrer; + std::string href; + }; + + std::vector pending; + pending.reserve(document_links.size()); + for (const auto& href : document_links) { + pending.push_back({ctx.document_uri, href}); + } + + std::vector visited {ctx.document_uri}; + for (size_t i = 0; i < pending.size(); ++i) { + const Pending link = pending[i]; // copied, the vector grows below + const auto loaded = ctx.template_loader(link.referrer, link.href); + if (!loaded) { + TOAST_WARN("UI", "Template '{}' linked from '{}' could not be read, its binds are not bound", link.href, link.referrer); + continue; + } + if (std::ranges::find(visited, loaded->uri) != visited.end()) { + continue; + } + visited.push_back(loaded->uri); + + std::vector nested; + scanMarkup(loaded->source, state, nested); + for (auto& href : nested) { + pending.push_back({loaded->uri, std::move(href)}); + } + } +} + +} + +auto preprocessDocument(std::string_view rml, const PreprocessContext& ctx) -> DocumentScan { + ZoneScoped; + DocumentScan scan; + ScanState state {.scan = scan}; + std::vector template_links; + scanMarkup(rml, state, template_links); + scanTemplates(ctx, state, template_links); + + auto& aliases = state.aliases; + auto& member_roots = state.member_roots; + auto& array_candidates = state.array_candidates; // loop variables are locals of their `data-for` const auto is_alias = [&](const std::string& name) { return std::ranges::find(aliases, name) != aliases.end(); }; diff --git a/engine/src/toast/ui/document_preprocess.hpp b/engine/src/toast/ui/document_preprocess.hpp index 44a5d5400..49bb618de 100644 --- a/engine/src/toast/ui/document_preprocess.hpp +++ b/engine/src/toast/ui/document_preprocess.hpp @@ -18,10 +18,20 @@ namespace ui { using ColorResolver = std::function(std::string_view)>; +struct TemplateSource { + std::string uri; + std::string source; +}; + +using TemplateLoader = std::function(std::string_view referrer, std::string_view href)>; + struct PreprocessContext { std::vector style_uris; ColorResolver color_resolver; bool inject_data_model = false; + + std::string document_uri; + TemplateLoader template_loader; }; struct DocumentScan { diff --git a/engine/src/toast/ui/nodes/panel.cpp b/engine/src/toast/ui/nodes/panel.cpp index 13a8d540c..1d9c28cfe 100644 --- a/engine/src/toast/ui/nodes/panel.cpp +++ b/engine/src/toast/ui/nodes/panel.cpp @@ -157,6 +157,8 @@ void Panel::loadDocument() { preprocess_ctx.inject_data_model = true; preprocess_ctx.style_uris = ui.globalStyleUris(); preprocess_ctx.color_resolver = [&ui](std::string_view name) { return ui.colorHex(name); }; + preprocess_ctx.document_uri = std::string(m_element.path()); + preprocess_ctx.template_loader = &ui::UISystem::loadTemplateSource; const auto scan = ui::preprocessDocument(m_element->source(), preprocess_ctx); m_bind_store->reconcile(scan); diff --git a/engine/src/toast/ui/nodes/panel_3d.cpp b/engine/src/toast/ui/nodes/panel_3d.cpp index 8c2051cfa..145db39a8 100644 --- a/engine/src/toast/ui/nodes/panel_3d.cpp +++ b/engine/src/toast/ui/nodes/panel_3d.cpp @@ -186,6 +186,8 @@ void Panel3D::loadDocument() { preprocess_ctx.inject_data_model = true; preprocess_ctx.style_uris = ui.globalStyleUris(); preprocess_ctx.color_resolver = [&ui](std::string_view name) { return ui.colorHex(name); }; + preprocess_ctx.document_uri = std::string(m_element.path()); + preprocess_ctx.template_loader = &ui::UISystem::loadTemplateSource; const auto scan = ui::preprocessDocument(m_element->source(), preprocess_ctx); m_bind_store->reconcile(scan); diff --git a/engine/src/toast/ui/ui_system.cpp b/engine/src/toast/ui/ui_system.cpp index 701ecc6c7..aeb2240f5 100644 --- a/engine/src/toast/ui/ui_system.cpp +++ b/engine/src/toast/ui/ui_system.cpp @@ -41,6 +41,7 @@ UISystem::UISystem() noexcept { m_asset_listener.subscribe([this](const event::UIAssetReloaded& e) { m_documents_reload_pending = true; m_stylesheet_cache_dirty = m_stylesheet_cache_dirty || e.type == "ui_style" || e.type == "color_scheme"; + m_template_cache_dirty = m_template_cache_dirty || e.type == "ui_element"; TOAST_INFO("UI", "Queued document reload after {} changed ({})", e.type, e.uid); }); @@ -207,6 +208,10 @@ void UISystem::tick() noexcept { Rml::Factory::ClearStyleSheetCache(); m_stylesheet_cache_dirty = false; } + if (m_template_cache_dirty) { + Rml::Factory::ClearTemplateCache(); + m_template_cache_dirty = false; + } reloadAllDocuments(); } @@ -319,6 +324,25 @@ void UISystem::unregisterGlobalStyle(const assets::Handle& styl } } +auto UISystem::loadTemplateSource(std::string_view referrer, std::string_view href) -> std::optional { + Rml::SystemInterface* system = Rml::GetSystemInterface(); + if (system == nullptr) { + return std::nullopt; + } + + Rml::String uri; + system->JoinPath(uri, Rml::String(referrer), Rml::String(href)); + if (!uri.contains("://")) { + return std::nullopt; + } + + auto bytes = assets::AssetManager::get().tryLoadBytes(uri); + if (!bytes) { + return std::nullopt; + } + return TemplateSource {.uri = std::move(uri), .source = std::string(bytes->begin(), bytes->end())}; +} + auto UISystem::globalStyleUris() const -> std::vector { std::vector uris; uris.reserve(m_global_styles.size()); diff --git a/engine/src/toast/ui/ui_system.hpp b/engine/src/toast/ui/ui_system.hpp index 3691667ff..73c3558d4 100644 --- a/engine/src/toast/ui/ui_system.hpp +++ b/engine/src/toast/ui/ui_system.hpp @@ -8,6 +8,7 @@ #pragma once #include "assets.hpp" +#include "document_preprocess.hpp" #include #include @@ -106,6 +107,9 @@ class UISystem { void setLanguage(std::string language); void reloadAllDocuments(); + [[nodiscard]] + static auto loadTemplateSource(std::string_view referrer, std::string_view href) -> std::optional; + [[nodiscard]] auto translate(std::string_view input) const -> std::optional; [[nodiscard]] @@ -169,6 +173,7 @@ class UISystem { bool m_documents_reload_pending = false; bool m_stylesheet_cache_dirty = false; + bool m_template_cache_dirty = false; uint64_t m_next_context_id = 1; ///< context names must be unique uint64_t m_frame_counter = 0;