Skip to content
Repos

DevPlaceApp

C++ 2.5 KB66 lines Opens in site

Directory Raw Download

#pragma once
// Spec: INV-posts 5.3 (CommentEditorView). Mention suggestions above a text editor (BG_0), a counter (min 3, max
// 125000 UTF-16 units), the uploader and the cancel and submit icons. One object per open composer; the owner
// calls view() every frame.
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <vector>

#include "model/UploadResponse.hpp"
#include "model/UserSearch.hpp"
#include "screens/components/AttachmentUploader.hpp"
#include "screens/components/MentionSuggestions.hpp"
#include "ui/TextInput.hpp"
#include "ui/Views.hpp"

namespace dp {

struct CommentEditorProps {
  ui::Binding text;
  std::string placeholder;  // empty = PostsText::kReplyPlaceholder
  std::optional<int> initialLineCount;
  std::optional<float> initialHeight;
  bool focusOnAppear = false;
  // Attachments are offered only when onAttachmentsChange is set (replies, not edits).
  std::vector<UploadResponse> attachments;
  std::function<void(const std::vector<UploadResponse>&)> onAttachmentsChange;
  std::vector<UserSearch::Result> mentionParticipants;
  std::function<void()> onCancel;
  std::function<void(const std::string&)> onSubmit;
  std::string anchorKey;  // AnchorMark key of the composer (scroll anchor), empty = unmarked
};

class CommentEditor {
 public:
  // `key` makes the text editor state (height) unique among the visible composers.
  explicit CommentEditor(std::string key);
  ~CommentEditor();
  CommentEditor(const CommentEditor&) = delete;
  CommentEditor& operator=(const CommentEditor&) = delete;

  ui::ViewPtr view(const CommentEditorProps& props);
  // Height of the mention suggestion list (0 while hidden); the screen re-scrolls the composer when it changes.
  float mentionListHeight() const { return mentions_.listHeight(); }

  // 3 <= UTF-16 units <= 125000 (CommentEditorView.swift:104-108).
  static bool canSubmit(const std::string& text);
  // Scroll anchor id of the composer (CommentEditorView.scrollAnchorName).
  static const char* scrollAnchorName() { return "commentComposerAnchor"; }

 private:
  void syncUploaders(const CommentEditorProps& props);
  void scheduleFocus();

  std::string key_;
  std::shared_ptr<ui::TextInputHandle> handle_ = std::make_shared<ui::TextInputHandle>();
  MentionSuggestions mentions_;
  std::unique_ptr<AttachmentUploader> small_;
  std::unique_ptr<AttachmentUploader> full_;
  std::vector<std::unique_ptr<AttachmentUploader>> retired_;
  bool focusScheduled_ = false;
  std::shared_ptr<bool> alive_ = std::make_shared<bool>(true);
};

}  // namespace dp
40.9ms