imsd-messages/interfaces/ImsdMessages.Ui-Screens.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

325 lines
19 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
/*
ImsdMessages.Ui:Screens — the screens, drawn into the canvas's layers: the
conversation list, one conversation, the new-conversation screen and the
long-press sheets. A screen lays out and draws in the same pass and
records the regions a tap can hit; 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 start = 6;
constexpr std::uint32_t newField = 7;
constexpr std::uint32_t sheetScrim = 10;
constexpr std::uint32_t sheetCancel = 11;
constexpr std::uint32_t sheetDelete = 12;
constexpr std::uint32_t sheetCopy = 13;
constexpr std::uint32_t sheetPanel = 14; // the panel beside and between its buttons: inert
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 };
c.chrome.Quad(field, c.theme.bg, c.Dp(18)); // the field's box on the surface bar
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) });
}
}
};
// The screen behind the +: a number, and Start.
class NewScreen {
public:
ComposeState field;
ComposeLayout fieldLayout;
bool caretVisible = true;
std::string error;
void Draw(Canvas& c, Box area, std::vector<HitRegion>& hits) {
const float x = area.x + c.Dp(16);
const float w = area.w - c.Dp(32);
float y = area.y + c.Dp(20);
c.chrome.Text("To", x, y + c.Ascent(c.Dp(13)), c.Font(), c.Dp(13), c.theme.muted);
y += c.LineHeight(c.Dp(13)) + c.Dp(6);
const Box box{ x, y, w, c.LineHeight(c.Dp(16)) + 2 * c.Dp(8) };
c.chrome.Quad(box, c.theme.surface, c.Dp(18), c.Dp(1), c.theme.divider);
fieldLayout = DrawCompose(c, field, box, caretVisible, "Number");
hits.push_back({ box, Hit::newField });
y = box.Bottom() + c.Dp(8);
const std::string hint = error.empty() ? "Like +31 6 1234 5678 or 06 1234 5678" : error;
c.chrome.Text(c.Wrapper().Ellipsize(hint, c.Dp(13), w), x, y + c.Ascent(c.Dp(13)), c.Font(), c.Dp(13), error.empty() ? c.theme.muted : c.theme.danger);
y += c.LineHeight(c.Dp(13)) + c.Dp(20);
const bool enabled = !field.value.empty();
const Box button{ x, y, w, c.Dp(44) };
c.chrome.Quad(button, enabled ? c.theme.accent : c.theme.divider, c.Dp(22));
c.chrome.Text("Start", button.x + button.w / 2, c.CenteredBaseline(button, c.Dp(16)), c.Font(), c.Dp(16), enabled ? c.theme.onAccent : c.theme.muted, Crafter::TextAlign::Center);
if (enabled) hits.push_back({ button, Hit::start });
}
};
// A bottom sheet over everything: a title, two or three actions, a scrim
// that dismisses. Drawn in the overlay layer; its regions are pushed last,
// so they win the hit test.
struct Sheet {
enum class Kind : std::uint8_t { DeleteConversation, MessageActions };
Kind kind = Kind::DeleteConversation;
std::string key; // the conversation
std::uint64_t messageId = 0;
std::string text; // the message (for Copy)
};
void DrawSheet(Canvas& c, const Sheet& sheet, std::vector<HitRegion>& hits) {
const Box all{ 0, 0, c.Width(), c.Height() };
c.overlay.Quad(all, c.theme.scrim);
hits.push_back({ all, Hit::sheetScrim });
const bool conversation = sheet.kind == Sheet::Kind::DeleteConversation;
const std::size_t buttons = conversation ? 2 : 3;
const float buttonH = c.Dp(48);
const float panelH = c.Dp(16) + c.LineHeight(c.Dp(16)) + c.Dp(12) + buttonH * static_cast<float>(buttons) + c.Dp(8) * static_cast<float>(buttons - 1) + c.Dp(20);
const Box panel{ 0, c.Height() - panelH, c.Width(), panelH + c.Dp(24) };
c.overlay.Quad(panel, c.theme.surface, c.Dp(20));
hits.push_back({ panel, Hit::sheetPanel }); // over the scrim: a tap on the panel itself dismisses nothing
const std::string title = conversation ? std::format("Delete the conversation with {}?", DisplayNumber(sheet.key)) : "Message";
c.overlay.Text(c.Wrapper().Ellipsize(title, c.Dp(16), panel.w - c.Dp(32)), c.Dp(16), panel.y + c.Dp(16) + c.Ascent(c.Dp(16)), c.Font(), c.Dp(16), c.theme.text);
float y = panel.y + c.Dp(16) + c.LineHeight(c.Dp(16)) + c.Dp(12);
auto button = [&](std::string_view label, Color bg, Color fg, std::uint32_t id) {
const Box b{ c.Dp(16), y, panel.w - c.Dp(32), buttonH };
c.overlay.Quad(b, bg, c.Dp(24));
c.overlay.Text(label, b.x + b.w / 2, c.CenteredBaseline(b, c.Dp(16)), c.Font(), c.Dp(16), fg, Crafter::TextAlign::Center);
hits.push_back({ b, id });
y += buttonH + c.Dp(8);
};
if (!conversation) button("Copy text", c.theme.bg, c.theme.text, Hit::sheetCopy);
button(conversation ? "Delete conversation" : "Delete message", c.theme.danger, c.theme.onAccent, Hit::sheetDelete);
button("Cancel", c.theme.bg, c.theme.text, Hit::sheetCancel);
}
}