From 20e687452f49c3fa952d8660d0039f89d2460e9c Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Sun, 1 Nov 2026 18:11:40 +0100 Subject: [PATCH] Ui, main: the new-conversation screen and the long-press sheets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The + opens a screen with a number field (its placeholder "Number", a parameter of the compose field now) and Start; the number becomes a conversation key through Number's ConversationKey, as an sms: link's does (E.164 through the country code, a short code as digits), anything else is refused in place. A long press on a conversation row offers to delete the conversation, on a message to copy its text to the clipboard or delete it; only those two have one: App tells the recogniser before each press whether what it lands on has a long press (GestureDetector's longPress), and without one a press held as long — on a button, a field, a sheet's row — stays a tap at its release, so a slow finger presses the button all the same. A sheet draws on the overlay layer over a scrim and pushes its regions last, so they win the hit test, and the wheel scrolls nothing under it; Escape, the back arrow and a tap outside dismiss it. Deleting a conversation also closes its notification and drops its draft; the + keeps the draft of the conversation it leaves, as Back does, and so does a view that closes because its last message was deleted. The deletes go through Sync (Erase, EraseConversation: the commit before, which keeps a deleted text deleted). The number field's box is drawn by its screen, as the send bar's is by the bar: the shared field drawing paints no background of its own. Opening a conversation, from a notification or a link, leaves the new-conversation screen and any sheet, and the + leaves any sheet. A key whose press changed the screen acts once: held, its repeats are not the new screen's — Enter, which started the conversation, would type line breaks into its field, and Escape would go on back, from a sheet through the conversation and its draft. A sheet that asks to delete a conversation goes when a text arrives for it: a yes would delete that text unread. A press on the conversation's row opens no such sheet either while the row it aimed at had not shown the text — the regions are a frame's: the press that is down when the text arrives, and one that comes down before the next frame is drawn, have no long press; a release before the long-press time is still the row's tap, while the row stays under the finger. --new and --bench-sheet put either on screen for the bench (--bench-sheet conversation or message, each on the screen a long press opens it over: another value is refused, and one that finds nothing to press says so in the log), and are window flags as --screenshot is. Tested: the screens and the sheets by the bench (--new, --bench-sheet) and the phone step's screenshots; the deletes' logic with the commit before, in tests/Store and tests/Sync; the press without a long press in tests/Gesture (the timer gives no long press and a move within the slop nothing; the release is the tap, however late; a slide off the control is a drag); a deleted conversation's draft dropped, in tests/Edit. The press on a row a text arrived for has no bench: none holds a finger on a row. --- implementations/main.cpp | 32 ++- interfaces/ImsdMessages-Edit.cppm | 8 +- interfaces/ImsdMessages-Gesture.cppm | 8 +- interfaces/ImsdMessages.Bus-Notifier.cppm | 2 +- interfaces/ImsdMessages.Ui-App.cppm | 228 ++++++++++++++++++++-- interfaces/ImsdMessages.Ui-Compose.cppm | 10 +- interfaces/ImsdMessages.Ui-Screens.cppm | 83 +++++++- interfaces/ImsdMessages.Ui.cppm | 5 +- tests/Edit/main.cpp | 4 + tests/Gesture/main.cpp | 16 ++ 10 files changed, 358 insertions(+), 38 deletions(-) diff --git a/implementations/main.cpp b/implementations/main.cpp index 6ab17d9..63f93bd 100644 --- a/implementations/main.cpp +++ b/implementations/main.cpp @@ -50,8 +50,10 @@ struct Options { std::optional quitAfter; // seconds std::optional hideAfter; // seconds: the bench's close std::optional hideEvery; // seconds: the bench's repeated close - std::optional prefill; // typed into the opened conversation's field + std::optional prefill; // typed into the open screen's field bool sendDraft = false; // and sent, as if the button were tapped + bool openNew = false; // the bench opens the new-conversation screen + std::optional benchSheet; // "conversation" or "message": the bench's long press std::string country; std::string error; }; @@ -61,7 +63,7 @@ struct Options { // a running instance, whose window is not this one. What configures a // process (its store, its country code) is not among them: beside a // running instance that instance's own stands. -bool WindowFlags(const Options& o) { return o.screenshot || o.prefill || o.sendDraft || o.hideAfter || o.hideEvery; } +bool WindowFlags(const Options& o) { return o.screenshot || o.prefill || o.sendDraft || o.hideAfter || o.hideEvery || o.openNew || o.benchSheet; } // A bench clock's seconds, which its timer takes as a guint of // milliseconds: what is not such a number is the error. @@ -94,6 +96,8 @@ Options Parse(std::span args) { else if (a == "--hide-every" && i + 1 < args.size()) o.hideEvery = Seconds(a, args[++i], o.error); else if (a == "--prefill" && i + 1 < args.size()) o.prefill = args[++i]; else if (a == "--send-draft") o.sendDraft = true; + else if (a == "--new") o.openNew = true; + else if (a == "--bench-sheet" && i + 1 < args.size()) o.benchSheet = args[++i]; else if (a == "--country" && i + 1 < args.size()) o.country = args[++i]; else if (IsSmsUri(a)) o.uri = std::string(a); else o.error = std::format("unknown argument: {}", a); @@ -111,6 +115,7 @@ Options Parse(std::span args) { // completed key is made of it as it is, and read by that rule. if (!o.country.empty() && (o.country.front() == '0' || CountryCodeOf(std::format("+{}0000000", o.country)) != o.country)) o.error = "--country: not a country code (one to three digits, without its +)"; if (o.hideEvery && *o.hideEvery * 1000 < 1) o.error = "--hide-every: a millisecond at least"; + if (o.benchSheet && *o.benchSheet != "conversation" && *o.benchSheet != "message") o.error = "--bench-sheet: conversation or message"; if (o.send && o.quit) o.error = "--send and --quit do not go together"; if (o.send && o.uri) o.error = "--send and an sms: link do not go together"; if (o.send && WindowFlags(o)) o.error = "--send and a window do not go together"; @@ -186,7 +191,7 @@ public: void OnImported(const Message& m) override { if (verbose) Log(std::format("received {} from {}: {} ({} byte(s), segments {})", m.imsdId, m.conversation, m.read ? "read" : "unread", m.text.size(), m.segments)); if (!m.read) notifier.Notify(m); - if (app) app->Changed(); + if (app) app->Imported(m); } void OnMessageChanged(const Message& m) override { @@ -276,7 +281,7 @@ int main(int argc, char** argv) { return 0; } if (o.help) { - std::println("imsd-messages [--daemon|--headless] [--send NUMBER TEXT] [--quit] [--session] [--data-dir DIR] [--country CC] [--screenshot FILE.png] [--screenshot-after SECONDS] [--quit-after SECONDS] [--hide-after SECONDS] [--hide-every SECONDS] [--prefill TEXT] [--send-draft] [sms:NUMBER] [--version]"); + std::println("imsd-messages [--daemon|--headless] [--send NUMBER TEXT] [--quit] [--session] [--data-dir DIR] [--country CC] [--screenshot FILE.png] [--screenshot-after SECONDS] [--quit-after SECONDS] [--hide-after SECONDS] [--hide-every SECONDS] [--prefill TEXT] [--send-draft] [--new] [--bench-sheet conversation|message] [sms:NUMBER] [--version]"); return 0; } @@ -392,6 +397,7 @@ int main(int argc, char** argv) { app->sync = &sync; app->onQuit = [loop] { g_main_loop_quit(loop); }; app->onConversationOpened = [¬ifier](const std::string& conversation) { notifier.Close(conversation); }; + app->onConversationDeleted = [¬ifier](const std::string& conversation) { notifier.Close(conversation); }; app->onLog = Log; // The compositor's connection broke, and the process cannot connect its // display again: it ends (the store flushed, the name freed), so the next @@ -417,6 +423,10 @@ int main(int argc, char** argv) { } else if (!o.daemon) { app->Show(); } + if (o.openNew) { + app->NewConversation(); + if (o.prefill) app->Prefill(*o.prefill); + } // A start that asked for the window and has none (Ui's log said why) // ends with exit 1, as one without its database does; a resident // instance runs on without a window, and the next Activate or Open @@ -427,6 +437,20 @@ int main(int argc, char** argv) { g_main_loop_unref(loop); return 1; } + struct SheetJob { + Ui::App* app; + std::string which; + }; + std::optional sheetJob; + if (o.benchSheet) { + sheetJob.emplace(&*app, *o.benchSheet); + g_timeout_add(2000, [](gpointer data) -> gboolean { + SheetJob& job = *static_cast(data); + job.app->Show(); + job.app->BenchSheet(job.which); + return G_SOURCE_REMOVE; + }, &*sheetJob); + } // The bench's tap on Send, 1.5 s after start. if (o.sendDraft) g_timeout_add(1500, [](gpointer data) -> gboolean { static_cast(data)->SendDraft(); diff --git a/interfaces/ImsdMessages-Edit.cppm b/interfaces/ImsdMessages-Edit.cppm index ad24422..a234f90 100644 --- a/interfaces/ImsdMessages-Edit.cppm +++ b/interfaces/ImsdMessages-Edit.cppm @@ -106,7 +106,8 @@ export namespace ImsdMessages { // 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. + // 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 and comes @@ -140,6 +141,11 @@ export namespace ImsdMessages { 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 diff --git a/interfaces/ImsdMessages-Gesture.cppm b/interfaces/ImsdMessages-Gesture.cppm index 39033a0..40c9d6b 100644 --- a/interfaces/ImsdMessages-Gesture.cppm +++ b/interfaces/ImsdMessages-Gesture.cppm @@ -41,12 +41,14 @@ export namespace ImsdMessages { // the long-press time is a tap; a press held past that time without // moving is a long press (the host runs the timer and calls Timeout); // movement past the slop starts a drag whose release is a fling when it - // was fast enough. + // was fast enough. Where the host says that what the press lands on has + // no long press, a press held as long is still a tap at its release. class GestureDetector { public: float slopPx = 8; std::int64_t longPressMs = 500; float flingMinPxPerS = 60; + bool longPress = true; // whether the press that follows can be a long press: the host's word, before each Down GestureEvent Feed(PointerPhase phase, Point p, std::int64_t tMs) { GestureEvent e; @@ -100,7 +102,7 @@ export namespace ImsdMessages { e.kind = GestureEvent::Kind::Release; } e.at = p; - } else if (!longPressed && tMs - downAt < longPressMs) { + } else if (!longPressed && (!longPress || tMs - downAt < longPressMs)) { e.kind = GestureEvent::Kind::Tap; e.at = down; } else { @@ -122,7 +124,7 @@ export namespace ImsdMessages { // The host's timer at Down + longPressMs. GestureEvent Timeout(std::int64_t tMs) { GestureEvent e; - if (!pressed || dragging || longPressed || tMs - downAt < longPressMs) return e; + if (!longPress || !pressed || dragging || longPressed || tMs - downAt < longPressMs) return e; longPressed = true; e.kind = GestureEvent::Kind::LongPress; e.at = down; diff --git a/interfaces/ImsdMessages.Bus-Notifier.cppm b/interfaces/ImsdMessages.Bus-Notifier.cppm index 1f87fe8..7aa0e58 100644 --- a/interfaces/ImsdMessages.Bus-Notifier.cppm +++ b/interfaces/ImsdMessages.Bus-Notifier.cppm @@ -108,7 +108,7 @@ export namespace ImsdMessages::Bus { g_dbus_connection_call(conn, notifyName, notifyPath, notifyName, "Notify", g_variant_new("(susssasa{sv}i)", "imsd-messages", replaces, std::string(appId).c_str(), summary.c_str(), body.c_str(), &actions, &hints, -1), G_VARIANT_TYPE("(u)"), G_DBUS_CALL_FLAGS_NONE, -1, nullptr, OnNotified, new std::pair(this, m.conversation)); } - // The conversation was opened: its notification goes. + // The conversation was opened or deleted: its notification goes. void Close(const std::string& conversation) { if (!conn) return; waiting.erase(conversation); diff --git a/interfaces/ImsdMessages.Ui-App.cppm b/interfaces/ImsdMessages.Ui-App.cppm index 47d1981..1fe3ab0 100644 --- a/interfaces/ImsdMessages.Ui-App.cppm +++ b/interfaces/ImsdMessages.Ui-App.cppm @@ -67,6 +67,7 @@ export namespace ImsdMessages::Ui { hits.clear(); hitKeys.clear(); hitMessages.clear(); + textSinceFrame.clear(); } static gboolean OnHideIdle(gpointer data) { @@ -102,6 +103,7 @@ export namespace ImsdMessages::Ui { bool closeQuits = false; // no session bus: a close ends the process, since nothing could show the window again std::function onQuit; // the screenshot's run is over, or the window closed with no bus to come back on std::function onConversationOpened; // the notification goes + std::function onConversationDeleted; // so does the notification std::function onLog; std::function onDisplayLost; // the compositor's connection broke std::int32_t exitCode = 0; // what main returns when nothing else decided: 1 for a screenshot not written @@ -165,6 +167,7 @@ export namespace ImsdMessages::Ui { if (gestures.Pressed()) Pointer(PointerPhase::Move); }); scrollSub.emplace(&w.onMouseScroll, [this](std::uint32_t raw) { + if (sheet || fresh) return; // a sheet takes the wheel as it takes the taps; the new-conversation screen scrolls nothing const std::int32_t detents = static_cast(raw); ActiveScroll().ScrollBy(static_cast(detents) * canvas->Dp(48)); if (view) view->stickToBottom = false; @@ -178,15 +181,24 @@ export namespace ImsdMessages::Ui { } }); textSub.emplace(&w.onTextInput, [this](const std::string_view text) { - if (!view) return; - ComposeInsert(view->compose, text); - ShowCaret(); + if (sheet) return; + if (fresh) { + // 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'; }); + ComposeInsert(fresh->field, oneLine); + fresh->error.clear(); + ShowCaret(); + } else if (view) { + ComposeInsert(view->compose, text); + ShowCaret(); + } }); resizeSub.emplace(&w.onResize, [this] { Dirty(); }); closeSub.emplace(&w.onClose, [this] { Close(); }); Reload(); Opened(); - if (view && view->compose.focused) ShowCaret(); // the blink a hide stopped + if (fresh || (view && view->compose.focused)) ShowCaret(); // the blink a hide stopped w.Render(); frames->RequestRedraw(); } @@ -236,6 +248,8 @@ export namespace ImsdMessages::Ui { StopFling(); CancelGesture(); KeepWindow(); + fresh.reset(); + sheet.reset(); if (!view || view->key != key) { KeepDraft(); view.emplace(); @@ -261,6 +275,21 @@ export namespace ImsdMessages::Ui { } void Back() { + // Every exit is a screen change: a press held across it (Escape + // on a keyboard while a finger or a button is down) is cancelled, + // not resolved against what the next frame draws there. + if (sheet) { + CancelGesture(); + sheet.reset(); + Dirty(); + return; + } + if (fresh) { + CancelGesture(); + fresh.reset(); + Dirty(); + return; + } if (!view) return; StopFling(); CancelGesture(); @@ -274,10 +303,48 @@ export namespace ImsdMessages::Ui { Dirty(); } - // Puts text into the open conversation's field (the bench's typing). + // The screen behind the +. + void NewConversation() { + StopFling(); + CancelGesture(); + KeepDraft(); + view.reset(); + sheet.reset(); + fresh.emplace(); + Show(); + if (sync) sync->SetOpenConversation(std::nullopt); + ShowCaret(); + } + + // 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() : ""); + if (key.empty()) { + fresh->error = "Enter a phone number"; + Dirty(); + return; + } + const std::string dialled = DialledNumber(fresh->field.value); + fresh.reset(); + Open(key, dialled); + } + + // 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 (!view) return; - ComposeInsert(view->compose, text); + if (fresh) ComposeInsert(fresh->field, text); + else if (view) ComposeInsert(view->compose, text); + Dirty(); + } + + void BenchSheet(std::string_view which) { + // As a long press opens them: a conversation's sheet over the + // list, a message's over its conversation — and a line when the + // screen has nothing of the kind to press. + 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)); Dirty(); } @@ -342,6 +409,24 @@ export namespace ImsdMessages::Ui { void Changed() { Reload(); } + // A text arrived. A sheet that asks to delete its conversation goes: + // the question was about the texts the list showed, and a yes to it + // would delete this one unread. A press on its row opens no such + // sheet either while the row it aimed at had not shown the text — + // the regions are a frame's: the press that is down now, and one + // that comes down before the next frame is drawn (Pointer), have no + // long press. A release before the long-press time is still the + // row's tap, while the row stays under the finger. + void Imported(const Message& m) { + if (sheet && sheet->kind == Sheet::Kind::DeleteConversation && sheet->key == m.conversation) { + CancelGesture(); + sheet.reset(); + } + textSinceFrame.insert(m.conversation); + if (gestures.Pressed() && pressedOn && pressedOn->id == Hit::rowBase && pressedOn->key == m.conversation) pressGotText = true; + Reload(); + } + private: void Log(std::string_view line) { if (onLog) onLog(line); @@ -419,7 +504,11 @@ export namespace ImsdMessages::Ui { if (frames->Animating()) { if (!ActiveScroll().Tick(std::min(dt, 0.05f))) frames->SetAnimating(false); } - if (view) { + if (fresh) { + Box area = DrawHeader(c, "New message", true, hits); + area = DrawBanner(c, area, status); + fresh->Draw(c, area, hits); + } else if (view) { Box area = DrawHeader(c, DisplayNumber(view->key), true, hits); area = DrawBanner(c, area, status); area = view->DrawComposeBar(c, area, CanSend(status) && !Destination().empty(), hits); @@ -429,12 +518,14 @@ export namespace ImsdMessages::Ui { area = DrawBanner(c, area, status); list.Draw(c, area, convs, now, zone, hits); } + if (sheet) DrawSheet(c, *sheet, hits); // What the regions name, as drawn: a reload before the next frame // re-sorts the vectors, so a gesture resolves against these. hitKeys.clear(); for (const auto& conv : convs) hitKeys.push_back(conv.key); hitMessages.clear(); if (view) for (const Message& m : view->messages) hitMessages.push_back(m.id); + textSinceFrame.clear(); // this frame's regions are of the texts as they are now if (c.Overflowed() && !overflowLogged) { overflowLogged = true; Log("a layer ran out of items this frame; the buffers are too small for this screen"); @@ -449,11 +540,19 @@ export namespace ImsdMessages::Ui { gestures.slopPx = canvas->Dp(8); gestures.flingMinPxPerS = canvas->Dp(60); if (phase == PointerPhase::Down) ActiveScroll().velocity = 0; // a finger down stops the fling under it - if (phase == PointerPhase::Down) pressedOn = Target(p); + if (phase == PointerPhase::Down) { + pressedOn = Target(p); + pressGotText = pressedOn && pressedOn->id == Hit::rowBase && textSinceFrame.contains(pressedOn->key); + // Only a row and a message have a long press. A press on + // anything else — a button, a field, a sheet's row — stays + // that control's tap at its release, however long it was + // held: a slow finger presses the button all the same. + gestures.longPress = pressedOn && pressedOn->id >= Hit::rowBase; + } Handle(gestures.Feed(phase, p, NowMs())); if (phase == PointerPhase::Down) { if (longPressTimer) g_source_remove(longPressTimer); - longPressTimer = g_timeout_add(static_cast(gestures.longPressMs), OnLongPress, this); + longPressTimer = gestures.longPress ? g_timeout_add(static_cast(gestures.longPressMs), OnLongPress, this) : 0; } else if (phase != PointerPhase::Move && longPressTimer) { g_source_remove(longPressTimer); longPressTimer = 0; @@ -468,23 +567,27 @@ export namespace ImsdMessages::Ui { } void Handle(const GestureEvent& e) { + if (sheet && e.kind != GestureEvent::Kind::Tap) return; // A tap or a long press acts on what the press went down on: a // frame drawn meanwhile (a new text snapped the conversation, the // list re-sorted) that moved another bubble or row under the // finger names something else, and the release acts on nothing. if ((e.kind == GestureEvent::Kind::Tap || e.kind == GestureEvent::Kind::LongPress) && Target(e.at) != pressedOn) return; + if (e.kind == GestureEvent::Kind::LongPress && pressGotText) return; // the pressed row had not shown a text of its conversation (Imported) switch (e.kind) { case GestureEvent::Kind::None: return; case GestureEvent::Kind::Tap: Tap(e.at); return; - case GestureEvent::Kind::LongPress: Log("long press (actions come in a later step)"); return; + case GestureEvent::Kind::LongPress: LongPress(e.at); return; case GestureEvent::Kind::DragStart: case GestureEvent::Kind::Drag: + if (fresh) return; // the new-conversation screen scrolls nothing frames->SetAnimating(false); ActiveScroll().ScrollBy(-e.delta.y); if (view) view->stickToBottom = false; Dirty(); return; case GestureEvent::Kind::Fling: + if (fresh) return; ActiveScroll().Fling(-e.velocity.y); frames->SetAnimating(true); return; @@ -505,13 +608,34 @@ export namespace ImsdMessages::Ui { return nullptr; } - // A key: Escape goes back; Ctrl+Enter sends; the rest edits the field. + // A key: Escape goes back; Enter on the number screen starts the + // conversation; Ctrl+Enter sends; the rest edits the field. void Key(Crafter::KeyCode code) { using Crafter::CrafterKeys; + // A key whose press changed the screen acts once: held, the + // library sends it again (its repeat is that key's while it + // runs), and those are the old screen's — Enter's, which + // started the conversation, would type line breaks into its + // field, and Escape's would go on back, from a sheet through + // the conversation and its draft. A new press acts again. + if (spentKey == code) { + if (Crafter::Device::keyRepeat.active && Crafter::Device::keyRepeat.key == code) return; + spentKey.reset(); + } if (code == Crafter::Key(CrafterKeys::Escape)) { + spentKey = code; Back(); return; } + if (sheet) return; + if (fresh) { + if (code == Crafter::Key(CrafterKeys::Enter) || code == Crafter::Key(CrafterKeys::keypad_enter)) { + Start(); + if (!fresh) spentKey = code; // started: the conversation's screen is up + } else if (ComposeKey(fresh->field, code)) ShowCaret(); + if (!repeatTimer) repeatTimer = g_timeout_add(30, OnRepeat, this); + return; + } if (!view) return; const std::unordered_set& held = canvas->Window().heldKeys; const bool ctrl = held.contains(Crafter::Key(CrafterKeys::LeftCtrl)) || held.contains(Crafter::Key(CrafterKeys::RightCtrl)); @@ -539,8 +663,9 @@ export namespace ImsdMessages::Ui { // the window is up (without one nothing draws it; Show starts the // blink again). void ShowCaret() { - if (!view) return; - view->caretVisible = true; + if (!view && !fresh) return; + 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; Dirty(); @@ -548,20 +673,82 @@ export namespace ImsdMessages::Ui { static gboolean OnBlink(gpointer data) { App& self = *static_cast(data); - if (!self.view) { + if (!self.view && !self.fresh) { self.blinkTimer = 0; return G_SOURCE_REMOVE; } - self.view->caretVisible = !self.view->caretVisible; + if (self.view) self.view->caretVisible = !self.view->caretVisible; + if (self.fresh) self.fresh->caretVisible = !self.fresh->caretVisible; self.Dirty(); return G_SOURCE_CONTINUE; } + void LongPress(Point at) { + const std::optional id = HitTest(hits, at); + if (!id) return; + if (*id >= Hit::messageBase && view) { + if (const Message* m = HitMessage(*id - Hit::messageBase)) sheet = Sheet{ Sheet::Kind::MessageActions, view->key, m->id, m->text }; + } else if (*id >= Hit::rowBase && !view && !fresh) { + const std::size_t i = *id - Hit::rowBase; + if (i < hitKeys.size()) sheet = Sheet{ Sheet::Kind::DeleteConversation, hitKeys[i], 0, "" }; + } + Dirty(); + } + + // 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 + // deleted whole takes its draft with it, SheetAction). + void ConversationGone(const std::string& key) { + if (onConversationDeleted) onConversationDeleted(key); + if (view && view->key == key) { + StopFling(); + CancelGesture(); + KeepDraft(); + view.reset(); + if (sync) sync->SetOpenConversation(std::nullopt); + } + } + + void SheetAction(std::uint32_t id) { + if (!sheet) return; + const Sheet s = *sheet; + sheet.reset(); + if (id == Hit::sheetDelete) { + if (s.kind == Sheet::Kind::DeleteConversation) { + if (sync) sync->EraseConversation(s.key); + else store.EraseConversation(s.key); + } else { + if (sync) sync->Erase(s.messageId); + else store.Erase(s.messageId); + } + // Gone once no message is left (the list groups messages) — + // not on a delete the store refused: the rows stay, their + // notification with them, and the banner says why. + if (store.Conversation(s.key).empty()) { + ConversationGone(s.key); + if (s.kind == Sheet::Kind::DeleteConversation) drafts.Drop(s.key); + } + Reload(); + } else if (id == Hit::sheetCopy) { + if (!Crafter::Clipboard::SetText(s.text)) Log("the clipboard did not take the text"); + } + Dirty(); + } + void Tap(Point at) { const std::optional id = HitTest(hits, at); if (!id) return; - if (*id == Hit::back) { + if (*id == Hit::sheetScrim || *id == Hit::sheetCancel || *id == Hit::sheetDelete || *id == Hit::sheetCopy) { + SheetAction(*id); + } else if (*id == Hit::back) { Back(); + } else if (*id == Hit::start) { + Start(); + } else if (*id == Hit::newField) { + if (!fresh) return; + fresh->field.cursor = ComposeHit(*canvas, fresh->field, fresh->fieldLayout, at); + ShowCaret(); } else if (*id == Hit::compose) { if (!view) return; view->compose.focused = true; @@ -570,7 +757,7 @@ export namespace ImsdMessages::Ui { } else if (*id == Hit::send) { SendDraft(); } else if (*id == Hit::fab) { - Log("new conversation (comes in a later step)"); + NewConversation(); } else if (*id >= Hit::messageBase) { if (!view || !sync) return; const Message* m = HitMessage(*id - Hit::messageBase); @@ -590,16 +777,21 @@ export namespace ImsdMessages::Ui { std::vector hitKeys; // the conversation each row region named, as drawn std::vector hitMessages; // the message each bubble region named, as drawn std::optional pressedOn; // what the press went down on, as drawn then + bool pressGotText = false; // the pressed row had not shown a text of its conversation: no long press (Imported) + std::unordered_set textSinceFrame; // the conversations a text arrived for since the regions were recorded std::optional source; std::optional frames; std::optional day; // while the window is up ListScreen list; std::optional view; Drafts drafts; // what was typed in the conversations left and not sent + std::optional fresh; // the new-conversation screen + std::optional sheet; GestureDetector gestures; std::vector hits; guint longPressTimer = 0; guint repeatTimer = 0; + std::optional spentKey; // the key whose press changed the screen (Key) guint blinkTimer = 0; guint hideIdle = 0; // a close's Hide, deferred to the loop std::chrono::steady_clock::time_point lastFrame{}; diff --git a/interfaces/ImsdMessages.Ui-Compose.cppm b/interfaces/ImsdMessages.Ui-Compose.cppm index 9782d99..ae6736c 100644 --- a/interfaces/ImsdMessages.Ui-Compose.cppm +++ b/interfaces/ImsdMessages.Ui-Compose.cppm @@ -51,9 +51,10 @@ export namespace ImsdMessages::Ui { return std::max(1, static_cast(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) { + // The field's text: one to five lines tall, a placeholder when empty, the + // caret at the cursor; the box itself is the caller's, whose screen + // decides its fill and outline. Returns the layout the app needs for taps. + ComposeLayout DrawCompose(Canvas& c, const ComposeState& s, Box field, bool caretVisible, std::string_view placeholder = "Message") { const float px = c.Dp(16); const float padX = c.Dp(12); const float padY = c.Dp(8); @@ -61,9 +62,8 @@ export namespace ImsdMessages::Ui { 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); + c.chrome.Text(placeholder, 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); diff --git a/interfaces/ImsdMessages.Ui-Screens.cppm b/interfaces/ImsdMessages.Ui-Screens.cppm index 0ff927b..5550a37 100644 --- a/interfaces/ImsdMessages.Ui-Screens.cppm +++ b/interfaces/ImsdMessages.Ui-Screens.cppm @@ -2,10 +2,10 @@ // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® /* -ImsdMessages.Ui:Screens — the two screens of the baseline, drawn into the -canvas's layers: the conversation list and one conversation. A screen -lays out and draws in the same pass, records the regions a tap can hit, -and keeps its scroll state; the app decides what a hit means. +ImsdMessages.Ui:Screens — the screens, drawn into the canvas's layers: the +conversation list, one conversation, the new-conversation screen and the +long-press sheets. A screen lays out and draws in the same pass and +records the regions a tap can hit; the app decides what a hit means. */ module; #include "vulkan/vulkan.h" @@ -26,6 +26,13 @@ export namespace ImsdMessages::Ui { constexpr std::uint32_t back = 2; constexpr std::uint32_t compose = 4; constexpr std::uint32_t send = 5; + constexpr std::uint32_t start = 6; + constexpr std::uint32_t newField = 7; + constexpr std::uint32_t sheetScrim = 10; + constexpr std::uint32_t sheetCancel = 11; + constexpr std::uint32_t sheetDelete = 12; + constexpr std::uint32_t sheetCopy = 13; + constexpr std::uint32_t sheetPanel = 14; // the panel beside and between its buttons: inert constexpr std::uint32_t rowBase = 1000; constexpr std::uint32_t messageBase = 100000; } @@ -160,6 +167,7 @@ export namespace ImsdMessages::Ui { 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 }; + c.chrome.Quad(field, c.theme.bg, c.Dp(18)); // the field's box on the surface bar composeLayout = DrawCompose(c, compose, field, caretVisible); hits.push_back({ field, Hit::compose }); const bool enabled = canSend && !compose.value.empty(); @@ -247,4 +255,71 @@ export namespace ImsdMessages::Ui { } } }; + + // The screen behind the +: a number, and Start. + class NewScreen { + public: + ComposeState field; + ComposeLayout fieldLayout; + bool caretVisible = true; + std::string error; + + void Draw(Canvas& c, Box area, std::vector& hits) { + const float x = area.x + c.Dp(16); + const float w = area.w - c.Dp(32); + float y = area.y + c.Dp(20); + c.chrome.Text("To", x, y + c.Ascent(c.Dp(13)), c.Font(), c.Dp(13), c.theme.muted); + y += c.LineHeight(c.Dp(13)) + c.Dp(6); + const Box box{ x, y, w, c.LineHeight(c.Dp(16)) + 2 * c.Dp(8) }; + c.chrome.Quad(box, c.theme.surface, c.Dp(18), c.Dp(1), c.theme.divider); + fieldLayout = DrawCompose(c, field, box, caretVisible, "Number"); + hits.push_back({ box, Hit::newField }); + y = box.Bottom() + c.Dp(8); + const std::string hint = error.empty() ? "Like +31 6 1234 5678 or 06 1234 5678" : error; + c.chrome.Text(c.Wrapper().Ellipsize(hint, c.Dp(13), w), x, y + c.Ascent(c.Dp(13)), c.Font(), c.Dp(13), error.empty() ? c.theme.muted : c.theme.danger); + y += c.LineHeight(c.Dp(13)) + c.Dp(20); + const bool enabled = !field.value.empty(); + const Box button{ x, y, w, c.Dp(44) }; + c.chrome.Quad(button, enabled ? c.theme.accent : c.theme.divider, c.Dp(22)); + c.chrome.Text("Start", button.x + button.w / 2, c.CenteredBaseline(button, c.Dp(16)), c.Font(), c.Dp(16), enabled ? c.theme.onAccent : c.theme.muted, Crafter::TextAlign::Center); + if (enabled) hits.push_back({ button, Hit::start }); + } + }; + + // A bottom sheet over everything: a title, two or three actions, a scrim + // that dismisses. Drawn in the overlay layer; its regions are pushed last, + // so they win the hit test. + struct Sheet { + enum class Kind : std::uint8_t { DeleteConversation, MessageActions }; + Kind kind = Kind::DeleteConversation; + std::string key; // the conversation + std::uint64_t messageId = 0; + std::string text; // the message (for Copy) + }; + + void DrawSheet(Canvas& c, const Sheet& sheet, std::vector& hits) { + const Box all{ 0, 0, c.Width(), c.Height() }; + c.overlay.Quad(all, c.theme.scrim); + hits.push_back({ all, Hit::sheetScrim }); + const bool conversation = sheet.kind == Sheet::Kind::DeleteConversation; + const std::size_t buttons = conversation ? 2 : 3; + const float buttonH = c.Dp(48); + const float panelH = c.Dp(16) + c.LineHeight(c.Dp(16)) + c.Dp(12) + buttonH * static_cast(buttons) + c.Dp(8) * static_cast(buttons - 1) + c.Dp(20); + const Box panel{ 0, c.Height() - panelH, c.Width(), panelH + c.Dp(24) }; + c.overlay.Quad(panel, c.theme.surface, c.Dp(20)); + hits.push_back({ panel, Hit::sheetPanel }); // over the scrim: a tap on the panel itself dismisses nothing + const std::string title = conversation ? std::format("Delete the conversation with {}?", DisplayNumber(sheet.key)) : "Message"; + c.overlay.Text(c.Wrapper().Ellipsize(title, c.Dp(16), panel.w - c.Dp(32)), c.Dp(16), panel.y + c.Dp(16) + c.Ascent(c.Dp(16)), c.Font(), c.Dp(16), c.theme.text); + float y = panel.y + c.Dp(16) + c.LineHeight(c.Dp(16)) + c.Dp(12); + auto button = [&](std::string_view label, Color bg, Color fg, std::uint32_t id) { + const Box b{ c.Dp(16), y, panel.w - c.Dp(32), buttonH }; + c.overlay.Quad(b, bg, c.Dp(24)); + c.overlay.Text(label, b.x + b.w / 2, c.CenteredBaseline(b, c.Dp(16)), c.Font(), c.Dp(16), fg, Crafter::TextAlign::Center); + hits.push_back({ b, id }); + y += buttonH + c.Dp(8); + }; + if (!conversation) button("Copy text", c.theme.bg, c.theme.text, Hit::sheetCopy); + button(conversation ? "Delete conversation" : "Delete message", c.theme.danger, c.theme.onAccent, Hit::sheetDelete); + button("Cancel", c.theme.bg, c.theme.text, Hit::sheetCancel); + } } diff --git a/interfaces/ImsdMessages.Ui.cppm b/interfaces/ImsdMessages.Ui.cppm index f43b8d7..3f62b9f 100644 --- a/interfaces/ImsdMessages.Ui.cppm +++ b/interfaces/ImsdMessages.Ui.cppm @@ -6,8 +6,9 @@ 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, 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. +turning keys into its edits, Screens draw the conversation list, a +conversation, the new-conversation screen and the sheets, App wires them to +the store and to Sync. */ export module ImsdMessages.Ui; export import :Loop; diff --git a/tests/Edit/main.cpp b/tests/Edit/main.cpp index a48c334..6237c8a 100644 --- a/tests/Edit/main.cpp +++ b/tests/Edit/main.cpp @@ -136,6 +136,10 @@ int main() { 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"); + drafts.Keep("+31612345678", typed); + drafts.Drop("+31612345678"); + drafts.Drop("no such conversation"); + Check(drafts.Size() == 0, "a deleted conversation's draft goes with it"); } // A kept typed address follows imsd's status as an open conversation's // key does: typed before the country code was known, found after. diff --git a/tests/Gesture/main.cpp b/tests/Gesture/main.cpp index 92ed520..cc18ed4 100644 --- a/tests/Gesture/main.cpp +++ b/tests/Gesture/main.cpp @@ -60,6 +60,22 @@ int main() { Check(!g.Pressed() && g.Feed(PointerPhase::Up, { 0, 0 }, 5020).kind == Kind::None, "a cancel forgets the press"); Check(g.Feed(PointerPhase::Down, { 5, 5 }, 6000).kind == Kind::None && g.Feed(PointerPhase::Up, { 5, 5 }, 6600).kind == Kind::Release, "a long press released without the timer is a release"); + // A press on what has no long press, the host's word before the Down: + // held as long, it is still a tap at its release. + g.longPress = false; + g.Feed(PointerPhase::Down, { 5, 5 }, 8000); + Check(g.Timeout(8600).kind == Kind::None, "without a long press the timer past the time gives nothing"); + Check(g.Feed(PointerPhase::Move, { 6, 5 }, 8700).kind == Kind::None, "nor does a move within the slop"); + const GestureEvent slow = g.Feed(PointerPhase::Up, { 6, 5 }, 9500); + Check(slow.kind == Kind::Tap && slow.at == Point{ 5, 5 }, "and the release is the tap, however late"); + g.Feed(PointerPhase::Down, { 5, 5 }, 10000); + Check(g.Feed(PointerPhase::Move, { 5, 40 }, 10800).kind == Kind::DragStart, "a finger that slides off past the time drags, as one that slides off before it"); + Check(g.Feed(PointerPhase::Up, { 5, 40 }, 11500).kind == Kind::Release, "and its release taps nothing"); + g.longPress = true; + g.Feed(PointerPhase::Down, { 5, 5 }, 12000); + Check(g.Timeout(12500).kind == Kind::LongPress, "a press the host gives a long press has it as before"); + g.Feed(PointerPhase::Up, { 5, 5 }, 12600); + ScrollState s; s.content = 1000; s.viewport = 400;