With the Crafter.Graphics branch delivering touch (the first finger as the pointer), zwp_text_input_v3 and xdg app ids, the app: - sets its app id on the window; - tells the compositor which field has the focus and where its caret is, after every frame that changed it: the number field at once (purpose phone), the message field on a tap or a key; a sheet or leaving the screen takes it back; - shows the input method's composing text in the accent colour at the cursor, each run placed at the advance the one before it was drawn with, applies its deletions by code point and leaves the composing text to the input method, and takes its committed text as typed (the composing text and its deletions are the core's Edit, in tests/Edit; a draft kept for a conversation that is left keeps a word still being composed as text); a tap inside the composing text leaves the cursor where it is, a tap elsewhere in the field commits the word where it was typed and resets the input method, as Send does, so the half-typed word does not move to the tap; a sheet, a hide and leaving a screen by Open (over the number screen or over another conversation), by Back or by the + end the composition the same way and at once (EndComposition: the word committed, the input method told), since the input method starts afresh after, and left on until the next frame it would send its next commit to the screen that opens or have it dropped. The message field starts unfocused, as on a phone, and the caret blinks only in a focused field; - sends what the field shows: Send, like Start, reads the shown text once, the composing word included, and clears the field only once the store took it (a field of nothing but spaces and line breaks is cleared with nothing sent); the field keeps the focus while the input method is reset (disabled, enabled again the next frame) — a refused text leaves the field and the composing word as they are; Start reads the shown text once — the key and the address dialled come from it — so a refused number leaves the composing word to the input method; - logs the window's size on every resize, and which conversation was opened — under --headless; a plain start names no correspondent in its log, as the rest of the app does not. Touch takes the mouse's gesture path, and a touch the compositor cancels (a gesture of its own) arrives as a release without the finger's touch-up: it is fed to the gestures as a cancel, so it taps nothing. Tested: tests/Edit (the composing text kept by a deletion, and put into the field's text by ComposeCommitPreedit).
139 lines
7.7 KiB
C++
139 lines
7.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, and where the
|
|
caret is, for the input method.
|
|
*/
|
|
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
|
|
Box caret; // where the caret is, drawn or not: the input method's cursor rectangle
|
|
};
|
|
|
|
// 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)));
|
|
}
|
|
|
|
// Draws the field's text and lays it out; the box itself is the caller's,
|
|
// whose screen decides its fill and outline. The layout reports where the
|
|
// text sits (for a tap) and the caret (for the input method), drawn or not.
|
|
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 };
|
|
const std::string shown = ComposeShown(s);
|
|
const std::size_t cursor = ShownCursor(s);
|
|
const std::size_t preeditBegin = s.cursor;
|
|
const std::size_t preeditEnd = s.cursor + s.preedit.size();
|
|
if (shown.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(shown, 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 <= 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];
|
|
// Up to three runs: before, inside and after the composing text,
|
|
// which shows in the accent colour until the input method commits it.
|
|
float x = layout.text.x;
|
|
const std::size_t bounds[4] = { l.begin, std::clamp(preeditBegin, l.begin, l.end), std::clamp(preeditEnd, l.begin, l.end), l.end };
|
|
for (std::int32_t run = 0; run < 3; ++run) {
|
|
if (bounds[run + 1] <= bounds[run]) continue;
|
|
const std::string_view part = std::string_view(shown).substr(bounds[run], bounds[run + 1] - bounds[run]);
|
|
x += c.chrome.Text(part, x, baseline, c.Font(), px, run == 1 ? c.theme.accent : c.theme.text);
|
|
}
|
|
baseline += layout.lineHeight;
|
|
}
|
|
const WrappedLine& l = layout.lines[li];
|
|
const std::size_t end = std::min(cursor, l.end);
|
|
const float cx = layout.text.x + (end > l.begin ? c.TextWidth(std::string_view(shown).substr(l.begin, end - l.begin), px) : 0.0f);
|
|
const float cy = layout.text.y + layout.lineHeight * static_cast<float>(li - layout.first);
|
|
layout.caret = { cx, cy + c.Dp(2), c.Dp(2), layout.lineHeight - c.Dp(4) };
|
|
if (s.focused && caretVisible) c.chrome.Quad(layout.caret, c.theme.accent);
|
|
return layout;
|
|
}
|
|
|
|
// How many 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() && s.preedit.empty()) return 1;
|
|
return std::clamp<std::size_t>(c.Wrapper().Wrap(ComposeShown(s), c.Dp(16), textWidth).size(), 1, composeMaxLines);
|
|
}
|
|
|
|
// The byte offset in the shown text a tap at p asks for (App::TapField
|
|
// 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) {
|
|
const std::string shown = ComposeShown(s);
|
|
if (shown.empty()) return 0;
|
|
if (layout.lineHeight <= 0) return shown.size(); // no frame has laid the field out (a view just opened): the text's end
|
|
const std::vector<WrappedLine>& lines = c.Wrapper().Wrap(shown, 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(shown).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, shown);
|
|
return l.begin + byte;
|
|
}
|
|
}
|