2026-10-18 01:54:06 +03:00
//! Top-level application state: library ⇄ game, settings, engine, background dialogs.
use crate ::i18n ::{ tr , trf } ;
use std ::path ::PathBuf ;
use std ::sync ::mpsc ::{ Receiver , Sender , channel } ;
use std ::sync ::atomic ::AtomicBool ;
use chess_core ::{ Game , PgnDatabase , TextEncoding } ;
use egui ::{ Key , KeyboardShortcut , Modifiers , RichText } ;
use crate ::analysis ::Analysis ;
use crate ::assets ::Sound ;
use crate ::game_view ::{ GameAction , GameSource , GameView } ;
use crate ::library ::{ Library , LibraryAction } ;
use crate ::settings ::{ EngineConfig , Settings , write_atomic } ;
use crate ::settings_ui ::{ SettingsAction , SettingsWindow , Tab } ;
use crate ::sound ::SoundPlayer ;
/// Results of work done off the UI thread (file dialogs, engine checks).
enum Async {
DatabasesPicked ( Vec < PathBuf > ) ,
SavePicked ( PathBuf ) ,
SaveCancelled ,
EngineChecked ( Result < ( PathBuf , String ) , String > ) ,
}
#[ derive(Clone, Copy, PartialEq) ]
enum CloseIntent {
BackToLibrary ,
QuitApp ,
2026-10-19 23:20:37 +03:00
/// Back to the game this one was opened from (reference tab).
ReturnToPrevious ,
2026-10-18 01:54:06 +03:00
}
pub struct App {
settings : Settings ,
settings_dirty : bool ,
sound : SoundPlayer ,
library : Library ,
game : Option < GameView > ,
analysis : Analysis ,
Game analysis: whole-game engine review with graph, report and notation marks
- Separate background engine job over the main line (go nodes N), live engine
paused meanwhile; header button with progress/cancel, Shift+A.
- Lichess-method metrics: win %, move/game accuracy, inaccuracy/mistake/blunder,
ACPL, accuracy per game phase.
- Evaluation graph under the board, Report tab, engine badges and better lines
in the notation (preview on click), launch dialog with quality/threads/memory
and time estimate.
- Results cached in ~/.cache/chesspenguin/analysis; [%eval] read from PGN.
- Write analysis to the game as one undo step (chess-core: Game::annotate,
Game::edit_group).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 11:03:23 +03:00
/// The live engine was stopped for a whole-game analysis and resumes after it.
live_paused : bool ,
2026-10-18 01:54:06 +03:00
settings_ui : SettingsWindow ,
toast : Option < ( String , f64 ) > ,
tx : Sender < Async > ,
rx : Receiver < Async > ,
confirm_close : Option < CloseIntent > ,
/// Close the game once the pending "save as" succeeds.
close_after_save : Option < CloseIntent > ,
2026-10-19 09:33:37 +03:00
/// The offer to download Stockfish when an action needs an engine.
engine_offer : crate ::engine_offer ::EngineOffer ,
offer_hook_done : bool ,
2026-10-19 21:10:22 +03:00
/// Lichess / Chess.com: which answered at start, and the download window.
online_probe : crate ::online ::Probe ,
online : crate ::online_ui ::OnlineDialog ,
ChessPenguin 0.5.0: find games by position on a board
The "Board" pane next to the game list (Ctrl+B): moves played on it filter
the list to the games that reach the position by any move order, together
with the filters; the moves played next are listed with games, a results
bar, average rating and last year. Games reached by another order are
marked and "only this order" narrows the list; strongest games first.
A game opens at the found position; "Find in database" (Ctrl+Shift+F)
searches the position of the game on screen.
Large ChessBase databases: a fast main-line walk (moves built from the
board), pruning by material and pawns, the .cbb search booster when valid;
Mega Database: 0.4 s for the starting position, under 0.1 s deeper.
PGN databases and books: positions of all games, variations included,
kept in memory once built.
Also: × to clear filter and search fields; "Open as a new game" opens at
the last move; the "Moves" column is now "Length".
Docs: decision 13, design (docs/design/board-search.md), research
(position-search, position-index), dev notes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 08:42:08 +03:00
/// A position to find in a database once the game is closed.
find_after_close : Option < ( PathBuf , shakmaty ::Chess , Vec < shakmaty ::Move > , bool ) > ,
2026-10-19 23:20:37 +03:00
/// Games left for one opened from the reference tab ("⬅ Back to …"), kept as they
/// were (unsaved edits included).
previous_games : Vec < GameView > ,
/// The last game showed the reference tab: the next one opens with it too.
reference_tab : bool ,
2026-10-20 07:55:59 +03:00
/// Updates: the release check, how this copy is installed, the window, and the
/// executable to start after quitting (a restart into the new version).
update_check : crate ::update ::Check ,
update_kind : crate ::update ::Install ,
update_ui : crate ::update_ui ::UpdateDialog ,
restart_to : Option < PathBuf > ,
2026-10-19 23:20:37 +03:00
/// The reference tab of games (kept across games: its results stay cached).
reference : crate ::reference ::Reference ,
2026-10-18 01:54:06 +03:00
allow_quit : bool ,
applied_dark : Option < bool > ,
debug : DebugStartup ,
}
/// Dev-only startup automation for screenshots/manual QA (see docs/dev/desktop.md):
/// `CP_OPEN=<game index>` opens a game from the first database, `CP_PLY=<n>` steps
Settings → About: version, website, release notes, sources, support, donations
The About tab shows the version and links to the website (in the interface language),
the release notes of this version, the source code and the support contacts; "Support
the project" opens chess.vrubel.xyz/donate, which the website server sends on to
DONATE_URL (the support section while it is not set), so the donation service can change
without a new release. Links open in the browser (eframe "links": webbrowser, MIT OR
Apache-2.0).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 15:11:25 +03:00
/// along the main line, `CP_ANALYZE=1` starts the engine, `CP_SETTINGS=1` opens settings (`=board`, `=about`: that tab),
Game analysis: whole-game engine review with graph, report and notation marks
- Separate background engine job over the main line (go nodes N), live engine
paused meanwhile; header button with progress/cancel, Shift+A.
- Lichess-method metrics: win %, move/game accuracy, inaccuracy/mistake/blunder,
ACPL, accuracy per game phase.
- Evaluation graph under the board, Report tab, engine badges and better lines
in the notation (preview on click), launch dialog with quality/threads/memory
and time estimate.
- Results cached in ~/.cache/chesspenguin/analysis; [%eval] read from PGN.
- Write analysis to the game as one undo step (chess-core: Game::annotate,
Game::edit_group).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 11:03:23 +03:00
/// `CP_REVIEW=1` starts the whole-game analysis (`=dialog` only opens its dialog).
2026-10-18 01:54:06 +03:00
#[ derive(Default) ]
struct DebugStartup {
open : Option < usize > ,
ply : usize ,
analyze : bool ,
/// `CP_SCREENSHOT=<file.png>`: go fullscreen, wait `CP_SCREENSHOT_DELAY` seconds,
/// save the window contents and quit (README screenshots without desktop overlays).
screenshot : Option < PathBuf > ,
screenshot_delay : f64 ,
screenshot_requested : bool ,
open_settings : bool ,
}
impl DebugStartup {
fn from_env ( ) -> Self {
let num = | k | std ::env ::var ( k ) . ok ( ) . and_then ( | v | v . parse ::< usize > ( ) . ok ( ) ) ;
Self {
open : num ( " CP_OPEN " ) ,
ply : num ( " CP_PLY " ) . unwrap_or ( 0 ) ,
analyze : std ::env ::var ( " CP_ANALYZE " ) . is_ok ( ) ,
screenshot : std ::env ::var_os ( " CP_SCREENSHOT " ) . map ( PathBuf ::from ) ,
screenshot_delay : std ::env ::var ( " CP_SCREENSHOT_DELAY " )
. ok ( )
. and_then ( | v | v . parse ( ) . ok ( ) )
. unwrap_or ( 4.0 ) ,
screenshot_requested : false ,
open_settings : false ,
}
}
}
impl App {
pub fn new ( ctx : & egui ::Context ) -> Self {
let mut settings = Settings ::load ( ) ;
2026-10-19 12:24:40 +03:00
// Before the samples, which are named in this language.
2026-10-18 01:54:06 +03:00
crate ::i18n ::set_lang ( settings . language ) ;
2026-10-19 09:33:37 +03:00
crate ::engine_download ::clean_leftovers ( ) ;
2026-10-20 07:55:59 +03:00
crate ::update ::clean_leftovers ( ) ;
let settings_check_updates = settings . check_updates ;
// "Updated to …" once, when this version runs for the first time after another.
let current = env! ( " CARGO_PKG_VERSION " ) ;
let updated_from = settings
. last_version
. clone ( )
. filter ( | v | crate ::update ::parse_version ( v ) < crate ::update ::parse_version ( current ) ) ;
if settings . last_version . as_deref ( ) ! = Some ( current ) {
settings . last_version = Some ( current . to_owned ( ) ) ;
let _ = settings . save ( ) ;
}
2026-10-19 12:21:09 +03:00
let engines_found = crate ::engines ::first_run_setup ( & mut settings ) ;
if crate ::examples ::first_run_setup ( & mut settings ) | | engines_found {
2026-10-18 11:53:30 +03:00
let _ = settings . save ( ) ;
}
2026-10-18 01:54:06 +03:00
// PGN files passed on the command line (e.g. "Open with…") join the library, first one selected.
let args : Vec < PathBuf > = std ::env ::args_os ( )
. skip ( 1 )
. map ( PathBuf ::from )
. filter ( | p | p . is_file ( ) )
. collect ( ) ;
for p in args . iter ( ) . rev ( ) {
let p = p . canonicalize ( ) . unwrap_or_else ( | _ | p . clone ( ) ) ;
settings . touch_database ( & p ) ;
}
if ! args . is_empty ( ) {
let _ = settings . save ( ) ;
}
let ( tx , rx ) = channel ( ) ;
let mut library = Library ::new ( ) ;
if let Some ( first ) = settings . databases . first ( ) . filter ( | p | p . exists ( ) ) {
library . select_db ( first , ctx ) ;
}
2026-10-20 07:55:59 +03:00
let mut app = Self {
2026-10-18 01:54:06 +03:00
settings ,
settings_dirty : false ,
sound : SoundPlayer ::new ( ) ,
library ,
game : None ,
analysis : Analysis ::new ( ) ,
Game analysis: whole-game engine review with graph, report and notation marks
- Separate background engine job over the main line (go nodes N), live engine
paused meanwhile; header button with progress/cancel, Shift+A.
- Lichess-method metrics: win %, move/game accuracy, inaccuracy/mistake/blunder,
ACPL, accuracy per game phase.
- Evaluation graph under the board, Report tab, engine badges and better lines
in the notation (preview on click), launch dialog with quality/threads/memory
and time estimate.
- Results cached in ~/.cache/chesspenguin/analysis; [%eval] read from PGN.
- Write analysis to the game as one undo step (chess-core: Game::annotate,
Game::edit_group).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 11:03:23 +03:00
live_paused : false ,
2026-10-18 01:54:06 +03:00
settings_ui : SettingsWindow ::new ( ) ,
toast : None ,
tx ,
rx ,
confirm_close : None ,
close_after_save : None ,
ChessPenguin 0.5.0: find games by position on a board
The "Board" pane next to the game list (Ctrl+B): moves played on it filter
the list to the games that reach the position by any move order, together
with the filters; the moves played next are listed with games, a results
bar, average rating and last year. Games reached by another order are
marked and "only this order" narrows the list; strongest games first.
A game opens at the found position; "Find in database" (Ctrl+Shift+F)
searches the position of the game on screen.
Large ChessBase databases: a fast main-line walk (moves built from the
board), pruning by material and pawns, the .cbb search booster when valid;
Mega Database: 0.4 s for the starting position, under 0.1 s deeper.
PGN databases and books: positions of all games, variations included,
kept in memory once built.
Also: × to clear filter and search fields; "Open as a new game" opens at
the last move; the "Moves" column is now "Length".
Docs: decision 13, design (docs/design/board-search.md), research
(position-search, position-index), dev notes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 08:42:08 +03:00
find_after_close : None ,
2026-10-19 23:20:37 +03:00
previous_games : Vec ::new ( ) ,
reference_tab : false ,
2026-10-20 07:55:59 +03:00
update_check : if settings_check_updates {
crate ::update ::Check ::start ( ctx )
} else {
crate ::update ::Check ::idle ( )
} ,
update_kind : crate ::update ::detect ( ) ,
update_ui : crate ::update_ui ::UpdateDialog ::from_env ( ) ,
restart_to : None ,
2026-10-19 23:20:37 +03:00
reference : Default ::default ( ) ,
2026-10-19 09:33:37 +03:00
engine_offer : crate ::engine_offer ::EngineOffer ::new ( ) ,
offer_hook_done : false ,
2026-10-19 21:10:22 +03:00
online_probe : crate ::online ::Probe ::start ( ctx ) ,
online : crate ::online_ui ::OnlineDialog ::from_env ( ) ,
2026-10-18 01:54:06 +03:00
allow_quit : false ,
applied_dark : None ,
debug : DebugStartup ::from_env ( ) ,
}
2026-10-20 07:55:59 +03:00
. with_debug_settings ( ) ;
if let Some ( from ) = updated_from {
let msg = trf (
" ChessPenguin is updated: {from} → {to} " ,
& [ ( " from " , & from ) , ( " to " , & current ) ] ,
) ;
app . toast ( ctx , msg ) ;
}
app
2026-10-18 01:54:06 +03:00
}
fn with_debug_settings ( mut self ) -> Self {
if let Ok ( tab ) = std ::env ::var ( " CP_SETTINGS " ) {
// Opened after a short delay (see debug_screenshot) so a fullscreen switch is done first.
self . debug . open_settings = true ;
Settings → About: version, website, release notes, sources, support, donations
The About tab shows the version and links to the website (in the interface language),
the release notes of this version, the source code and the support contacts; "Support
the project" opens chess.vrubel.xyz/donate, which the website server sends on to
DONATE_URL (the support section while it is not set), so the donation service can change
without a new release. Links open in the browser (eframe "links": webbrowser, MIT OR
Apache-2.0).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 15:11:25 +03:00
match tab . as_str ( ) {
" board " = > self . settings_ui . tab = Tab ::Board ,
" about " = > self . settings_ui . tab = Tab ::About ,
_ = > { }
2026-10-18 01:54:06 +03:00
}
}
self
}
fn debug_screenshot ( & mut self , ctx : & egui ::Context ) {
if self . debug . open_settings & & ctx . input ( | i | i . time ) > 1.5 {
self . debug . open_settings = false ;
self . settings_ui . open = true ;
}
let Some ( path ) = self . debug . screenshot . clone ( ) else {
return ;
} ;
let now = ctx . input ( | i | i . time ) ;
if ! self . debug . screenshot_requested {
if ctx . cumulative_frame_nr ( ) = = 1 {
ctx . send_viewport_cmd ( egui ::ViewportCommand ::Fullscreen ( true ) ) ;
}
if now > = self . debug . screenshot_delay {
self . debug . screenshot_requested = true ;
ctx . send_viewport_cmd ( egui ::ViewportCommand ::Screenshot ( Default ::default ( ) ) ) ;
}
ctx . request_repaint_after ( std ::time ::Duration ::from_millis ( 100 ) ) ;
return ;
}
let shot = ctx . input ( | i | {
i . raw . events . iter ( ) . find_map ( | e | match e {
egui ::Event ::Screenshot { image , .. } = > Some ( image . clone ( ) ) ,
_ = > None ,
} )
} ) ;
if let Some ( img ) = shot {
let [ w , h ] = img . size ;
let bytes : Vec < u8 > = img . pixels . iter ( ) . flat_map ( | c | c . to_array ( ) ) . collect ( ) ;
if let Err ( e ) =
image ::save_buffer ( & path , & bytes , w as u32 , h as u32 , image ::ColorType ::Rgba8 )
{
eprintln! ( " screenshot: {e} " ) ;
}
self . allow_quit = true ;
ctx . send_viewport_cmd ( egui ::ViewportCommand ::Close ) ;
}
}
fn debug_step ( & mut self , ctx : & egui ::Context ) {
let Some ( index ) = self . debug . open else { return } ;
let Some ( path ) = self . settings . databases . first ( ) . cloned ( ) else {
return ;
} ;
if self . library . database ( & path ) . is_none ( ) {
return ;
}
self . debug . open = None ;
ChessPenguin 0.5.0: find games by position on a board
The "Board" pane next to the game list (Ctrl+B): moves played on it filter
the list to the games that reach the position by any move order, together
with the filters; the moves played next are listed with games, a results
bar, average rating and last year. Games reached by another order are
marked and "only this order" narrows the list; strongest games first.
A game opens at the found position; "Find in database" (Ctrl+Shift+F)
searches the position of the game on screen.
Large ChessBase databases: a fast main-line walk (moves built from the
board), pruning by material and pawns, the .cbb search booster when valid;
Mega Database: 0.4 s for the starting position, under 0.1 s deeper.
PGN databases and books: positions of all games, variations included,
kept in memory once built.
Also: × to clear filter and search fields; "Open as a new game" opens at
the last move; the "Moves" column is now "Length".
Docs: decision 13, design (docs/design/board-search.md), research
(position-search, position-index), dev notes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 08:42:08 +03:00
self . open_game ( ctx , path , index , None ) ;
2026-10-18 01:54:06 +03:00
if let Some ( g ) = & mut self . game {
g . debug_advance ( self . debug . ply ) ;
if std ::env ::var ( " CP_ENGINE_PANEL " ) . is_ok ( ) {
g . debug_open_engine_settings ( ) ;
}
2026-10-18 12:10:40 +03:00
if std ::env ::var ( " CP_REPORT " ) . is_ok ( ) {
g . debug_show_report ( ) ;
}
2026-10-18 11:45:13 +03:00
if let Some ( k ) = std ::env ::var ( " CP_BEST " ) . ok ( ) . and_then ( | v | v . parse ( ) . ok ( ) ) {
g . debug_best_line ( k ) ;
}
Game analysis: whole-game engine review with graph, report and notation marks
- Separate background engine job over the main line (go nodes N), live engine
paused meanwhile; header button with progress/cancel, Shift+A.
- Lichess-method metrics: win %, move/game accuracy, inaccuracy/mistake/blunder,
ACPL, accuracy per game phase.
- Evaluation graph under the board, Report tab, engine badges and better lines
in the notation (preview on click), launch dialog with quality/threads/memory
and time estimate.
- Results cached in ~/.cache/chesspenguin/analysis; [%eval] read from PGN.
- Write analysis to the game as one undo step (chess-core: Game::annotate,
Game::edit_group).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 11:03:23 +03:00
// CP_REVIEW=dialog opens the game-analysis dialog, any other value starts the analysis.
if let Ok ( v ) = std ::env ::var ( " CP_REVIEW " ) {
g . debug_review ( & self . settings , ctx , v ! = " dialog " ) ;
}
2026-10-18 01:54:06 +03:00
}
if self . debug . analyze {
self . toggle_engine ( ctx ) ;
}
}
fn toast ( & mut self , ctx : & egui ::Context , msg : impl Into < String > ) {
self . toast = Some ( ( msg . into ( ) , ctx . input ( | i | i . time ) + 3.5 ) ) ;
}
fn play ( & self , s : Sound ) {
if self . settings . sound_enabled {
self . sound
. play ( self . settings . sound_set , s , self . settings . volume ) ;
}
}
fn spawn_dialog (
& self ,
ctx : & egui ::Context ,
f : impl FnOnce ( ) -> Option < Async > + Send + 'static ,
) {
let tx = self . tx . clone ( ) ;
let ctx = ctx . clone ( ) ;
let _ = std ::thread ::Builder ::new ( )
. name ( " dialog " . into ( ) )
. spawn ( move | | {
if let Some ( r ) = f ( ) {
let _ = tx . send ( r ) ;
}
ctx . request_repaint ( ) ;
} ) ;
}
fn pick_databases ( & self , ctx : & egui ::Context ) {
self . spawn_dialog ( ctx , | | {
rfd ::FileDialog ::new ( )
Open ChessBase databases (.cbh, .cbv) and zip/7z archives
New crate chess-import converts non-PGN sources to PGN in memory and returns
a PgnDatabase, so the library, notation and analysis work unchanged:
- .cbv archives (container rewritten after uncbv), .cbh with companion files
(layout after the public Morphy format description): games, variations,
comments with per-database code page detection, NAGs, squares/arrows, set-up
positions, time spent, book chapters (guiding texts) as move-less records;
- ChessBase figurines become piece letters, diagram/object markers handled,
Western names in Cyrillic databases read correctly;
- .zip/.7z with PGN (own encoding each), .cbh and .cbv; duplicates skipped;
- .cbz/encrypted archives and 2CBH rejected with clear messages.
Library: '+ Add database', format shown, import notes; comments keep
paragraph breaks. Tests on the user's sample books (skipped when absent).
Also: AUR PKGBUILD for 0.2.0 with the bundled Stockfish.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 13:18:27 +03:00
. set_title ( tr ( " Open database " ) )
. add_filter (
tr ( " Chess databases " ) ,
& [
" pgn " , " PGN " , " cbh " , " CBH " , " cbv " , " CBV " , " zip " , " ZIP " , " 7z " , " 7Z " ,
] ,
)
2026-10-18 01:54:06 +03:00
. add_filter ( " PGN " , & [ " pgn " , " PGN " ] )
. add_filter ( tr ( " All files " ) , & [ " * " ] )
. pick_files ( )
. map ( Async ::DatabasesPicked )
} ) ;
}
fn pick_save ( & self , ctx : & egui ::Context ) {
let Some ( g ) = & self . game else { return } ;
let name = sanitize ( & g . title ( ) ) + " .pgn " ;
self . spawn_dialog ( ctx , move | | {
rfd ::FileDialog ::new ( )
. set_title ( tr ( " Save game " ) )
. set_file_name ( name )
. add_filter ( " PGN " , & [ " pgn " ] )
. save_file ( )
. map_or ( Some ( Async ::SaveCancelled ) , | p | Some ( Async ::SavePicked ( p ) ) )
} ) ;
}
fn pick_engine ( & mut self , ctx : & egui ::Context ) {
self . settings_ui . checking_engine = true ;
let tx = self . tx . clone ( ) ;
let ctx2 = ctx . clone ( ) ;
let _ = std ::thread ::Builder ::new ( )
. name ( " engine-check " . into ( ) )
. spawn ( move | | {
let Some ( path ) = rfd ::FileDialog ::new ( )
. set_title ( tr ( " Choose the UCI engine executable " ) )
. pick_file ( )
else {
let _ = tx . send ( Async ::EngineChecked ( Err ( String ::new ( ) ) ) ) ;
ctx2 . request_repaint ( ) ;
return ;
} ;
let res = uci ::Engine ::spawn ( & path , std ::iter ::empty ::< & str > ( ) , Box ::new ( | | { } ) )
. map ( | h | {
let name = h . info ( ) . name . clone ( ) . unwrap_or_else ( | | {
path . file_name ( )
. map ( | s | s . to_string_lossy ( ) . into_owned ( ) )
. unwrap_or_default ( )
} ) ;
h . quit ( ) ;
( path . clone ( ) , name )
} )
. map_err ( | e | format! ( " {} : {e} " , path . display ( ) ) ) ;
let _ = tx . send ( Async ::EngineChecked ( res ) ) ;
ctx2 . request_repaint ( ) ;
} ) ;
}
fn handle_async ( & mut self , ctx : & egui ::Context ) {
while let Ok ( msg ) = self . rx . try_recv ( ) {
match msg {
Async ::DatabasesPicked ( paths ) = > {
for p in paths . iter ( ) . rev ( ) {
self . settings . touch_database ( p ) ;
}
if let Some ( first ) = paths . first ( ) {
self . library . select_db ( first , ctx ) ;
}
self . settings_dirty = true ;
}
Async ::SavePicked ( path ) = > self . save_game ( ctx , path ) ,
2026-10-20 07:55:59 +03:00
Async ::SaveCancelled = > {
self . close_after_save = None ;
self . restart_to = None ;
}
2026-10-18 01:54:06 +03:00
Async ::EngineChecked ( res ) = > {
self . settings_ui . checking_engine = false ;
match res {
Ok ( ( path , name ) ) = > {
self . settings
. engines
. push ( EngineConfig ::new ( name . clone ( ) , path ) ) ;
if self . settings . active_engine . is_none ( ) {
self . settings . active_engine = Some ( self . settings . engines . len ( ) - 1 ) ;
}
self . settings_dirty = true ;
self . toast ( ctx , trf ( " Engine “{name}” added " , & [ ( " name " , & name ) ] ) ) ;
}
Err ( e ) if e . is_empty ( ) = > { }
Err ( e ) = > self . toast (
ctx ,
trf ( " Not a UCI engine or it failed to start: {e} " , & [ ( " e " , & e ) ] ) ,
) ,
}
}
}
}
}
fn save_game ( & mut self , ctx : & egui ::Context , path : PathBuf ) {
// Never replace a source database with a single game (explicit invariant).
let same = | a : & std ::path ::Path | match ( a . canonicalize ( ) , path . canonicalize ( ) ) {
( Ok ( x ) , Ok ( y ) ) = > x = = y ,
_ = > a = = path ,
} ;
let is_source = self
. game
. as_ref ( )
. and_then ( | g | g . source . as_ref ( ) )
. is_some_and ( | s | same ( & s . path ) ) ;
if is_source | | self . settings . databases . iter ( ) . any ( | d | same ( d ) ) {
self . close_after_save = None ;
self . toast (
ctx ,
tr ( " An open database cannot be overwritten with a single game — choose another file " ) ,
) ;
return ;
}
let Some ( g ) = & mut self . game else { return } ;
let pgn = g . game . to_pgn ( ) ;
match write_atomic ( & path , pgn . as_bytes ( ) ) {
Ok ( ( ) ) = > {
g . game . mark_saved ( ) ;
self . toast ( ctx , trf ( " Saved: {path} " , & [ ( " path " , & path . display ( ) ) ] ) ) ;
if let Some ( intent ) = self . close_after_save . take ( ) {
self . finish_close ( ctx , intent ) ;
}
}
Err ( e ) = > {
self . close_after_save = None ;
self . toast ( ctx , trf ( " Save failed: {e} " , & [ ( " e " , & e ) ] ) ) ;
}
}
}
ChessPenguin 0.5.0: find games by position on a board
The "Board" pane next to the game list (Ctrl+B): moves played on it filter
the list to the games that reach the position by any move order, together
with the filters; the moves played next are listed with games, a results
bar, average rating and last year. Games reached by another order are
marked and "only this order" narrows the list; strongest games first.
A game opens at the found position; "Find in database" (Ctrl+Shift+F)
searches the position of the game on screen.
Large ChessBase databases: a fast main-line walk (moves built from the
board), pruning by material and pawns, the .cbb search booster when valid;
Mega Database: 0.4 s for the starting position, under 0.1 s deeper.
PGN databases and books: positions of all games, variations included,
kept in memory once built.
Also: × to clear filter and search fields; "Open as a new game" opens at
the last move; the "Moves" column is now "Length".
Docs: decision 13, design (docs/design/board-search.md), research
(position-search, position-index), dev notes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 08:42:08 +03:00
fn open_game (
& mut self ,
ctx : & egui ::Context ,
path : PathBuf ,
index : usize ,
at : Option < shakmaty ::Chess > ,
) {
2026-10-18 01:54:06 +03:00
let Some ( db ) = self . library . database ( & path ) else {
return ;
} ;
match db . load_game ( index ) {
Some ( parsed ) = > {
self . game = Some ( GameView ::new (
parsed . game ,
Some ( GameSource {
path : path . clone ( ) ,
index ,
} ) ,
parsed . diagnostics ,
) ) ;
2026-10-19 23:20:37 +03:00
if self . reference_tab
& & let Some ( g ) = self . game . as_mut ( )
{
g . show_reference_tab ( ) ;
}
ChessPenguin 0.5.0: find games by position on a board
The "Board" pane next to the game list (Ctrl+B): moves played on it filter
the list to the games that reach the position by any move order, together
with the filters; the moves played next are listed with games, a results
bar, average rating and last year. Games reached by another order are
marked and "only this order" narrows the list; strongest games first.
A game opens at the found position; "Find in database" (Ctrl+Shift+F)
searches the position of the game on screen.
Large ChessBase databases: a fast main-line walk (moves built from the
board), pruning by material and pawns, the .cbb search booster when valid;
Mega Database: 0.4 s for the starting position, under 0.1 s deeper.
PGN databases and books: positions of all games, variations included,
kept in memory once built.
Also: × to clear filter and search fields; "Open as a new game" opens at
the last move; the "Moves" column is now "Length".
Docs: decision 13, design (docs/design/board-search.md), research
(position-search, position-index), dev notes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 08:42:08 +03:00
if let ( Some ( target ) , Some ( g ) ) = ( at , self . game . as_mut ( ) ) {
g . goto_position ( & target ) ;
}
2026-10-18 14:00:19 +03:00
self . library . mark_viewed ( & path , index ) ;
2026-10-18 01:54:06 +03:00
self . settings . touch_database ( & path ) ;
self . settings_dirty = true ;
}
None = > self . toast ( ctx , tr ( " Could not read the game " ) ) ,
}
}
fn request_close ( & mut self , ctx : & egui ::Context , intent : CloseIntent ) {
if self . game . as_ref ( ) . is_some_and ( | g | g . game . is_modified ( ) ) {
self . confirm_close = Some ( intent ) ;
} else {
self . finish_close ( ctx , intent ) ;
}
}
fn finish_close ( & mut self , ctx : & egui ::Context , intent : CloseIntent ) {
self . analysis . stop ( ) ;
Game analysis: whole-game engine review with graph, report and notation marks
- Separate background engine job over the main line (go nodes N), live engine
paused meanwhile; header button with progress/cancel, Shift+A.
- Lichess-method metrics: win %, move/game accuracy, inaccuracy/mistake/blunder,
ACPL, accuracy per game phase.
- Evaluation graph under the board, Report tab, engine badges and better lines
in the notation (preview on click), launch dialog with quality/threads/memory
and time estimate.
- Results cached in ~/.cache/chesspenguin/analysis; [%eval] read from PGN.
- Write analysis to the game as one undo step (chess-core: Game::annotate,
Game::edit_group).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 11:03:23 +03:00
self . live_paused = false ;
2026-10-19 23:20:37 +03:00
let closed = self . game . take ( ) ;
if intent = = CloseIntent ::ReturnToPrevious {
self . game = self . previous_games . pop ( ) ;
return ;
}
2026-10-19 23:48:02 +03:00
// Games left for others (reference tab) with unsaved edits are asked about one
// by one (each shown with the question) before the library, the search board or
// quitting.
2026-10-19 23:20:37 +03:00
if let Some ( k ) = self
. previous_games
. iter ( )
. rposition ( | g | g . game . is_modified ( ) )
{
self . previous_games . truncate ( k + 1 ) ;
self . game = self . previous_games . pop ( ) ;
2026-10-19 23:48:02 +03:00
// Ask about it right away; its answer continues to where the user was going
// (the library, the search board, quitting).
self . confirm_close = Some ( intent ) ;
2026-10-19 23:20:37 +03:00
return ;
}
self . previous_games . clear ( ) ;
if let Some ( g ) = closed
2026-10-18 01:54:06 +03:00
& & let Some ( src ) = g . source
{
self . library . reveal ( & src . path , src . index ) ;
}
ChessPenguin 0.5.0: find games by position on a board
The "Board" pane next to the game list (Ctrl+B): moves played on it filter
the list to the games that reach the position by any move order, together
with the filters; the moves played next are listed with games, a results
bar, average rating and last year. Games reached by another order are
marked and "only this order" narrows the list; strongest games first.
A game opens at the found position; "Find in database" (Ctrl+Shift+F)
searches the position of the game on screen.
Large ChessBase databases: a fast main-line walk (moves built from the
board), pruning by material and pawns, the .cbb search booster when valid;
Mega Database: 0.4 s for the starting position, under 0.1 s deeper.
PGN databases and books: positions of all games, variations included,
kept in memory once built.
Also: × to clear filter and search fields; "Open as a new game" opens at
the last move; the "Moves" column is now "Length".
Docs: decision 13, design (docs/design/board-search.md), research
(position-search, position-index), dev notes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 08:42:08 +03:00
if let Some ( ( path , start , line , flipped ) ) = self . find_after_close . take ( ) {
self . library . show_position ( & path , start , line , flipped , ctx ) ;
}
2026-10-19 23:20:37 +03:00
2026-10-18 01:54:06 +03:00
if intent = = CloseIntent ::QuitApp {
2026-10-20 07:55:59 +03:00
// A restart into the new version: start it as this one goes.
if let Some ( exe ) = self . restart_to . take ( ) {
// The settings first (the new process reads them), and none of the dev
// hooks of this one (they would start the update again).
let _ = self . settings . save ( ) ;
let mut cmd = std ::process ::Command ::new ( exe ) ;
for ( k , _ ) in std ::env ::vars_os ( ) {
if k . to_string_lossy ( ) . starts_with ( " CP_ " ) {
cmd . env_remove ( k ) ;
}
}
let _ = cmd . spawn ( ) ;
}
2026-10-18 01:54:06 +03:00
self . allow_quit = true ;
ctx . send_viewport_cmd ( egui ::ViewportCommand ::Close ) ;
}
}
ChessPenguin 0.5.0: find games by position on a board
The "Board" pane next to the game list (Ctrl+B): moves played on it filter
the list to the games that reach the position by any move order, together
with the filters; the moves played next are listed with games, a results
bar, average rating and last year. Games reached by another order are
marked and "only this order" narrows the list; strongest games first.
A game opens at the found position; "Find in database" (Ctrl+Shift+F)
searches the position of the game on screen.
Large ChessBase databases: a fast main-line walk (moves built from the
board), pruning by material and pawns, the .cbb search booster when valid;
Mega Database: 0.4 s for the starting position, under 0.1 s deeper.
PGN databases and books: positions of all games, variations included,
kept in memory once built.
Also: × to clear filter and search fields; "Open as a new game" opens at
the last move; the "Moves" column is now "Length".
Docs: decision 13, design (docs/design/board-search.md), research
(position-search, position-index), dev notes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 08:42:08 +03:00
/// Leaves the game for the library's search board with the game's current position.
fn find_position ( & mut self , ctx : & egui ::Context , db : Option < PathBuf > ) {
let Some ( ( start , line , flipped ) ) = self . game . as_ref ( ) . and_then ( | g | g . search_line ( ) ) else {
return ;
} ;
let path = db
. or_else ( | | self . library . selected_db . clone ( ) )
. or_else ( | | self . settings . databases . first ( ) . cloned ( ) ) ;
let Some ( path ) = path else {
self . toast ( ctx , tr ( " Add a database first " ) ) ;
return ;
} ;
self . find_after_close = Some ( ( path , start , line , flipped ) ) ;
self . request_close ( ctx , CloseIntent ::BackToLibrary ) ;
}
2026-10-18 01:54:06 +03:00
fn toggle_engine ( & mut self , ctx : & egui ::Context ) {
Game analysis: whole-game engine review with graph, report and notation marks
- Separate background engine job over the main line (go nodes N), live engine
paused meanwhile; header button with progress/cancel, Shift+A.
- Lichess-method metrics: win %, move/game accuracy, inaccuracy/mistake/blunder,
ACPL, accuracy per game phase.
- Evaluation graph under the board, Report tab, engine badges and better lines
in the notation (preview on click), launch dialog with quality/threads/memory
and time estimate.
- Results cached in ~/.cache/chesspenguin/analysis; [%eval] read from PGN.
- Write analysis to the game as one undo step (chess-core: Game::annotate,
Game::edit_group).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 11:03:23 +03:00
self . live_paused = false ;
2026-10-18 01:54:06 +03:00
if self . analysis . is_on ( ) {
self . analysis . stop ( ) ;
return ;
}
match self . settings . active_engine ( ) . cloned ( ) {
2026-10-19 09:33:37 +03:00
Some ( cfg ) if crate ::engine_offer ::EngineOffer ::engine_ready ( & self . settings ) = > {
self . analysis . start ( & cfg , ctx )
}
_ = > self
. engine_offer
. ask ( crate ::engine_offer ::Pending ::LiveAnalysis , & self . settings ) ,
}
}
/// Makes ChessPenguin's Stockfish (just downloaded) or a found engine the active
/// one, then runs what the user was doing.
fn engine_arrived (
& mut self ,
ctx : & egui ::Context ,
cfg : crate ::settings ::EngineConfig ,
pending : Option < crate ::engine_offer ::Pending > ,
) {
let known = self . settings . engines . iter ( ) . position ( | e | {
( cfg . builtin & & e . builtin ) | | ( ! cfg . builtin & & ! e . builtin & & e . path = = cfg . path )
} ) ;
let index = known . unwrap_or_else ( | | {
self . settings . engines . push ( cfg ) ;
self . settings . engines . len ( ) - 1
} ) ;
self . settings . active_engine = Some ( index ) ;
self . settings_dirty = true ;
match pending {
Some ( crate ::engine_offer ::Pending ::LiveAnalysis ) = > {
if ! self . analysis . is_on ( ) {
self . toggle_engine ( ctx ) ;
}
}
Some ( crate ::engine_offer ::Pending ::GameAnalysis ) = > {
let actions = match self . game . as_mut ( ) {
Some ( g ) = > g . start_review_now ( & self . settings , ctx ) ,
None = > Vec ::new ( ) ,
} ;
for a in actions {
match a {
GameAction ::PauseLive if self . analysis . is_on ( ) = > {
self . analysis . stop ( ) ;
self . live_paused = true ;
}
GameAction ::Toast ( m ) = > self . toast ( ctx , m ) ,
_ = > { }
}
}
}
None = > { }
}
}
fn offer_events ( & mut self , ctx : & egui ::Context ) {
for e in self . engine_offer . show ( ctx ) {
match e {
crate ::engine_offer ::OfferEvent ::Installed ( pending ) = > {
self . engine_arrived ( ctx , crate ::engines ::builtin_config ( ) , pending )
}
crate ::engine_offer ::OfferEvent ::UseSystem ( path , pending ) = > {
let cfg = crate ::settings ::EngineConfig ::new ( " Stockfish " . into ( ) , path ) ;
self . engine_arrived ( ctx , cfg , pending )
}
crate ::engine_offer ::OfferEvent ::ChooseFile = > self . pick_engine ( ctx ) ,
crate ::engine_offer ::OfferEvent ::Toast ( m ) = > self . toast ( ctx , m ) ,
2026-10-18 01:54:06 +03:00
}
}
}
fn paste_pgn ( & mut self , ctx : & egui ::Context ) {
let text = arboard ::Clipboard ::new ( ) . and_then ( | mut c | c . get_text ( ) ) ;
match text {
ChessPenguin 0.5.0: find games by position on a board
The "Board" pane next to the game list (Ctrl+B): moves played on it filter
the list to the games that reach the position by any move order, together
with the filters; the moves played next are listed with games, a results
bar, average rating and last year. Games reached by another order are
marked and "only this order" narrows the list; strongest games first.
A game opens at the found position; "Find in database" (Ctrl+Shift+F)
searches the position of the game on screen.
Large ChessBase databases: a fast main-line walk (moves built from the
board), pruning by material and pawns, the .cbb search booster when valid;
Mega Database: 0.4 s for the starting position, under 0.1 s deeper.
PGN databases and books: positions of all games, variations included,
kept in memory once built.
Also: × to clear filter and search fields; "Open as a new game" opens at
the last move; the "Moves" column is now "Length".
Docs: decision 13, design (docs/design/board-search.md), research
(position-search, position-index), dev notes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 08:42:08 +03:00
Ok ( t ) if ! t . trim ( ) . is_empty ( ) = > self . open_pgn_text ( ctx , t , false ) ,
2026-10-18 01:54:06 +03:00
_ = > self . toast ( ctx , tr ( " The clipboard is empty or unavailable " ) ) ,
}
}
ChessPenguin 0.5.0: find games by position on a board
The "Board" pane next to the game list (Ctrl+B): moves played on it filter
the list to the games that reach the position by any move order, together
with the filters; the moves played next are listed with games, a results
bar, average rating and last year. Games reached by another order are
marked and "only this order" narrows the list; strongest games first.
A game opens at the found position; "Find in database" (Ctrl+Shift+F)
searches the position of the game on screen.
Large ChessBase databases: a fast main-line walk (moves built from the
board), pruning by material and pawns, the .cbb search booster when valid;
Mega Database: 0.4 s for the starting position, under 0.1 s deeper.
PGN databases and books: positions of all games, variations included,
kept in memory once built.
Also: × to clear filter and search fields; "Open as a new game" opens at
the last move; the "Moves" column is now "Length".
Docs: decision 13, design (docs/design/board-search.md), research
(position-search, position-index), dev notes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 08:42:08 +03:00
/// Opens PGN text (the first game of it) as a new game.
/// `at_end`: show the position after the last main-line move (a line from the
/// search board continues where the user stopped).
fn open_pgn_text ( & mut self , ctx : & egui ::Context , t : String , at_end : bool ) {
// Split into records first: several games must not be glued into one.
let never = AtomicBool ::new ( false ) ;
let db = PgnDatabase ::from_bytes (
t . into_bytes ( ) ,
Some ( TextEncoding ::Utf8 ) ,
& mut | _ | { } ,
& never ,
) ;
let ( parsed , count ) = match db . as_ref ( ) . ok ( ) . filter ( | db | ! db . is_empty ( ) ) {
Some ( db ) = > ( db . load_game ( 0 ) , db . len ( ) ) ,
None = > ( None , 0 ) ,
} ;
let Some ( parsed ) = parsed else {
self . toast ( ctx , tr ( " The clipboard contains no recognizable PGN game " ) ) ;
return ;
} ;
if parsed . game . main_child ( parsed . game . root ( ) ) . is_none ( )
& & parsed . game . tags ( ) . len ( ) < = 7
& & ! parsed . diagnostics . is_empty ( )
{
self . toast ( ctx , tr ( " The clipboard contains no recognizable PGN game " ) ) ;
return ;
}
if count > 1 {
self . toast ( ctx , trf ( " The clipboard has {count} games — the first one is open. To work with all of them, save them to a .pgn file and add it as a database " , & [ ( " count " , & count ) ] ) ) ;
}
let mut view = GameView ::new ( parsed . game , None , parsed . diagnostics ) ;
if at_end {
view . goto_main_line_end ( ) ;
}
self . game = Some ( view ) ;
}
2026-10-18 01:54:06 +03:00
fn confirm_dialog ( & mut self , ctx : & egui ::Context ) {
let Some ( intent ) = self . confirm_close else {
return ;
} ;
let mut choice = None ;
egui ::Modal ::new ( egui ::Id ::new ( " confirm_close " ) ) . show ( ctx , | ui | {
ui . set_width ( 380.0 ) ;
ui . heading ( tr ( " Game modified " ) ) ;
2026-10-19 23:20:37 +03:00
if let Some ( g ) = & self . game {
ui . label ( RichText ::new ( g . title ( ) ) . strong ( ) ) ;
}
2026-10-18 01:54:06 +03:00
ui . label ( tr (
" The source PGN is not changed. Save the changes to a separate file? " ,
) ) ;
ui . add_space ( 8.0 ) ;
ui . horizontal ( | ui | {
if ui . button ( tr ( " Save as… " ) ) . clicked ( ) {
choice = Some ( 0 ) ;
}
if ui . button ( tr ( " Don't save " ) ) . clicked ( ) {
choice = Some ( 1 ) ;
}
if ui . button ( tr ( " Cancel " ) ) . clicked ( ) | | ui . input ( | i | i . key_pressed ( Key ::Escape ) ) {
choice = Some ( 2 ) ;
}
} ) ;
} ) ;
match choice {
Some ( 0 ) = > {
self . confirm_close = None ;
self . close_after_save = Some ( intent ) ;
self . pick_save ( ctx ) ;
}
Some ( 1 ) = > {
self . confirm_close = None ;
self . finish_close ( ctx , intent ) ;
}
ChessPenguin 0.5.0: find games by position on a board
The "Board" pane next to the game list (Ctrl+B): moves played on it filter
the list to the games that reach the position by any move order, together
with the filters; the moves played next are listed with games, a results
bar, average rating and last year. Games reached by another order are
marked and "only this order" narrows the list; strongest games first.
A game opens at the found position; "Find in database" (Ctrl+Shift+F)
searches the position of the game on screen.
Large ChessBase databases: a fast main-line walk (moves built from the
board), pruning by material and pawns, the .cbb search booster when valid;
Mega Database: 0.4 s for the starting position, under 0.1 s deeper.
PGN databases and books: positions of all games, variations included,
kept in memory once built.
Also: × to clear filter and search fields; "Open as a new game" opens at
the last move; the "Moves" column is now "Length".
Docs: decision 13, design (docs/design/board-search.md), research
(position-search, position-index), dev notes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 08:42:08 +03:00
Some ( 2 ) = > {
self . confirm_close = None ;
self . find_after_close = None ;
2026-10-20 07:55:59 +03:00
// A restart asked for waits for no later quit.
self . restart_to = None ;
ChessPenguin 0.5.0: find games by position on a board
The "Board" pane next to the game list (Ctrl+B): moves played on it filter
the list to the games that reach the position by any move order, together
with the filters; the moves played next are listed with games, a results
bar, average rating and last year. Games reached by another order are
marked and "only this order" narrows the list; strongest games first.
A game opens at the found position; "Find in database" (Ctrl+Shift+F)
searches the position of the game on screen.
Large ChessBase databases: a fast main-line walk (moves built from the
board), pruning by material and pawns, the .cbb search booster when valid;
Mega Database: 0.4 s for the starting position, under 0.1 s deeper.
PGN databases and books: positions of all games, variations included,
kept in memory once built.
Also: × to clear filter and search fields; "Open as a new game" opens at
the last move; the "Moves" column is now "Length".
Docs: decision 13, design (docs/design/board-search.md), research
(position-search, position-index), dev notes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 08:42:08 +03:00
}
2026-10-18 01:54:06 +03:00
_ = > { }
}
}
}
impl eframe ::App for App {
fn ui ( & mut self , ui : & mut egui ::Ui , _frame : & mut eframe ::Frame ) {
let ctx = ui . ctx ( ) . clone ( ) ;
if self . applied_dark ! = Some ( self . settings . dark_mode ) {
ctx . set_visuals ( if self . settings . dark_mode {
egui ::Visuals ::dark ( )
} else {
egui ::Visuals ::light ( )
} ) ;
self . applied_dark = Some ( self . settings . dark_mode ) ;
}
2026-10-19 23:20:37 +03:00
// 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 ( ) )
& & ! self . allow_quit
& & ! self . game . as_ref ( ) . is_some_and ( | g | g . game . is_modified ( ) )
& & let Some ( k ) = self
. previous_games
. iter ( )
. rposition ( | g | g . game . is_modified ( ) )
{
self . previous_games . truncate ( k + 1 ) ;
self . game = self . previous_games . pop ( ) ;
}
2026-10-18 01:54:06 +03:00
if ctx . input ( | i | i . viewport ( ) . close_requested ( ) )
& & ! self . allow_quit
& & self . game . as_ref ( ) . is_some_and ( | g | g . game . is_modified ( ) )
{
ctx . send_viewport_cmd ( egui ::ViewportCommand ::CancelClose ) ;
self . confirm_close = Some ( CloseIntent ::QuitApp ) ;
}
self . handle_async ( & ctx ) ;
2026-10-19 09:33:37 +03:00
// Dev hook (screenshots): the engine offer as on pressing L without an engine.
if std ::env ::var ( " CP_ENGINE_OFFER " ) . is_ok ( ) & & ! self . offer_hook_done {
self . offer_hook_done = true ;
self . engine_offer
. ask ( crate ::engine_offer ::Pending ::LiveAnalysis , & self . settings ) ;
if std ::env ::var ( " CP_ENGINE_OFFER " ) . as_deref ( ) = = Ok ( " download " ) {
self . engine_offer . start ( & ctx ) ;
}
}
self . offer_events ( & ctx ) ;
2026-10-18 01:54:06 +03:00
self . debug_step ( & ctx ) ;
self . debug_screenshot ( & ctx ) ;
self . analysis . poll ( ) ;
let ctrl = | k | KeyboardShortcut ::new ( Modifiers ::COMMAND , k ) ;
if ctx . input_mut ( | i | i . consume_shortcut ( & ctrl ( Key ::O ) ) ) {
self . pick_databases ( & ctx ) ;
}
if ctx . input_mut ( | i | i . consume_shortcut ( & ctrl ( Key ::N ) ) ) & & self . game . is_none ( ) {
self . game = Some ( GameView ::new ( Game ::new ( ) , None , Vec ::new ( ) ) ) ;
}
if ctx . input_mut ( | i | i . consume_shortcut ( & ctrl ( Key ::Comma ) ) ) {
self . settings_ui . open = ! self . settings_ui . open ;
}
if let Some ( game ) = & mut self . game {
let input_enabled = self . confirm_close . is_none ( ) ;
2026-10-19 23:20:37 +03:00
// A database opened for the reference tab arrives here (the library is
// not drawn while a game is shown).
self . library . poll ( ) ;
self . reference . dbs = self
. settings
. databases
. iter ( )
. filter ( | p | p . exists ( ) )
. map ( | p | crate ::reference ::RefDb {
path : p . clone ( ) ,
name : p
. file_stem ( )
. map ( | s | s . to_string_lossy ( ) . into_owned ( ) )
. unwrap_or_default ( ) ,
db : self . library . database ( p ) ,
failed : self . library . failure ( p ) . map ( str ::to_owned ) ,
bytes : self . library . file_size ( p ) ,
} )
. collect ( ) ;
let actions = game . show (
ui ,
& mut self . settings ,
& mut self . analysis ,
& mut self . reference ,
input_enabled ,
) ;
self . reference_tab = game . reference_tab ( ) ;
2026-10-18 01:54:06 +03:00
for a in actions {
match a {
GameAction ::Back = > self . request_close ( & ctx , CloseIntent ::BackToLibrary ) ,
ChessPenguin 0.5.0: find games by position on a board
The "Board" pane next to the game list (Ctrl+B): moves played on it filter
the list to the games that reach the position by any move order, together
with the filters; the moves played next are listed with games, a results
bar, average rating and last year. Games reached by another order are
marked and "only this order" narrows the list; strongest games first.
A game opens at the found position; "Find in database" (Ctrl+Shift+F)
searches the position of the game on screen.
Large ChessBase databases: a fast main-line walk (moves built from the
board), pruning by material and pawns, the .cbb search booster when valid;
Mega Database: 0.4 s for the starting position, under 0.1 s deeper.
PGN databases and books: positions of all games, variations included,
kept in memory once built.
Also: × to clear filter and search fields; "Open as a new game" opens at
the last move; the "Moves" column is now "Length".
Docs: decision 13, design (docs/design/board-search.md), research
(position-search, position-index), dev notes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 08:42:08 +03:00
GameAction ::FindPosition ( db ) = > self . find_position ( & ctx , db ) ,
2026-10-19 23:20:37 +03:00
GameAction ::ReferenceChoose ( p ) = > {
self . settings . reference_db = Some ( p ) ;
self . settings_dirty = true ;
}
GameAction ::ReferenceLoad ( p ) = > self . library . load ( & p , & ctx ) ,
GameAction ::OpenGame ( path , index , at ) = > {
if let Some ( prev ) = self . game . take ( ) {
let title = prev . title ( ) ;
self . previous_games . push ( prev ) ;
self . open_game ( & ctx , path , index , Some ( at ) ) ;
match & mut self . game {
Some ( g ) = > {
g . previous = Some ( title ) ;
g . show_reference_tab ( ) ;
}
// Not opened: back to where we were.
None = > self . game = self . previous_games . pop ( ) ,
}
}
}
2026-10-20 07:55:59 +03:00
GameAction ::OpenUpdate = > self . update_ui . open = true ,
2026-10-19 23:20:37 +03:00
GameAction ::ReturnToPrevious = > {
if ! self . previous_games . is_empty ( ) {
self . request_close ( & ctx , CloseIntent ::ReturnToPrevious ) ;
}
}
2026-10-19 09:33:37 +03:00
GameAction ::NeedEngine = > self
. engine_offer
. ask ( crate ::engine_offer ::Pending ::GameAnalysis , & self . settings ) ,
2026-10-18 01:54:06 +03:00
GameAction ::Sound ( s ) = > self . play ( s ) ,
GameAction ::CopyText ( text , msg ) = > {
ctx . copy_text ( text ) ;
self . toast ( & ctx , msg ) ;
}
GameAction ::SaveAs = > self . pick_save ( & ctx ) ,
GameAction ::ToggleEngine = > self . toggle_engine ( & ctx ) ,
GameAction ::EngineReconfigure = > {
self . settings_dirty = true ;
if self . analysis . is_on ( )
& & let Some ( cfg ) = self . settings . active_engine ( ) . cloned ( )
{
self . analysis . reconfigure ( & cfg , & ctx ) ;
}
}
GameAction ::EngineSwitch = > {
self . settings_dirty = true ;
if self . analysis . is_on ( )
& & let Some ( cfg ) = self . settings . active_engine ( ) . cloned ( )
{
self . analysis . start ( & cfg , & ctx ) ;
}
}
GameAction ::OpenEngineSettings = > {
self . settings_ui . open = true ;
self . settings_ui . tab = Tab ::Engines ;
}
GameAction ::OpenSettings = > self . settings_ui . open = ! self . settings_ui . open ,
GameAction ::Toast ( m ) = > self . toast ( & ctx , m ) ,
Game analysis: whole-game engine review with graph, report and notation marks
- Separate background engine job over the main line (go nodes N), live engine
paused meanwhile; header button with progress/cancel, Shift+A.
- Lichess-method metrics: win %, move/game accuracy, inaccuracy/mistake/blunder,
ACPL, accuracy per game phase.
- Evaluation graph under the board, Report tab, engine badges and better lines
in the notation (preview on click), launch dialog with quality/threads/memory
and time estimate.
- Results cached in ~/.cache/chesspenguin/analysis; [%eval] read from PGN.
- Write analysis to the game as one undo step (chess-core: Game::annotate,
Game::edit_group).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 11:03:23 +03:00
GameAction ::PauseLive = > {
if self . analysis . is_on ( ) {
self . analysis . stop ( ) ;
self . live_paused = true ;
}
}
GameAction ::ResumeLive = > {
if std ::mem ::take ( & mut self . live_paused )
& & let Some ( cfg ) = self . settings . active_engine ( ) . cloned ( )
{
self . analysis . start ( & cfg , & ctx ) ;
}
}
2026-10-18 01:54:06 +03:00
}
}
} else {
let dbs = self . settings . databases . clone ( ) ;
2026-10-19 21:10:22 +03:00
self . online_probe . poll ( ) ;
self . library . online = self . online_probe . any ( ) ;
self . library . online_busy = self . online . busy ( ) ;
2026-10-20 08:30:31 +03:00
self . library . online_running = self . online . running ( ) ;
ChessPenguin 0.5.0: find games by position on a board
The "Board" pane next to the game list (Ctrl+B): moves played on it filter
the list to the games that reach the position by any move order, together
with the filters; the moves played next are listed with games, a results
bar, average rating and last year. Games reached by another order are
marked and "only this order" narrows the list; strongest games first.
A game opens at the found position; "Find in database" (Ctrl+Shift+F)
searches the position of the game on screen.
Large ChessBase databases: a fast main-line walk (moves built from the
board), pruning by material and pawns, the .cbb search booster when valid;
Mega Database: 0.4 s for the starting position, under 0.1 s deeper.
PGN databases and books: positions of all games, variations included,
kept in memory once built.
Also: × to clear filter and search fields; "Open as a new game" opens at
the last move; the "Moves" column is now "Length".
Docs: decision 13, design (docs/design/board-search.md), research
(position-search, position-index), dev notes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 08:42:08 +03:00
for a in self . library . show ( ui , & dbs , & self . settings ) {
2026-10-18 01:54:06 +03:00
match a {
ChessPenguin 0.5.0: find games by position on a board
The "Board" pane next to the game list (Ctrl+B): moves played on it filter
the list to the games that reach the position by any move order, together
with the filters; the moves played next are listed with games, a results
bar, average rating and last year. Games reached by another order are
marked and "only this order" narrows the list; strongest games first.
A game opens at the found position; "Find in database" (Ctrl+Shift+F)
searches the position of the game on screen.
Large ChessBase databases: a fast main-line walk (moves built from the
board), pruning by material and pawns, the .cbb search booster when valid;
Mega Database: 0.4 s for the starting position, under 0.1 s deeper.
PGN databases and books: positions of all games, variations included,
kept in memory once built.
Also: × to clear filter and search fields; "Open as a new game" opens at
the last move; the "Moves" column is now "Length".
Docs: decision 13, design (docs/design/board-search.md), research
(position-search, position-index), dev notes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 08:42:08 +03:00
LibraryAction ::OpenGame { path , index , at } = > {
self . open_game ( & ctx , path , index , at )
}
LibraryAction ::Sound ( s ) = > self . play ( s ) ,
LibraryAction ::CopyText ( text , msg ) = > {
ctx . copy_text ( text ) ;
self . toast ( & ctx , msg ) ;
}
LibraryAction ::OpenPgn ( text ) = > self . open_pgn_text ( & ctx , text , true ) ,
2026-10-18 01:54:06 +03:00
LibraryAction ::NewGame = > {
self . game = Some ( GameView ::new ( Game ::new ( ) , None , Vec ::new ( ) ) )
}
LibraryAction ::PastePgn = > self . paste_pgn ( & ctx ) ,
LibraryAction ::AddDatabases = > self . pick_databases ( & ctx ) ,
2026-10-20 21:59:00 +03:00
LibraryAction ::OnlineGames = > self . online . open ( ) ,
2026-10-19 21:10:22 +03:00
LibraryAction ::UpdateOnline ( p ) = > self . online . update ( & p , & ctx ) ,
2026-10-20 07:55:59 +03:00
LibraryAction ::OpenUpdate = > self . update_ui . open = true ,
2026-10-18 01:54:06 +03:00
LibraryAction ::RemoveDatabase ( p ) = > {
self . settings . databases . retain ( | d | * d ! = p ) ;
self . library . forget ( & p ) ;
self . settings_dirty = true ;
}
LibraryAction ::OpenSettings = > self . settings_ui . open = true ,
}
}
}
2026-10-20 21:59:00 +03:00
let events = self . online . show ( & ctx , & self . online_probe ) ;
2026-10-19 21:10:22 +03:00
for e in events {
match e {
crate ::online_ui ::OnlineEvent ::Added ( p ) = > {
let known = self . settings . databases . contains ( & p ) ;
self . settings . touch_database ( & p ) ;
if known {
self . library . reload ( & p , & ctx ) ;
}
self . library . select_db ( & p , & ctx ) ;
self . settings_dirty = true ;
}
crate ::online_ui ::OnlineEvent ::Updated ( p ) = > self . library . reload ( & p , & ctx ) ,
2026-10-20 08:30:31 +03:00
crate ::online_ui ::OnlineEvent ::Open ( p ) = > {
self . settings . touch_database ( & p ) ;
self . library . select_db ( & p , & ctx ) ;
self . settings_dirty = true ;
}
crate ::online_ui ::OnlineEvent ::Message ( t ) = > self . toast ( & ctx , t ) ,
2026-10-19 21:10:22 +03:00
}
}
2026-10-20 07:55:59 +03:00
// Updates: the badge next to Settings, the window, the status in Settings → About.
self . update_check . poll ( ) ;
let skipped = self . settings . skipped_version . clone ( ) ;
let badge = self
. update_check
. newer ( )
. filter ( | r | self . update_ui . active ( ) | | skipped . as_deref ( ) ! = Some ( r . version . as_str ( ) ) )
. map ( | r | self . update_ui . badge ( r ) ) ;
self . library . update = badge . clone ( ) ;
if let Some ( g ) = & mut self . game {
g . update = badge ;
}
for e in self
. update_ui
. show ( & ctx , self . update_check . newer ( ) , & self . update_kind )
{
match e {
crate ::update_ui ::UpdateEvent ::Restart ( exe ) = > {
self . restart_to = Some ( exe ) ;
self . request_close ( & ctx , CloseIntent ::QuitApp ) ;
}
crate ::update_ui ::UpdateEvent ::CopyText ( t ) = > {
ctx . copy_text ( t ) ;
self . toast ( & ctx , tr ( " Copied " ) ) ;
}
crate ::update_ui ::UpdateEvent ::Quit = > {
self . request_close ( & ctx , CloseIntent ::QuitApp ) ;
}
crate ::update_ui ::UpdateEvent ::Skip ( v ) = > {
self . settings . skipped_version = Some ( v ) ;
self . settings_dirty = true ;
}
}
}
self . settings_ui . update_available = self . update_check . newer ( ) . map ( | r | r . version . clone ( ) ) ;
self . settings_ui . update_status = if self . update_check . running ( ) {
Some ( tr ( " Checking… " ) . to_owned ( ) )
} else if let Some ( r ) = self . update_check . newer ( ) {
Some ( trf ( " Version {v} is out " , & [ ( " v " , & r . version ) ] ) )
} else if self . update_check . release . is_some ( ) {
Some ( tr ( " This is the latest version " ) . to_owned ( ) )
} else if self . update_check . error . is_some ( ) {
Some ( tr ( " Could not check for updates " ) . to_owned ( ) )
} else {
None
} ;
2026-10-19 21:10:22 +03:00
2026-10-19 09:33:37 +03:00
self . settings_ui . download = self . engine_offer . stage ( ) ;
2026-10-18 01:54:06 +03:00
let ( actions , changed ) = self . settings_ui . show ( & ctx , & mut self . settings ) ;
self . settings_dirty | = changed ;
for a in actions {
match a {
SettingsAction ::AddEngine = > self . pick_engine ( & ctx ) ,
2026-10-20 07:55:59 +03:00
SettingsAction ::CheckUpdates = > {
self . update_check = self . update_check . again ( & ctx ) ;
}
SettingsAction ::OpenUpdate = > self . update_ui . open = true ,
2026-10-19 09:33:37 +03:00
SettingsAction ::DownloadEngine = > self . engine_offer . start ( & ctx ) ,
SettingsAction ::CancelDownload = > self . engine_offer . cancel ( ) ,
SettingsAction ::RemoveDownloaded = > {
if let Err ( e ) = crate ::engine_download ::remove ( ) {
self . toast ( & ctx , e . to_string ( ) ) ;
}
if self . settings . active_engine ( ) . is_some_and ( | e | e . builtin )
& & ! crate ::engine_offer ::EngineOffer ::engine_ready ( & self . settings )
{
self . analysis . stop ( ) ;
}
}
2026-10-18 01:54:06 +03:00
SettingsAction ::TestSound = > {
if self . settings . sound_enabled {
self . sound
. play ( self . settings . sound_set , Sound ::Move , self . settings . volume ) ;
}
}
SettingsAction ::EngineChanged = > {
if self . analysis . is_on ( ) {
self . analysis . stop ( ) ;
if let Some ( cfg ) = self . settings . active_engine ( ) . cloned ( ) {
self . analysis . start ( & cfg , & ctx ) ;
}
}
}
}
}
self . confirm_dialog ( & ctx ) ;
if let Some ( ( msg , until ) ) = & self . toast {
let now = ctx . input ( | i | i . time ) ;
if now > * until {
self . toast = None ;
} else {
egui ::Area ::new ( egui ::Id ::new ( " toast " ) )
. anchor ( egui ::Align2 ::CENTER_BOTTOM , egui ::vec2 ( 0.0 , - 24.0 ) )
. order ( egui ::Order ::Foreground )
. show ( & ctx , | ui | {
egui ::Frame ::popup ( ui . style ( ) ) . show ( ui , | ui | {
ui . label ( RichText ::new ( msg . as_str ( ) ) . size ( 14.0 ) ) ;
} ) ;
} ) ;
ctx . request_repaint_after ( std ::time ::Duration ::from_millis ( 250 ) ) ;
}
}
if self . settings_dirty & & ! ctx . input ( | i | i . pointer . any_down ( ) ) {
self . settings_dirty = false ;
if let Err ( e ) = self . settings . save ( ) {
eprintln! ( " settings: save failed: {e} " ) ;
}
}
}
fn on_exit ( & mut self ) {
self . analysis . stop ( ) ;
2026-10-20 08:30:31 +03:00
// Downloads of online games stop and save what they got; a new database goes on
// the shelf with it (the next download continues it).
for p in self . online . stop_all ( ) {
if crate ::online ::State ::load ( & p ) . is_some_and ( | s | s . games > 0 ) {
self . settings . touch_database ( & p ) ;
}
}
2026-10-18 01:54:06 +03:00
let _ = self . settings . save ( ) ;
}
}
fn sanitize ( s : & str ) -> String {
s . chars ( )
. map ( | c | match c {
'—' | '– ' = > '-' ,
c if c . is_alphanumeric ( ) | | " -., " . contains ( c ) = > c ,
_ = > '_' ,
} )
. collect ::< String > ( )
}