Added const variables to stylesheet

This commit is contained in:
OmniaX-Dev 2026-04-07 14:00:37 +02:00
parent da0d6e0c19
commit c28829bd72
7 changed files with 59 additions and 27 deletions

View file

@ -1,3 +1,2 @@
CompileFlags:
CompilationDatabase: bin
Add: ["--target=x86_64-w64-mingw32"]
CompilationDatabase: bin

View file

@ -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)

View file

@ -1 +1 @@
2052
2053

View file

@ -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<ostd::String, std::pair<ostd::String, bool>> variables)
{
if (clearCurrentRules)
m_values.clear();
std::vector<ostd::String> lines = content.tokenize("\n", false, true).getRawData();
std::vector<ostd::String> originalLines = lines;
uint32_t lineNumber = 0;
ostd::String lineCopy = "";
std::unordered_map<ostd::String, ostd::String> 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<ostd::String> 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;
}

View file

@ -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<ostd::String, std::pair<ostd::String, bool>> 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);

View file

@ -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();

View file

@ -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";
}
});