From d54e359f76e3f78c731d23536c24730c9c1a44c6 Mon Sep 17 00:00:00 2001 From: vrubelroman Date: Fri, 27 Nov 2026 01:40:14 +0300 Subject: [PATCH] opening trainer: whole card frames, buttons never cut, path cut on the left, no arrow after a stopped session --- apps/desktop/src/i18n.rs | 11 +++++++ apps/desktop/src/opening_layout.rs | 47 +++++++++++++++++++++++++++++ apps/desktop/src/opening_lines.rs | 9 ++---- apps/desktop/src/opening_session.rs | 19 +++++++++--- apps/desktop/src/repertoire_ui.rs | 26 +++++++++------- 5 files changed, 90 insertions(+), 22 deletions(-) diff --git a/apps/desktop/src/i18n.rs b/apps/desktop/src/i18n.rs index 431e489..b5e6efd 100644 --- a/apps/desktop/src/i18n.rs +++ b/apps/desktop/src/i18n.rs @@ -315,6 +315,17 @@ const ROWS: &[[&str; 9]] = &[ "Yurishlar sanalmoqda…", "Жүрістер саналуда…", ], + [ + "Review all", + "Повторить всё", + "Tout réviser", + "Alles wiederholen", + "Repasar todo", + "Ripassa tutto", + "Revisar tudo", + "Hammasini takrorlash", + "Барлығын қайталау", + ], [ "Review all lines", "Повторить всё подряд", diff --git a/apps/desktop/src/opening_layout.rs b/apps/desktop/src/opening_layout.rs index 61df871..ffadbed 100644 --- a/apps/desktop/src/opening_layout.rs +++ b/apps/desktop/src/opening_layout.rs @@ -83,6 +83,12 @@ pub(crate) fn card(ui: &Ui, active: bool, alert: bool) -> egui::Frame { frame } +/// The content size of a [`card`] filling a block of `height`: the frame's margins and +/// its stroke stay inside the block. +pub(crate) fn card_inner(ui: &Ui, height: f32) -> egui::Vec2 { + vec2(ui.available_width() - 22.0, height - 14.0).max(egui::Vec2::ZERO) +} + /// A block of a fixed height: whatever it holds, what follows never moves. pub(crate) fn fixed_block(ui: &mut Ui, height: f32, add: impl FnOnce(&mut Ui) -> R) -> R { let width = ui.available_width().max(1.0); @@ -108,6 +114,47 @@ pub(crate) fn line(ui: &mut Ui, text: impl Into) -> egui::Response { } } +/// `text` cut on the left by whole words ("… 4. Ba4 Nf6") to fit `width` in `font`: +/// the last moves of a path matter most. +pub(crate) fn cut_left(ui: &Ui, text: &str, font: &egui::FontId, width: f32) -> String { + let fits = |text: &str| { + ui.fonts_mut(|fonts| { + fonts + .layout_no_wrap(text.to_owned(), font.clone(), Color32::WHITE) + .size() + .x + }) <= width + }; + if fits(text) { + return text.to_owned(); + } + let mut rest = text; + while let Some((_, tail)) = rest.split_once(' ') { + rest = tail; + let cut = format!("… {rest}"); + if fits(&cut) { + return cut; + } + } + format!("… {rest}") +} + +/// One line cut on the left ([`cut_left`]), the whole text on hover. +pub(crate) fn line_left(ui: &mut Ui, text: &str, strong: bool) -> egui::Response { + let font = egui::TextStyle::Body.resolve(ui.style()); + let shown = cut_left(ui, text, &font, ui.available_width()); + let mut rich = RichText::new(shown); + if strong { + rich = rich.strong(); + } + let response = ui.add(egui::Label::new(rich).truncate()); + if text.is_empty() { + response + } else { + response.on_hover_text(text) + } +} + /// The trained colour of a repertoire, kept in the settings since 2.2.0 (an old "both /// colours" repertoire opens for White). pub(crate) fn side_of(settings: &Settings, id: &RepertoireId) -> LearningSide { diff --git a/apps/desktop/src/opening_lines.rs b/apps/desktop/src/opening_lines.rs index a1f3b98..2f9fb60 100644 --- a/apps/desktop/src/opening_lines.rs +++ b/apps/desktop/src/opening_lines.rs @@ -292,7 +292,7 @@ impl OpeningLinesUi { let path = self.cursor_path(); let disabled = self.disabled_by(&path); layout::fixed_block(ui, SELECTED_H, |ui| { - let inner = vec2(ui.available_width() - 20.0, SELECTED_H - 12.0); + let inner = layout::card_inner(ui, SELECTED_H); layout::card(ui, false, false).show(ui, |ui| { ui.set_min_size(inner); ui.set_max_size(inner); @@ -300,12 +300,7 @@ impl OpeningLinesUi { if self.cursor == NodeId::ROOT { layout::line(ui, RichText::new(tr("Start position")).strong()); } else { - let label = self.path_label(self.cursor); - // Cut on the left: the last moves matter. - ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { - ui.set_max_height(20.0); - layout::line(ui, RichText::new(label).strong()); - }); + layout::line_left(ui, &self.path_label(self.cursor), true); } if disabled.is_some() { layout::line( diff --git a/apps/desktop/src/opening_session.rs b/apps/desktop/src/opening_session.rs index 9fe7337..6d5a174 100644 --- a/apps/desktop/src/opening_session.rs +++ b/apps/desktop/src/opening_session.rs @@ -36,6 +36,8 @@ const DEFAULT_ANIMATION: Duration = Duration::from_millis(250); /// Mistakes listed under «Moves to remember». const REMEMBER_ROWS: usize = 8; const CARD_H: f32 = 64.0; +/// The words of a branch's path in the header ("4. Ba4 Nf6" is three). +const BRANCH_WORDS: usize = 12; const LINE_H: f32 = 20.0; const ROW_H: f32 = 30.0; const TOOL_H: f32 = 28.0; @@ -74,7 +76,15 @@ impl SessionKind { LearningSide::White => tr("Review all lines · you play White").to_owned(), LearningSide::Black => tr("Review all lines · you play Black").to_owned(), }, - Self::Branch { label, .. } => trf("Branch: {path}", &[("path", label)]), + Self::Branch { label, .. } => { + // Cut on the left: the last moves name the branch. + let words: Vec<&str> = label.split_whitespace().collect(); + let path = match words.len().checked_sub(BRANCH_WORDS) { + Some(skip) if skip > 0 => format!("… {}", words[skip..].join(" ")), + _ => label.clone(), + }; + trf("Branch: {path}", &[("path", &path)]) + } } } } @@ -1464,7 +1474,7 @@ impl OpeningSessionUi { /// The move shown by an arrow: revealed by the core, or a new move about to be shown. fn revealed_uci(&self) -> Option { - if self.playback.is_some() || self.pending_automatic.is_some() { + if self.playback.is_some() || self.pending_automatic.is_some() || self.stopped { return None; } if let Some(uci) = self @@ -1570,7 +1580,8 @@ impl OpeningSessionUi { } fn more_new_count(&self) -> usize { - if self.kind != SessionKind::Daily { + // A stopped session still has questions: the card starts the next one. + if self.kind != SessionKind::Daily || self.stopped { return 0; } self.summary.as_ref().map_or(0, |summary| summary.more_new) @@ -2262,7 +2273,7 @@ struct CardView { fn show_card(ui: &mut Ui, view: &CardView) { layout::fixed_block(ui, CARD_H, |ui| { let frame = layout::card(ui, view.active, view.alert); - let inner = vec2(ui.available_width() - 20.0, CARD_H - 12.0); + let inner = layout::card_inner(ui, CARD_H); frame.show(ui, |ui| { ui.set_min_size(inner); ui.set_max_size(inner); diff --git a/apps/desktop/src/repertoire_ui.rs b/apps/desktop/src/repertoire_ui.rs index 5086db0..3236ef2 100644 --- a/apps/desktop/src/repertoire_ui.rs +++ b/apps/desktop/src/repertoire_ui.rs @@ -1524,11 +1524,11 @@ impl RepertoireUi { let name = self.selected_name(); let mut chosen = None; layout::fixed_block(ui, CARD_H, |ui| { - let inner = vec2(ui.available_width() - 20.0, CARD_H - 12.0); + 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); - ui.spacing_mut().item_spacing.y = 7.0; + ui.spacing_mut().item_spacing.y = 6.0; let width = ui.available_width(); // 1. The name and the colour. layout::fixed_block(ui, 26.0, |ui| { @@ -1564,16 +1564,19 @@ impl RepertoireUi { layout::fixed_block(ui, 30.0, |ui| { ui.horizontal(|ui| { let (label, enabled, item) = main_button(&state, side); - let third = matches!(state, CardState::OnlyNew { .. }); let gap = ui.spacing().item_spacing.x; + let pad = 2.0 * ui.spacing().button_padding.x + 2.0; let room = ui.available_width(); - let (main_w, lines_w, third_w) = - if MAIN_W + LINES_W + THIRD_W + 2.0 * gap <= room || !third { - (MAIN_W, LINES_W, THIRD_W) - } else { - let rest = ((room - MAIN_W - 2.0 * gap) / 2.0).max(60.0); - (MAIN_W.min(room / 2.0), rest, rest) - }; + let main_w = MAIN_W.min((room - LINES_W - gap).max(0.0)); + // Buttons are never cut: in a narrow panel the third one (always + // in the `⋯` menu too) stays out. + let third_need = text_width(ui, tr("Review all"), 14.0) + pad; + let lines_w = LINES_W.min(text_width(ui, tr("Lines…"), 14.0) + pad + 12.0); + let third_w = room - main_w - lines_w - 2.0 * gap; + let third = + matches!(state, CardState::OnlyNew { .. }) && third_w >= third_need; + let lines_w = if third { lines_w } else { LINES_W }; + let third_w = third_w.min(THIRD_W.max(third_need)); let response = ui .add_enabled_ui(enabled, |ui| { crate::widgets::primary_wide(ui, &label, main_w) @@ -1601,10 +1604,11 @@ impl RepertoireUi { if third && ui .add( - egui::Button::new(tr("Review all lines")) + 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);