A text message needs several lines and UTF-8, and the library's InputField is one line edited by byte, so the field is the app's own. Its state and editing are the core's (ImsdMessages:Edit, tested without a window): a plain struct, insertion at the cursor, Backspace and Delete by code point, Home and End within the line, a newline. Ui:Compose turns the keys into those edits and draws the field one to five lines tall — as many as half the area under the header and the banner holds, and one at the least — with the canvas's word wrapper (a longer text shows the lines that end at the caret's, or the first ones while the caret is among them), a placeholder when empty, the caret at the cursor, and a tap placing the cursor at the nearest byte by the widths the glyphs are drawn at, in the text as it is when the tap lands (an edit since the last frame only asked for one; a field no frame has laid out yet takes a tap as one at its text's end; a tap at or past the last line's end asks for the text's end, where a space the wrapper hung off the line may be). The bar under a conversation holds the field and Send, enabled only with text, a sendable status and an address to send to; Send hands the text to Sync with that address and clears the field (a store that refuses the text keeps the draft) (Model's SendAddress: as a link gave it, else the latest message's — the network's form of a received text, the dialled form of ours — never the key, whose completed country code is a guess that groups; a conversation with neither has Send off, and so has one whose address is not international — a national number, a short code; typed, linked or the message's — and was typed or written under another SIM's plan than the one known now, which the plan each row and the view carry tells (the view's is that of the SIM the address was typed under, which Number's AfterStatus keeps; Number's AnotherPlan, its second caller)); Sync's StateChanged moves the bubble's caption. Keys reach the field while a conversation is open (Escape goes back, Ctrl+Enter sends), a held key repeats through a timer since frames are on demand, and the caret blinks on one. A text typed and not sent is its conversation's, not the view's: leaving the conversation (Back, another one opened by a notification or a link) keeps it by the conversation's key for the run, and opening the conversation again takes it up (Edit's Drafts: an empty field keeps nothing and drops nothing; the draft of a typed or linked address follows imsd's status as the open conversation's key does — Number's AfterStatus over the address and the plan kept with it — so one left before the status brought the country code is found under the key the address makes afterwards; and a second text kept under one key — two keys that became one — goes after the first on a line of its own). Tested: tests/Model, the address a text goes to (the typed or linked one, the latest message's, none for an emptied conversation, none under another SIM's plan than it was typed or written under — a national number with or without a trunk prefix under another country's SIM, a short code under another operator's too, though dialled under the same operator, the own number read when it was typed or not; a national number typed before the own number was read, none under another operator — an international one under any — and a typed address judged by its own plan, whatever the latest row's); tests/Edit (code points of one to four bytes, the moves within a line, the edits that change nothing, the first line shown as the caret moves down a long text; the drafts: kept and taken up by key with the cursor, taken once, an empty field keeping and dropping nothing, one per conversation, a second text under one key on a line of its own; a typed address's following the status — moved when the code comes, not while texts are stored under its key, not under another SIM's status, a list conversation's left alone and the store not asked, the learned plan kept, two keys that became one keeping both texts).
125 lines
6.7 KiB
C++
125 lines
6.7 KiB
C++
// 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<WrappedLine> 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<std::size_t>(1, static_cast<std::size_t>(std::lround(layout.text.h / layout.lineHeight)));
|
|
}
|
|
|
|
// The field: rounded, one to five lines tall, a placeholder when empty,
|
|
// the caret at the cursor. Returns the layout the app needs for taps.
|
|
ComposeLayout DrawCompose(Canvas& c, const ComposeState& s, Box field, bool caretVisible) {
|
|
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 };
|
|
c.chrome.Quad(field, c.theme.bg, c.Dp(18));
|
|
if (s.value.empty()) {
|
|
c.chrome.Text("Message", 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<float>(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<std::size_t>(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<WrappedLine>& 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::size_t>(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;
|
|
}
|
|
}
|