#include "screens/components/PollView.hpp"
#include <algorithm>
#include "screens/components/Box.hpp"
#include "screens/components/PollViewModel.hpp"
#include "spec/PostsSpec.hpp"
#include "ui/Palette.hpp"
namespace dp {
using namespace ui;
using spec::PostsLayout;
namespace {
// Rectangle of `pct` percent of the proposed width (GeometryReader plus Rectangle in PV.swift:84-91).
class PercentBarView : public View {
public:
PercentBarView(std::int64_t pct, Color color, float opacity) : pct_(std::clamp<std::int64_t>(pct, 0, 100)), color_(color), opacity_(opacity) {}
void draw(Canvas& c) override {
c.fillRect({frame_.x, frame_.y, frame_.w * static_cast<float>(pct_) / 100.0f, frame_.h}, color_, opacity_);
}
protected:
Size compute(const Proposal& p) override { return {p.w.value_or(0), p.h.value_or(0)}; }
private:
std::int64_t pct_;
Color color_;
float opacity_;
};
ViewPtr optionRow(const PollOption& option, bool selected, bool enabled, const std::shared_ptr<PollViewModel>& model) {
std::vector<ViewPtr> parts;
parts.push_back(Text(option.label)->font(FontSpec::body().with(selected ? Weight::Semibold : Weight::Regular))
->foreground(Palette::fg1())->maxWidth(Alignment::leading()));
if (selected) {
parts.push_back(Icon("checkmark", 13)->weight(Weight::Semibold)->foreground(Palette::accent()));
}
parts.push_back(Text(std::to_string(option.pct) + "%")->font(FontSpec::subheadline())->foreground(Palette::fg2()));
const float radius = PostsLayout::kPollOptionRadius;
const float fill = selected ? PostsLayout::kPollSelectedFill : PostsLayout::kPollUnselectedFill;
ViewPtr row = HStack(parts, PostsLayout::kPollRowSpacing)
->paddingH(PostsLayout::kPollOptionHPad)->paddingV(PostsLayout::kPollOptionVPad)
->background(std::make_shared<PercentBarView>(option.pct, Palette::accent(), fill), Alignment::leading())
->background(Palette::bg1())
->clipRounded(radius)
->border(selected ? Palette::accent() : Palette::fg2(), radius,
selected ? PostsLayout::kPollSelectedBorder : PostsLayout::kPollBorder,
selected ? 1.0f : PostsLayout::kPollBorderOpacity);
if (!enabled) return row;
const std::string id = option.id;
return Button(row, [model, id] { model->select(id); }, ButtonStyles::plain())->id("poll-option-" + option.id);
}
} // namespace
ViewPtr PollView(const Poll& poll) {
auto model = PollViewModel::shared(poll);
const bool can = model->userCanVote();
std::vector<ViewPtr> rows;
for (const PollOption& option : model->options()) {
const bool selected = model->myChoice() && *model->myChoice() == option.id;
rows.push_back(optionRow(option, selected, can, model));
}
ViewPtr content = VStack({Text(model->question())->font(FontSpec::headline())->foreground(Palette::fg1())->maxWidth(Alignment::leading()),
VStack(rows, PostsLayout::kPollOptionSpacing),
Text(model->votesLabel())->font(FontSpec::footnote())->foreground(Palette::fg2())
->paddingH(PostsLayout::kPollTotalHPad)->maxWidth(Alignment::leading())},
PostsLayout::kPollSpacing, HAlign::Leading);
return BoxView(content->maxWidth(Alignment::leading()));
}
} // namespace dp