imsd-messages/interfaces/ImsdMessages.Ui-Compose.cppm
Jorijn van der Graaf 20e687452f
Ui, main: the new-conversation screen and the long-press sheets
The + opens a screen with a number field (its placeholder "Number", a
parameter of the compose field now) and Start; the number becomes a
conversation key through Number's ConversationKey, as an sms: link's
does (E.164 through the country code, a short code as digits), anything
else is refused in place. A long press on a conversation row offers to
delete the conversation, on a message to copy its text to the clipboard
or delete it; only those two have one: App tells the recogniser before
each press whether what it lands on has a long press (GestureDetector's
longPress), and without one a press held as long — on a button, a field,
a sheet's row — stays a tap at its release, so a slow finger presses the
button all the same. A sheet draws on the overlay layer over a scrim and
pushes its regions last, so they win the hit test, and the wheel scrolls
nothing under it; Escape, the back arrow and a tap outside dismiss it.
Deleting a conversation also closes its notification and drops its
draft; the + keeps the draft of the conversation it leaves, as Back
does, and so does a view that closes because its last message was
deleted. The deletes go through Sync (Erase, EraseConversation: the
commit before, which keeps a deleted text deleted). The number field's
box is drawn by its screen, as the send bar's is by the bar: the shared
field drawing paints no background of its own. Opening a conversation,
from a notification or a link, leaves the new-conversation screen and
any sheet, and the + leaves any sheet. A key whose press changed the
screen acts once: held, its repeats are not the new screen's — Enter,
which started the conversation, would type line breaks into its field,
and Escape would go on back, from a sheet through the conversation and
its draft. A sheet that asks to delete a conversation goes when a text
arrives for it: a yes would delete that text unread. A press on the
conversation's row opens no such sheet either while the row it aimed at
had not shown the text — the regions are a frame's: the press that is
down when the text arrives, and one that comes down before the next
frame is drawn, have no long press; a release before the long-press time
is still the row's tap, while the row stays under the finger.

--new and --bench-sheet put either on screen for the bench
(--bench-sheet conversation or message, each on the screen a long press
opens it over: another value is refused, and one that finds nothing to
press says so in the log), and are window flags as --screenshot is.

Tested: the screens and the sheets by the bench (--new, --bench-sheet)
and the phone step's screenshots; the deletes' logic with the commit
before, in tests/Store and tests/Sync; the press without a long press in
tests/Gesture (the timer gives no long press and a move within the slop
nothing; the release is the tap, however late; a slide off the control
is a drag); a deleted conversation's draft dropped, in tests/Edit. The
press on a row a text arrived for has no bench: none holds a finger on a
row.
2026-11-18 01:12:27 +01:00

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'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<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;
}
}