LibHTML: Add a Frame class, start fleshing out recursive layout.

Layout is initiated from Frame::layout(). It makes the document's layout
node as wide as the frame, and then we'll take it from there.
This commit is contained in:
Andreas Kling 2019-06-16 21:35:03 +02:00
parent f49e5c6732
commit 0db2f3cbe6
Notes: sideshowbarker 2024-07-19 13:34:23 +09:00
9 changed files with 91 additions and 3 deletions

31
LibHTML/Frame.cpp Normal file
View file

@ -0,0 +1,31 @@
#include <LibHTML/Frame.h>
#include <LibHTML/Layout/LayoutNode.h>
Frame::Frame()
: m_size(800, 600)
{
}
Frame::~Frame()
{
}
void Frame::set_document(Document* document)
{
m_document = document;
}
void Frame::layout()
{
if (!m_document)
return;
if (!m_document->layout_node())
m_document->create_layout_node();
ASSERT(m_document->layout_node());
m_document->layout_node()->style().size().set_width(m_size.width());
m_document->layout_node()->layout();
}

21
LibHTML/Frame.h Normal file
View file

@ -0,0 +1,21 @@
#pragma once
#include <LibHTML/DOM/Document.h>
#include <SharedGraphics/Size.h>
class Frame {
public:
Frame();
~Frame();
Document* document() { return m_document.ptr(); }
const Document* document() const { return m_document.ptr(); }
void set_document(Document*);
void layout();
private:
RetainPtr<Document> m_document;
Size m_size;
};

View file

@ -8,3 +8,9 @@ LayoutDocument::LayoutDocument(const Document& document)
LayoutDocument::~LayoutDocument()
{
}
void LayoutDocument::layout()
{
rect().set_width(style().size().width());
LayoutNode::layout();
}

View file

@ -3,14 +3,13 @@
#include <LibHTML/Layout/LayoutNode.h>
#include <LibHTML/DOM/Document.h>
class LayoutDocument : public LayoutNode {
class LayoutDocument final : public LayoutNode {
public:
explicit LayoutDocument(const Document&);
virtual ~LayoutDocument() override;
const Document& node() const { return static_cast<const Document&>(*LayoutNode::node()); }
virtual const char* class_name() const override { return "LayoutDocument"; }
virtual void layout() override;
private:
};

View file

@ -31,3 +31,10 @@ void LayoutNode::append_child(Retained<LayoutNode> node)
if (!m_first_child)
m_first_child = m_last_child;
}
void LayoutNode::layout()
{
for_each_child([](auto& child) {
child.layout();
});
}

View file

@ -16,6 +16,7 @@ public:
int retain_count() const { return m_retain_count; }
const Rect& rect() const { return m_rect; }
Rect& rect() { return m_rect; }
void set_rect(const Rect& rect) { m_rect = rect; }
LayoutStyle& style() { return m_style; }
@ -45,9 +46,18 @@ public:
callback(*node);
}
template<typename Callback>
inline void for_each_child(Callback callback)
{
for (auto* node = first_child(); node; node = node->next_sibling())
callback(*node);
}
virtual const char* class_name() const { return "LayoutNode"; }
virtual bool is_text() const { return false; }
virtual void layout();
protected:
explicit LayoutNode(const Node*);

View file

@ -1,6 +1,7 @@
#pragma once
#include <SharedGraphics/Color.h>
#include <SharedGraphics/Size.h>
struct Box {
int top { 0 };
@ -32,6 +33,9 @@ public:
FontStyle font_style() const { return m_font_style; }
const Size& size() const { return m_size; }
Size& size() { return m_size; }
private:
Color m_text_color;
Color m_background_color;
@ -40,5 +44,7 @@ private:
Box m_margin;
Box m_padding;
Size m_size;
FontStyle m_font_style { FontStyle::Normal };
};

View file

@ -13,6 +13,7 @@ LIBHTML_OBJS = \
Layout/LayoutInline.o \
Layout/LayoutDocument.o \
Layout/LayoutStyle.o \
Frame.o \
Dump.o
TEST_OBJS = test.o

View file

@ -1,5 +1,6 @@
#include <LibCore/CFile.h>
#include <LibHTML/Dump.h>
#include <LibHTML/Frame.h>
#include <LibHTML/Parser/Parser.h>
#include <stdio.h>
@ -16,6 +17,12 @@ int main(int argc, char** argv)
doc->build_layout_tree();
ASSERT(doc->layout_node());
dump_tree(*doc->layout_node());
auto frame = make<Frame>();
frame->set_document(doc);
frame->layout();
dump_tree(*doc->layout_node());
return 0;
}