Added css style propagation + hover propagation from child

This commit is contained in:
antopilo
2024-09-15 11:08:21 -04:00
parent c5dca4dc37
commit 24a4281a3d
4 changed files with 52 additions and 34 deletions

View File

@@ -36,8 +36,8 @@ namespace NuakeUI
if (!mRootNode)
return;
mRootNode->UpdateInput(mInputManager);
mRootNode->Tick(mInputManager);
mRootNode->UpdateInput(mInputManager);
mInputManager->ScrollX = 0.f;
mInputManager->ScrollY = 0.f;
@@ -84,29 +84,26 @@ namespace NuakeUI
}
}
void Canvas::ComputeStyle(NodePtr node)
void Canvas::ComputeStyle(NodePtr node, const std::vector<StyleRule>& inheritedRules)
{
for (auto& s : node->GetDataModelOperations())
std::vector<StyleRule> relationStyles;
auto allRules = mStyleSheet->Rules;
for (auto& i : inheritedRules)
{
if (s->Type != OperationType::IfClass)
continue;
if (auto dataModel = node->GetDataModel(); s->Compare(dataModel))
{
node->AddClass(s->ClassName);
}
else
{
node->RemoveClass(s->ClassName);
}
allRules.push_back(i);
}
std::vector<StyleRule> relationStyles;
for (auto& rule : mStyleSheet->Rules)
if (node->Classes.size() == 1 && node->Classes[0] == "dropdown")
{
Logger::Log("Found dropdown");
}
for (auto& rule : allRules)
{
bool respectSelector = true;
bool containsRelation = false;
std::vector<StyleRule> relationalStyleInRule;
// TODO: Apply descendant selectors to child nodes.
for (StyleSelector& selector : rule.Selector)
{
@@ -150,39 +147,47 @@ namespace NuakeUI
}
}
if (selector.SelectorRelation != Relation::None)
if (respectSelector && selector.SelectorRelation != Relation::None)
{
containsRelation = true;
if (selector.SelectorRelation == Relation::Descendant)
{
containsRelation = true;
foundSelector = true;
auto ruleSelector = StyleSelector{ selector.Type, selector.Value, Relation::None };
StyleRule newStyleRule = StyleRule({ ruleSelector });
newStyleRule.Properties = rule.Properties;
relationStyles.push_back(std::move(newStyleRule));
relationalStyleInRule.push_back(std::move(newStyleRule));
}
}
if (!foundSelector)
else if(!foundSelector)
{
respectSelector = false;
}
if (respectSelector)
{
}
}
if (respectSelector && !containsRelation)
{
node->ApplyStyleProperties(rule.Properties);
}
if (relationalStyleInRule.size() > 0 && respectSelector)
{
for (auto& s : relationalStyleInRule)
{
relationStyles.push_back(s);
}
}
}
for (auto& c : node->GetChildrens())
{
if (relationStyles.size() > 0)
{
//c->ApplyStyleProperties(relationStyles.);
}
ComputeStyle(c);
ComputeStyle(c, relationStyles);
}
}

View File

@@ -51,7 +51,7 @@ namespace NuakeUI
void Tick();
void Draw();
void ComputeLayout(Vector2 size);
void ComputeStyle(NodePtr node);
void ComputeStyle(NodePtr node, const std::vector<StyleRule>& inheritedRules = {});
NodePtr GetRoot() const;
void SetRoot(NodePtr root);

View File

@@ -316,6 +316,11 @@ namespace NuakeUI
bool Node::IsMouseHover(float x, float y)
{
if (ComputedStyle.Visibility != VisibilityType::Show)
{
return false;
}
YGNodeRef ygNode = GetYogaNode();
float parentScroll = 0.f;
@@ -340,7 +345,15 @@ namespace NuakeUI
left += parentLeft;
top += parentTop;
bool isHover = x > left && x < left + width && y > top && y < top + height;
bool isHover = x >= left && x < left + width && y >= top && y < top + height;
for (auto& c : Childrens)
{
if (c->IsMouseHover(x, y))
{
return true;
}
}
return isHover;
}

View File

@@ -227,9 +227,9 @@ void StyleSheetParser::ParseStyleRule(KatanaRule* rule, StyleSheetPtr styleSheet
currentStyleSelector.SelectorRelation = Relation::Child;
break;
case KatanaSelectorRelationSubSelector:
currentStyleSelector.SelectorRelation = Relation::SubSelection;
break;
//case KatanaSelectorRelationSubSelector:
// currentStyleSelector.SelectorRelation = Relation::SubSelection;
// break;
default:
currentStyleSelector.SelectorRelation = Relation::None;