blob: 88b5cfd535bac64d6fc34fe8ae0af2e6179b40e3 [file] [log] [blame]
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef NGConstraintSpace_h
#define NGConstraintSpace_h
#include "core/CoreExport.h"
#include "core/layout/LayoutBox.h"
#include "core/layout/ng/ng_physical_constraint_space.h"
#include "core/layout/ng/ng_writing_mode.h"
#include "platform/heap/Handle.h"
#include "wtf/text/WTFString.h"
#include "wtf/Vector.h"
namespace blink {
class LayoutBox;
class NGDerivedConstraintSpace;
class NGFragment;
class NGLayoutOpportunityIterator;
// The NGConstraintSpace represents a set of constraints and available space
// which a layout algorithm may produce a NGFragment within. It is a view on
// top of a NGPhysicalConstraintSpace and provides accessor methods in the
// logical coordinate system defined by the writing mode given.
class CORE_EXPORT NGConstraintSpace final
: public GarbageCollected<NGConstraintSpace> {
public:
// Constructs a constraint space with a new backing NGPhysicalConstraintSpace.
NGConstraintSpace(NGWritingMode, NGLogicalSize);
// Constructs a constraint space based on an existing backing
// NGPhysicalConstraintSpace.
NGConstraintSpace(NGWritingMode, NGPhysicalConstraintSpace*);
// Constructs a constraint space with a different NGWritingMode.
NGConstraintSpace(NGWritingMode, const NGConstraintSpace*);
// Constructs a derived constraint space sharing the same backing
// NGPhysicalConstraintSpace and NGWritingMode.
NGConstraintSpace(const NGConstraintSpace& other,
NGLogicalOffset,
NGLogicalSize);
// Constructs a derived constraint space sharing the same backing
// NGPhysicalConstraintSpace and a different NGWritingMode.
NGConstraintSpace(NGWritingMode,
const NGConstraintSpace& other,
NGLogicalOffset,
NGLogicalSize);
// This should live on NGBox or another layout bridge and probably take a root
// NGConstraintSpace or a NGPhysicalConstraintSpace.
static NGConstraintSpace* CreateFromLayoutObject(const LayoutBox&);
NGPhysicalConstraintSpace* PhysicalSpace() const { return physical_space_; }
NGWritingMode WritingMode() const {
return static_cast<NGWritingMode>(writing_mode_);
}
// Size of the container. Used for the following three cases:
// 1) Percentage resolution.
// 2) Resolving absolute positions of children.
// 3) Defining the threshold that triggers the presence of a scrollbar. Only
// applies if the corresponding scrollbarTrigger flag has been set for the
// direction.
NGLogicalSize ContainerSize() const;
// Offset relative to the root constraint space.
NGLogicalOffset Offset() const { return offset_; }
// Returns the effective size of the constraint space. Equal to the
// ContainerSize() for the root constraint space but derived constraint spaces
// return the size of the layout opportunity.
virtual NGLogicalSize Size() const { return size_; }
// Whether exceeding the containerSize triggers the presence of a scrollbar
// for the indicated direction.
// If exceeded the current layout should be aborted and invoked again with a
// constraint space modified to reserve space for a scrollbar.
bool InlineTriggersScrollbar() const;
bool BlockTriggersScrollbar() const;
// Some layout modes “stretch” their children to a fixed size (e.g. flex,
// grid). These flags represented whether a layout needs to produce a
// fragment that satisfies a fixed constraint in the inline and block
// direction respectively.
bool FixedInlineSize() const;
bool FixedBlockSize() const;
// If specified a layout should produce a Fragment which fragments at the
// blockSize if possible.
NGFragmentationType BlockFragmentationType() const;
// Modifies constraint space to account for a placed fragment. Depending on
// the shape of the fragment this will either modify the inline or block
// size, or add an exclusion.
void Subtract(const NGFragment*);
NGLayoutOpportunityIterator* LayoutOpportunities(
unsigned clear = NGClearNone,
bool for_inline_or_bfc = false);
DEFINE_INLINE_VIRTUAL_TRACE() { visitor->trace(physical_space_); }
// The setters for the NGConstraintSpace should only be used when constructing
// a derived NGConstraintSpace.
void SetOverflowTriggersScrollbar(bool inlineTriggers, bool blockTriggers);
void SetFixedSize(bool inlineFixed, bool blockFixed);
void SetFragmentationType(NGFragmentationType);
String toString() const;
private:
Member<NGPhysicalConstraintSpace> physical_space_;
NGLogicalOffset offset_;
NGLogicalSize size_;
unsigned writing_mode_ : 3;
};
class CORE_EXPORT NGLayoutOpportunityIterator final
: public GarbageCollectedFinalized<NGLayoutOpportunityIterator> {
public:
NGLayoutOpportunityIterator(NGConstraintSpace* space,
unsigned clear,
bool for_inline_or_bfc);
~NGLayoutOpportunityIterator() {}
NGConstraintSpace* Next();
DEFINE_INLINE_VIRTUAL_TRACE() {
visitor->trace(constraint_space_);
visitor->trace(current_opportunities_);
}
private:
void computeForExclusion(unsigned index);
LayoutUnit heightForOpportunity(LayoutUnit left,
LayoutUnit top,
LayoutUnit right,
LayoutUnit bottom);
void addLayoutOpportunity(LayoutUnit left,
LayoutUnit top,
LayoutUnit right,
LayoutUnit bottom);
Member<NGConstraintSpace> constraint_space_;
unsigned clear_;
bool for_inline_or_bfc_;
Vector<NGExclusion> filtered_exclusions_;
HeapVector<Member<NGConstraintSpace>> current_opportunities_;
unsigned current_exclusion_idx_;
};
inline std::ostream& operator<<(std::ostream& stream,
const NGConstraintSpace& value) {
return stream << value.toString();
}
} // namespace blink
#endif // NGConstraintSpace_h