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).
250 lines
14 KiB
C++
250 lines
14 KiB
C++
// SPDX-License-Identifier: GPL-3.0-only
|
||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
||
/*
|
||
ImsdMessages.Ui:Screens — the two screens of the baseline, drawn into the
|
||
canvas's layers: the conversation list and one conversation. A screen
|
||
lays out and draws in the same pass, records the regions a tap can hit,
|
||
and keeps its scroll state; the app decides what a hit means.
|
||
*/
|
||
module;
|
||
#include "vulkan/vulkan.h"
|
||
export module ImsdMessages.Ui:Screens;
|
||
import std;
|
||
import Crafter.Graphics;
|
||
import ImsdMessages;
|
||
import :Canvas;
|
||
import :Compose;
|
||
|
||
export namespace ImsdMessages::Ui {
|
||
// The local UTC offset, in minutes, at an instant.
|
||
using ZoneFn = std::function<std::int32_t(std::int64_t)>;
|
||
|
||
// Hit ids: the fixed controls, then the list's rows and the messages.
|
||
namespace Hit {
|
||
constexpr std::uint32_t fab = 1;
|
||
constexpr std::uint32_t back = 2;
|
||
constexpr std::uint32_t compose = 4;
|
||
constexpr std::uint32_t send = 5;
|
||
constexpr std::uint32_t rowBase = 1000;
|
||
constexpr std::uint32_t messageBase = 100000;
|
||
}
|
||
|
||
Box Intersect(Box a, Box b) {
|
||
const float x = std::max(a.x, b.x);
|
||
const float y = std::max(a.y, b.y);
|
||
const float r = std::min(a.Right(), b.Right());
|
||
const float bottom = std::min(a.Bottom(), b.Bottom());
|
||
if (r <= x || bottom <= y) return {};
|
||
return { x, y, r - x, bottom - y };
|
||
}
|
||
|
||
// The letter on an avatar: a number gets '#'.
|
||
std::string Initial(std::string_view name) {
|
||
if (name.empty()) return "?";
|
||
const char c = name.front();
|
||
if (c == '+' || (c >= '0' && c <= '9')) return "#";
|
||
std::size_t i = 0;
|
||
const std::uint32_t cp = DecodeUtf8(name, i);
|
||
std::string out(name.substr(0, i));
|
||
if (cp < 0x80) out[0] = static_cast<char>(std::toupper(static_cast<std::uint8_t>(out[0])));
|
||
return out;
|
||
}
|
||
|
||
std::string OneLine(std::string_view text) {
|
||
std::string out(text);
|
||
for (char& c : out) {
|
||
if (c == '\n' || c == '\r') c = ' ';
|
||
}
|
||
return out;
|
||
}
|
||
|
||
// The header: a title and, below the list, a back control. Returns the
|
||
// box under it.
|
||
Box DrawHeader(Canvas& c, std::string_view title, bool back, std::vector<HitRegion>& hits) {
|
||
const Box header{ 0, 0, c.Width(), c.Dp(56) };
|
||
c.chrome.Quad(header, c.theme.surface);
|
||
c.chrome.Quad({ 0, header.Bottom() - c.Dp(1), c.Width(), c.Dp(1) }, c.theme.divider);
|
||
float x = c.Dp(16);
|
||
if (back) {
|
||
const Box b{ 0, 0, c.Dp(56), c.Dp(56) };
|
||
c.chrome.Text("‹", c.Dp(24), c.CenteredBaseline(b, c.Dp(34)), c.Font(), c.Dp(34), c.theme.accent, Crafter::TextAlign::Center);
|
||
hits.push_back({ b, Hit::back });
|
||
x = c.Dp(56);
|
||
}
|
||
c.chrome.Text(c.Wrapper().Ellipsize(title, c.Dp(20), c.Width() - x - c.Dp(16)), x, c.CenteredBaseline(header, c.Dp(20)), c.Font(), c.Dp(20), c.theme.text);
|
||
return { 0, header.Bottom(), c.Width(), c.Height() - header.Bottom() };
|
||
}
|
||
|
||
// The strip under the header while the status needs saying; returns the
|
||
// box under it.
|
||
Box DrawBanner(Canvas& c, Box below, const AppStatus& status) {
|
||
const std::string text = BannerText(status);
|
||
if (text.empty()) return below;
|
||
const Box strip{ below.x, below.y, below.w, c.Dp(30) };
|
||
c.chrome.Quad(strip, c.theme.banner);
|
||
c.chrome.Text(c.Wrapper().Ellipsize(text, c.Dp(13), strip.w - c.Dp(32)), c.Dp(16), c.CenteredBaseline(strip, c.Dp(13)), c.Font(), c.Dp(13), c.theme.bannerText);
|
||
return { below.x, strip.Bottom(), below.w, below.h - strip.h };
|
||
}
|
||
|
||
class ListScreen {
|
||
public:
|
||
ScrollState scroll;
|
||
|
||
void Draw(Canvas& c, Box area, std::span<const Conversation> convs, std::int64_t now, const ZoneFn& zone, std::vector<HitRegion>& hits) {
|
||
const float rowH = c.Dp(72);
|
||
c.content.SetClip(area);
|
||
scroll.viewport = area.h;
|
||
scroll.content = rowH * static_cast<float>(convs.size()) + c.Dp(88); // room under the last row to scroll clear of the + button (56 dp, its 16 dp margin, 16 dp of air)
|
||
scroll.Clamp();
|
||
if (convs.empty()) c.content.Text("No messages yet", area.x + area.w / 2, c.CenteredBaseline(area, c.Dp(16)), c.Font(), c.Dp(16), c.theme.muted, Crafter::TextAlign::Center);
|
||
const std::int32_t nowOffset = zone(now);
|
||
for (std::size_t i = 0; i < convs.size(); ++i) {
|
||
const Box row{ area.x, area.y - scroll.offset + rowH * static_cast<float>(i), area.w, rowH };
|
||
if (row.Bottom() < area.y || row.y > area.Bottom()) continue;
|
||
const Conversation& conv = convs[i];
|
||
const std::string name = DisplayNumber(conv.key);
|
||
const Point avatar{ row.x + c.Dp(36), row.y + rowH / 2 };
|
||
c.content.Circle(avatar, c.Dp(20), c.theme.accent);
|
||
c.content.Text(Initial(name), avatar.x, c.CenteredBaseline({ row.x, row.y, row.w, rowH }, c.Dp(18)), c.Font(), c.Dp(18), c.theme.onAccent, Crafter::TextAlign::Center);
|
||
const float textX = row.x + c.Dp(72);
|
||
const float right = row.Right() - c.Dp(16);
|
||
const std::string time = ListCaption(conv.lastTime, zone(conv.lastTime), now, nowOffset);
|
||
const float timeW = c.TextWidth(time, c.Dp(12));
|
||
c.content.Text(time, right, row.y + c.Dp(28), c.Font(), c.Dp(12), c.theme.muted, Crafter::TextAlign::Right);
|
||
c.content.Text(c.Wrapper().Ellipsize(name, c.Dp(16), right - timeW - c.Dp(8) - textX), textX, row.y + c.Dp(28), c.Font(), c.Dp(16), c.theme.text);
|
||
const float previewW = right - (conv.unread ? c.Dp(32) : 0.0f) - textX;
|
||
const std::string preview = conv.lastDirection == Direction::Outgoing ? std::format("You: {}", OneLine(conv.lastText)) : OneLine(conv.lastText);
|
||
c.content.Text(c.Wrapper().Ellipsize(preview, c.Dp(14), previewW), textX, row.y + c.Dp(52), c.Font(), c.Dp(14), c.theme.muted);
|
||
if (conv.unread > 0) {
|
||
const Point badge{ right - c.Dp(10), row.y + c.Dp(46) };
|
||
c.content.Circle(badge, c.Dp(10), c.theme.accent);
|
||
c.content.Text(std::to_string(conv.unread), badge.x, c.CenteredBaseline({ badge.x - c.Dp(10), badge.y - c.Dp(10), c.Dp(20), c.Dp(20) }, c.Dp(11)), c.Font(), c.Dp(11), c.theme.onAccent, Crafter::TextAlign::Center);
|
||
}
|
||
c.content.Quad({ textX, row.Bottom() - c.Dp(1), row.Right() - textX, c.Dp(1) }, c.theme.divider);
|
||
hits.push_back({ Intersect(row, area), Hit::rowBase + static_cast<std::uint32_t>(i) });
|
||
}
|
||
const Point fab{ area.Right() - c.Dp(44), area.Bottom() - c.Dp(44) };
|
||
const Box fabBox{ fab.x - c.Dp(28), fab.y - c.Dp(28), c.Dp(56), c.Dp(56) };
|
||
c.chrome.Circle(fab, c.Dp(28), c.theme.accent);
|
||
c.chrome.Text("+", fab.x, c.CenteredBaseline(fabBox, c.Dp(30)), c.Font(), c.Dp(30), c.theme.onAccent, Crafter::TextAlign::Center);
|
||
hits.push_back({ fabBox, Hit::fab });
|
||
}
|
||
};
|
||
|
||
class ViewScreen {
|
||
public:
|
||
std::string key;
|
||
std::string dialled; // the address as the user typed or a link gave it; "" from the list or a notification
|
||
SimPlan dialledPlan; // the plan of the SIM it was typed under, as far as that SIM's statuses have told it (Number's AfterStatus)
|
||
std::vector<Message> messages;
|
||
ScrollState scroll;
|
||
bool stickToBottom = true;
|
||
ComposeState compose;
|
||
ComposeLayout composeLayout;
|
||
bool caretVisible = true;
|
||
|
||
// The bar at the bottom: the field, grown to its lines, and Send.
|
||
// Returns the area left above it.
|
||
Box DrawComposeBar(Canvas& c, Box area, bool canSend, std::vector<HitRegion>& hits) {
|
||
const float sendW = c.Dp(72);
|
||
const float fieldW = area.w - c.Dp(12) - sendW - c.Dp(8) - c.Dp(12);
|
||
const float lineH = c.LineHeight(c.Dp(16));
|
||
// At most half the area for the bar: a short window keeps its
|
||
// header and some of the conversation above the field.
|
||
const std::size_t room = static_cast<std::size_t>(std::max(0.0f, (area.h * 0.5f - 2 * c.Dp(10) - 2 * c.Dp(8)) / lineH));
|
||
const std::size_t lines = std::clamp<std::size_t>(ComposeLines(c, compose, fieldW - 2 * c.Dp(12)), 1, std::max<std::size_t>(1, room));
|
||
const float fieldH = lineH * static_cast<float>(lines) + 2 * c.Dp(8);
|
||
const float barH = fieldH + 2 * c.Dp(10);
|
||
const Box bar{ area.x, area.Bottom() - barH, area.w, barH };
|
||
c.chrome.Quad(bar, c.theme.surface);
|
||
c.chrome.Quad({ bar.x, bar.y, bar.w, c.Dp(1) }, c.theme.divider);
|
||
const Box field{ bar.x + c.Dp(12), bar.y + c.Dp(10), fieldW, fieldH };
|
||
composeLayout = DrawCompose(c, compose, field, caretVisible);
|
||
hits.push_back({ field, Hit::compose });
|
||
const bool enabled = canSend && !compose.value.empty();
|
||
const Box button{ field.Right() + c.Dp(8), bar.Bottom() - c.Dp(10) - c.Dp(40), sendW, c.Dp(40) };
|
||
c.chrome.Quad(button, enabled ? c.theme.accent : c.theme.divider, c.Dp(20));
|
||
c.chrome.Text("Send", button.x + button.w / 2, c.CenteredBaseline(button, c.Dp(15)), c.Font(), c.Dp(15), enabled ? c.theme.onAccent : c.theme.muted, Crafter::TextAlign::Center);
|
||
if (enabled) hits.push_back({ button, Hit::send });
|
||
return { area.x, area.y, area.w, std::max(0.0f, area.h - barH) };
|
||
}
|
||
|
||
void Draw(Canvas& c, Box area, std::int64_t now, const ZoneFn& zone, std::vector<HitRegion>& hits) {
|
||
const float px = c.Dp(16);
|
||
const float lineH = c.LineHeight(px);
|
||
const float padX = c.Dp(12);
|
||
const float padY = c.Dp(8);
|
||
const float captionPx = c.Dp(11);
|
||
const float captionH = c.LineHeight(captionPx);
|
||
const float sepPx = c.Dp(12);
|
||
const float maxW = area.w * 0.78f;
|
||
const std::int32_t nowOffset = zone(now);
|
||
|
||
struct Placed {
|
||
const Message* message;
|
||
Box bubble;
|
||
std::vector<WrappedLine> lines;
|
||
std::string separator;
|
||
float separatorBaseline;
|
||
};
|
||
std::vector<Placed> placed;
|
||
placed.reserve(messages.size());
|
||
float y = c.Dp(8);
|
||
std::int64_t lastDay = std::numeric_limits<std::int64_t>::min();
|
||
for (const Message& m : messages) {
|
||
Placed p{ &m, {}, {}, "", 0 };
|
||
const std::int32_t offset = zone(m.sortTime);
|
||
const std::int64_t day = LocalDay(m.sortTime, offset);
|
||
if (day != lastDay) {
|
||
p.separator = DayCaption(m.sortTime, offset, now, nowOffset);
|
||
y += c.Dp(6);
|
||
p.separatorBaseline = y + c.Ascent(sepPx);
|
||
y += c.LineHeight(sepPx) + c.Dp(10);
|
||
lastDay = day;
|
||
}
|
||
p.lines = c.Wrapper().Wrap(m.text, px, maxW - 2 * padX);
|
||
float w = 0;
|
||
for (const WrappedLine& l : p.lines) w = std::max(w, l.width);
|
||
const float h = lineH * static_cast<float>(p.lines.size()) + 2 * padY;
|
||
const bool out = m.direction == Direction::Outgoing;
|
||
const float bw = w + 2 * padX;
|
||
p.bubble = { out ? area.Right() - c.Dp(12) - bw : area.x + c.Dp(12), y, bw, h };
|
||
y += h + c.Dp(2) + captionH + c.Dp(8);
|
||
placed.push_back(std::move(p));
|
||
}
|
||
scroll.viewport = area.h;
|
||
scroll.content = y + c.Dp(8);
|
||
if (stickToBottom) scroll.offset = scroll.Max();
|
||
scroll.Clamp();
|
||
c.content.SetClip(area);
|
||
const float base = area.y - scroll.offset;
|
||
const float sepH = c.LineHeight(sepPx);
|
||
for (std::size_t i = 0; i < placed.size(); ++i) {
|
||
const Placed& p = placed[i];
|
||
const Message& m = *p.message;
|
||
// Only what is in view goes to the layer: its glyph budget is
|
||
// finite, and a long conversation is not.
|
||
if (!p.separator.empty() && base + p.separatorBaseline + sepH >= area.y && base + p.separatorBaseline - sepH <= area.Bottom()) c.content.Text(p.separator, area.x + area.w / 2, base + p.separatorBaseline, c.Font(), sepPx, c.theme.muted, Crafter::TextAlign::Center);
|
||
const Box bubble{ p.bubble.x, base + p.bubble.y, p.bubble.w, p.bubble.h };
|
||
if (bubble.Bottom() + captionH + c.Dp(10) < area.y || bubble.y > area.Bottom()) continue;
|
||
const bool out = m.direction == Direction::Outgoing;
|
||
c.content.Quad(bubble, out ? c.theme.bubbleOut : c.theme.bubbleIn, c.Dp(14));
|
||
float baseline = bubble.y + padY + c.Ascent(px);
|
||
for (const WrappedLine& l : p.lines) {
|
||
if (baseline + lineH >= area.y && baseline - lineH <= area.Bottom()) c.content.Text(m.text.substr(l.begin, l.end - l.begin), bubble.x + padX, baseline, c.Font(), px, out ? c.theme.onAccent : c.theme.text);
|
||
baseline += lineH;
|
||
}
|
||
std::string caption = ClockCaption(m.sortTime, zone(m.sortTime));
|
||
if (out) {
|
||
caption = std::format("{} · {}", caption, StateCaption(m.state));
|
||
if (m.state == MessageState::Failed) caption = m.stateReason.empty() ? std::format("{} · tap to retry", caption) : std::format("{} — {} · tap to retry", caption, m.stateReason);
|
||
}
|
||
const float captionBaseline = bubble.Bottom() + c.Dp(2) + c.Ascent(captionPx);
|
||
if (out) c.content.Text(c.Wrapper().Ellipsize(caption, captionPx, area.w - 2 * c.Dp(12)), bubble.Right(), captionBaseline, c.Font(), captionPx, m.state == MessageState::Failed ? c.theme.danger : c.theme.muted, Crafter::TextAlign::Right);
|
||
else c.content.Text(caption, bubble.x, captionBaseline, c.Font(), captionPx, c.theme.muted);
|
||
hits.push_back({ Intersect(bubble, area), Hit::messageBase + static_cast<std::uint32_t>(i) });
|
||
}
|
||
}
|
||
};
|
||
}
|