From 07c6c3e5bd6ee3210ec3ce350b2b726ff5fcf25b Mon Sep 17 00:00:00 2001 From: Jorijn van der Graaf Date: Sun, 18 Oct 2026 01:16:35 +0200 Subject: [PATCH] Ui, main: closing the window hides it; Activate shows it again MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- implementations/main.cpp | 18 +++- interfaces/ImsdMessages.Ui-App.cppm | 131 ++++++++++++++++++++++------ 2 files changed, 121 insertions(+), 28 deletions(-) diff --git a/implementations/main.cpp b/implementations/main.cpp index b2cd358..4a36e12 100644 --- a/implementations/main.cpp +++ b/implementations/main.cpp @@ -48,6 +48,8 @@ struct Options { std::optional screenshot; double screenshotAfter = 2.5; // seconds after start 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 bool sendDraft = false; // and sent, as if the button were tapped std::string country; @@ -59,7 +61,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; } +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 // milliseconds: what is not such a number is the error. @@ -88,6 +90,8 @@ Options Parse(std::span args) { 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 == "--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 == "--send-draft") o.sendDraft = true; else if (a == "--country" && i + 1 < args.size()) o.country = args[++i]; @@ -106,6 +110,7 @@ Options Parse(std::span args) { // 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. 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.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"; @@ -271,7 +276,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] [--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; } @@ -364,6 +369,7 @@ int main(int argc, char** argv) { shell.sync = &sync; 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 + app->closeQuits = session == nullptr; app->sync = &sync; app->onQuit = [loop] { g_main_loop_quit(loop); }; app->onConversationOpened = [¬ifier](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(SIGTERM, OnQuitSignal, &shell); if (o.send) g_timeout_add_seconds(60, OnSendTimeout, &shell); + if (o.hideAfter) g_timeout_add(static_cast(*o.hideAfter * 1000), [](gpointer data) -> gboolean { + static_cast(data)->Close(); + return G_SOURCE_REMOVE; + }, &*app); + if (o.hideEvery) g_timeout_add(static_cast(*o.hideEvery * 1000), [](gpointer data) -> gboolean { + static_cast(data)->Close(); + return G_SOURCE_CONTINUE; + }, &*app); // A bench's clock: the smoke tests on the phone end on their own. if (o.quitAfter) g_timeout_add(static_cast(*o.quitAfter * 1000), [](gpointer data) -> gboolean { static_cast(data)->QuitAfter(); diff --git a/interfaces/ImsdMessages.Ui-App.cppm b/interfaces/ImsdMessages.Ui-App.cppm index 8312c98..47d1981 100644 --- a/interfaces/ImsdMessages.Ui-App.cppm +++ b/interfaces/ImsdMessages.Ui-App.cppm @@ -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(data); + self.hideIdle = 0; + self.Hide(); + return G_SOURCE_REMOVE; } - private: static gboolean OnScreenshot(gpointer data) { App& self = *static_cast(data); if (self.canvas) { @@ -81,7 +99,8 @@ export namespace ImsdMessages::Ui { App& operator=(const App&) = delete; Sync* sync = nullptr; - std::function 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 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 onLog; std::function 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;