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:
parent
dd1108da58
commit
efd8dd92d3
12 changed files with 727 additions and 22 deletions
|
|
@ -27,7 +27,7 @@ and §7.9 (Messaging) and §8.1 (the frozen one).
|
|||
| Path | What it is |
|
||||
|---|---|
|
||||
| `project.cpp` | The build description for Crafter.Build: four products (below) and the tests. |
|
||||
| `interfaces/ImsdMessages*.cppm` | `imsd-messages-core`: std + Crafter.DB, no GLib, no window — numbers, time, the model, word wrap, gestures, the store, the sync logic. |
|
||||
| `interfaces/ImsdMessages*.cppm` | `imsd-messages-core`: std + Crafter.DB, no GLib, no window — numbers, time, the model, word wrap, gestures, the message field's editing, the store, the sync logic. |
|
||||
| `interfaces/ImsdMessages.Bus*.cppm` | `imsd-messages-bus`: GLib/GDBus — imsd's dictionaries, the client, notifications, the app's own bus name. |
|
||||
| `interfaces/ImsdMessages.Ui*.cppm` | `imsd-messages-ui`: the window on Crafter.Graphics, driven from the GLib main loop. |
|
||||
| `implementations/main.cpp` | The executable `imsd-messages`. |
|
||||
|
|
|
|||
172
interfaces/ImsdMessages-Edit.cppm
Normal file
172
interfaces/ImsdMessages-Edit.cppm
Normal file
|
|
@ -0,0 +1,172 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
/*
|
||||
ImsdMessages:Edit — the message field's text and its editing, by code
|
||||
point: a text message is several lines of UTF-8, and an edit that took a
|
||||
single byte would leave half a character behind. The window draws the
|
||||
state, places the cursor where a tap lands and turns its keys into
|
||||
EditKeys. What a field held when its conversation was left is here too
|
||||
(Drafts).
|
||||
*/
|
||||
export module ImsdMessages:Edit;
|
||||
import std;
|
||||
import :Number;
|
||||
import :Wrap;
|
||||
|
||||
export namespace ImsdMessages {
|
||||
struct ComposeState {
|
||||
std::string value;
|
||||
std::size_t cursor = 0; // a byte offset on a code point boundary
|
||||
bool focused = true;
|
||||
};
|
||||
|
||||
// The byte offset of the code point before `at`.
|
||||
std::size_t PreviousCodepoint(std::string_view s, std::size_t at) {
|
||||
if (at == 0) return 0;
|
||||
std::size_t i = at - 1;
|
||||
while (i > 0 && (static_cast<std::uint8_t>(s[i]) & 0xC0) == 0x80) --i;
|
||||
return i;
|
||||
}
|
||||
|
||||
std::size_t NextCodepoint(std::string_view s, std::size_t at) {
|
||||
if (at >= s.size()) return s.size();
|
||||
std::size_t i = at;
|
||||
DecodeUtf8(s, i);
|
||||
return i;
|
||||
}
|
||||
|
||||
void ComposeInsert(ComposeState& c, std::string_view utf8) {
|
||||
c.value.insert(c.cursor, utf8);
|
||||
c.cursor += utf8.size();
|
||||
}
|
||||
|
||||
enum class EditKey : std::uint8_t { Backspace, Delete, Left, Right, Home, End, Newline };
|
||||
|
||||
// Applies one key. False when Backspace or Delete has nothing to delete;
|
||||
// a move is true even where the cursor cannot go further.
|
||||
bool ComposeEdit(ComposeState& c, EditKey key) {
|
||||
if (key == EditKey::Backspace) {
|
||||
if (c.cursor == 0) return false;
|
||||
const std::size_t from = PreviousCodepoint(c.value, c.cursor);
|
||||
c.value.erase(from, c.cursor - from);
|
||||
c.cursor = from;
|
||||
return true;
|
||||
}
|
||||
if (key == EditKey::Delete) {
|
||||
if (c.cursor >= c.value.size()) return false;
|
||||
c.value.erase(c.cursor, NextCodepoint(c.value, c.cursor) - c.cursor);
|
||||
return true;
|
||||
}
|
||||
if (key == EditKey::Left) {
|
||||
c.cursor = PreviousCodepoint(c.value, c.cursor);
|
||||
return true;
|
||||
}
|
||||
if (key == EditKey::Right) {
|
||||
c.cursor = NextCodepoint(c.value, c.cursor);
|
||||
return true;
|
||||
}
|
||||
if (key == EditKey::Home) {
|
||||
const std::size_t nl = c.value.rfind('\n', c.cursor == 0 ? 0 : c.cursor - 1);
|
||||
c.cursor = (nl == std::string::npos || c.cursor == 0) ? 0 : nl + 1;
|
||||
return true;
|
||||
}
|
||||
if (key == EditKey::End) {
|
||||
const std::size_t nl = c.value.find('\n', c.cursor);
|
||||
c.cursor = nl == std::string::npos ? c.value.size() : nl;
|
||||
return true;
|
||||
}
|
||||
if (key == EditKey::Newline) {
|
||||
ComposeInsert(c, "\n");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// The byte a tap at or past a line's end asks for: the end of the text
|
||||
// on the last line (the last by index: a text ending in a newline has
|
||||
// an empty last line, and the line before it ends at that newline);
|
||||
// before a hard break, the newline — after the space that overflowed
|
||||
// and hangs off the line with it (Wrap), so a word typed there stays a
|
||||
// word of its own; at a soft wrap, the line's end.
|
||||
std::size_t ComposeLineEnd(std::span<const WrappedLine> lines, std::size_t li, std::string_view text) {
|
||||
if (li + 1 >= lines.size()) return text.size();
|
||||
const std::size_t next = lines[li + 1].begin;
|
||||
if (next > lines[li].end && text[next - 1] == '\n') return next - 1;
|
||||
return lines[li].end;
|
||||
}
|
||||
|
||||
// The first of the `shown` lines a field of that height draws so that the
|
||||
// caret's line is among them: the text's first lines until the caret
|
||||
// passes the field's last line, then the caret's line is the last shown.
|
||||
std::size_t ComposeFirstLine(std::size_t caretLine, std::size_t shown) {
|
||||
return caretLine + 1 > shown ? caretLine + 1 - shown : 0;
|
||||
}
|
||||
|
||||
// What was typed in the conversations that were left, and not sent, by
|
||||
// conversation key, for this run. A draft is its conversation's, not
|
||||
// the view's: leaving the conversation keeps it, opening it again
|
||||
// takes it up, and it goes when it is sent.
|
||||
class Drafts {
|
||||
public:
|
||||
// A conversation is left: what its field holds is kept and comes
|
||||
// back as a field is opened; an empty field keeps nothing and drops
|
||||
// nothing. `dialled` and `plan` are the view's: the address as typed
|
||||
// or linked — "" for a conversation opened from the list, whose
|
||||
// draft Status leaves where it is — and the plan of the SIM it was
|
||||
// opened under. A second text kept under one key (two keys that
|
||||
// became one: Status, and the open conversation's own move) goes
|
||||
// after the first on a line of its own, so nothing typed is dropped.
|
||||
void Keep(std::string_view key, ComposeState field, std::string_view dialled = {}, SimPlan plan = {}) {
|
||||
if (field.value.empty()) return;
|
||||
field.focused = ComposeState{}.focused;
|
||||
const auto kept = byKey.find(key);
|
||||
if (kept == byKey.end()) {
|
||||
byKey.emplace(std::string(key), Kept{ std::move(field), std::string(dialled), std::move(plan) });
|
||||
return;
|
||||
}
|
||||
kept->second.field.value += '\n';
|
||||
kept->second.field.value += field.value;
|
||||
kept->second.field.cursor = kept->second.field.value.size();
|
||||
}
|
||||
|
||||
// A conversation is opened: its draft, which the field holds from
|
||||
// here on; an empty field when it has none.
|
||||
ComposeState Take(std::string_view key) {
|
||||
const auto kept = byKey.find(key);
|
||||
if (kept == byKey.end()) return {};
|
||||
ComposeState field = std::move(kept->second.field);
|
||||
byKey.erase(kept);
|
||||
return field;
|
||||
}
|
||||
|
||||
// imsd's status: the key of a kept typed address follows it as an
|
||||
// open conversation's does (Number's AfterStatus, with the same
|
||||
// arguments; `nothingStored` says whether a key has no texts) — a
|
||||
// draft kept before the status brought the country code is found
|
||||
// under the key the same address makes afterwards.
|
||||
void Status(const SimPlan& now, std::string_view code, const std::function<bool(std::string_view)>& nothingStored) {
|
||||
std::vector<std::pair<std::string, std::string>> moves;
|
||||
for (auto& [key, kept] : byKey) {
|
||||
if (kept.dialled.empty()) continue;
|
||||
TypedKey typed = AfterStatus(kept.dialled, { key, kept.plan }, now, code, nothingStored(key));
|
||||
kept.plan = std::move(typed.plan);
|
||||
if (typed.key != key) moves.emplace_back(key, std::move(typed.key));
|
||||
}
|
||||
for (const auto& [from, to] : moves) {
|
||||
auto node = byKey.extract(from);
|
||||
Keep(to, std::move(node.mapped().field), node.mapped().dialled, std::move(node.mapped().plan));
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t Size() const noexcept { return byKey.size(); }
|
||||
|
||||
private:
|
||||
struct Kept {
|
||||
ComposeState field;
|
||||
std::string dialled;
|
||||
SimPlan plan;
|
||||
};
|
||||
std::map<std::string, Kept, std::less<>> byKey;
|
||||
};
|
||||
}
|
||||
|
|
@ -170,6 +170,27 @@ export namespace ImsdMessages {
|
|||
return out;
|
||||
}
|
||||
|
||||
// The address a text from a conversation goes to: the one the user typed
|
||||
// or a link gave (`dialled`), else the latest message's `peer` — the
|
||||
// network's form of a received text, the dialled form of ours (the
|
||||
// conversation oldest first, as the store gives it) — and "" when the
|
||||
// conversation has neither, which turns Send off. Never the conversation
|
||||
// key: a country code completed for it groups (ImsdMessages:Number) and
|
||||
// must not be dialled. An address that is not international — a
|
||||
// national number, a short code; typed, linked or a peer's — belongs
|
||||
// to the plan it was typed or the row written under (`dialledPlan`,
|
||||
// the message's `plan`); under another SIM's plan (`now`, the one
|
||||
// known now: Number's AnotherPlan) it would reach another subscriber
|
||||
// or another service — so no address, until the user types it again
|
||||
// (the view keeps `dialled` across the change, so it is checked each
|
||||
// time).
|
||||
std::string SendAddress(std::string_view dialled, const SimPlan& dialledPlan, std::span<const Message> conversation, const SimPlan& now) {
|
||||
const std::string address = !dialled.empty() ? std::string(dialled) : conversation.empty() ? std::string() : conversation.back().peer;
|
||||
const SimPlan plan = !dialled.empty() ? dialledPlan : conversation.empty() ? SimPlan{} : conversation.back().plan;
|
||||
if (AnotherPlan(address, plan, now)) return "";
|
||||
return address;
|
||||
}
|
||||
|
||||
// What the app knows about imsd, for the banner and the send button.
|
||||
struct AppStatus {
|
||||
bool imsdPresent = false; // net.catcrafts.IMS1 has an owner
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -209,6 +209,26 @@ export namespace ImsdMessages::Ui {
|
|||
return width;
|
||||
}
|
||||
|
||||
// The byte offset in a line nearest to x from its start, by the
|
||||
// widths TextWidth measures: where a tap puts the cursor.
|
||||
std::size_t NearestByte(std::string_view line, float px, float x) {
|
||||
if (x <= 0) return 0;
|
||||
const float scale = font->ScaleForSize(px);
|
||||
float width = 0;
|
||||
float bestDistance = x;
|
||||
std::size_t best = 0;
|
||||
for (std::size_t i = 0; i < line.size();) {
|
||||
const std::uint32_t cp = DecodeUtf8(line, i);
|
||||
if (cp == 0) break;
|
||||
if (cp != '\n') width += static_cast<float>(font->AdvanceUnits(cp)) * scale;
|
||||
const float distance = std::abs(x - width);
|
||||
if (distance > bestDistance) break; // the widths only grow; at a tie the later offset wins (past a zero-width mark)
|
||||
bestDistance = distance;
|
||||
best = i;
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
// The baseline that centres one line of text in a box.
|
||||
float CenteredBaseline(Box b, float px) { return b.y + (b.h - LineHeight(px)) / 2.0f + Ascent(px); }
|
||||
|
||||
|
|
|
|||
125
interfaces/ImsdMessages.Ui-Compose.cppm
Normal file
125
interfaces/ImsdMessages.Ui-Compose.cppm
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
/*
|
||||
ImsdMessages.Ui:Compose — the message field on the canvas. The library's
|
||||
InputField is one line and erases single bytes, which breaks UTF-8; a text
|
||||
message needs neither limit, so the field's state and its editing are the
|
||||
core's (ImsdMessages:Edit). Here the keys become its EditKeys, and the
|
||||
draw function lays the field out with the canvas's word wrapper and
|
||||
reports where the text sits, so a tap can place the cursor.
|
||||
*/
|
||||
module;
|
||||
#include "vulkan/vulkan.h"
|
||||
export module ImsdMessages.Ui:Compose;
|
||||
import std;
|
||||
import Crafter.Graphics;
|
||||
import ImsdMessages;
|
||||
import :Canvas;
|
||||
|
||||
export namespace ImsdMessages::Ui {
|
||||
// Backspace, Delete, Left, Right, Home, End and Enter (a newline), as the
|
||||
// core's edits; any other key is ignored (false).
|
||||
bool ComposeKey(ComposeState& c, Crafter::KeyCode code) {
|
||||
using Crafter::CrafterKeys;
|
||||
using Crafter::Key;
|
||||
if (code == Key(CrafterKeys::Backspace)) return ComposeEdit(c, EditKey::Backspace);
|
||||
if (code == Key(CrafterKeys::Delete)) return ComposeEdit(c, EditKey::Delete);
|
||||
if (code == Key(CrafterKeys::Left)) return ComposeEdit(c, EditKey::Left);
|
||||
if (code == Key(CrafterKeys::Right)) return ComposeEdit(c, EditKey::Right);
|
||||
if (code == Key(CrafterKeys::Home)) return ComposeEdit(c, EditKey::Home);
|
||||
if (code == Key(CrafterKeys::End)) return ComposeEdit(c, EditKey::End);
|
||||
if (code == Key(CrafterKeys::Enter) || code == Key(CrafterKeys::keypad_enter)) return ComposeEdit(c, EditKey::Newline);
|
||||
return false;
|
||||
}
|
||||
|
||||
// The field grows to this many lines, then scrolls to keep the caret's
|
||||
// line in view.
|
||||
constexpr std::size_t composeMaxLines = 5;
|
||||
|
||||
// Where the field's text sits, for the caret and for taps.
|
||||
struct ComposeLayout {
|
||||
Box text; // the text area inside the field
|
||||
std::vector<WrappedLine> lines;
|
||||
float lineHeight = 0;
|
||||
float px = 0;
|
||||
std::size_t first = 0; // the first line drawn: a longer text shows the lines around the caret
|
||||
};
|
||||
|
||||
// The lines the field's box shows: as many as it is tall, at least one.
|
||||
std::size_t ComposeShownLines(const ComposeLayout& layout) {
|
||||
return std::max<std::size_t>(1, static_cast<std::size_t>(std::lround(layout.text.h / layout.lineHeight)));
|
||||
}
|
||||
|
||||
// The field: rounded, one to five lines tall, a placeholder when empty,
|
||||
// the caret at the cursor. Returns the layout the app needs for taps.
|
||||
ComposeLayout DrawCompose(Canvas& c, const ComposeState& s, Box field, bool caretVisible) {
|
||||
const float px = c.Dp(16);
|
||||
const float padX = c.Dp(12);
|
||||
const float padY = c.Dp(8);
|
||||
ComposeLayout layout;
|
||||
layout.px = px;
|
||||
layout.lineHeight = c.LineHeight(px);
|
||||
layout.text = { field.x + padX, field.y + padY, field.w - 2 * padX, field.h - 2 * padY };
|
||||
c.chrome.Quad(field, c.theme.bg, c.Dp(18));
|
||||
if (s.value.empty()) {
|
||||
c.chrome.Text("Message", layout.text.x, layout.text.y + c.Ascent(px), c.Font(), px, c.theme.muted);
|
||||
layout.lines = { { 0, 0, 0 } };
|
||||
} else {
|
||||
layout.lines = c.Wrapper().Wrap(s.value, px, layout.text.w);
|
||||
}
|
||||
// The caret's line: the last line that starts at or before the
|
||||
// cursor; a cursor right after a break belongs to the next line.
|
||||
std::size_t li = 0;
|
||||
for (std::size_t i = 0; i < layout.lines.size(); ++i) {
|
||||
if (layout.lines[i].begin <= s.cursor) li = i;
|
||||
}
|
||||
// The field holds as many lines as its box is tall (the bar grows it
|
||||
// to composeMaxLines; a one-line box shows one): a longer text shows
|
||||
// the lines from `first`, chosen so the caret's line is among them.
|
||||
const std::size_t shownLines = ComposeShownLines(layout);
|
||||
layout.first = ComposeFirstLine(li, shownLines);
|
||||
const std::size_t last = std::min(layout.lines.size(), layout.first + shownLines);
|
||||
float baseline = layout.text.y + c.Ascent(px);
|
||||
for (std::size_t i = layout.first; i < last; ++i) {
|
||||
const WrappedLine& l = layout.lines[i];
|
||||
if (!s.value.empty()) c.chrome.Text(std::string_view(s.value).substr(l.begin, l.end - l.begin), layout.text.x, baseline, c.Font(), px, c.theme.text);
|
||||
baseline += layout.lineHeight;
|
||||
}
|
||||
if (s.focused && caretVisible) {
|
||||
const WrappedLine& l = layout.lines[li];
|
||||
const std::size_t end = std::min(s.cursor, l.end);
|
||||
const float x = layout.text.x + (end > l.begin ? c.TextWidth(std::string_view(s.value).substr(l.begin, end - l.begin), px) : 0.0f);
|
||||
const float y = layout.text.y + layout.lineHeight * static_cast<float>(li - layout.first);
|
||||
c.chrome.Quad({ x, y + c.Dp(2), c.Dp(2), layout.lineHeight - c.Dp(4) }, c.theme.accent);
|
||||
}
|
||||
return layout;
|
||||
}
|
||||
|
||||
// The number of lines the field needs for its text, one to five.
|
||||
std::size_t ComposeLines(Canvas& c, const ComposeState& s, float textWidth) {
|
||||
if (s.value.empty()) return 1;
|
||||
return std::clamp<std::size_t>(c.Wrapper().Wrap(s.value, c.Dp(16), textWidth).size(), 1, composeMaxLines);
|
||||
}
|
||||
|
||||
// The byte offset in the text a tap at p asks for (App's Tap makes it
|
||||
// the cursor). The lines are the text's as it is now: an edit since the
|
||||
// frame that laid the field out only asked for a frame, so the layout's
|
||||
// lines may describe text that is gone.
|
||||
std::size_t ComposeHit(Canvas& c, const ComposeState& s, const ComposeLayout& layout, Point p) {
|
||||
if (s.value.empty()) return 0;
|
||||
if (layout.lineHeight <= 0) return s.value.size(); // no frame has laid the field out (a view just opened): the text's end
|
||||
const std::vector<WrappedLine>& lines = c.Wrapper().Wrap(s.value, layout.px, layout.text.w);
|
||||
if (lines.empty()) return 0;
|
||||
std::size_t li = layout.first + static_cast<std::size_t>(std::max(0.0f, (p.y - layout.text.y) / layout.lineHeight));
|
||||
li = std::min(li, layout.first + ComposeShownLines(layout) - 1); // the drawn lines only: the padding below belongs to the last
|
||||
li = std::min(li, lines.size() - 1);
|
||||
const WrappedLine& l = lines[li];
|
||||
const std::string_view line = std::string_view(s.value).substr(l.begin, l.end - l.begin);
|
||||
const std::size_t byte = c.NearestByte(line, layout.px, p.x - layout.text.x);
|
||||
// At or past the line's end: the text's end on the last line, the
|
||||
// newline after a hung space before a hard break (Edit's ComposeLineEnd).
|
||||
if (byte == line.size()) return ComposeLineEnd(lines, li, s.value);
|
||||
return l.begin + byte;
|
||||
}
|
||||
}
|
||||
|
|
@ -14,6 +14,7 @@ import std;
|
|||
import Crafter.Graphics;
|
||||
import ImsdMessages;
|
||||
import :Canvas;
|
||||
import :Compose;
|
||||
|
||||
export namespace ImsdMessages::Ui {
|
||||
// The local UTC offset, in minutes, at an instant.
|
||||
|
|
@ -23,6 +24,8 @@ export namespace ImsdMessages::Ui {
|
|||
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;
|
||||
}
|
||||
|
|
@ -137,6 +140,35 @@ export namespace ImsdMessages::Ui {
|
|||
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);
|
||||
|
|
|
|||
|
|
@ -4,12 +4,14 @@
|
|||
/*
|
||||
ImsdMessages.Ui — the window: Crafter.Graphics driven from the GLib main
|
||||
loop. Loop makes the Wayland display a GSource and owns the frame callback,
|
||||
Canvas holds the window, the font and the four compositing layers, Screens
|
||||
draw the conversation list and a conversation, App wires them to the store
|
||||
and to Sync.
|
||||
Canvas holds the window, the font and the four compositing layers, Compose
|
||||
lays a field out and draws it over the core's editing (ImsdMessages:Edit),
|
||||
turning keys into its edits, Screens draw the conversation list and a
|
||||
conversation, App wires them to the store and to Sync.
|
||||
*/
|
||||
export module ImsdMessages.Ui;
|
||||
export import :Loop;
|
||||
export import :Canvas;
|
||||
export import :Compose;
|
||||
export import :Screens;
|
||||
export import :App;
|
||||
|
|
|
|||
|
|
@ -4,9 +4,9 @@
|
|||
/*
|
||||
ImsdMessages — the core of imsd-messages: the parts that need neither the
|
||||
bus nor a window (numbers, time, the message model, word wrap, gestures,
|
||||
the store and the sync logic), so each of them is unit-testable on a dev box
|
||||
with no phone, no imsd and no GPU. The bus layer is ImsdMessages.Bus, the
|
||||
window ImsdMessages.Ui.
|
||||
the message field's editing, the store and the sync logic), so each of them
|
||||
is unit-testable on a dev box with no phone, no imsd and no GPU. The bus
|
||||
layer is ImsdMessages.Bus, the window ImsdMessages.Ui.
|
||||
*/
|
||||
export module ImsdMessages;
|
||||
export import :Number;
|
||||
|
|
@ -16,3 +16,4 @@ export import :Wrap;
|
|||
export import :Store;
|
||||
export import :Sync;
|
||||
export import :Gesture;
|
||||
export import :Edit;
|
||||
|
|
|
|||
12
project.cpp
12
project.cpp
|
|
@ -84,8 +84,9 @@ extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> a
|
|||
: GitProject({ .source = { .url = "https://forgejo.catcrafts.net/Catcrafts/Crafter.Graphics.git" }, .args = depArgs });
|
||||
|
||||
// imsd-messages-core — numbers, time, the model, word wrap, gestures,
|
||||
// the store and the sync logic: std + Crafter.DB, no GLib, so every
|
||||
// test runs with no bus, no phone and no GPU.
|
||||
// the message field's editing, the store and the sync logic: std +
|
||||
// Crafter.DB, no GLib, so every test runs with no bus, no phone and no
|
||||
// GPU.
|
||||
static auto Core = std::make_unique<Configuration>();
|
||||
Core->path = "./";
|
||||
Core->name = "imsd-messages-core";
|
||||
|
|
@ -94,7 +95,7 @@ extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> a
|
|||
Core->type = ConfigurationType::LibraryStatic;
|
||||
Core->dependencies = { db };
|
||||
{
|
||||
std::array<fs::path, 8> ifaces = {
|
||||
std::array<fs::path, 9> ifaces = {
|
||||
"interfaces/ImsdMessages",
|
||||
"interfaces/ImsdMessages-Number",
|
||||
"interfaces/ImsdMessages-Time",
|
||||
|
|
@ -103,6 +104,7 @@ extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> a
|
|||
"interfaces/ImsdMessages-Store",
|
||||
"interfaces/ImsdMessages-Sync",
|
||||
"interfaces/ImsdMessages-Gesture",
|
||||
"interfaces/ImsdMessages-Edit",
|
||||
};
|
||||
std::array<fs::path, 0> impls = {};
|
||||
Core->GetInterfacesAndImplementations(ifaces, impls);
|
||||
|
|
@ -142,10 +144,11 @@ extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> a
|
|||
Ui->type = ConfigurationType::LibraryStatic;
|
||||
Ui->dependencies = { Core.get(), graphics };
|
||||
{
|
||||
std::array<fs::path, 5> ifaces = {
|
||||
std::array<fs::path, 6> ifaces = {
|
||||
"interfaces/ImsdMessages.Ui",
|
||||
"interfaces/ImsdMessages.Ui-Loop",
|
||||
"interfaces/ImsdMessages.Ui-Canvas",
|
||||
"interfaces/ImsdMessages.Ui-Compose",
|
||||
"interfaces/ImsdMessages.Ui-Screens",
|
||||
"interfaces/ImsdMessages.Ui-App",
|
||||
};
|
||||
|
|
@ -196,6 +199,7 @@ extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> a
|
|||
cfg.AddTest("Store").Dependencies({ Core.get(), db });
|
||||
cfg.AddTest("Sync").Dependencies({ Core.get() });
|
||||
cfg.AddTest("Gesture").Dependencies({ Core.get() });
|
||||
cfg.AddTest("Edit").Dependencies({ Core.get() });
|
||||
// The bus library's decoding test includes GLib's headers: the library's
|
||||
// compile flags (its link flags come with the library).
|
||||
TestBuilder decode = cfg.AddTest("Decode");
|
||||
|
|
|
|||
177
tests/Edit/main.cpp
Normal file
177
tests/Edit/main.cpp
Normal file
|
|
@ -0,0 +1,177 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
// The message field's editing by code point: one to four bytes of UTF-8,
|
||||
// and the lines of a text with newlines.
|
||||
import std;
|
||||
import ImsdMessages;
|
||||
using namespace ImsdMessages;
|
||||
|
||||
namespace {
|
||||
|
||||
std::int32_t failures = 0;
|
||||
|
||||
void Check(bool ok, std::string_view what) {
|
||||
std::println("{} {}", ok ? "PASS" : "FAIL", what);
|
||||
if (!ok) ++failures;
|
||||
}
|
||||
|
||||
void CheckEqual(std::string_view actual, std::string_view expected, std::string_view what) {
|
||||
const bool ok = actual == expected;
|
||||
if (ok) std::println("PASS {}", what);
|
||||
else std::println("FAIL {} — expected \"{}\", got \"{}\"", what, expected, actual);
|
||||
if (!ok) ++failures;
|
||||
}
|
||||
|
||||
ComposeState At(std::string value, std::size_t cursor) {
|
||||
ComposeState s;
|
||||
s.value = std::move(value);
|
||||
s.cursor = cursor;
|
||||
return s;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
// a, é, € and 😀: one, two, three and four bytes; boundaries 0 1 3 6 10.
|
||||
const std::string mixed = "aé€\U0001F600";
|
||||
Check(mixed.size() == 10, "the sample is ten bytes");
|
||||
Check(NextCodepoint(mixed, 0) == 1 && NextCodepoint(mixed, 1) == 3 && NextCodepoint(mixed, 3) == 6 && NextCodepoint(mixed, 6) == 10, "NextCodepoint steps over one to four bytes");
|
||||
Check(NextCodepoint(mixed, 10) == 10, "NextCodepoint stays at the end");
|
||||
Check(PreviousCodepoint(mixed, 10) == 6 && PreviousCodepoint(mixed, 6) == 3 && PreviousCodepoint(mixed, 3) == 1 && PreviousCodepoint(mixed, 1) == 0, "PreviousCodepoint steps back over one to four bytes");
|
||||
Check(PreviousCodepoint(mixed, 0) == 0, "PreviousCodepoint stays at the start");
|
||||
|
||||
ComposeState s = At(mixed, 10);
|
||||
Check(ComposeEdit(s, EditKey::Backspace) && s.cursor == 6, "Backspace takes the four-byte emoji");
|
||||
CheckEqual(s.value, "aé€", "what is left after Backspace");
|
||||
s = At(mixed, 1);
|
||||
Check(ComposeEdit(s, EditKey::Delete) && s.cursor == 1, "Delete takes the two-byte é");
|
||||
CheckEqual(s.value, "a€\U0001F600", "what is left after Delete");
|
||||
s = At("abc", 0);
|
||||
Check(!ComposeEdit(s, EditKey::Backspace) && s.value == "abc" && s.cursor == 0, "Backspace at the start deletes nothing and says so");
|
||||
s = At("abc", 3);
|
||||
Check(!ComposeEdit(s, EditKey::Delete) && s.value == "abc" && s.cursor == 3, "Delete at the end deletes nothing and says so");
|
||||
|
||||
s = At(mixed, 6);
|
||||
Check(ComposeEdit(s, EditKey::Left) && s.cursor == 3, "Left moves back over the three-byte €");
|
||||
Check(ComposeEdit(s, EditKey::Right) && s.cursor == 6, "Right moves over it again");
|
||||
s = At("ab", 0);
|
||||
Check(ComposeEdit(s, EditKey::Left) && s.cursor == 0, "Left at the start stays, and is still a move");
|
||||
s = At("ab", 2);
|
||||
Check(ComposeEdit(s, EditKey::Right) && s.cursor == 2, "Right at the end stays, and is still a move");
|
||||
|
||||
// "one\ntwo\nthree": the lines start at 0, 4 and 8; the newlines are at 3 and 7.
|
||||
s = At("one\ntwo\nthree", 6);
|
||||
ComposeEdit(s, EditKey::Home);
|
||||
Check(s.cursor == 4, "Home goes to the start of the cursor's line");
|
||||
ComposeEdit(s, EditKey::End);
|
||||
Check(s.cursor == 7, "End goes to the end of the line, before its newline");
|
||||
ComposeEdit(s, EditKey::Home);
|
||||
Check(s.cursor == 4, "Home from the end of a line");
|
||||
s = At("one\ntwo\nthree", 4);
|
||||
ComposeEdit(s, EditKey::Home);
|
||||
Check(s.cursor == 4, "Home at the start of a line stays");
|
||||
s = At("one\ntwo\nthree", 3);
|
||||
ComposeEdit(s, EditKey::Home);
|
||||
Check(s.cursor == 0, "Home from the end of the first line");
|
||||
s = At("one\ntwo\nthree", 9);
|
||||
ComposeEdit(s, EditKey::End);
|
||||
Check(s.cursor == 13, "End on the last line goes to the end of the text");
|
||||
s = At("\nx", 0);
|
||||
ComposeEdit(s, EditKey::Home);
|
||||
Check(s.cursor == 0, "Home at the very start, before a newline, stays");
|
||||
|
||||
s = At("ab", 1);
|
||||
Check(ComposeEdit(s, EditKey::Newline) && s.cursor == 2, "Newline inserts at the cursor and moves past it");
|
||||
CheckEqual(s.value, "a\nb", "the text after Newline");
|
||||
s = At("ab", 1);
|
||||
ComposeInsert(s, "é");
|
||||
Check(s.cursor == 3, "an insert moves the cursor past its bytes");
|
||||
CheckEqual(s.value, "aéb", "the text after an insert");
|
||||
|
||||
// A tap at or past a line's end: the text's end on the last line, the
|
||||
// newline after a hung space before a hard break, the line's end at a
|
||||
// soft wrap (the lines as tests/Wrap pins them, 10 px a glyph, 50 px wide).
|
||||
const std::vector<WrappedLine> hung = { { 0, 5, 50 }, { 7, 8, 10 } }; // "abcde \nx"
|
||||
Check(ComposeLineEnd(hung, 0, "abcde \nx") == 6, "past the end of a line that a hung space and a newline follow: before the newline, after the space");
|
||||
Check(ComposeLineEnd(hung, 1, "abcde \nx") == 8, "past the end of the last line: the text's end");
|
||||
const std::vector<WrappedLine> soft = { { 0, 5, 50 }, { 6, 8, 20 } }; // "ab cd ef"
|
||||
Check(ComposeLineEnd(soft, 0, "ab cd ef") == 5, "at a soft wrap the line's end, before the space that broke it");
|
||||
const std::vector<WrappedLine> trailing = { { 0, 5, 50 } }; // "abcde "
|
||||
Check(ComposeLineEnd(trailing, 0, "abcde ") == 6, "a trailing hung space: the text's end");
|
||||
const std::vector<WrappedLine> ended = { { 0, 2, 20 }, { 3, 3, 0 } }; // "ab\n"
|
||||
Check(ComposeLineEnd(ended, 0, "ab\n") == 2, "past the end of the line before a text's empty last line: before the newline, not on the empty line below");
|
||||
Check(ComposeLineEnd(ended, 1, "ab\n") == 3, "past the empty last line: the text's end");
|
||||
const std::vector<WrappedLine> hungEnd = { { 0, 5, 50 }, { 7, 7, 0 } }; // "abcde \n"
|
||||
Check(ComposeLineEnd(hungEnd, 0, "abcde \n") == 6, "the same with a hung space: after the space, before the newline");
|
||||
|
||||
// The field shows five lines at most and keeps the caret's line among them.
|
||||
Check(ComposeFirstLine(0, 5) == 0 && ComposeFirstLine(4, 5) == 0, "a caret within the first five lines: the field starts at the first");
|
||||
Check(ComposeFirstLine(5, 5) == 1 && ComposeFirstLine(6, 5) == 2, "a caret below them: its line is the fifth shown");
|
||||
Check(ComposeFirstLine(19, 5) == 15, "the same far down a long text");
|
||||
Check(ComposeFirstLine(0, 1) == 0 && ComposeFirstLine(7, 1) == 7, "a one-line field shows the caret's line");
|
||||
|
||||
// The drafts of the conversations left: kept by key, taken up at the
|
||||
// next opening.
|
||||
{
|
||||
Drafts drafts;
|
||||
ComposeState typed;
|
||||
ComposeInsert(typed, "half a thought");
|
||||
typed.cursor = 4;
|
||||
typed.focused = !ComposeState{}.focused;
|
||||
drafts.Keep("+31612345678", typed);
|
||||
Check(drafts.Size() == 1, "a left conversation's text is kept");
|
||||
const ComposeState back = drafts.Take("+31612345678");
|
||||
Check(back.value == "half a thought" && back.cursor == 4 && back.focused == ComposeState{}.focused, "and taken up as typed, the cursor where it was, the focus as a field is opened");
|
||||
Check(drafts.Size() == 0 && drafts.Take("+31612345678").value.empty(), "taken, it is the field's: nothing is kept twice");
|
||||
drafts.Keep("+31612345678", typed);
|
||||
drafts.Keep("+31612345678", ComposeState{});
|
||||
Check(drafts.Take("+31612345678").value == "half a thought", "an empty field keeps nothing and drops nothing");
|
||||
ComposeState other;
|
||||
ComposeInsert(other, "to another");
|
||||
drafts.Keep("+31612345678", typed);
|
||||
drafts.Keep("1234", other);
|
||||
Check(drafts.Size() == 2 && drafts.Take("1234").value == "to another" && drafts.Take("+31612345678").value == "half a thought", "each conversation has its own");
|
||||
drafts.Keep("+31612345678", typed);
|
||||
drafts.Keep("+31612345678", other);
|
||||
const ComposeState both = drafts.Take("+31612345678");
|
||||
Check(both.value == "half a thought\nto another" && both.cursor == both.value.size(), "a second text kept under one key goes after the first, on a line of its own");
|
||||
}
|
||||
// A kept typed address follows imsd's status as an open conversation's
|
||||
// key does: typed before the country code was known, found after.
|
||||
{
|
||||
const SimPlan kpn{ "31", "20408" };
|
||||
const auto nothing = [](std::string_view) { return true; };
|
||||
Drafts drafts;
|
||||
ComposeState typed;
|
||||
ComposeInsert(typed, "typed before the status");
|
||||
drafts.Keep("0612345678", typed, "0612345678", SimPlan{});
|
||||
drafts.Status(kpn, "31", nothing);
|
||||
Check(drafts.Take("0612345678").value.empty() && drafts.Take("+31612345678").value == "typed before the status", "the status brings the code: a kept typed address moves to the key it makes now");
|
||||
drafts.Keep("0612345678", typed, "0612345678", SimPlan{});
|
||||
drafts.Status(kpn, "31", [](std::string_view) { return false; });
|
||||
Check(drafts.Take("0612345678").value == "typed before the status", "not while texts are stored under its key: the conversation stays on the key its rows carry");
|
||||
drafts.Keep("0612345678", typed, "0612345678", SimPlan{ "32", "20601" });
|
||||
drafts.Status(kpn, "31", nothing);
|
||||
Check(drafts.Take("0612345678").value == "typed before the status", "nor under another SIM's status");
|
||||
std::size_t asked = 0;
|
||||
drafts.Keep("0612345678", typed);
|
||||
drafts.Status(kpn, "31", [&asked](std::string_view) {
|
||||
++asked;
|
||||
return true;
|
||||
});
|
||||
Check(asked == 0 && drafts.Take("0612345678").value == "typed before the status", "nor a conversation opened from the list, which has no typed address: the store is not asked about it");
|
||||
drafts.Keep("0612345678", typed, "0612345678", SimPlan{});
|
||||
drafts.Status(kpn, "31", nothing);
|
||||
drafts.Status(SimPlan{ "32", "20601" }, "32", nothing);
|
||||
Check(drafts.Take("+31612345678").value == "typed before the status", "a moved draft keeps the plan it learned: another SIM's status leaves it where it is");
|
||||
ComposeState earlier;
|
||||
ComposeInsert(earlier, "in the thread");
|
||||
drafts.Keep("+31612345678", earlier);
|
||||
drafts.Keep("0612345678", typed, "0612345678", SimPlan{});
|
||||
drafts.Status(kpn, "31", nothing);
|
||||
Check(drafts.Size() == 1 && drafts.Take("+31612345678").value == "in the thread\ntyped before the status", "two keys that became one: both texts, each on its line");
|
||||
}
|
||||
|
||||
return failures == 0 ? 0 : 1;
|
||||
}
|
||||
|
|
@ -143,5 +143,49 @@ int main() {
|
|||
s.registered = false;
|
||||
Check(!CanSend(s), "cannot send unregistered");
|
||||
|
||||
// The address a text goes to: the typed or linked one, else the latest
|
||||
// message's, never the key.
|
||||
Message in;
|
||||
in.conversation = "+31612345678";
|
||||
in.peer = "+31612345678";
|
||||
const SimPlan kpn{ "31", "20408" };
|
||||
const SimPlan vodafoneNl{ "31", "20404" };
|
||||
const SimPlan telekomDe{ "49", "26201" };
|
||||
in.plan = kpn;
|
||||
Message out;
|
||||
out.conversation = "+31612345678";
|
||||
out.peer = "0612345678";
|
||||
out.plan = kpn;
|
||||
out.direction = Direction::Outgoing;
|
||||
const std::vector<Message> thread = { in, out };
|
||||
CheckEqual(SendAddress("06 12", kpn, thread, kpn), "06 12", "the typed or linked address wins");
|
||||
CheckEqual(SendAddress("", {}, thread, kpn), "0612345678", "else the latest message's address, as dialled or as the network gave it");
|
||||
CheckEqual(SendAddress("", {}, thread, {}), "0612345678", "with no plan known now, as stored");
|
||||
CheckEqual(SendAddress("", {}, thread, telekomDe), "", "a national-form peer under another SIM's plan than its row's: no address, Send off");
|
||||
CheckEqual(SendAddress("", {}, thread, vodafoneNl), "0612345678", "under another operator of its country it means the same: as stored");
|
||||
CheckEqual(SendAddress("0612345678", kpn, thread, telekomDe), "", "a typed or linked national-form address under another SIM's plan than it was typed under: none either — the view keeps it across the change");
|
||||
CheckEqual(SendAddress("0612345678", kpn, {}, kpn), "0612345678", "typed under the plan known now: dialled");
|
||||
Message abroad = out;
|
||||
abroad.plan = telekomDe;
|
||||
CheckEqual(SendAddress("0612345678", kpn, std::vector<Message>{ abroad }, kpn), "0612345678", "a typed or linked address is judged by its own plan, whatever plan the latest row was written under");
|
||||
CheckEqual(SendAddress("+31612345678", kpn, thread, telekomDe), "+31612345678", "an international address names its own plan: dialled under any SIM");
|
||||
Message unplanned;
|
||||
unplanned.conversation = "0612345678";
|
||||
unplanned.peer = "0612345678";
|
||||
unplanned.direction = Direction::Outgoing;
|
||||
CheckEqual(SendAddress("", {}, std::vector<Message>{ unplanned }, telekomDe), "0612345678", "a row written under no known plan names none: as stored");
|
||||
Message closed;
|
||||
closed.conversation = "+393477123456";
|
||||
closed.peer = "3477123456";
|
||||
closed.plan = { "39", "22210" };
|
||||
CheckEqual(SendAddress("", {}, std::vector<Message>{ closed }, { "1", "310260" }), "", "a national number of a plan without a trunk prefix under another SIM's plan: none — the row's plan tells");
|
||||
CheckEqual(SendAddress("", {}, std::vector<Message>{ closed }, { "39", "22210" }), "3477123456", "and under its own: as stored");
|
||||
CheckEqual(SendAddress("1234", kpn, {}, telekomDe), "", "a short code typed under another SIM's plan: none");
|
||||
CheckEqual(SendAddress("1234", kpn, {}, vodafoneNl), "", "nor under another operator of the same country: a short code is its operator's");
|
||||
CheckEqual(SendAddress("1234", { "", "20408" }, {}, kpn), "1234", "under the same operator it is dialled, the own number not read when it was typed");
|
||||
CheckEqual(SendAddress("1234", kpn, {}, kpn), "1234", "and read when it was typed");
|
||||
CheckEqual(SendAddress("0612345678", { "", "20408" }, {}, { "44", "23410" }), "", "a national number typed before the own number was read, under another operator: none — the centre's country is not known to be the SIM's");
|
||||
CheckEqual(SendAddress("", {}, {}, kpn), "", "an emptied conversation has no address: never its key");
|
||||
|
||||
return failures == 0 ? 0 : 1;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue