Start UI core layout value tests
This commit is contained in:
57
src/ui_core/layout_value.cpp
Normal file
57
src/ui_core/layout_value.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#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";
|
||||
}
|
||||
|
||||
}
|
||||
24
src/ui_core/layout_value.h
Normal file
24
src/ui_core/layout_value.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include "foundation/result.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <string_view>
|
||||
|
||||
namespace pp::ui {
|
||||
|
||||
enum class LayoutLengthKind : std::uint8_t {
|
||||
auto_value,
|
||||
pixels,
|
||||
percent,
|
||||
};
|
||||
|
||||
struct LayoutLength {
|
||||
LayoutLengthKind kind = LayoutLengthKind::auto_value;
|
||||
std::uint32_t value = 0;
|
||||
};
|
||||
|
||||
[[nodiscard]] pp::foundation::Result<LayoutLength> parse_layout_length(std::string_view text) noexcept;
|
||||
[[nodiscard]] const char* layout_length_kind_name(LayoutLengthKind kind) noexcept;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user