feat(gui): a tab owns its threads; kept state belongs to its books; one dock layout

An independent review of the tab work found that closing a tab did not
end what the tab started, that what the viewer keeps was filed under the
book id alone, and a few smaller things. All of it is the viewer's own
state: nothing here writes the books.

Threads: every thread a tab starts (loader, receipts' workers, export,
owner job, the dock's start and its agent's reaper) is counted in one
per-view registry (jobs.rs). Dropping the view tells them to stop and the
registry says, on a condition, when the last has ended. An owner job that
changes something (restore, format upgrade, new books, connect, close the
books, check for updates, update) is never abandoned: its tab's close
control is off with a hover text saying what runs, App::close refuses, and
the window's close request is cancelled with a dialog that offers only to
wait. Read-only jobs (an export, a backup, the restore's check) stop with
the tab: an export checks at its phase boundaries and saves nothing (the
save is one atomic write of a new file; one the stop reaches too late is
removed). The dock's agent is hung up on and, if it ignores that for two
seconds, killed and reaped; the window's drop waits for that.

Release on close: the loader checks "gone" before every retry and between
the phases of a load, its retry wait is woken by the drop, and the viewer
presence is let go in the drop itself, not when a refresh in flight ends.
A thumbnail for a closed tab is not written.

Kept state: filed under a hash of the data root's canonical path and the
id (books outside any root: under their folder), with the books' identity,
the SHA-256 of the journal's first line, taken once they load. A stored
state whose identity differs from the loaded books' is discarded with the
remembered name. Old unkeyed keys are read without error and not used. A
start that names a company never writes the list of open companies.

The dock's height is the app's one layout in every tab (said in the spec
block and tested); the sidebar, rail and top bar panels now take ids made
from their view's, since egui keys a panel's size by the id as given.

Every new test was seen to fail with its fix switched off. The four
sidenav tests keep their BAR_H: at the owner's 519 they fail by 15 points
(sidebar) and 11 (rail) without it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Sonnet 5.5 committed 2026-11-20 19:03:26 -07:00
1 parent 5cd0eded36
commit ec436cfc02
16 files changed
+2388 -154

No files matched your search

+140 -37
View File
@@ -29,6 +29,17 @@
//! owner's `upgrade_format` (the server checks it; nothing here
//! does). "Not now" changes nothing; a refusal (a wrong passphrase)
//! is said in the dialog, which stays.
//! 6. An action that changes or replaces the books, their format or
//! what the owner holds (restore, the format upgrade, new books,
//! connect, close the books, check for updates, update) is never
//! abandoned: while one runs, its tab's close control is off, with
//! the words of what is running ("Restoring these books: wait for it
//! to finish"), and the window's close is held (app.rs); its result
//! is shown in that tab when it ends. A read-only one (back up now,
//! the restore's check of a backup) is stopped with the tab: it does
//! not start once the tab has closed, and what it sends after is
//! dropped (a request the books server already has it finishes: the
//! viewer only reads its answer).
//! ```
use crate::books::Books;
@@ -231,6 +242,31 @@ pub fn book_id(books: &Books) -> Option<String> {
type Outcome = Result<Vec<String>, String>;
/// What a job does to the books (INVARIANT 6).
#[derive(Clone, Copy)]
enum Effect {
/// It only reads: stopped with its tab.
Reads,
/// It changes something and is never abandoned; the words say what is
/// running ("Restoring these books").
Changes(&'static str),
}
impl Effect {
fn hold(self) -> crate::jobs::Hold {
match self {
Effect::Reads => crate::jobs::Hold::Free,
Effect::Changes(what) => {
crate::jobs::Hold::Keep(format!("{what}: wait for it to finish"))
}
}
}
}
/// A job to run off the UI thread: what it is called, what it does to the
/// books, and the work.
type Start = (String, Effect, Box<dyn FnOnce() -> Outcome + Send>);
/// A dialog open over the viewer.
#[derive(Default)]
enum Dialog {
@@ -323,16 +359,20 @@ pub struct OwnerMenu {
/// The update waiting, as last read, and when (re-read every few
/// seconds and after any job).
update: Option<(std::time::Instant, Option<UpdateWaiting>)>,
/// The tab's threads: every job is one of them (INVARIANT 6).
jobs: crate::jobs::Spawner,
}
/// How long a reading of the waiting update is kept.
const UPDATE_LOOK: std::time::Duration = std::time::Duration::from_secs(5);
impl OwnerMenu {
/// A menu whose ids belong to the view `scope`.
pub fn scoped(scope: egui::Id) -> Self {
/// A menu whose ids belong to the view `scope`, and whose jobs are
/// threads of that view (`jobs`).
pub fn scoped(scope: egui::Id, jobs: crate::jobs::Spawner) -> Self {
Self {
scope: Some(scope),
jobs,
..Default::default()
}
}
@@ -411,7 +451,14 @@ impl OwnerMenu {
});
});
if let Some(work) = start {
self.start(ui.ctx(), "Update".into(), work);
self.start(
ui.ctx(),
(
"Update".into(),
Effect::Changes("Updating wanderbooks"),
work,
),
);
}
}
let Some(upgrade) = books.format_upgrade.clone() else {
@@ -500,7 +547,7 @@ impl OwnerMenu {
}
let busy = self.job.is_some();
let waiting = self.update_waiting(ui.ctx(), &owner);
let mut start: Option<(String, Box<dyn FnOnce() -> Outcome + Send>)> = None;
let mut start: Option<Start> = None;
let response = ui
.add_enabled_ui(!busy, |ui| {
egui::containers::menu::MenuButton::from_button(button).ui(ui, |ui| {
@@ -511,7 +558,11 @@ impl OwnerMenu {
.clicked()
{
let (o, b) = (owner.clone(), book.clone());
start = Some(("Back up now".into(), Box::new(move || o.back_up_now(&b))));
start = Some((
"Back up now".into(),
Effect::Reads,
Box::new(move || o.back_up_now(&b)),
));
}
if ui
.button(format!("{} Restore…", icon::CLOCK_COUNTER_CLOCKWISE))
@@ -543,6 +594,10 @@ impl OwnerMenu {
let (o, b) = (owner.clone(), book.clone());
start = Some((
format!("Connect {}", agent.label()),
Effect::Changes(match agent {
Agent::Claude => "Connecting Claude Code to these books",
Agent::Codex => "Connecting Codex to these books",
}),
Box::new(move || o.connect(agent, &b)),
));
}
@@ -564,7 +619,11 @@ impl OwnerMenu {
.clicked()
{
let o = owner.clone();
start = Some(("Check for updates".into(), Box::new(move || o.check_for_updates())));
start = Some((
"Check for updates".into(),
Effect::Changes("Checking for updates"),
Box::new(move || o.check_for_updates()),
));
}
if let Some(u) = &waiting
&& ui
@@ -573,7 +632,11 @@ impl OwnerMenu {
.clicked()
{
let o = owner.clone();
start = Some(("Update".into(), Box::new(move || o.apply_update())));
start = Some((
"Update".into(),
Effect::Changes("Updating wanderbooks"),
Box::new(move || o.apply_update()),
));
}
ui.separator();
if ui
@@ -586,8 +649,8 @@ impl OwnerMenu {
})
})
.inner;
if let Some((what, work)) = start {
self.start(ui.ctx(), what, work);
if let Some(start) = start {
self.start(ui.ctx(), start);
}
self.dialogs(ui.ctx(), &owner, &book);
let said = if self.job.is_some() {
@@ -609,18 +672,21 @@ impl OwnerMenu {
}
}
fn start(
&mut self,
ctx: &egui::Context,
what: String,
work: Box<dyn FnOnce() -> Outcome + Send>,
) {
fn start(&mut self, ctx: &egui::Context, (what, effect, work): Start) {
let (tx, rx) = channel();
let ctx = ctx.clone();
std::thread::spawn(move || {
let started = self.jobs.spawn(&what.clone(), effect.hold(), move |stop| {
// A read-only job of a tab that has closed is not begun.
if matches!(effect, Effect::Reads) && stop.requested() {
return;
}
let _ = tx.send(work());
ctx.request_repaint();
});
if !started {
self.done = Some((what, Err("the system gave no thread for it".into())));
return;
}
self.job = Some((what, rx));
self.done = None;
}
@@ -628,7 +694,8 @@ impl OwnerMenu {
/// The open dialog, if any, for the books `book`.
fn dialogs(&mut self, ctx: &egui::Context, owner: &Arc<dyn Owner>, book: &str) {
let mut close = false;
let mut run: Option<(String, Box<dyn FnOnce() -> Outcome + Send>)> = None;
let mut run: Option<Start> = None;
let jobs = self.jobs.clone();
match &mut self.dialog {
Dialog::None => {}
Dialog::Upgrade(form) => {
@@ -660,6 +727,7 @@ impl OwnerMenu {
form,
owner,
book,
&jobs,
&mut close,
);
}
@@ -671,7 +739,11 @@ impl OwnerMenu {
ui.horizontal(|ui| {
if ui.button("Close the books").clicked() {
let o = owner.clone();
run = Some(("Close the books".into(), Box::new(move || o.close_books().map(|l| vec![l]))));
run = Some((
"Close the books".into(),
Effect::Changes("Closing the books"),
Box::new(move || o.close_books().map(|l| vec![l])),
));
close = true;
}
if ui.button("Cancel").clicked() {
@@ -697,6 +769,7 @@ impl OwnerMenu {
let (o, i, c) = (owner.clone(), id.clone(), currency.clone());
run = Some((
"New books".into(),
Effect::Changes("Making new books"),
Box::new(move || o.new_books(&i, &c).map(|l| vec![l])),
));
close = true;
@@ -780,13 +853,27 @@ impl OwnerMenu {
let (o, s, r) = (owner.clone(), form.source.clone(), form.replace);
let id = Some(form.id.trim().to_string()).filter(|i| !i.is_empty());
let ctx = ui.ctx().clone();
std::thread::spawn(move || {
let _ = tx.send(o.plan_restore(&s, id, r));
ctx.request_repaint();
});
let asked = (form.source.clone(), form.id.clone(), form.replace);
form.planning = Some((rx, asked));
form.problem = None;
// Only reads the backup: stopped with its tab.
let started = jobs.spawn(
"restore check",
crate::jobs::Hold::Free,
move |stop| {
if stop.requested() {
return;
}
let _ = tx.send(o.plan_restore(&s, id, r));
ctx.request_repaint();
},
);
if started {
let asked =
(form.source.clone(), form.id.clone(), form.replace);
form.planning = Some((rx, asked));
form.problem = None;
} else {
form.problem =
Some("the system gave no thread for it".into());
}
}
if planning {
ui.label("Checking the backup…");
@@ -795,7 +882,11 @@ impl OwnerMenu {
Some(plan) => {
if ui.button("Restore it").clicked() {
let (o, plan) = (owner.clone(), plan.clone());
run = Some(("Restore".into(), Box::new(move || o.restore(&plan))));
run = Some((
"Restore".into(),
Effect::Changes("Restoring these books"),
Box::new(move || o.restore(&plan)),
));
close = true;
}
}
@@ -810,8 +901,8 @@ impl OwnerMenu {
if close {
self.dialog = Dialog::None;
}
if let Some((what, work)) = run {
self.start(ctx, what, work);
if let Some(run) = run {
self.start(ctx, run);
}
}
}
@@ -824,6 +915,7 @@ fn upgrade_dialog(
form: &mut UpgradeForm,
owner: &Arc<dyn Owner>,
book: &str,
jobs: &crate::jobs::Spawner,
close: &mut bool,
) {
let p = crate::theme::pal(ctx);
@@ -864,15 +956,26 @@ fn upgrade_dialog(
let (o, b) = (owner.clone(), book.to_string());
let (tx, rx) = channel();
let ctx = ui.ctx().clone();
std::thread::spawn(move || {
// The window is woken however this ends (a panic
// too), once the answer, or its absence, can be seen.
let _wake = Wake(ctx);
let (tx, passphrase) = (tx, passphrase);
let _ = tx.send(o.upgrade_format(&b, &passphrase));
});
form.working = Some(rx);
form.problem = None;
// Changes the books' format: never abandoned (INVARIANT 6).
let started = jobs.spawn(
"format upgrade",
crate::jobs::Hold::Keep(
"Upgrading these books' format: wait for it to finish".into(),
),
move |_| {
// The window is woken however this ends (a panic
// too), once the answer, or its absence, can be seen.
let _wake = Wake(ctx);
let (tx, passphrase) = (tx, passphrase);
let _ = tx.send(o.upgrade_format(&b, &passphrase));
},
);
if started {
form.working = Some(rx);
form.problem = None;
} else {
form.problem = Some("the system gave no thread for it".into());
}
}
if ui
.add_enabled(!working, egui::Button::new("Not now"))