#include "screens/comments/CommentView.hpp" #include #include "screens/comments/FadeInView.hpp" #include "screens/comments/MeasuredView.hpp" #include "screens/components/AttachmentViews.hpp" #include "screens/components/ReportButton.hpp" #include "screens/components/VoteView.hpp" #include "screens/post/ActionIconButton.hpp" #include "screens/post/AnchorMark.hpp" #include "screens/post/DoubleTapView.hpp" #include "screens/post/PostContentView.hpp" #include "screens/post/PostFooterView.hpp" #include "screens/post/PostHeaderView.hpp" #include "screens/reactions/ReactionsBar.hpp" #include "spec/PostsSpec.hpp" #include "ui/Palette.hpp" namespace dp { using namespace ui; using spec::PostsLayout; using spec::PostsText; int commentEffectiveLevel(int level) { return std::min(level, PostsLayout::kMaxIndentationLevel); } float commentDividerLeading(int level, std::optional previousLevel) { const int eff = commentEffectiveLevel(level); const int prev = previousLevel ? commentEffectiveLevel(*previousLevel) : eff; return PostsLayout::kIndentWidth + static_cast(std::max(eff, prev) - eff) * PostsLayout::kIndentWidth; } namespace { // The guide lines of all levels, drawn behind the row at its full height: VerticalLine shapes stroked with FG_2 at // 0.3, width 1, at the right edge of each 16 pt column; the deepest column (level 3) is dashed [3, 3] // (CommentView.swift:246-262). They size to the row instead of stretching it, so a row never grows past its content. class GuideLinesView : public View { public: explicit GuideLinesView(int levels) : levels_(levels) {} void draw(Canvas& c) override { const Color color = Palette::fg2(); const float opacity = PostsLayout::kGuideLineOpacity; for (int level = 1; level <= levels_; ++level) { const float x = frame_.x + static_cast(level) * PostsLayout::kIndentWidth - 1.0f; if (level != PostsLayout::kMaxIndentationLevel) { c.fillRect({x, frame_.y, 1, frame_.h}, color, opacity); continue; } for (float y = frame_.y; y < frame_.bottom(); y += 2 * PostsLayout::kGuideDash) { c.fillRect({x, y, 1, std::min(PostsLayout::kGuideDash, frame_.bottom() - y)}, color, opacity); } } } protected: Size compute(const Proposal& p) override { return {p.w.value_or(0), p.h.value_or(0)}; } private: int levels_; }; ViewPtr actionRow(const Comment& comment, const CommentRowInfo& row, const CommentsProps& p, bool isReplying, bool canEdit) { const CommentRef ref = CommentRef::from(comment); CommentsSession* session = p.session; std::vector actions; if (p.onReply && !isReplying) { auto onReply = p.onReply; actions.push_back(ActionIconButton("arrowshape.turn.up.left", PostsText::kReplyToComment, [onReply, ref] { onReply(ref); })); } actions.push_back(ReportButton(TargetType::Comment, comment.data.id, comment.data.userId)); if (canEdit) { actions.push_back(ActionIconButton("pencil", PostsText::kEditComment, [session, ref] { session->beginEditing(ref.prefixedId, ref.content); })); } if (row.own && p.onDelete) { auto onDelete = p.onDelete; actions.push_back(ActionIconButton("trash", PostsText::kDeleteComment, [onDelete, ref] { presentDeleteConfirmation(PostsText::kDeleteCommentQuestion, [onDelete, ref] { onDelete(ref); }); })); } return HStack(actions, PostsLayout::kFooterActionSpacing); } ViewPtr editorView(const Comment& comment, const CommentRowInfo& row, const CommentsProps& p) { CommentsSession* session = p.session; const std::string key = comment.id(); CommentsSession::RowState& state = session->row(key); if (auto quote = session->takePendingEditQuote()) state.editedText += *quote; CommentEditorProps props; props.text = Binding{[session, key] { return session->row(key).editedText; }, [session, key](const std::string& v) { session->row(key).editedText = v; }}; props.placeholder = PostsText::kEditPlaceholder; if (state.contentHeight > 0) props.initialHeight = state.contentHeight; props.mentionParticipants = p.mentionParticipants; props.anchorKey = p.composerAnchorKey; props.onCancel = [session, key] { session->endEditing(key); }; auto onEdit = p.onEdit; const CommentRef ref = CommentRef::from(comment); props.onSubmit = [session, key, onEdit, ref](const std::string& content) { if (onEdit) onEdit(ref, content); session->endEditing(key); }; (void)row; return FadeIn(session->editor("edit:" + key).view(props), "edit-" + key); } ViewPtr replyEditorView(const Comment& comment, const CommentsProps& p) { CommentsSession* session = p.session; const CommentRef ref = CommentRef::from(comment); CommentEditorProps props; props.text = p.replyText; props.initialLineCount = PostsLayout::kEditorInitialLines; props.focusOnAppear = true; props.anchorKey = p.composerAnchorKey; props.attachments = session->attachments(); props.onAttachmentsChange = p.onReplyAttachmentsChange; props.mentionParticipants = p.mentionParticipants; auto onCancel = p.onCancelReply; auto onSubmit = p.onSubmitReply; props.onCancel = [onCancel] { if (onCancel) onCancel(); }; props.onSubmit = [onSubmit, ref](const std::string& content) { if (onSubmit) onSubmit(ref, content); }; return FadeIn(session->editor("reply:" + comment.id()).view(props), "reply-" + comment.id() + "-" + std::to_string(session->epoch())); } } // namespace ViewPtr CommentView(const Comment& comment, const CommentRowInfo& row, const CommentsProps& p) { CommentsSession* session = p.session; const std::string key = comment.id(); const int eff = commentEffectiveLevel(row.level); const bool isReplying = p.replyingCommentId && *p.replyingCommentId == key; const bool isEditing = session->row(key).isEditing; const bool canEdit = row.own && static_cast(p.onEdit); const CommentRef ref = CommentRef::from(comment); // Gestures: double tap edits own comments, else votes; single tap only when idle (CommentView.swift:146-159). TapHandlers taps; auto onSingle = p.onSingleTap; auto onDouble = p.onDoubleTap; taps.doubleTap = [session, key, ref, canEdit, onDouble] { if (session->row(key).isEditing || session->isReplying(key)) return; if (canEdit) session->beginEditing(key, ref.content); else if (onDouble) onDouble(ref); }; if (onSingle) { taps.single = [session, key, ref, onSingle] { if (!session->row(key).isEditing && !session->isReplying(key)) onSingle(ref); }; } PostEnvironment env = p.env; env.taps = taps; PostHeaderOptions headerOptions; headerOptions.linksToProfile = p.linksToProfile; std::vector body; body.push_back(PostHeaderView(comment.author, comment.data.createdAt, headerOptions, env)); if (isEditing) { body.push_back(editorView(comment, row, p)); } else { ViewPtr content = PostContentView(std::nullopt, std::nullopt, comment.data.content, env); body.push_back(std::make_shared(content, [session, key](float h) { session->row(key).contentHeight = h; })); } const std::size_t shown = p.maxAttachments ? std::min(*p.maxAttachments, comment.attachments.size()) : comment.attachments.size(); for (std::size_t i = 0; i < shown; ++i) body.push_back(AttachmentViewer(comment.attachments[i])); auto onReact = p.onReact; std::function react = [onReact, ref](const std::string& emoji) { if (onReact) onReact(ref, emoji); }; std::vector footer{VoteView(TargetType::Comment, comment.data.id, comment.voteCount(), comment.myVote), ReactionsBar(comment.reactions, react), Spacer(0)}; if (!isEditing) footer.push_back(actionRow(comment, row, p, isReplying, canEdit)); body.push_back(HStack(footer, PostsLayout::kFooterSpacing)); if (isReplying && !isEditing) body.push_back(replyEditorView(comment, p)); ViewPtr commentBody = OnTaps(VStack(body, PostsLayout::kCardSpacing, HAlign::Leading)->maxWidth(Alignment::leading()) ->paddingH(PostsLayout::kScreenPadding)->paddingV(PostsLayout::kCommentVPad), taps); std::vector column; if (row.showsTopDivider) { column.push_back(Divider(Palette::separator(), 0.5f)->padding(Insets{0, commentDividerLeading(row.level, row.previousLevel), 0, PostsLayout::kIndentWidth})); } column.push_back(commentBody); ViewPtr content = VStack(column, 0, HAlign::Leading)->maxWidth(Alignment::leading()) ->paddingLeading(static_cast(eff) * PostsLayout::kIndentWidth); if (eff > 0) content = content->background(std::make_shared(eff), Alignment::topLeading()); ViewPtr rowView = content->foreground(Palette::fg1())->id(key); return p.rowAnchorPrefix.empty() ? rowView : AnchorMark(rowView, p.rowAnchorPrefix + key); } } // namespace dp