#include "screens/comments/CommentEditor.hpp" #include "core/TextCount.hpp" #include "core/Timer.hpp" #include "screens/components/DevPlaceTextEditor.hpp" #include "screens/post/AnchorMark.hpp" #include "screens/post/ActionIconButton.hpp" #include "spec/ModelLimits.hpp" #include "spec/PostsSpec.hpp" #include "ui/Controls.hpp" #include "ui/Palette.hpp" namespace dp { using namespace ui; using spec::PostsLayout; using spec::PostsText; CommentEditor::CommentEditor(std::string key) : key_(std::move(key)) {} CommentEditor::~CommentEditor() { *alive_ = false; } bool CommentEditor::canSubmit(const std::string& text) { const auto count = static_cast(TextCount::utf16(text)); return count >= spec::ModelLimits::kMinCommentContentLength && count <= spec::ModelLimits::kMaxCommentContentLength; } void CommentEditor::scheduleFocus() { if (focusScheduled_) return; focusScheduled_ = true; auto alive = alive_; auto handle = handle_; Timer::after(static_cast(spec::PostsTiming::kFocusDelayMs), [alive, handle] { if (*alive) handle->focus(); }); } namespace { bool sameIds(const std::vector& a, const std::vector& b) { if (a.size() != b.size()) return false; for (std::size_t i = 0; i < a.size(); ++i) { if (a[i].id != b[i].id) return false; } return true; } } // namespace // Small uploader while the list is empty, full uploader below the editor once it has entries. An uploader that // reports a change may be replaced inside its own callback, so replaced ones are parked until the next frame. void CommentEditor::syncUploaders(const CommentEditorProps& props) { retired_.clear(); if (!props.onAttachmentsChange) return; auto change = props.onAttachmentsChange; if (props.attachments.empty()) { if (full_) retired_.push_back(std::move(full_)); if (!small_) small_ = std::make_unique(AttachmentUploader::Options{true, {}, change}); return; } if (small_) retired_.push_back(std::move(small_)); if (!full_ || !sameIds(full_->attachments(), props.attachments)) { if (full_) retired_.push_back(std::move(full_)); full_ = std::make_unique(AttachmentUploader::Options{false, props.attachments, change}); } } ViewPtr CommentEditor::view(const CommentEditorProps& props) { syncUploaders(props); if (props.focusOnAppear) scheduleFocus(); const std::string text = props.text.get(); std::optional caret; if (handle_->id != 0) caret = handle_->caret(); mentions_.update(text, caret, props.mentionParticipants); Binding binding = props.text; auto handle = handle_; ViewPtr suggestions = mentions_.view([binding, handle](const UserSearch::Result& r) { MentionSuggestions::insertMention(*handle, binding, r.username); }); DevPlaceTextEditorOptions opts; opts.placeholder = props.placeholder.empty() ? PostsText::kReplyPlaceholder : props.placeholder; opts.initialLineCount = props.initialLineCount.value_or(0); opts.initialHeight = props.initialHeight; opts.background = Palette::bg0(); opts.handle = handle_; opts.stateKey = "comment-editor-" + key_; ViewPtr editor = DevPlaceTextEditor(binding, opts); const bool can = canSubmit(text); std::vector actions; if (small_) actions.push_back(small_->view()); auto cancel = props.onCancel; auto submit = props.onSubmit; actions.push_back(ActionIconButton("xmark", PostsText::kCancel, [cancel] { if (cancel) cancel(); }, Palette::fg2())); ViewPtr submitButton = ActionIconButton("checkmark", PostsText::kSubmit, [submit, binding] { if (submit) submit(binding.get()); }, Palette::accent()); actions.push_back(can ? submitButton : submitButton->disabled()); ViewPtr counter = CharacterCounter(TextCount::utf16(text), static_cast(spec::ModelLimits::kMaxCommentContentLength), static_cast(spec::ModelLimits::kMinCommentContentLength)) ->paddingH(PostsLayout::kEditorCounterPad); ViewPtr row = HStack({counter, Spacer(0), HStack(actions, PostsLayout::kFooterActionSpacing)->paddingH(PostsLayout::kEditorCounterPad)}, PostsLayout::kFooterSpacing, VAlign::Top); std::vector column{editor, row}; if (full_) column.push_back(full_->view()); ViewPtr result = VStack({suggestions, VStack(column, PostsLayout::kEditorSpacing, HAlign::Leading)->maxWidth(Alignment::leading())}, 0, HAlign::Leading) ->paddingBottom(PostsLayout::kEditorGap)->id(CommentEditor::scrollAnchorName()); return props.anchorKey.empty() ? result : AnchorMark(result, props.anchorKey); } } // namespace dp