From efc4b8bae78cc4f65ee50b6b4a4129794249d8d6 Mon Sep 17 00:00:00 2001 From: antopilo Date: Fri, 13 Sep 2024 21:45:56 -0400 Subject: [PATCH] Added stylesheet @import into live reload filewatch for a canvas --- Nuake/src/Scene/Systems/UISystem.cpp | 5 +++++ Nuake/src/UI/Parsers/CanvasParser.cpp | 2 +- Nuake/src/UI/Parsers/StyleSheetParser.cpp | 10 ++++++++-- Nuake/src/UI/Parsers/StyleSheetParser.h | 5 ++++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Nuake/src/Scene/Systems/UISystem.cpp b/Nuake/src/Scene/Systems/UISystem.cpp index a36af23b..537f2505 100644 --- a/Nuake/src/Scene/Systems/UISystem.cpp +++ b/Nuake/src/Scene/Systems/UISystem.cpp @@ -50,6 +50,11 @@ namespace Nuake bool sourceHasChanged = false; for (auto& fileAssociated : ui->GetCanvas()->GetSourceFiles()) { + if (!fileAssociated) + { + continue; + } + // Re-fetching the file object because the Scan might have invalided the pointer. if (FileSystem::GetFile(fileAssociated->GetRelativePath())->GetHasBeenModified()) { diff --git a/Nuake/src/UI/Parsers/CanvasParser.cpp b/Nuake/src/UI/Parsers/CanvasParser.cpp index ad2318e9..149fb15c 100644 --- a/Nuake/src/UI/Parsers/CanvasParser.cpp +++ b/Nuake/src/UI/Parsers/CanvasParser.cpp @@ -359,7 +359,7 @@ Ref CanvasParser::Parse(CanvasPtr canvas, const std::string& path) std::string relativePath = styleSheet->Value(); if (FileSystem::FileExists(relativePath)) { - auto styleSheet = StyleSheetParser::Get().Parse(relativePath); + auto styleSheet = StyleSheetParser::Get().Parse(canvas, relativePath); currentParsingCanvas->AddSourceFile(FileSystem::GetFile(relativePath)); canvas->SetStyleSheet(styleSheet); } diff --git a/Nuake/src/UI/Parsers/StyleSheetParser.cpp b/Nuake/src/UI/Parsers/StyleSheetParser.cpp index 940bcb68..ecf880f8 100644 --- a/Nuake/src/UI/Parsers/StyleSheetParser.cpp +++ b/Nuake/src/UI/Parsers/StyleSheetParser.cpp @@ -1,6 +1,7 @@ #include "StyleSheetParser.h" #include "src/FileSystem/FileSystem.h" +#include #include #include @@ -9,10 +10,11 @@ using namespace NuakeUI; -std::shared_ptr StyleSheetParser::Parse(const std::string& path) +std::shared_ptr StyleSheetParser::Parse(Ref canvas, const std::string& path) { assert(FileSystem::FileExists(path)); + currentCanvas = canvas; _parsingPath = path; std::string fileContent = FileSystem::ReadFile(path); @@ -31,6 +33,7 @@ std::shared_ptr StyleSheetParser::Parse(const std::string& path) std::cout << "ERROR at line " + std::to_string(error->first_line) + " : " + std::to_string(error->first_column) << std::endl; } + return styleSheet; } else @@ -95,6 +98,9 @@ void StyleSheetParser::ParseImportRule(KatanaImportRule* rule, StyleSheetPtr sty std::string fileContent = FileSystem::ReadFile(path); auto data = katana_parse(fileContent.c_str(), fileContent.length(), KatanaParserModeStylesheet); + // Add to source files anyway since the user might fix his errors, we want to reload. + currentCanvas->AddSourceFile(FileSystem::GetFile(path)); + if (data->errors.length > 0) { KatanaArray errors = data->errors; @@ -106,7 +112,7 @@ void StyleSheetParser::ParseImportRule(KatanaImportRule* rule, StyleSheetPtr sty std::cout << "ERROR at line " + std::to_string(error->first_line) + " : " + std::to_string(error->first_column) << std::endl; } - + return; } diff --git a/Nuake/src/UI/Parsers/StyleSheetParser.h b/Nuake/src/UI/Parsers/StyleSheetParser.h index d389f3fc..38d0bd00 100644 --- a/Nuake/src/UI/Parsers/StyleSheetParser.h +++ b/Nuake/src/UI/Parsers/StyleSheetParser.h @@ -6,6 +6,8 @@ #include "../Vendors/katana-parser/katana.h" +#include "src/UI/Nodes/Canvas.h" + namespace NuakeUI { class StyleSheetParser @@ -20,9 +22,10 @@ namespace NuakeUI StyleSheetParser() = default; ~StyleSheetParser() = default; - std::shared_ptr Parse(const std::string& path); + std::shared_ptr Parse(Ref canvas, const std::string& path); private: + Ref currentCanvas; std::string _parsingPath; std::vector _visitedFiles;