Explorar o código

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.
Andreas Kling %!s(int64=6) %!d(string=hai) anos
pai
achega
0db2f3cbe6

+ 31 - 0
LibHTML/Frame.cpp

@@ -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 - 0
LibHTML/Frame.h

@@ -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;
+};

+ 6 - 0
LibHTML/Layout/LayoutDocument.cpp

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

+ 2 - 3
LibHTML/Layout/LayoutDocument.h

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

+ 7 - 0
LibHTML/Layout/LayoutNode.cpp

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

+ 10 - 0
LibHTML/Layout/LayoutNode.h

@@ -16,6 +16,7 @@ public:
     int retain_count() const { return m_retain_count; }
     int retain_count() const { return m_retain_count; }
 
 
     const Rect& rect() const { return m_rect; }
     const Rect& rect() const { return m_rect; }
+    Rect& rect() { return m_rect; }
     void set_rect(const Rect& rect) { m_rect = rect; }
     void set_rect(const Rect& rect) { m_rect = rect; }
 
 
     LayoutStyle& style() { return m_style; }
     LayoutStyle& style() { return m_style; }
@@ -45,9 +46,18 @@ public:
             callback(*node);
             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 const char* class_name() const { return "LayoutNode"; }
     virtual bool is_text() const { return false; }
     virtual bool is_text() const { return false; }
 
 
+    virtual void layout();
+
 protected:
 protected:
     explicit LayoutNode(const Node*);
     explicit LayoutNode(const Node*);
 
 

+ 6 - 0
LibHTML/Layout/LayoutStyle.h

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

+ 1 - 0
LibHTML/Makefile

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

+ 7 - 0
LibHTML/test.cpp

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