Skip to content
Repos

DevPlaceApp

C++ 3.5 KB96 lines Opens in site

Directory Raw Download

#pragma once
// Spec: SPEC.md UX-LAYOUT-01. Point-based geometry, matching SwiftUI semantics (points, not pixels).
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <optional>

namespace dp::ui {

constexpr float kInf = 1.0e9f;

struct Size {
  float w = 0;
  float h = 0;
};

struct Point {
  float x = 0;
  float y = 0;
};

struct Insets {
  float top = 0, left = 0, bottom = 0, right = 0;
  static Insets all(float v) { return {v, v, v, v}; }
  static Insets symmetric(float horizontal, float vertical) { return {vertical, horizontal, vertical, horizontal}; }
  float horizontal() const { return left + right; }
  float vertical() const { return top + bottom; }
};

struct Rect {
  float x = 0, y = 0, w = 0, h = 0;
  float right() const { return x + w; }
  float bottom() const { return y + h; }
  Point center() const { return {x + w / 2, y + h / 2}; }
  bool contains(Point p) const { return p.x >= x && p.x < x + w && p.y >= y && p.y < y + h; }
  Rect inset(const Insets& i) const {
    return {x + i.left, y + i.top, std::max(0.0f, w - i.horizontal()), std::max(0.0f, h - i.vertical())};
  }
  Rect outset(float v) const { return {x - v, y - v, w + 2 * v, h + 2 * v}; }
  Rect intersect(const Rect& o) const {
    const float nx = std::max(x, o.x), ny = std::max(y, o.y);
    const float nr = std::min(right(), o.right()), nb = std::min(bottom(), o.bottom());
    return {nx, ny, std::max(0.0f, nr - nx), std::max(0.0f, nb - ny)};
  }
  Rect offset(float dx, float dy) const { return {x + dx, y + dy, w, h}; }
  bool empty() const { return w <= 0 || h <= 0; }
};

enum class Axis { Horizontal, Vertical };
enum class HAlign { Leading, Center, Trailing };
enum class VAlign { Top, Center, Bottom };

struct Alignment {
  HAlign h = HAlign::Center;
  VAlign v = VAlign::Center;
  static Alignment center() { return {HAlign::Center, VAlign::Center}; }
  static Alignment leading() { return {HAlign::Leading, VAlign::Center}; }
  static Alignment trailing() { return {HAlign::Trailing, VAlign::Center}; }
  static Alignment top() { return {HAlign::Center, VAlign::Top}; }
  static Alignment bottom() { return {HAlign::Center, VAlign::Bottom}; }
  static Alignment topLeading() { return {HAlign::Leading, VAlign::Top}; }
  static Alignment topTrailing() { return {HAlign::Trailing, VAlign::Top}; }
  static Alignment bottomLeading() { return {HAlign::Leading, VAlign::Bottom}; }
  static Alignment bottomTrailing() { return {HAlign::Trailing, VAlign::Bottom}; }
};

// SwiftUI layout proposal: nullopt means "ideal size", kInf means "as large as you like".
struct Proposal {
  std::optional<float> w;
  std::optional<float> h;
  static Proposal ideal() { return {}; }
  static Proposal fixed(float w, float h) { return {w, h}; }
  static Proposal width(float w) { return {w, std::nullopt}; }
  float along(Axis a) const { return (a == Axis::Horizontal ? w : h).value_or(kInf); }
};

struct Color {
  std::uint8_t r = 0, g = 0, b = 0, a = 255;
  static constexpr Color rgb(std::uint32_t hex, std::uint8_t alpha = 255) {
    return {static_cast<std::uint8_t>(hex >> 16), static_cast<std::uint8_t>(hex >> 8), static_cast<std::uint8_t>(hex),
            alpha};
  }
  static constexpr Color clear() { return {0, 0, 0, 0}; }
  Color opacity(float f) const {
    return {r, g, b, static_cast<std::uint8_t>(std::clamp(a * f, 0.0f, 255.0f) + 0.5f)};
  }
  bool operator==(const Color&) const = default;
};

struct Corners {
  float tl = 0, tr = 0, br = 0, bl = 0;
  static Corners all(float r) { return {r, r, r, r}; }
  bool uniform() const { return tl == tr && tr == br && br == bl; }
};

}  // namespace dp::ui
33.6ms