From f5e1557261db817a9ca9a9a65e7e228462136214 Mon Sep 17 00:00:00 2001 From: chris bell Date: Sun, 12 Jul 2026 21:45:57 -0500 Subject: [PATCH] Can now add scoped rcss files --- assets/main.rml | 2 +- assets/pages/about.rml | 4 ---- assets/pages/home.rml | 14 ++++++++++++++ assets/pages/lua-test.rml | 4 +++- assets/pages/test-page.rcss | 3 +++ assets/pages/test-page.rml | 7 +++++++ assets/pages/testPage.rml | 5 ----- src/app.cpp | 14 ++++++++++++++ src/app.hpp | 2 ++ src/main.cpp | 8 ++++---- 10 files changed, 48 insertions(+), 15 deletions(-) delete mode 100644 assets/pages/about.rml create mode 100644 assets/pages/home.rml create mode 100644 assets/pages/test-page.rcss create mode 100644 assets/pages/test-page.rml delete mode 100644 assets/pages/testPage.rml diff --git a/assets/main.rml b/assets/main.rml index d17d059..5b1857e 100644 --- a/assets/main.rml +++ b/assets/main.rml @@ -7,8 +7,8 @@ diff --git a/assets/pages/about.rml b/assets/pages/about.rml deleted file mode 100644 index 8e0207c..0000000 --- a/assets/pages/about.rml +++ /dev/null @@ -1,4 +0,0 @@ -
-

About

-

This is a static page

-
\ No newline at end of file diff --git a/assets/pages/home.rml b/assets/pages/home.rml new file mode 100644 index 0000000..032f7eb --- /dev/null +++ b/assets/pages/home.rml @@ -0,0 +1,14 @@ +
+

Home

+

This is an RmlUi template project with some nice built in features such as:

+ +
\ No newline at end of file diff --git a/assets/pages/lua-test.rml b/assets/pages/lua-test.rml index f9e728c..a89e916 100644 --- a/assets/pages/lua-test.rml +++ b/assets/pages/lua-test.rml @@ -1,8 +1,10 @@

Lua test page

-

This button uses NavigateTo('about') lua code instead of a registered C++ event

+

This button uses NavigateTo('about') lua code instead of being harcoded in a C++ PageBase class:

+

If you have a .lua file with the same name as the page in the pages directory, it will be loaded at runtime so you can add functionality to a page like this:

+

This button calls LuaTest.Test() from the scoped `lua-test.lua` file

diff --git a/assets/pages/test-page.rcss b/assets/pages/test-page.rcss new file mode 100644 index 0000000..8903548 --- /dev/null +++ b/assets/pages/test-page.rcss @@ -0,0 +1,3 @@ +#page-root h1 { + color: red; +} \ No newline at end of file diff --git a/assets/pages/test-page.rml b/assets/pages/test-page.rml new file mode 100644 index 0000000..6d7c007 --- /dev/null +++ b/assets/pages/test-page.rml @@ -0,0 +1,7 @@ +
+

Test page

+

{{greeting}}

+ + +

If you have a .rcss file with the same name as the page in the pages directory, it will be loaded at runtime so you can add custom styling to a page, which is why the header is red on this page only.

+
\ No newline at end of file diff --git a/assets/pages/testPage.rml b/assets/pages/testPage.rml deleted file mode 100644 index 0843462..0000000 --- a/assets/pages/testPage.rml +++ /dev/null @@ -1,5 +0,0 @@ -
-

Test page

-

{{greeting}}

- -
\ No newline at end of file diff --git a/src/app.cpp b/src/app.cpp index 25a9b95..bcb3346 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -98,6 +99,17 @@ bool App::NavigateTo(const std::string &pageName) { } else { std::cout << "[WARN] failed to create data model for page '" << pageName << "'\n"; } + + std::string rcssPath = GetAssetsPath() + "/pages/" + pageName + ".rcss"; + if (std::filesystem::exists(rcssPath)) { + auto pageStyleSheet = Rml::Factory::InstanceStyleSheetFile(rcssPath); + if (pageStyleSheet && baseStyleSheet) { + auto combined = baseStyleSheet->CombineStyleSheetContainer(*pageStyleSheet); + doc->SetStyleSheetContainer(combined); + } + } else if (doc && baseStyleSheet) { + doc->SetStyleSheetContainer(baseStyleSheet); + } pageOutlet->SetInnerRML(content); @@ -125,6 +137,8 @@ bool App::LoadUi() { std::cout << "[WARN] main.rml has no element with id=\"page-content\"\n"; } + baseStyleSheet = Rml::Factory::InstanceStyleSheetFile(GetAssetsPath() + "/styles/main.rcss"); + listeners.clear(); if (onBindListeners) { onBindListeners(doc); diff --git a/src/app.hpp b/src/app.hpp index 24d381e..3b6f1f4 100644 --- a/src/app.hpp +++ b/src/app.hpp @@ -42,6 +42,8 @@ private: Rml::ElementDocument* doc; Rml::Element* pageOutlet = nullptr; + Rml::SharedPtr baseStyleSheet; + std::vector> listeners; std::function onBindListeners; diff --git a/src/main.cpp b/src/main.cpp index b3085da..96e7747 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -96,18 +96,18 @@ int main() { std::cout << "Loading UI...\n"; - app->RegisterPage("testPage"); + app->RegisterPage("test-page"); app->SetupListeners([](Rml::ElementDocument *doc) { if (auto *btn = doc->GetElementById("nav-test")) { app->RegisterListener(btn, std::make_unique([](Rml::Event&){ - app->NavigateTo("testPage"); + app->NavigateTo("test-page"); })); } - if (auto *btn = doc->GetElementById("nav-about")) { + if (auto *btn = doc->GetElementById("nav-home")) { app->RegisterListener(btn, std::make_unique([](Rml::Event&){ - app->NavigateTo("about"); + app->NavigateTo("home"); })); } });