#include "ui/View.hpp"
#include <algorithm>
#include "ui/Modifiers.hpp"
namespace dp::ui {
std::uint64_t hashCombine(std::uint64_t a, std::uint64_t b) {
a ^= b + 0x9e3779b97f4a7c15ULL + (a << 6) + (a >> 2);
return a * 0xff51afd7ed558ccdULL;
}
std::uint64_t hashString(const std::string& s) {
std::uint64_t h = 1469598103934665603ULL;
for (unsigned char c : s) h = (h ^ c) * 1099511628211ULL;
return h;
}
UiState& UiState::instance() {
static UiState s;
return s;
}
void UiState::prune(std::size_t keepAtMost) {
if (slots_.size() > keepAtMost) slots_.clear();
}
Size View::measure(const Proposal& p) {
for (const CacheEntry& e : cache_) {
if (e.w == p.w && e.h == p.h) return e.size;
}
Size s = compute(p);
if (cache_.size() < 12) cache_.push_back({p.w, p.h, s});
return s;
}
void View::place(const Rect& r) { frame_ = r; }
void View::draw(Canvas& c) {
const Rect clip = c.clipBounds();
for (const ViewPtr& ch : children_) {
const Rect& b = ch->bounds();
if (b.right() < clip.x - 1 || b.x > clip.right() + 1 || b.bottom() < clip.y - 1 || b.y > clip.bottom() + 1) {
if (!b.empty()) continue;
}
ch->draw(c);
}
}
void View::assignIds(std::uint64_t parent, std::uint64_t index) {
id_ = hasKey_ ? hashCombine(parent, hashString(key_)) : hashCombine(parent, index + 1);
std::uint64_t i = 0;
for (const ViewPtr& ch : children_) ch->assignIds(id_, i++);
}
View* View::findById(std::uint64_t target) {
if (id_ == target) return this;
for (const ViewPtr& ch : children_) {
if (View* f = ch->findById(target)) return f;
}
return nullptr;
}
void View::hits(Point p, std::vector<View*>& out) {
for (auto it = children_.rbegin(); it != children_.rend(); ++it) (*it)->hits(p, out);
}
// ---- fluent modifiers ----
ViewPtr View::padding(float all) { return std::make_shared<PaddingView>(self(), Insets::all(all)); }
ViewPtr View::padding(const Insets& i) { return std::make_shared<PaddingView>(self(), i); }
ViewPtr View::paddingH(float h) { return padding(Insets{0, h, 0, h}); }
ViewPtr View::paddingV(float v) { return padding(Insets{v, 0, v, 0}); }
ViewPtr View::paddingTop(float v) { return padding(Insets{v, 0, 0, 0}); }
ViewPtr View::paddingBottom(float v) { return padding(Insets{0, 0, v, 0}); }
ViewPtr View::paddingLeading(float v) { return padding(Insets{0, v, 0, 0}); }
ViewPtr View::paddingTrailing(float v) { return padding(Insets{0, 0, 0, v}); }
ViewPtr View::frame(std::optional<float> w, std::optional<float> h, Alignment a) {
FrameSpec s;
s.minW = s.idealW = s.maxW = w;
s.minH = s.idealH = s.maxH = h;
s.align = a;
return std::make_shared<FrameView>(self(), s);
}
ViewPtr View::frameFlex(std::optional<float> minW, std::optional<float> idealW, std::optional<float> maxW,
std::optional<float> minH, std::optional<float> idealH, std::optional<float> maxH, Alignment a) {
return std::make_shared<FrameView>(self(), FrameSpec{minW, idealW, maxW, minH, idealH, maxH, a});
}
ViewPtr View::maxWidth(Alignment a) {
return std::make_shared<FrameView>(self(), FrameSpec{std::nullopt, std::nullopt, kInf, std::nullopt, std::nullopt, std::nullopt, a});
}
ViewPtr View::maxHeight(Alignment a) {
return std::make_shared<FrameView>(self(), FrameSpec{std::nullopt, std::nullopt, std::nullopt, std::nullopt, std::nullopt, kInf, a});
}
ViewPtr View::maxSize(Alignment a) {
return std::make_shared<FrameView>(self(), FrameSpec{std::nullopt, std::nullopt, kInf, std::nullopt, std::nullopt, kInf, a});
}
ViewPtr View::fixedSize(bool h, bool v) { return std::make_shared<FixedSizeView>(self(), h, v); }
ViewPtr View::layoutPriority(float p) { return std::make_shared<PriorityView>(self(), p); }
ViewPtr View::background(Color c, const Corners& r) { return std::make_shared<BackgroundView>(self(), c, r); }
ViewPtr View::background(ViewPtr v, Alignment a) { return std::make_shared<BackgroundView>(self(), std::move(v), a); }
ViewPtr View::overlay(ViewPtr v, Alignment a) { return std::make_shared<OverlayView>(self(), std::move(v), a); }
ViewPtr View::roundedBackground(Color c, float radius) { return background(c, Corners::all(radius)); }
ViewPtr View::border(Color c, float radius, float width, float opacity) {
return std::make_shared<BorderView>(self(), c, radius, width, opacity);
}
ViewPtr View::clipRounded(float radius) { return std::make_shared<ClipView>(self(), Corners::all(radius), false); }
ViewPtr View::clipCircle() { return std::make_shared<ClipView>(self(), Corners{}, true); }
ViewPtr View::clipRect() { return std::make_shared<ClipView>(self(), Corners{}, false); }
ViewPtr View::opacity(float f) { return std::make_shared<OpacityView>(self(), f); }
ViewPtr View::offset(float dx, float dy) { return std::make_shared<OffsetView>(self(), dx, dy); }
ViewPtr View::offsetFraction(float fx, float fy) { return std::make_shared<OffsetFractionView>(self(), fx, fy); }
ViewPtr View::scale(float s) { return std::make_shared<ScaleView>(self(), s); }
ViewPtr View::shadow(Color c, float blur, float dx, float dy, const Corners& shape) {
return std::make_shared<ShadowView>(self(), c, blur, dx, dy, shape);
}
ViewPtr View::font(const FontSpec& f) {
return std::make_shared<EnvView>(self(), [f](EnvValues& e) { e.font = f; });
}
ViewPtr View::bold() {
return std::make_shared<EnvView>(self(), [](EnvValues& e) {
FontSpec f = e.font.value_or(FontSpec::body());
f.weight = Weight::Bold;
e.font = f;
});
}
ViewPtr View::weight(Weight w) {
return std::make_shared<EnvView>(self(), [w](EnvValues& e) {
FontSpec f = e.font.value_or(FontSpec::body());
f.weight = w;
e.font = f;
});
}
ViewPtr View::foreground(Color c) {
return std::make_shared<EnvView>(self(), [c](EnvValues& e) { e.foreground = c; });
}
ViewPtr View::lineLimit(int n) {
return std::make_shared<EnvView>(self(), [n](EnvValues& e) { e.lineLimit = n; });
}
ViewPtr View::multilineAlign(TextAlign a) {
return std::make_shared<EnvView>(self(), [a](EnvValues& e) { e.multilineAlign = a; });
}
ViewPtr View::lineSpacing(float v) {
return std::make_shared<EnvView>(self(), [v](EnvValues& e) { e.lineSpacing = v; });
}
ViewPtr View::disabled(bool v) {
return std::make_shared<EnvView>(self(), [v](EnvValues& e) { e.disabled = e.disabled || v; });
}
ViewPtr View::id(const std::string& key) { return std::make_shared<KeyView>(self(), key); }
ViewPtr View::onTap(std::function<void()> fn) { return std::make_shared<TapView>(self(), std::move(fn), nullptr); }
ViewPtr View::onLongPress(std::function<void()> fn) { return std::make_shared<TapView>(self(), nullptr, std::move(fn)); }
ViewPtr View::contentShape(bool) { return self(); }
} // namespace dp::ui