// 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 std::string preedit; // the input method's composing text: shown at the cursor, not yet in the value bool focused = false; }; // The text as shown: the value with the composing text at the cursor. std::string ComposeShown(const ComposeState& s) { std::string shown = s.value; shown.insert(s.cursor, s.preedit); return shown; } std::size_t ShownCursor(const ComposeState& s) { return s.cursor + s.preedit.size(); } // 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(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(); } // The input method's deletion: so many bytes before and after the cursor, // rounded out to code points. The composing text stays; the input method // replaces it when it changes. void ComposeDeleteAround(ComposeState& c, std::size_t beforeBytes, std::size_t afterBytes) { std::size_t from = c.cursor; while (from > 0 && c.cursor - from < beforeBytes) from = PreviousCodepoint(c.value, from); std::size_t to = c.cursor; while (to < c.value.size() && to - c.cursor < afterBytes) to = NextCodepoint(c.value, to); c.value.erase(from, to - from); c.cursor = from; } 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 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; } // Takes the composing text into the value at the cursor, as the input // method would commit it: what Send and Start read is what the field shows. void ComposeCommitPreedit(ComposeState& c) { if (c.preedit.empty()) return; const std::string preedit = std::move(c.preedit); c.preedit.clear(); ComposeInsert(c, preedit); } // 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 or the conversation is // deleted. class Drafts { public: // A conversation is left: what its field holds is kept — a word // still being composed as text — 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 = {}) { ComposeCommitPreedit(field); 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; } // The conversation was deleted: its draft goes with it. void Drop(std::string_view key) { if (const auto kept = byKey.find(key); kept != byKey.end()) byKey.erase(kept); } // 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& nothingStored) { std::vector> 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> byKey; }; }