Skip to content

Commit

Permalink
Templatify addWithLayout
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelOtter committed Oct 6, 2024
1 parent 4bdf828 commit df29c9b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 86 deletions.
82 changes: 67 additions & 15 deletions src/ui/include/growl/ui/pack.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,32 +51,84 @@ struct PackInfo {
float computedHeight = 0;
};

template <class T>
class Packer {
public:
Packer(Node* node, PackInfo* pack)
Packer(T* node, PackInfo* pack)
: node{node}
, pack{pack} {}

Node* getNode() {
T* getNode() {
return node;
}

Packer& expand();
Packer& fill();
Packer& width(Value width);
Packer& height(Value height);
Packer& align(Align align);
Packer<T>& expand() {
pack->expand = true;
return *this;
}

Packer<T>& fill() {
pack->fill = true;
return *this;
}

Packer<T>& width(Value width) {
pack->prefWidth = width;
return *this;
}

Packer<T>& height(Value height) {
pack->prefHeight = height;
return *this;
}

Packer<T>& align(Align align) {
pack->alignment = align;
return *this;
}

Packer<T>& margin(Value margin) {
pack->marginTop = margin;
pack->marginBottom = margin;
pack->marginLeft = margin;
pack->marginRight = margin;
return *this;
}

Packer& margin(Value margin);
Packer& marginTop(Value margin);
Packer& marginBottom(Value margin);
Packer& marginLeft(Value margin);
Packer& marginRight(Value margin);
Packer& marginX(Value margin);
Packer& marginY(Value margin);
Packer<T>& marginTop(Value margin) {
pack->marginTop = margin;
return *this;
}

Packer<T>& marginBottom(Value margin) {
pack->marginBottom = margin;
return *this;
}

Packer<T>& marginLeft(Value margin) {
pack->marginLeft = margin;
return *this;
}

Packer<T>& marginRight(Value margin) {
pack->marginRight = margin;
return *this;
}

Packer<T>& marginX(Value margin) {
pack->marginLeft = margin;
pack->marginRight = margin;
return *this;
}

Packer<T>& marginY(Value margin) {
pack->marginTop = margin;
pack->marginBottom = margin;
return *this;
}

private:
Node* node;
T* node;
PackInfo* pack;
};

Expand Down
8 changes: 7 additions & 1 deletion src/ui/include/growl/ui/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ class Widget : public Node {
Widget(std::string&& name);

virtual Node* addChild(std::unique_ptr<Node> node) override;
Packer addWithLayout(std::unique_ptr<Node> child);

template <class T, class... Args>
Packer<T> addWithLayout(Args&&... args) {
Node* n = addChild(std::make_unique<T>(std::forward<Args>(args)...));
return Packer{
static_cast<T*>(n), &(pack_info.at(pack_info.size() - 1))};
}

void invalidate();
void invalidateHierarchy();
Expand Down
65 changes: 0 additions & 65 deletions src/ui/src/pack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,68 +16,3 @@ float Value::evaluate(Node* context) {
}
return 0;
}

Packer& Packer::expand() {
pack->expand = true;
return *this;
}

Packer& Packer::fill() {
pack->fill = true;
return *this;
}

Packer& Packer::width(Value width) {
pack->prefWidth = width;
return *this;
}

Packer& Packer::height(Value height) {
pack->prefHeight = height;
return *this;
}

Packer& Packer::align(Align align) {
pack->alignment = align;
return *this;
}

Packer& Packer::margin(Value margin) {
pack->marginTop = margin;
pack->marginBottom = margin;
pack->marginLeft = margin;
pack->marginRight = margin;
return *this;
}

Packer& Packer::marginTop(Value margin) {
pack->marginTop = margin;
return *this;
}

Packer& Packer::marginBottom(Value margin) {
pack->marginBottom = margin;
return *this;
}

Packer& Packer::marginLeft(Value margin) {
pack->marginLeft = margin;
return *this;
}

Packer& Packer::marginRight(Value margin) {
pack->marginRight = margin;
return *this;
}

Packer& Packer::marginX(Value margin) {
pack->marginLeft = margin;
pack->marginRight = margin;
return *this;
}

Packer& Packer::marginY(Value margin) {
pack->marginTop = margin;
pack->marginBottom = margin;
return *this;
}
5 changes: 0 additions & 5 deletions src/ui/src/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ Node* Widget::addChild(std::unique_ptr<Node> node) {
return Node::addChild(std::move(node));
}

Packer Widget::addWithLayout(std::unique_ptr<Node> child) {
Node* n = addChild(std::move(child));
return Packer{n, &(pack_info.at(pack_info.size() - 1))};
}

void Widget::invalidate() {
invalidated = true;
}
Expand Down

0 comments on commit df29c9b

Please sign in to comment.