#include "screens/reactions/ReactionRowView.hpp" #include namespace dp { using namespace ui; ReactionRowView::ReactionRowView(std::vector chips, ViewPtr more, float spacing) : spacing_(spacing) { for (ViewPtr& c : chips) addChild(std::move(c)); addChild(std::move(more)); } ReactionRowView::Plan ReactionRowView::plan(const std::vector& chipSizes, Size moreSize, float spacing, float available) { Plan out; out.sizes = chipSizes; out.sizes.push_back(moreSize); const std::size_t chipCount = chipSizes.size(); float height = moreSize.h; for (const Size& s : chipSizes) height = std::max(height, s.h); out.visible.assign(chipCount + 1, false); if (chipCount == 0) { out.visible[0] = true; out.size = {moreSize.w, height}; return out; } float all = spacing * static_cast(chipCount - 1); for (const Size& s : chipSizes) all += s.w; if (all <= available) { for (std::size_t i = 0; i < chipCount; ++i) out.visible[i] = true; out.size = {all, height}; return out; } std::size_t fitting = 0; float used = 0; for (std::size_t i = 0; i < chipCount; ++i) { const float candidate = used + (i > 0 ? spacing : 0) + chipSizes[i].w; if (candidate + spacing + moreSize.w <= available) { used = candidate; fitting = i + 1; } else { break; } } for (std::size_t i = 0; i < fitting; ++i) out.visible[i] = true; out.visible[chipCount] = true; out.size = {used + (fitting > 0 ? spacing : 0) + moreSize.w, height}; return out; } ReactionRowView::Plan ReactionRowView::planFor(const Proposal& p) { std::vector sizes; for (std::size_t i = 0; i + 1 < children_.size(); ++i) sizes.push_back(children_[i]->measure(Proposal::ideal())); const Size more = children_.back()->measure(Proposal::ideal()); return plan(sizes, more, spacing_, p.w.value_or(kInf)); } Size ReactionRowView::compute(const Proposal& p) { return planFor(p).size; } void ReactionRowView::place(const Rect& r) { frame_ = r; const Plan pl = planFor(Proposal{r.w, r.h}); visible_ = pl.visible; visibleChips_ = 0; float x = r.x; for (std::size_t i = 0; i < children_.size(); ++i) { const Size s = pl.sizes[i]; if (pl.visible[i]) { children_[i]->place({x, r.y + (pl.size.h - s.h) / 2, s.w, s.h}); x += s.w + spacing_; if (i + 1 < children_.size()) ++visibleChips_; } else { children_[i]->place({-100000.0f, r.y, s.w, s.h}); } } } void ReactionRowView::draw(Canvas& c) { for (std::size_t i = 0; i < children_.size(); ++i) { if (i < visible_.size() && visible_[i]) children_[i]->draw(c); } } void ReactionRowView::hits(Point p, std::vector& out) { for (std::size_t i = children_.size(); i > 0; --i) { if (i - 1 < visible_.size() && visible_[i - 1]) children_[i - 1]->hits(p, out); } } } // namespace dp