Ui, main: the new-conversation screen and the long-press sheets

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.
This commit is contained in:
Jorijn van der Graaf 2026-11-01 18:11:40 +01:00
commit 20e687452f
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
10 changed files with 358 additions and 38 deletions

View file

@ -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<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
std::function<void(std::string_view)> onLog;
std::function<void()> 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<std::int32_t>(raw);
ActiveScroll().ScrollBy(static_cast<float>(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<guint>(gestures.longPressMs), OnLongPress, this);
longPressTimer = gestures.longPress ? g_timeout_add(static_cast<guint>(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<Crafter::KeyCode>& 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<App*>(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<std::uint32_t> 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<std::uint32_t> 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<std::string> hitKeys; // the conversation each row region named, as drawn
std::vector<std::uint64_t> hitMessages; // the message each bubble region named, as drawn
std::optional<HitTarget> 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<std::string> textSinceFrame; // the conversations a text arrived for since the regions were recorded
std::optional<WaylandSource> source;
std::optional<FrameDriver> frames;
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
std::optional<NewScreen> fresh; // the new-conversation screen
std::optional<Sheet> sheet;
GestureDetector gestures;
std::vector<HitRegion> hits;
guint longPressTimer = 0;
guint repeatTimer = 0;
std::optional<Crafter::KeyCode> 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{};