Skip to content

Commit

Permalink
add elements to identifier index
Browse files Browse the repository at this point in the history
  • Loading branch information
mlomb committed Apr 10, 2021
1 parent 74b1261 commit dff1fd0
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 20 deletions.
3 changes: 2 additions & 1 deletion include/flexui/Misc/String.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ namespace flexui {

operator const String&() const { return _value; }

const bool empty() const { return _hash == HashStr(""); }
const String& str() const { return _value; }
const uint32_t hash() const { return _hash; }

bool operator ==(const HashedString& oth) const { return _hash == oth._hash; }

private:
Expand Down
4 changes: 2 additions & 2 deletions include/flexui/Nodes/Element.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ namespace flexui {
Element(const std::string& tag);
virtual ~Element();

void setID(const std::string& id) { m_ID = id; };
void addClass(const std::string& klass);
void setID(const HashedString& id);
void addClass(const HashedString& klass);
void setPseudoStates(const PseudoStates states);
void removePseudoStates(const PseudoStates states);

Expand Down
6 changes: 5 additions & 1 deletion include/flexui/Style/StyleEngine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@ namespace flexui {
void _attachedToTree(Node* node) override;
void _detachedFromTree(Node* node) override;

void _addElementID(const HashedString& id, Element* element);
void _addElementClass(const HashedString& klass, Element* element);
void _removeElementID(const HashedString& id, Element* element);
void _removeElementClass(const HashedString& klass, Element* element);

private:
Document* m_Document;

void calcStylesRecursive(ContainerNode* parent);
void calcStyles(Element* element);

std::vector<std::shared_ptr<StyleSheet>> m_StyleSheets;
//std::vector<SelectorMatch> m_MatchedSelectors;

IdentifierIndex<std::shared_ptr<StyleRule>> m_RulesIndex;
IdentifierIndex<Element*> m_ElementsIndex;
Expand Down
19 changes: 17 additions & 2 deletions src/flexui/Nodes/ContainerNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,23 @@ namespace flexui {

void ContainerNode::removeChild(Node* child_node)
{
// TODO: !
FUI_ASSERT(false);
FUI_ASSERT(child_node->m_Parent == this);

child_node->m_Parent = nullptr;

// connect pointers
if (child_node->m_PreviousSibling) {
child_node->m_PreviousSibling->m_NextSibling = child_node->m_NextSibling;
}
else {
m_FirstChild = child_node->m_NextSibling;
}
if (child_node->m_NextSibling) {
child_node->m_NextSibling->m_PreviousSibling = child_node->m_PreviousSibling;
}
else {
m_LastChild = child_node->m_PreviousSibling;
}

DeatchFromTree(child_node);
}
Expand Down
27 changes: 23 additions & 4 deletions src/flexui/Nodes/Element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "flexui/Style/StyleComputed.hpp"
#include "flexui/Events/Events.hpp"
#include "flexui/Layout/ElementLayoutObject.hpp"
#include "flexui/Nodes/Document.hpp"

namespace flexui {

Expand Down Expand Up @@ -72,16 +73,34 @@ namespace flexui {
}
}

void Element::addClass(const std::string& klass)
void Element::setID(const HashedString& id)
{
HashedString _klass;
if (m_Document && !m_ID.empty()) {
// remove the ID from the index
m_Document->getStyleEngine()._removeElementID(m_ID, this);
}
m_ID = id;
if (m_Document && !m_ID.empty()) {
// add the ID to the index
m_Document->getStyleEngine()._addElementID(m_ID, this);
}
}

void Element::addClass(const HashedString& klass)
{
if (klass.empty())
return;

for (auto it = m_Classes.begin(); it != m_Classes.end(); it++) {
if ((*it) == _klass)
if ((*it) == klass)
return; // already present
}

m_Classes.emplace_back(_klass);
m_Classes.emplace_back(klass);
if (m_Document) {
// add the ID to the index
m_Document->getStyleEngine()._addElementClass(klass, this);
}
}

void Element::setPseudoStates(const PseudoStates states)
Expand Down
68 changes: 58 additions & 10 deletions src/flexui/Style/StyleEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ namespace flexui {
{
auto it = std::find(m_StyleSheets.begin(), m_StyleSheets.end(), stylesheet);
if (it != m_StyleSheets.end()) {
// TODO: remove rules from index
m_StyleSheets.erase(it);
// TODO: remove rules from index
}
}

Expand All @@ -54,24 +54,52 @@ namespace flexui {

void StyleEngine::_attachedToTree(Node* node)
{
if (node->getNodeType() == NodeType::STYLE) {
switch (node->getNodeType()) {
case NodeType::STYLE:
{
Style* s = static_cast<Style*>(node);
addStyleSheet(s->getStyleSheet());
} else if (node->getNodeType() == NodeType::ELEMENT) {
std::shared_ptr<StyleSheet> ss = s->getStyleSheet();
if (ss)
addStyleSheet(ss);
break;
}
case NodeType::ELEMENT:
{
Element* e = static_cast<Element*>(node);
// add element to index
m_ElementsIndex.addIdentifier(SelectorIdentifierType::ID, e->getID().hash(), e);
// add existing identifiers to index
_addElementID(e->getID(), e);
m_ElementsIndex.addIdentifier(SelectorIdentifierType::TAG, e->getTag().hash(), e);
for (auto klass : e->getClasses()) {
m_ElementsIndex.addIdentifier(SelectorIdentifierType::CLASS, klass.hash(), e);
for (auto& klass : e->getClasses()) {
_addElementClass(klass, e);
}
break;
}
}
printf("NODE: %s\n", node->getDebugInfo().c_str());
}

void StyleEngine::_detachedFromTree(Node* node)
{

switch (node->getNodeType()) {
case NodeType::STYLE:
{
Style* s = static_cast<Style*>(node);
std::shared_ptr<StyleSheet> ss = s->getStyleSheet();
if (ss)
removeStyleSheet(ss);
break;
}
case NodeType::ELEMENT:
{
Element* e = static_cast<Element*>(node);
// remove identifiers from index
_removeElementID(e->getID(), e);
m_ElementsIndex.removeIdentifier(SelectorIdentifierType::TAG, e->getTag().hash(), e);
for (auto& klass : e->getClasses()) {
_removeElementClass(klass, e);
}
break;
}
}
}

void StyleEngine::calcStylesRecursive(ContainerNode* node)
Expand Down Expand Up @@ -124,4 +152,24 @@ namespace flexui {
Element* underCursor = m_Document->getEventsController().getElementOverMouse();
return underCursor ? underCursor->m_ComputedStyle->cursor.value : Cursor::AUTO;
}

void StyleEngine::_addElementID(const HashedString& id, Element* element)
{
m_ElementsIndex.addIdentifier(SelectorIdentifierType::ID, id.hash(), element);
}

void StyleEngine::_addElementClass(const HashedString& klass, Element* element)
{
m_ElementsIndex.addIdentifier(SelectorIdentifierType::CLASS, klass.hash(), element);
}

void StyleEngine::_removeElementID(const HashedString& id, Element* element)
{
m_ElementsIndex.removeIdentifier(SelectorIdentifierType::ID, id.hash(), element);
}

void StyleEngine::_removeElementClass(const HashedString& klass, Element* element)
{
m_ElementsIndex.removeIdentifier(SelectorIdentifierType::CLASS, klass.hash(), element);
}
}

0 comments on commit dff1fd0

Please sign in to comment.