2020-06-15 17:29:35 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-15 17:29:35 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/QuickSort.h>
|
2021-01-01 16:42:44 +01:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-06-15 17:29:35 +02:00
|
|
|
#include <LibWeb/DOM/Node.h>
|
2020-11-22 15:53:01 +01:00
|
|
|
#include <LibWeb/Layout/Box.h>
|
|
|
|
#include <LibWeb/Layout/InitialContainingBlockBox.h>
|
2020-06-18 22:01:05 +02:00
|
|
|
#include <LibWeb/Painting/StackingContext.h>
|
2020-06-15 17:29:35 +02:00
|
|
|
|
2020-11-22 15:53:01 +01:00
|
|
|
namespace Web::Layout {
|
2020-06-15 17:29:35 +02:00
|
|
|
|
2020-11-22 15:53:01 +01:00
|
|
|
StackingContext::StackingContext(Box& box, StackingContext* parent)
|
2020-06-15 17:29:35 +02:00
|
|
|
: m_box(box)
|
|
|
|
, m_parent(parent)
|
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(m_parent != this);
|
2020-06-15 17:29:35 +02:00
|
|
|
if (m_parent) {
|
|
|
|
m_parent->m_children.append(this);
|
|
|
|
|
|
|
|
// FIXME: Don't sort on every append..
|
2020-07-01 18:35:50 +02:00
|
|
|
quick_sort(m_parent->m_children, [](auto& a, auto& b) {
|
2021-01-06 11:07:02 +01:00
|
|
|
return a->m_box.computed_values().z_index().value_or(0) < b->m_box.computed_values().z_index().value_or(0);
|
2020-06-15 17:29:35 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-03 19:15:27 +01:00
|
|
|
void StackingContext::paint(PaintContext& context, PaintPhase phase)
|
2020-06-15 17:29:35 +02:00
|
|
|
{
|
2021-01-01 18:55:47 +01:00
|
|
|
if (!is<InitialContainingBlockBox>(m_box)) {
|
2020-06-18 21:35:44 +02:00
|
|
|
m_box.paint(context, phase);
|
2020-06-15 17:29:35 +02:00
|
|
|
} else {
|
2020-11-22 15:53:01 +01:00
|
|
|
// NOTE: InitialContainingBlockBox::paint() merely calls StackingContext::paint()
|
2020-06-15 17:29:35 +02:00
|
|
|
// so we call its base class instead.
|
2020-11-22 15:53:01 +01:00
|
|
|
downcast<InitialContainingBlockBox>(m_box).BlockBox::paint(context, phase);
|
2020-06-15 17:29:35 +02:00
|
|
|
}
|
|
|
|
for (auto* child : m_children) {
|
2020-06-18 21:35:44 +02:00
|
|
|
child->paint(context, phase);
|
2020-06-15 17:29:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-05 16:55:56 +02:00
|
|
|
HitTestResult StackingContext::hit_test(const Gfx::IntPoint& position, HitTestType type) const
|
2020-07-01 19:02:28 +02:00
|
|
|
{
|
|
|
|
HitTestResult result;
|
2021-01-01 18:55:47 +01:00
|
|
|
if (!is<InitialContainingBlockBox>(m_box)) {
|
2020-08-05 16:55:56 +02:00
|
|
|
result = m_box.hit_test(position, type);
|
2020-07-01 19:02:28 +02:00
|
|
|
} else {
|
2020-11-22 15:53:01 +01:00
|
|
|
// NOTE: InitialContainingBlockBox::hit_test() merely calls StackingContext::hit_test()
|
2020-07-01 19:02:28 +02:00
|
|
|
// so we call its base class instead.
|
2020-11-22 15:53:01 +01:00
|
|
|
result = downcast<InitialContainingBlockBox>(m_box).BlockBox::hit_test(position, type);
|
2020-07-01 19:02:28 +02:00
|
|
|
}
|
|
|
|
|
2021-03-31 10:26:11 -04:00
|
|
|
int z_index = m_box.computed_values().z_index().value_or(0);
|
|
|
|
|
2020-07-01 19:02:28 +02:00
|
|
|
for (auto* child : m_children) {
|
2021-03-31 10:26:11 -04:00
|
|
|
int child_z_index = child->m_box.computed_values().z_index().value_or(0);
|
|
|
|
if (result.layout_node && (child_z_index < z_index))
|
|
|
|
continue;
|
|
|
|
|
2020-08-05 16:55:56 +02:00
|
|
|
auto result_here = child->hit_test(position, type);
|
2020-07-01 19:02:28 +02:00
|
|
|
if (result_here.layout_node)
|
|
|
|
result = result_here;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-06-15 17:29:35 +02:00
|
|
|
void StackingContext::dump(int indent) const
|
|
|
|
{
|
2021-01-01 16:42:44 +01:00
|
|
|
StringBuilder builder;
|
2020-06-15 17:29:35 +02:00
|
|
|
for (int i = 0; i < indent; ++i)
|
2021-01-01 16:42:44 +01:00
|
|
|
builder.append(' ');
|
|
|
|
builder.appendff("SC for {}({}) {} [children: {}]", m_box.class_name(), m_box.dom_node() ? m_box.dom_node()->node_name().characters() : "(anonymous)", m_box.absolute_rect().to_string().characters(), m_children.size());
|
|
|
|
dbgln("{}", builder.string_view());
|
2020-06-15 17:29:35 +02:00
|
|
|
for (auto& child : m_children)
|
|
|
|
child->dump(indent + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|