Ui, main: the input method, the app id, and touch through the pointer path

With the Crafter.Graphics branch delivering touch (the first finger as the
pointer), zwp_text_input_v3 and xdg app ids, the app:
- sets its app id on the window;
- tells the compositor which field has the focus and where its caret is,
  after every frame that changed it: the number field at once (purpose
  phone), the message field on a tap or a key; a sheet or leaving the
  screen takes it back;
- shows the input method's composing text in the accent colour at the
  cursor, each run placed at the advance the one before it was drawn
  with, applies its deletions by code point and leaves the composing
  text to the input method, and takes its committed text as typed (the
  composing text and its deletions are the core's Edit, in tests/Edit; a
  draft kept for a conversation that is left keeps a word still being
  composed as text); a tap inside the composing text leaves the cursor
  where it is, a tap elsewhere in the field commits the word where it
  was typed and resets the input method, as Send does, so the half-typed
  word does not move to the tap; a sheet, a hide and leaving a screen by
  Open (over the number screen or over another conversation), by Back or
  by the + end the composition the same way and at once (EndComposition:
  the word committed, the input method told), since the input method
  starts afresh after, and left on until the next frame it would send
  its next commit to the screen that opens or have it dropped. The
  message field starts unfocused, as on a phone, and the caret blinks
  only in a focused field;
- sends what the field shows: Send, like Start, reads the shown text
  once, the composing word included, and clears the field only once the
  store took it (a field of nothing but spaces and line breaks is
  cleared with nothing sent); the field keeps the focus while the input
  method is reset (disabled, enabled again the next frame) — a refused
  text leaves the field and the composing word as they are; Start reads
  the shown text once — the key and the address dialled come from it —
  so a refused number leaves the composing word to the input method;
- logs the window's size on every resize, and which conversation was
  opened — under --headless; a plain start names no correspondent in its
  log, as the rest of the app does not.
Touch takes the mouse's gesture path, and a touch the compositor cancels
(a gesture of its own) arrives as a release without the finger's
touch-up: it is fed to the gestures as a cancel, so it taps nothing.

Tested: tests/Edit (the composing text kept by a deletion, and put into
the field's text by ComposeCommitPreedit).
This commit is contained in:
Jorijn van der Graaf 2026-10-18 02:30:01 +02:00
commit f93144ae2f
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
7 changed files with 325 additions and 66 deletions

View file

@ -6,9 +6,10 @@
ImsdMessages.Ui:App — the window as a whole: created on the first Show and
again after a Hide, fed by
Sync through the shell, drawn from the store on demand. Mouse events
become gestures (touch joins them once Crafter.Graphics delivers it), a
tap is resolved against the regions the last frame recorded, scrolling is
by wheel, drag or fling, and nothing is drawn while nothing changes.
become gestures (the library's first finger drives the pointer; a touch
the compositor cancels is a cancel), a tap is resolved against the
regions the last frame recorded, scrolling is by wheel, drag or fling,
and nothing is drawn while nothing changes.
*/
module;
#include <gio/gio.h>
@ -48,16 +49,26 @@ export namespace ImsdMessages::Ui {
void Teardown() {
if (!canvas) return;
StopFling();
// The composition ends with the window: committed into the field,
// and the input method told (the disable Send's reset sends), so
// it does not carry the word into the next window.
EndComposition();
vkDeviceWaitIdle(Crafter::Device::device);
if (blinkTimer) g_source_remove(blinkTimer);
blinkTimer = 0;
downSub.reset();
upSub.reset();
touchDownSub.reset();
touchUpSub.reset();
touchCancelSub.reset();
touch.reset();
moveSub.reset();
scrollSub.reset();
keySub.reset();
keyUpSub.reset();
textSub.reset();
preeditSub.reset();
deleteSub.reset();
resizeSub.reset();
closeSub.reset();
day.reset();
@ -101,6 +112,8 @@ export namespace ImsdMessages::Ui {
Sync* sync = nullptr;
bool closeQuits = false; // no session bus: a close ends the process, since nothing could show the window again
bool verbose = false; // --headless: log lines may name a correspondent
std::string appId = "net.catcrafts.ImsdMessages"; // the xdg app id; main sets it from the bus layer's constant
std::function<void()> onQuit; // the screenshot's run is over, or the window closed with no bus to come back on
std::function<void(const std::string&)> onConversationOpened; // the notification goes
std::function<void(const std::string&)> onConversationDeleted; // so does the notification
@ -132,7 +145,7 @@ export namespace ImsdMessages::Ui {
}
canvas.emplace();
try {
canvas->Create(400, 800, "Messages", assetDir, cacheDir);
canvas->Create(400, 800, "Messages", appId, assetDir, cacheDir);
} catch (const std::exception& e) {
// No display, a compositor without a global the library
// needs, no Vulkan device: the library throws, and the app
@ -163,7 +176,27 @@ export namespace ImsdMessages::Ui {
canvas->onDraw = [this](VkCommandBuffer cmd) { Build(cmd); };
Crafter::Window& w = canvas->Window();
downSub.emplace(&w.onMouseLeftClick, [this] { Pointer(PointerPhase::Down); });
upSub.emplace(&w.onMouseLeftRelease, [this] { Pointer(PointerPhase::Up); });
// The first finger drives the pointer. A touch the compositor
// cancels (a gesture of its own) arrives as a release without the
// finger's touch-up: a cancel, which taps nothing.
touchDownSub.emplace(&w.onTouchDown, [this](Crafter::TouchPoint t) {
if (canvas->Window().primaryTouchId < 0) {
touch = t.id;
touchUp = false;
}
});
touchUpSub.emplace(&w.onTouchUp, [this](Crafter::TouchPoint t) {
if (touch && t.id == *touch) touchUp = true;
});
upSub.emplace(&w.onMouseLeftRelease, [this] {
const bool cancelled = touch && !touchUp;
touch.reset();
Pointer(cancelled ? PointerPhase::Cancel : PointerPhase::Up);
});
touchCancelSub.emplace(&w.onTouchCancel, [this] {
touch.reset();
if (gestures.Pressed()) Pointer(PointerPhase::Cancel);
});
moveSub.emplace(&w.onMouseMove, [this] {
if (gestures.Pressed()) Pointer(PointerPhase::Move);
});
@ -187,15 +220,37 @@ export namespace ImsdMessages::Ui {
// One line: a pasted number's line breaks are dropped, the rest kept.
std::string oneLine(text);
std::erase_if(oneLine, [](char c) { return c == '\n' || c == '\r'; });
fresh->field.preedit.clear();
ComposeInsert(fresh->field, oneLine);
fresh->error.clear();
ShowCaret();
} else if (view) {
view->compose.preedit.clear();
view->compose.focused = true;
ComposeInsert(view->compose, text);
ShowCaret();
}
});
resizeSub.emplace(&w.onResize, [this] { Dirty(); });
// The input method's composing text and its deletions; what it
// commits arrives as text input above.
preeditSub.emplace(&w.onPreedit, [this](const std::string_view text) {
if (sheet) return;
if (ComposeState* field = FocusedField()) {
field->preedit = std::string(text);
ShowCaret();
}
});
deleteSub.emplace(&w.onTextInputDelete, [this](Crafter::TextDelete d) {
if (sheet) return;
if (ComposeState* field = FocusedField()) {
ComposeDeleteAround(*field, d.beforeBytes, d.afterBytes);
ShowCaret();
}
});
resizeSub.emplace(&w.onResize, [this] {
if (verbose) Log(std::format("window {}x{} px", canvas->Window().width, canvas->Window().height));
Dirty();
});
closeSub.emplace(&w.onClose, [this] { Close(); });
Reload();
Opened();
@ -246,6 +301,9 @@ export namespace ImsdMessages::Ui {
// owned before anything runs.
const std::string opening(key);
key = opening;
// Over the number screen, or over another conversation: the
// field that goes ends its composition first.
if (fresh || (view && view->key != key)) EndComposition();
StopFling();
CancelGesture();
KeepWindow();
@ -261,6 +319,8 @@ export namespace ImsdMessages::Ui {
view->dialledPlan = sync ? sync->Plan() : SimPlan{}; // for an address typed or linked now: the SIM's as known now
view->stickToBottom = true;
view->messages = store.Conversation(key);
if (verbose) Log(std::format("opened {}", key));
else Log("opened a conversation");
if (canvas) Opened();
else Show();
ShowCaret();
@ -287,6 +347,7 @@ export namespace ImsdMessages::Ui {
}
if (fresh) {
CancelGesture();
EndComposition();
fresh.reset();
Dirty();
return;
@ -294,6 +355,7 @@ export namespace ImsdMessages::Ui {
if (!view) return;
StopFling();
CancelGesture();
EndComposition();
KeepDraft();
view.reset();
if (blinkTimer) {
@ -308,10 +370,12 @@ export namespace ImsdMessages::Ui {
void NewConversation() {
StopFling();
CancelGesture();
EndComposition();
KeepDraft();
view.reset();
sheet.reset();
fresh.emplace();
fresh->field.focused = true;
Show();
if (sync) sync->SetOpenConversation(std::nullopt);
ShowCaret();
@ -320,13 +384,17 @@ export namespace ImsdMessages::Ui {
// Start, from the new-conversation screen: a number becomes a key.
void Start() {
if (!fresh) return;
const std::string key = ConversationKey(fresh->field.value, sync ? sync->CountryCode() : "");
// One reading of the field, the composing text included: the key
// and the address dialled come from the same digits.
const std::string shown = ComposeShown(fresh->field);
const std::string key = ConversationKey(shown, sync ? sync->CountryCode() : "");
if (key.empty()) {
fresh->error = "Enter a phone number";
Dirty();
return;
}
const std::string dialled = DialledNumber(fresh->field.value);
const std::string dialled = DialledNumber(shown);
ResetTextInput(); // the composing text is used up: told before the screen changes, as Send does
fresh.reset();
Open(key, dialled);
}
@ -334,9 +402,13 @@ export namespace ImsdMessages::Ui {
// The bench's hands: types into the field of the open screen, and
// opens a sheet as a long press would.
void Prefill(std::string_view text) {
if (fresh) ComposeInsert(fresh->field, text);
else if (view) ComposeInsert(view->compose, text);
Dirty();
if (fresh) {
ComposeInsert(fresh->field, text);
} else if (view) {
view->compose.focused = true;
ComposeInsert(view->compose, text);
}
ShowCaret();
}
void BenchSheet(std::string_view which) {
@ -346,6 +418,7 @@ export namespace ImsdMessages::Ui {
if (which == "conversation" && !view && !fresh && !convs.empty()) sheet = Sheet{ Sheet::Kind::DeleteConversation, convs.front().key, 0, "" };
else if (which == "message" && view && !view->messages.empty()) sheet = Sheet{ Sheet::Kind::MessageActions, view->key, view->messages.front().id, view->messages.front().text };
else Log(std::format("--bench-sheet {}: nothing on this screen to press", which));
if (sheet) EndComposition();
Dirty();
}
@ -355,14 +428,21 @@ export namespace ImsdMessages::Ui {
// 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.
// Sends what the field shows, a word the input method is still
// composing included, to the open conversation. The field keeps the
// focus for the next text, and the input method starts afresh.
void SendDraft() {
if (!view || !sync || view->compose.value.empty() || !CanSend(status) || Destination().empty()) return;
std::string text = view->compose.value;
if (!view || !sync || !CanSend(status) || Destination().empty()) return;
// One reading of the shown text, the composing word included, as
// Start takes it: a store that refuses the text (its disk full)
// leaves the field and the composing word as they are; taken, the
// field is cleared and the input method told its text is used up.
std::string text = ComposeShown(view->compose);
if (text.empty()) return;
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->compose = ComposeState{ .focused = true };
ResetTextInput();
view->stickToBottom = true;
Dirty();
}
@ -520,6 +600,7 @@ export namespace ImsdMessages::Ui {
list.Draw(c, area, convs, now, zone, hits);
}
if (sheet) DrawSheet(c, *sheet, hits);
UpdateTextInput();
// What the regions name, as drawn: a reload before the next frame
// re-sorts the vectors, so a gesture resolves against these.
hitKeys.clear();
@ -533,6 +614,46 @@ export namespace ImsdMessages::Ui {
}
}
ComposeState* FocusedField() {
if (fresh) return &fresh->field;
if (view && view->compose.focused) return &view->compose;
return nullptr;
}
struct TextInputState {
bool active = false;
Box caret{};
Crafter::TextInputPurpose purpose = Crafter::TextInputPurpose::Normal;
};
// The word the input method is composing goes into its field, as a
// tap elsewhere does: once the text input is off, the input method
// starts afresh and would replace or drop it.
void CommitComposition() {
if (fresh) ComposeCommitPreedit(fresh->field);
if (view) ComposeCommitPreedit(view->compose);
}
// Tells the compositor which field has the focus and where its caret
// is, after every frame, when that changed: the number field at once,
// the message field after a tap or a key; a sheet or leaving the
// screen takes it back.
void UpdateTextInput() {
TextInputState want;
if (!sheet) {
if (fresh) want = { true, fresh->fieldLayout.caret, Crafter::TextInputPurpose::Phone };
else if (view && view->compose.focused) want = { true, view->composeLayout.caret, Crafter::TextInputPurpose::Normal };
}
const bool sameCaret = want.caret.x == lastTextInput.caret.x && want.caret.y == lastTextInput.caret.y && want.caret.w == lastTextInput.caret.w && want.caret.h == lastTextInput.caret.h;
if (want.active == lastTextInput.active && want.purpose == lastTextInput.purpose && (!want.active || sameCaret)) return;
if (want.active != lastTextInput.active) Log(want.active ? (want.purpose == Crafter::TextInputPurpose::Phone ? "text input on (a number)" : "text input on") : "text input off");
// Turning the input off ends the composition on the input
// method's side; the field keeps the word it showed.
if (!want.active && lastTextInput.active) CommitComposition();
lastTextInput = want;
canvas->Window().SetTextInput(want.active, static_cast<std::int32_t>(want.caret.x), static_cast<std::int32_t>(want.caret.y), static_cast<std::int32_t>(want.caret.w), static_cast<std::int32_t>(want.caret.h), want.purpose);
}
void Pointer(PointerPhase phase) {
const Point p{ canvas->Window().currentMousePos.x, canvas->Window().currentMousePos.y };
// Buffer pixels: the slop and the fling threshold follow the
@ -644,7 +765,10 @@ export namespace ImsdMessages::Ui {
SendDraft();
return;
}
if (ComposeKey(view->compose, code)) ShowCaret();
if (ComposeKey(view->compose, code)) {
view->compose.focused = true;
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);
@ -660,21 +784,46 @@ export namespace ImsdMessages::Ui {
return G_SOURCE_CONTINUE;
}
// The caret shows at once after an edit and blinks from there while
// the window is up (without one nothing draws it; Show starts the
// blink again).
// A tap places the cursor. Inside the composing text it stays where it
// is (that span is the input method's); outside, the word is committed
// where it was typed and the input method reset, as Send does, so the
// half-typed word does not move to the tap.
void TapField(ComposeState& s, const ComposeLayout& layout, Point at) {
const std::size_t shown = ComposeHit(*canvas, s, layout, at);
if (!s.preedit.empty()) {
if (shown > s.cursor && shown < s.cursor + s.preedit.size()) return;
ComposeCommitPreedit(s);
ResetTextInput();
}
s.cursor = shown;
}
// Disables the text input now and lets the next frame enable it
// again: an enable resets the input method, the word it was composing
// included.
void ResetTextInput() {
if (!canvas || !lastTextInput.active) return;
canvas->Window().SetTextInput(false, 0, 0, 0, 0, lastTextInput.purpose);
lastTextInput = {};
}
// The caret shows at once after an edit and blinks from there while a
// field has the focus and the window is up (without one nothing
// draws it; Show starts the blink again).
void ShowCaret() {
if (!view && !fresh) return;
if (blinkTimer) {
g_source_remove(blinkTimer);
blinkTimer = 0;
}
if (view) view->caretVisible = true;
if (fresh) fresh->caretVisible = true;
if (blinkTimer) g_source_remove(blinkTimer);
blinkTimer = canvas ? g_timeout_add(500, OnBlink, this) : 0;
if (canvas && FocusedField()) blinkTimer = g_timeout_add(500, OnBlink, this);
Dirty();
}
static gboolean OnBlink(gpointer data) {
App& self = *static_cast<App*>(data);
if (!self.view && !self.fresh) {
if (!self.FocusedField()) {
self.blinkTimer = 0;
return G_SOURCE_REMOVE;
}
@ -693,9 +842,23 @@ export namespace ImsdMessages::Ui {
const std::size_t i = *id - Hit::rowBase;
if (i < hitKeys.size()) sheet = Sheet{ Sheet::Kind::DeleteConversation, hitKeys[i], 0, "" };
}
if (sheet) EndComposition();
Dirty();
}
// Leaving a screen with its field (Open, Back, the +), or a sheet
// over the screen, ends the composition of the field that had the
// text input, now and not at the next frame's UpdateTextInput: the
// word goes into the field as shown and the input method is told.
// Left on for that frame, it would send its next commit to the
// screen that opens — into another conversation's field — or have
// it dropped on a sheet.
void EndComposition() {
if (!lastTextInput.active) return;
CommitComposition();
ResetTextInput();
}
// A conversation that no longer exists: its notification goes, and
// the view on it closes, its field's text kept as at any leave (a
// view closes when its last message is deleted; a conversation
@ -748,12 +911,12 @@ export namespace ImsdMessages::Ui {
Start();
} else if (*id == Hit::newField) {
if (!fresh) return;
fresh->field.cursor = ComposeHit(*canvas, fresh->field, fresh->fieldLayout, at);
TapField(fresh->field, fresh->fieldLayout, at);
ShowCaret();
} else if (*id == Hit::compose) {
if (!view) return;
view->compose.focused = true;
view->compose.cursor = ComposeHit(*canvas, view->compose, view->composeLayout, at);
TapField(view->compose, view->composeLayout, at);
ShowCaret();
} else if (*id == Hit::send) {
SendDraft();
@ -800,11 +963,19 @@ export namespace ImsdMessages::Ui {
std::filesystem::path screenshotPath;
std::optional<Crafter::EventListener<void>> downSub;
std::optional<Crafter::EventListener<void>> upSub;
std::optional<Crafter::EventListener<Crafter::TouchPoint>> touchDownSub;
std::optional<Crafter::EventListener<Crafter::TouchPoint>> touchUpSub;
std::optional<Crafter::EventListener<void>> touchCancelSub;
std::optional<std::int32_t> touch; // the finger that drives the pointer
bool touchUp = false; // its touch-up came before the release
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<const std::string_view>> preeditSub;
std::optional<Crafter::EventListener<Crafter::TextDelete>> deleteSub;
TextInputState lastTextInput;
std::optional<Crafter::EventListener<void>> resizeSub;
std::optional<Crafter::EventListener<void>> closeSub;
};