1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-30 23:28:12 +00:00

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.
This commit is contained in:
Andreas Kling 2020-06-24 14:17:05 +02:00
parent 5e83a97fa2
commit 6f28f08096
5 changed files with 76 additions and 4 deletions

View file

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

View file

@ -216,8 +216,16 @@ LayoutNodeWithStyle::LayoutNodeWithStyle(const Node* node, NonnullRefPtr<StylePr
, m_specified_style(move(style)) , m_specified_style(move(style))
{ {
m_has_style = true; m_has_style = true;
m_position = m_specified_style->position(); apply_style(this->specified_style());
m_text_align = m_specified_style->text_align(); }
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());
} }
} }

View file

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

View file

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

View file

@ -42,7 +42,7 @@ StackingContext::StackingContext(LayoutBox& box, StackingContext* parent)
// FIXME: Don't sort on every append.. // FIXME: Don't sort on every append..
quick_sort(m_children, [](auto& a, auto& b) { 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);
}); });
} }
} }