173 lines
9.4 KiB
C++
173 lines
9.4 KiB
C++
#include "detail/control_types.hpp"
|
|
|
|
|
|
auto luma::label(std::string text) -> std::expected<std::unique_ptr<luma::control>, modulus::error> {
|
|
if (text.empty()) {
|
|
return luma::detail::invalid<std::unique_ptr<luma::control>>("label text must not be empty");
|
|
}
|
|
return modulus::success(std::unique_ptr<luma::control>{std::make_unique<luma::detail::label_control>(std::move(text))});
|
|
}
|
|
|
|
auto luma::button(std::string text, std::function<void()> on_click) -> std::expected<std::unique_ptr<luma::control>, modulus::error> {
|
|
if (text.empty()) {
|
|
return luma::detail::invalid<std::unique_ptr<luma::control>>("button text must not be empty");
|
|
}
|
|
if (!on_click) {
|
|
return luma::detail::invalid<std::unique_ptr<luma::control>>("button click callback must not be empty");
|
|
}
|
|
return modulus::success(std::unique_ptr<luma::control>{std::make_unique<luma::detail::button_control>(std::move(text), std::move(on_click))});
|
|
}
|
|
|
|
auto luma::text_field(luma::text_field_options options, std::function<void(std::string)> on_change) -> std::expected<std::unique_ptr<luma::control>, modulus::error> {
|
|
auto input = luma::make_text_input(std::move(options), std::move(on_change));
|
|
if (!input) {
|
|
return modulus::fail<std::unique_ptr<luma::control>>(input.error());
|
|
}
|
|
return luma::text_field(std::move(*input));
|
|
}
|
|
|
|
auto luma::text_field(std::shared_ptr<luma::text_input_state> input) -> std::expected<std::unique_ptr<luma::control>, modulus::error> {
|
|
if (input == nullptr) {
|
|
return luma::detail::invalid<std::unique_ptr<luma::control>>("text input state must not be null");
|
|
}
|
|
return modulus::success(std::unique_ptr<luma::control>{std::make_unique<luma::detail::text_field_control>(std::move(input))});
|
|
}
|
|
|
|
auto luma::soft_keyboard(luma::keyboard_layout layout, std::shared_ptr<luma::text_input_state> input, std::function<void()> on_enter) -> std::expected<std::unique_ptr<luma::control>, modulus::error> {
|
|
if (auto status = luma::detail::valid_keyboard_layout(layout); !status) {
|
|
return modulus::fail<std::unique_ptr<luma::control>>(status.error());
|
|
}
|
|
if (input == nullptr) {
|
|
return luma::detail::invalid<std::unique_ptr<luma::control>>("text input state must not be null");
|
|
}
|
|
return modulus::success(std::unique_ptr<luma::control>{std::make_unique<luma::detail::soft_keyboard_control>(std::move(layout), std::move(input), std::move(on_enter))});
|
|
}
|
|
|
|
auto luma::soft_keyboard(luma::keyboard_layout layout, std::function<void(std::string)> on_text, std::function<void()> on_enter) -> std::expected<std::unique_ptr<luma::control>, modulus::error> {
|
|
if (auto status = luma::detail::valid_keyboard_layout(layout); !status) {
|
|
return modulus::fail<std::unique_ptr<luma::control>>(status.error());
|
|
}
|
|
if (!on_text) {
|
|
return luma::detail::invalid<std::unique_ptr<luma::control>>("keyboard text callback must not be empty");
|
|
}
|
|
return modulus::success(std::unique_ptr<luma::control>{std::make_unique<luma::detail::soft_keyboard_control>(std::move(layout), std::move(on_text), std::move(on_enter))});
|
|
}
|
|
|
|
auto luma::panel(std::expected<std::vector<std::unique_ptr<luma::control>>, modulus::error> children) -> std::expected<std::unique_ptr<luma::control>, modulus::error> {
|
|
if (!children) {
|
|
return modulus::fail<std::unique_ptr<luma::control>>(children.error());
|
|
}
|
|
return luma::panel(std::move(*children));
|
|
}
|
|
|
|
auto luma::panel(std::vector<std::unique_ptr<luma::control>> children) -> std::expected<std::unique_ptr<luma::control>, modulus::error> {
|
|
if (auto status = luma::detail::validate_children(children); !status) {
|
|
return modulus::fail<std::unique_ptr<luma::control>>(status.error());
|
|
}
|
|
return modulus::success(std::unique_ptr<luma::control>{std::make_unique<luma::detail::panel_control>(std::move(children))});
|
|
}
|
|
|
|
auto luma::spacer(luma::size value) -> std::expected<std::unique_ptr<luma::control>, modulus::error> {
|
|
if (!luma::detail::finite_size(value)) {
|
|
return luma::detail::invalid<std::unique_ptr<luma::control>>("spacer size must be finite and non-negative");
|
|
}
|
|
return modulus::success(std::unique_ptr<luma::control>{std::make_unique<luma::detail::spacer_control>(value)});
|
|
}
|
|
|
|
auto luma::progress_bar(std::float32_t value) -> std::expected<std::unique_ptr<luma::control>, modulus::error> {
|
|
if (!std::isfinite(value)) {
|
|
return luma::detail::invalid<std::unique_ptr<luma::control>>("progress value must be finite");
|
|
}
|
|
return modulus::success(std::unique_ptr<luma::control>{std::make_unique<luma::detail::progress_bar_control>(value)});
|
|
}
|
|
|
|
auto luma::image(std::shared_ptr<luma::texture> texture, luma::image_fit fit) -> std::expected<std::unique_ptr<luma::control>, modulus::error> {
|
|
if (texture == nullptr) {
|
|
return luma::detail::invalid<std::unique_ptr<luma::control>>("image texture must not be null");
|
|
}
|
|
auto info = texture->info();
|
|
if (info.width <= 0 || info.height <= 0) {
|
|
return luma::detail::invalid<std::unique_ptr<luma::control>>("image texture dimensions must be positive");
|
|
}
|
|
return modulus::success(std::unique_ptr<luma::control>{std::make_unique<luma::detail::image_control>(std::move(texture), fit)});
|
|
}
|
|
|
|
auto luma::row(std::expected<std::vector<std::unique_ptr<luma::control>>, modulus::error> children, std::float32_t gap, luma::alignment align) -> std::expected<std::unique_ptr<luma::control>, modulus::error> {
|
|
if (!children) {
|
|
return modulus::fail<std::unique_ptr<luma::control>>(children.error());
|
|
}
|
|
return luma::row(std::move(*children), gap, align);
|
|
}
|
|
|
|
auto luma::row(std::vector<std::unique_ptr<luma::control>> children, std::float32_t gap, luma::alignment align) -> std::expected<std::unique_ptr<luma::control>, modulus::error> {
|
|
if (auto status = luma::detail::validate_children(children); !status) {
|
|
return modulus::fail<std::unique_ptr<luma::control>>(status.error());
|
|
}
|
|
if (!std::isfinite(gap) || gap < 0.0F) {
|
|
return luma::detail::invalid<std::unique_ptr<luma::control>>("row gap must be finite and non-negative");
|
|
}
|
|
return modulus::success(std::unique_ptr<luma::control>{std::make_unique<luma::detail::linear_control>(luma::direction::ROW, std::move(children), gap, align)});
|
|
}
|
|
|
|
auto luma::column(std::expected<std::vector<std::unique_ptr<luma::control>>, modulus::error> children, std::float32_t gap, luma::alignment align) -> std::expected<std::unique_ptr<luma::control>, modulus::error> {
|
|
if (!children) {
|
|
return modulus::fail<std::unique_ptr<luma::control>>(children.error());
|
|
}
|
|
return luma::column(std::move(*children), gap, align);
|
|
}
|
|
|
|
auto luma::column(std::vector<std::unique_ptr<luma::control>> children, std::float32_t gap, luma::alignment align) -> std::expected<std::unique_ptr<luma::control>, modulus::error> {
|
|
if (auto status = luma::detail::validate_children(children); !status) {
|
|
return modulus::fail<std::unique_ptr<luma::control>>(status.error());
|
|
}
|
|
if (!std::isfinite(gap) || gap < 0.0F) {
|
|
return luma::detail::invalid<std::unique_ptr<luma::control>>("column gap must be finite and non-negative");
|
|
}
|
|
return modulus::success(std::unique_ptr<luma::control>{std::make_unique<luma::detail::linear_control>(luma::direction::COLUMN, std::move(children), gap, align)});
|
|
}
|
|
|
|
auto luma::stack(std::expected<std::vector<std::unique_ptr<luma::control>>, modulus::error> children) -> std::expected<std::unique_ptr<luma::control>, modulus::error> {
|
|
if (!children) {
|
|
return modulus::fail<std::unique_ptr<luma::control>>(children.error());
|
|
}
|
|
return luma::stack(std::move(*children));
|
|
}
|
|
|
|
auto luma::stack(std::vector<std::unique_ptr<luma::control>> children) -> std::expected<std::unique_ptr<luma::control>, modulus::error> {
|
|
if (auto status = luma::detail::validate_children(children); !status) {
|
|
return modulus::fail<std::unique_ptr<luma::control>>(status.error());
|
|
}
|
|
return modulus::success(std::unique_ptr<luma::control>{std::make_unique<luma::detail::stack_control>(std::move(children))});
|
|
}
|
|
|
|
auto luma::scroll_view(std::expected<std::unique_ptr<luma::control>, modulus::error> child, std::bool_t vertical, std::bool_t horizontal) -> std::expected<std::unique_ptr<luma::control>, modulus::error> {
|
|
if (!child) {
|
|
return modulus::fail<std::unique_ptr<luma::control>>(child.error());
|
|
}
|
|
return luma::scroll_view(std::move(*child), vertical, horizontal);
|
|
}
|
|
|
|
auto luma::scroll_view(std::unique_ptr<luma::control> child, std::bool_t vertical, std::bool_t horizontal) -> std::expected<std::unique_ptr<luma::control>, modulus::error> {
|
|
if (child == nullptr) {
|
|
return luma::detail::invalid<std::unique_ptr<luma::control>>("scroll view child must not be null");
|
|
}
|
|
if (!vertical && !horizontal) {
|
|
return luma::detail::invalid<std::unique_ptr<luma::control>>("scroll view must enable at least one axis");
|
|
}
|
|
return modulus::success(std::unique_ptr<luma::control>{std::make_unique<luma::detail::scroll_view_control>(std::move(child), vertical, horizontal)});
|
|
}
|
|
|
|
auto luma::list(std::expected<std::vector<std::unique_ptr<luma::control>>, modulus::error> children, std::float32_t gap) -> std::expected<std::unique_ptr<luma::control>, modulus::error> {
|
|
if (!children) {
|
|
return modulus::fail<std::unique_ptr<luma::control>>(children.error());
|
|
}
|
|
return luma::list(std::move(*children), gap);
|
|
}
|
|
|
|
auto luma::list(std::vector<std::unique_ptr<luma::control>> children, std::float32_t gap) -> std::expected<std::unique_ptr<luma::control>, modulus::error> {
|
|
if (!std::isfinite(gap) || gap < 0.0F) {
|
|
return luma::detail::invalid<std::unique_ptr<luma::control>>("list gap must be finite and non-negative");
|
|
}
|
|
return luma::column(std::move(children), gap, luma::alignment::STRETCH);
|
|
}
|