diff --git a/apps/desktop/src/app.rs b/apps/desktop/src/app.rs index 6a315d7..62a0569 100644 --- a/apps/desktop/src/app.rs +++ b/apps/desktop/src/app.rs @@ -249,6 +249,9 @@ pub struct App { /// between the library and the game (opponent-prep.md §3.1). prep_dialog: Option, prep: Option, + /// Quitting while the opening errors are searched: asked first; «Quit» answered. + prep_quit_ask: bool, + prep_quit_ok: bool, allow_quit: bool, applied_dark: Option, debug: DebugStartup, @@ -421,6 +424,8 @@ impl App { reference: Default::default(), prep_dialog: None, prep: None, + prep_quit_ask: false, + prep_quit_ok: false, engine_offer: crate::engine_offer::EngineOffer::new(), offer_hook_done: false, online_probe: crate::online::Probe::start(ctx), @@ -668,7 +673,7 @@ impl App { exact: None, side, }; - self.prep_dialog = Some(PrepDialog::new(path, entry, &self.settings)); + self.prep_dialog = Some(PrepDialog::new(path, entry)); } let Some(index) = self.debug.open else { return }; let Some(path) = self.settings.databases.first().cloned() else { @@ -793,7 +798,8 @@ impl App { if let Some(p) = self.prep.as_mut() && p.matches(&start.path, &start.player) { - p.shown = true; + // The report as it was, with the colour and the book of the dialog. + p.reopen(start.side, start.book); return; } if let Some(mut old) = self.prep.take() { @@ -802,6 +808,44 @@ impl App { self.prep = Some(PrepScreen::new(start, db, &self.settings, ctx)); } + /// «Opening errors are being searched. Quit and stop?» + fn prep_quit_question(&mut self, ctx: &egui::Context) { + if !self.prep_quit_ask { + return; + } + let mut choice = None; + egui::Modal::new(egui::Id::new("prep_quit")).show(ctx, |ui| { + ui.set_width(400.0); + ui.label(tr("Opening errors are being searched. Quit and stop?")); + ui.label(RichText::new(tr("What was found is kept.")).weak()); + ui.add_space(8.0); + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + if ui.button(tr("Cancel")).clicked() { + choice = Some(false); + } + if crate::widgets::primary(ui, tr("Quit")).clicked() { + choice = Some(true); + } + }); + }); + if choice.is_none() && ctx.input_mut(|i| i.consume_key(Modifiers::NONE, Key::Escape)) { + choice = Some(false); + } + match choice { + Some(true) => { + self.prep_quit_ask = false; + self.prep_quit_ok = true; + if let Some(p) = self.prep.as_mut() { + p.stop(); + } + // The usual way out from here (unsaved games are asked about). + ctx.send_viewport_cmd(egui::ViewportCommand::Close); + } + Some(false) => self.prep_quit_ask = false, + None => {} + } + } + /// The preparation's dialog, when it is open. fn prep_dialog(&mut self, ctx: &egui::Context) { if self.prep_dialog.is_none() { @@ -2808,9 +2852,21 @@ impl eframe::App for App { }); self.applied_dark = Some(self.settings.dark_mode); } + // Quitting while the opening errors are searched: asked first (what was found + // is kept either way). + if ctx.input(|i| i.viewport().close_requested()) + && !self.allow_quit + && !self.prep_quit_ok + && self.prep.as_ref().is_some_and(|p| p.running()) + { + ctx.send_viewport_cmd(egui::ViewportCommand::CancelClose); + self.prep_quit_ask = true; + } + let quitting = !self.prep_quit_ask; // Quitting: a comment being written goes into the game first, so the question // about unsaved changes counts it. - if ctx.input(|i| i.viewport().close_requested()) + if quitting + && ctx.input(|i| i.viewport().close_requested()) && !self.allow_quit && let Some(g) = self.game.as_mut() { @@ -2829,7 +2885,8 @@ impl eframe::App for App { } // Quitting with a modified game left for another (reference tab): show that one, // so the question "save?" is about the game that has the changes. - if ctx.input(|i| i.viewport().close_requested()) + if quitting + && ctx.input(|i| i.viewport().close_requested()) && !self.allow_quit && !self.game.as_ref().is_some_and(|g| g.game.is_modified()) && let Some(k) = self @@ -2841,7 +2898,8 @@ impl eframe::App for App { self.game = self.previous_games.pop(); } // (A game against Maia in progress has asked «The game is on» instead.) - if ctx.input(|i| i.viewport().close_requested()) + if quitting + && ctx.input(|i| i.viewport().close_requested()) && !self.allow_quit && self .game @@ -2920,6 +2978,7 @@ impl eframe::App for App { // The analysis options (its dialog) are kept. self.settings_dirty |= self.settings.review != review; self.reference_tab = game.reference_tab(); + let game_back_previous = game.previous_prep && game.previous.is_some(); // Before the actions: leaving may take the game away. if let Some(res) = game.record_news.take() { let name = game.player_name(); @@ -2928,6 +2987,16 @@ impl eframe::App for App { for a in actions { match a { GameAction::Back => self.request_close(&ctx, CloseIntent::BackToLibrary), + GameAction::BackKey => { + // Ctrl+W: back where the game was opened from (the report, a + // game), else to the library. + let intent = if game_back_previous { + CloseIntent::ReturnToPrevious + } else { + CloseIntent::BackToLibrary + }; + self.request_close(&ctx, intent); + } // The game's database in the library, the game's row shown (its row is // revealed as the game closes). GameAction::OpenDatabase(path) => { @@ -3114,11 +3183,7 @@ impl eframe::App for App { }) && self.prep_dialog.is_none() && let Some((path, entry)) = self.library.prep_entry() { - self.prep_dialog = Some(PrepDialog::new( - path, - entry.unwrap_or_default(), - &self.settings, - )); + self.prep_dialog = Some(PrepDialog::new(path, entry.unwrap_or_default())); } self.online_probe.poll(); self.library.online = self.online_probe.any(); @@ -3188,7 +3253,7 @@ impl eframe::App for App { self.edit_game_data(); } LibraryAction::PrepareAgainst { path, entry } => { - self.prep_dialog = Some(PrepDialog::new(path, entry, &self.settings)); + self.prep_dialog = Some(PrepDialog::new(path, entry)); } } } @@ -3331,6 +3396,7 @@ impl eframe::App for App { self.confirm_dialog(&ctx); self.own_dialogs(&ctx); self.prep_dialog(&ctx); + self.prep_quit_question(&ctx); // Whether the database the dialog names is there, as last found. if let Some(d) = self.play_dialog.as_mut() { d.target_there = d diff --git a/apps/desktop/src/game_view.rs b/apps/desktop/src/game_view.rs index e652813..79b4fa7 100644 --- a/apps/desktop/src/game_view.rs +++ b/apps/desktop/src/game_view.rs @@ -120,6 +120,8 @@ enum SideTab { pub enum GameAction { Back, + /// Ctrl+W: back to what the game was opened from, else to the library. + BackKey, /// Back to the library with this database chosen (the game's own). OpenDatabase(PathBuf), /// «Play on from here»: the play dialog for this position. @@ -1016,7 +1018,9 @@ impl GameView { ui.horizontal(|ui| { if ui .button(if compact { "⬅" } else { tr("⬅ Library") }) - .on_hover_text(if self.previous.is_some() { + .on_hover_text(if self.previous_prep { + tr("Back to the game list") + } else if self.previous.is_some() { tr("Back to the game list (Ctrl+W)") } else { tr("Back to the game list (Esc, Ctrl+W)") @@ -1028,13 +1032,13 @@ impl GameView { if let Some(b) = &back_to && ui .button(if compact { "⬅" } else { b.as_str() }) - .on_hover_text(if compact { - format!( - "{b}\n{}", + .on_hover_text({ + let hint = if self.previous_prep { + tr("The preparation this game was opened from, as you left it (Esc, Ctrl+W)") + } else { tr("The game this one was opened from, as you left it (Esc)") - ) - } else { - tr("The game this one was opened from, as you left it (Esc)").to_owned() + }; + if compact { format!("{b}\n{hint}") } else { hint.to_owned() } }) .clicked() { @@ -4826,7 +4830,7 @@ impl GameView { } "add_to_db" => actions.push(GameAction::AddToDatabase), "game_data" => actions.push(GameAction::EditGameData), - "back" => actions.push(GameAction::Back), + "back" => actions.push(GameAction::BackKey), "find" if self.search_line().is_some() => { actions.push(GameAction::FindPosition(None)) } diff --git a/apps/desktop/src/i18n.rs b/apps/desktop/src/i18n.rs index 798d1df..8303654 100644 --- a/apps/desktop/src/i18n.rs +++ b/apps/desktop/src/i18n.rs @@ -14060,17 +14060,6 @@ const ROWS: &[[&str; 9]] = &[ "Daraxt va dvijok qanchalik chuqur qaraydi", "Ағаш пен қозғалтқыш қаншалықты терең қарайды", ], - [ - "♙ Board", - "♙ Доска", - "♙ Échiquier", - "♙ Brett", - "♙ Tablero", - "♙ Scacchiera", - "♙ Tabuleiro", - "♙ Taxta", - "♙ Тақта", - ], [ "The board of the chosen position", "Доска выбранной позиции", @@ -14346,28 +14335,6 @@ const ROWS: &[[&str; 9]] = &[ "Birinchi yurishlar", "Алғашқы жүрістер", ], - [ - "♙ White", - "♙ Белыми", - "♙ Blancs", - "♙ Weiß", - "♙ Blancas", - "♙ Bianco", - "♙ Brancas", - "♙ Oq", - "♙ Ақ", - ], - [ - "♟ Black", - "♟ Чёрными", - "♟ Noirs", - "♟ Schwarz", - "♟ Negras", - "♟ Nero", - "♟ Pretas", - "♟ Qora", - "♟ Қара", - ], [ "no name", "без названия", @@ -14434,17 +14401,6 @@ const ROWS: &[[&str; 9]] = &[ "O‘sha pozitsiyalar «Oddiy» sifatda", "Сол позициялар «Қалыпты» сапада", ], - [ - "of the checked positions", - "из проверенных позиций", - "sur les positions vérifiées", - "von den geprüften Stellungen", - "de las posiciones comprobadas", - "delle posizioni verificate", - "das posições verificadas", - "tekshirilgan pozitsiyalardan", - "тексерілген позициялардан", - ], [ "Repeated errors — the same position, the same move", "Повторяющиеся ошибки — одна и та же позиция, тот же ход", @@ -14456,28 +14412,6 @@ const ROWS: &[[&str; 9]] = &[ "Takrorlanuvchi xatolar — o‘sha pozitsiya, o‘sha yurish", "Қайталанатын қателер — сол позиция, сол жүріс", ], - [ - "None so far", - "Пока нет", - "Aucune pour l'instant", - "Bisher keine", - "Ninguno por ahora", - "Nessuno finora", - "Nenhum até agora", - "Hozircha yo‘q", - "Әзірге жоқ", - ], - [ - "Position", - "Позиция", - "Position", - "Stellung", - "Posición", - "Posizione", - "Posição", - "Pozitsiya", - "Позиция", - ], [ "Played", "Сыграл", @@ -14687,17 +14621,6 @@ const ROWS: &[[&str; 9]] = &[ "▶ Xatolar ≈ {time}", "▶ Қателер ≈ {time}", ], - [ - "Analyse {n} more positions ≈ {time}", - "Доразобрать {n} позиций ≈ {time}", - "Analyser {n} positions de plus ≈ {time}", - "{n} weitere Stellungen analysieren ≈ {time}", - "Analizar {n} posiciones más ≈ {time}", - "Analizza altre {n} posizioni ≈ {time}", - "Analisar mais {n} posições ≈ {time}", - "Yana {n} pozitsiyani tahlil qilish ≈ {time}", - "Тағы {n} позицияны талдау ≈ {time}", - ], [ "▶ Find opening errors ≈ {time}", "▶ Найти ошибки в дебютах ≈ {time}", @@ -14753,17 +14676,6 @@ const ROWS: &[[&str; 9]] = &[ "kitobdan {n}-yurishda chiqadi, partiyalarning {p} % ida birinchi bo‘lib", "кітаптан {n}-жүрісте шығады, партиялардың {p} % інде бірінші болып", ], - [ - "Opening errors: {i} inaccuracies · {m} mistakes · {b} blunders — in {g} games ({p} %)", - "Ошибок в дебюте: неточностей {i} · ошибок {m} · зевков {b} — в {g} партиях ({p} %)", - "Erreurs d'ouverture : {i} imprécisions · {m} erreurs · {b} gaffes — dans {g} parties ({p} %)", - "Eröffnungsfehler: {i} Ungenauigkeiten · {m} Fehler · {b} grobe Fehler — in {g} Partien ({p} %)", - "Errores de apertura: {i} imprecisiones · {m} errores · {b} errores graves — en {g} partidas ({p} %)", - "Errori di apertura: {i} imprecisioni · {m} errori · {b} svarioni — in {g} partite ({p} %)", - "Erros de abertura: {i} imprecisões · {m} erros · {b} erros graves — em {g} partidas ({p} %)", - "Debyut xatolari: {i} noaniqlik · {m} xato · {b} qo‘pol xato — {g} partiyada ({p} %)", - "Дебют қателері: {i} дәлсіздік · {m} қате · {b} өрескел қате — {g} партияда ({p} %)", - ], [ "Few games ({n}): percentages and scores are unreliable", "Партий мало ({n}) — проценты и счёт ненадёжны", @@ -14819,17 +14731,6 @@ const ROWS: &[[&str; 9]] = &[ "Boshqa tartibda ham kelingan: {orders}", "Басқа жүріс ретімен де келген: {orders}", ], - [ - "Mistake: better {best} ({before} → {after}) · in {n} games", - "Ошибка: лучше {best} (оценка {before} → {after}) · в {n} партиях", - "Erreur : mieux {best} ({before} → {after}) · dans {n} parties", - "Fehler: besser {best} ({before} → {after}) · in {n} Partien", - "Error: mejor {best} ({before} → {after}) · en {n} partidas", - "Errore: meglio {best} ({before} → {after}) · in {n} partite", - "Erro: melhor {best} ({before} → {after}) · em {n} partidas", - "Xato: yaxshisi {best} ({before} → {after}) · {n} partiyada", - "Қате: жақсысы {best} ({before} → {after}) · {n} партияда", - ], [ "Here he leaves the book: in “{book}” this position occurs {n} times", "Здесь он выходит из книги: в «{book}» такая позиция встречается {n} раз", @@ -14885,17 +14786,6 @@ const ROWS: &[[&str; 9]] = &[ "{total} dan {done} pozitsiya tekshirildi", "{total} ішінен {done} позиция тексерілді", ], - [ - "{i} inaccuracies · {m} mistakes · {b} blunders — in {g} games of {t} ({p} %)", - "неточностей {i} · ошибок {m} · зевков {b} — в {g} партиях из {t} ({p} %)", - "{i} imprécisions · {m} erreurs · {b} gaffes — dans {g} parties sur {t} ({p} %)", - "{i} Ungenauigkeiten · {m} Fehler · {b} grobe Fehler — in {g} von {t} Partien ({p} %)", - "{i} imprecisiones · {m} errores · {b} errores graves — en {g} partidas de {t} ({p} %)", - "{i} imprecisioni · {m} errori · {b} svarioni — in {g} partite su {t} ({p} %)", - "{i} imprecisões · {m} erros · {b} erros graves — em {g} partidas de {t} ({p} %)", - "{i} noaniqlik · {m} xato · {b} qo‘pol xato — {t} tadan {g} partiyada ({p} %)", - "{i} дәлсіздік · {m} қате · {b} өрескел қате — {t} ішінен {g} партияда ({p} %)", - ], [ "No errors in the first {n} moves ({i} inaccuracies).", "В первых {n} ходах ошибок не нашлось (неточностей {i}).", @@ -14984,6 +14874,204 @@ const ROWS: &[[&str; 9]] = &[ "{played} — yaxshisi {best} · {before} → {after}", "{played} — жақсысы {best} · {before} → {after}", ], + [ + "Opening errors are being searched. Quit and stop?", + "Идёт поиск ошибок в дебютах. Выйти и остановить?", + "La recherche d'erreurs d'ouverture est en cours. Quitter et l'arrêter ?", + "Die Suche nach Eröffnungsfehlern läuft. Beenden und stoppen?", + "Se están buscando errores de apertura. ¿Salir y detenerla?", + "È in corso la ricerca degli errori di apertura. Uscire e fermarla?", + "A busca de erros de abertura está em andamento. Sair e parar?", + "Debyut xatolari qidirilmoqda. Chiqib, to‘xtatilsinmi?", + "Дебют қателері ізделуде. Шығып, тоқтату керек пе?", + ], + [ + "What was found is kept.", + "Найденное сохранится.", + "Ce qui a été trouvé est conservé.", + "Was gefunden wurde, bleibt erhalten.", + "Lo encontrado se conserva.", + "Quanto trovato viene conservato.", + "O que foi encontrado é mantido.", + "Topilganlar saqlanadi.", + "Табылғаны сақталады.", + ], + [ + "Quit", + "Выйти", + "Quitter", + "Beenden", + "Salir", + "Esci", + "Sair", + "Chiqish", + "Шығу", + ], + [ + "Back to the game list", + "Назад к списку партий", + "Retour à la liste des parties", + "Zurück zur Partienliste", + "Volver a la lista de partidas", + "Torna all'elenco delle partite", + "Voltar à lista de partidas", + "Partiyalar ro‘yxatiga qaytish", + "Партиялар тізіміне оралу", + ], + [ + "The preparation this game was opened from, as you left it (Esc, Ctrl+W)", + "Подготовка, из которой открыта партия, — такой, как вы её оставили (Esc, Ctrl+W)", + "La préparation d'où cette partie a été ouverte, telle que vous l'avez laissée (Échap, Ctrl+W)", + "Die Vorbereitung, aus der diese Partie geöffnet wurde, so wie Sie sie verlassen haben (Esc, Strg+W)", + "La preparación desde la que se abrió esta partida, tal como la dejó (Esc, Ctrl+W)", + "La preparazione da cui è stata aperta questa partita, come l'hai lasciata (Esc, Ctrl+W)", + "A preparação de onde esta partida foi aberta, como você a deixou (Esc, Ctrl+W)", + "Bu partiya ochilgan tayyorgarlik — siz qoldirgan holatda (Esc, Ctrl+W)", + "Осы партия ашылған дайындық — сіз қалдырған күйінде (Esc, Ctrl+W)", + ], + [ + "no errors", + "ошибок нет", + "aucune erreur", + "keine Fehler", + "sin errores", + "nessun errore", + "sem erros", + "xatolar yo‘q", + "қателер жоқ", + ], + [ + "Share of the colour's games; a variation's — of its family's", + "Доля от партий цветом; у варианта — от семейства", + "Part des parties de cette couleur ; pour une variante — de sa famille", + "Anteil an den Partien dieser Farbe; bei einer Variante — an ihrer Familie", + "Parte de las partidas de ese color; de una variante — de su familia", + "Quota delle partite di quel colore; di una variante — della sua famiglia", + "Parte das partidas dessa cor; de uma variante — da sua família", + "Shu rangdagi partiyalardan ulush; variantda — oilasidan", + "Осы түстегі партиялардан үлес; нұсқада — тобынан", + ], + [ + "The counts are of the checked positions", + "Числа — по проверенным позициям", + "Les nombres portent sur les positions vérifiées", + "Die Zahlen gelten für die geprüften Stellungen", + "Las cifras son de las posiciones comprobadas", + "I numeri riguardano le posizioni verificate", + "Os números são das posições verificadas", + "Sonlar tekshirilgan pozitsiyalar bo‘yicha", + "Сандар тексерілген позициялар бойынша", + ], + [ + "Show the inaccuracies (?!) with the mistakes and blunders", + "Показывать неточности (?!) вместе с ошибками и зевками", + "Afficher les imprécisions (?!) avec les erreurs et les gaffes", + "Ungenauigkeiten (?!) mit Fehlern und groben Fehlern zeigen", + "Mostrar las imprecisiones (?!) con los errores y errores graves", + "Mostra le imprecisioni (?!) con errori e svarioni", + "Mostrar as imprecisões (?!) com os erros e erros graves", + "Noaniqliklarni (?!) xato va qo‘pol xatolar bilan ko‘rsatish", + "Дәлсіздіктерді (?!) қателер мен өрескел қателермен бірге көрсету", + ], + [ + "Book: {name}", + "Книга: {name}", + "Livre : {name}", + "Buch: {name}", + "Libro: {name}", + "Libro: {name}", + "Livro: {name}", + "Kitob: {name}", + "Кітап: {name}", + ], + [ + "White wins {w} % · draws {d} % · Black wins {b} %", + "Победы белых {w} % · ничьи {d} % · победы чёрных {b} %", + "Victoires des Blancs {w} % · nulles {d} % · victoires des Noirs {b} %", + "Weißsiege {w} % · Remis {d} % · Schwarzsiege {b} %", + "Victorias blancas {w} % · tablas {d} % · victorias negras {b} %", + "Vittorie del Bianco {w} % · patte {d} % · vittorie del Nero {b} %", + "Vitórias das brancas {w} % · empates {d} % · vitórias das pretas {b} %", + "Oq g‘alabalari {w} % · durang {d} % · qora g‘alabalari {b} %", + "Ақ жеңістері {w} % · тең {d} % · қара жеңістері {b} %", + ], + [ + "{n} moves", + "{n} ходов", + "{n} coups", + "{n} Züge", + "{n} jugadas", + "{n} mosse", + "{n} lances", + "{n} yurish", + "{n} жүріс", + ], + [ + "{n} m.", + "{n} х.", + "{n} c.", + "{n} Z.", + "{n} j.", + "{n} m.", + "{n} l.", + "{n} yur.", + "{n} ж.", + ], + [ + "Continue ≈ {time}", + "Продолжить ≈ {time}", + "Continuer ≈ {time}", + "Fortsetzen ≈ {time}", + "Continuar ≈ {time}", + "Continua ≈ {time}", + "Continuar ≈ {time}", + "Davom ettirish ≈ {time}", + "Жалғастыру ≈ {time}", + ], + [ + "Errors found · {quality} · {n} positions", + "Ошибки найдены · {quality} · {n} позиций", + "Erreurs trouvées · {quality} · {n} positions", + "Fehler gefunden · {quality} · {n} Stellungen", + "Errores encontrados · {quality} · {n} posiciones", + "Errori trovati · {quality} · {n} posizioni", + "Erros encontrados · {quality} · {n} posições", + "Xatolar topildi · {quality} · {n} pozitsiya", + "Қателер табылды · {quality} · {n} позиция", + ], + [ + "in {p} % of games", + "в {p} % партий", + "dans {p} % des parties", + "in {p} % der Partien", + "en el {p} % de las partidas", + "nel {p} % delle partite", + "em {p} % das partidas", + "partiyalarning {p} % ida", + "партиялардың {p} % інде", + ], + [ + "others ({n})", + "другие ({n})", + "autres ({n})", + "andere ({n})", + "otras ({n})", + "altre ({n})", + "outros ({n})", + "boshqalar ({n})", + "басқалары ({n})", + ], + [ + "{judgement}: better {best} ({before} → {after}) · in {n} games", + "{judgement}: лучше {best} (оценка {before} → {after}) · в {n} партиях", + "{judgement} : mieux {best} ({before} → {after}) · dans {n} parties", + "{judgement}: besser {best} ({before} → {after}) · in {n} Partien", + "{judgement}: mejor {best} ({before} → {after}) · en {n} partidas", + "{judgement}: meglio {best} ({before} → {after}) · in {n} partite", + "{judgement}: melhor {best} ({before} → {after}) · em {n} partidas", + "{judgement}: yaxshisi {best} ({before} → {after}) · {n} partiyada", + "{judgement}: жақсысы {best} ({before} → {after}) · {n} партияда", + ], ]; #[cfg(test)] diff --git a/apps/desktop/src/prep_ui.rs b/apps/desktop/src/prep_ui.rs index 9ada6d6..34649fd 100644 --- a/apps/desktop/src/prep_ui.rs +++ b/apps/desktop/src/prep_ui.rs @@ -24,8 +24,9 @@ use shakmaty::uci::UciMove; use shakmaty::{Chess, Color, Move, Position, Square}; use crate::board::{Arrow, BoardState, BoardView}; -use crate::board_search::{self, Background, PREVIEW_ARROW}; +use crate::board_search::{Background, PREVIEW_ARROW}; use crate::i18n::{tr, trf}; +use crate::large_db::grouped; use crate::reference::RefDb; use crate::review::{self, DialogAction}; use crate::settings::{REVIEW_QUALITY, Settings}; @@ -142,13 +143,22 @@ fn book_picker( id: &str, current: &BookChoice, own: &Path, - own_name: &str, dbs: &[RefDb], width: f32, + with_caption: bool, ) -> Option { let mut picked = None; + let own_name = base_name(own); + let own_name = own_name.as_str(); + let label = current.label(own_name, dbs); + // Without its caption beside it, «None» alone would say nothing. + let shown = if with_caption { + trf("Book: {name}", &[("name", &label)]) + } else { + label + }; egui::ComboBox::from_id_salt(id) - .selected_text(current.label(own_name, dbs)) + .selected_text(shown) .width(width) .truncate() .show_ui(ui, |ui| { @@ -260,7 +270,8 @@ pub struct PrepDialog { /// The name the dialog was opened with (ticked by itself when found). wanted: Option, side: ColorChoice, - book: BookChoice, + /// The book; `None` until the shelf is known (the setting names a database of it). + book: Option, /// Games of the ticked names by colour, and the job counting them. counts: Option<(Vec, (u32, u32))>, count_job: Option<(Vec, CountJob)>, @@ -271,9 +282,9 @@ pub struct PrepDialog { } impl PrepDialog { - pub fn new(path: PathBuf, entry: PrepEntry, settings: &Settings) -> Self { + pub fn new(path: PathBuf, entry: PrepEntry) -> Self { PrepDialog { - book: BookChoice::from_setting(settings.prep.book.as_deref(), &path, &[]), + book: None, path, dbs: Vec::new(), query: entry.query, @@ -386,10 +397,12 @@ impl PrepDialog { self.poll(ctx, db); } let own_name = base_name(&self.path); - if self.book == BookChoice::None && settings.prep.book.is_none() && !self.dbs.is_empty() { - // The default book: the largest database on the shelf (once the shelf is known). - self.book = BookChoice::from_setting(None, &self.path, &self.dbs); + if self.book.is_none() && !self.dbs.is_empty() { + // The book chosen last time, else the largest database on the shelf: once + // the shelf is known. + self.book = Some(BookChoice::from_setting(settings.prep.book.as_deref(), &self.path, &self.dbs)); } + let book = self.book.clone().unwrap_or(BookChoice::None); let mut outcome = None; let ready = db.is_some() && !self.ticked.is_empty(); let ticked_names: Vec = self @@ -454,11 +467,11 @@ impl PrepDialog { self.ticked.clear(); self.counts = None; self.count_job = None; - self.book = BookChoice::from_setting( + self.book = Some(BookChoice::from_setting( settings.prep.book.as_deref(), &self.path, &self.dbs, - ); + )); } }); ui.horizontal(|ui| { @@ -524,7 +537,6 @@ impl PrepDialog { ui.add_space(8.0); ui.horizontal_wrapped(|ui| { ui.add_sized([label_w, 20.0], egui::Label::new(tr("Colour"))); - ui.radio_value(&mut self.side, ColorChoice::Both, tr("Both")); let (w, b) = match counts { Some((w, b)) => (w.to_string(), b.to_string()), None if self.ticked.is_empty() => ("—".to_owned(), "—".to_owned()), @@ -540,13 +552,14 @@ impl PrepDialog { ColorChoice::Black, trf("Black — {n}", &[("n", &b)]), ); + ui.radio_value(&mut self.side, ColorChoice::Both, tr("Both")); }); ui.horizontal(|ui| { ui.add_sized([label_w, 20.0], egui::Label::new(tr("Book"))); let width = ui.available_width() - 8.0; - if let Some(b) = book_picker(ui, "prep_dialog_book", &self.book, &self.path, &own_name, &self.dbs, width) { - self.book = b; - settings.prep.book = Some(self.book.setting(&self.path)); + if let Some(b) = book_picker(ui, "prep_dialog_book", &book, &self.path, &self.dbs, width, false) { + settings.prep.book = Some(b.setting(&self.path)); + self.book = Some(b); } }); ui.label( @@ -591,7 +604,7 @@ impl PrepDialog { path: self.path.clone(), player: Player::new(self.ticked.iter().copied()), side: self.side, - book: self.book.clone(), + book: self.book.clone().unwrap_or(BookChoice::None), } } @@ -815,6 +828,46 @@ enum TreeRow { Others { white: bool, parent: u32, depth: usize, count: usize }, } +/// A row of the errors tab. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum ErrRow { + /// «Repeated errors — …» / «By opening». + RepeatedTitle, + OpeningsTitle, + /// A repeated error (colour, index into `repeated`) and one of its games. + Repeated { white: bool, index: usize }, + RepeatedGame { white: bool, index: usize, game: u32 }, + /// A variation with errors (colour, family, variation) and one of its errors. + Variation { white: bool, family: usize, variation: usize }, + Error { white: bool, index: usize }, +} + +impl ErrRow { + fn is_title(self) -> bool { + matches!(self, ErrRow::RepeatedTitle | ErrRow::OpeningsTitle) + } +} + +/// The engine part of the controls. +enum EngineTexts { + Running, + /// A button to start or go on, and a note beside it. + Button { label: String, note: Option }, + /// Done: a quiet label. + Done { label: String, note: Option }, +} + +/// Where the engine run stands for the colour(s) shown. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum EngineState { + Running, + /// No evaluation yet. + NotRun, + /// Some positions evaluated (a run stopped, a new depth, new games). + Partial { done: usize, total: usize }, + Complete { total: usize }, +} + /// The default expansion: nodes with at least this many games, this deep. const EXPAND_GAMES: u32 = 5; const EXPAND_PLIES: u8 = 6; @@ -868,6 +921,18 @@ pub struct PrepScreen { others_open: HashSet<(bool, u32)>, open_families: HashSet<(bool, usize)>, open_repeated: HashSet<(bool, usize)>, + open_variations: HashSet<(bool, usize, usize)>, + /// The row of the errors tab the keyboard is on. + err_cursor: Option, + /// The fingerprint of the chosen book, once it is open (`None`: no book), and the + /// book and its database it was taken from. + book_fp: Option, + book_fp_of: Option<(BookChoice, usize)>, + /// Inaccuracies shown (`settings.prep.inaccuracies`, read every frame). + show_inaccuracies: bool, + /// The board stands over the lists (a narrow or low window); the window is low. + stacked: bool, + low_window: bool, selection: Selection, shown_pos: Option, /// The user turned the board (else it is the player's colour). @@ -927,17 +992,54 @@ fn line_text(sans: &[String]) -> String { crate::san::show_line(&out) } -/// "…" in the middle when the text is longer than `max` characters: the first and -/// the last moves of a line matter more than the middle. -fn shorten_middle(s: &str, max: usize) -> String { - let n = s.chars().count(); - if n <= max { - return s.to_owned(); +/// The table's line ("1. e4 c5 2. Nf3") as the program writes lines: moves with +/// their numbers joined ("1.e4", "c5", "2.Nf3"). +fn table_line(line: &str) -> Vec { + let mut out: Vec = Vec::new(); + let mut number: Option<&str> = None; + for tok in line.split_whitespace() { + if tok.ends_with('.') && tok.trim_end_matches('.').chars().all(|c| c.is_ascii_digit()) { + number = Some(tok); + } else { + out.push(match number.take() { + Some(n) if n.ends_with("...") => format!("{}…{tok}", n.trim_end_matches('.')), + Some(n) => format!("{n}{tok}"), + None => tok.to_owned(), + }); + } + } + out +} + +/// The moves in `width`: whole moves, the first and the last kept, «…» for the middle +/// ones that do not fit (they matter less). +fn fit_line(ui: &Ui, tokens: &[String], width: f32) -> String { + let fits = |t: &str| text_width(ui, t, egui::TextStyle::Body) <= width; + let full = crate::san::show_line(&tokens.join(" ")); + if fits(&full) || tokens.len() < 3 { + return full; + } + // Drop moves from the middle until the rest fits. + let mut head = tokens.len().div_ceil(2); + let mut tail = tokens.len() - head; + loop { + if head > 1 && head >= tail { + head -= 1; + } else if tail > 1 { + tail -= 1; + } else { + return crate::san::show_line(&format!("{} … {}", tokens[0], tokens[tokens.len() - 1])); + } + let text = format!( + "{} … {}", + tokens[..head].join(" "), + tokens[tokens.len() - tail..].join(" ") + ); + let text = crate::san::show_line(&text); + if fits(&text) { + return text; + } } - let keep = max.saturating_sub(1) / 2; - let head: String = s.chars().take(keep).collect(); - let tail: String = s.chars().skip(n - keep).collect(); - format!("{head}…{tail}") } /// "01.2026 – 10.2026" from the games' dates (empty without dates). @@ -962,15 +1064,45 @@ fn period(games: &[GameInfo]) -> String { } } -/// The player's tally as the result bar wants it (White's wins · draws · Black's wins). -fn bar_tally(t: &Tally, white: bool) -> chess_import::position::Tally { +/// The results as a bar, by lightness (White's wins light, draws grey, Black's wins +/// dark, as on the search board) with no numbers in it: the score beside it is the +/// player's, the percentages are in its tooltip. Faint over few games. +fn score_bar(ui: &mut Ui, t: &Tally, white: bool, few: bool) { + let (rect, resp) = ui.allocate_exact_size(vec2(56.0, 12.0), Sense::hover()); let (white_wins, black_wins) = if white { (t.wins, t.losses) } else { (t.losses, t.wins) }; - chess_import::position::Tally { - games: t.games, - white_wins, - draws: t.draws, - black_wins, - ..Default::default() + let decided = white_wins + t.draws + black_wins; + let fade = |c: Color32| if few { c.gamma_multiply(0.45) } else { c }; + let painter = ui.painter(); + let outline = if ui.visuals().dark_mode { + Color32::from_gray(0x70) + } else { + ui.visuals().weak_text_color() + }; + if decided > 0 { + let mut x = rect.left(); + for (n, fill) in [ + (white_wins, Color32::from_gray(0xe8)), + (t.draws, Color32::from_gray(0x8c)), + (black_wins, Color32::from_gray(0x2b)), + ] { + let w = rect.width() * n as f32 / decided as f32; + if w > 0.0 { + painter.rect_filled( + egui::Rect::from_min_max(egui::pos2(x, rect.top()), egui::pos2(x + w, rect.bottom())), + 0.0, + fade(fill), + ); + } + x += w; + } + } + painter.rect_stroke(rect, 2.0, egui::Stroke::new(1.0, fade(outline)), egui::StrokeKind::Inside); + if decided > 0 { + let pct = |n: u32| 100 * n / decided; + resp.on_hover_text(trf( + "White wins {w} % · draws {d} % · Black wins {b} %", + &[("w", &pct(white_wins)), ("d", &pct(t.draws)), ("b", &pct(black_wins))], + )); } } @@ -1008,6 +1140,20 @@ fn pill(ui: &mut Ui, j: Judgement) -> egui::Response { resp } +/// The width of [`counts_text`]. +fn errors_text_width(ui: &Ui, c: &ErrorCounts) -> f32 { + let parts: Vec = [ + (c.inaccuracies, Judgement::Inaccuracy), + (c.mistakes, Judgement::Mistake), + (c.blunders, Judgement::Blunder), + ] + .iter() + .filter(|(n, _)| *n > 0) + .map(|(n, j)| format!("{n} {}", review::judgement_symbol(*j))) + .collect(); + text_width(ui, &parts.join(" · "), egui::TextStyle::Body) + 8.0 * parts.len() as f32 +} + /// "3 ? · 1 ??" in the colours of the signs (nothing when there are none). fn counts_text(ui: &mut Ui, c: &ErrorCounts) { let dark = ui.visuals().dark_mode; @@ -1078,6 +1224,13 @@ impl PrepScreen { others_open: HashSet::new(), open_families: HashSet::new(), open_repeated: HashSet::new(), + open_variations: HashSet::new(), + err_cursor: None, + book_fp: None, + book_fp_of: None, + show_inaccuracies: settings.prep.inaccuracies, + stacked: false, + low_window: false, selection: Selection::None, shown_pos: None, flipped: None, @@ -1174,11 +1327,174 @@ impl PrepScreen { } } } + // A book step of the report this one replaces is not this report's. + self.book_job = None; self.report = Some(report); self.rebuild_marks(); self.selection = Selection::None; self.shown_pos = None; self.games_cursor = None; + self.err_cursor = None; + } + + /// The same player asked for again from the dialog: its colour and book. + pub fn reopen(&mut self, side: ColorChoice, book: BookChoice) { + self.shown = true; + self.set_color(side); + if book != self.book { + self.book = book; + self.book_failed = None; + self.book_asked = None; + } + } + + fn set_color(&mut self, color: ColorChoice) { + self.color = color; + // A selection of a colour no longer shown is dropped. + let keep = match &self.selection { + Selection::None => true, + Selection::Node { white, .. } + | Selection::Opening { white, .. } + | Selection::Repeated { white, .. } + | Selection::Error { white, .. } => color.colours().contains(white), + }; + if !keep { + self.select(Selection::None); + } + } + + /// Another depth: the report of that depth from the cache, else built anew. + fn set_plies(&mut self, plies: usize, settings: &mut Settings, ctx: &egui::Context) { + if plies == self.plies { + return; + } + self.stop_engine(&mut Vec::new()); + self.save(); + self.plies = plies; + settings.prep.plies = plies; + self.report = None; + let options = Options { + plies, + side: Side::Any, + }; + let cached = cache_dir() + .as_deref() + .and_then(|d| PrepReport::load(d, &self.db, &self.player, &options)); + match cached { + Some(r) => self.take_report(r, settings), + None => self.rebuild(ctx), + } + } + + /// The report's book is the one chosen (else its column and line are not shown). + fn book_ok(&self) -> bool { + let Some(report) = &self.report else { + return false; + }; + self.book_fp.is_some() + && report.book.as_ref().map(|b| b.fingerprint) == self.book_fp + } + + /// The judgement is shown (inaccuracies can be hidden). + fn visible(&self, j: Judgement) -> bool { + self.show_inaccuracies || j != Judgement::Inaccuracy + } + + /// The counts as shown. + fn shown_counts(&self, c: &ErrorCounts) -> ErrorCounts { + let mut c = *c; + if !self.show_inaccuracies { + c.inaccuracies = 0; + } + c + } + + /// The engine run for the colour(s) shown. + fn engine_state(&self, settings: &Settings) -> EngineState { + if self.job.is_some() { + return EngineState::Running; + } + let Some(report) = &self.report else { + return EngineState::NotRun; + }; + if !report.evaluated() { + return EngineState::NotRun; + } + let side = self.color.side(); + let total = report.distinct_positions_for(side); + let missing = report.missing_for(settings.prep.nodes(), side); + if missing == 0 { + EngineState::Complete { total } + } else { + EngineState::Partial { + done: total - missing, + total, + } + } + } + + /// Judges the errors anew (`f` changes the evaluations): the lists of errors are + /// rebuilt, so a chosen error is found again in them by what it is, not by its + /// place (else the choice is dropped). + fn rejudge(&mut self, f: impl FnOnce(&mut PrepReport)) { + let Some(report) = self.report.as_mut() else { return }; + // What the selection and the errors tab's row are, by content. + let error_id = |r: &PrepReport, white: bool, i: usize| { + r.color(white).errors.get(i).map(|e| (e.game, e.ply)) + }; + let repeated_id = |r: &PrepReport, white: bool, i: usize| { + r.color(white).repeated.get(i).map(|x| (x.key, x.uci.clone())) + }; + let sel = match &self.selection { + Selection::Error { white, index } => error_id(report, *white, *index).map(|id| (*white, Ok(id))), + Selection::Repeated { white, index } => repeated_id(report, *white, *index).map(|id| (*white, Err(id))), + _ => None, + }; + let cursor = match self.err_cursor { + Some(ErrRow::Error { white, index }) => error_id(report, white, index).map(|id| (white, Ok(id))), + Some(ErrRow::Repeated { white, index }) => repeated_id(report, white, index).map(|id| (white, Err(id))), + _ => None, + }; + let open: Vec<(bool, (u64, String))> = self + .open_repeated + .iter() + .filter_map(|&(w, i)| repeated_id(report, w, i).map(|id| (w, id))) + .collect(); + f(report); + let find = |r: &PrepReport, white: bool, id: &Result<(u32, u32), (u64, String)>| -> Option { + match id { + Ok((game, ply)) => r.color(white).errors.iter().position(|e| e.game == *game && e.ply == *ply), + Err((key, uci)) => r.color(white).repeated.iter().position(|x| x.key == *key && x.uci == *uci), + } + }; + if let Some((white, id)) = &sel { + let found = find(report, *white, id); + self.selection = match (found, id) { + (Some(index), Ok(_)) => Selection::Error { white: *white, index }, + (Some(index), Err(_)) => Selection::Repeated { white: *white, index }, + (None, _) => Selection::None, + }; + if found.is_none() { + self.shown_pos = None; + } + } + if let Some((white, id)) = &cursor { + self.err_cursor = match (find(report, *white, id), id) { + (Some(index), Ok(_)) => Some(ErrRow::Error { white: *white, index }), + (Some(index), Err(_)) => Some(ErrRow::Repeated { white: *white, index }), + (None, _) => None, + }; + } else if matches!(self.err_cursor, Some(ErrRow::RepeatedGame { .. })) { + self.err_cursor = None; + } + self.open_repeated = open + .into_iter() + .filter_map(|(w, (key, uci))| { + let i = report.color(w).repeated.iter().position(|x| x.key == key && x.uci == uci)?; + Some((w, i)) + }) + .collect(); + self.rebuild_marks(); } /// The errors by node, for the tree and the hover texts. @@ -1216,6 +1532,7 @@ impl PrepScreen { None => self.error = Some(tr("The games could not be read").to_owned()), } } + self.show_inaccuracies = settings.prep.inaccuracies; if let Some((fp, job)) = &self.book_job && let Some(r) = job.poll() { @@ -1258,26 +1575,35 @@ impl PrepScreen { if self.book_job.is_some() || self.build.is_some() { return; } - let (book_db, same) = match &self.book { - BookChoice::None => return, - BookChoice::Same => (self.db.clone(), true), - BookChoice::Database(p) => { - let Some(d) = self.dbs.iter().find(|d| d.path == *p) else { - return; - }; - match &d.db { - Some(db) => (db.clone(), false), + let book = match &self.book { + BookChoice::None => None, + BookChoice::Same => Some((self.db.clone(), true)), + BookChoice::Database(p) => match self.dbs.iter().find(|d| d.path == *p) { + Some(d) => match &d.db { + Some(db) => Some((db.clone(), false)), None => { if d.failed.is_none() && self.book_asked.as_ref() != Some(p) { self.book_asked = Some(p.clone()); actions.push(PrepAction::LoadDb(p.clone())); } - return; + None } - } - } + }, + None => None, + }, }; - let fp = prep::fingerprint(&book_db); + let Some((book_db, same)) = book else { + self.book_fp = None; + self.book_fp_of = None; + return; + }; + // The fingerprint reads the file's data: once per book and database read. + let of = (self.book.clone(), Arc::as_ptr(&book_db) as usize); + if self.book_fp_of.as_ref() != Some(&of) { + self.book_fp = Some(prep::fingerprint(&book_db)); + self.book_fp_of = Some(of); + } + let Some(fp) = self.book_fp else { return }; if report.book.as_ref().is_some_and(|b| b.fingerprint == fp) || self.book_failed.as_ref().is_some_and(|(f, _)| *f == fp) { @@ -1300,9 +1626,10 @@ impl PrepScreen { /// The book's count in a position, when the book step looked at it. fn book_count(&self, key: u64) -> Option { - let report = self.report.as_ref()?; - report.book.as_ref()?; - report.book_counts.get(&key).copied() + if !self.book_ok() { + return None; + } + self.report.as_ref()?.book_counts.get(&key).copied() } // ----- the engine ---------------------------------------------------------------- @@ -1341,11 +1668,13 @@ impl PrepScreen { actions.push(PrepAction::NeedEngine); return; }; - let Some(report) = self.report.as_mut() else { return }; let nodes = settings.prep.nodes(); - if report.take_cached(&self.cache, nodes) > 0 { - self.dirty = true; - } + let cache = std::mem::take(&mut self.cache); + let mut taken = 0; + self.rejudge(|r| taken = r.take_cached(&cache, nodes)); + self.cache = cache; + self.dirty |= taken > 0; + let Some(report) = self.report.as_ref() else { return }; let targets = report.targets_for(nodes, self.color.side()); if targets.is_empty() { actions.push(PrepAction::Toast(tr("Every position is already evaluated").to_owned())); @@ -1442,10 +1771,9 @@ impl PrepScreen { } let now = ctx.input(|i| i.time); if self.pending_refresh && (ended || now - self.last_refresh > 1.0) { - report.refresh(); self.pending_refresh = false; self.last_refresh = now; - self.rebuild_marks(); + self.rejudge(PrepReport::refresh); } if ended { self.job = None; @@ -1454,13 +1782,15 @@ impl PrepScreen { } } + /// Stops the engine run (quitting): what was evaluated is kept. + pub fn stop(&mut self) { + self.stop_engine(&mut Vec::new()); + } + /// «Stop»: what was evaluated stays. fn stop_engine(&mut self, actions: &mut Vec) { if self.job.take().is_some() { - if let Some(r) = self.report.as_mut() { - r.refresh(); - } - self.rebuild_marks(); + self.rejudge(PrepReport::refresh); self.pending_refresh = false; self.save(); actions.push(PrepAction::EngineEnded); @@ -1549,13 +1879,13 @@ impl PrepScreen { .map_or(0, |r| r.distinct_positions_for(self.color.side())); ui.label(trf( "{n} positions, about {time}", - &[("n", &missing), ("time", &review::format_duration(secs))], + &[("n", &grouped(missing)), ("time", &review::format_duration(secs))], )); let cached = all.saturating_sub(missing); let note = if cached > 0 { trf( "identical positions are counted once; {n} already evaluated earlier", - &[("n", &cached)], + &[("n", &grouped(cached))], ) } else { tr("identical positions are counted once").to_owned() @@ -1736,13 +2066,14 @@ impl PrepScreen { .iter() .flat_map(|&w| report.color(w).games.iter().copied()) .collect(), - Selection::Node { white, node } => report - .color(*white) - .tree - .nodes - .get(*node as usize) - .map(|n| n.games.clone()) - .unwrap_or_default(), + // The games of the position, by any move order. + Selection::Node { white, node } => { + let tree = &report.color(*white).tree; + tree.nodes + .get(*node as usize) + .map(|n| tree.position_games(n.key)) + .unwrap_or_default() + } Selection::Opening { white, family, @@ -1780,6 +2111,24 @@ impl PrepScreen { self.shown_pos.as_ref().map_or(0, |s| s.ucis.len()) } + /// Where game `gi` reaches the chosen position: a node's position may come by + /// another move order (another ply) in some of its games. + fn game_ply(&self, gi: u32) -> usize { + if let (Selection::Node { white, node }, Some(report)) = (&self.selection, &self.report) + && let Some(n) = report.color(*white).tree.nodes.get(*node as usize) + && let Some(p) = report.games[gi as usize].keys.iter().position(|k| *k == n.key) + { + return p; + } + self.selection_ply() + } + + /// The move to open game `gi` at: the chosen position, or after the error. + fn open_ply(&self, gi: u32) -> usize { + let after = matches!(self.selection, Selection::Error { .. } | Selection::Repeated { .. }); + self.game_ply(gi) + usize::from(after) + } + /// The player's colour the board is seen from. fn board_white(&self) -> bool { let base = self @@ -1812,6 +2161,8 @@ impl PrepScreen { let (w, h) = (content.width(), content.height() - 44.0); let stacked = w < BOARD_MIN + 24.0 + RIGHT_MIN || h < 600.0; let low = stacked && h < 720.0; + self.stacked = stacked; + self.low_window = h < 600.0; egui::Panel::top("prep_controls").show(ui, |ui| { ui.add_space(4.0); self.controls(ui, settings, low, &mut actions); @@ -1827,7 +2178,10 @@ impl PrepScreen { ui.add_space(4.0); ui.horizontal(|ui| { ui.allocate_ui(vec2(240.0, 240.0), |ui| self.board(ui, settings, 240.0)); - ui.vertical(|ui| self.board_info(ui, &mut actions)); + ui.vertical(|ui| { + self.board_caption(ui); + self.board_info(ui, &mut actions); + }); }); ui.add_space(4.0); }); @@ -1925,29 +2279,33 @@ impl PrepScreen { /// The colour switch, the book, the depth and the engine button or its progress. fn controls(&mut self, ui: &mut Ui, settings: &mut Settings, low: bool, actions: &mut Vec) { let width = ui.available_width(); + let gap = ui.spacing().item_spacing.x; + let pad = 2.0 * ui.spacing().button_padding.x; + let state = self.engine_state(settings); + // The engine part on the right is measured first: the rest gets what it leaves. + let engine = self.engine_texts(settings, state, width); + let engine_w = match &engine { + EngineTexts::Running => (width * 0.35).clamp(90.0, 360.0) + text_width(ui, tr("Stop"), egui::TextStyle::Button) + pad + 2.0 * gap, + EngineTexts::Button { label, note } | EngineTexts::Done { label, note } => { + text_width(ui, label, egui::TextStyle::Button) + + pad + + note.as_deref().map_or(0.0, |n| text_width(ui, n, egui::TextStyle::Body) + gap) + + gap + } + } + 26.0 + gap; ui.horizontal(|ui| { for choice in [ColorChoice::White, ColorChoice::Black, ColorChoice::Both] { - if ui.selectable_value(&mut self.color, choice, choice.label()).clicked() { - // A selection of a colour no longer shown is dropped. - let keep = match &self.selection { - Selection::None => true, - Selection::Node { white, .. } - | Selection::Opening { white, .. } - | Selection::Repeated { white, .. } - | Selection::Error { white, .. } => self.color.colours().contains(white), - }; - if !keep { - self.select(Selection::None); - } + if ui.selectable_label(self.color == choice, choice.label()).clicked() { + self.set_color(choice); } } ui.separator(); - let book_w = if width < 900.0 { 150.0 } else { 240.0 }; - if width >= 640.0 { + let caption_apart = width >= 640.0; + if caption_apart { ui.label(tr("Book")); } - let own_name = self.db_name.clone(); - if let Some(b) = book_picker(ui, "prep_book", &self.book, &self.path, &own_name, &self.dbs, book_w) { + let book_w = if width < 900.0 { 150.0 } else { 240.0 }; + if let Some(b) = book_picker(ui, "prep_book", &self.book, &self.path, &self.dbs, book_w, !caption_apart) { self.book = b; self.book_failed = None; self.book_asked = None; @@ -1955,15 +2313,22 @@ impl PrepScreen { actions.push(PrepAction::SettingsChanged); } if let Some((_, why)) = &self.book_failed { - ui.label(RichText::new(trf("Book: could not open — {reason}", &[("reason", why)])).color(ui.visuals().warn_fg_color)); + ui.label(RichText::new("⚠").color(ui.visuals().warn_fg_color)) + .on_hover_text(trf("Book: could not open — {reason}", &[("reason", why)])); } ui.separator(); + // The depth's caption by the room left: «Opening: 10 moves», «10 moves», «10 m.». let moves = self.plies.div_ceil(2); - let depth_label = if width < 760.0 { - format!("{moves}") - } else { - trf("Opening: {n} moves", &[("n", &moves)]) - }; + let board_w = if low { text_width(ui, "♙", egui::TextStyle::Button) + pad + 2.0 * gap } else { 0.0 }; + let room = ui.available_width() - engine_w - board_w; + let depth_label = [ + trf("Opening: {n} moves", &[("n", &moves)]), + trf("{n} moves", &[("n", &moves)]), + trf("{n} m.", &[("n", &moves)]), + ] + .into_iter() + .find(|l| text_width(ui, l, egui::TextStyle::Button) + pad + 20.0 + gap <= room) + .unwrap_or_else(|| trf("{n} m.", &[("n", &moves)])); let mut new_plies = None; egui::ComboBox::from_id_salt("prep_plies") .selected_text(depth_label) @@ -1979,31 +2344,14 @@ impl PrepScreen { }) .response .on_hover_text(tr("How deep the tree and the engine look")); - if let Some(p) = new_plies.filter(|p| *p != self.plies) { - self.plies = p; - settings.prep.plies = p; + if let Some(p) = new_plies { + self.set_plies(p, settings, ui.ctx()); actions.push(PrepAction::SettingsChanged); - self.save(); - self.report = None; - let dir = cache_dir(); - let options = Options { - plies: p, - side: Side::Any, - }; - let cached = dir - .as_deref() - .and_then(|d| PrepReport::load(d, &self.db, &self.player, &options)); - match cached { - Some(r) => self.take_report(r, settings), - None => self.rebuild(ui.ctx()), - } - self.selection = Selection::None; - self.shown_pos = None; } if low { ui.separator(); if ui - .selectable_label(self.board_popup, if width < 760.0 { "♙" } else { tr("♙ Board") }) + .selectable_label(self.board_popup, "♙") .on_hover_text(tr("The board of the chosen position")) .clicked() { @@ -2011,13 +2359,63 @@ impl PrepScreen { } } ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { - self.engine_control(ui, settings, width, actions); + self.engine_control(ui, settings, engine, width, actions); }); }); } - /// The engine button, the progress while it runs, the state after. - fn engine_control(&mut self, ui: &mut Ui, settings: &Settings, width: f32, actions: &mut Vec) { + /// What the engine part of the controls says, by the room there is. + fn engine_texts(&self, settings: &Settings, state: EngineState, width: f32) -> EngineTexts { + let (missing, secs) = self.estimate(settings); + let time = review::format_duration(secs); + match state { + EngineState::Running => EngineTexts::Running, + EngineState::NotRun => EngineTexts::Button { + label: if width < 700.0 { + format!("▶ ≈ {time}") + } else if width < 900.0 { + trf("▶ Errors ≈ {time}", &[("time", &time)]) + } else { + trf("▶ Find opening errors ≈ {time}", &[("time", &time)]) + }, + note: None, + }, + EngineState::Partial { done, total } => EngineTexts::Button { + label: if width < 700.0 { + format!("▶ ≈ {time}") + } else { + trf("Continue ≈ {time}", &[("time", &time)]) + }, + note: (width >= 900.0).then(|| { + trf( + "Checked {done} of {total} positions", + &[("done", &grouped(done)), ("total", &grouped(total))], + ) + }), + }, + EngineState::Complete { .. } => { + let _ = missing; + EngineTexts::Done { + label: tr("Errors found").to_owned(), + note: None, + } + } + } + } + + /// The engine button, the progress while it runs, the state after; «▾» (the + /// options) at the right end. + fn engine_control( + &mut self, + ui: &mut Ui, + settings: &Settings, + texts: EngineTexts, + width: f32, + actions: &mut Vec, + ) { + if ui.button("▾").on_hover_text(tr("Options…")).clicked() { + self.dialog_open = true; + } if let Some(job) = &self.job { let (done, total) = (job.done, job.total); if ui.button(tr("Stop")).on_hover_text("Shift+A").clicked() { @@ -2028,11 +2426,11 @@ impl PrepScreen { let per_thread = self.nps_per_thread.unwrap_or(review::DEFAULT_NPS); let secs = review::estimate_secs(left, job.nodes, per_thread, job.threads); let text = if width < 760.0 { - format!("{done}/{total}") + format!("{}/{}", grouped(done), grouped(total)) } else { trf( "Finding errors: {done} of {total} positions · ≈ {time}", - &[("done", &done), ("total", &total), ("time", &review::format_duration(secs))], + &[("done", &grouped(done)), ("total", &grouped(total)), ("time", &review::format_duration(secs))], ) }; ui.add( @@ -2043,44 +2441,23 @@ impl PrepScreen { return; } let Some(report) = &self.report else { return }; - let (missing, secs) = self.estimate(settings); - let time = review::format_duration(secs); - let done_before = report.engine.is_some(); - let label = if missing == 0 && done_before { - tr("Errors found").to_owned() - } else if width < 700.0 { - format!("▶ ≈ {time}") - } else if width < 900.0 { - trf("▶ Errors ≈ {time}", &[("time", &time)]) - } else if done_before { - trf("Analyse {n} more positions ≈ {time}", &[("n", &missing), ("time", &time)]) - } else { - trf("▶ Find opening errors ≈ {time}", &[("time", &time)]) - }; - let hint = match &report.engine { - Some(e) => { - let quality = REVIEW_QUALITY - .iter() - .find(|q| q.1 == e.nodes) - .map_or_else(|| review::format_count(e.nodes), |q| tr(q.0).to_owned()); - trf( - "Errors found · {engine} · {quality} · {n} positions", - &[("engine", &e.name), ("quality", &quality), ("n", &report.evals.len())], - ) + let hint = self.engine_status(settings).unwrap_or_else(|| { + tr("The engine finds the opening errors in his games (Shift+A)").to_owned() + }); + let _ = report; + match texts { + EngineTexts::Done { label, .. } => { + ui.label(RichText::new(label).weak()).on_hover_text(hint); } - None => tr("The engine finds the opening errors in his games (Shift+A)").to_owned(), - }; - if missing == 0 && done_before { - ui.label(RichText::new(label).weak()).on_hover_text(hint); - } else if crate::widgets::primary(ui, &label).on_hover_text(hint).clicked() { - self.engine_asked(settings, ui.ctx(), actions); - } - if ui - .button("▾") - .on_hover_text(tr("Options…")) - .clicked() - { - self.dialog_open = true; + EngineTexts::Button { label, note } => { + if crate::widgets::primary(ui, &label).on_hover_text(hint).clicked() { + self.engine_asked(settings, ui.ctx(), actions); + } + if let Some(n) = note { + ui.label(RichText::new(n).weak()); + } + } + EngineTexts::Running => {} } if let Some(e) = &self.error { ui.label(RichText::new("⚠").color(ui.visuals().error_fg_color)) @@ -2088,91 +2465,155 @@ impl PrepScreen { } } - /// A line per colour: games, score, opponents, the last game, the book. + /// «Errors found · Stockfish 19 · Fast · 2 947 positions», once evaluated. + fn engine_status(&self, settings: &Settings) -> Option { + let report = self.report.as_ref()?; + if !report.evaluated() { + return None; + } + let nodes = report.engine.as_ref().map_or(settings.prep.nodes(), |e| e.nodes); + let quality = REVIEW_QUALITY + .iter() + .find(|q| q.1 == nodes) + .map_or_else(|| review::format_count(nodes), |q| tr(q.0).to_owned()); + let n = grouped(report.distinct_positions_for(self.color.side())); + Some(match report.engine.as_ref().filter(|e| !e.name.is_empty()) { + Some(e) => trf( + "Errors found · {engine} · {quality} · {n} positions", + &[("engine", &e.name), ("quality", &quality), ("n", &n)], + ), + None => trf( + "Errors found · {quality} · {n} positions", + &[("quality", &quality), ("n", &n)], + ), + }) + } + + /// The summary: a row per colour in columns — colour, games, score, opponents, + /// the last game, the book, the errors; the columns give way from the right (the + /// last game, the book, the opponents) instead of wrapping. fn summary(&self, ui: &mut Ui, stacked: bool) { let Some(report) = &self.report else { return }; let colours = if stacked { self.color.colours() } else { vec![true, false] }; - for white in colours { + let evaluated = report.evaluated() && !stacked; + let book = self.book_ok(); + // Texts per colour: [games, score, opponents, last, book]. + let mut rows: Vec<(bool, [String; 5], ErrorCounts, usize)> = Vec::new(); + for &white in &colours { let cr = report.color(white); - let tally = &cr.tree.nodes[0].tally; - let weak = !self.color.colours().contains(&white); + let root = &cr.tree.nodes[0]; + let tally = &root.tally; let title = if white { tr("As White") } else { tr("As Black") }; - ui.horizontal_wrapped(|ui| { - let text = |ui: &mut Ui, s: String| { - let rt = RichText::new(s); - ui.label(if weak { rt.weak() } else { rt }); - }; - text(ui, format!("{title} {}", tally.games)); - if tally.games == 0 { - return; - } - text(ui, format!("· {} ({})", score_text(tally), tally_hint(tally))); - if !stacked { - if let Some(elo) = cr.tree.nodes[0].average_elo() { - text(ui, format!("· {}", trf("opponents ≈ {elo}", &[("elo", &elo)]))); - } - let last = crate::library::display_date(&cr.tree.nodes[0].last_date); - if !last.is_empty() { - text(ui, format!("· {}", trf("last {date}", &[("date", &last)]))); - } - } - if report.book.is_some() { - let mut plies: Vec = cr - .games - .iter() - .filter_map(|&g| report.games[g as usize].book.map(|b| b.ply)) - .collect(); - if !plies.is_empty() { - plies.sort_unstable(); - let median = plies[plies.len() / 2]; + let games = format!("{title} {}", tally.games); + let score = if tally.games > 0 { + format!("{} ({})", score_text(tally), tally_hint(tally)) + } else { + String::new() + }; + let opponents = root + .average_elo() + .map(|elo| trf("opponents ≈ {elo}", &[("elo", &elo)])) + .unwrap_or_default(); + let last = crate::library::display_date(&root.last_date); + let last = if last.is_empty() { String::new() } else { trf("last {date}", &[("date", &last)]) }; + let book_text = if book { + let mut plies: Vec = cr + .games + .iter() + .filter_map(|&g| report.games[g as usize].book.map(|b| b.ply)) + .collect(); + plies.sort_unstable(); + match plies.get(plies.len() / 2) { + Some(median) => { let (own, other) = cr.book_exits(&report.games); let p = 100 * own / (own + other).max(1); - text( - ui, - format!( - "· {}", - trf( - "leaves the book at move {n}, first in {p} % of games", - &[("n", &median.div_ceil(2).max(1)), ("p", &p)] - ) - ), - ); + trf( + "leaves the book at move {n}, first in {p} % of games", + &[("n", &median.div_ceil(2).max(1)), ("p", &p)], + ) } - } else if self.book_job.is_some() { - text(ui, format!("· {}", tr("book…"))); + None => String::new(), } - if !stacked && report.engine.is_some() && cr.counts.total() > 0 { - let games_with: HashSet = cr.errors.iter().map(|e| e.game).collect(); - let n = games_with.len(); - let p = 100 * n / cr.games.len().max(1); - let c = &cr.counts; - ui.label(RichText::new("·").weak()); - let line = trf( - "Opening errors: {i} inaccuracies · {m} mistakes · {b} blunders — in {g} games ({p} %)", - &[ - ("i", &c.inaccuracies), - ("m", &c.mistakes), - ("b", &c.blunders), - ("g", &n), - ("p", &p), - ], - ); - let rt = RichText::new(line); - ui.label(if weak { rt.weak() } else { rt }); + } else if self.book_job.is_some() { + tr("book…").to_owned() + } else { + String::new() + }; + let with_errors: HashSet = cr + .errors + .iter() + .filter(|e| self.visible(e.judgement)) + .map(|e| e.game) + .collect(); + let p = 100 * with_errors.len() / cr.games.len().max(1); + rows.push((white, [games, score, opponents, last, book_text], self.shown_counts(&cr.counts), p)); + } + // Which columns fit: the errors stay; last, book, opponents give way in turn. + let gap = 14.0; + let col_w = |k: usize| -> f32 { + rows.iter() + .map(|r| text_width(ui, &r.1[k], egui::TextStyle::Body)) + .fold(0.0, f32::max) + }; + let errors_w = if evaluated { + rows.iter() + .map(|(_, _, c, p)| { + errors_text_width(ui, c) + text_width(ui, &trf("in {p} % of games", &[("p", p)]), egui::TextStyle::Body) + 12.0 + }) + .fold(0.0, f32::max) + } else { + 0.0 + }; + let width = ui.available_width(); + let mut shown = [true; 5]; + let used = |shown: &[bool; 5]| -> f32 { + (0..5).filter(|&k| shown[k]).map(|k| col_w(k) + gap).sum::() + errors_w + }; + for k in [3, 4, 2, 1] { + if used(&shown) <= width { + break; + } + shown[k] = false; + } + egui::Grid::new("prep_summary") + .num_columns(6) + .spacing(vec2(gap, 2.0)) + .show(ui, |ui| { + for (white, texts, counts, p) in &rows { + let weak = !self.color.colours().contains(white); + for k in 0..5 { + if !shown[k] { + continue; + } + let rt = RichText::new(&texts[k]); + let rt = if k == 0 { rt.strong() } else { rt }; + ui.label(if weak { rt.weak() } else { rt }); + } + if evaluated { + ui.horizontal(|ui| { + ui.spacing_mut().item_spacing.x = 4.0; + if counts.total() > 0 { + counts_text(ui, counts); + ui.label(RichText::new(trf("in {p} % of games", &[("p", p)])).weak()); + } else { + ui.label(RichText::new(tr("no errors")).weak()); + } + }); + } + ui.end_row(); } }); - } - let shown: usize = self + let n: usize = self .color .colours() .iter() .map(|&w| report.color(w).games.len()) .sum(); - if shown > 0 && shown < 10 { + if n > 0 && n < 10 { ui.label( RichText::new(trf( "Few games ({n}): percentages and scores are unreliable", - &[("n", &shown)], + &[("n", &n)], )) .color(ui.visuals().warn_fg_color), ); @@ -2298,7 +2739,12 @@ impl PrepScreen { ui.label(RichText::new(tr("Start position")).weak()); } for (i, san) in s.sans.iter().enumerate() { - let label = short_label(i, san); + // Numbers at White's moves only: «1.e4 c5 2.Nf3 d6». + let label = if i.is_multiple_of(2) { + short_label(i, san) + } else { + crate::san::show(san).into_owned() + }; let current = i + 1 == s.ply; let text = if current { RichText::new(label).strong() @@ -2443,11 +2889,13 @@ impl PrepScreen { fn right_column(&mut self, ui: &mut Ui, settings: &mut Settings, actions: &mut Vec) { let height = ui.available_height(); let width = ui.available_width(); - let least = 4.0 * ROW_H + 60.0; + // In a low window the tabs keep the most: two rows of games at least, a quarter. + let (rows, share) = if self.low_window { (2.0, 0.25) } else { (4.0, 0.35) }; + let least = rows * ROW_H + 60.0; let most = (height - 160.0).max(least); let games_h = self .games_height - .unwrap_or(height * 0.35) + .unwrap_or(height * share) .clamp(least, most) .floor(); let tabs_h = (height - games_h - 10.0).max(0.0); @@ -2637,7 +3085,7 @@ impl PrepScreen { let Some(report) = self.report.as_ref() else { return }; let width = ui.available_width(); let gap = ui.spacing().item_spacing.x; - let has_book = report.book.is_some() || self.book_job.is_some(); + let has_book = self.book_ok() || (self.book_job.is_some() && self.book != BookChoice::None); let mut used = 180.0 + 56.0 + 40.0 + 50.0 + 4.0 * gap; let mut fits = |w: f32, wanted: bool| { let ok = wanted && used + w + gap <= width; @@ -2713,7 +3161,14 @@ impl PrepScreen { if ui.add(egui::Label::new(RichText::new(glyph).weak()).sense(Sense::click())).clicked() { toggled = Some(r); } - ui.label(RichText::new(trf("other moves ({n})", &[("n", &count)])).weak()); + // Short when the column is narrow at this depth. + let full = trf("other moves ({n})", &[("n", &count)]); + let text = if text_width(ui, &full, egui::TextStyle::Body) > ui.available_width() { + trf("others ({n})", &[("n", &count)]) + } else { + full + }; + ui.label(RichText::new(text).weak()); }); let rest = 2 + usize::from(show_opening) + 1 + usize::from(show_last) + usize::from(show_book); for _ in 0..rest { @@ -2726,7 +3181,9 @@ impl PrepScreen { let parent = n.parent.map(|p| &cr.tree.nodes[p as usize]); let has_children = !n.children.is_empty(); let open = self.expanded.contains(&(white, node)); - let few = n.tally.games < FEW_GAMES; + // The statistics are the position's, whatever the move order. + let tally = cr.tree.position_tally(n.key); + let few = tally.games < FEW_GAMES; row.col(|ui| { ui.add_space(depth as f32 * 14.0); let glyph = if !has_children { " " } else if open { "▾" } else { "▸" }; @@ -2744,7 +3201,9 @@ impl PrepScreen { let label = short_label(ply, &n.san); let text = if players { RichText::new(label) } else { RichText::new(label).weak() }; let l = ui.add(egui::Label::new(text).truncate()); - if !show_opening && let Some(o) = n.opening.filter(|_| n.named).and_then(|o| report.openings.get(o)) { + if !show_opening + && let Some(o) = n.opening.filter(|_| n.named).and_then(|o| report.openings.get(o)) + { l.on_hover_text(format!("{} · {}", o.name, o.eco)); } if n.transposition { @@ -2763,16 +3222,18 @@ impl PrepScreen { } if let Some(p) = parent && let Some(m) = self.mark_of(white, p.key, &n.uci) + && self.visible(m.judgement) { let best = crate::san::show(&m.best_san).into_owned(); - pill(ui, m.judgement).on_hover_text(format!( - "{}: {}", - review::judgement_name(m.judgement), - trf( - "Mistake: better {best} ({before} → {after}) · in {n} games", - &[("best", &best), ("before", &m.before.format()), ("after", &m.after.format()), ("n", &m.games)], - ) - .trim_start_matches(&format!("{}: ", tr("Mistake"))) + pill(ui, m.judgement).on_hover_text(trf( + "{judgement}: better {best} ({before} → {after}) · in {n} games", + &[ + ("judgement", &review::judgement_name(m.judgement)), + ("best", &best), + ("before", &m.before.format()), + ("after", &m.after.format()), + ("n", &m.games), + ], )); if m.games > 1 { ui.label(RichText::new(format!("×{}", m.games)).weak().small()); @@ -2781,24 +3242,25 @@ impl PrepScreen { }); if show_opening { row.col(|ui| { - // The name where it first appears (the table names - // 2.Nf3 «Sicilian Defense» again: not repeated). - let new_name = n.named - && parent.is_none_or(|p| { - p.opening.map(|o| report.openings[o].name.as_str()) - != n.opening.map(|o| report.openings[o].name.as_str()) + // The name where it changes (`named`); the ECO keeps + // its place at the right, the name takes the rest. + if n.named && let Some(o) = n.opening.and_then(|o| report.openings.get(o)) { + let eco = RichText::new(&o.eco).weak().small(); + let eco_w = text_width(ui, &o.eco, egui::TextStyle::Small) + ui.spacing().item_spacing.x; + let name_w = (ui.available_width() - eco_w).max(0.0); + ui.allocate_ui(vec2(name_w, ROW_H), |ui| { + ui.add(egui::Label::new(RichText::new(&o.name).weak()).truncate()) + .on_hover_text(format!("{} · {}", o.name, o.eco)); }); - if new_name && let Some(o) = n.opening.and_then(|o| report.openings.get(o)) { - ui.add(egui::Label::new(RichText::new(&o.name).weak()).truncate()) - .on_hover_text(format!("{} · {}", o.name, o.eco)); - ui.label(RichText::new(&o.eco).weak().small()); + ui.label(eco); } }); } row.col(|ui| { - ui.label(n.tally.games.to_string()); + ui.label(tally.games.to_string()); }); row.col(|ui| { + // The move's share where the games were (this order). if let Some(p) = parent { let pct = 100 * n.tally.games / p.tally.games.max(1); let t = RichText::new(format!("{pct}")); @@ -2809,19 +3271,16 @@ impl PrepScreen { } }); row.col(|ui| { - let t = RichText::new(score_text(&n.tally)); + let t = RichText::new(score_text(&tally)); let l = ui.label(if few { t.weak() } else { t }); - l.on_hover_text(tally_hint(&n.tally)); + l.on_hover_text(tally_hint(&tally)); if show_bar { - let (rect, _) = ui.allocate_exact_size(vec2(56.0, 14.0), Sense::hover()); - let painter_alpha = if few { 0.5 } else { 1.0 }; - let _ = painter_alpha; - board_search::results_bar(ui, rect, &bar_tally(&n.tally, white), ui.visuals().text_color()); + score_bar(ui, &tally, white, few); } }); if show_last { row.col(|ui| { - let d = crate::library::display_date(&n.last_date); + let d = crate::library::display_date(cr.tree.position_last_date(n.key)); ui.label(RichText::new(if d.is_empty() { "—".to_owned() } else { d }).weak()); }); } @@ -2949,7 +3408,7 @@ impl PrepScreen { let both = self.color == ColorChoice::Both; let width = ui.available_width(); let gap = ui.spacing().item_spacing.x; - let mut used = 200.0 + 56.0 + 40.0 + 50.0 + 4.0 * gap + if both { 90.0 + gap } else { 0.0 }; + let mut used = 180.0 + 56.0 + 40.0 + 50.0 + 4.0 * gap + if both { 24.0 + gap } else { 0.0 }; let mut fits = |w: f32, wanted: bool| { let ok = wanted && used + w + gap <= width; if ok { @@ -2958,11 +3417,11 @@ impl PrepScreen { ok }; // The errors stay as long as possible; the first moves give way first. - let show_errors = fits(90.0, report.engine.is_some()); + let show_errors = fits(90.0, report.evaluated()); let show_bar = fits(64.0, true); let show_eco = fits(64.0, true); let show_last = fits(84.0, true); - let show_moves = fits(160.0, true); + let show_moves = fits(140.0, true); ui.style_mut().interaction.selectable_labels = false; let mut table = TableBuilder::new(ui) .id_salt("prep_openings") @@ -2971,14 +3430,14 @@ impl PrepScreen { .auto_shrink([false, false]) .cell_layout(egui::Layout::left_to_right(egui::Align::Center)); if both { - table = table.column(Column::exact(90.0)); + table = table.column(Column::exact(24.0)); } - table = table.column(Column::remainder().at_least(200.0).clip(true)); + table = table.column(Column::remainder().at_least(180.0).clip(true)); if show_eco { table = table.column(Column::exact(64.0)); } if show_moves { - table = table.column(Column::remainder().at_least(160.0).clip(true)); + table = table.column(Column::remainder().at_least(140.0).clip(true)); } table = table.column(Column::exact(56.0)).column(Column::exact(40.0)); table = table.column(Column::exact(if show_bar { 110.0 } else { 50.0 })); @@ -3002,7 +3461,7 @@ impl PrepScreen { table .header(ROW_H, |mut h| { if both { - head_col(&mut h, tr("Colour"), ""); + head_col(&mut h, "", ""); } head_col(&mut h, tr("Opening"), ""); if show_eco { @@ -3012,7 +3471,7 @@ impl PrepScreen { head_col(&mut h, tr("First moves"), ""); } head_col(&mut h, tr("Games"), ""); - head_col(&mut h, "%", ""); + head_col(&mut h, "%", tr("Share of the colour's games; a variation's — of its family's")); head_col(&mut h, tr("Score"), tr("The opponent's points in these games")); if show_last { head_col(&mut h, tr("Last"), ""); @@ -3039,7 +3498,8 @@ impl PrepScreen { if both { row.col(|ui| { if v.is_none() { - ui.label(RichText::new(if white { tr("♙ White") } else { tr("♟ Black") }).weak()); + ui.label(RichText::new(if white { "♙" } else { "♟" }).weak()) + .on_hover_text(if white { tr("As White") } else { tr("As Black") }); } }); } @@ -3101,9 +3561,10 @@ impl PrepScreen { None => family_line(report, fam), }; if let Some(l) = line { - let shown = crate::san::show_line(&shorten_middle(&l, 40)); - ui.add(egui::Label::new(RichText::new(shown).weak()).truncate()) - .on_hover_text(crate::san::show_line(&l)); + let tokens = table_line(&l); + let full = crate::san::show_line(&tokens.join(" ")); + let shown = fit_line(ui, &tokens, ui.available_width()); + ui.label(RichText::new(shown).weak()).on_hover_text(full); } }); } @@ -3123,8 +3584,7 @@ impl PrepScreen { let t = RichText::new(score_text(tally)); ui.label(if few { t.weak() } else { t }).on_hover_text(tally_hint(tally)); if show_bar { - let (rect, _) = ui.allocate_exact_size(vec2(56.0, 14.0), Sense::hover()); - board_search::results_bar(ui, rect, &bar_tally(tally, white), ui.visuals().text_color()); + score_bar(ui, tally, white, few); } }); if show_last { @@ -3141,7 +3601,7 @@ impl PrepScreen { if show_errors { row.col(|ui| { ui.spacing_mut().item_spacing.x = 4.0; - counts_text(ui, errors); + counts_text(ui, &self.shown_counts(errors)); }); } let resp = row.response(); @@ -3164,10 +3624,11 @@ impl PrepScreen { // ----- the errors ---------------------------------------------------------------- - fn errors_tab(&mut self, ui: &mut Ui, settings: &Settings, actions: &mut Vec) { + fn errors_tab(&mut self, ui: &mut Ui, settings: &mut Settings, actions: &mut Vec) { let Some(report) = self.report.as_ref() else { return }; let moves = self.plies.div_ceil(2); - if report.engine.is_none() && self.job.is_none() { + let state = self.engine_state(settings); + if state == EngineState::NotRun { // The invitation (§5). let (positions, secs) = self.estimate(settings); let games: usize = self.color.colours().iter().map(|&w| report.color(w).games.len()).sum(); @@ -3185,7 +3646,7 @@ impl PrepScreen { ("n", &moves), ("games", &games), ("colour", &colour), - ("positions", &positions), + ("positions", &grouped(positions)), ("time", &review::format_duration(secs)), ], )); @@ -3196,56 +3657,72 @@ impl PrepScreen { }); return; } - let dark = ui.visuals().dark_mode; let (mut ask_engine, mut open_dialog) = (false, false); - // The state line and the summary per colour. + let mut inaccuracies = settings.prep.inaccuracies; + // The state line: how far the engine got; the switch of the inaccuracies. ui.horizontal_wrapped(|ui| { - match (&self.job, &report.engine) { - (Some(job), _) => { - let left = job.total.saturating_sub(job.done); - let per_thread = self.nps_per_thread.unwrap_or(review::DEFAULT_NPS); - let secs = review::estimate_secs(left, job.nodes, per_thread, job.threads); + match state { + EngineState::Running => { + if let Some(job) = &self.job { + let left = job.total.saturating_sub(job.done); + let per_thread = self.nps_per_thread.unwrap_or(review::DEFAULT_NPS); + let secs = review::estimate_secs(left, job.nodes, per_thread, job.threads); + ui.label( + RichText::new(trf( + "Checked {done} of {total} positions", + &[("done", &grouped(job.done)), ("total", &grouped(job.total))], + )) + .weak(), + ); + ui.label(RichText::new(format!("· ≈ {}", review::format_duration(secs))).weak()); + } + } + EngineState::Partial { done, total } => { + let (_, secs) = self.estimate(settings); ui.label( RichText::new(trf( "Checked {done} of {total} positions", - &[("done", &job.done), ("total", &job.total)], + &[("done", &grouped(done)), ("total", &grouped(total))], )) .weak(), - ); - ui.label(RichText::new(format!("· ≈ {}", review::format_duration(secs))).weak()); + ) + .on_hover_text(tr("The counts are of the checked positions")); + ui.label(RichText::new("·").weak()); + if ui + .link(trf("Continue ≈ {time}", &[("time", &review::format_duration(secs))])) + .clicked() + { + ask_engine = true; + } } - (None, Some(e)) => { - let quality = REVIEW_QUALITY - .iter() - .find(|q| q.1 == e.nodes) - .map_or_else(|| review::format_count(e.nodes), |q| tr(q.0).to_owned()); - ui.label( - RichText::new(trf( - "Errors found · {engine} · {quality} · {n} positions", - &[("engine", &e.name), ("quality", &quality), ("n", &report.evals.len())], - )) - .weak(), - ); - let (missing, secs) = self.estimate(settings); - if missing > 0 { - ui.label(RichText::new("·").weak()); - if ui - .link(trf("Analyse {n} more positions ≈ {time}", &[("n", &missing), ("time", &review::format_duration(secs))])) - .clicked() - { - ask_engine = true; - } + EngineState::Complete { .. } => { + if let Some(status) = self.engine_status(settings) { + ui.label(RichText::new(status).weak()); } if settings.prep.quality == 0 { ui.label(RichText::new("·").weak()); - if ui.link(tr("More precisely…")).on_hover_text(tr("The same positions at «Normal» quality")).clicked() { + if ui + .link(tr("More precisely…")) + .on_hover_text(tr("The same positions at «Normal» quality")) + .clicked() + { open_dialog = true; } } } - (None, None) => {} + EngineState::NotRun => {} } + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + ui.checkbox(&mut inaccuracies, tr("Inaccuracies")) + .on_hover_text(tr("Show the inaccuracies (?!) with the mistakes and blunders")); + }); }); + if inaccuracies != settings.prep.inaccuracies { + settings.prep.inaccuracies = inaccuracies; + self.show_inaccuracies = inaccuracies; + self.rebuild_marks(); + actions.push(PrepAction::SettingsChanged); + } if ask_engine { self.engine_asked(settings, ui.ctx(), actions); return; @@ -3254,32 +3731,20 @@ impl PrepScreen { self.dialog_open = true; } let Some(report) = self.report.as_ref() else { return }; - for white in self.color.colours() { - let cr = report.color(white); - let games_with: HashSet = cr.errors.iter().map(|e| e.game).collect(); - let c = &cr.counts; - let title = if white { tr("As White") } else { tr("As Black") }; - let p = 100 * games_with.len() / cr.games.len().max(1); - ui.horizontal_wrapped(|ui| { - ui.label(RichText::new(format!("{title}:")).strong()); - let text = trf( - "{i} inaccuracies · {m} mistakes · {b} blunders — in {g} games of {t} ({p} %)", - &[ - ("i", &c.inaccuracies), - ("m", &c.mistakes), - ("b", &c.blunders), - ("g", &games_with.len()), - ("t", &cr.games.len()), - ("p", &p), - ], - ); - let l = ui.label(text); - if self.job.is_some() { - l.on_hover_text(tr("of the checked positions")); - } - }); + // The lines per colour: in the stack only (beside the board the summary has them). + if self.stacked { + for white in self.color.colours() { + let cr = report.color(white); + let with: HashSet = cr.errors.iter().filter(|e| self.visible(e.judgement)).map(|e| e.game).collect(); + let p = 100 * with.len() / cr.games.len().max(1); + ui.horizontal_wrapped(|ui| { + ui.spacing_mut().item_spacing.x = 4.0; + ui.label(RichText::new(if white { tr("As White") } else { tr("As Black") }).strong()); + counts_text(ui, &self.shown_counts(&cr.counts)); + ui.label(RichText::new(trf("in {p} % of games", &[("p", &p)])).weak()); + }); + } } - let total_errors: u32 = self.color.colours().iter().map(|&w| report.color(w).counts.total()).sum(); let serious: u32 = self .color .colours() @@ -3289,269 +3754,401 @@ impl PrepScreen { c.mistakes + c.blunders }) .sum(); - if self.job.is_none() && serious == 0 { + let inacc: u32 = self.color.colours().iter().map(|&w| report.color(w).counts.inaccuracies).sum(); + if matches!(state, EngineState::Complete { .. }) && serious == 0 { + let mut deeper = false; ui.add_space(8.0); ui.horizontal_wrapped(|ui| { - ui.label(trf("No errors in the first {n} moves ({i} inaccuracies).", &[("n", &moves), ("i", &(total_errors - serious))])); + ui.label(trf("No errors in the first {n} moves ({i} inaccuracies).", &[("n", &moves), ("i", &inacc)])); if self.plies < 30 && ui.link(trf("Deeper: {n} moves", &[("n", &15)])).clicked() { - // The depth picker's choice, as if chosen there. - self.plies = 30; - self.report = None; - self.rebuild(ui.ctx()); - actions.push(PrepAction::SettingsChanged); + deeper = true; } }); - if total_errors == 0 { + if deeper { + // As if chosen in the depth list. + self.set_plies(30, settings, ui.ctx()); + actions.push(PrepAction::SettingsChanged); return; } } - ui.add_space(6.0); - let both = self.color == ColorChoice::Both; - egui::ScrollArea::vertical() - .id_salt("prep_errors") - .auto_shrink([false, false]) - .show(ui, |ui| { - ui.style_mut().interaction.selectable_labels = false; - self.repeated_list(ui, both, dark, actions); - ui.add_space(10.0); - self.by_opening_list(ui, both, dark, actions); - }); + ui.add_space(4.0); + self.errors_table(ui, actions); } - /// «Repeated errors — the same position, the same move». - fn repeated_list(&mut self, ui: &mut Ui, both: bool, dark: bool, actions: &mut Vec) { - let Some(report) = self.report.as_ref() else { return }; - let mut list: Vec<(bool, usize)> = Vec::new(); - for white in self.color.colours() { - for i in 0..report.color(white).repeated.len() { - list.push((white, i)); + /// The repeated errors (the same position and move in several games) and the + /// errors by opening (the variations, the worst first), the inaccuracies left out + /// when they are hidden. + fn repeated_shown(&self) -> Vec<(bool, usize)> { + let Some(report) = &self.report else { + return Vec::new(); + }; + let mut list: Vec<(bool, usize)> = self + .color + .colours() + .into_iter() + .flat_map(|w| { + report + .color(w) + .repeated + .iter() + .enumerate() + .filter(|(_, r)| self.visible(r.judgement)) + .map(move |(i, _)| (w, i)) + }) + .collect(); + list.sort_by(|a, b| { + let ra = &report.color(a.0).repeated[a.1]; + let rb = &report.color(b.0).repeated[b.1]; + rb.games + .len() + .cmp(&ra.games.len()) + .then(judgement_weight(rb.judgement).cmp(&judgement_weight(ra.judgement))) + }); + list + } + + /// The games of a repeated error, the newest first. + fn repeated_games(&self, white: bool, index: usize) -> Vec { + let Some(report) = &self.report else { + return Vec::new(); + }; + let mut games = report.color(white).repeated[index].games.clone(); + games.sort_by_key(|&g| std::cmp::Reverse(report.games[g as usize].date_number())); + games + } + + fn err_rows(&self) -> Vec { + let Some(report) = &self.report else { + return Vec::new(); + }; + let mut rows = vec![ErrRow::RepeatedTitle]; + for (white, index) in self.repeated_shown() { + rows.push(ErrRow::Repeated { white, index }); + if self.open_repeated.contains(&(white, index)) { + for game in self.repeated_games(white, index) { + rows.push(ErrRow::RepeatedGame { white, index, game }); + } } } - if both { - list.sort_by(|a, b| { - let ra = &report.color(a.0).repeated[a.1]; - let rb = &report.color(b.0).repeated[b.1]; - rb.games.len().cmp(&ra.games.len()).then(judgement_weight(rb.judgement).cmp(&judgement_weight(ra.judgement))) - }); - } - ui.label(RichText::new(tr("Repeated errors — the same position, the same move")).strong()); - if list.is_empty() { - ui.label(RichText::new(tr("None so far")).weak()); - return; - } - let width = ui.available_width(); - let show_opening = width > 700.0; - let show_score = width > 560.0; - let mut clicked = None; - let mut opened = None; - let mut toggled = None; - egui::Grid::new("prep_repeated") - .num_columns(5 + usize::from(show_score) + usize::from(show_opening) + usize::from(both)) - .striped(true) - .spacing(vec2(10.0, 4.0)) - .show(ui, |ui| { - let head = |ui: &mut Ui, t: &str| { - ui.label(RichText::new(t).strong()); - }; - head(ui, ""); - if both { - head(ui, tr("Colour")); - } - head(ui, tr("Position")); - head(ui, tr("Played")); - head(ui, tr("Better")); - head(ui, "×"); - if show_score { - head(ui, tr("Score")); - } - if show_opening { - head(ui, tr("Opening")); - } - ui.end_row(); - for &(white, i) in &list { - let cr = report.color(white); - let r = &cr.repeated[i]; - let selected = self.selection == Selection::Repeated { white, index: i }; - let open = self.open_repeated.contains(&(white, i)); - let g = &report.games[r.games[0] as usize]; - let glyph = if open { "▾" } else { "▸" }; - if ui.add(egui::Label::new(RichText::new(glyph).weak()).sense(Sense::click())).clicked() { - toggled = Some((white, i)); - } - if both { - ui.label(RichText::new(if white { tr("♙ White") } else { tr("♟ Black") }).weak()); - } - let before = (r.ply as usize) - .checked_sub(1) - .map(|p| short_label(p, &g.sans[p])) - .map_or_else(|| tr("start").to_owned(), |m| trf("after {move}", &[("move", &m)])); - let side = if r.ply.is_multiple_of(2) { tr("White to move") } else { tr("Black to move") }; - let pos_text = format!("{before} · {side}"); - let t = RichText::new(pos_text); - let resp = ui.add(egui::Label::new(if selected { t.strong() } else { t }).sense(Sense::click())); - if resp.clicked() { - clicked = Some((white, i)); - } - if resp.double_clicked() { - opened = Some((r.games[0], r.ply as usize)); - } - ui.horizontal(|ui| { - ui.spacing_mut().item_spacing.x = 4.0; - ui.label(short_label(r.ply as usize, &r.san)); - pill(ui, r.judgement).on_hover_text(review::judgement_name(r.judgement)); - }); - ui.label(short_label(r.ply as usize, &r.best_san)); - ui.label(r.times().to_string()); - if show_score { - ui.label(RichText::new(tally_hint(&r.tally)).weak()); - } - if show_opening { - let name = r.opening.and_then(|o| report.openings.get(o)).map(|o| format!("{} · {}", o.name, o.eco)).unwrap_or_default(); - ui.label(RichText::new(name).weak()); - } - ui.end_row(); - if open { - for &gi in &r.games { - let g = &report.games[gi as usize]; - let row = &self.rows[gi as usize]; - ui.label(""); - if both { - ui.label(""); - } - let text = format!("{} — {}", row.white, row.black); - let resp = ui.add(egui::Label::new(RichText::new(text).weak()).sense(Sense::click()).truncate()); - if resp.double_clicked() { - opened = Some((gi, r.ply as usize)); - } - ui.label(RichText::new(result_text(row.result)).weak()); - ui.label(RichText::new(crate::library::display_date(&g.date)).weak()); - ui.end_row(); - } - } - } - }); - let _ = dark; - let opened = opened.map(|(gi, ply)| (report.games[gi as usize].index as usize, ply)); - if let Some(k) = toggled - && !self.open_repeated.remove(&k) - { - self.open_repeated.insert(k); - } - if let Some((white, index)) = clicked { - self.focus_games = false; - self.select(Selection::Repeated { white, index }); - } - if let Some((index, ply)) = opened { - actions.push(PrepAction::OpenGame { - path: self.path.clone(), - index, - ply, - }); - } - } - - /// «By opening»: the variations with errors, the worst first, their errors by game. - fn by_opening_list(&mut self, ui: &mut Ui, both: bool, dark: bool, actions: &mut Vec) { - let Some(report) = self.report.as_ref() else { return }; - ui.label(RichText::new(tr("By opening")).strong()); + rows.push(ErrRow::OpeningsTitle); // (white, family, variation, weight) let mut vars: Vec<(bool, usize, usize, u32)> = Vec::new(); for white in self.color.colours() { let cr = report.color(white); for (f, fam) in cr.families.iter().enumerate() { for (v, var) in fam.variations.iter().enumerate() { - if var.errors.total() == 0 { - continue; + let weight: u32 = var + .error_list + .iter() + .map(|&e| cr.errors[e as usize].judgement) + .filter(|j| self.visible(*j)) + .map(judgement_weight) + .sum(); + if weight > 0 { + vars.push((white, f, v, weight)); } - let weight = var.error_list.iter().map(|&e| judgement_weight(cr.errors[e as usize].judgement)).sum(); - vars.push((white, f, v, weight)); } } } vars.sort_by_key(|v| std::cmp::Reverse(v.3)); - if vars.is_empty() { - ui.label(RichText::new(tr("None so far")).weak()); - return; - } - let mut clicked = None; - let mut opened = None; - let mut toggled = None; - for (white, f, v, _) in vars { - let cr = report.color(white); - let var = &cr.families[f].variations[v]; - let name = var - .opening - .and_then(|o| report.openings.get(o)) - .map_or_else(|| tr("no name").to_owned(), |o| format!("{} · {}", o.name, o.eco)); - let open = self.open_families.contains(&(white, f * 1000 + v + 100_000)); - ui.horizontal(|ui| { - let glyph = if open { "▾" } else { "▸" }; - if ui.add(egui::Label::new(RichText::new(glyph).weak()).sense(Sense::click())).clicked() { - toggled = Some((white, f * 1000 + v + 100_000)); - } - if both { - ui.label(RichText::new(if white { "♙" } else { "♟" }).weak()); - } - let selected = self.selection == Selection::Opening { white, family: f, variation: Some(v) }; - let t = RichText::new(name); - let resp = ui.add(egui::Label::new(if selected { t.strong() } else { t }).sense(Sense::click()).truncate()); - if resp.clicked() { - clicked = Some(Selection::Opening { white, family: f, variation: Some(v) }); - } - if resp.double_clicked() { - toggled = Some((white, f * 1000 + v + 100_000)); - } - ui.label(RichText::new(format!("· {}", crate::i18n::games(var.games.len()))).weak()); - ui.label(RichText::new("·").weak()); - ui.spacing_mut().item_spacing.x = 4.0; - counts_text(ui, &var.errors); - }); - if open { - for &ei in &var.error_list { - let e = &cr.errors[ei as usize]; - let g = &report.games[e.game as usize]; - let row = &self.rows[e.game as usize]; - let selected = self.selection == Selection::Error { white, index: ei as usize }; - ui.horizontal(|ui| { - ui.add_space(24.0); - ui.spacing_mut().item_spacing.x = 4.0; - let t = RichText::new(short_label(e.ply as usize, &e.san)); - let resp = ui.add(egui::Label::new(if selected { t.strong() } else { t }).sense(Sense::click())); - pill(ui, e.judgement).on_hover_text(format!( - "{} · {} → {}", - review::judgement_name(e.judgement), - e.before.format(), - e.after.format() - )); - ui.add_space(6.0); - ui.label(RichText::new(trf("better {best}", &[("best", &crate::san::show(&e.best_san))])).weak()); - ui.add_space(8.0); - let names = format!("{} — {}", row.white, row.black); - let r2 = ui.add(egui::Label::new(RichText::new(names).weak()).sense(Sense::click()).truncate()); - ui.label(RichText::new(result_text(row.result)).weak()); - ui.label(RichText::new(crate::library::display_date(&g.date)).weak()); - if resp.clicked() || r2.clicked() { - clicked = Some(Selection::Error { white, index: ei as usize }); - } - if resp.double_clicked() || r2.double_clicked() { - opened = Some((e.game, e.ply as usize)); - } - }); + for (white, family, variation, _) in vars { + rows.push(ErrRow::Variation { white, family, variation }); + if self.open_variations.contains(&(white, family, variation)) { + let cr = report.color(white); + for &e in &cr.families[family].variations[variation].error_list { + if self.visible(cr.errors[e as usize].judgement) { + rows.push(ErrRow::Error { white, index: e as usize }); + } } } } - let _ = dark; - let opened = opened.map(|(gi, ply)| (report.games[gi as usize].index as usize, ply)); - if let Some(k) = toggled - && !self.open_families.remove(&k) + rows + } + + /// The error of a repeated error in one of its games. + fn error_in(&self, white: bool, index: usize, game: u32) -> Option { + let cr = self.report.as_ref()?.color(white); + let r = cr.repeated.get(index)?; + cr.errors + .iter() + .position(|e| e.game == game && e.key == r.key && e.uci == r.uci) + } + + /// What a row of the errors tab chooses. + fn err_select(&mut self, row: ErrRow) { + self.err_cursor = Some(row); + self.focus_games = false; + let sel = match row { + ErrRow::Repeated { white, index } => Selection::Repeated { white, index }, + ErrRow::RepeatedGame { white, index, game } => match self.error_in(white, index, game) { + Some(e) => Selection::Error { white, index: e }, + None => return, + }, + ErrRow::Variation { white, family, variation } => Selection::Opening { + white, + family, + variation: Some(variation), + }, + ErrRow::Error { white, index } => Selection::Error { white, index }, + ErrRow::RepeatedTitle | ErrRow::OpeningsTitle => return, + }; + self.select(sel); + } + + /// The game a row opens, at the move after the error: (record, ply). A repeated + /// error opens its newest game. + fn err_open(&self, row: ErrRow) -> Option<(usize, usize)> { + let report = self.report.as_ref()?; + let (white, e) = match row { + ErrRow::Repeated { white, index } => { + let game = *self.repeated_games(white, index).first()?; + (white, self.error_in(white, index, game)?) + } + ErrRow::RepeatedGame { white, index, game } => (white, self.error_in(white, index, game)?), + ErrRow::Error { white, index } => (white, index), + _ => return None, + }; + let e = report.color(white).errors.get(e)?; + Some((report.games[e.game as usize].index as usize, e.ply as usize + 1)) + } + + fn err_toggle(&mut self, row: ErrRow) { + match row { + ErrRow::Repeated { white, index } => { + if !self.open_repeated.remove(&(white, index)) { + self.open_repeated.insert((white, index)); + } + } + ErrRow::Variation { white, family, variation } => { + let k = (white, family, variation); + if !self.open_variations.remove(&k) { + self.open_variations.insert(k); + } + } + _ => {} + } + } + + /// The errors tab as one table: the repeated errors, then by opening; a click + /// chooses, a double click (Enter) opens the game at the error. + fn errors_table(&mut self, ui: &mut Ui, actions: &mut Vec) { + let rows = self.err_rows(); + let Some(report) = self.report.as_ref() else { return }; + let both = self.color == ColorChoice::Both; + let width = ui.available_width(); + let show_last = width > 640.0; + let cursor = self.err_cursor.and_then(|c| rows.iter().position(|r| *r == c)); + ui.style_mut().interaction.selectable_labels = false; + let mut table = TableBuilder::new(ui) + .id_salt("prep_errors") + .striped(true) + .sense(Sense::click()) + .auto_shrink([false, false]) + .cell_layout(egui::Layout::left_to_right(egui::Align::Center)) + .column(Column::remainder().at_least(180.0).clip(true)) + .column(Column::exact(110.0)) + .column(Column::exact(80.0)) + .column(Column::exact(70.0)) + .column(Column::exact(120.0).clip(true)); + if show_last { + table = table.column(Column::exact((width * 0.24).clamp(120.0, 260.0)).clip(true)); + } + if self.scroll_to_selected + && let Some(i) = cursor { - self.open_families.insert(k); + table = table.scroll_to_row(i, None); } - if let Some(sel) = clicked { - self.focus_games = false; - self.select(sel); + self.scroll_to_selected = false; + let mut clicked = None; + let mut toggled = None; + let mut opened = None; + let colour_icon = |ui: &mut Ui, white: bool| { + if both { + ui.label(RichText::new(if white { "♙" } else { "♟" }).weak()) + .on_hover_text(if white { tr("As White") } else { tr("As Black") }); + } + }; + let repeated_n = rows.iter().filter(|r| matches!(r, ErrRow::Repeated { .. })).count(); + let openings_n = rows.iter().filter(|r| matches!(r, ErrRow::Variation { .. })).count(); + table.body(|body| { + body.rows(ROW_H, rows.len(), |mut row| { + let r = rows[row.index()]; + row.set_selected(Some(row.index()) == cursor); + let weak = |t: &str| RichText::new(t).weak(); + match r { + ErrRow::RepeatedTitle | ErrRow::OpeningsTitle => { + let repeated = r == ErrRow::RepeatedTitle; + let (title, empty) = if repeated { + (tr("Repeated errors — the same position, the same move"), repeated_n == 0) + } else { + (tr("By opening"), openings_n == 0) + }; + row.col(|ui| { + ui.label(RichText::new(title).strong()); + if empty { + ui.label(weak(tr("none"))); + } + }); + let captions: [&str; 5] = if repeated { + [tr("Played"), tr("Better"), "×", tr("Score"), tr("Opening")] + } else { + [tr("Played"), tr("Better"), tr("Games"), tr("Errors"), tr("Date")] + }; + for (k, c) in captions.iter().enumerate() { + if k < 4 || show_last { + row.col(|ui| { + ui.label(weak(c)); + }); + } + } + } + ErrRow::Repeated { white, index } => { + let cr = report.color(white); + let rep = &cr.repeated[index]; + let g = &report.games[rep.games[0] as usize]; + let open = self.open_repeated.contains(&(white, index)); + row.col(|ui| { + if ui.add(egui::Label::new(weak(if open { "▾" } else { "▸" })).sense(Sense::click())).clicked() { + toggled = Some(r); + } + colour_icon(ui, white); + let before = (rep.ply as usize) + .checked_sub(1) + .map(|p| short_label(p, &g.sans[p])) + .map_or_else(|| tr("start").to_owned(), |m| trf("after {move}", &[("move", &m)])); + let side = if rep.ply.is_multiple_of(2) { tr("White to move") } else { tr("Black to move") }; + ui.add(egui::Label::new(format!("{before} · {side}")).truncate()); + }); + row.col(|ui| { + ui.spacing_mut().item_spacing.x = 4.0; + ui.label(short_label(rep.ply as usize, &rep.san)); + pill(ui, rep.judgement).on_hover_text(review::judgement_name(rep.judgement)); + }); + row.col(|ui| { + ui.label(short_label(rep.ply as usize, &rep.best_san)); + }); + row.col(|ui| { + ui.label(rep.times().to_string()); + }); + row.col(|ui| { + ui.label(weak(&tally_hint(&rep.tally))); + }); + if show_last { + row.col(|ui| { + let name = rep + .opening + .and_then(|o| report.openings.get(o)) + .map(|o| format!("{} · {}", o.name, o.eco)) + .unwrap_or_default(); + ui.add(egui::Label::new(weak(&name)).truncate()).on_hover_text(name); + }); + } + } + ErrRow::RepeatedGame { white, index, game } => { + let g = &report.games[game as usize]; + let lr = &self.rows[game as usize]; + let _ = (white, index); + row.col(|ui| { + ui.add_space(28.0); + ui.add(egui::Label::new(format!("{} — {}", lr.white, lr.black)).truncate()); + }); + row.col(|_| {}); + row.col(|_| {}); + row.col(|_| {}); + row.col(|ui| { + ui.label(result_text(lr.result)); + }); + if show_last { + row.col(|ui| { + ui.label(weak(&crate::library::display_date(&g.date))); + }); + } + } + ErrRow::Variation { white, family, variation } => { + let cr = report.color(white); + let var = &cr.families[family].variations[variation]; + let open = self.open_variations.contains(&(white, family, variation)); + let mut counts = ErrorCounts::default(); + for &e in &var.error_list { + counts.add(cr.errors[e as usize].judgement); + } + row.col(|ui| { + if ui.add(egui::Label::new(weak(if open { "▾" } else { "▸" })).sense(Sense::click())).clicked() { + toggled = Some(r); + } + colour_icon(ui, white); + let name = var + .opening + .and_then(|o| report.openings.get(o)) + .map_or_else(|| tr("no name").to_owned(), |o| format!("{} · {}", o.name, o.eco)); + ui.add(egui::Label::new(name.as_str()).truncate()).on_hover_text(name); + }); + row.col(|_| {}); + row.col(|_| {}); + row.col(|ui| { + ui.label(weak(&var.games.len().to_string())); + }); + row.col(|ui| { + ui.spacing_mut().item_spacing.x = 4.0; + counts_text(ui, &self.shown_counts(&counts)); + }); + if show_last { + row.col(|_| {}); + } + } + ErrRow::Error { white, index } => { + let e = &report.color(white).errors[index]; + let g = &report.games[e.game as usize]; + let lr = &self.rows[e.game as usize]; + row.col(|ui| { + ui.add_space(28.0); + ui.add(egui::Label::new(format!("{} — {}", lr.white, lr.black)).truncate()); + }); + row.col(|ui| { + ui.spacing_mut().item_spacing.x = 4.0; + ui.label(short_label(e.ply as usize, &e.san)); + pill(ui, e.judgement).on_hover_text(format!( + "{} · {} → {}", + review::judgement_name(e.judgement), + e.before.format(), + e.after.format() + )); + }); + row.col(|ui| { + ui.label(short_label(e.ply as usize, &e.best_san)); + }); + row.col(|_| {}); + row.col(|ui| { + ui.label(result_text(lr.result)); + }); + if show_last { + row.col(|ui| { + ui.label(weak(&crate::library::display_date(&g.date))); + }); + } + } + } + if !r.is_title() { + let resp = row.response(); + if resp.double_clicked() { + match r { + ErrRow::Repeated { .. } | ErrRow::Variation { .. } => toggled = Some(r), + _ => opened = Some(r), + } + clicked = Some(r); + } else if resp.clicked() { + clicked = Some(r); + } + } + }); + }); + if let Some(r) = toggled { + self.err_toggle(r); } - if let Some((index, ply)) = opened { + if let Some(r) = clicked { + self.err_select(r); + } + if let Some((index, ply)) = opened.and_then(|r| self.err_open(r)) { actions.push(PrepAction::OpenGame { path: self.path.clone(), index, @@ -3560,6 +4157,72 @@ impl PrepScreen { } } + /// The keyboard in the errors tab: ↑/↓ the rows, →/← open and close, Enter the game. + #[allow(clippy::too_many_arguments)] + fn errors_keys(&mut self, up: bool, down: bool, left: bool, right: bool, enter: bool, space: bool, home: bool, end: bool, actions: &mut Vec) { + let rows: Vec = self.err_rows().into_iter().filter(|r| !r.is_title()).collect(); + if rows.is_empty() { + return; + } + let at = self.err_cursor.and_then(|c| rows.iter().position(|r| *r == c)); + let Some(at) = at else { + if up || down || home || end { + self.err_select(rows[0]); + self.scroll_to_selected = true; + } + return; + }; + let r = rows[at]; + let next = if down { + Some((at + 1).min(rows.len() - 1)) + } else if up { + Some(at.saturating_sub(1)) + } else if home { + Some(0) + } else if end { + Some(rows.len() - 1) + } else { + None + }; + if let Some(i) = next { + self.err_select(rows[i]); + self.scroll_to_selected = true; + return; + } + let open = match r { + ErrRow::Repeated { white, index } => Some(self.open_repeated.contains(&(white, index))), + ErrRow::Variation { white, family, variation } => { + Some(self.open_variations.contains(&(white, family, variation))) + } + _ => None, + }; + if (right && open == Some(false)) || (left && open == Some(true)) || (space && open.is_some()) { + self.err_toggle(r); + } else if left { + // A game or an error row: to the row it is under. + if let Some(parent) = rows[..at] + .iter() + .rev() + .find(|p| matches!(p, ErrRow::Repeated { .. } | ErrRow::Variation { .. })) + { + self.err_select(*parent); + self.scroll_to_selected = true; + } + } else if enter { + match self.err_open(r) { + Some((index, ply)) => actions.push(PrepAction::OpenGame { + path: self.path.clone(), + index, + ply, + }), + None => { + self.focus_games = true; + self.games_cursor = Some(0); + } + } + } + } + // ----- the games list ------------------------------------------------------------ fn games_list(&mut self, ui: &mut Ui, actions: &mut Vec) { @@ -3595,7 +4258,7 @@ impl PrepScreen { ui.horizontal(|ui| { ui.label(RichText::new(title).strong()); if self.selection != Selection::None - && ui.small_button("🗙").on_hover_text(tr("All games")).clicked() + && crate::widgets::small_close_button(ui).on_hover_text(tr("All games")).clicked() { clear = true; } @@ -3607,26 +4270,44 @@ impl PrepScreen { if list.is_empty() { return; } - let ply = self.selection_ply(); let width = ui.available_width(); - let show_event = width > 820.0; - let show_then = width > 600.0; - let show_date = width > 480.0; - let show_elo = width > 400.0; - let show_errors = report.engine.is_some() && width > 540.0; - let columns = 3 + 2 * usize::from(show_elo) + usize::from(show_date) + usize::from(show_then) + usize::from(show_event) + usize::from(show_errors); - let fixed = 44.0 - + if show_elo { 88.0 } else { 0.0 } - + if show_date { 84.0 } else { 0.0 } - + if show_errors { 80.0 } else { 0.0 } - + match (show_then, show_event) { - (true, true) => 150.0, - (true, false) => 100.0, - _ => 0.0, + // The names first (at least 110 each): the event, «Then», the ratings (they go + // into the name cell), the errors and the date give way in turn. + let mut show_event = true; + let mut show_then = true; + let mut show_elo = true; + let mut show_errors = report.evaluated(); + let mut show_date = true; + let gap = ui.spacing().item_spacing.x; + let player_w_of = |event: bool, then: bool, elo: bool, errors: bool, date: bool| -> f32 { + let columns = 3 + 2 * usize::from(elo) + usize::from(date) + usize::from(then) + usize::from(event) + usize::from(errors); + let fixed = 44.0 + + if elo { 88.0 } else { 0.0 } + + if date { 84.0 } else { 0.0 } + + if errors { 80.0 } else { 0.0 } + + match (then, event) { + (true, true) => 150.0, + (true, false) => 100.0, + _ => 0.0, + } + + if event { 100.0 } else { 0.0 } + + gap * columns as f32; + (width - fixed) / 2.0 + }; + for drop in 0..5 { + if player_w_of(show_event, show_then, show_elo, show_errors, show_date) >= 110.0 { + break; } - + if show_event { 100.0 } else { 0.0 } - + ui.spacing().item_spacing.x * columns as f32; - let player_w = ((width - fixed) / 2.0).clamp(60.0, 170.0); + match drop { + 0 => show_event = false, + 1 => show_then = false, + 2 => show_elo = false, + 3 => show_errors = false, + _ => show_date = false, + } + } + let player_w = player_w_of(show_event, show_then, show_elo, show_errors, show_date).clamp(60.0, 170.0); + let plies: HashMap = list.iter().map(|&g| (g, self.game_ply(g))).collect(); ui.style_mut().interaction.selectable_labels = false; let mut table = TableBuilder::new(ui) .id_salt("prep_games_table") @@ -3716,13 +4397,21 @@ impl PrepScreen { ui.add(egui::Label::new(if player_names.contains(n) { t.strong() } else { t }).truncate()); }; let elo = |e: u16| if e > 0 { e.to_string() } else { String::new() }; - row.col(|ui| name(ui, &r.white)); + let ply = plies.get(&gi).copied().unwrap_or(0); + // Without their column the ratings go after the names, weak. + let name = |ui: &mut Ui, n: &str, e: u16| { + name(ui, n); + if !show_elo && e > 0 { + ui.label(RichText::new(e.to_string()).weak().small()); + } + }; + row.col(|ui| name(ui, &r.white, r.white_elo)); if show_elo { row.col(|ui| { ui.label(RichText::new(elo(r.white_elo)).weak()); }); } - row.col(|ui| name(ui, &r.black)); + row.col(|ui| name(ui, &r.black, r.black_elo)); if show_elo { row.col(|ui| { ui.label(RichText::new(elo(r.black_elo)).weak()); @@ -3761,7 +4450,7 @@ impl PrepScreen { row.col(|ui| { ui.spacing_mut().item_spacing.x = 3.0; let cr = report.color(g.white); - for e in cr.errors.iter().filter(|e| e.game == gi) { + for e in cr.errors.iter().filter(|e| e.game == gi && self.visible(e.judgement)) { let number = e.ply as usize / 2 + 1; ui.label(RichText::new(format!("{number}.")).small()); pill(ui, e.judgement).on_hover_text(format!( @@ -3793,6 +4482,7 @@ impl PrepScreen { && s.ply + 1 == s.positions.len() { let g = &report.games[gi as usize]; + let ply = plies.get(&gi).copied().unwrap_or(0); if let Some(m) = g.ucis.get(ply).and_then(|u| u.parse::().ok()).and_then(|u| u.to_move(&s.positions[s.ply]).ok()) { self.hover_arrow = m.from().map(|f| (f, crate::board::ui_target(&m))); } @@ -3805,7 +4495,7 @@ impl PrepScreen { actions.push(PrepAction::OpenGame { path: self.path.clone(), index: report.games[gi as usize].index as usize, - ply, + ply: self.open_ply(gi), }); } } @@ -3918,7 +4608,7 @@ impl PrepScreen { actions.push(PrepAction::OpenGame { path: self.path.clone(), index: r.games[list[next] as usize].index as usize, - ply: self.selection_ply(), + ply: self.open_ply(list[next]), }); } return; @@ -3926,28 +4616,7 @@ impl PrepScreen { match self.tab { Tab::Repertoire => self.tree_keys(up, down, left, right, enter, space, home, end), Tab::Openings => self.openings_keys(up, down, left, right, enter, space, home, end), - Tab::Errors => { - if enter && let Some(r) = &self.report { - let open = match &self.selection { - Selection::Repeated { white, index } => { - let rep = &r.color(*white).repeated[*index]; - Some((rep.games[0], rep.ply as usize)) - } - Selection::Error { white, index } => { - let e = &r.color(*white).errors[*index]; - Some((e.game, e.ply as usize)) - } - _ => None, - }; - if let Some((gi, ply)) = open { - actions.push(PrepAction::OpenGame { - path: self.path.clone(), - index: r.games[gi as usize].index as usize, - ply, - }); - } - } - } + Tab::Errors => self.errors_keys(up, down, left, right, enter, space, home, end, actions), } } @@ -4218,8 +4887,8 @@ mod tests { assert_eq!(short_label(4, "Bb5"), "3.Bb5"); let sans: Vec = ["e4", "c5", "Nf3"].iter().map(|s| (*s).to_owned()).collect(); assert_eq!(line_text(&sans), "1.e4 c5 2.Nf3"); - assert_eq!(shorten_middle("1. e4 c5 2. Nf3", 100), "1. e4 c5 2. Nf3"); - assert_eq!(shorten_middle("abcdefghij", 7), "abc…hij"); + assert_eq!(table_line("1. e4 c5 2. Nf3 Nc6 3. Bb5"), vec!["1.e4", "c5", "2.Nf3", "Nc6", "3.Bb5"]); + assert_eq!(table_line("1... e5"), vec!["1…e5"]); let ucis: Vec = ["e2e4", "c7c5", "g1f3"].iter().map(|s| (*s).to_owned()).collect(); assert_eq!(replay(&ucis).len(), 4); assert_eq!(replay(&["zz".to_owned()]).len(), 1); diff --git a/apps/desktop/src/settings.rs b/apps/desktop/src/settings.rs index b57d7df..90396cf 100644 --- a/apps/desktop/src/settings.rs +++ b/apps/desktop/src/settings.rs @@ -167,6 +167,8 @@ pub struct PrepSettings { pub branches_from: u32, /// The engine was started from the dialog once: the button starts it at once. pub chosen: bool, + /// Inaccuracies (?!) are shown with the mistakes and blunders. + pub inaccuracies: bool, } impl Default for PrepSettings { @@ -177,6 +179,7 @@ impl Default for PrepSettings { plies: 20, branches_from: 3, chosen: false, + inaccuracies: true, } } } diff --git a/crates/prep/src/lib.rs b/crates/prep/src/lib.rs index 99a1869..2a9623f 100644 --- a/crates/prep/src/lib.rs +++ b/crates/prep/src/lib.rs @@ -138,7 +138,7 @@ pub struct BookInfo { pub left: u32, } -const REPORT_VERSION: u32 = 2; +const REPORT_VERSION: u32 = 3; /// Everything the preparation knows about the player. #[derive(Clone, Debug, Serialize, Deserialize)] @@ -400,6 +400,12 @@ impl PrepReport { out } + /// Some position has an engine evaluation (from a run or from the cache): the + /// errors are judged. + pub fn evaluated(&self) -> bool { + self.evals.values().any(|e| e.nodes != EXACT) + } + /// Distinct positions an engine run looks at. pub fn distinct_positions(&self) -> usize { self.positions().len() @@ -890,6 +896,7 @@ mod tests { } assert_eq!(report.missing(1000), 0); assert_eq!(report.missing(2000), total); + assert!(report.evaluated()); report.refresh(); assert_eq!(report.engine.as_ref().map(|e| e.nodes), Some(1000)); let w = &report.white; @@ -921,7 +928,9 @@ mod tests { assert!(PrepReport::load(&dir, &db, &other, &Options::default()).is_none()); let mut fresh = build(&db, &player, &Options::default(), &AtomicUsize::new(0), &never) .expect("report"); + assert!(!fresh.evaluated()); assert_eq!(fresh.take_cached(&cache, 1000), total); + assert!(fresh.evaluated(), "evaluations from the cache judge the errors too"); assert_eq!(fresh.white.repeated, report.white.repeated); assert_eq!(fresh.take_cached(&cache, 1000), 0); let _ = std::fs::remove_dir_all(&dir); diff --git a/crates/prep/src/openings.rs b/crates/prep/src/openings.rs index f7cecb0..21a8604 100644 --- a/crates/prep/src/openings.rs +++ b/crates/prep/src/openings.rs @@ -122,13 +122,34 @@ pub fn get(id: usize) -> &'static Opening { &table().entries[id] } -/// The deepest named position among `keys` (the positions of a line, in order): its -/// index in `keys` and its entry. +/// The name of a line (the positions `keys`, in order): its deepest named position, +/// its index in `keys` and its entry — but a deeper position whose name is only a +/// more general one ("Sicilian Defense" after 3.d4 in the "Modern Variations") does +/// not take the name back: the most precise name met stays. pub fn deepest(keys: &[u64]) -> Option<(usize, usize)> { - keys.iter() - .enumerate() - .rev() - .find_map(|(i, k)| by_position(*k).map(|id| (i, id))) + let mut out: Option<(usize, usize)> = None; + for (i, k) in keys.iter().enumerate() { + if let Some(id) = by_position(*k) + && out.is_none_or(|(_, cur)| !more_general(id, cur)) + { + out = Some((i, id)); + } + } + out +} + +/// Entry `a`'s name is a more general form of entry `b`'s ("Sicilian Defense" of +/// "Sicilian Defense: Modern Variations"; "…: Najdorf Variation" of "…: Najdorf +/// Variation, English Attack"). +pub fn more_general(a: usize, b: usize) -> bool { + is_prefix_name(&get(a).name, &get(b).name) +} + +/// `general` names a family or a variation `precise` is part of. +pub fn is_prefix_name(general: &str, precise: &str) -> bool { + precise.len() > general.len() + && precise.starts_with(general) + && matches!(precise[general.len()..].chars().next(), Some(':' | ',')) } /// The openings a report refers to, each once, in the order they were first met. @@ -198,6 +219,17 @@ mod tests { assert_eq!(o.variation(), None); // Nothing before the first move. assert_eq!(deepest(&keys(&[])), None); + // A deeper position with a more general name keeps the precise one: the + // table names 1.e4 c5 2.Nf3 d6 3.d4 just «Sicilian Defense». + let line = keys(&["e4", "c5", "Nf3", "d6", "d4"]); + let after_d4 = by_position(line[5]).map(|id| get(id).name.clone()); + assert_eq!(after_d4.as_deref(), Some("Sicilian Defense")); + let (at, id) = deepest(&line).expect("named"); + assert_eq!((at, get(id).name.as_str()), (4, "Sicilian Defense: Modern Variations")); + assert!(is_prefix_name("Sicilian Defense", "Sicilian Defense: Modern Variations")); + assert!(is_prefix_name("A: B", "A: B, C")); + assert!(!is_prefix_name("Sicilian Defense", "Sicilian Defense")); + assert!(!is_prefix_name("English", "English Opening")); } #[test] diff --git a/crates/prep/src/tree.rs b/crates/prep/src/tree.rs index 5796c97..764f3f4 100644 --- a/crates/prep/src/tree.rs +++ b/crates/prep/src/tree.rs @@ -96,7 +96,14 @@ impl Tree { Some(&c) => c as usize, None => { let key = game.keys[i + 1]; - let named = openings::by_position(key); + // A more general name than the path's keeps the path's + // (see `openings::deepest`). + let parent = tree.nodes[at].opening.map(|o| openings.list[o].name.clone()); + let named = openings::by_position(key).filter(|&id| { + parent + .as_deref() + .is_none_or(|p| !openings::is_prefix_name(&openings::get(id).name, p)) + }); let opening = named .map(|id| openings.intern(id)) .or(tree.nodes[at].opening); @@ -139,9 +146,52 @@ impl Tree { for n in &mut tree.nodes { n.transposition = seen.get(&n.key).is_some_and(|c| *c > 1); } + // One position, one name: the most precise of its move orders' names; a node + // is «named» where its name differs from its parent's. + let mut best: HashMap = HashMap::new(); + for n in tree.nodes.iter().filter(|n| n.transposition) { + if let Some(o) = n.opening { + let e = best.entry(n.key).or_insert(o); + if openings::is_prefix_name(&openings.list[*e].name, &openings.list[o].name) { + *e = o; + } + } + } + for i in 0..tree.nodes.len() { + if let Some(&o) = best.get(&tree.nodes[i].key) { + tree.nodes[i].opening = Some(o); + } + let parent = tree.nodes[i].parent.and_then(|p| tree.nodes[p as usize].opening); + let name = |o: Option| o.map(|o| openings.list[o].name.as_str()); + tree.nodes[i].named = tree.nodes[i].opening.is_some() && name(tree.nodes[i].opening) != name(parent); + } tree } + /// The games through the position `key` by any move order (indices into + /// [`crate::PrepReport::games`], in order). + pub fn position_games(&self, key: u64) -> Vec { + let mut out: Vec = self + .nodes + .iter() + .filter(|n| n.key == key) + .flat_map(|n| n.games.iter().copied()) + .collect(); + out.sort_unstable(); + out.dedup(); + out + } + + /// The latest date among the games through the position `key`. + pub fn position_last_date(&self, key: u64) -> &str { + self.nodes + .iter() + .filter(|n| n.key == key) + .map(|n| n.last_date.as_str()) + .max_by_key(|d| chess_import::pgn_catalog::date_number(d)) + .unwrap_or("") + } + fn count(&mut self, node: usize, g: u32, game: &GameInfo) { let n = &mut self.nodes[node]; n.tally.add(game.outcome); @@ -244,6 +294,8 @@ pub(crate) mod tests { let after_d4 = tree.nodes.iter().find(|n| n.ply == 3 && n.san == "d4").unwrap(); assert_eq!(after_nf3.key, after_d4.key); assert_eq!(tree.position_tally(after_nf3.key).games, 2); + assert_eq!(tree.position_games(after_nf3.key), vec![0, 1]); + assert_eq!(tree.position_last_date(after_nf3.key), "2026.03.01"); assert!(!tree.nodes.iter().find(|n| n.san == "c4").unwrap().transposition); assert_eq!(tree.path(after_d4.parent.unwrap() as usize), vec!["Nf3", "d5"]); // Names: 1.d4 d5 2.Nf3 is named in the table, 2...Nf6 is too (Zukertort); diff --git a/docs/design/opponent-prep.md b/docs/design/opponent-prep.md index 697fe89..fe60a31 100644 --- a/docs/design/opponent-prep.md +++ b/docs/design/opponent-prep.md @@ -477,6 +477,55 @@ 9. **Период** (последний год / всё) как переключатель в отчёте — не делаю (объём «только дебюты», фильтр библиотеки закрывает); подтвердить. 10. **«Играть отсюда…» и «Открыть как партию»** с доски отчёта — в первую версию (рекомендация: это существующие действия) или после? +## 12. Ревью реализации (f02f140, 2026-10-28) + +Проверено по снимкам разработчика (1400×1000 тёмная, 640×1000, 640×480) и своим: светлая тема (репертуар, ошибки), «Чёрные» с книгой «Эта же база», «Дебюты» при «Чёрные», диалог движка, ход движка, 804/803×900, 960×1050, диалог при 640×480, «Ошибки» и «Дебюты» при 640×1000, вход из панели фильтров. Снимки — `scratchpad/prep-design/*.png`. Не снято: выбранная ошибка со стрелками на доске (выбрать её можно только мышью — см. А6). + +Общее: архитектура экрана, диалог, три вкладки, список партий, книга, движок, стопка и низкое окно — по дизайну; палитра отметок и пилюли — как в нотации, в обеих темах читаются. Замечания ниже — по приоритету: **А** мешает читать и понимать данные, **Б** расходится с визуальной системой, **В** узкие окна. У каждого — правка. + +### А. Данные и понимание + +1. **Сводка — одно длинное предложение.** С книгой и после движка строка цвета — ~130 знаков из семи частей через «·»; при 960 и 804 она переносится посреди фразы («Ошибок в / дебюте: …», `w960.png`, `w804.png`), и вторая строка читается как новая запись. Правка: сводка — `egui::Grid` в две строки с колонками «Цвет · Партий · Счёт (+= −) · Соперники · Последняя · Книга · Ошибки»; при нехватке ширины колонки прячутся справа налево (Книга → Соперники → Последняя), как в дереве, — переносов не бывает. Счётчики ошибок — через уже существующий `counts_text` («60 ?! · 14 ? · 8 ??» цветом отметок; плюс «в 40 % партий»): три числа одного вида в простом тексте («неточностей 60 · ошибок 14 · зевков 8») глаз не различает и они не связаны с колонками «Ошибки» в таблицах, где те же счётчики цветные. «Последняя» в сводке дублирует корень дерева — можно убрать первой. +2. **Числа в полосе счёта спорят с «Счётом».** В строке «Белыми · 138» рядом стоят «78 %» (очки) и «75» внутри полосы (доля побед белых); у чёрных внутри светлого сегмента стоит доля побед **соперника** («Queen's Pawn Game · 56 % · [56]», `openings_black.png`). Два разных числа в одной ячейке без подписи — главный источник непонимания колонки «Счёт». Правка: в полосе 56 точек чисел не рисовать (`results_bar` с флагом без текста либо своя узкая версия); проценты — только в подсказке «+10 =1 −3». Полоса остаётся абсолютной (Б светлый слева, Ч тёмный справа), как на доске поиска. Заодно: при «мало партий» полоса не приглушается (`painter_alpha` вычислен и выброшен) — рисовать её с `gamma_multiply(0.5)` или не рисовать вовсе. +3. **Линия под доской нумерует чёрные ходы:** «1.e4 1…c5 2.Nf3 2…d6». Нигде в программе так не пишут. Правка: `short_label` только для первого хода и для белых ходов; чёрные — голым SAN («1.e4 c5 2.Nf3 d6»). Та же линия нужна в «Первые ходы» таблицы дебютов, где сейчас формат таблицы lichess «1. e4 c5» (с пробелом после номера) — привести к `line_text`. +4. **«Первые ходы» нет на 1400×1000 при «Оба»** (`openings.png`): колонка «Цвет» 90 + «Дебют» ≥ 200 не оставляют 160 для ходов, и между «Дебют» и «ECO» пустота. Это именно те данные, ради которых владелец просил «всегда с первыми ходами». Правка: «Цвет» — только значок `♙`/`♟` шириной 24 с подсказкой (слово «Белыми» 11 раз подряд — шум); минимум «Дебют» 180, «Первые ходы» 140; порядок скрытия как в §6. Альтернатива лучше, но дороже: вместо колонки — строки-заголовки «Белыми · 138» / «Чёрными · 133», как корни дерева. Также сокращение линии режет ход пополам и даёт двойное многоточие («…5 4. Bg2 Be7 5. …», `openings_black.png`): `shorten_middle` — по границам ходов и по измеренной ширине, без второго `truncate`. +5. **Имя дебюта в дереве «откатывается»:** под «2…d6 Sicilian Defense: Modern Variations» стоит «3.d4 Sicilian Defense» — таблица даёт менее точное имя глубже, и правило «показывать, когда меняется» печатает его как новое. Правка: не показывать имя, если оно — префикс имени родителя (семейство без варианта); лучше — в `prep` сделать имя узла «липким» (самое глубокое имя по пути), тогда и подсказка, и таблица дебютов согласуются. +6. **Вкладка «Ошибки» без клавиатуры и без строк.** Стрелки там ничего не выбирают (`keyboard` → только Enter), а щелчок ловит лишь подпись «Позиция» / имя дебюта, не строка (`egui::Grid` без ответа строки). В «По дебютам» счётчики ошибок стоят на разной высоте — не сравнить. Правка: обе части — `TableBuilder` со `striped`, `sense(click)` и выделением строки, как дерево; клавиши ↑/↓/→/← по общему списку строк (повторяющиеся + дебюты + раскрытые партии), Enter — открыть партию. Это же даст хук для снимка выбранной ошибки (`CP_KEYS`). +7. **Четыре строки одних и тех же чисел** на 1400 (`errors.png`): сводка (2 строки) и сразу под ней в «Ошибках» «Белыми: … / Чёрными: …». Правка: во вкладке строки цветов показывать только в стопке (там сводка их не несёт, §6); рядом — только строка состояния «Ошибки найдены · Stockfish 19 · Быстро · 2 947 позиций · Точнее…». +8. **Колонка «Дебют» в дереве теряет ECO:** длинное имя забирает всю ширину, ECO обрезается до «··» («Nyezhmetdinov-Rossolimo Attack ··», `repertoire.png`). Правка: ECO класть первым в `right_to_left`, имя — в остаток с `truncate`. +9. Мелкое: «2947 позиций» без разделителя тысяч (статус, диалог, прогресс) — `large_db::grouped`; «%» в таблице дебютов без подсказки (в дереве есть) — «Доля от партий цветом; у варианта — от семейства»; «Книга [Нет ▾]» без слова «Книга» при W < 640 читается как «Нет» неизвестно чего — текст в самом списке «Книга: Нет». + +### Б. Визуальная система + +1. **«▾» слева от «Ошибки найдены»** (все снимки): в `right_to_left` кнопка параметров добавлена второй и встала левее подписи — выглядит как сворачивалка. Правка: добавлять «▾» первой (окажется справа), как `[Точнее…] [▾]` в §4.4. +2. **Три порядка цветов:** диалог «Оба · Белые · Чёрные», диалог движка «Белые · Чёрные · Оба», переключатель «Белые · Чёрные · Оба». Правка: везде «Белые · Чёрные · Оба». +3. **«🗙» у заголовка списка партий** — крупная рамочная кнопка тяжелее заголовка. Правка: `widgets::small_close_button`, как в диалоге у поля «Игрок» и у чипов. +4. **Полоса счёта в светлой теме** почти сливается с фоном строки (`light_rep.png`) — общее свойство `results_bar`, не этого экрана; если править п. А2 своей версией полосы — дать светлому сегменту обводку 1 px `weak_text_color` в светлой теме. +5. Глубина «Дебют: 10 ходов» при W < 760 превращается в голое «10» (`narrow640.png`), хотя в строке остаётся ~150 точек. Правка: выбирать подпись по измеренной ширине («Дебют: 10 ходов» → «10 ходов» → «10 х.»), а не по порогу окна. + +### В. Узкие и низкие окна + +1. **Имена в списке партий уже `NAME_MIN` 110** при 640–804 («Вафин Рад…», «Конюшенко С…», `narrow640.png`, `w804.png`): Elo и «Дальше» остаются, имена ужимаются до 82–95. Правка (как §3.10 и `board-search.md` §2.7): сначала считать `player_w`; если < 110 — прятать «Дальше», затем Elo (в ячейку имени слабым), и только потом резать имена. +2. **640×480: дереву остаётся 6 строк** (`low640.png`) — минимум списка партий 4 строки + 60 и доля 35 %. Правка: при H < 600 минимум списка — 2 строки, доля 25 %; дерево получает ≈ 10 строк. +3. В стопке над доской нет подписи «Ход белых · после 2.Nf3» (`board_caption` не вызывается, `w803.png`): добавить первой строкой в колонку справа от доски. +4. «другие ходы (1)» на глубине обрезается в «другие ход…» при минимуме колонки 180 (`w804.png`): короче — «другие (1)». +5. Пороги 804/803 и 720 работают как задумано (`w804.png` — рядом, `w803.png` — стопка; `low640.png` — доска по кнопке); диалоги при 640×480 помещаются. + +### Что принимаю из отклонений разработчика + +- «⤳» → «⇄» — да, яснее. +- Имя дебюта в дереве только когда меняется относительно родителя — да, с поправкой А5 (не показывать менее точное имя). +- «Первые ходы» семейства — его позиция по таблице — да. +- Строки ошибок в сводке нет в стопке — да (§6 так и задумано). +- Строка ошибок в сводке простым текстом — **нет**, см. А1: `counts_text` уже есть, это три строки правки. +- Отложены: вход с экрана партии, мини-доска в подсказке, «показать в дереве», сортировка таблицы дебютов — да; «Точнее…» только во вкладке «Ошибки», а не в строке переключателей — да, строка и так тесная. +- Автопереход на вкладку «Ошибки» при запуске движка (в дизайне не было) — да, полезно. + +### Не проверено + +Выбранная ошибка (стрелки сыгранного/лучшего хода, строка «… — лучше … · 0,0 → −1,2» под доской), «Остановлено → Продолжить», «нет партий цветом», «книга не открылась», партия, открытая из отчёта («⬅ К подготовке»). Для снимков нужна клавиатура во вкладке «Ошибки» (А6) или хук `CP_PREP_SELECT`. + ## Изменения - 2026-10-28: первая версия. +- 2026-10-28: §12 — ревью реализации f02f140 (замечания А/Б/В, принятые отклонения, что не проверено).