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:
1 parent
5cd0eded36
commit
ec436cfc02
16 files changed
+2388
-154
No files matched your search
@@ -17,6 +17,16 @@
|
||||
//! missing TIN never stops the file: the package lists the gaps.
|
||||
//! 3. The file is saved as a new file in Downloads (export::save), never
|
||||
//! over one, off the UI thread; what came of it is shown.
|
||||
//! 4. An export belongs to its tab: closing the tab, with no question,
|
||||
//! tells the job to stop (jobs.rs). It checks at its phase
|
||||
//! boundaries (before and after the books are read, after the file
|
||||
//! is built, and just before it is saved) and stops there, saving
|
||||
//! nothing: the save is one atomic write of a new file
|
||||
//! (`ledger_store::saving`: written aside whole and linked in), so a
|
||||
//! file is whole or is not there, and one that a stop reaches
|
||||
//! too late to prevent (it came after the last check) is removed
|
||||
//! again, being the job's own new file. What the builder
|
||||
//! (`ledger_package`) does inside one call is not interrupted.
|
||||
//! ```
|
||||
|
||||
use crate::books::Books;
|
||||
@@ -35,9 +45,19 @@ pub type Done = Result<(PathBuf, Option<String>), String>;
|
||||
pub struct Menu {
|
||||
job: Option<(String, Receiver<Done>)>,
|
||||
done: Option<(String, Done)>,
|
||||
/// The tab's threads: the job is one of them (INVARIANT 4).
|
||||
jobs: crate::jobs::Spawner,
|
||||
}
|
||||
|
||||
impl Menu {
|
||||
/// A menu whose jobs are threads of a tab.
|
||||
pub fn in_tab(jobs: crate::jobs::Spawner) -> Self {
|
||||
Self {
|
||||
jobs,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// The menu button, its items, and a note of the last job's outcome.
|
||||
pub fn ui(&mut self, ui: &mut Ui, books: &Arc<Books>, compact: bool) {
|
||||
let p = crate::theme::pal(ui.ctx());
|
||||
@@ -117,13 +137,30 @@ impl Menu {
|
||||
}
|
||||
|
||||
fn start(&mut self, ctx: &egui::Context, books: Arc<Books>, job: Job) {
|
||||
let (tx, rx) = channel();
|
||||
let what = job.label();
|
||||
self.start_with(ctx, what, Box::new(move |stop| job.run_until(&books, stop)));
|
||||
}
|
||||
|
||||
/// Run `work` as an export job: off the UI thread, one of the tab's
|
||||
/// threads, told to stop with the tab (INVARIANT 4).
|
||||
pub fn start_with(
|
||||
&mut self,
|
||||
ctx: &egui::Context,
|
||||
what: String,
|
||||
work: Box<dyn FnOnce(&crate::jobs::Stop) -> Done + Send>,
|
||||
) {
|
||||
let (tx, rx) = channel();
|
||||
let ctx = ctx.clone();
|
||||
std::thread::spawn(move || {
|
||||
let _ = tx.send(job.run(&books));
|
||||
ctx.request_repaint();
|
||||
});
|
||||
let started = self
|
||||
.jobs
|
||||
.spawn("export", crate::jobs::Hold::Free, move |stop| {
|
||||
let _ = tx.send(work(stop));
|
||||
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;
|
||||
}
|
||||
@@ -148,7 +185,20 @@ impl Job {
|
||||
|
||||
/// Make the file and save it (INVARIANTS 1-3).
|
||||
pub fn run(self, books: &Books) -> Done {
|
||||
self.run_until(books, &crate::jobs::Stop::never())
|
||||
}
|
||||
|
||||
/// [`Self::run`], stopping at its phase boundaries when `stop` is
|
||||
/// given, saving nothing (INVARIANT 4).
|
||||
pub fn run_until(self, books: &Books, stop: &crate::jobs::Stop) -> Done {
|
||||
let stopped = || -> Done { Err("stopped: its tab was closed".into()) };
|
||||
if stop.requested() {
|
||||
return stopped();
|
||||
}
|
||||
let snap = ledger_store::Snapshot::load(&books.data_dir).map_err(|e| e.to_string())?;
|
||||
if stop.requested() {
|
||||
return stopped();
|
||||
}
|
||||
let ledger = snap.ledger();
|
||||
// The settings the rest of the viewer shows, read as the server
|
||||
// reads them (data::settings).
|
||||
@@ -176,8 +226,17 @@ impl Job {
|
||||
tins: Some(&tin),
|
||||
},
|
||||
)?;
|
||||
if stop.requested() {
|
||||
return stopped();
|
||||
}
|
||||
let (stem, ext) = crate::export::split_name(&p.file_name);
|
||||
let path = crate::export::save(stem, ext, &p.zip)?;
|
||||
if stop.requested() {
|
||||
// Reached too late to stop the write: the new file it
|
||||
// made is the job's own, and goes.
|
||||
let _ = std::fs::remove_file(&path);
|
||||
return stopped();
|
||||
}
|
||||
let mut notes = Vec::new();
|
||||
if !p.tin_gaps.is_empty() {
|
||||
notes.push(format!(
|
||||
@@ -205,8 +264,15 @@ impl Job {
|
||||
)?;
|
||||
let csv = ledger_package::vendor_1099_csv(&w, true);
|
||||
let name = ledger_package::vendor_1099_csv_name(year, true);
|
||||
if stop.requested() {
|
||||
return stopped();
|
||||
}
|
||||
let (stem, ext) = crate::export::split_name(&name);
|
||||
let path = crate::export::save(stem, ext, csv.as_bytes())?;
|
||||
if stop.requested() {
|
||||
let _ = std::fs::remove_file(&path);
|
||||
return stopped();
|
||||
}
|
||||
let gaps = w
|
||||
.lines
|
||||
.iter()
|
||||
|
||||
Reference in new issue
Block a user