From c28829bd725e624c540c90c0304714f7367a85b6 Mon Sep 17 00:00:00 2001 From: OmniaX-Dev Date: Tue, 7 Apr 2026 14:00:37 +0200 Subject: [PATCH] Added const variables to stylesheet --- .clangd | 3 +- extra/testTheme.txt | 4 ++- other/build.nr | 2 +- src/ostd/io/Stylesheet.cpp | 66 ++++++++++++++++++++++++++------------ src/ostd/io/Stylesheet.hpp | 2 +- src/ostd/string/String.cpp | 4 +-- src/test/GuiTest.cpp | 5 +++ 7 files changed, 59 insertions(+), 27 deletions(-) diff --git a/.clangd b/.clangd index 32169b9..122ab09 100644 --- a/.clangd +++ b/.clangd @@ -1,3 +1,2 @@ CompileFlags: - CompilationDatabase: bin - Add: ["--target=x86_64-w64-mingw32"] \ No newline at end of file + CompilationDatabase: bin \ No newline at end of file diff --git a/extra/testTheme.txt b/extra/testTheme.txt index 07512c8..ac7cc05 100644 --- a/extra/testTheme.txt +++ b/extra/testTheme.txt @@ -2,11 +2,13 @@ % ---- Variables ---- $accentColor = Color(rgba(255, 0, 255, 255)) -$color2 = Color(#0000FFFF) +const $color2 = Color(#0000FFFF) % ------------------- window.backgroundColor = Color(rgba(120, 120, 120, 255)) +$accentColor = Color(rgb(255, 0, 0)) + (label) { textColor = $accentColor backgroundColor = Color(#000000FF) diff --git a/other/build.nr b/other/build.nr index 366c7bb..6a36181 100644 --- a/other/build.nr +++ b/other/build.nr @@ -1 +1 @@ -2052 +2053 diff --git a/src/ostd/io/Stylesheet.cpp b/src/ostd/io/Stylesheet.cpp index f98fa77..61e8c75 100644 --- a/src/ostd/io/Stylesheet.cpp +++ b/src/ostd/io/Stylesheet.cpp @@ -41,54 +41,66 @@ namespace ostd return loadFromString(outContent, filePath, clearCurrentRules); } - Stylesheet& Stylesheet::loadFromString(const ostd::String& content, const ostd::String& filePath, bool clearCurrentRules) + Stylesheet& Stylesheet::loadFromString(const ostd::String& content, const ostd::String& filePath, bool clearCurrentRules, std::unordered_map> variables) { if (clearCurrentRules) m_values.clear(); std::vector lines = content.tokenize("\n", false, true).getRawData(); + std::vector originalLines = lines; uint32_t lineNumber = 0; - ostd::String lineCopy = ""; - std::unordered_map variables; auto l_warn = [&](const ostd::String& msg) -> void { - OX_WARN("%s in theme line. File: <%s:%d>:\n\t%s", msg.c_str(), filePath.c_str(), lineNumber, lineCopy.c_str()); + OX_WARN("%s in theme line. File: <%s:%d>", msg.c_str(), filePath.c_str(), lineNumber, originalLines[lineNumber - 1].c_str()); }; auto l_parseLine = [&](ostd::String& line) -> void { for (const auto&[var, val] : variables) - line.replaceAll(var, val); + line.replaceAll(var, val.first); if (!parseThemeFileLine(line)) l_warn("Error"); }; + uint8_t lineNumberMaxWidth = ostd::String("").add(lines.size()).len(); ostd::String groupSelector = ""; - bool groupLines = false; + bool groupLines = true; std::vector group; + bool debug_print = true; for (auto& line : lines) { lineNumber++; - lineCopy = line; line.trim(); if (line.startsWith("%")) - continue; + goto custom_continue; if (line.contains("%")) line.substr(0, line.indexOf("%")).trim(); if (line == "") - continue; - if (line.startsWith("$")) + goto custom_continue; + if (line.startsWith("const ") || line.startsWith("$")) { + bool is_const = false; + if (line.startsWith("const ")) + { + line.substr(6).trim(); + is_const = true; + } line.substr(1); if (line.count("=") != 1 || line.endsWith("=")) { l_warn("Invalid variable"); - continue; + goto custom_continue; } ostd::String varName = line.new_substr(0, line.indexOf("=")).trim(); ostd::String varValue = line.new_substr(line.indexOf("=") + 1).trim(); if (!varName.regexMatches(m_validNameRegex)) { l_warn("Invalid variable name"); - continue; + goto custom_continue; } - variables["$" + varName] = varValue; - continue; + auto var = variables.find("$" + varName); + if (var != variables.end() && var->second.second) + { + l_warn("Trying to re-assign a const variable"); + goto custom_continue; + } + variables["$" + varName] = { varValue, is_const }; + goto custom_continue; } if (groupSelector != "") { @@ -98,11 +110,16 @@ namespace ostd { groupLines = false; auto newLines = parseGroup(groupSelector, group); - for (auto& l : newLines) + lineNumber -= newLines.size(); + for (int32_t i = 0; i < newLines.size(); i++) + { + auto& l = newLines[i]; l_parseLine(l); + lineNumber++; + } groupSelector = ""; group.clear(); - continue; + goto custom_continue; } group.push_back(line); } @@ -111,30 +128,39 @@ namespace ostd if (line == "{") groupLines = true; } - continue; + goto custom_continue; } if (line.startsWith("(")) { if (line.count(")") != 1 || line.indexOf(")") < 3) { l_warn("Invalid group selector"); - continue; + goto custom_continue; } ostd::String rawSelector = line.new_substr(1, line.indexOf(")")).trim(); groupSelector = parseGroupSelector(rawSelector); + if (groupSelector == "") + { + l_warn("Invalid group selector"); + goto custom_continue; + } if (line.contains("{")) { line.substr(line.indexOf("{")).trim(); if (line != "{") { l_warn("Invalid group selector"); - continue; + goto custom_continue; } groupLines = true; } - continue; + goto custom_continue; } l_parseLine(line); + continue; +custom_continue: + if (debug_print) + std::cout << ostd::String("").add(lineNumber).addLeftPadding(lineNumberMaxWidth, ' ') << "| " << originalLines[lineNumber - 1] << "\n"; } return *this; } diff --git a/src/ostd/io/Stylesheet.hpp b/src/ostd/io/Stylesheet.hpp index c909339..55ec5b1 100644 --- a/src/ostd/io/Stylesheet.hpp +++ b/src/ostd/io/Stylesheet.hpp @@ -35,7 +35,7 @@ namespace ostd Stylesheet(void); Stylesheet& clear(void); Stylesheet& loadFromFile(const ostd::String& filePath, bool clearCurrentRules = true); - Stylesheet& loadFromString(const ostd::String& content, const ostd::String& filePath = "memory://", bool clearCurrentRules = true); + Stylesheet& loadFromString(const ostd::String& content, const ostd::String& filePath = "memory://", bool clearCurrentRules = true, std::unordered_map> variables = {}); void set(const std::string& key, TypeVariant value, const ostd::String& themeID); void removeRule(const ostd::String& fullKey); void setFull(const ostd::String& fullKey, TypeVariant value); diff --git a/src/ostd/string/String.cpp b/src/ostd/string/String.cpp index 7996be2..ac90b55 100644 --- a/src/ostd/string/String.cpp +++ b/src/ostd/string/String.cpp @@ -584,9 +584,9 @@ namespace ostd } else { - if (allow_white_space_only_tokens && __token != "") + if (allow_white_space_only_tokens && __token == "") tokens.m_tokens.push_back(__token.cpp_str()); - else if (!allow_white_space_only_tokens && __token.new_trim() != "") + else if (__token.new_trim() != "") tokens.m_tokens.push_back(__token.cpp_str()); } sindex = eindex + delimiter.len(); diff --git a/src/test/GuiTest.cpp b/src/test/GuiTest.cpp index 5ad4fcd..b49e0ea 100644 --- a/src/test/GuiTest.cpp +++ b/src/test/GuiTest.cpp @@ -72,6 +72,11 @@ class Window : public ogfx::gui::Window if (signal.ID == ostd::BuiltinSignals::FileDragAndDropped) { auto& data = (ogfx::DropEventData&)signal.userData; + if (ostd::FileSystem::fileExists(data.textOrFilePath)) + { + m_theme.loadFromFile(data.textOrFilePath); + m_rootWidget.reloadTheme(); + } std::cout << data.textOrFilePath << "\n"; } });