Files
yoga/yoga/algorithm/SizingMode.h
Nick Gerleman a121483e81 Use CSS terminology for sizing rules (#1460)
Summary:
X-link: https://github.com/facebook/react-native/pull/41390

Pull Request resolved: https://github.com/facebook/yoga/pull/1460

Yoga passes `MeasureMode`/`YGMeasureMode` to express constraints in how a box should be measured, given definite or indefinite available space.

This is modeled after Android [MeasureSpec](https://developer.android.com/reference/android/view/View.MeasureSpec), with a table above `calculateLayoutImpl()` explaining the CSS terms they map to. This can be confusing when flipping between the spec, and code.

This switches internal usages to the CSS terms, but leaves around `YGMeasureMode` since it is the public API passed to measure functions.

Reviewed By: joevilches

Differential Revision: D51068417

fbshipit-source-id: 0a76266a4e7e0cc39996164607229c3c41de2818
2023-11-25 20:41:22 -08:00

74 lines
2.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <yoga/debug/AssertFatal.h>
#include <yoga/enums/MeasureMode.h>
namespace facebook::yoga {
/**
* Corresponds to a CSS auto box sizes. Missing "min-content", as Yoga does not
* current support automatic minimum sizes.
* https://www.w3.org/TR/css-sizing-3/#auto-box-sizes
* https://www.w3.org/TR/css-flexbox-1/#min-size-auto
*/
enum class SizingMode {
/**
* The size a box would take if its outer size filled the available space in
* the given axis; in other words, the stretch fit into the available space,
* if that is definite. Undefined if the available space is indefinite.
*/
StretchFit,
/**
* A boxs “ideal” size in a given axis when given infinite available space.
* Usually this is the smallest size the box could take in that axis while
* still fitting around its contents, i.e. minimizing unfilled space while
* avoiding overflow.
*/
MaxContent,
/**
* If the available space in a given axis is definite, equal to
* clamp(min-content size, stretch-fit size, max-content size) (i.e.
* max(min-content size, min(max-content size, stretch-fit size))). When
* sizing under a min-content constraint, equal to the min-content size.
* Otherwise, equal to the max-content size in that axis.
*/
FitContent,
};
inline MeasureMode measureMode(SizingMode mode) {
switch (mode) {
case SizingMode::StretchFit:
return MeasureMode::Exactly;
case SizingMode::MaxContent:
return MeasureMode::Undefined;
case SizingMode::FitContent:
return MeasureMode::AtMost;
}
fatalWithMessage("Invalid SizingMode");
}
inline SizingMode sizingMode(MeasureMode mode) {
switch (mode) {
case MeasureMode::Exactly:
return SizingMode::StretchFit;
case MeasureMode::Undefined:
return SizingMode::MaxContent;
case MeasureMode::AtMost:
return SizingMode::FitContent;
}
fatalWithMessage("Invalid MeasureMode");
}
} // namespace facebook::yoga