Desktop: side panel content fits its panel (reference tab, engine settings, game header)

- Reference tab: the database box's text was shortened to the whole row (a wrapping row
  reports it as available), and an 8 pt gap stuck out at the end of a full row; the
  result bar was never given up. The content was 51 pt wider than the 320 pt panel, and
  egui moved the panel left, under the board, from a 1200 pt window down.
- Engine settings: the slider gets what the measured names and values leave; under
  100 pt the name and value share a line above a slider across the panel.
- Game header: the buttons are measured first; the title gets the rest and is shortened,
  the result always shows; under 160 pt for the title the buttons show icons.
- CP_PANEL_CHECK=1 reports a side panel wider than its place (a debug build also marks
  what widened it).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vrubelroman 2026-10-24 10:12:10 +03:00
parent b265e32da3
commit f65668c0b7
9 changed files with 516 additions and 118 deletions

View file

@ -301,13 +301,14 @@ impl GameView {
("game_side", (width * 0.4).clamp(260.0, 460.0).min(room))
};
let max = room.max(side_width).min((width * 0.7).max(240.0));
egui::Panel::right(side_id)
let side = egui::Panel::right(side_id)
.resizable(true)
.default_size(side_width)
.size_range(least.min(width * 0.5).min(max)..=max)
.show(ui, |ui| {
self.side_panel(ui, settings, analysis, reference, hub, &mut actions)
});
crate::widgets::check_panel(side_id, &side.response);
self.sync_analysis(analysis);
let hover = (self.side_tab == SideTab::Reference)
.then(|| reference.hover())
@ -445,9 +446,71 @@ impl GameView {
actions: &mut Vec<GameAction>,
) {
ui.add_space(4.0);
let warnings = self
.diagnostics
.iter()
.filter(|d| d.severity >= Severity::Warning)
.count();
let notes = (!self.diagnostics.is_empty()).then(|| {
if warnings > 0 {
RichText::new(format!("⚠ {warnings}")).color(ui.visuals().warn_fg_color)
} else {
RichText::new(format!("ℹ {}", self.diagnostics.len())).weak()
}
});
let back_to = self
.previous
.as_ref()
.map(|t| trf("⬅ Back to {game}", &[("game", t)]));
// The buttons are measured first: the title gets what they leave (shortened with
// "…"; the result always shows). When it would get under 160 pt, the buttons
// around it give up their words for their icons (the words go to the tooltips).
let gap = ui.spacing().item_spacing.x;
let button = |ui: &Ui, text: &str| {
text_width(ui, text, egui::TextStyle::Button)
+ 2.0 * ui.spacing().button_padding.x
+ gap
};
let left = |compact: bool| {
button(ui, tr("⬅ Library"))
+ back_to
.as_deref()
.map_or(0.0, |b| button(ui, if compact { "⬅" } else { b }))
+ gap
+ 2.0 * gap
};
let right = |compact: bool| {
button(ui, if compact { "⚙" } else { tr("⚙ Settings") })
+ self.update.as_deref().map_or(0.0, |v| button(ui, v))
+ button(ui, "⏷")
+ button(
ui,
if compact {
"🔍"
} else {
tr("🔍 Find in database")
},
)
+ notes.as_ref().map_or(0.0, |n| button(ui, n.text()))
};
let result = result_label(self.game.result());
let result_w = result.map_or(0.0, |r| {
ui.painter()
.layout_no_wrap(
format!(" {r} "),
egui::FontId::proportional(15.0),
Color32::WHITE,
)
.size()
.x
+ gap
});
let row = ui.available_width();
let compact = row - left(false) - right(false) - result_w < 160.0;
let title_room = (row - left(compact) - right(compact)).max(0.0);
ui.horizontal(|ui| {
if ui
.button(tr("⬅ Library"))
.button(if compact { "⬅" } else { tr("⬅ Library") })
.on_hover_text(if self.previous.is_some() {
tr("Back to the game list (Ctrl+W)")
} else {
@ -457,42 +520,58 @@ impl GameView {
{
actions.push(GameAction::Back);
}
if let Some(t) = &self.previous
if let Some(b) = &back_to
&& ui
.button(trf("⬅ Back to {game}", &[("game", t)]))
.on_hover_text(tr("The game this one was opened from, as you left it (Esc)"))
.button(if compact { "⬅" } else { b.as_str() })
.on_hover_text(if compact {
format!(
"{b}\n{}",
tr("The game this one was opened from, as you left it (Esc)")
)
} else {
tr("The game this one was opened from, as you left it (Esc)").to_owned()
})
.clicked()
{
actions.push(GameAction::ReturnToPrevious);
}
ui.separator();
ui.vertical(|ui| {
let modified = if self.game.is_modified() { " •" } else { "" };
ui.horizontal(|ui| {
ui.label(
RichText::new(format!("{}{modified}", self.title()))
.strong()
.size(16.0),
);
if let Some(result) = result_label(self.game.result()) {
ui.label(
RichText::new(format!(" {result} "))
.strong()
.size(15.0)
.background_color(ui.visuals().widgets.inactive.weak_bg_fill),
)
.on_hover_text(result_hint(self.game.result()));
let height = ui.available_height();
ui.allocate_ui_with_layout(
vec2(title_room, height),
egui::Layout::top_down(egui::Align::Min),
|ui| {
ui.set_width(title_room);
let modified = if self.game.is_modified() { " •" } else { "" };
ui.horizontal(|ui| {
ui.set_max_width(title_room);
let title = format!("{}{modified}", self.title());
ui.allocate_ui(vec2((title_room - result_w).max(0.0), height), |ui| {
ui.add(
egui::Label::new(RichText::new(&title).strong().size(16.0))
.truncate(),
);
});
if let Some(result) = result {
ui.label(
RichText::new(format!(" {result} "))
.strong()
.size(15.0)
.background_color(ui.visuals().widgets.inactive.weak_bg_fill),
)
.on_hover_text(result_hint(self.game.result()));
}
});
let sub = self.subtitle();
if !sub.is_empty() {
ui.add(egui::Label::new(RichText::new(sub).weak()).truncate());
}
});
let sub = self.subtitle();
if !sub.is_empty() {
ui.label(RichText::new(sub).weak());
}
});
},
);
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if ui
.button(tr("⚙ Settings"))
.on_hover_text("Ctrl+,")
.button(if compact { "⚙" } else { tr("⚙ Settings") })
.on_hover_text(if compact { format!("{} (Ctrl+,)", tr("Settings")) } else { "Ctrl+,".to_owned() })
.clicked()
{
actions.push(GameAction::OpenSettings);
@ -521,7 +600,7 @@ impl GameView {
.response
.on_hover_text(tr("Find in another database"));
if ui
.button(tr("🔍 Find in database"))
.button(if compact { "🔍" } else { tr("🔍 Find in database") })
.on_hover_text(tr("Find this position in a database (Ctrl+Shift+F)"))
.on_disabled_hover_text(if searchable { tr("Add a database first") } else { why })
.clicked()
@ -529,24 +608,13 @@ impl GameView {
actions.push(GameAction::FindPosition(None));
}
});
let warnings = self
.diagnostics
.iter()
.filter(|d| d.severity >= Severity::Warning)
.count();
if !self.diagnostics.is_empty() {
let text = if warnings > 0 {
RichText::new(format!("⚠ {warnings}")).color(ui.visuals().warn_fg_color)
} else {
RichText::new(format!("ℹ {}", self.diagnostics.len())).weak()
};
if ui
if let Some(text) = notes
&& ui
.button(text)
.on_hover_text(tr("PGN compatibility notes"))
.clicked()
{
self.show_diagnostics = true;
}
{
self.show_diagnostics = true;
}
});
});
@ -1569,53 +1637,91 @@ fn engine_settings(ui: &mut Ui, settings: &mut Settings, actions: &mut Vec<GameA
.unwrap_or(1);
let hash_steps = hash_steps();
let mut settled = false;
ui.spacing_mut().slider_width = (ui.available_width() - 230.0).max(80.0);
egui::Grid::new("engine_settings_grid")
.num_columns(3)
.spacing(vec2(12.0, 8.0))
.show(ui, |ui| {
// Search time
let mut t = SEARCH_TIMES
.iter()
.position(|s| *s == e.search_secs)
.unwrap_or(SEARCH_TIMES.len() - 1);
ui.label(tr("Search time"));
settled |= index_slider(ui, &mut t, SEARCH_TIMES.len());
e.search_secs = SEARCH_TIMES[t];
ui.label(
e.search_secs
.map(|s| trf("{n} s", &[("n", &s)]))
.unwrap_or_else(|| "∞".into()),
);
ui.end_row();
// Lines
let mut k = e.multipv.clamp(1, 5) as usize - 1;
ui.label(tr("Lines"));
settled |= index_slider(ui, &mut k, 5);
e.multipv = k as u32 + 1;
ui.label(format!("{} / 5", e.multipv));
ui.end_row();
// Threads
let mut th = e.threads.clamp(1, cores) as usize - 1;
ui.label(tr("Threads"));
settled |= index_slider(ui, &mut th, cores as usize);
e.threads = th as u32 + 1;
ui.label(format!("{} / {cores}", e.threads));
ui.end_row();
// Memory
let mut h = hash_steps
.iter()
.position(|m| *m >= e.hash_mb)
.unwrap_or(hash_steps.len() - 1);
ui.label(tr("Memory"));
settled |= index_slider(ui, &mut h, hash_steps.len());
e.hash_mb = hash_steps[h];
ui.label(format_mb(e.hash_mb));
ui.end_row();
});
// Each setting: its name, the slider, the value. The slider gets what the
// measured names and values leave; in a narrow panel (under 100 pt for it)
// the name and the value share a line above a slider across the panel.
let mut t = SEARCH_TIMES
.iter()
.position(|s| *s == e.search_secs)
.unwrap_or(SEARCH_TIMES.len() - 1);
let mut k = e.multipv.clamp(1, 5) as usize - 1;
let mut th = e.threads.clamp(1, cores) as usize - 1;
let mut h = hash_steps
.iter()
.position(|m| *m >= e.hash_mb)
.unwrap_or(hash_steps.len() - 1);
let search_value = |t: usize| {
SEARCH_TIMES[t]
.map(|s| trf("{n} s", &[("n", &s)]))
.unwrap_or_else(|| "∞".into())
};
let names = [tr("Search time"), tr("Lines"), tr("Threads"), tr("Memory")];
// The widest value each setting can show, so the slider does not jump.
let widest = [
(0..SEARCH_TIMES.len())
.map(search_value)
.collect::<Vec<_>>(),
vec!["5 / 5".to_owned()],
vec![format!("{cores} / {cores}")],
hash_steps.iter().map(|m| format_mb(*m)).collect(),
];
let font = egui::TextStyle::Body.resolve(ui.style());
let text_w = |t: &str| {
ui.painter()
.layout_no_wrap(t.to_owned(), font.clone(), Color32::WHITE)
.size()
.x
};
let name_w = names.iter().map(|n| text_w(n)).fold(0.0, f32::max);
let value_w = widest
.iter()
.flatten()
.map(|v| text_w(v))
.fold(0.0, f32::max);
let gap = 12.0;
let slider = (ui.available_width() - name_w - value_w - 2.0 * gap).floor();
let mut rows: [(&str, &mut usize, usize); 4] = [
(names[0], &mut t, SEARCH_TIMES.len()),
(names[1], &mut k, 5),
(names[2], &mut th, cores as usize),
(names[3], &mut h, hash_steps.len()),
];
let value = |i: usize, idx: usize| match i {
0 => search_value(idx),
1 => format!("{} / 5", idx + 1),
2 => format!("{} / {cores}", idx + 1),
_ => format_mb(hash_steps[idx]),
};
if slider >= 100.0 {
ui.spacing_mut().slider_width = slider;
egui::Grid::new("engine_settings_grid")
.num_columns(3)
.spacing(vec2(gap, 8.0))
.show(ui, |ui| {
for (i, (name, idx, len)) in rows.iter_mut().enumerate() {
ui.label(*name);
settled |= index_slider(ui, idx, *len);
ui.label(value(i, **idx));
ui.end_row();
}
});
} else {
ui.spacing_mut().slider_width = ui.available_width().floor();
for (i, (name, idx, len)) in rows.iter_mut().enumerate() {
ui.horizontal(|ui| {
ui.label(*name);
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
ui.label(value(i, **idx));
});
});
settled |= index_slider(ui, idx, *len);
ui.add_space(4.0);
}
}
e.search_secs = SEARCH_TIMES[t];
e.multipv = k as u32 + 1;
e.threads = th as u32 + 1;
e.hash_mb = hash_steps[h];
if settled {
actions.push(GameAction::EngineReconfigure);
}
@ -1737,3 +1843,11 @@ fn shape_color(c: ShapeColor) -> Color32 {
ShapeColor::Other(_) => Color32::from_rgba_unmultiplied(120, 120, 120, 170),
}
}
/// The width of `text` in `style` (as a label or a button would lay it out on one line).
pub(crate) fn text_width(ui: &Ui, text: &str, style: egui::TextStyle) -> f32 {
ui.painter()
.layout_no_wrap(text.to_owned(), style.resolve(ui.style()), Color32::WHITE)
.size()
.x
}

View file

@ -328,19 +328,21 @@ impl Library {
let db = db.clone();
match view.pane() {
Pane::Filters => {
egui::Panel::right("filters")
let r = egui::Panel::right("filters")
.resizable(true)
.default_size(280.0)
.size_range(260.0..=380.0)
.show(ui, |ui| view.panel(ui, &db));
crate::widgets::check_panel("filters", &r.response);
}
Pane::Board => {
let width = ui.available_width();
egui::Panel::right("search_board")
let r = egui::Panel::right("search_board")
.resizable(true)
.default_size((width * 0.5).max(340.0))
.size_range(340.0..=(width - 420.0).max(340.0))
.show(ui, |ui| view.board_panel(ui, &db, settings, &mut actions));
crate::widgets::check_panel("search_board", &r.response);
}
Pane::Hidden => {}
}

View file

@ -925,21 +925,27 @@ impl Reference {
// come a moment later; on this line they take no room of their own).
ui.horizontal_wrapped(|ui| {
ui.label(tr("Database"));
egui::ComboBox::from_id_salt("reference_db")
.selected_text(current_label)
.width(ui.available_width().min(340.0))
.truncate()
.show_ui(ui, |ui| {
for d in &self.dbs {
if ui.selectable_label(d.path == path, db_label(d)).clicked()
&& d.path != path
{
action = Some(RefAction::Choose(d.path.clone()));
// What is left of the row after the label, and the box shortens its text to
// that: in a wrapping row `available_width` is the whole row (with it a long
// name pushed the side panel out under the board).
let width = ui.available_size_before_wrap().x.min(340.0);
ui.allocate_ui(vec2(width, ui.spacing().interact_size.y), |ui| {
egui::ComboBox::from_id_salt("reference_db")
.selected_text(current_label)
.width(width)
.truncate()
.show_ui(ui, |ui| {
for d in &self.dbs {
if ui.selectable_label(d.path == path, db_label(d)).clicked()
&& d.path != path
{
action = Some(RefAction::Choose(d.path.clone()));
}
}
}
});
});
});
if let Some(node) = novelty {
ui.add_space(8.0);
row_gap(ui, 8.0);
ui.label(RichText::new(tr("Novelty:")).weak());
if ui
.link(node_label(game, node).unwrap_or_default())
@ -952,7 +958,7 @@ impl Reference {
}
}
if let (Some(c), Some(q)) = (copy, &query) {
ui.add_space(8.0);
row_gap(ui, 8.0);
ui.label(RichText::new(tr("This game is in the database")).weak());
if ui
.link(tr("open it"))
@ -1125,15 +1131,12 @@ impl Reference {
// As tall as its rows, but at most 40 % of the panel (at least 120 px of it).
let natural = 26.0 * (rows as f32 + 1.0) + 8.0;
let height = natural.min((ui.available_height() * 0.4).max(120.0));
// Columns that give way in a narrow panel: players, year, White's score, rating.
// Each is shown only while the table still fits the panel (in this order).
// Columns that give way in a narrow panel: players, year, White's score, rating,
// the result bar. Each is shown only while the table still fits the panel (in
// this order): a wider table would push the whole side panel under the board.
let width = ui.available_width();
let gap = ui.spacing().item_spacing.x;
let show_bar = !books;
let mut used = 96.0 + 64.0 + 44.0 + 3.0 * gap;
if show_bar {
used += 86.0 + gap;
}
let mut used = 96.0 + 64.0 + 44.0 + 2.0 * gap;
let mut fits = |w: f32, wanted: bool| {
let ok = wanted && used + w + gap <= width;
if ok {
@ -1141,6 +1144,7 @@ impl Reference {
}
ok
};
let show_bar = fits(86.0, !books);
let show_elo = fits(56.0, !books);
let show_score = fits(48.0, !books);
let show_year = fits(44.0, true);
@ -1596,6 +1600,14 @@ impl Reference {
}
}
/// A gap between items of a wrapping row, only where the row goes on: at the end of a
/// full row a plain `add_space` does not wrap but sticks out of the panel.
fn row_gap(ui: &mut Ui, gap: f32) {
if ui.available_size_before_wrap().x > gap + ui.spacing().item_spacing.x {
ui.add_space(gap);
}
}
#[cfg(test)]
mod tests {
use super::*;

View file

@ -2,6 +2,36 @@
use egui::{Color32, Sense, Stroke, Ui, vec2};
/// A dev check (`CP_PANEL_CHECK=1`): a panel whose content is wider than the panel. egui
/// then moves a right panel out (its left part under the central panel, its right part
/// off the window); the content of every panel must fit its least width
/// (docs/design/narrow-windows.md).
pub fn check_panel(name: &str, panel: &egui::Response) {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
let on = *ON.get_or_init(|| {
let on = std::env::var_os("CP_PANEL_CHECK").is_some();
// In a debug build egui also marks (red) what makes its parent wider.
#[cfg(debug_assertions)]
if on {
panel
.ctx
.all_styles_mut(|s| s.debug.show_expand_width = true);
}
on
});
if !on {
return;
}
let window = panel.ctx.content_rect();
let over = (panel.rect.right() - window.right()).max(window.left() - panel.rect.left());
if over > 0.5 {
eprintln!(
"CP_PANEL_CHECK: panel {name} is {over:.0} pt wider than its place (window {:.0} pt)",
window.width()
);
}
}
/// Compact ✕ (e.g. to cancel a running task); the caller adds the tooltip.
pub fn small_close_button(ui: &mut Ui) -> egui::Response {
x_button(ui, vec2(22.0, 20.0), 4.5)