// SPDX-License-Identifier: GPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® /* ImsdMessages.Ui:Compose — the message field on the canvas. The library's InputField is one line and erases single bytes, which breaks UTF-8; a text message needs neither limit, so the field's state and its editing are the core's (ImsdMessages:Edit). Here the keys become its EditKeys, and the draw function lays the field out with the canvas's word wrapper and reports where the text sits, so a tap can place the cursor. */ module; #include "vulkan/vulkan.h" export module ImsdMessages.Ui:Compose; import std; import Crafter.Graphics; import ImsdMessages; import :Canvas; export namespace ImsdMessages::Ui { // Backspace, Delete, Left, Right, Home, End and Enter (a newline), as the // core's edits; any other key is ignored (false). bool ComposeKey(ComposeState& c, Crafter::KeyCode code) { using Crafter::CrafterKeys; using Crafter::Key; if (code == Key(CrafterKeys::Backspace)) return ComposeEdit(c, EditKey::Backspace); if (code == Key(CrafterKeys::Delete)) return ComposeEdit(c, EditKey::Delete); if (code == Key(CrafterKeys::Left)) return ComposeEdit(c, EditKey::Left); if (code == Key(CrafterKeys::Right)) return ComposeEdit(c, EditKey::Right); if (code == Key(CrafterKeys::Home)) return ComposeEdit(c, EditKey::Home); if (code == Key(CrafterKeys::End)) return ComposeEdit(c, EditKey::End); if (code == Key(CrafterKeys::Enter) || code == Key(CrafterKeys::keypad_enter)) return ComposeEdit(c, EditKey::Newline); return false; } // The field grows to this many lines, then scrolls to keep the caret's // line in view. constexpr std::size_t composeMaxLines = 5; // Where the field's text sits, for the caret and for taps. struct ComposeLayout { Box text; // the text area inside the field std::vector lines; float lineHeight = 0; float px = 0; std::size_t first = 0; // the first line drawn: a longer text shows the lines around the caret }; // The lines the field's box shows: as many as it is tall, at least one. std::size_t ComposeShownLines(const ComposeLayout& layout) { return std::max(1, static_cast(std::lround(layout.text.h / layout.lineHeight))); } // The field's text: one to five lines tall, a placeholder when empty, the // caret at the cursor; the box itself is the caller's, whose screen // decides its fill and outline. Returns the layout the app needs for taps. ComposeLayout DrawCompose(Canvas& c, const ComposeState& s, Box field, bool caretVisible, std::string_view placeholder = "Message") { const float px = c.Dp(16); const float padX = c.Dp(12); const float padY = c.Dp(8); ComposeLayout layout; layout.px = px; layout.lineHeight = c.LineHeight(px); layout.text = { field.x + padX, field.y + padY, field.w - 2 * padX, field.h - 2 * padY }; if (s.value.empty()) { c.chrome.Text(placeholder, layout.text.x, layout.text.y + c.Ascent(px), c.Font(), px, c.theme.muted); layout.lines = { { 0, 0, 0 } }; } else { layout.lines = c.Wrapper().Wrap(s.value, px, layout.text.w); } // The caret's line: the last line that starts at or before the // cursor; a cursor right after a break belongs to the next line. std::size_t li = 0; for (std::size_t i = 0; i < layout.lines.size(); ++i) { if (layout.lines[i].begin <= s.cursor) li = i; } // The field holds as many lines as its box is tall (the bar grows it // to composeMaxLines; a one-line box shows one): a longer text shows // the lines from `first`, chosen so the caret's line is among them. const std::size_t shownLines = ComposeShownLines(layout); layout.first = ComposeFirstLine(li, shownLines); const std::size_t last = std::min(layout.lines.size(), layout.first + shownLines); float baseline = layout.text.y + c.Ascent(px); for (std::size_t i = layout.first; i < last; ++i) { const WrappedLine& l = layout.lines[i]; if (!s.value.empty()) c.chrome.Text(std::string_view(s.value).substr(l.begin, l.end - l.begin), layout.text.x, baseline, c.Font(), px, c.theme.text); baseline += layout.lineHeight; } if (s.focused && caretVisible) { const WrappedLine& l = layout.lines[li]; const std::size_t end = std::min(s.cursor, l.end); const float x = layout.text.x + (end > l.begin ? c.TextWidth(std::string_view(s.value).substr(l.begin, end - l.begin), px) : 0.0f); const float y = layout.text.y + layout.lineHeight * static_cast(li - layout.first); c.chrome.Quad({ x, y + c.Dp(2), c.Dp(2), layout.lineHeight - c.Dp(4) }, c.theme.accent); } return layout; } // The number of lines the field needs for its text, one to five. std::size_t ComposeLines(Canvas& c, const ComposeState& s, float textWidth) { if (s.value.empty()) return 1; return std::clamp(c.Wrapper().Wrap(s.value, c.Dp(16), textWidth).size(), 1, composeMaxLines); } // The byte offset in the text a tap at p asks for (App's Tap makes it // the cursor). The lines are the text's as it is now: an edit since the // frame that laid the field out only asked for a frame, so the layout's // lines may describe text that is gone. std::size_t ComposeHit(Canvas& c, const ComposeState& s, const ComposeLayout& layout, Point p) { if (s.value.empty()) return 0; if (layout.lineHeight <= 0) return s.value.size(); // no frame has laid the field out (a view just opened): the text's end const std::vector& lines = c.Wrapper().Wrap(s.value, layout.px, layout.text.w); if (lines.empty()) return 0; std::size_t li = layout.first + static_cast(std::max(0.0f, (p.y - layout.text.y) / layout.lineHeight)); li = std::min(li, layout.first + ComposeShownLines(layout) - 1); // the drawn lines only: the padding below belongs to the last li = std::min(li, lines.size() - 1); const WrappedLine& l = lines[li]; const std::string_view line = std::string_view(s.value).substr(l.begin, l.end - l.begin); const std::size_t byte = c.NearestByte(line, layout.px, p.x - layout.text.x); // At or past the line's end: the text's end on the last line, the // newline after a hung space before a hard break (Edit's ComposeLineEnd). if (byte == line.size()) return ComposeLineEnd(lines, li, s.value); return l.begin + byte; } }