58 lines
1.7 KiB
C++
58 lines
1.7 KiB
C++
#include "ui_core/layout_value.h"
|
|
|
|
#include "foundation/parse.h"
|
|
|
|
namespace pp::ui {
|
|
|
|
pp::foundation::Result<LayoutLength> parse_layout_length(std::string_view text) noexcept
|
|
{
|
|
if (text == "auto") {
|
|
return pp::foundation::Result<LayoutLength>::success(
|
|
LayoutLength { .kind = LayoutLengthKind::auto_value, .value = 0 });
|
|
}
|
|
|
|
if (text.empty()) {
|
|
return pp::foundation::Result<LayoutLength>::failure(
|
|
pp::foundation::Status::invalid_argument("layout length must not be empty"));
|
|
}
|
|
|
|
if (text.back() == '%') {
|
|
const auto number = pp::foundation::parse_u32(text.substr(0, text.size() - 1U));
|
|
if (!number) {
|
|
return pp::foundation::Result<LayoutLength>::failure(number.status());
|
|
}
|
|
|
|
if (number.value() > 100U) {
|
|
return pp::foundation::Result<LayoutLength>::failure(
|
|
pp::foundation::Status::out_of_range("layout percent must be between 0 and 100"));
|
|
}
|
|
|
|
return pp::foundation::Result<LayoutLength>::success(
|
|
LayoutLength { .kind = LayoutLengthKind::percent, .value = number.value() });
|
|
}
|
|
|
|
const auto pixels = pp::foundation::parse_u32(text);
|
|
if (!pixels) {
|
|
return pp::foundation::Result<LayoutLength>::failure(pixels.status());
|
|
}
|
|
|
|
return pp::foundation::Result<LayoutLength>::success(
|
|
LayoutLength { .kind = LayoutLengthKind::pixels, .value = pixels.value() });
|
|
}
|
|
|
|
const char* layout_length_kind_name(LayoutLengthKind kind) noexcept
|
|
{
|
|
switch (kind) {
|
|
case LayoutLengthKind::auto_value:
|
|
return "auto";
|
|
case LayoutLengthKind::pixels:
|
|
return "pixels";
|
|
case LayoutLengthKind::percent:
|
|
return "percent";
|
|
}
|
|
|
|
return "unknown";
|
|
}
|
|
|
|
}
|