Skip to content
Repos

DevPlaceApp

C++ 2.0 KB46 lines Opens in site

Directory Raw Download

#pragma once
// Spec: INV-social 10 (DevPlaceTextEditor). Multi-line editor in a BoxView(BG_1, border 0.5, radius 14, no padding)
// with a placeholder, a resizable height (drag the bottom 24 pt band, grip drawn bottom trailing) or fill mode.
//
//   ViewPtr DevPlaceTextEditor(ui::Binding text, DevPlaceTextEditorOptions opts);
//
// - `handle` (ui::TextInputHandle) gives the caret (byte offset), focus() and insertAtCaret() to the caller.
// - `stateKey` names the editor so its height survives rebuilds (default "dp-text-editor"); give distinct keys when
//   one screen shows several editors.
// - sizeMode Resizable: height = initialHeight, else initialLineCount lines plus 20 pt, else one line plus 20 pt;
//   the user drag changes it (min one line plus 20 pt, cap spec::ComposeSpec::kEditorMaxHeight).
// - sizeMode Fill: takes all proposed width and height.
// DIFF: animatesHeightChanges is not supported (the height never animates).
#include <functional>
#include <memory>
#include <optional>
#include <string>

#include "ui/TextInput.hpp"

namespace dp {

struct DevPlaceTextEditorOptions {
  enum class SizeMode { Resizable, Fill };
  std::string placeholder;
  SizeMode sizeMode = SizeMode::Resizable;
  int initialLineCount = 0;                  // 0 = none
  std::optional<float> initialHeight;
  std::optional<ui::Color> background;       // default BG_1
  std::shared_ptr<ui::TextInputHandle> handle;
  std::function<void(const std::string&)> onChange;
  std::function<void(int caret)> onCaretChange;
  std::string stateKey = "dp-text-editor";
  std::size_t maxChars = 0;                  // 0 = global edit cap
};

ui::ViewPtr DevPlaceTextEditor(ui::Binding text, DevPlaceTextEditorOptions opts = {});

// Pure helpers (unit tested).
struct DevPlaceTextEditorMetrics {
  static float singleLineHeight();                         // body line height + 2 * measure inset
  static float initialHeight(const DevPlaceTextEditorOptions& opts);
  static float clampHeight(float height);                  // [singleLineHeight, kEditorMaxHeight]
};

}  // namespace dp
42.4ms