2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-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.
|
|
|
|
*/
|
|
|
|
|
2019-10-15 17:48:38 +03:00
|
|
|
#pragma once
|
|
|
|
|
2020-06-15 18:29:35 +03:00
|
|
|
#include <AK/OwnPtr.h>
|
2020-07-26 07:31:47 +03:00
|
|
|
#include <LibGfx/Rect.h>
|
2020-11-22 15:38:18 +03:00
|
|
|
#include <LibWeb/Layout/LineBox.h>
|
2020-11-22 17:53:01 +03:00
|
|
|
#include <LibWeb/Layout/Node.h>
|
2020-06-18 22:39:27 +03:00
|
|
|
#include <LibWeb/Painting/StackingContext.h>
|
2019-10-15 17:48:38 +03:00
|
|
|
|
2020-11-22 17:53:01 +03:00
|
|
|
namespace Web::Layout {
|
2020-03-07 12:27:02 +03:00
|
|
|
|
2020-11-22 17:53:01 +03:00
|
|
|
class Box : public NodeWithStyleAndBoxModelMetrics {
|
2019-10-15 17:48:38 +03:00
|
|
|
public:
|
2020-06-10 11:42:29 +03:00
|
|
|
const Gfx::FloatRect absolute_rect() const;
|
|
|
|
|
|
|
|
Gfx::FloatPoint effective_offset() const;
|
|
|
|
|
|
|
|
void set_offset(const Gfx::FloatPoint& offset);
|
|
|
|
void set_offset(float x, float y) { set_offset({ x, y }); }
|
|
|
|
|
|
|
|
const Gfx::FloatSize& size() const { return m_size; }
|
|
|
|
void set_size(const Gfx::FloatSize&);
|
|
|
|
void set_size(float width, float height) { set_size({ width, height }); }
|
|
|
|
|
|
|
|
void set_width(float width) { set_size(width, height()); }
|
|
|
|
void set_height(float height) { set_size(width(), height); }
|
|
|
|
float width() const { return m_size.width(); }
|
|
|
|
float height() const { return m_size.height(); }
|
|
|
|
|
|
|
|
float absolute_x() const { return absolute_rect().x(); }
|
|
|
|
float absolute_y() const { return absolute_rect().y(); }
|
|
|
|
Gfx::FloatPoint absolute_position() const { return absolute_rect().location(); }
|
|
|
|
|
2020-08-05 17:55:56 +03:00
|
|
|
virtual HitTestResult hit_test(const Gfx::IntPoint&, HitTestType) const override;
|
2019-10-15 17:48:38 +03:00
|
|
|
virtual void set_needs_display() override;
|
|
|
|
|
2019-10-19 12:49:46 +03:00
|
|
|
bool is_body() const;
|
|
|
|
|
2020-06-10 11:42:29 +03:00
|
|
|
void set_containing_line_box_fragment(LineBoxFragment&);
|
|
|
|
|
2020-06-15 18:29:35 +03:00
|
|
|
bool establishes_stacking_context() const;
|
|
|
|
StackingContext* stacking_context() { return m_stacking_context; }
|
2020-07-01 20:02:28 +03:00
|
|
|
const StackingContext* stacking_context() const { return m_stacking_context; }
|
2020-06-15 18:29:35 +03:00
|
|
|
void set_stacking_context(NonnullOwnPtr<StackingContext> context) { m_stacking_context = move(context); }
|
|
|
|
StackingContext* enclosing_stacking_context();
|
|
|
|
|
2020-06-18 22:35:44 +03:00
|
|
|
virtual void paint(PaintContext&, PaintPhase) override;
|
2020-06-15 18:29:35 +03:00
|
|
|
|
2020-11-22 15:38:18 +03:00
|
|
|
Vector<LineBox>& line_boxes() { return m_line_boxes; }
|
|
|
|
const Vector<LineBox>& line_boxes() const { return m_line_boxes; }
|
|
|
|
|
|
|
|
LineBox& ensure_last_line_box();
|
|
|
|
LineBox& add_line_box();
|
|
|
|
|
|
|
|
virtual float width_of_logical_containing_block() const;
|
|
|
|
|
2019-10-15 17:48:38 +03:00
|
|
|
protected:
|
2020-11-22 17:53:01 +03:00
|
|
|
Box(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
|
|
|
|
: NodeWithStyleAndBoxModelMetrics(document, node, move(style))
|
2019-10-15 17:48:38 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-06-10 11:42:29 +03:00
|
|
|
virtual void did_set_rect() { }
|
2020-06-05 20:13:27 +03:00
|
|
|
|
2020-11-22 15:38:18 +03:00
|
|
|
Vector<LineBox> m_line_boxes;
|
|
|
|
|
2019-10-15 17:48:38 +03:00
|
|
|
private:
|
LibWeb: Reorganize layout algorithm
Previously, layout recursively performed these steps (roughly):
1. Compute own width
2. Compute own position
3. Layout in-flow children
4. Compute own height
5. Layout absolutely positioned descendants
However, step (2) was pretty inconsistent. Some things computed their
own position, others had their parent do it for them, etc.
To get closer to CSS spec language, and make things easier in general,
this patch reorganizes the algorithm into:
1. Compute own width & height
2. Compute width & height of in-flow managed descendants
3. Move in-flow managed descendants to their final position
4. Layout absolutely positioned descendants
Block layout is now driven by the containing block, which will iterate
the descendants it's responsible for. There are a lot of inefficient
patterns in this logic right now, but they can easily be replaced with
better iteration functions once we settle on a long-term architecture.
Since the ICB (LayoutDocument) is at (0, 0), it doesn't rely on a
containing block to move it into place.
This code is still evolving along with my understanding of CSS layout,
so it's likely that we'll reorganize this again sooner or later. :^)
2020-06-14 19:48:10 +03:00
|
|
|
virtual bool is_box() const final { return true; }
|
2019-10-15 17:48:38 +03:00
|
|
|
|
2020-06-10 11:42:29 +03:00
|
|
|
Gfx::FloatPoint m_offset;
|
|
|
|
Gfx::FloatSize m_size;
|
|
|
|
|
|
|
|
// Some boxes hang off of line box fragments. (inline-block, inline-table, replaced, etc)
|
|
|
|
WeakPtr<LineBoxFragment> m_containing_line_box_fragment;
|
2020-06-15 18:29:35 +03:00
|
|
|
|
|
|
|
OwnPtr<StackingContext> m_stacking_context;
|
2019-10-15 17:48:38 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2020-03-07 12:27:02 +03:00
|
|
|
|
2020-11-22 17:53:01 +03:00
|
|
|
AK_BEGIN_TYPE_TRAITS(Web::Layout::Box)
|
|
|
|
static bool is_type(const Web::Layout::Node& layout_node) { return layout_node.is_box(); }
|
2020-07-26 18:16:18 +03:00
|
|
|
AK_END_TYPE_TRAITS()
|