ConstantPropertyMap.cpp [plain text]
#include "config.h"
#include "ConstantPropertyMap.h"
#include "CSSCustomPropertyValue.h"
#include "CSSParserTokenRange.h"
#include "CSSVariableData.h"
#include "Document.h"
#include "Page.h"
#include <wtf/NeverDestroyed.h>
namespace WebCore {
ConstantPropertyMap::ConstantPropertyMap(Document& document)
: m_document(document)
{
}
const ConstantPropertyMap::Values& ConstantPropertyMap::values() const
{
if (!m_values)
const_cast<ConstantPropertyMap&>(*this).buildValues();
return *m_values;
}
const AtomicString& ConstantPropertyMap::nameForProperty(ConstantProperty property) const
{
static NeverDestroyed<AtomicString> safeAreaInsetTopName("safe-area-inset-top", AtomicString::ConstructFromLiteral);
static NeverDestroyed<AtomicString> safeAreaInsetRightName("safe-area-inset-right", AtomicString::ConstructFromLiteral);
static NeverDestroyed<AtomicString> safeAreaInsetBottomName("safe-area-inset-bottom", AtomicString::ConstructFromLiteral);
static NeverDestroyed<AtomicString> safeAreaInsetLeftName("safe-area-inset-left", AtomicString::ConstructFromLiteral);
switch (property) {
case ConstantProperty::SafeAreaInsetTop:
return safeAreaInsetTopName;
case ConstantProperty::SafeAreaInsetRight:
return safeAreaInsetRightName;
case ConstantProperty::SafeAreaInsetBottom:
return safeAreaInsetBottomName;
case ConstantProperty::SafeAreaInsetLeft:
return safeAreaInsetLeftName;
}
return nullAtom();
}
void ConstantPropertyMap::setValueForProperty(ConstantProperty property, Ref<CSSVariableData>&& data)
{
if (!m_values)
buildValues();
auto& name = nameForProperty(property);
m_values->set(name, CSSCustomPropertyValue::createWithVariableData(name, WTFMove(data)));
}
void ConstantPropertyMap::buildValues()
{
m_values = Values { };
updateConstantsForSafeAreaInsets();
}
static Ref<CSSVariableData> variableDataForPositivePixelLength(float lengthInPx)
{
ASSERT(lengthInPx >= 0);
CSSParserToken token(NumberToken, lengthInPx, NumberValueType, NoSign);
token.convertToDimensionWithUnit("px");
Vector<CSSParserToken> tokens { token };
CSSParserTokenRange tokenRange(tokens);
return CSSVariableData::create(tokenRange, false);
}
void ConstantPropertyMap::updateConstantsForSafeAreaInsets()
{
FloatBoxExtent unobscuredSafeAreaInsets = m_document.page() ? m_document.page()->unobscuredSafeAreaInsets() : FloatBoxExtent();
setValueForProperty(ConstantProperty::SafeAreaInsetTop, variableDataForPositivePixelLength(unobscuredSafeAreaInsets.top()));
setValueForProperty(ConstantProperty::SafeAreaInsetRight, variableDataForPositivePixelLength(unobscuredSafeAreaInsets.right()));
setValueForProperty(ConstantProperty::SafeAreaInsetBottom, variableDataForPositivePixelLength(unobscuredSafeAreaInsets.bottom()));
setValueForProperty(ConstantProperty::SafeAreaInsetLeft, variableDataForPositivePixelLength(unobscuredSafeAreaInsets.left()));
}
void ConstantPropertyMap::didChangeSafeAreaInsets()
{
updateConstantsForSafeAreaInsets();
m_document.invalidateMatchedPropertiesCacheAndForceStyleRecalc();
}
}