浏览代码

LibWeb: Add LayoutStyle, a place to store style info for layout & paint

StyleProperties is really only the specified "input" to what eventually
becomes the used/computed style we use for layout and painting.

Unlike StyleProperties, LayoutStyle will have strongly typed values for
everything it contains (i.e no CSS::ValueID or strings, etc.)

This first patch moves z-index into LayoutStyle.
Andreas Kling 5 年之前
父节点
当前提交
6f28f08096

+ 1 - 1
Libraries/LibWeb/Layout/LayoutBox.cpp

@@ -324,7 +324,7 @@ bool LayoutBox::establishes_stacking_context() const
     if (node() == document().root())
         return true;
     auto position = this->position();
-    auto z_index = specified_style().z_index();
+    auto z_index = style().z_index();
     if (position == CSS::Position::Absolute || position == CSS::Position::Relative) {
         if (z_index.has_value())
             return true;

+ 10 - 2
Libraries/LibWeb/Layout/LayoutNode.cpp

@@ -216,8 +216,16 @@ LayoutNodeWithStyle::LayoutNodeWithStyle(const Node* node, NonnullRefPtr<StylePr
     , m_specified_style(move(style))
 {
     m_has_style = true;
-    m_position = m_specified_style->position();
-    m_text_align = m_specified_style->text_align();
+    apply_style(this->specified_style());
+}
+
+void LayoutNodeWithStyle::apply_style(const StyleProperties& specified_style)
+{
+    auto& style = static_cast<MutableLayoutStyle&>(m_style);
+
+    m_position = specified_style.position();
+    m_text_align = specified_style.text_align();
+    style.set_z_index(specified_style.z_index());
 }
 
 }

+ 15 - 0
Libraries/LibWeb/Layout/LayoutNode.h

@@ -34,6 +34,7 @@
 #include <LibWeb/Forward.h>
 #include <LibWeb/Layout/BoxModelMetrics.h>
 #include <LibWeb/Layout/LayoutPosition.h>
+#include <LibWeb/Layout/LayoutStyle.h>
 #include <LibWeb/Painting/PaintContext.h>
 #include <LibWeb/TreeNode.h>
 
@@ -190,6 +191,7 @@ public:
     virtual LayoutNode& inline_wrapper() { return *this; }
 
     const StyleProperties& specified_style() const;
+    const ImmutableLayoutStyle& style() const;
     CSS::Position position() const;
     CSS::TextAlign text_align() const;
 
@@ -259,6 +261,8 @@ public:
     const StyleProperties& specified_style() const { return m_specified_style; }
     void set_specified_style(const StyleProperties& style) { m_specified_style = style; }
 
+    const ImmutableLayoutStyle& style() const { return static_cast<const ImmutableLayoutStyle&>(m_style); }
+
     CSS::Position position() const { return m_position; }
     CSS::TextAlign text_align() const { return m_text_align; }
 
@@ -266,6 +270,10 @@ protected:
     explicit LayoutNodeWithStyle(const Node*, NonnullRefPtr<StyleProperties>);
 
 private:
+    void apply_style(const StyleProperties&);
+
+    LayoutStyle m_style;
+
     NonnullRefPtr<StyleProperties> m_specified_style;
     CSS::Position m_position;
     CSS::TextAlign m_text_align;
@@ -293,6 +301,13 @@ inline const StyleProperties& LayoutNode::specified_style() const
     return parent()->specified_style();
 }
 
+inline const ImmutableLayoutStyle& LayoutNode::style() const
+{
+    if (m_has_style)
+        return static_cast<const LayoutNodeWithStyle*>(this)->style();
+    return parent()->style();
+}
+
 inline CSS::Position LayoutNode::position() const
 {
     if (m_has_style)

+ 49 - 0
Libraries/LibWeb/Layout/LayoutStyle.h

@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ *    list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <AK/Optional.h>
+
+namespace Web {
+
+class LayoutStyle {
+public:
+    Optional<int> z_index() const { return m_z_index; }
+
+protected:
+    Optional<int> m_z_index;
+};
+
+class ImmutableLayoutStyle final : public LayoutStyle {
+};
+
+class MutableLayoutStyle final : public LayoutStyle {
+public:
+    void set_z_index(Optional<int> value) { m_z_index = value; }
+};
+
+}

+ 1 - 1
Libraries/LibWeb/Painting/StackingContext.cpp

@@ -42,7 +42,7 @@ StackingContext::StackingContext(LayoutBox& box, StackingContext* parent)
 
         // FIXME: Don't sort on every append..
         quick_sort(m_children, [](auto& a, auto& b) {
-            return a->m_box.specified_style().z_index().value_or(0) < b->m_box.specified_style().z_index().value_or(0);
+            return a->m_box.style().z_index().value_or(0) < b->m_box.style().z_index().value_or(0);
         });
     }
 }