S047: close to tray on Wayland: window reopens, hidden runs tested, tools/tray-check

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
astar76 2026-11-27 07:33:47 -06:00
commit 62147e743a
12 changed files with 151 additions and 7 deletions

View file

@ -69,6 +69,7 @@ Never modify: everything outside this folder. `~/Projects/*` is read-only refere
| `python3 tools/gen_assets.py icons` | adds icons: put the name in `tools/icons.txt` first | S003 |
| `nice cargo build --release -j 2` | release build (never at the same time as the tests) | 01 Foundation |
| `nice cargo test -j 2 -p medlar-testserver` / `cargo run -p medlar-testserver -- [--port N] [--tls] [--media] [--proxies]` | local server with faults and proxy stubs; API in the `crates/testserver/src/lib.rs` header | S002 |
| `PORT=… TS=… tools/tray-check/run.sh <name> <driver> [tray] [second]` | close to tray in a hidden niri with a fake tray host (drivers `tray.sh`, `notray.sh`, `min.sh`); testserver on `$TS` | S047 |
| `ssh [email protected] hub-release` | release notes on Astar Hub (release only, after the user's yes) | now |
Don't assume a tool exists: check first. When your plan creates a tool, add its row here.

View file

@ -19,12 +19,12 @@ eframe with `NativeOptions::run_and_return = true` returns from `run_native` whe
If `run_and_return` cannot reopen on this eframe/winit, stop and report with the error; do not build a second process design without asking.
## Steps
- [ ] Read eframe 0.37 `run_and_return` / `run_native` docs and source in ~/.cargo/registry; confirm reopen works with a 20-line spike in a test or example
- [ ] Session refactor: long-lived parts out of `App`; nothing else changes behaviour
- [ ] Close/show/quit loop in `main` as above; tray Show and Quit, second instance, connector arrival, `--minimised`
- [ ] Tests: unit for the decide-on-close logic (tray on/off, host yes/no); hidden nested niri run with a fake StatusNotifierWatcher (bus.rs from S043) on a spare port and throwaway home: send close (niri msg action close-window), process stays, downloads keep going (testserver, throttled), port answers; tray Show reopens; POST to the port reopens with the Add window; Quit ends with tasks saved; without the fake tray, close quits
- [ ] Full workspace suite once, release build, smoke as S046; add to docs/releases/0.1.1.md; rebuild packages via tools/package.sh (0.1.1), nothing into releases/
- [ ] Test file with Results, PROGRESS, tick INDEX, commit `S047: …`, push
- [x] Read eframe 0.37 `run_and_return` / `run_native` docs and source in ~/.cargo/registry; confirm reopen works with a 20-line spike in a test or example
- [x] Session refactor: long-lived parts out of `App`; nothing else changes behaviour
- [x] Close/show/quit loop in `main` as above; tray Show and Quit, second instance, connector arrival, `--minimised`
- [x] Tests: unit for the decide-on-close logic (tray on/off, host yes/no); hidden nested niri run with a fake StatusNotifierWatcher (bus.rs from S043) on a spare port and throwaway home: send close (niri msg action close-window), process stays, downloads keep going (testserver, throttled), port answers; tray Show reopens; POST to the port reopens with the Add window; Quit ends with tasks saved; without the fake tray, close quits
- [x] Full workspace suite once, release build, smoke as S046; add to docs/releases/0.1.1.md; rebuild packages via tools/package.sh (0.1.1), nothing into releases/
- [x] Test file with Results, PROGRESS, tick INDEX, commit `S047: …`, push
## Pitfalls
The user's Medlar 0.1.0 is running on 17616: never stop it; spare port, throwaway home. Never weaken a test. Idle (hidden) draws no frames and uses ~0 CPU.

View file

@ -67,7 +67,7 @@ Tracker: https://claude.ai/artifact/XouimfVJS6FYVKskGJ8s7H
- [x] [S045](10%20Distribution/S045-installer-arch.md) installer.sh and Arch package
- [x] [S046](10%20Distribution/S046-fixes-0.1.1.md) 0.1.1: names from the server, raise on arrival, copy in fields
- [ ] [S047](10%20Distribution/S047-close-to-tray-wayland.md) Close to tray that works on Wayland
- [x] [S047](10%20Distribution/S047-close-to-tray-wayland.md) Close to tray that works on Wayland
- [x] [S048](10%20Distribution/S048-server-farms.md) Servers behind a load balancer with different validators
- [ ] [S049](10%20Distribution/S049-niri-focus.md) Take focus on niri when a download arrives

View file

@ -280,3 +280,9 @@ Seen: boxes were ticked together after the tests, against rule 3.
- Same-size replies with other dates/tags (nginx `W/"size-mtime"`) are checked by 64 KiB of bytes before writing; proven versions per link, no If-Range on a farm, `tasks.farm` stored (migration 007). HTTP only; FTP keeps mtime strict.
- testserver `farm=N`, `liar=1`; `crates/core/tests/farm.rs`. Hidden smoke on a farm passed; dist/ rebuilt (0.1.1), copied to `~/Projects/Medlar/dist/`.
- Not done: `connector_flood_15` fails (connector untouched), CPU right after done 160 %, packaged builds not launched, no real farm.
## S047 — Close to tray on Wayland
- One `egui::Context` and the `App` live in `open()`; windows come and go via `run_native_ext` + `run_and_return`; while closed `App::hidden` ticks tray, port and second starts, waking on repaint requests. Stopgap removed.
- Hidden nested niri with a fake tray host (`tools/tray-check/`): close stays, 0 % CPU, port answers, tray Show / POST / second start reopen, download finishes hidden, tray Quit ends; no host: close quits. Suite 576 passed.
- Not tested: Ctrl+Q key, tray Pause/Resume all and notification actions while hidden, X11, real tray hosts, packages (not rebuilt).
- Later: clipboard monitoring while hidden; reopen if the tray host vanishes while hidden.

View file

@ -26,6 +26,11 @@ Fixes and small changes after 0.1.0. Linux x86_64, glibc 2.44 or newer.
dates but the same size, Medlar compares 64 KiB of bytes it already has, or that a known server sent, before writing
anything. The same bytes: the download goes on in parts and remembers the farm across restarts. Other bytes or another
size: it still stops with "File changed", and what is already downloaded is kept. Two files are never mixed.
- **Close to the tray on Wayland.** Closing the window (also with the compositor's close, such as Win+Q on niri) now
leaves Medlar running in the tray when "Close to tray" is on and the desktop shows tray icons: downloads, the browser
port and the tray keep going, and the window comes back from the tray, a second start or a link from the browser.
Without a tray, closing quits as before. Ctrl+Q and the tray's Quit always quit. Starting with `--minimised` opens no
window when there is a tray.
## Known gaps
- Needs glibc 2.44 or newer; not tested on other systems.

View file

@ -0,0 +1,45 @@
# S047 — Close to tray that works on Wayland: test file
Written and run by the S047 session (Phase: test).
## Built
- `crates/app/src/app.rs` · `open()` makes one `egui::Context` and the `App` before any window; the window runs through `eframe::run_native_ext(.., Some(ctx), ..)` with `run_and_return`, borrowing the `App` (`Window<'a>`). Closing with close to tray on and a tray host returns to `App::hidden`, which runs without a window until Show, an arrival or Quit; then the window opens again or the app quits (pause/save via `bridge.close()` only then).
- `App::hidden` wakes on every repaint request (a `set_repaint_observer` into a 1-slot channel), plus a 2 s look round. It ticks the shell (tray, notifications, launcher), the inbox (port, second starts) and saves changed settings. No frames are drawn.
- `App::act` · tray/notification actions shared by the window and the hidden loop; Add and Sign in queue their dialog and ask for the window.
- `shell::hides(quitting, close_to_tray, host)` · the decision; the Wayland stopgap is gone, `CancelClose`/`Visible(false)` are no longer used.
- `views/incoming.rs` · `raise()` also sets a flag (`take_raised`) so that an arrival while hidden reopens the window.
- `Main::all(resume)` · pause/resume all, also without a window.
- `--minimised` with a tray host starts without a window.
- `tools/tray-check/` · hidden nested niri runs with a fake StatusNotifierWatcher (Python Gio) on the run's private bus.
## Checks
Risk: HIGH = data loss, security, crash, wrong result · MED = feature broken · LOW = looks or comfort.
| # | Risk | Do this | Expect | How | Result |
|---|---|---|---|---|---|
| 1 | HIGH | `hides(quitting, close_to_tray, host)` for all cases | Hides only with close to tray on, a host and no quit | unit `shell::tests` | pass |
| 2 | HIGH | Hidden run, fake tray host, close the window (`niri msg action close-window`) | No window, process stays | `tools/tray-check/run.sh tray tray.sh 1` | pass |
| 3 | MED | While hidden and idle, CPU over 5 s | ~0 % | same run (`cpu 5`) | pass: 0.00 % |
| 4 | HIGH | While hidden, GET the connector port | Answers | same run | pass: 200 |
| 5 | MED | Tray `Activate` (left click) over D-Bus | Window comes back where it was | same run, grab `shown` | pass |
| 6 | HIGH | Close, then POST `{url, filename}` to the port | Window reopens with the Add dialog filled in | same run, grab `add` | pass |
| 7 | HIGH | Start a throttled 4 MB download (300 kB/s), close at 15 %, wait 18 s hidden, Show from the tray | Download kept going: Done 100 % | same run, grabs `started`, `after` | pass |
| 8 | HIGH | Close, then tray menu Quit (dbusmenu Event on the "Quit" item) while hidden | Process ends within 10 s, exit 0, no panic | same run | pass |
| 9 | HIGH | No tray host; close the window | Process ends (quit as before) | `run.sh notray notray.sh` | pass |
| 10 | MED | `--minimised` with a tray host, second start 5 s later | No window until the second start, then it opens; tray menu answers | `MEDLAR_ARGS=--minimised run.sh min min.sh 1 1` | pass: window after 6 s |
| 11 | HIGH | Ctrl+Q with close to tray on | Quits (quitting set, `hides` false) | unit for the decision; key path not driven | not run: Ctrl+Q key not sent in the hidden run, only the decision is unit-tested |
| 12 | MED | Tray Pause all / Resume all while hidden | Downloads pause / resume without a window | by hand | not run: no driver for these menu items |
| 13 | MED | Notification action (Open, Retry) while hidden | Works; Sign in reopens the window with its dialog | by hand | not run: no fake notification daemon in the hidden run |
| 14 | LOW | X11 session | Same behaviour (close ends the window, tray reopens) | by hand | not run: only Wayland (nested niri) |
| 15 | HIGH | Workspace suite | All pass | `nice cargo test -j 2` | pass: 576 passed, 0 failed, 9 ignored |
## Needs
Release build, `medlar-testserver --port $TS`, spare connector port (`PORT=17997`, never 17616), throwaway home from `tools/hidden.sh`, `python3` with `gi` (Gio), `busctl`. The fake tray host lives on the run's private `dbus-run-session` bus, never the user's.
## Doubts
- Settings changed from the tray while hidden (mode) are applied and saved at once, not through the 0.4 s debounce.
- Repaint requests with a delay are not timed while hidden; a 2 s look round covers them (timers that need finer timing would be late).
- The clipboard watcher and the drop target do not run while hidden (they need a frame). Clipboard monitoring in the tray: see PROGRESS Later.
- If the tray host disappears while hidden, the window stays closed until a second start, a browser link or a notification brings it back.
- The `Context` is reused across windows: viewport state from the old window (e.g. close_requested) has shown no effect, but it is not checked beyond these runs.
- Wayland only: nothing was run on X11 or on a real Plasma/GNOME tray host; packaged builds were not launched.

View file

@ -0,0 +1,28 @@
# Fake StatusNotifierWatcher on the session bus, so that Medlar finds a tray host.
import gi
gi.require_version("Gio", "2.0")
from gi.repository import Gio, GLib
XML = """<node><interface name="org.kde.StatusNotifierWatcher">
<method name="RegisterStatusNotifierItem"><arg type="s" direction="in"/></method>
<method name="RegisterStatusNotifierHost"><arg type="s" direction="in"/></method>
<property name="IsStatusNotifierHostRegistered" type="b" access="read"/>
<property name="ProtocolVersion" type="i" access="read"/>
<property name="RegisteredStatusNotifierItems" type="as" access="read"/>
</interface></node>"""
items = []
def call(conn, sender, path, iface, method, params, inv):
if method == "RegisterStatusNotifierItem":
s = params.unpack()[0]
items.append(s if s.startswith("/") is False else sender)
open(OUT, "w").write(sender + "\n")
inv.return_value(None)
def prop(conn, sender, path, iface, name):
return {"IsStatusNotifierHostRegistered": GLib.Variant("b", True), "ProtocolVersion": GLib.Variant("i", 0),
"RegisteredStatusNotifierItems": GLib.Variant("as", items)}[name]
import sys
OUT = sys.argv[1]
info = Gio.DBusNodeInfo.new_for_xml(XML).interfaces[0]
def acquired(conn, name):
conn.register_object("/StatusNotifierWatcher", info, call, prop, None)
Gio.bus_own_name(Gio.BusType.SESSION, "org.kde.StatusNotifierWatcher", 0, acquired, None, None)
GLib.MainLoop().run()

5
tools/tray-check/min.sh Normal file
View file

@ -0,0 +1,5 @@
# Driver: started with --minimised under a tray host; only the second start (5 s later) opened the window.
echo "window after $(( $(date +%s) - $(cat "$S047_OUT/t0") )) s (second start at 5 s)"
grab minwin
addr=$(cat "$S047_OUT/bus"); svc=$(cat "$S047_OUT/item")
busctl --address="$addr" --json=short -- call "$svc" /MenuBar com.canonical.dbusmenu GetLayout iias 0 -1 0 >/dev/null && echo "tray menu answers"

View file

@ -0,0 +1,4 @@
# Driver: no tray host, so closing quits.
quit
for _ in $(seq 50); do kill -0 "$pid" 2>/dev/null || break; sleep 0.2; done
kill -0 "$pid" 2>/dev/null && { echo "FAIL still running after close"; exit 1; } || echo "close without tray: ended"

9
tools/tray-check/run.sh Executable file
View file

@ -0,0 +1,9 @@
#!/bin/bash
# S047 close-to-tray check: run.sh <name> <driver> [fake-tray] [second-start]. Needs PORT (spare connector port, never
# 17616) and TS (a running medlar-testserver port) exported, and a release build. Output in target/tmp/tray-check/<name>.
here=$(dirname "$(realpath "$0")"); root=$(realpath "$here/../..")
base=$root/target/tmp/tray-check; out=$base/$1; rm -rf "$out" "$base"/big.bin*; mkdir -p "$out/home/.config/medlar"
printf '{"desktop_colors": false, "connector_port": %s, "close_to_tray": true, "tray": true}\n' "$PORT" > "$out/home/.config/medlar/settings.json"
mkdir -p "$base"; cd "$base" || exit 2
export TMPDIR=$base
S047_OUT=$out FAKE_TRAY=${3:-} S047_SECOND=${4:-} S047_BIN=$root/target/release/medlar MEDLAR_BIN=$here/wrap.sh "$root/tools/hidden.sh" "$out" "$here/$2"

33
tools/tray-check/tray.sh Normal file
View file

@ -0,0 +1,33 @@
# Driver: close to tray with a fake tray host. Needs S047_OUT, PORT, TS (testserver port).
wins() { niri msg --json windows | grep -o '"app_id":"medlar"' | wc -l; }
waitwin() { for _ in $(seq 50); do [ "$(wins)" = "$1" ] && return 0; sleep 0.2; done; echo "FAIL window count not $1"; return 1; }
addr=$(cat "$S047_OUT/bus")
svc=$(cat "$S047_OUT/item")
bus() { busctl --address="$addr" --json=short -- "$@"; }
alive() { kill -0 "$pid" 2>/dev/null && echo "alive" || { echo "FAIL process ended"; return 1; }; }
echo "item: $svc"
echo "1 close -> hidden"; quit; waitwin 0; sleep 1; alive
cpu 5
echo "2 port answers while hidden: $(curl -s -o /dev/null -w '%{http_code}' "http://127.0.0.1:$PORT/")"
echo "3 tray Activate"; bus call "$svc" /StatusNotifierItem org.kde.StatusNotifierItem Activate ii 0 0; waitwin 1; grab shown
echo "4 close, POST a link while hidden"; quit; waitwin 0
curl -s -o /dev/null -w 'post %{http_code}\n' -H 'Content-Type: application/json' -d "{\"url\":\"http://127.0.0.1:$TS/data/7/4m/big.bin?throttle_total=300000\",\"filename\":\"big.bin\"}" "http://127.0.0.1:$PORT/"
waitwin 1; sleep 1.5; grab add
click 890 678; sleep 2; grab started
echo "5 close while downloading"; quit; waitwin 0; alive
sleep 18; echo "port while hidden: $(curl -s -o /dev/null -w '%{http_code}' "http://127.0.0.1:$PORT/")"
bus call "$svc" /StatusNotifierItem org.kde.StatusNotifierItem Activate ii 0 0; waitwin 1; sleep 1; grab after; quit; waitwin 0
echo "6 tray menu Quit"
bus call "$svc" /MenuBar com.canonical.dbusmenu GetLayout iias 0 -1 0 > "$S047_OUT/layout.json"
id=$(python3 -c '
import json,sys
d=json.load(open(sys.argv[1]))
def walk(n):
i,props,kids=n
if props.get("label",{}).get("data","").lower().startswith("quit"): print(i); sys.exit()
for k in kids: walk(k["data"])
walk(d["data"][1])' "$S047_OUT/layout.json")
echo "quit id: $id"
bus call "$svc" /MenuBar com.canonical.dbusmenu Event isvu "$id" clicked s "" 0
for _ in $(seq 50); do kill -0 "$pid" 2>/dev/null || break; sleep 0.2; done
kill -0 "$pid" 2>/dev/null && echo "FAIL still running after Quit" || echo "quit: ended"

8
tools/tray-check/wrap.sh Executable file
View file

@ -0,0 +1,8 @@
#!/bin/bash
# Runs inside hidden.sh's dbus-run-session: saves the bus address, starts the fake tray host if asked, then Medlar.
d=$(dirname "$(realpath "$0")")
echo "$DBUS_SESSION_BUS_ADDRESS" > "$S047_OUT/bus"
if [ -n "${FAKE_TRAY:-}" ]; then python3 "$d/fake_watcher.py" "$S047_OUT/item" & sleep 1; fi
date +%s > "$S047_OUT/t0"
[ -n "${S047_SECOND:-}" ] && (sleep 5; "$S047_BIN") &
exec "$S047_BIN" "$@"