Skip to content
Repos

DevPlaceApp

C++ 1.6 KB49 lines Opens in site

Directory Raw Download

#pragma once
// Spec: SPEC.md UX-INPUT-01. Single-line TextField / SecureField and multi-line TextEditor with caret,
// selection, clipboard, word movement, IME commit text. Caps: spec kMaxEditChars.
#include <functional>
#include <memory>
#include <string>

#include "ui/Views.hpp"

namespace dp::ui {

// Screen-side handle: query/move the caret, focus the field. Valid after the first frame.
class TextInputHandle {
 public:
  int caret() const;                 // byte offset
  void setCaret(int byteOffset);
  void focus();
  bool focused() const;
  void insertAtCaret(const std::string& utf8);  // replaces the selection

  std::uint64_t id = 0;
  Binding* binding = nullptr;        // set by the view each frame
};

struct TextInputOptions {
  std::string placeholder;
  bool secure = false;
  std::optional<FontSpec> font;
  std::function<void()> onSubmit;                 // Return in single-line mode
  std::function<void(const std::string&)> onChange;
  std::function<void(int caret)> onCaretChange;
  std::shared_ptr<TextInputHandle> handle;
  int minLines = 1;                               // editor: grows between minLines and maxLines
  int maxLines = 6;
  bool disabled = false;
  std::size_t maxChars = 0;                       // 0 = global cap
};

ViewPtr TextField(Binding text, TextInputOptions options = {});
ViewPtr TextEditor(Binding text, TextInputOptions options = {});

// Clipboard (SDL3) with a size cap.
struct Clipboard {
  static std::string get();
  static void set(const std::string& utf8);
  static constexpr std::size_t kMaxBytes = 4u * 1024u * 1024u;
};

}  // namespace dp::ui
45.4ms