chessbaseLinux/apps/desktop/src/examples.rs
vrubelroman 1518fc5f70
Some checks are pending
CI / check (push) Waiting to run
CI / windows-build (push) Waiting to run
CI / macos-build (push) Waiting to run
Library: shelf actions in the title row, «+ Game» for a database, read-only samples
The cards row holds only databases; «+ Add database», «✚ New database» and
«🌐 Online games» stand beside «Databases» (folded to «+ ▾ 🌐» when narrow); an
empty shelf shows them as large cards. A database that takes games has «+ Game»
in its table header: a new game meant for it, whose Ctrl+S adds it there (the
target is dropped when the base leaves the shelf or stops taking games). The
chooser lists only databases that take games; with none, «✚ New database…» is
the main button; the UTF-8 copy is in the card menu (also for a file not read
yet). Built-in samples are read-only. An empty database has no filter panel or
search; a narrow search field says «🔍 Search». The table header keeps a short
database name. Task 8 closed: the quick search stays.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 01:39:19 +03:00

94 lines
3 KiB
Rust

//! Sample databases for the first start, so the library is not empty: classic games
//! with short introductions, and Alekhine's games (moves only). They are written once,
//! named in the interface language, to `<local data dir>/examples/` and put on the
//! library shelf; a sample removed from the shelf does not come back.
use std::path::PathBuf;
use crate::i18n::tr;
use crate::settings::Settings;
fn files() -> [(&'static str, &'static str); 2] {
[
(
tr("Classic games"),
include_str!("../../../examples/classics.pgn"),
),
(
tr("Alekhine"),
include_str!("../../../examples/alekhine.pgn"),
),
]
}
fn dir() -> Option<PathBuf> {
directories::ProjectDirs::from("io.github", "vrubelroman", "ChessPenguin")
.map(|d| d.data_local_dir().join("examples"))
}
/// A sample database of the program: only read (games added there would end up in a
/// hidden data folder).
pub fn is_example(path: &std::path::Path) -> bool {
dir().is_some_and(|d| path.starts_with(d))
}
/// Adds the samples once; `true` when the settings changed. A new library starts with
/// them (the classics open); an existing one gets them after its own databases.
pub fn first_run_setup(settings: &mut Settings) -> bool {
if settings.examples_added {
return false;
}
settings.examples_added = true;
let Some(dir) = dir() else {
return true;
};
if let Err(e) = std::fs::create_dir_all(&dir) {
eprintln!("examples: cannot create {}: {e}", dir.display());
return true;
}
for (name, text) in files() {
let path = dir.join(format!("{name}.pgn"));
if !path.exists()
&& let Err(e) = std::fs::write(&path, text)
{
eprintln!("examples: cannot write {}: {e}", path.display());
continue;
}
if !settings.databases.contains(&path) {
settings.databases.push(path);
}
}
true
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn samples_are_valid_pgn() {
for (name, text) in files() {
let dir = std::env::temp_dir().join(format!("cp-examples-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join(format!("{name}.pgn"));
std::fs::write(&path, text).unwrap();
let db = chess_core::PgnDatabase::open(
&path,
&mut |_| {},
&std::sync::atomic::AtomicBool::new(false),
)
.unwrap();
assert!(db.len() >= 30, "{name}: {} games", db.len());
for i in 0..db.len() {
let parsed = db.load_game(i).unwrap();
assert!(
parsed.diagnostics.is_empty(),
"{name} #{}: {:?}",
i + 1,
parsed.diagnostics
);
}
let _ = std::fs::remove_dir_all(&dir);
}
}
}