Skip to content
Repos

DevPlaceApp

C++ 1.9 KB44 lines Opens in site

Directory Raw Download

#include "screens/components/VoteView.hpp"

#include "spec/PostsSpec.hpp"
#include "screens/components/VoteViewModel.hpp"
#include "screens/post/Accessible.hpp"
#include "ui/Palette.hpp"

namespace dp {

using namespace ui;
using spec::PostsLayout;

namespace {

// SF Symbols minus.circle / plus.circle and their .fill variants. The filled variant is a solid disc with the glyph
// (bars of 1.7 pt, half the disc wide) knocked out in the card background.
ViewPtr voteGlyph(bool plus, bool filled, Color color) {
  const float size = PostsLayout::kVoteIcon;
  if (!filled) return Icon(plus ? "plus.circle" : "minus.circle", size)->foreground(color);
  const float bar = size * 0.5f, thick = 1.7f;
  std::vector<ViewPtr> layers{CircleShape(color)->frame(size, size), RectangleShape(Palette::bg1())->frame(bar, thick)};
  if (plus) layers.push_back(RectangleShape(Palette::bg1())->frame(thick, bar));
  return ZStack(layers)->frame(size, size);
}

ViewPtr voteButton(bool plus, bool active, bool enabled, std::function<void()> action, const std::string& key) {
  const Color color = active ? Palette::accent() : Palette::fg2();
  ViewPtr button = Button(voteGlyph(plus, active, color), std::move(action), ButtonStyles::plain())->id(key);
  return enabled ? button : button->disabled();
}

}  // namespace

ViewPtr VoteView(TargetType type, const std::string& targetId, std::int64_t count, Vote currentVote) {
  auto model = VoteViewModel::shared(type, targetId, count, currentVote);
  const bool can = model->userCanVote();
  const Vote vote = model->currentVote();
  ViewPtr down = voteButton(false, vote == Vote::Down, can, [model] { model->voteDown(); }, "vote-down");
  ViewPtr up = voteButton(true, vote == Vote::Up, can, [model] { model->voteUp(); }, "vote-up");
  ViewPtr text = Text(std::to_string(model->count()))->font(FontSpec::system(PostsLayout::kVoteCountFont, Weight::Semibold));
  return HStack({down, text, up}, PostsLayout::kVoteSpacing);
}

}  // namespace dp
32.0ms