imsd-messages/interfaces/ImsdMessages.Ui-Screens.cppm
Jorijn van der Graaf dd1108da58
main: show the window; --screenshot and --quit-after
The executable shows the window at start unless --daemon (which
--headless and --send imply), on Activate and Open from a second launch
or a notification (of the URIs an Open carries, its first sms: link),
and on an sms: link, which opens the conversation of its first recipient
at once (Number's SmsUriRecipient: the scheme in any case, the fields
after '?' dropped, percent-escapes decoded; then ConversationKey with
the country code known at that moment — --country's, else the one imsd's
status gave once it is in, else what the number itself carries — which
takes NormalizeNumber's result as a phone number — E.164, or digits: a
short code, a national number no country code completed — and nothing
else), or just the window when it names no phone number; the
conversation remembers the address as dialled and the plan of the SIM it
was typed under, and that SIM's statuses move its key (Number's
AfterStatus, one step, applied in App::Status: another SIM's status —
another operator's, or without operators another own number's code,
Number's SameSim — changes nothing, so a SIM change leaves the key and
the plan the address was typed under; the same SIM's fills in what the
plan lacks, and the key follows from its digits to the centre's stand-in
and from any key to the own number's code once the plan has it, never
from one stand-in's key to another's and not while the code is lost, the
conversation it moves to shown at its latest message — and only while
nothing is stored under the key: a text stored before the code arrived
keeps the conversation on the key its rows carry). The shell forwards
Sync's events to the window and the notifier closes a conversation's
notification when it is opened (one closed while its Notify is on its
way goes when the answer comes). --screenshot renders one frame outside
the compositor's pacing 2.5 s after start — 600 ms later where it has to
make the window first — and saves it as a PNG before quitting, or ends
with exit 1 when no frame was captured — with a line when no window
could render it or another instance is running (a flag that acts on the
window finds none there: the run forwards nothing), and without one when
the run ended before the capture (the display gone, a close that quits);
a PNG the library cannot write is not noticed at this commit, and a
capture that throws ends the process — the verification path for a
machine without eyes, which must not depend on a desktop that repaints —
and --quit-after ends a bench on its own clock (a number of seconds its
timer can take: anything else is refused); a --send errand ended by
anything but its final state — that clock — exits 2, as it does on a
signal, its end latched before the loop stops; --screenshot beside
--send or --quit is refused, neither having a window. The window is torn
down in order before the bus client stops, and when the compositor's
connection breaks the process ends, so the next launch starts afresh. A
window the library cannot make (no display, no Vulkan driver) is logged
by Ui's Show and the start ends with exit 1, as a start without its
database does; a resident instance logs it and runs on without one, and
a later Activate or Open logs the same at once, the device's set-up
running only once.

Tested: tests/Number, the sms: links (a query, an escape, a list, the
scheme in capitals, no number), the keys of typed addresses, SameSim
(the same operator, another operator of one country, a status without
its operator, nothing known, own numbers' codes without operators) and
AfterStatus through the statuses a typed address meets: the stand-in
completing its digits while the plan learns the operator; another
stand-in, a lost code and another operator's status — its own number's
code the stand-in's or not — changing nothing; rows keeping the key
while the plan learns the code; the own number's code moving the
stand-in's key, at once or once nothing is stored; a key the own
number's code made, and its plan, staying under another SIM and under
its own SIM's stand-in; an own code that makes the stand-in's key; a
short code; --country before the operator; no operators; no address; an
international address, under another SIM and under its own; and the
address as dialled making the key the address as typed made.
2026-11-18 01:12:27 +01:00

218 lines
12 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 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;
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 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;
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) });
}
}
};
}