Ui, main: closing the window hides it; Activate shows it again

The resident model: a close tears the window down — a fling stops, the
device finishes its frame, the listeners let go, the frame driver and
the canvas go — and keeps the process, the store, the screens and the
bus subscription. Show builds the window again, with a conversation left
open coming back (its caret blinking again; an Open or a Show that lands
between a close and its deferred teardown keeps the window); the Wayland
display source is kept across a hide. A text that arrives for the kept
conversation while the window is hidden — from the close on, not from
its deferred teardown — stays unread and keeps its notification, like
any other conversation's; showing the window again marks it read and
closes the notification. Without a session bus, where nothing could show
the window again, a close ends the process as before. A second launch
reaches the running instance over the bus name and a notification's tap
through its ActionInvoked signal, as before.

--hide-after and --hide-every drive the cycle from the bench as closes
(without a session bus such a close ends the process, as any close does
there), and are window flags as --screenshot is.
This commit is contained in:
Jorijn van der Graaf 2026-10-18 01:16:35 +02:00
commit 07c6c3e5bd
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
2 changed files with 121 additions and 28 deletions

View file

@ -3,7 +3,8 @@
// lint-disable-file no-char-pointer
/*
ImsdMessages.Ui:App — the window as a whole: created on first Show, fed by
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
@ -31,14 +32,22 @@ export namespace ImsdMessages::Ui {
Shutdown();
if (longPressTimer) g_source_remove(longPressTimer);
if (repeatTimer) g_source_remove(repeatTimer);
if (hideIdle) g_source_remove(hideIdle);
}
// Tears the window down in order: the GPU first finishes the frame
// it may still be drawing from the item buffers, then the listeners
// let go of the window, then the frame driver, the display source
// and the canvas go.
// let go of the window, then the frame driver and the canvas go, and
// the display source last.
void Shutdown() {
Teardown();
source.reset();
}
private:
void Teardown() {
if (!canvas) return;
StopFling();
vkDeviceWaitIdle(Crafter::Device::device);
if (blinkTimer) g_source_remove(blinkTimer);
blinkTimer = 0;
@ -53,11 +62,20 @@ export namespace ImsdMessages::Ui {
closeSub.reset();
day.reset();
frames.reset();
source.reset();
canvas.reset();
gestures = GestureDetector{};
hits.clear();
hitKeys.clear();
hitMessages.clear();
}
static gboolean OnHideIdle(gpointer data) {
App& self = *static_cast<App*>(data);
self.hideIdle = 0;
self.Hide();
return G_SOURCE_REMOVE;
}
private:
static gboolean OnScreenshot(gpointer data) {
App& self = *static_cast<App*>(data);
if (self.canvas) {
@ -81,7 +99,8 @@ export namespace ImsdMessages::Ui {
App& operator=(const App&) = delete;
Sync* sync = nullptr;
std::function<void()> onQuit; // the window was closed, or the screenshot's run is over
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(std::string_view)> onLog;
std::function<void()> onDisplayLost; // the compositor's connection broke
@ -89,9 +108,26 @@ export namespace ImsdMessages::Ui {
bool Visible() const noexcept { return canvas.has_value(); }
// Creates the window on the first call.
// Cancels a close's deferred Hide: a Show or an Open before it runs
// keeps the window. True when there was one.
bool KeepWindow() {
if (!hideIdle) return false;
g_source_remove(hideIdle);
hideIdle = 0;
return true;
}
// Whether the window shows the screens: it is up, and no close is
// waiting for its Hide.
bool OnScreen() const noexcept { return canvas.has_value() && hideIdle == 0; }
// Creates the window; shows it again after a Hide.
void Show() {
if (canvas) return;
const bool kept = KeepWindow();
if (canvas) {
if (kept) Opened(); // a close taken back: the kept conversation is on screen again
return;
}
canvas.emplace();
try {
canvas->Create(400, 800, "Messages", assetDir, cacheDir);
@ -106,10 +142,12 @@ export namespace ImsdMessages::Ui {
Log(std::format("no window: {}", e.what()));
return;
}
source.emplace(Crafter::Device::display);
source->onLost = [this] {
if (onDisplayLost) onDisplayLost();
};
if (!source) {
source.emplace(Crafter::Device::display);
source->onLost = [this] {
if (onDisplayLost) onDisplayLost();
};
}
frames.emplace(canvas->Window());
frames->onFrame = [this] { canvas->Window().Update(); };
day.emplace();
@ -145,14 +183,44 @@ export namespace ImsdMessages::Ui {
ShowCaret();
});
resizeSub.emplace(&w.onResize, [this] { Dirty(); });
closeSub.emplace(&w.onClose, [this] {
if (onQuit) onQuit();
});
closeSub.emplace(&w.onClose, [this] { Close(); });
Reload();
Opened();
if (view && view->compose.focused) ShowCaret(); // the blink a hide stopped
w.Render();
frames->RequestRedraw();
}
// The window's close, the compositor's or the bench's (--hide-after,
// --hide-every). Closing hides: the process stays for the next text
// (the resident model); --quit or a signal ends it. The library runs
// onClose from inside its own listener, so the teardown is deferred
// to the loop; a Show before it runs keeps the window. The
// conversation is off the screen from here, not from the Hide: a
// text for it meanwhile is unread, and notified. Without a session
// bus nothing could show the window again, so there a close ends
// the process.
void Close() {
if (!canvas) return;
if (closeQuits) {
Log("window closed; no session bus to come back on: quitting");
if (onQuit) onQuit();
return;
}
if (hideIdle) return;
if (sync) sync->SetOpenConversation(std::nullopt);
hideIdle = g_idle_add(OnHideIdle, this);
}
// Tears the window down and keeps everything else: the store, the
// screens (a conversation left open comes back on Show; its texts
// stay unread meanwhile), the bus.
void Hide() {
if (!canvas) return;
Teardown();
Log("window hidden; still running");
}
// 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 a text
@ -167,8 +235,7 @@ export namespace ImsdMessages::Ui {
key = opening;
StopFling();
CancelGesture();
Show();
if (!canvas) return;
KeepWindow();
if (!view || view->key != key) {
KeepDraft();
view.emplace();
@ -179,10 +246,18 @@ 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 (store.MarkRead(key) > 0) Reload();
if (canvas) Opened();
else Show();
ShowCaret();
}
// The conversation the window shows: read, its notification gone,
// Sync told (a text for it is stored read).
void Opened() {
if (!view) return;
if (store.MarkRead(view->key) > 0) Reload();
if (sync) sync->SetOpenConversation(view->key);
if (onConversationOpened) onConversationOpened(view->key);
ShowCaret();
}
void Back() {
@ -257,9 +332,7 @@ export namespace ImsdMessages::Ui {
// whatever a turn of the wheel did over the empty one.
view->scroll = ScrollState{};
view->stickToBottom = true;
if (store.MarkRead(view->key) > 0) Reload();
if (sync) sync->SetOpenConversation(view->key);
if (onConversationOpened) onConversationOpened(view->key);
if (OnScreen()) Opened(); // hidden, the kept conversation stays unread and keeps its notification: Show opens it
}
}
// The drafts of the typed addresses that were left follow it too.
@ -290,7 +363,9 @@ export namespace ImsdMessages::Ui {
const std::size_t before = view->messages.size();
view->messages = store.Conversation(view->key);
if (view->messages.size() > before) view->stickToBottom = true;
if (sync && store.MarkRead(view->key) > 0) convs = GroupConversations(store.All());
// Shown texts are read; a conversation kept across a hide is
// not on screen, and its new texts stay unread until it is.
if (OnScreen() && sync && store.MarkRead(view->key) > 0) convs = GroupConversations(store.All());
}
Dirty();
}
@ -369,7 +444,8 @@ export namespace ImsdMessages::Ui {
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
// window's scale like every other distance, set at the point of use.
// window's scale like every other distance, set at the point of use,
// so a fresh detector after a teardown has them too.
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
@ -459,12 +535,14 @@ export namespace ImsdMessages::Ui {
return G_SOURCE_CONTINUE;
}
// The caret shows at once after an edit and blinks from there.
// 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).
void ShowCaret() {
if (!view) return;
view->caretVisible = true;
if (blinkTimer) g_source_remove(blinkTimer);
blinkTimer = g_timeout_add(500, OnBlink, this);
blinkTimer = canvas ? g_timeout_add(500, OnBlink, this) : 0;
Dirty();
}
@ -523,6 +601,7 @@ export namespace ImsdMessages::Ui {
guint longPressTimer = 0;
guint repeatTimer = 0;
guint blinkTimer = 0;
guint hideIdle = 0; // a close's Hide, deferred to the loop
std::chrono::steady_clock::time_point lastFrame{};
bool overflowLogged = false;
std::filesystem::path screenshotPath;