Ui: the compose field and the send bar

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).
This commit is contained in:
Jorijn van der Graaf 2026-10-18 00:35:39 +02:00
commit efd8dd92d3
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
12 changed files with 727 additions and 22 deletions

View file

@ -20,6 +20,7 @@ import Crafter.Event;
import ImsdMessages;
import :Loop;
import :Canvas;
import :Compose;
import :Screens;
export namespace ImsdMessages::Ui {
@ -29,6 +30,7 @@ export namespace ImsdMessages::Ui {
~App() {
Shutdown();
if (longPressTimer) g_source_remove(longPressTimer);
if (repeatTimer) g_source_remove(repeatTimer);
}
// Tears the window down in order: the GPU first finishes the frame
@ -38,11 +40,15 @@ export namespace ImsdMessages::Ui {
void Shutdown() {
if (!canvas) return;
vkDeviceWaitIdle(Crafter::Device::device);
if (blinkTimer) g_source_remove(blinkTimer);
blinkTimer = 0;
downSub.reset();
upSub.reset();
moveSub.reset();
scrollSub.reset();
keySub.reset();
keyUpSub.reset();
textSub.reset();
resizeSub.reset();
closeSub.reset();
day.reset();
@ -126,8 +132,17 @@ export namespace ImsdMessages::Ui {
if (view) view->stickToBottom = false;
Dirty();
});
keySub.emplace(&w.onRawKeyDown, [this](Crafter::KeyCode code) {
if (code == Crafter::Key(Crafter::CrafterKeys::Escape)) Back();
keySub.emplace(&w.onRawKeyDown, [this](Crafter::KeyCode code) { Key(code); });
keyUpSub.emplace(&w.onRawKeyUp, [this](Crafter::KeyCode) {
if (canvas->Window().heldKeys.empty() && repeatTimer) {
g_source_remove(repeatTimer);
repeatTimer = 0;
}
});
textSub.emplace(&w.onTextInput, [this](const std::string_view text) {
if (!view) return;
ComposeInsert(view->compose, text);
ShowCaret();
});
resizeSub.emplace(&w.onResize, [this] { Dirty(); });
closeSub.emplace(&w.onClose, [this] {
@ -140,9 +155,10 @@ export namespace ImsdMessages::Ui {
// Shows the window on a conversation. Without a window (Show says
// why) nothing is on screen, so nothing is read or opened. `dialled`
// is the address as the user typed or a link gave it, what the
// status re-keys the conversation from (Status); "" from the list
// or a notification.
// is the address as the user typed or a link gave it, what a text
// from here is sent to and what the status re-keys the conversation
// from (Status); "" from the list or a notification, where the
// latest message's address is (Destination).
void Open(std::string_view key, std::string_view dialled = "") {
// The view may point into what opening replaces (a row's key in
// `hitKeys`, the notifier's record of the tapped notification):
@ -154,8 +170,10 @@ export namespace ImsdMessages::Ui {
Show();
if (!canvas) return;
if (!view || view->key != key) {
KeepDraft();
view.emplace();
view->key = std::string(key);
view->compose = drafts.Take(key);
}
view->dialled = std::string(dialled);
view->dialledPlan = sync ? sync->Plan() : SimPlan{}; // for an address typed or linked now: the SIM's as known now
@ -164,18 +182,41 @@ export namespace ImsdMessages::Ui {
if (store.MarkRead(key) > 0) Reload();
if (sync) sync->SetOpenConversation(view->key);
if (onConversationOpened) onConversationOpened(view->key);
Dirty();
ShowCaret();
}
void Back() {
if (!view) return;
StopFling();
CancelGesture();
KeepDraft();
view.reset();
if (blinkTimer) {
g_source_remove(blinkTimer);
blinkTimer = 0;
}
if (sync) sync->SetOpenConversation(std::nullopt);
Dirty();
}
// The address a text from the open conversation is sent to (Model's
// SendAddress: as the user typed or a link gave it, else the latest
// message's, never the key); "" when none is known or it belongs to
// another SIM's plan, and Send is off.
std::string Destination() const { return SendAddress(view->dialled, view->dialledPlan, view->messages, sync ? sync->Plan() : SimPlan{}); }
// Sends the field's text to the open conversation.
void SendDraft() {
if (!view || !sync || view->compose.value.empty() || !CanSend(status) || Destination().empty()) return;
std::string text = view->compose.value;
while (!text.empty() && (text.back() == '\n' || text.back() == ' ')) text.pop_back();
// A store that refuses the text (its disk full) keeps the draft.
if (!text.empty() && sync->Send(view->key, Destination(), text) == Sync::kNotStored) return;
view->compose = ComposeState{};
view->stickToBottom = true;
Dirty();
}
// Renders one frame now, outside the compositor's pacing, saves it as
// a PNG and asks to quit: the verification path for a machine without
// eyes, which must not depend on a desktop that repaints. A window
@ -214,6 +255,8 @@ export namespace ImsdMessages::Ui {
if (onConversationOpened) onConversationOpened(view->key);
}
}
// The drafts of the typed addresses that were left follow it too.
if (s.statusKnown && sync) drafts.Status(sync->Plan(), sync->CountryCode(), [this](std::string_view key) { return store.Conversation(key).empty(); });
Dirty();
}
@ -228,6 +271,12 @@ export namespace ImsdMessages::Ui {
if (frames) frames->RequestRedraw();
}
// Leaving a conversation keeps what its field holds, by its key
// (Edit's Drafts); Open takes it up again.
void KeepDraft() {
if (view) drafts.Keep(view->key, view->compose, view->dialled, view->dialledPlan);
}
void Reload() {
convs = GroupConversations(store.All());
if (view) {
@ -291,11 +340,7 @@ export namespace ImsdMessages::Ui {
if (view) {
Box area = DrawHeader(c, DisplayNumber(view->key), true, hits);
area = DrawBanner(c, area, status);
const Box compose{ area.x, area.Bottom() - c.Dp(56), area.w, c.Dp(56) };
c.chrome.Quad(compose, c.theme.surface);
c.chrome.Quad({ compose.x, compose.y, compose.w, c.Dp(1) }, c.theme.divider);
c.chrome.Text("Composing comes in a later step", compose.x + c.Dp(16), c.CenteredBaseline(compose, c.Dp(14)), c.Font(), c.Dp(14), c.theme.muted);
area.h -= compose.h;
area = view->DrawComposeBar(c, area, CanSend(status) && !Destination().empty(), hits);
view->Draw(c, area, now, zone, hits);
} else {
Box area = DrawHeader(c, "Messages", false, hits);
@ -377,11 +422,68 @@ export namespace ImsdMessages::Ui {
return nullptr;
}
// A key: Escape goes back; Ctrl+Enter sends; the rest edits the field.
void Key(Crafter::KeyCode code) {
using Crafter::CrafterKeys;
if (code == Crafter::Key(CrafterKeys::Escape)) {
Back();
return;
}
if (!view) return;
const std::unordered_set<Crafter::KeyCode>& held = canvas->Window().heldKeys;
const bool ctrl = held.contains(Crafter::Key(CrafterKeys::LeftCtrl)) || held.contains(Crafter::Key(CrafterKeys::RightCtrl));
if (ctrl && (code == Crafter::Key(CrafterKeys::Enter) || code == Crafter::Key(CrafterKeys::keypad_enter))) {
SendDraft();
return;
}
if (ComposeKey(view->compose, code)) ShowCaret();
// The library repeats a held key only while frames run; with frames
// on demand a timer ticks the repeat instead.
if (!repeatTimer) repeatTimer = g_timeout_add(30, OnRepeat, this);
}
static gboolean OnRepeat(gpointer data) {
App& self = *static_cast<App*>(data);
if (!self.canvas || self.canvas->Window().heldKeys.empty()) {
self.repeatTimer = 0;
return G_SOURCE_REMOVE;
}
Crafter::Device::TickKeyRepeats();
return G_SOURCE_CONTINUE;
}
// The caret shows at once after an edit and blinks from there.
void ShowCaret() {
if (!view) return;
view->caretVisible = true;
if (blinkTimer) g_source_remove(blinkTimer);
blinkTimer = g_timeout_add(500, OnBlink, this);
Dirty();
}
static gboolean OnBlink(gpointer data) {
App& self = *static_cast<App*>(data);
if (!self.view) {
self.blinkTimer = 0;
return G_SOURCE_REMOVE;
}
self.view->caretVisible = !self.view->caretVisible;
self.Dirty();
return G_SOURCE_CONTINUE;
}
void Tap(Point at) {
const std::optional<std::uint32_t> id = HitTest(hits, at);
if (!id) return;
if (*id == Hit::back) {
Back();
} else if (*id == Hit::compose) {
if (!view) return;
view->compose.focused = true;
view->compose.cursor = ComposeHit(*canvas, view->compose, view->composeLayout, at);
ShowCaret();
} else if (*id == Hit::send) {
SendDraft();
} else if (*id == Hit::fab) {
Log("new conversation (comes in a later step)");
} else if (*id >= Hit::messageBase) {
@ -408,9 +510,12 @@ export namespace ImsdMessages::Ui {
std::optional<DayWatch> day; // while the window is up
ListScreen list;
std::optional<ViewScreen> view;
Drafts drafts; // what was typed in the conversations left and not sent
GestureDetector gestures;
std::vector<HitRegion> hits;
guint longPressTimer = 0;
guint repeatTimer = 0;
guint blinkTimer = 0;
std::chrono::steady_clock::time_point lastFrame{};
bool overflowLogged = false;
std::filesystem::path screenshotPath;
@ -419,6 +524,8 @@ export namespace ImsdMessages::Ui {
std::optional<Crafter::EventListener<void>> moveSub;
std::optional<Crafter::EventListener<std::uint32_t>> scrollSub;
std::optional<Crafter::EventListener<Crafter::KeyCode>> keySub;
std::optional<Crafter::EventListener<Crafter::KeyCode>> keyUpSub;
std::optional<Crafter::EventListener<const std::string_view>> textSub;
std::optional<Crafter::EventListener<void>> resizeSub;
std::optional<Crafter::EventListener<void>> closeSub;
};