Ui: the window on Crafter.Graphics, driven from the GLib main loop

The window library. Loop makes the Wayland display a GSource
(libwayland's prepare_read protocol) so D-Bus and the window are served
from one thread without the library's StartSync, which runs a loop of
its own that serves no GMainContext and would starve the bus; the app
owns its frame callback too — a redraw is a frame request and a bare
commit, the compositor's done runs one Window::Update, and nothing
re-arms unless something is dirty or animating — or the day changes: the
captions name the day and an idle window draws no frame by itself, so a
timer on the wall clock, which wakes at the next local midnight and
whenever that clock is set, asks for one when the local date is another
one (DayWatch); when the compositor's connection breaks the source stops
and says so (App's onDisplayLost), since the process cannot connect its
display again. Canvas initialises the device once per process and holds
the window, the font with a word wrapper and a text width over its
unrounded advances, as DrawText draws them, and four compositing layers
(background, the clipped scrolling content, the chrome, an overlay):
within one fused dispatch all quads composite before all text, so the
layers are separate dispatches in that order. The layers' buffer slots
hand their numbers back to the descriptor heap when they go, so the heap
is declared before them and outlives them. Screens draw the conversation
list (avatars, previews, time captions, unread badges, the
new-conversation button, with room under the last row to scroll clear of
it) and one conversation (day separators, wrapped bubbles, clock and
state captions — only what is in view, the layer's glyph budget being
finite) and record the regions a tap can hit. App wires them to the
store and Sync: mouse events become gestures (the detector's slop and
fling threshold set from the window's scale, the positions being buffer
pixels), a tap is resolved against the last frame's regions (which name
the conversation or message they drew, not a position a reload may
shift), scrolling is by wheel, drag or fling (a press stops a fling; a
screen change stops a fling and cancels the press under it, so the
scroll left behind keeps no velocity for a return to revive and a
release after a link's or a notification's Open is no tap on the new
screen), the window is created on the first Show (when the library
cannot and says so — no display, a compositor without wl_shm,
wl_compositor or xdg_wm_base, no Vulkan driver — Show logs why, Open
opens nothing, and the app runs on without one; a later Show fails at
once, the device's set-up running only once) and torn down in order on
Shutdown. ScreenshotThenQuit renders one frame outside the compositor's
pacing and saves it; a window it creates first waits 600 ms for the
compositor's configure, which sizes it (to the screen, on a phone). A
capture with no window to render it — none could be made, or it went
before the frame — is logged and sets App's exitCode to 1.

project.cpp gains Crafter.Graphics (--local takes the branch worktree with
the Wayland input work until it is merged), the imsd-messages-ui library,
and libevdev from the prefix of packaging/build-libevdev.sh on the
library's include path and the executable's library path (a sysroot
build looks for it in the sysroot instead).
This commit is contained in:
Jorijn van der Graaf 2026-10-18 00:26:09 +02:00
commit 6eea7228d0
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
8 changed files with 1132 additions and 3 deletions

View file

@ -0,0 +1,216 @@
// 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::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) });
}
}
};
}