Skip to content
Repos

DevPlaceApp

C++ 6.9 KB241 lines Opens in site

Directory Raw Download

#include "ui/Modifiers.hpp"

#include <algorithm>
#include <cmath>

namespace dp::ui {

namespace {

Point alignInside(const Rect& outer, Size inner, Alignment a) {
  float x = outer.x, y = outer.y;
  if (a.h == HAlign::Center) x += (outer.w - inner.w) / 2;
  else if (a.h == HAlign::Trailing) x += outer.w - inner.w;
  if (a.v == VAlign::Center) y += (outer.h - inner.h) / 2;
  else if (a.v == VAlign::Bottom) y += outer.h - inner.h;
  return {x, y};
}

std::optional<float> shrink(std::optional<float> v, float by) {
  if (!v) return v;
  if (*v >= kInf / 2) return v;
  return std::max(0.0f, *v - by);
}

}  // namespace

// ---- WrapperView ----
Size WrapperView::compute(const Proposal& p) { return child() ? child()->measure(p) : Size{}; }
void WrapperView::place(const Rect& r) {
  frame_ = r;
  if (child()) child()->place(r);
}
void WrapperView::assignIds(std::uint64_t parent, std::uint64_t index) {
  View::assignIds(parent, index);
}

// ---- Padding ----
Size PaddingView::compute(const Proposal& p) {
  Proposal q{shrink(p.w, insets_.horizontal()), shrink(p.h, insets_.vertical())};
  const Size s = child() ? child()->measure(q) : Size{};
  return {s.w + insets_.horizontal(), s.h + insets_.vertical()};
}
void PaddingView::place(const Rect& r) {
  frame_ = r;
  if (child()) child()->place(r.inset(insets_));
}

// ---- Frame ----
Proposal FrameView::childProposal(const Proposal& p) const {
  Proposal q = p;
  auto axis = [](std::optional<float> in, std::optional<float> mn, std::optional<float> ideal, std::optional<float> mx) {
    std::optional<float> v = in;
    if (!v) return ideal ? std::optional<float>(*ideal) : std::nullopt;
    if (mn) v = std::max(*v, *mn);
    if (mx) v = std::min(*v, *mx);
    return v;
  };
  // Fixed frame (min == max): child is always proposed exactly that value.
  q.w = (spec_.minW && spec_.maxW && *spec_.minW == *spec_.maxW) ? spec_.maxW : axis(p.w, spec_.minW, spec_.idealW, spec_.maxW);
  q.h = (spec_.minH && spec_.maxH && *spec_.minH == *spec_.maxH) ? spec_.maxH : axis(p.h, spec_.minH, spec_.idealH, spec_.maxH);
  return q;
}

Size FrameView::finish(const Proposal& p, Size c) const {
  auto axis = [](std::optional<float> in, float childLen, std::optional<float> mn, std::optional<float> mx) {
    if (mn && mx && *mn == *mx) return *mx;
    float v = childLen;
    if (mx) {
      // A flexible frame expands up to its maximum when offered more room.
      if (in && *in < kInf / 2) v = std::max(v, std::min(*in, *mx));
      else if (in && *in >= kInf / 2) v = *mx >= kInf / 2 ? kInf : std::max(v, *mx);
    }
    if (mn) v = std::max(v, *mn);
    if (mx) v = std::min(v, *mx);
    return v;
  };
  return {axis(p.w, c.w, spec_.minW, spec_.maxW), axis(p.h, c.h, spec_.minH, spec_.maxH)};
}

Size FrameView::compute(const Proposal& p) {
  const Size c = child() ? child()->measure(childProposal(p)) : Size{};
  return finish(p, c);
}

void FrameView::place(const Rect& r) {
  frame_ = r;
  if (!child()) return;
  Proposal q = childProposal(Proposal{r.w, r.h});
  const Size cs = child()->measure(q);
  const Point o = alignInside(r, cs, spec_.align);
  child()->place({o.x, o.y, cs.w, cs.h});
}

// ---- FixedSize ----
Size FixedSizeView::compute(const Proposal& p) {
  Proposal q = p;
  if (h_) q.w = std::nullopt;
  if (v_) q.h = std::nullopt;
  return child() ? child()->measure(q) : Size{};
}
void FixedSizeView::place(const Rect& r) {
  frame_ = r;
  if (!child()) return;
  Proposal q{r.w, r.h};
  if (h_) q.w = std::nullopt;
  if (v_) q.h = std::nullopt;
  const Size cs = child()->measure(q);
  child()->place({r.x, r.y, h_ ? cs.w : r.w, v_ ? cs.h : r.h});
}

// ---- Background ----
void BackgroundView::assignIds(std::uint64_t parent, std::uint64_t index) {
  View::assignIds(parent, index);
  if (bg_) bg_->assignIds(hashCombine(id_, 0xB6), 0);
}
void BackgroundView::place(const Rect& r) {
  WrapperView::place(r);
  if (bg_) {
    const Size s = bg_->measure(Proposal{r.w, r.h});
    const Point o = alignInside(r, s, align_);
    bg_->place({o.x, o.y, s.w, s.h});
  }
}
void BackgroundView::draw(Canvas& cv) {
  if (color_) cv.fillRRect(frame_, radius_, *color_);
  if (bg_) bg_->draw(cv);
  WrapperView::draw(cv);
}

// ---- Overlay ----
void OverlayView::assignIds(std::uint64_t parent, std::uint64_t index) {
  View::assignIds(parent, index);
  if (over_) over_->assignIds(hashCombine(id_, 0x0E), 0);
}
void OverlayView::place(const Rect& r) {
  WrapperView::place(r);
  if (over_) {
    const Size s = over_->measure(Proposal{r.w, r.h});
    const Point o = alignInside(r, s, align_);
    over_->place({o.x, o.y, s.w, s.h});
  }
}
void OverlayView::draw(Canvas& cv) {
  WrapperView::draw(cv);
  if (over_) over_->draw(cv);
}
void OverlayView::hits(Point p, std::vector<View*>& out) {
  if (over_) over_->hits(p, out);
  WrapperView::hits(p, out);
}

// ---- Border / Clip / Opacity / Offset / Scale / Shadow ----
void BorderView::draw(Canvas& cv) {
  WrapperView::draw(cv);
  cv.strokeRRect(frame_, Corners::all(radius_), color_, width_, opacity_);
}

void ClipView::draw(Canvas& cv) {
  Corners r = radius_;
  if (circle_) r = Corners::all(std::min(frame_.w, frame_.h) / 2);
  cv.pushClip(frame_, r);
  WrapperView::draw(cv);
  cv.popClip();
}
void ClipView::hits(Point p, std::vector<View*>& out) {
  if (!frame_.contains(p)) return;
  WrapperView::hits(p, out);
}

void OpacityView::draw(Canvas& cv) {
  cv.pushOpacity(f_);
  WrapperView::draw(cv);
  cv.popOpacity();
}

void OffsetView::place(const Rect& r) {
  frame_ = r.offset(dx_, dy_);
  if (child()) child()->place(frame_);
}

void OffsetFractionView::place(const Rect& r) {
  frame_ = r.offset(fx_ * r.w, fy_ * r.h);
  if (child()) child()->place(frame_);
}

void ScaleView::place(const Rect& r) {
  frame_ = r;
  if (!child()) return;
  const float dw = r.w * (1 - s_) / 2, dh = r.h * (1 - s_) / 2;
  child()->place({r.x + dw, r.y + dh, r.w * s_, r.h * s_});
}

void ShadowView::draw(Canvas& cv) {
  cv.shadow(frame_, shape_, color_, blur_, {dx_, dy_});
  WrapperView::draw(cv);
}

// ---- Env ----
EnvValues EnvView::scoped() const {
  EnvValues v = Env::cur();
  mutate_(v);
  return v;
}
Size EnvView::compute(const Proposal& p) {
  EnvScope s(scoped());
  return child() ? child()->measure(p) : Size{};
}
void EnvView::place(const Rect& r) {
  EnvScope s(scoped());
  WrapperView::place(r);
}
void EnvView::draw(Canvas& cv) {
  EnvScope s(scoped());
  const EnvValues& e = Env::cur();
  if (e.disabled && !disabledDrawn_) {
    disabledDrawn_ = true;
    cv.pushOpacity(0.4f);
    WrapperView::draw(cv);
    cv.popOpacity();
    disabledDrawn_ = false;
  } else {
    WrapperView::draw(cv);
  }
}
void EnvView::hits(Point p, std::vector<View*>& out) {
  EnvScope s(scoped());
  WrapperView::hits(p, out);
}
void EnvView::assignIds(std::uint64_t parent, std::uint64_t index) {
  EnvScope s(scoped());
  View::assignIds(parent, index);
}

// ---- Tap ----
void TapView::hits(Point p, std::vector<View*>& out) {
  if (!frame_.contains(p)) return;
  WrapperView::hits(p, out);
  if (!Env::cur().disabled) out.push_back(this);
}

}  // namespace dp::ui
23.0ms