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

@ -48,6 +48,8 @@ struct Options {
std::optional<fs::path> screenshot; std::optional<fs::path> screenshot;
double screenshotAfter = 2.5; // seconds after start double screenshotAfter = 2.5; // seconds after start
std::optional<double> quitAfter; // seconds std::optional<double> quitAfter; // seconds
std::optional<double> hideAfter; // seconds: the bench's close
std::optional<double> hideEvery; // seconds: the bench's repeated close
std::optional<std::string> prefill; // typed into the opened conversation's field std::optional<std::string> prefill; // typed into the opened conversation's field
bool sendDraft = false; // and sent, as if the button were tapped bool sendDraft = false; // and sent, as if the button were tapped
std::string country; std::string country;
@ -59,7 +61,7 @@ struct Options {
// a running instance, whose window is not this one. What configures a // a running instance, whose window is not this one. What configures a
// process (its store, its country code) is not among them: beside a // process (its store, its country code) is not among them: beside a
// running instance that instance's own stands. // running instance that instance's own stands.
bool WindowFlags(const Options& o) { return o.screenshot || o.prefill || o.sendDraft; } bool WindowFlags(const Options& o) { return o.screenshot || o.prefill || o.sendDraft || o.hideAfter || o.hideEvery; }
// A bench clock's seconds, which its timer takes as a guint of // A bench clock's seconds, which its timer takes as a guint of
// milliseconds: what is not such a number is the error. // milliseconds: what is not such a number is the error.
@ -88,6 +90,8 @@ Options Parse(std::span<const char* const> args) {
else if (a == "--screenshot" && i + 1 < args.size()) o.screenshot = fs::path(args[++i]); else if (a == "--screenshot" && i + 1 < args.size()) o.screenshot = fs::path(args[++i]);
else if (a == "--screenshot-after" && i + 1 < args.size()) o.screenshotAfter = Seconds(a, args[++i], o.error); else if (a == "--screenshot-after" && i + 1 < args.size()) o.screenshotAfter = Seconds(a, args[++i], o.error);
else if (a == "--quit-after" && i + 1 < args.size()) o.quitAfter = Seconds(a, args[++i], o.error); else if (a == "--quit-after" && i + 1 < args.size()) o.quitAfter = Seconds(a, args[++i], o.error);
else if (a == "--hide-after" && i + 1 < args.size()) o.hideAfter = Seconds(a, args[++i], o.error);
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 == "--prefill" && i + 1 < args.size()) o.prefill = args[++i];
else if (a == "--send-draft") o.sendDraft = true; else if (a == "--send-draft") o.sendDraft = true;
else if (a == "--country" && i + 1 < args.size()) o.country = args[++i]; else if (a == "--country" && i + 1 < args.size()) o.country = args[++i];
@ -106,6 +110,7 @@ Options Parse(std::span<const char* const> args) {
// and CountryCodeOf gives it back from a number that begins with it): a // and CountryCodeOf gives it back from a number that begins with it): a
// completed key is made of it as it is, and read by that rule. // 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.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.send && o.quit) o.error = "--send and --quit do not go together"; 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 && 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"; if (o.send && WindowFlags(o)) o.error = "--send and a window do not go together";
@ -271,7 +276,7 @@ int main(int argc, char** argv) {
return 0; return 0;
} }
if (o.help) { 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] [--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] [sms:NUMBER] [--version]");
return 0; return 0;
} }
@ -364,6 +369,7 @@ int main(int argc, char** argv) {
shell.sync = &sync; shell.sync = &sync;
app.emplace(*store, AssetDir(), CacheDir()); app.emplace(*store, AssetDir(), CacheDir());
if (o.screenshot) app->exitCode = 1; // an asked-for screenshot is a failure until the PNG is written: a run that ends first says so if (o.screenshot) app->exitCode = 1; // an asked-for screenshot is a failure until the PNG is written: a run that ends first says so
app->closeQuits = session == nullptr;
app->sync = &sync; app->sync = &sync;
app->onQuit = [loop] { g_main_loop_quit(loop); }; app->onQuit = [loop] { g_main_loop_quit(loop); };
app->onConversationOpened = [&notifier](const std::string& conversation) { notifier.Close(conversation); }; app->onConversationOpened = [&notifier](const std::string& conversation) { notifier.Close(conversation); };
@ -427,6 +433,14 @@ int main(int argc, char** argv) {
g_unix_signal_add(SIGINT, OnQuitSignal, &shell); g_unix_signal_add(SIGINT, OnQuitSignal, &shell);
g_unix_signal_add(SIGTERM, OnQuitSignal, &shell); g_unix_signal_add(SIGTERM, OnQuitSignal, &shell);
if (o.send) g_timeout_add_seconds(60, OnSendTimeout, &shell); if (o.send) g_timeout_add_seconds(60, OnSendTimeout, &shell);
if (o.hideAfter) g_timeout_add(static_cast<guint>(*o.hideAfter * 1000), [](gpointer data) -> gboolean {
static_cast<Ui::App*>(data)->Close();
return G_SOURCE_REMOVE;
}, &*app);
if (o.hideEvery) g_timeout_add(static_cast<guint>(*o.hideEvery * 1000), [](gpointer data) -> gboolean {
static_cast<Ui::App*>(data)->Close();
return G_SOURCE_CONTINUE;
}, &*app);
// A bench's clock: the smoke tests on the phone end on their own. // A bench's clock: the smoke tests on the phone end on their own.
if (o.quitAfter) g_timeout_add(static_cast<guint>(*o.quitAfter * 1000), [](gpointer data) -> gboolean { if (o.quitAfter) g_timeout_add(static_cast<guint>(*o.quitAfter * 1000), [](gpointer data) -> gboolean {
static_cast<Shell*>(data)->QuitAfter(); static_cast<Shell*>(data)->QuitAfter();

View file

@ -3,7 +3,8 @@
// lint-disable-file no-char-pointer // 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 Sync through the shell, drawn from the store on demand. Mouse events
become gestures (touch joins them once Crafter.Graphics delivers it), a become gestures (touch joins them once Crafter.Graphics delivers it), a
tap is resolved against the regions the last frame recorded, scrolling is tap is resolved against the regions the last frame recorded, scrolling is
@ -31,14 +32,22 @@ export namespace ImsdMessages::Ui {
Shutdown(); Shutdown();
if (longPressTimer) g_source_remove(longPressTimer); if (longPressTimer) g_source_remove(longPressTimer);
if (repeatTimer) g_source_remove(repeatTimer); if (repeatTimer) g_source_remove(repeatTimer);
if (hideIdle) g_source_remove(hideIdle);
} }
// Tears the window down in order: the GPU first finishes the frame // Tears the window down in order: the GPU first finishes the frame
// it may still be drawing from the item buffers, then the listeners // it may still be drawing from the item buffers, then the listeners
// let go of the window, then the frame driver, the display source // let go of the window, then the frame driver and the canvas go, and
// and the canvas go. // the display source last.
void Shutdown() { void Shutdown() {
Teardown();
source.reset();
}
private:
void Teardown() {
if (!canvas) return; if (!canvas) return;
StopFling();
vkDeviceWaitIdle(Crafter::Device::device); vkDeviceWaitIdle(Crafter::Device::device);
if (blinkTimer) g_source_remove(blinkTimer); if (blinkTimer) g_source_remove(blinkTimer);
blinkTimer = 0; blinkTimer = 0;
@ -53,11 +62,20 @@ export namespace ImsdMessages::Ui {
closeSub.reset(); closeSub.reset();
day.reset(); day.reset();
frames.reset(); frames.reset();
source.reset();
canvas.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) { static gboolean OnScreenshot(gpointer data) {
App& self = *static_cast<App*>(data); App& self = *static_cast<App*>(data);
if (self.canvas) { if (self.canvas) {
@ -81,7 +99,8 @@ export namespace ImsdMessages::Ui {
App& operator=(const App&) = delete; App& operator=(const App&) = delete;
Sync* sync = nullptr; 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(const std::string&)> onConversationOpened; // the notification goes
std::function<void(std::string_view)> onLog; std::function<void(std::string_view)> onLog;
std::function<void()> onDisplayLost; // the compositor's connection broke 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(); } 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() { 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(); canvas.emplace();
try { try {
canvas->Create(400, 800, "Messages", assetDir, cacheDir); canvas->Create(400, 800, "Messages", assetDir, cacheDir);
@ -106,10 +142,12 @@ export namespace ImsdMessages::Ui {
Log(std::format("no window: {}", e.what())); Log(std::format("no window: {}", e.what()));
return; return;
} }
source.emplace(Crafter::Device::display); if (!source) {
source->onLost = [this] { source.emplace(Crafter::Device::display);
if (onDisplayLost) onDisplayLost(); source->onLost = [this] {
}; if (onDisplayLost) onDisplayLost();
};
}
frames.emplace(canvas->Window()); frames.emplace(canvas->Window());
frames->onFrame = [this] { canvas->Window().Update(); }; frames->onFrame = [this] { canvas->Window().Update(); };
day.emplace(); day.emplace();
@ -145,14 +183,44 @@ export namespace ImsdMessages::Ui {
ShowCaret(); ShowCaret();
}); });
resizeSub.emplace(&w.onResize, [this] { Dirty(); }); resizeSub.emplace(&w.onResize, [this] { Dirty(); });
closeSub.emplace(&w.onClose, [this] { closeSub.emplace(&w.onClose, [this] { Close(); });
if (onQuit) onQuit();
});
Reload(); Reload();
Opened();
if (view && view->compose.focused) ShowCaret(); // the blink a hide stopped
w.Render(); w.Render();
frames->RequestRedraw(); 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 // Shows the window on a conversation. Without a window (Show says
// why) nothing is on screen, so nothing is read or opened. `dialled` // 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 // 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; key = opening;
StopFling(); StopFling();
CancelGesture(); CancelGesture();
Show(); KeepWindow();
if (!canvas) return;
if (!view || view->key != key) { if (!view || view->key != key) {
KeepDraft(); KeepDraft();
view.emplace(); 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->dialledPlan = sync ? sync->Plan() : SimPlan{}; // for an address typed or linked now: the SIM's as known now
view->stickToBottom = true; view->stickToBottom = true;
view->messages = store.Conversation(key); 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 (sync) sync->SetOpenConversation(view->key);
if (onConversationOpened) onConversationOpened(view->key); if (onConversationOpened) onConversationOpened(view->key);
ShowCaret();
} }
void Back() { void Back() {
@ -257,9 +332,7 @@ export namespace ImsdMessages::Ui {
// whatever a turn of the wheel did over the empty one. // whatever a turn of the wheel did over the empty one.
view->scroll = ScrollState{}; view->scroll = ScrollState{};
view->stickToBottom = true; view->stickToBottom = true;
if (store.MarkRead(view->key) > 0) Reload(); if (OnScreen()) Opened(); // hidden, the kept conversation stays unread and keeps its notification: Show opens it
if (sync) sync->SetOpenConversation(view->key);
if (onConversationOpened) onConversationOpened(view->key);
} }
} }
// The drafts of the typed addresses that were left follow it too. // 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(); const std::size_t before = view->messages.size();
view->messages = store.Conversation(view->key); view->messages = store.Conversation(view->key);
if (view->messages.size() > before) view->stickToBottom = true; 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(); Dirty();
} }
@ -369,7 +444,8 @@ export namespace ImsdMessages::Ui {
void Pointer(PointerPhase phase) { void Pointer(PointerPhase phase) {
const Point p{ canvas->Window().currentMousePos.x, canvas->Window().currentMousePos.y }; const Point p{ canvas->Window().currentMousePos.x, canvas->Window().currentMousePos.y };
// Buffer pixels: the slop and the fling threshold follow the // 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.slopPx = canvas->Dp(8);
gestures.flingMinPxPerS = canvas->Dp(60); gestures.flingMinPxPerS = canvas->Dp(60);
if (phase == PointerPhase::Down) ActiveScroll().velocity = 0; // a finger down stops the fling under it 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; 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() { void ShowCaret() {
if (!view) return; if (!view) return;
view->caretVisible = true; view->caretVisible = true;
if (blinkTimer) g_source_remove(blinkTimer); if (blinkTimer) g_source_remove(blinkTimer);
blinkTimer = g_timeout_add(500, OnBlink, this); blinkTimer = canvas ? g_timeout_add(500, OnBlink, this) : 0;
Dirty(); Dirty();
} }
@ -523,6 +601,7 @@ export namespace ImsdMessages::Ui {
guint longPressTimer = 0; guint longPressTimer = 0;
guint repeatTimer = 0; guint repeatTimer = 0;
guint blinkTimer = 0; guint blinkTimer = 0;
guint hideIdle = 0; // a close's Hide, deferred to the loop
std::chrono::steady_clock::time_point lastFrame{}; std::chrono::steady_clock::time_point lastFrame{};
bool overflowLogged = false; bool overflowLogged = false;
std::filesystem::path screenshotPath; std::filesystem::path screenshotPath;