library: task 42 review — cursor frame on the row's fill, copied base at the end of the shelf, write error in the conditions line, CP_BATCH_CANCEL_AFTER

This commit is contained in:
vrubelroman 2026-11-27 14:24:36 +03:00
parent e0ca581303
commit d5eff1a256
5 changed files with 96 additions and 21 deletions

View file

@ -1948,7 +1948,9 @@ impl App {
match end {
BatchEnd::Done { target, report } => {
let name = own_db::base_name(&target);
self.settings.touch_database(&target);
// At the end of the shelf: the cards stay where they are; «Open» brings it
// to the front.
self.settings.add_database_last(&target);
self.settings_dirty = true;
self.toast_for(ctx, crate::batch_copy::summary(&name, &report), 8.0);
self.toast_open_db = Some(target);
@ -1984,6 +1986,7 @@ impl App {
],
),
};
self.library.set_batch_error(&source, text.clone());
self.toast_for(ctx, text, 8.0);
}
}
@ -4599,6 +4602,8 @@ impl eframe::App for App {
if let Some(path) = open_copy {
self.toast = None;
self.toast_open_db = None;
self.settings.touch_database(&path);
self.settings_dirty = true;
self.library.select_db(&path, &ctx);
}
#[cfg(not(web))]

View file

@ -330,6 +330,8 @@ pub struct LargeView {
pub batch: Option<BatchStatus>,
/// «Cancel» of that copy was clicked.
pub cancel_batch: bool,
/// Why the last copy wrote nothing: said in red where its progress was, until `×`.
pub batch_error: Option<String>,
scroll_to_selected: bool,
scroll_to_top: bool,
focus_player: bool,
@ -454,6 +456,7 @@ impl LargeView {
hidden: None,
batch: None,
cancel_batch: false,
batch_error: None,
scroll_to_selected: false,
scroll_to_top: false,
focus_player: false,
@ -1345,6 +1348,8 @@ impl LargeView {
let chosen = self.marked.len();
let hidden = if chosen >= 2 { self.hidden_marked() } else { 0 };
let batch = self.batch.clone();
let batch_error = self.batch_error.clone();
let mut close_error = false;
let mut clear_marked = false;
let mut cancel_batch = false;
ui.horizontal(|ui| {
@ -1383,10 +1388,23 @@ impl LargeView {
}
}
// The choice, or the copy being written, in the same place (§42.3).
if batch.is_some() || chosen >= 2 {
if batch.is_some() || batch_error.is_some() || chosen >= 2 {
ui.add_space(SLOT_GAP);
}
if let Some(b) = &batch {
if let (None, Some(e)) = (&batch, &batch_error) {
if crate::widgets::small_close_button(ui).clicked() {
close_error = true;
}
let width = (ui.available_width() - 140.0).max(80.0);
ui.scope(|ui| {
ui.set_max_width(width);
ui.add(
egui::Label::new(RichText::new(e).color(ui.visuals().error_fg_color))
.truncate(),
)
.on_hover_text(e);
});
} else if let Some(b) = &batch {
if b.cancelling {
ui.spinner();
ui.label(RichText::new(tr("Cancelling…")).weak());
@ -1479,6 +1497,9 @@ impl LargeView {
if clear_marked {
self.mark_cursor_only();
}
if close_error {
self.batch_error = None;
}
if cancel_batch {
self.cancel_batch = true;
}
@ -2390,6 +2411,7 @@ impl LargeView {
);
let frame_color = ui.visuals().selection.stroke.color;
let table_clip = ui.clip_rect().intersect(ui.max_rect());
let spacing = ui.spacing().item_spacing;
let mut table = TableBuilder::new(ui)
.id_salt(("game table", layout))
.striped(true)
@ -2457,6 +2479,8 @@ impl LargeView {
let r = db.row(index);
let is_marked = marked.contains(index);
row.set_selected(is_marked);
// Where the row's cells are: the cursor's frame follows the row's fill.
let mut cells = egui::Rect::NOTHING;
let key = viewed::key(db.identity(index));
let seen = viewed.contains(key);
let elo = |e: u16| if e > 0 { e.to_string() } else { String::new() };
@ -2469,6 +2493,7 @@ impl LargeView {
};
for (label, key_, _) in &columns {
row.col(|ui| {
cells = cells.union(ui.max_rect());
let cell = match (*label, *key_) {
(l, None) if l == tr("Move") => {
let Some(m) = found else { return };
@ -2550,7 +2575,7 @@ impl LargeView {
// The cursor among several chosen rows, or on a row not chosen: a frame
// (a shape, not a colour; §42.4).
if selected == Some(index) && (many || !is_marked) {
ui_painter_frame(&resp, frame_color, table_clip);
ui_painter_frame(&resp, cells.expand2(spacing / 2.0), frame_color, table_clip);
}
let resp = if about.is_empty() { resp } else { resp.on_hover_text(about.join(" · ")) };
if resp.secondary_clicked() {
@ -2871,17 +2896,12 @@ fn batch_items(ui: &mut Ui, n: usize, writing: Option<&str>) -> Option<BatchItem
}
/// The keyboard cursor's frame: 1 px inside the row.
fn ui_painter_frame(row: &egui::Response, color: Color32, clip: egui::Rect) {
fn ui_painter_frame(row: &egui::Response, rect: egui::Rect, color: Color32, clip: egui::Rect) {
let stroke = Stroke::new(1.0, color);
row.ctx
.layer_painter(row.layer_id)
.with_clip_rect(clip)
.rect_stroke(
row.rect.intersect(clip),
0.0,
stroke,
egui::StrokeKind::Inside,
);
.rect_stroke(rect.intersect(clip), 0.0, stroke, egui::StrokeKind::Inside);
}
/// A condition of the list, shown as a chip.

View file

@ -362,6 +362,9 @@ impl Library {
}) else {
return false;
};
if let Some(view) = self.large.get_mut(source) {
view.batch_error = None;
}
self.batch = Some(BatchJob {
source: source.to_path_buf(),
target,
@ -372,6 +375,13 @@ impl Library {
true
}
/// Why the copy from `path` wrote nothing, said where its progress was.
pub fn set_batch_error(&mut self, path: &Path, text: String) {
if let Some(view) = self.large.get_mut(path) {
view.batch_error = Some(text);
}
}
/// The chosen records of `path` let go (their numbers no longer mean the same games).
pub fn clear_selection(&mut self, path: &Path) {
if let Some(view) = self.large.get_mut(path) {
@ -399,6 +409,18 @@ impl Library {
/// The end of the copy being written, once it came (called every frame, whatever is
/// on the screen); while it runs the source's conditions line shows it.
pub fn poll_batch(&mut self, ctx: &egui::Context) -> Option<BatchEnd> {
let job = self.batch.as_ref()?;
// `CP_BATCH_CANCEL_AFTER=<ms>`: «Cancel» pressed that long after the start (checks,
// screenshots of the cancelling).
let cancel_after = std::env::var("CP_BATCH_CANCEL_AFTER")
.ok()
.and_then(|ms| ms.parse::<f64>().ok());
if let Some(ms) = cancel_after
&& !job.cancelling
&& ctx.input(|i| i.time) - job.work.started >= ms / 1000.0
{
self.cancel_batch();
}
let job = self.batch.as_ref()?;
let Some(result) = job.work.poll() else {
let status = large_db::BatchStatus {

View file

@ -324,6 +324,16 @@ impl Settings {
/// Puts the database first on the shelf; a database there already by another path
/// to the same file (a link) keeps its path. Returns the path on the shelf.
/// Puts `path` at the end of the shelf, unless it is there: a database made in the
/// background appears without moving the cards (task 42).
pub fn add_database_last(&mut self, path: &Path) {
let real = |p: &Path| std::fs::canonicalize(p).ok();
let same = |p: &PathBuf| p == path || real(p).is_some_and(|r| Some(r) == real(path));
if !self.databases.iter().any(same) {
self.databases.push(path.to_path_buf());
}
}
pub fn touch_database(&mut self, path: &Path) -> PathBuf {
let real = |p: &Path| std::fs::canonicalize(p).ok();
let same = |p: &PathBuf| p == path || real(p).is_some_and(|r| Some(r) == real(path));
@ -359,6 +369,25 @@ pub fn write_atomic(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
mod tests {
use super::*;
/// A database made in the background goes to the end; opening it brings it first.
#[test]
fn a_copied_database_goes_to_the_end_of_the_shelf() {
let mut s = Settings::default();
s.databases = vec![PathBuf::from("/a.pgn"), PathBuf::from("/b.pgn")];
s.add_database_last(Path::new("/new.pgn"));
s.add_database_last(Path::new("/a.pgn"));
assert_eq!(
s.databases,
[
PathBuf::from("/a.pgn"),
PathBuf::from("/b.pgn"),
PathBuf::from("/new.pgn")
]
);
s.touch_database(Path::new("/new.pgn"));
assert_eq!(s.databases[0], PathBuf::from("/new.pgn"));
}
/// A database put on the shelf again by a link to its file stays itself, once.
#[cfg(unix)]
#[test]