opening trainer: four starter repertoires for new and updated users, wider repertoire panel; headless test windows

This commit is contained in:
vrubelroman 2026-11-27 12:54:40 +03:00
parent e60b0189c8
commit 2f75b52556
16 changed files with 1442 additions and 181 deletions

View file

@ -3789,6 +3789,20 @@ impl eframe::App for App {
});
self.applied_dark = Some(self.settings.dark_mode);
}
// The opening panel's dragged width: given to the panel, saved when dragged anew.
let side_width = egui::Id::new(crate::opening_layout::SIDE_WIDTH_ID);
match ctx.data(|d| d.get_temp::<f32>(side_width)) {
None => {
if let Some(width) = self.settings.opening_side_width {
ctx.data_mut(|d| d.insert_temp(side_width, width));
}
}
Some(width) if self.settings.opening_side_width != Some(width) => {
self.settings.opening_side_width = Some(width);
let _ = self.settings.save();
}
Some(_) => {}
}
// Quitting while the opening errors are searched: asked first (what was found
// is kept either way).
if ctx.input(|i| i.viewport().close_requested())

View file

@ -92,17 +92,27 @@ impl Lang {
}
}
#[cfg(not(test))]
static CURRENT: AtomicU8 = AtomicU8::new(0);
// Tests run in parallel threads: a test that switches the language switches only its own.
#[cfg(test)]
thread_local! {
static CURRENT: AtomicU8 = const { AtomicU8::new(0) };
}
pub fn set_lang(lang: Lang) {
#[cfg(not(test))]
CURRENT.store(lang as u8, Ordering::Relaxed);
#[cfg(test)]
CURRENT.with(|c| c.store(lang as u8, Ordering::Relaxed));
}
pub fn current() -> Lang {
Lang::ALL
.get(CURRENT.load(Ordering::Relaxed) as usize)
.copied()
.unwrap_or_default()
#[cfg(not(test))]
let index = CURRENT.load(Ordering::Relaxed);
#[cfg(test)]
let index = CURRENT.with(|c| c.load(Ordering::Relaxed));
Lang::ALL.get(index as usize).copied().unwrap_or_default()
}
fn table() -> &'static HashMap<&'static str, &'static [&'static str; 9]> {
@ -17641,6 +17651,39 @@ const ROWS: &[[&str; 9]] = &[
"Sitsiliya himoyasi: Naydorf",
"Сицилия қорғанысы: Найдорф",
],
[
"The record of example repertoires is damaged; examples were not added",
"Запись о примерах репертуаров повреждена; примеры не добавлены",
"L’enregistrement des répertoires d’exemple est endommagé ; les exemples n’ont pas été ajoutés",
"Die Liste der Beispielrepertoires ist beschädigt; Beispiele wurden nicht hinzugefügt",
"El registro de repertorios de ejemplo está dañado; no se añadieron ejemplos",
"Il registro dei repertori di esempio è danneggiato; gli esempi non sono stati aggiunti",
"O registro dos repertórios de exemplo está danificado; os exemplos não foram adicionados",
"Namuna repertuarlar yozuvi buzilgan; namunalar qo‘shilmadi",
"Мысал репертуарлар жазбасы бүлінген; мысалдар қосылмады",
],
[
"Queen's Gambit",
"Ферзевый гамбит",
"Gambit dame",
"Damengambit",
"Gambito de dama",
"Gambetto di donna",
"Gambito da Dama",
"Farzin gambiti",
"Ферзь гамбиті",
],
[
"Semi-Slav: Triangle",
"Полуславянская защита: треугольник",
"Semi-slave : triangle",
"Halbslawisch: Dreieck",
"Semieslava: triángulo",
"Semi-Slava: triangolo",
"Semi-Eslava: triângulo",
"Yarim slavyan himoyasi: uchburchak",
"Жартылай славян қорғанысы: үшбұрыш",
],
[
"New repertoire",
"Новый репертуар",

View file

@ -11,8 +11,11 @@ use crate::board::{Arrow, Badge};
use crate::i18n::{tr, trf};
use crate::settings::Settings;
/// The panel's least width (the game's reference tab, `REF_MIN`).
const SIDE_LEAST: f32 = 320.0;
/// The panel's least width: the table of repertoires still shows its numbers whole.
pub(crate) const SIDE_LEAST: f32 = 360.0;
/// Where the width the user dragged the panel to is kept for the app to save
/// (`Settings::opening_side_width`).
pub(crate) const SIDE_WIDTH_ID: &str = "opening_side_width";
/// Under a stacked board: the panel's least height (`STACK_BOTTOM_MIN`).
const STACK_BOTTOM_MIN: f32 = 300.0;
const BOARD_MIN: f32 = 220.0;
@ -48,11 +51,37 @@ pub(crate) fn screen<T: ?Sized>(
let board = height - 16.0;
let most = (width - BOARD_MIN - 24.0).max(SIDE_LEAST);
let room = (width - board - 24.0).clamp(SIDE_LEAST, most);
egui::Panel::right("opening_side")
let id = egui::Id::new(SIDE_WIDTH_ID);
let kept = ui.ctx().data(|d| d.get_temp::<f32>(id));
let default = kept.unwrap_or_else(|| side_default(width, room));
let handle = egui::Id::new("opening_side").with("__resize");
// Until the user drags it, the panel follows the window: a window that opens
// small and grows gets the width of its final size, not of its first frame.
let dragging = ui
.ctx()
.read_response(handle)
.is_some_and(|r| r.dragged() || r.drag_stopped());
if kept.is_none() && !dragging {
ui.ctx().data_mut(|d| {
d.remove::<egui::containers::panel::PanelState>(egui::Id::new("opening_side"))
});
}
let panel = egui::Panel::right("opening_side")
.resizable(true)
.default_size((width * 0.4).clamp(260.0, 460.0).min(room))
.default_size(default.clamp(SIDE_LEAST, most))
.size_range(SIDE_LEAST..=most)
.show(ui, |ui| side(this, ui))
.show(ui, |ui| side(this, ui));
// The width the user dragged to is kept; a window that squeezes the panel does
// not change it.
if ui
.ctx()
.read_response(handle)
.is_some_and(|r| r.drag_stopped())
{
let shown = panel.response.rect.width().round();
ui.ctx().data_mut(|d| d.insert_temp(id, shown));
}
panel
};
crate::widgets::check_panel("opening_side", &panel.response);
egui::CentralPanel::default().show(ui, |ui| {
@ -65,6 +94,12 @@ pub(crate) fn screen<T: ?Sized>(
});
}
/// The panel's width before the user drags it: about a third of the window, the board
/// keeping its room.
pub(crate) fn side_default(width: f32, room: f32) -> f32 {
(width * 0.36).clamp(380.0, 560.0).min(room)
}
/// The frame of a state card (the player's card of a game against the computer).
pub(crate) fn card(ui: &Ui, active: bool, alert: bool) -> egui::Frame {
let mut frame = egui::Frame::new()

View file

@ -1241,7 +1241,11 @@ fn key(key: Key) -> egui::Event {
#[test]
fn the_panel_takes_the_game_screens_place_at_both_reference_sizes() {
for (size, left) in [(vec2(1400.0, 900.0), 940.0), (vec2(960.0, 600.0), 576.0)] {
for (size, left) in [
(vec2(1920.0, 1080.0), 1360.0),
(vec2(1400.0, 900.0), 896.0),
(vec2(960.0, 600.0), 580.0),
] {
let mut f = Fixture::new("1. e4 *", LearningSide::White, &["e2e4"], "geometry");
let ctx = egui::Context::default();
crate::assets::install(&ctx);

View file

@ -16,7 +16,7 @@ use egui::{Color32, Key, Modifiers, RichText, Ui, vec2};
use trainer::repertoire::{
LearningData, LearningItemKey, LearningSide, LearningStore, LearningStoreData, Library,
LibraryStore, MergeReport, RepertoireId, ResetImpactProjection, SrsSettings, StatsScope,
active_learning_keys, aggregate_scope, fen4, reset_impact,
StoreError, active_learning_keys, aggregate_scope, fen4, reset_impact,
};
use web_time::Instant;
@ -43,7 +43,8 @@ pub enum RepertoireAction {
},
}
const CARD_H: f32 = 166.0;
const CARD_H: f32 = 168.0;
const BUTTON_ROW_H: f32 = 30.0;
const ROW_H: f32 = 24.0;
const HEADER_H: f32 = 26.0;
const MESSAGE_H: f32 = 20.0;
@ -52,10 +53,25 @@ const BOTTOM_W: f32 = 150.0;
const MAIN_W: f32 = 170.0;
const LINES_W: f32 = 110.0;
const THIRD_W: f32 = 150.0;
/// The table's fixed columns: «Today» phrase, the bar, «A/Y» (room for «999/999»).
const TODAY_W: f32 = 90.0;
/// The bar in the «Passed» column, left of «A/Y».
const MINI_BAR_W: f32 = 44.0;
const PASSED_W: f32 = 64.0;
/// The bar of a narrow panel (under `NARROW_TABLE`) whose name would keep under
/// `NAME_WANTED`.
const MINI_BAR_NARROW: f32 = 28.0;
const NARROW_TABLE: f32 = 420.0 - 16.0;
const NAME_WANTED: f32 = 60.0;
/// The room kept for the number of «Passed».
const NUMBER_SAMPLE: &str = "00/000";
const MINI_BAR_GAP: f32 = 6.0;
/// The name keeps up to this much; only it is ever cut, down to `NAME_FLOOR`.
const NAME_LEAST: f32 = 120.0;
const NAME_FLOOR: f32 = 30.0;
/// Space from «Today» to the «Passed» bar, the columns' own gap included: the bar
/// never reads as part of «Today».
const PASSED_GAP: f32 = 16.0;
/// The fixed columns besides «Today» and «Passed»: the colour and `⋯`.
const KING_W: f32 = 22.0;
const MENU_W: f32 = 26.0;
const DIALOG_W: f32 = 460.0;
/// A session appears at once when it is ready this fast; otherwise «Preparing…».
const PREPARING_SHOWN_AFTER: Duration = Duration::from_millis(300);
@ -1534,8 +1550,14 @@ impl RepertoireUi {
}
fn card(&mut self, ui: &mut Ui, settings: &Settings) -> Option<MenuItem> {
let two_rows = card_buttons_two_rows(ui, ui.available_width() - 22.0);
let card_h = if two_rows {
CARD_H + BUTTON_ROW_H + 6.0
} else {
CARD_H
};
let Some(id) = self.selected.clone() else {
layout::fixed_block(ui, CARD_H, |_| {});
layout::fixed_block(ui, card_h, |_| {});
return None;
};
let state = self.selected_card();
@ -1547,8 +1569,8 @@ impl RepertoireUi {
let side = layout::side_of(settings, &id);
let name = self.selected_name();
let mut chosen = None;
layout::fixed_block(ui, CARD_H, |ui| {
let inner = layout::card_inner(ui, CARD_H);
layout::fixed_block(ui, card_h, |ui| {
let inner = layout::card_inner(ui, card_h);
layout::card(ui, false, false).show(ui, |ui| {
ui.set_min_size(inner);
ui.set_max_size(inner);
@ -1557,15 +1579,29 @@ impl RepertoireUi {
// 1. The name and the colour.
layout::fixed_block(ui, 26.0, |ui| {
ui.horizontal(|ui| {
let you = format!("{} {}", layout::king(side), layout::you_play(side));
let you_w = text_width(ui, &you, 14.0) + 8.0;
// The colour gives way first: down to the king, the words on
// hover; only then is the name cut.
let gap = ui.spacing().item_spacing.x;
let full = format!("{} {}", layout::king(side), layout::you_play(side));
let name_w = style_width(ui, &name, egui::TextStyle::Heading);
let full_w = style_width(ui, &full, egui::TextStyle::Body) + gap;
let short = name_w + full_w > width;
let you = if short {
layout::king(side).to_owned()
} else {
full
};
let you_w = style_width(ui, &you, egui::TextStyle::Body) + gap;
ui.allocate_ui_with_layout(
vec2((width - you_w).max(0.0), 26.0),
vec2((width - you_w - gap).max(0.0), 26.0),
egui::Layout::left_to_right(egui::Align::Center),
|ui| layout::line(ui, RichText::new(&name).heading()),
);
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
ui.label(RichText::new(you).weak());
let label = ui.label(RichText::new(you).weak());
if short {
label.on_hover_text(layout::you_play(side));
}
});
});
});
@ -1579,83 +1615,120 @@ impl RepertoireUi {
layout::line(ui, text);
});
// 3. Progress.
layout::fixed_block(ui, 28.0, |ui| progress(ui, stats.as_ref()));
layout::fixed_block(ui, 30.0, |ui| progress(ui, stats.as_ref()));
// 4. A quiet explanation.
layout::fixed_block(ui, 20.0, |ui| {
layout::line(ui, RichText::new(note).weak());
});
// 5. Buttons.
layout::fixed_block(ui, 30.0, |ui| {
ui.horizontal(|ui| {
let (label, enabled, item) = main_button(&state, side);
let gap = ui.spacing().item_spacing.x;
let pad = 2.0 * ui.spacing().button_padding.x + 2.0;
let room = ui.available_width();
// The row is the same in every state with moves: «Review all» is
// there unless the main button already is it. A narrow panel makes
// the buttons narrower, down to their words.
let third = !matches!(
state,
CardState::Empty
| CardState::Failed(_)
| CardState::Counting
| CardState::OtherColor
) && !matches!(item, Some(MenuItem::ReviewAll));
let main_need = text_width(ui, &label, 14.0) + pad;
let lines_need = text_width(ui, tr("Lines…"), 14.0) + pad;
let third_need = text_width(ui, tr("Review all"), 14.0) + pad;
let (main_w, lines_w, third_w) = if third {
let spare = room - main_need - lines_need - third_need - 2.0 * gap;
let grow = |need: f32, most: f32, spare: &mut f32| {
let add = (most - need).max(0.0).min(spare.max(0.0));
*spare -= add;
need + add
};
let mut spare = spare;
let main_w = grow(main_need, MAIN_W, &mut spare);
let lines_w = grow(lines_need, LINES_W, &mut spare);
(main_w, lines_w, grow(third_need, THIRD_W, &mut spare))
} else {
(MAIN_W.min((room - LINES_W - gap).max(0.0)), LINES_W, 0.0)
};
let response = ui
.add_enabled_ui(enabled, |ui| {
crate::widgets::primary_wide(ui, &label, main_w)
})
.inner;
let response = if matches!(state, CardState::Counting) {
response.on_disabled_hover_text(tr("Counting moves…"))
} else {
response
};
if response.clicked() {
chosen = item;
}
if ui
.add_enabled(
!matches!(state, CardState::Empty | CardState::Failed(_)),
egui::Button::new(tr("Lines…"))
// 5. Buttons: one row when the widest row fits, else the main button
// on a row of its own. Which one depends on the width alone, so the
// card keeps its height whatever is selected.
let (label, enabled, item) = main_button(&state, side);
// «Review all» is there in every state with moves unless the main button
// already is it.
let third = !matches!(
state,
CardState::Empty
| CardState::Failed(_)
| CardState::Counting
| CardState::OtherColor
) && !matches!(item, Some(MenuItem::ReviewAll));
let gap = ui.spacing().item_spacing.x;
let mut main = |ui: &mut Ui, width: f32| {
let response = ui
.add_enabled_ui(enabled, |ui| {
crate::widgets::primary_wide(ui, &label, width)
})
.inner;
let response = if matches!(state, CardState::Counting) {
response.on_disabled_hover_text(tr("Counting moves…"))
} else {
response
};
if response.clicked() {
chosen = item;
}
};
let lines_enabled = !matches!(state, CardState::Empty | CardState::Failed(_));
let rest = |ui: &mut Ui, lines_w: f32, third_w: f32| -> Option<MenuItem> {
let mut picked = None;
if ui
.add_enabled(
lines_enabled,
egui::Button::new(tr("Lines…"))
.truncate()
.min_size(vec2(lines_w, BUTTON_ROW_H)),
)
.clicked()
{
picked = Some(MenuItem::Lines);
}
if third
&& ui
.add(
egui::Button::new(tr("Review all"))
.truncate()
.min_size(vec2(lines_w, 30.0)),
.min_size(vec2(third_w, BUTTON_ROW_H)),
)
.on_hover_text(tr("Review all lines"))
.clicked()
{
chosen = Some(MenuItem::Lines);
}
if third
&& ui
.add(
egui::Button::new(tr("Review all"))
.truncate()
.min_size(vec2(third_w, 30.0)),
)
.on_hover_text(tr("Review all lines"))
.clicked()
{
chosen = Some(MenuItem::ReviewAll);
}
{
picked = Some(MenuItem::ReviewAll);
}
picked
};
let mut picked = None;
if two_rows {
// In a row, as in the one-row layout: the label is centred.
layout::fixed_block(ui, BUTTON_ROW_H, |ui| {
ui.horizontal(|ui| {
let width = ui.available_width();
main(ui, width);
});
});
});
layout::fixed_block(ui, BUTTON_ROW_H, |ui| {
ui.horizontal(|ui| {
let half = ((ui.available_width() - gap) / 2.0).max(0.0);
picked = rest(ui, half, half);
});
});
} else {
layout::fixed_block(ui, BUTTON_ROW_H, |ui| {
ui.horizontal(|ui| {
let pad = 2.0 * ui.spacing().button_padding.x + 2.0;
let room = ui.available_width();
let need =
|text: &str| style_width(ui, text, egui::TextStyle::Button) + pad;
let main_need = need(&label);
let lines_need = need(tr("Lines…"));
let third_need = need(tr("Review all"));
let (main_w, lines_w, third_w) = if third {
let mut spare =
room - main_need - lines_need - third_need - 2.0 * gap;
let grow = |need: f32, most: f32, spare: &mut f32| {
let add = (most - need).max(0.0).min(spare.max(0.0));
*spare -= add;
need + add
};
let main_w = grow(main_need, MAIN_W, &mut spare);
let lines_w = grow(lines_need, LINES_W, &mut spare);
(main_w, lines_w, grow(third_need, THIRD_W, &mut spare))
} else {
let lines_w = lines_need.max(LINES_W.min(room / 3.0));
(
(room - lines_w - gap).max(0.0).min(MAIN_W.max(main_need)),
lines_w,
0.0,
)
};
main(ui, main_w);
picked = rest(ui, lines_w, third_w);
});
});
}
if picked.is_some() {
chosen = picked;
}
});
});
chosen
@ -1674,17 +1747,23 @@ impl RepertoireUi {
let mut chosen = None;
let open_menu = std::mem::take(&mut self.debug_menu);
ui.style_mut().interaction.selectable_labels = false;
let columns = table_widths(ui, ui.available_width());
let (today_w, passed_w) = (columns.today, columns.passed);
let bar_w = columns.bar;
let mut table = egui_extras::TableBuilder::new(ui)
.id_salt("opening_repertoires")
.striped(true)
.sense(egui::Sense::click())
.cell_layout(egui::Layout::left_to_right(egui::Align::Center))
.column(egui_extras::Column::remainder().at_least(80.0).clip(true))
.column(egui_extras::Column::exact(22.0))
.column(egui_extras::Column::exact(TODAY_W))
.column(egui_extras::Column::exact(MINI_BAR_W))
.column(egui_extras::Column::exact(PASSED_W))
.column(egui_extras::Column::exact(26.0));
.column(
egui_extras::Column::remainder()
.at_least(columns.name_least)
.clip(true),
)
.column(egui_extras::Column::exact(KING_W))
.column(egui_extras::Column::exact(today_w))
.column(egui_extras::Column::exact(passed_w))
.column(egui_extras::Column::exact(MENU_W));
if let Some(index) = scroll_row {
table = table.scroll_to_row(index, None);
}
@ -1700,11 +1779,10 @@ impl RepertoireUi {
});
});
// «Passed» heads the bar and the number together.
header.col(|_| {});
header.col(|ui| {
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
ui.add(egui::Label::new(RichText::new(tr("Passed")).strong()).truncate());
});
// From the left edge of the bars.
ui.add_space(columns.start);
ui.add(egui::Label::new(RichText::new(tr("Passed")).strong()).truncate());
});
header.col(|_| {});
})
@ -1738,14 +1816,21 @@ impl RepertoireUi {
RichText::new(trf("new: {n}", &[("n", &stats.new_today())]))
.weak()
} else if let Some((at, _)) = stats.next {
RichText::new(layout::when(at, layout::now())).weak()
RichText::new(when_short(at, layout::now())).weak()
} else {
RichText::new("—").weak()
};
ui.add(egui::Label::new(text).truncate()).on_hover_text(trf(
let mut hint = trf(
"To review {n}, new in the session {m}",
&[("n", &stats.due), ("m", &stats.new_today())],
));
);
if let Some((at, _)) = stats.next
&& stats.due == 0
&& stats.new_today() == 0
{
hint = format!("{}\n{hint}", layout::when(at, layout::now()));
}
ui.add(egui::Label::new(text).truncate()).on_hover_text(hint);
}
Some(Err(_)) => {
ui.label(RichText::new("—").weak());
@ -1757,42 +1842,41 @@ impl RepertoireUi {
});
});
let passed = matches!(stats, Some(Ok(stats)) if has_moves && stats.total > 0);
// [bar][6][number from the left]: neither moves with the counts.
row.col(|ui| {
if let Some(Ok(stats)) = stats
&& passed
{
let room = ui.available_rect_before_wrap();
let bar = egui::Rect::from_min_size(
egui::pos2(room.left(), room.center().y - 3.0),
vec2(MINI_BAR_W, 6.0),
);
progress_bar(ui, bar, Some(stats), selected);
ui.allocate_rect(room, egui::Sense::hover())
ui.add_space(columns.start);
match stats {
Some(Ok(stats)) if passed => {
let room = ui.available_rect_before_wrap();
let bar = egui::Rect::from_min_size(
egui::pos2(room.left(), room.center().y - 3.0),
vec2(bar_w, 6.0),
);
progress_bar(ui, bar, Some(stats), selected);
ui.interact(
bar.expand2(vec2(0.0, 6.0)),
ui.id().with("bar"),
egui::Sense::hover(),
)
.on_hover_text(passed_hint(stats));
}
});
row.col(|ui| {
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
match stats {
Some(Ok(_)) if !has_moves => {
ui.add(
egui::Label::new(RichText::new(tr("no moves")).weak())
.truncate(),
)
.on_hover_text(tr("The repertoire has no moves yet: import a PGN or add a line from a game"));
}
Some(Ok(stats)) if passed => {
ui.label(format!("{}/{}", stats.passed(), stats.total))
.on_hover_text(passed_hint(stats));
}
Some(_) => {
ui.label(RichText::new("—").weak());
}
None => {
ui.label(RichText::new("…").weak());
}
ui.add_space(bar_w + MINI_BAR_GAP);
ui.label(format!("{}/{}", stats.passed(), stats.total))
.on_hover_text(passed_hint(stats));
}
});
Some(Ok(_)) if !has_moves => {
ui.add(
egui::Label::new(RichText::new(tr("no moves")).weak())
.truncate(),
)
.on_hover_text(tr("The repertoire has no moves yet: import a PGN or add a line from a game"));
}
Some(_) => {
ui.label(RichText::new("—").weak());
}
None => {
ui.label(RichText::new("…").weak());
}
}
});
let mut opened = false;
row.col(|ui| {
@ -2557,9 +2641,9 @@ fn progress(ui: &mut Ui, stats: Option<&RowStats>) {
let width = ui.available_width();
let (bar, _) = ui.allocate_exact_size(vec2(width, 8.0), egui::Sense::hover());
progress_bar(ui, bar, stats, false);
ui.add_space(-2.0);
ui.spacing_mut().item_spacing.y = 4.0;
ui.horizontal(|ui| {
ui.set_height(20.0);
ui.set_height(18.0);
if let Some(stats) = stats {
let text = trf(
"Passed {a} of {y} moves · consolidated {l}",
@ -2785,17 +2869,111 @@ fn import_dialog(
result
}
fn text_width(ui: &Ui, text: &str, size: f32) -> f32 {
/// Whether the card's buttons take two rows at this inner width of the card: the widest
/// one-row set (a main button of the states with «Review all») does not fit.
fn card_buttons_two_rows(ui: &Ui, width: f32) -> bool {
let pad = 2.0 * ui.spacing().button_padding.x + 2.0;
let need = |text: &str| style_width(ui, text, egui::TextStyle::Button) + pad;
let main = need(tr("Train")).max(need(&trf("Learn {m} new", &[("m", &NEW_PER_SESSION)])));
let gap = ui.spacing().item_spacing.x;
main + need(tr("Lines…")) + need(tr("Review all")) + 2.0 * gap > width
}
/// The width of `text` in the font of `style`, as a label or a button draws it.
fn style_width(ui: &Ui, text: &str, style: egui::TextStyle) -> f32 {
ui.painter()
.layout_no_wrap(
text.to_owned(),
egui::FontId::proportional(size),
style.resolve(ui.style()),
egui::Color32::WHITE,
)
.size()
.x
}
/// The widths of the table's columns.
#[derive(Clone, Copy, Debug)]
struct TableColumns {
/// What the name keeps at the least: `NAME_LEAST`, less in a narrow panel so that
/// the fixed columns always fit.
name_least: f32,
today: f32,
passed: f32,
/// The bar of «Passed».
bar: f32,
/// Where the bar and the header of «Passed» start in its column.
start: f32,
}
impl TableColumns {
/// All columns at their least, with the spaces between them.
fn least(&self, gap: f32) -> f32 {
self.name_least + KING_W + self.today + self.passed + MENU_W + 4.0 * gap
}
}
/// The table's columns for a table `width` wide, measured once a frame from the texts
/// they may show, never from the rows on screen: counting or scrolling moves no column.
fn table_widths(ui: &Ui, width: f32) -> TableColumns {
let body = |text: &str| style_width(ui, text, egui::TextStyle::Body);
let now = layout::now();
let today = [
trf("review {n}", &[("n", &99)]),
trf("new: {n}", &[("n", &10)]),
when_short(now, now),
when_short(now + 24 * 60 * 60, now),
when_short(now + 3 * 24 * 60 * 60, now),
when_short(now + 30 * 24 * 60 * 60, now),
]
.iter()
.map(|t| body(t))
.fold(0.0, f32::max);
// A strong header is the body font in a brighter colour: as wide.
let today = (today.max(body(tr("Today"))) + 4.0).ceil();
// The lead keeps the bar off «Today»; the header and «no moves» may use it.
let gap = ui.spacing().item_spacing.x;
let lead = (PASSED_GAP - gap).max(0.0);
let columns = |bar: f32| {
// The group of the bar and the room of «00/000», or the header if wider; the
// lead before it unless the header needs that room.
let group = bar + MINI_BAR_GAP + body(NUMBER_SAMPLE);
let content = group.max(body(tr("Passed"))).max(body(tr("no moves")));
let passed = ((lead + group).max(content) + 4.0).ceil();
let start = (passed - 4.0 - content).max(0.0);
let fixed = KING_W + today + passed + MENU_W + 4.0 * gap;
TableColumns {
name_least: (width - fixed).clamp(NAME_FLOOR, NAME_LEAST),
today,
passed,
bar,
start,
}
};
let wide = columns(MINI_BAR_W);
if width < NARROW_TABLE && width - wide.least(gap) + wide.name_least < NAME_WANTED {
columns(MINI_BAR_NARROW)
} else {
wide
}
}
/// The date of the next review in a table cell: «12:30» today, «tomorrow», «in 3 days»,
/// then «dd.mm». The card and the hint say it in full.
fn when_short(at: i64, now: i64) -> String {
use chrono::{Local, TimeZone};
let (Some(at), Some(now)) = (
Local.timestamp_opt(at, 0).single(),
Local.timestamp_opt(now, 0).single(),
) else {
return String::new();
};
match (at.date_naive() - now.date_naive()).num_days() {
..=0 => at.format("%H:%M").to_string(),
1..=6 => layout::when(at.timestamp(), now.timestamp()),
_ => at.format("%d.%m").to_string(),
}
}
/// The colour whose moves a PGN repertoire gives: the side with fewer positions where
/// the PGN has several moves (one answer to many replies). White on a tie.
fn suggested_side(game: &Game) -> LearningSide {
@ -2928,54 +3106,175 @@ fn read_import(
})
}
const STARTER_REPERTOIRES: [(&str, &str, LearningSide); 2] = [
/// Stable key, name, PGN and colour of each example repertoire.
const STARTER_REPERTOIRES: [(&str, &str, &str, LearningSide); 4] = [
(
"spanish-white",
"Spanish Opening",
include_str!("../assets/repertoires/spanish-white.pgn"),
LearningSide::White,
),
(
"sicilian-black",
"Sicilian Defence: Najdorf",
include_str!("../assets/repertoires/sicilian-black.pgn"),
LearningSide::Black,
),
(
"queens-gambit-white",
"Queen's Gambit",
include_str!("../assets/repertoires/queens-gambit-white.pgn"),
LearningSide::White,
),
(
"triangle-black",
"Semi-Slav: Triangle",
include_str!("../assets/repertoires/triangle-black.pgn"),
LearningSide::Black,
),
];
/// The first opening of the section, before any repertoire library file exists, gets
/// two example repertoires. An existing file, even an empty one, is never touched, so
/// examples deleted by the user do not come back.
const STARTER_MARKS_VERSION: u32 = 1;
/// Keys of the example repertoires already given to the user, in a file of its own next
/// to the library: the library format read by 2.4.0 stays as it is.
#[derive(serde::Serialize, serde::Deserialize)]
struct StarterMarks {
version: u32,
given: BTreeSet<String>,
}
#[derive(serde::Deserialize)]
struct StarterMarksVersion {
version: u32,
}
fn starter_marks_path(store: &LibraryStore) -> PathBuf {
store.path().with_file_name("repertoire-starters.json")
}
/// Marks read from the file. `None`: no file yet. A file of a newer version means every
/// example was given: it is neither reissued nor overwritten.
enum StarterMarksState {
Missing,
Given(BTreeSet<String>),
Newer,
}
fn read_starter_marks(path: &Path) -> Result<StarterMarksState, String> {
let damaged =
|| tr("The record of example repertoires is damaged; examples were not added").to_owned();
let bytes = match crate::platform::fs::read(path) {
Ok(bytes) => bytes,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
return Ok(StarterMarksState::Missing);
}
Err(e) => {
eprintln!("{}: {e}", path.display());
return Err(damaged());
}
};
let version = serde_json::from_slice::<StarterMarksVersion>(&bytes).map_err(|e| {
eprintln!("{}: {e}", path.display());
damaged()
})?;
if version.version != STARTER_MARKS_VERSION {
eprintln!(
"{}: unknown version {}, examples are not added",
path.display(),
version.version
);
return Ok(StarterMarksState::Newer);
}
let marks = serde_json::from_slice::<StarterMarks>(&bytes).map_err(|e| {
eprintln!("{}: {e}", path.display());
damaged()
})?;
Ok(StarterMarksState::Given(marks.given))
}
/// Each example repertoire is given once. An example also counts as given whenever a
/// repertoire carries its name in any language: the examples of 2.4.0 had no marks, and a
/// lost write of the marks must not double them. Examples deleted after that do not come
/// back; the user's own repertoires are not touched. The library is read again right
/// before the examples are added, and a save that lost a race with another copy of the
/// program is retried on the fresh library.
fn seed_starter(store: &LibraryStore) -> Result<Vec<(RepertoireId, LearningSide)>, String> {
if store.path().exists() {
return Ok(Vec::new());
}
let mut library = Library::default();
let mut sides = Vec::new();
let cancel = AtomicBool::new(false);
for (name, pgn, side) in STARTER_REPERTOIRES {
let db = PgnDatabase::from_bytes(pgn.as_bytes().to_vec(), None, &mut |_| {}, &cancel)
.map_err(|e| e.to_string())?;
let mut id = None;
for index in 0..db.len() {
let Some(parsed) = db.load_game(index) else {
continue;
};
let current = match &id {
Some(id) => id,
None => id.insert(
library
.create(tr(name), &Game::from_start(parsed.game.start().clone()))
.map_err(|e| e.to_string())?,
),
};
library
.add_game(current, &parsed.game)
let marks_path = starter_marks_path(store);
let (mut given, marked) = match read_starter_marks(&marks_path)? {
StarterMarksState::Newer => return Ok(Vec::new()),
StarterMarksState::Missing => (BTreeSet::new(), false),
StarterMarksState::Given(given) => (given, true),
};
let mut attempts = 0;
let sides = loop {
attempts += 1;
let mut library = store.load().map_err(|e| e.to_string())?;
for (key, name, ..) in &STARTER_REPERTOIRES {
let names = crate::i18n::all_languages(name);
if library
.repertoires
.iter()
.any(|r| names.contains(&r.name.as_str()))
{
given.insert((*key).to_owned());
}
}
let missing: Vec<_> = STARTER_REPERTOIRES
.iter()
.filter(|(key, ..)| !given.contains(*key))
.collect();
if missing.is_empty() {
break Vec::new();
}
let mut sides = Vec::new();
let cancel = AtomicBool::new(false);
for (_, name, pgn, side) in &missing {
let db = PgnDatabase::from_bytes(pgn.as_bytes().to_vec(), None, &mut |_| {}, &cancel)
.map_err(|e| e.to_string())?;
let mut id = None;
for index in 0..db.len() {
let Some(parsed) = db.load_game(index) else {
continue;
};
let current = match &id {
Some(id) => id,
None => id.insert(
library
.create(tr(name), &Game::from_start(parsed.game.start().clone()))
.map_err(|e| e.to_string())?,
),
};
library
.add_game(current, &parsed.game)
.map_err(|e| e.to_string())?;
}
if let Some(id) = id {
sides.push((id, *side));
}
}
if let Some(id) = id {
sides.push((id, side));
match store.save(&mut library) {
Ok(()) => {
given.extend(missing.iter().map(|(key, ..)| (*key).to_owned()));
break sides;
}
Err(StoreError::Conflict { .. }) if attempts < 3 => continue,
Err(e) => return Err(e.to_string()),
}
};
if marked && sides.is_empty() {
return Ok(sides);
}
let marks = StarterMarks {
version: STARTER_MARKS_VERSION,
given,
};
let bytes = serde_json::to_vec_pretty(&marks).map_err(|e| e.to_string())?;
// The library is already saved; without the marks the names still keep the examples
// from being added again.
if let Err(e) = crate::settings::write_atomic(&marks_path, &bytes) {
eprintln!("{}: {e}", marks_path.display());
}
store.save(&mut library).map_err(|e| e.to_string())?;
Ok(sides)
}

View file

@ -346,7 +346,11 @@ fn the_three_screens_keep_the_board_and_the_panel_in_one_place() {
("London", "1. d4 d5 2. Bf4 *"),
],
);
for (size, left) in [(vec2(1400.0, 900.0), 940.0), (vec2(960.0, 600.0), 576.0)] {
for (size, left) in [
(vec2(1920.0, 1080.0), 1360.0),
(vec2(1400.0, 900.0), 896.0),
(vec2(960.0, 600.0), 580.0),
] {
let mut ui = dir.ui();
let mut settings = Settings::default();
let ctx = egui::Context::default();
@ -532,17 +536,33 @@ fn starter_repertoires_are_created_once_with_their_colours() {
let (store, _) = dir.stores();
let sides = seed_starter(&store).unwrap();
let library = store.load().unwrap();
assert_eq!(library.repertoires.len(), 2);
assert_eq!(library.repertoires.len(), 4);
assert_eq!(
sides.iter().map(|(_, side)| *side).collect::<Vec<_>>(),
[LearningSide::White, LearningSide::Black]
[
LearningSide::White,
LearningSide::Black,
LearningSide::White,
LearningSide::Black
]
);
for ((id, _), lines) in sides.iter().zip([3, 3]) {
for (id, _) in &sides {
let repertoire = library.repertoire(id).unwrap();
assert_eq!(layout::lines(&repertoire.game(), |_| false).len(), lines);
assert!(!layout::lines(&repertoire.game(), |_| false).is_empty());
}
assert_eq!(library.repertoires[0].name, "Spanish Opening");
assert_eq!(library.repertoires[1].name, "Sicilian Defence: Najdorf");
assert_eq!(
library
.repertoires
.iter()
.map(|r| r.name.as_str())
.collect::<Vec<_>>(),
[
"Spanish Opening",
"Sicilian Defence: Najdorf",
"Queen's Gambit",
"Semi-Slav: Triangle"
]
);
let mut settings = Settings::default();
let mut ui = dir.ui();
@ -554,14 +574,242 @@ fn starter_repertoires_are_created_once_with_their_colours() {
assert_eq!(layout::side_of(&settings, black), LearningSide::Black);
assert!(seed_starter(&store).unwrap().is_empty());
assert_eq!(store.load().unwrap().repertoires.len(), 2);
assert_eq!(store.load().unwrap().repertoires.len(), 4);
}
#[test]
fn starter_repertoires_do_not_return_to_an_existing_library() {
fn starter_repertoires_come_to_an_empty_library_of_2_3() {
let dir = TestDir::new("starter-existing");
let (store, _) = dir.stores();
store.save(&mut Library::default()).unwrap();
assert_eq!(seed_starter(&store).unwrap().len(), 4);
assert_eq!(store.load().unwrap().repertoires.len(), 4);
}
#[test]
fn starter_examples_of_2_4_0_are_not_doubled() {
let dir = TestDir::new("starter-240");
// 2.4.0 created the examples in the interface language and kept no marks.
save_library(
&dir,
&[
("Испанская партия", "1. e4 e5 *"),
("Sicilian Defence: Najdorf", "1. e4 c5 *"),
],
);
let (store, _) = dir.stores();
let sides = seed_starter(&store).unwrap();
assert_eq!(
sides.iter().map(|(_, side)| *side).collect::<Vec<_>>(),
[LearningSide::White, LearningSide::Black]
);
let names: Vec<_> = store
.load()
.unwrap()
.repertoires
.into_iter()
.map(|r| r.name)
.collect();
assert_eq!(
names,
[
"Испанская партия",
"Sicilian Defence: Najdorf",
"Queen's Gambit",
"Semi-Slav: Triangle"
]
);
assert!(seed_starter(&store).unwrap().is_empty());
}
#[test]
fn a_deleted_starter_does_not_come_back() {
let dir = TestDir::new("starter-deleted");
let (store, _) = dir.stores();
seed_starter(&store).unwrap();
let mut library = store.load().unwrap();
let id = library.repertoires[2].id.clone();
library.delete(&id).unwrap();
store.save(&mut library).unwrap();
assert!(seed_starter(&store).unwrap().is_empty());
let library = store.load().unwrap();
assert_eq!(library.repertoires.len(), 3);
assert!(
library
.repertoires
.iter()
.all(|r| r.name != "Queen's Gambit")
);
}
#[test]
fn starters_leave_own_repertoires_and_progress_alone() {
let dir = TestDir::new("starter-own");
let ids = save_library(&dir, &[("Mine", "1. d4 d5 2. c4 e6 *")]);
let (store, learning) = dir.stores();
std::fs::write(learning.path(), b"progress bytes").unwrap();
let before = serde_json::to_value(store.load().unwrap().repertoire(&ids[0]).unwrap()).unwrap();
assert_eq!(seed_starter(&store).unwrap().len(), 4);
let library = store.load().unwrap();
assert_eq!(library.repertoires.len(), 5);
assert_eq!(
serde_json::to_value(library.repertoire(&ids[0]).unwrap()).unwrap(),
before
);
assert_eq!(std::fs::read(learning.path()).unwrap(), b"progress bytes");
}
#[test]
fn starters_are_not_doubled_when_their_marks_were_lost() {
let dir = TestDir::new("starter-lost-marks");
let (store, _) = dir.stores();
seed_starter(&store).unwrap();
// A failed write of the marks, or marks from another copy that did not see them.
std::fs::remove_file(starter_marks_path(&store)).unwrap();
assert!(seed_starter(&store).unwrap().is_empty());
std::fs::write(starter_marks_path(&store), br#"{"version":1,"given":[]}"#).unwrap();
assert!(seed_starter(&store).unwrap().is_empty());
assert_eq!(store.load().unwrap().repertoires.len(), 4);
}
#[test]
fn two_copies_starting_together_give_the_starters_once() {
for round in 0..20 {
let dir = TestDir::new(&format!("starter-race-{round}"));
let (store, _) = dir.stores();
let results: Vec<_> = std::thread::scope(|s| {
let a = s.spawn(|| seed_starter(&store.clone()));
let b = s.spawn(|| seed_starter(&store.clone()));
vec![a.join().unwrap(), b.join().unwrap()]
});
let added: usize = results.iter().map(|r| r.as_ref().unwrap().len()).sum();
assert_eq!(added, 4);
assert_eq!(store.load().unwrap().repertoires.len(), 4);
}
}
#[test]
fn damaged_starter_marks_give_a_short_message_and_change_nothing() {
let dir = TestDir::new("starter-damaged");
let (store, _) = dir.stores();
store.save(&mut Library::default()).unwrap();
let path = starter_marks_path(&store);
std::fs::write(&path, br#"{"version":1,"given":["#).unwrap();
let error = seed_starter(&store).unwrap_err();
assert_eq!(
error,
"The record of example repertoires is damaged; examples were not added"
);
assert!(!error.contains(&*dir.0.to_string_lossy()));
assert_eq!(std::fs::read(&path).unwrap(), br#"{"version":1,"given":["#);
assert!(store.load().unwrap().repertoires.is_empty());
}
#[test]
fn starter_marks_of_an_unknown_version_are_left_alone() {
let dir = TestDir::new("starter-newer");
let (store, _) = dir.stores();
store.save(&mut Library::default()).unwrap();
let path = starter_marks_path(&store);
std::fs::write(&path, br#"{"version":99,"given":5}"#).unwrap();
assert!(seed_starter(&store).unwrap().is_empty());
assert_eq!(
std::fs::read(&path).unwrap(),
br#"{"version":99,"given":5}"#
);
assert!(store.load().unwrap().repertoires.is_empty());
}
#[test]
fn the_panel_opens_at_the_width_the_user_dragged_it_to() {
let dir = TestDir::new("side-width");
save_library(&dir, &[("Italian", "1. e4 e5 2. Nf3 Nc6 3. Bc4 *")]);
let size = vec2(1920.0, 1080.0);
for (kept, width) in [
(Some(400.0_f32), 400.0),
(Some(100.0), 360.0),
(None, 560.0),
] {
let mut ui = dir.ui();
let mut settings = Settings::default();
let ctx = egui::Context::default();
crate::assets::install(&ctx);
// As the app does: the saved width is given once the context runs.
ctx.run_ui(egui::RawInput::default(), |_| {})
.drop_without_applying_deltas();
if let Some(kept) = kept {
ctx.data_mut(|d| {
d.insert_temp(egui::Id::new(crate::opening_layout::SIDE_WIDTH_ID), kept)
});
}
settle(&mut ui, &ctx, &mut settings, size);
let rect = panel_rect(&ctx);
assert!((rect.width() - width).abs() <= 1.0, "{rect:?} for {kept:?}");
}
}
#[test]
fn a_window_that_grows_gives_the_panel_the_width_of_its_final_size() {
let dir = TestDir::new("side-grow");
save_library(&dir, &[("Italian", "1. e4 e5 2. Nf3 Nc6 3. Bc4 *")]);
let mut ui = dir.ui();
let mut settings = Settings::default();
let ctx = egui::Context::default();
crate::assets::install(&ctx);
settle(&mut ui, &ctx, &mut settings, vec2(960.0, 600.0));
settle(&mut ui, &ctx, &mut settings, vec2(1920.0, 1080.0));
let rect = panel_rect(&ctx);
assert!((rect.width() - 560.0).abs() <= 1.0, "{rect:?}");
}
#[test]
fn the_table_fits_the_least_panel_in_every_language() {
let ctx = egui::Context::default();
crate::assets::install(&ctx);
for lang in crate::i18n::Lang::ALL {
crate::i18n::set_lang(lang);
ctx.run_ui(egui::RawInput::default(), |root| {
egui::CentralPanel::default().show(root, |ui| {
// The table's width in a panel at its least: its frame takes 8 on each side.
let width = crate::opening_layout::SIDE_LEAST - 16.0;
let columns = table_widths(ui, width);
let gap = ui.spacing().item_spacing.x;
assert!(
columns.least(gap) <= width,
"{lang:?}: {columns:?} + gaps > {width}"
);
assert!(columns.name_least >= NAME_WANTED, "{lang:?}: {columns:?}");
// The bar, 6 px and the number from the left fit for any count, the
// header from the bar's left edge too; the bar keeps off «Today».
let body = |text: &str| style_width(ui, text, egui::TextStyle::Body);
for number in ["0/22", "12/22", "00/000"] {
assert!(
columns.start + columns.bar + MINI_BAR_GAP + body(number)
<= columns.passed - 4.0 + 0.5,
"{lang:?}: {number} in {columns:?}"
);
}
assert!(columns.start + body(tr("Passed")) <= columns.passed - 4.0 + 0.5);
assert!(
// Unless the header itself takes the whole column.
columns.start + gap >= PASSED_GAP - 0.5 || columns.start < 1.0,
"{lang:?}: {columns:?}"
);
});
})
.drop_without_applying_deltas();
}
crate::i18n::set_lang(crate::i18n::Lang::En);
}
#[test]
fn a_review_date_is_short_in_the_table() {
use chrono::{Local, TimeZone};
let now = Local
.with_ymd_and_hms(2026, 11, 27, 9, 0, 0)
.single()
.unwrap()
.timestamp();
assert_eq!(when_short(now + 3 * 60 * 60 + 30 * 60, now), "12:30");
assert_eq!(when_short(now + 40 * 24 * 60 * 60, now), "06.01");
}

View file

@ -122,6 +122,9 @@ pub struct Settings {
pub sound_set: SoundSet,
pub volume: f32,
pub dark_mode: bool,
/// The width of the opening-training panel dragged by the user, in points; `None`:
/// the default for the window.
pub opening_side_width: Option<f32>,
pub language: Lang,
pub engines: Vec<EngineConfig>,
pub active_engine: Option<usize>,
@ -221,6 +224,7 @@ impl Default for Settings {
sound_set: SoundSet::default(),
volume: 0.7,
dark_mode: true,
opening_side_width: None,
language: Lang::default(),
engines: Vec::new(),
active_engine: None,