User comments: an editor in the notation, symbols, arrows on the board
Enter on a move (the move's menu, a double click on a comment, the ☰ menu) opens the editor where the comment stands: after any move, in a variation too (the flow is cut at the move), or at the start of the game; Enter — done, Esc — cancel, Shift+Enter — a paragraph; another move writes the draft; one edit — one undo step. Symbols of the move (! ? !! ?? !? ?!, Ctrl+1…6) and of the position in the editor and in the menu. The right mouse button draws arrows and circles on the board ([%cal]/[%csl] of the current move; colours by modifiers). Existing comments are edited and deleted; their commands and untouched bytes are kept. chess_core: with_text, with_shapes, shapes, Game::set_comments_before. Dev hooks CP_COMMENT_EDIT, CP_TYPE, CP_SHAPE. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
9967d6b759
commit
dc7d9f88b3
15 changed files with 1592 additions and 71 deletions
|
|
@ -20,6 +20,8 @@ pub struct Notation<'a> {
|
|||
pub preview: Option<(NodeId, &'a [String], usize)>,
|
||||
/// Translated comments by their original text (none: the original is shown).
|
||||
pub translations: Option<&'a HashMap<String, Translated>>,
|
||||
/// The comment being written, drawn where the comment stands (user-comments.md §3).
|
||||
pub editor: Option<&'a mut CommentEditor>,
|
||||
}
|
||||
|
||||
/// Result of a frame of the notation panel.
|
||||
|
|
@ -29,6 +31,14 @@ pub struct NotationResponse {
|
|||
pub context: Option<(NodeId, NotationCommand)>,
|
||||
/// A move of the better line of this marked move was clicked: (marked node, moves to show).
|
||||
pub best: Option<(NodeId, usize)>,
|
||||
/// Open the editor for this comment (a double click, the menu).
|
||||
pub edit: Option<Slot>,
|
||||
/// Delete this comment (the menu).
|
||||
pub delete: Option<Slot>,
|
||||
/// Put or take off this symbol on this move (the menu), at once.
|
||||
pub nag: Option<(NodeId, Nag)>,
|
||||
/// The editor's «Done» or «Cancel».
|
||||
pub editor: Option<EditorOutcome>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
|
|
@ -37,6 +47,121 @@ pub enum NotationCommand {
|
|||
Delete,
|
||||
}
|
||||
|
||||
/// Which comment of the game: after a move (`After(ROOT)`: the start of the game), or
|
||||
/// before a move (an introduction to a line, only an existing one).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Slot {
|
||||
After(NodeId),
|
||||
Before(NodeId),
|
||||
}
|
||||
|
||||
impl Slot {
|
||||
pub fn node(self) -> NodeId {
|
||||
match self {
|
||||
Slot::After(id) | Slot::Before(id) => id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum EditorOutcome {
|
||||
Done,
|
||||
Cancel,
|
||||
}
|
||||
|
||||
/// A comment being written: its draft text and symbols (user-comments.md §3.4).
|
||||
pub struct CommentEditor {
|
||||
pub slot: Slot,
|
||||
pub text: String,
|
||||
/// The text as the editor opened (unchanged: the comment's bytes stay).
|
||||
pub initial: String,
|
||||
/// The draft symbols of the move (all of them; the two groups are toggled).
|
||||
pub nags: Vec<Nag>,
|
||||
pub initial_nags: Vec<Nag>,
|
||||
/// «Comment after 4…Nf6», and its short form for a narrow panel.
|
||||
pub title: String,
|
||||
pub short_title: String,
|
||||
/// A translation of this comment was on the screen: the original is edited.
|
||||
pub translated: bool,
|
||||
/// Put the keyboard into the field (on opening, on Enter from the game).
|
||||
pub focus: bool,
|
||||
/// Scroll the editor into view (on opening).
|
||||
pub scroll: bool,
|
||||
/// The text field's id, to know whether it has the keyboard.
|
||||
pub field_id: Option<egui::Id>,
|
||||
}
|
||||
|
||||
impl CommentEditor {
|
||||
/// Something to write: the text or the symbols changed.
|
||||
pub fn changed(&self) -> bool {
|
||||
self.text.trim() != self.initial.trim() || self.nags != self.initial_nags
|
||||
}
|
||||
|
||||
pub fn text_changed(&self) -> bool {
|
||||
self.text.trim() != self.initial.trim()
|
||||
}
|
||||
}
|
||||
|
||||
/// The six symbols of a move and the eight of a position, as the editor and the move's
|
||||
/// menu offer them (user-comments.md §3.4): (NAG, symbol, name).
|
||||
pub const MOVE_NAGS: [(Nag, &str, &str); 6] = [
|
||||
(Nag(1), "!", "Good move"),
|
||||
(Nag(2), "?", "Mistake"),
|
||||
(Nag(3), "!!", "Brilliant move"),
|
||||
(Nag(4), "??", "Blunder"),
|
||||
(Nag(5), "!?", "Interesting move"),
|
||||
(Nag(6), "?!", "Dubious move"),
|
||||
];
|
||||
pub const POSITION_NAGS: [(Nag, &str, &str); 8] = [
|
||||
(Nag(10), "=", "Equal position"),
|
||||
(Nag(13), "∞", "Unclear position"),
|
||||
(Nag(14), "⩲", "White is slightly better"),
|
||||
(Nag(15), "⩱", "Black is slightly better"),
|
||||
(Nag(16), "±", "White is better"),
|
||||
(Nag(17), "∓", "Black is better"),
|
||||
(Nag(18), "+−", "White is winning"),
|
||||
(Nag(19), "−+", "Black is winning"),
|
||||
];
|
||||
|
||||
fn is_position_nag(n: Nag) -> bool {
|
||||
(10..=19).contains(&n.0)
|
||||
}
|
||||
|
||||
/// `nag` put on or taken off `nags`: one symbol of its group (move or position) at a
|
||||
/// time; the same symbol again takes it off; the other symbols stay.
|
||||
pub fn toggle_nag(nags: &[Nag], nag: Nag) -> Vec<Nag> {
|
||||
let same_group = |n: &Nag| {
|
||||
if nag.is_move_assessment() {
|
||||
n.is_move_assessment()
|
||||
} else {
|
||||
is_position_nag(*n)
|
||||
}
|
||||
};
|
||||
let had = nags
|
||||
.iter()
|
||||
.any(|n| *n == nag || (n.0 >= 10 && n.0 <= 12 && nag.0 == 10));
|
||||
let mut out: Vec<Nag> = nags.iter().copied().filter(|n| !same_group(n)).collect();
|
||||
if !had {
|
||||
out.push(nag);
|
||||
out.sort();
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The symbol of a group that is on: `=` for $10–$12.
|
||||
fn group_current(nags: &[Nag], position: bool) -> Option<Nag> {
|
||||
nags.iter()
|
||||
.copied()
|
||||
.find(|n| {
|
||||
if position {
|
||||
is_position_nag(*n)
|
||||
} else {
|
||||
n.is_move_assessment()
|
||||
}
|
||||
})
|
||||
.map(|n| if (10..=12).contains(&n.0) { Nag(10) } else { n })
|
||||
}
|
||||
|
||||
const ROW_H: f32 = 26.0;
|
||||
const NUM_W: f32 = 38.0;
|
||||
/// A move's cell is no wider than this: in a wide panel (the notation under the board)
|
||||
|
|
@ -46,16 +171,26 @@ const CELL_MAX: f32 = 160.0;
|
|||
const MARK_LINE_MOVES: usize = 6;
|
||||
|
||||
impl Notation<'_> {
|
||||
pub fn show(self, ui: &mut Ui) -> NotationResponse {
|
||||
pub fn show(mut self, ui: &mut Ui) -> NotationResponse {
|
||||
let mut out = NotationResponse::default();
|
||||
let mut editor = self.editor.take();
|
||||
let game = self.game;
|
||||
let editing = editor.as_ref().map(|e| e.slot);
|
||||
let root_comments = comment_text(
|
||||
game.node(NodeId::ROOT)
|
||||
.map(|n| n.comments_after())
|
||||
.unwrap_or(&[]),
|
||||
);
|
||||
if !root_comments.is_empty() {
|
||||
comment_block(ui, &self, &root_comments);
|
||||
if editing == Some(Slot::After(NodeId::ROOT)) {
|
||||
editor_block(ui, &mut editor, &mut out);
|
||||
} else if !root_comments.is_empty() {
|
||||
comment_block(
|
||||
ui,
|
||||
&self,
|
||||
&root_comments,
|
||||
Slot::After(NodeId::ROOT),
|
||||
&mut out,
|
||||
);
|
||||
}
|
||||
|
||||
let mut row = Row::default();
|
||||
|
|
@ -63,9 +198,12 @@ impl Notation<'_> {
|
|||
while let Some(child) = game.main_child(node) {
|
||||
let Some(n) = game.node(child) else { break };
|
||||
let before = comment_text(n.comments_before());
|
||||
if !before.is_empty() {
|
||||
if editing == Some(Slot::Before(child)) {
|
||||
row.flush(ui, &self, &mut out);
|
||||
comment_block(ui, &self, &before);
|
||||
editor_block(ui, &mut editor, &mut out);
|
||||
} else if !before.is_empty() {
|
||||
row.flush(ui, &self, &mut out);
|
||||
comment_block(ui, &self, &before, Slot::Before(child), &mut out);
|
||||
}
|
||||
let (num, color) = game.move_number(child).unwrap_or((0, Color::White));
|
||||
if color == Color::White {
|
||||
|
|
@ -81,21 +219,27 @@ impl Notation<'_> {
|
|||
let after = comment_text(n.comments_after());
|
||||
let vars = game.variations(node);
|
||||
let mark = self.marks.and_then(|m| m.get(&child));
|
||||
let interrupted =
|
||||
!after.is_empty() || !vars.is_empty() || !n.unparsed().is_empty() || mark.is_some();
|
||||
let editing_after = editing == Some(Slot::After(child));
|
||||
let interrupted = editing_after
|
||||
|| !after.is_empty()
|
||||
|| !vars.is_empty()
|
||||
|| !n.unparsed().is_empty()
|
||||
|| mark.is_some();
|
||||
if interrupted || color == Color::Black {
|
||||
row.flush(ui, &self, &mut out);
|
||||
if let Some(mark) = mark {
|
||||
mark_block(ui, &self, child, mark, &mut out);
|
||||
}
|
||||
if !after.is_empty() {
|
||||
comment_block(ui, &self, &after);
|
||||
if editing_after {
|
||||
editor_block(ui, &mut editor, &mut out);
|
||||
} else if !after.is_empty() {
|
||||
comment_block(ui, &self, &after, Slot::After(child), &mut out);
|
||||
}
|
||||
for u in n.unparsed() {
|
||||
unparsed_block(ui, u);
|
||||
}
|
||||
for &v in vars {
|
||||
variation_block(ui, &self, v, &mut out);
|
||||
variation_block(ui, &self, v, &mut editor, &mut out);
|
||||
}
|
||||
}
|
||||
node = child;
|
||||
|
|
@ -182,7 +326,45 @@ fn handle_move_response(
|
|||
resp.scroll_to_me(Some(egui::Align::Center));
|
||||
}
|
||||
let is_variation = n.game.child_index(id).is_some_and(|i| i > 0) || !n.game.is_mainline(id);
|
||||
// The menu is about the move it opens on: the move becomes the current one.
|
||||
if resp.secondary_clicked() && id != n.cursor {
|
||||
out.clicked = Some(id);
|
||||
}
|
||||
resp.context_menu(|ui| {
|
||||
let node = n.game.node(id);
|
||||
let has_comment = node.is_some_and(|nd| !comment_text(nd.comments_after()).is_empty());
|
||||
let label = if has_comment {
|
||||
tr("Edit comment…")
|
||||
} else {
|
||||
tr("Comment…")
|
||||
};
|
||||
if ui
|
||||
.add(egui::Button::new(label).shortcut_text("Enter"))
|
||||
.clicked()
|
||||
{
|
||||
out.edit = Some(Slot::After(id));
|
||||
ui.close();
|
||||
}
|
||||
if has_comment && ui.button(tr("Delete comment")).clicked() {
|
||||
out.delete = Some(Slot::After(id));
|
||||
ui.close();
|
||||
}
|
||||
// The symbols act at once and keep the menu open: «?» off, «?!» on.
|
||||
let nags = node.map(|nd| nd.nags().to_vec()).unwrap_or_default();
|
||||
symbol_row(ui, &nags, &MOVE_NAGS, true, &mut |nag| {
|
||||
out.nag = Some((id, nag))
|
||||
});
|
||||
symbol_row(ui, &nags, &POSITION_NAGS, false, &mut |nag| {
|
||||
out.nag = Some((id, nag))
|
||||
});
|
||||
if n.game.main_child(NodeId::ROOT) == Some(id) {
|
||||
ui.separator();
|
||||
if ui.button(tr("Comment at the start of the game…")).clicked() {
|
||||
out.edit = Some(Slot::After(NodeId::ROOT));
|
||||
ui.close();
|
||||
}
|
||||
}
|
||||
ui.separator();
|
||||
if is_variation && ui.button(tr("Make main line")).clicked() {
|
||||
out.context = Some((id, NotationCommand::Promote));
|
||||
ui.close();
|
||||
|
|
@ -195,6 +377,44 @@ fn handle_move_response(
|
|||
let _ = ui;
|
||||
}
|
||||
|
||||
/// A row of symbol toggles: the one on is pressed; `on` gets the symbol clicked.
|
||||
fn symbol_row(
|
||||
ui: &mut Ui,
|
||||
nags: &[Nag],
|
||||
set: &[(Nag, &str, &'static str)],
|
||||
moves: bool,
|
||||
on: &mut dyn FnMut(Nag),
|
||||
) {
|
||||
let current = group_current(nags, !moves);
|
||||
ui.horizontal(|ui| {
|
||||
ui.spacing_mut().item_spacing.x = 2.0;
|
||||
for (nag, sym, name) in set {
|
||||
let mut text = RichText::new(*sym).size(15.0);
|
||||
if moves && let Some(c) = assessment_color(*nag) {
|
||||
text = text.color(c);
|
||||
}
|
||||
let r = ui
|
||||
.add_sized(
|
||||
[28.0, 24.0],
|
||||
egui::Button::selectable(current == Some(*nag), text),
|
||||
)
|
||||
.on_hover_text(symbol_hint(*nag, name));
|
||||
if r.clicked() {
|
||||
on(*nag);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// «Good move (Ctrl+1)».
|
||||
fn symbol_hint(nag: Nag, name: &'static str) -> String {
|
||||
if nag.is_move_assessment() {
|
||||
format!("{} (Ctrl+{})", tr(name), nag.0)
|
||||
} else {
|
||||
tr(name).to_owned()
|
||||
}
|
||||
}
|
||||
|
||||
fn paint_move_cell(
|
||||
ui: &Ui,
|
||||
cell: egui::Rect,
|
||||
|
|
@ -457,7 +677,7 @@ fn mark_info(
|
|||
/// Room kept on the right of a comment for its translation mark.
|
||||
const MARK_ROOM: f32 = 18.0;
|
||||
|
||||
fn comment_block(ui: &mut Ui, n: &Notation, text: &str) {
|
||||
fn comment_block(ui: &mut Ui, n: &Notation, text: &str, slot: Slot, out: &mut NotationResponse) {
|
||||
let (shown, mark) = translated(n, text);
|
||||
let mut frame = block_frame(ui);
|
||||
if mark.is_some() {
|
||||
|
|
@ -468,16 +688,190 @@ fn comment_block(ui: &mut Ui, n: &Notation, text: &str) {
|
|||
bottom: 6,
|
||||
});
|
||||
}
|
||||
let hovered = ui.rect_contains_pointer(ui.available_rect_before_wrap());
|
||||
let r = frame.show(ui, |ui| {
|
||||
ui.set_width(ui.available_width());
|
||||
ui.label(RichText::new(shown).color(comment_color(ui)).size(14.5));
|
||||
let resp = ui.add(
|
||||
egui::Label::new(RichText::new(shown).color(comment_color(ui)).size(14.5))
|
||||
.sense(Sense::click()),
|
||||
);
|
||||
comment_response(&resp, slot, out);
|
||||
});
|
||||
let _ = hovered;
|
||||
if let Some(m) = mark {
|
||||
let at = r.response.rect.right_top() + vec2(-8.0, 7.0);
|
||||
translation_mark(ui, at, m, text);
|
||||
}
|
||||
}
|
||||
|
||||
/// A double click opens the comment for editing; the right button offers that and
|
||||
/// deleting (user-comments.md §5.2).
|
||||
fn comment_response(resp: &egui::Response, slot: Slot, out: &mut NotationResponse) {
|
||||
if resp.hovered() {
|
||||
resp.ctx.set_cursor_icon(egui::CursorIcon::Text);
|
||||
}
|
||||
if resp.double_clicked() {
|
||||
out.edit = Some(slot);
|
||||
}
|
||||
resp.context_menu(|ui| {
|
||||
if ui.button(tr("Edit comment…")).clicked() {
|
||||
out.edit = Some(slot);
|
||||
ui.close();
|
||||
}
|
||||
if ui.button(tr("Delete comment")).clicked() {
|
||||
out.delete = Some(slot);
|
||||
ui.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// The comment editor on the spot of its comment (user-comments.md §3.4).
|
||||
fn editor_block(ui: &mut Ui, editor: &mut Option<&mut CommentEditor>, out: &mut NotationResponse) {
|
||||
let Some(ed) = editor.as_deref_mut() else {
|
||||
return;
|
||||
};
|
||||
let focused = ed
|
||||
.field_id
|
||||
.is_some_and(|id| ui.ctx().memory(|m| m.focused() == Some(id)));
|
||||
let sel = ui.visuals().selection;
|
||||
let stroke = if focused {
|
||||
Stroke::new(1.5, sel.stroke.color)
|
||||
} else {
|
||||
Stroke::new(1.0, ui.visuals().weak_text_color())
|
||||
};
|
||||
let width = ui.available_width();
|
||||
let narrow = width < 360.0;
|
||||
let r = egui::Frame::new()
|
||||
.fill(ui.visuals().extreme_bg_color)
|
||||
.inner_margin(egui::Margin::symmetric(10, 6))
|
||||
.stroke(stroke)
|
||||
.show(ui, |ui| {
|
||||
ui.set_width(ui.available_width());
|
||||
// The title and the buttons.
|
||||
ui.horizontal(|ui| {
|
||||
let buttons = if narrow { 2.0 * 30.0 } else { 150.0 };
|
||||
let room = ui.available_width() - buttons;
|
||||
let title = if narrow
|
||||
|| crate::game_view::text_width(ui, &ed.title, egui::TextStyle::Body) > room
|
||||
{
|
||||
&ed.short_title
|
||||
} else {
|
||||
&ed.title
|
||||
};
|
||||
ui.add(egui::Label::new(RichText::new(title).strong()).truncate());
|
||||
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
|
||||
if narrow {
|
||||
if ui.button("×").on_hover_text(tr("Cancel (Esc)")).clicked() {
|
||||
out.editor = Some(EditorOutcome::Cancel);
|
||||
}
|
||||
if crate::widgets::primary(ui, "✔")
|
||||
.on_hover_text(tr("Done (Enter)"))
|
||||
.clicked()
|
||||
{
|
||||
out.editor = Some(EditorOutcome::Done);
|
||||
}
|
||||
} else {
|
||||
if ui.button(tr("Cancel")).on_hover_text("Esc").clicked() {
|
||||
out.editor = Some(EditorOutcome::Cancel);
|
||||
}
|
||||
if crate::widgets::primary(ui, tr("Done"))
|
||||
.on_hover_text("Enter")
|
||||
.clicked()
|
||||
{
|
||||
out.editor = Some(EditorOutcome::Done);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
// The symbols: the move's six, then the position's eight (a «±▾» menu when
|
||||
// they do not fit on the line). Not for the start of the game.
|
||||
if ed.slot != Slot::After(NodeId::ROOT) && !matches!(ed.slot, Slot::Before(_)) {
|
||||
let both = 14.0 * 30.0 + 24.0 <= ui.available_width();
|
||||
ui.horizontal(|ui| {
|
||||
let mut toggled = None;
|
||||
symbol_row(ui, &ed.nags, &MOVE_NAGS, true, &mut |nag| {
|
||||
toggled = Some(nag)
|
||||
});
|
||||
if both {
|
||||
ui.separator();
|
||||
symbol_row(ui, &ed.nags, &POSITION_NAGS, false, &mut |nag| {
|
||||
toggled = Some(nag)
|
||||
});
|
||||
} else {
|
||||
let current = group_current(&ed.nags, true)
|
||||
.and_then(|n| n.symbol())
|
||||
.unwrap_or("±");
|
||||
ui.menu_button(format!("{current}▾"), |ui| {
|
||||
symbol_row(ui, &ed.nags, &POSITION_NAGS[..4], false, &mut |nag| {
|
||||
toggled = Some(nag)
|
||||
});
|
||||
symbol_row(ui, &ed.nags, &POSITION_NAGS[4..], false, &mut |nag| {
|
||||
toggled = Some(nag)
|
||||
});
|
||||
})
|
||||
.response
|
||||
.on_hover_text(tr("Position assessment"));
|
||||
}
|
||||
if let Some(nag) = toggled {
|
||||
ed.nags = toggle_nag(&ed.nags, nag);
|
||||
}
|
||||
});
|
||||
}
|
||||
// The text: 2 to 12 lines, then it scrolls inside.
|
||||
let color = comment_color(ui);
|
||||
let row_h = ui.fonts_mut(|f| f.row_height(&FontId::proportional(14.5)));
|
||||
let resp = egui::ScrollArea::vertical()
|
||||
.id_salt("comment_editor_scroll")
|
||||
.max_height(12.0 * row_h + 8.0)
|
||||
.auto_shrink([false, true])
|
||||
.show(ui, |ui| {
|
||||
ui.add(
|
||||
egui::TextEdit::multiline(&mut ed.text)
|
||||
.id_salt("comment_editor")
|
||||
.desired_rows(2)
|
||||
.desired_width(f32::INFINITY)
|
||||
.font(FontId::proportional(14.5))
|
||||
.text_color(color)
|
||||
.hint_text(tr("Your comment…")),
|
||||
)
|
||||
})
|
||||
.inner;
|
||||
ed.field_id = Some(resp.id);
|
||||
if std::mem::take(&mut ed.focus) {
|
||||
resp.request_focus();
|
||||
// The cursor at the end: one adds to a comment more often than replaces it.
|
||||
if let Some(mut state) = egui::TextEdit::load_state(ui.ctx(), resp.id) {
|
||||
let end = egui::text::CCursor::new(ed.text.chars().count());
|
||||
state
|
||||
.cursor
|
||||
.set_char_range(Some(egui::text::CCursorRange::one(end)));
|
||||
state.store(ui.ctx(), resp.id);
|
||||
}
|
||||
}
|
||||
if !narrow {
|
||||
ui.label(
|
||||
RichText::new(tr(
|
||||
"Enter — done · Esc — cancel · Shift+Enter — new paragraph",
|
||||
))
|
||||
.weak()
|
||||
.small(),
|
||||
);
|
||||
}
|
||||
if ed.translated {
|
||||
ui.label(
|
||||
RichText::new(tr(
|
||||
"⇄ You are editing the original; its translation will be dropped",
|
||||
))
|
||||
.weak()
|
||||
.small(),
|
||||
);
|
||||
}
|
||||
});
|
||||
if std::mem::take(&mut ed.scroll) {
|
||||
r.response.scroll_to_me(None);
|
||||
}
|
||||
}
|
||||
|
||||
fn unparsed_block(ui: &mut Ui, text: &str) {
|
||||
block_frame(ui).show(ui, |ui| {
|
||||
ui.set_width(ui.available_width());
|
||||
|
|
@ -498,18 +892,125 @@ fn comment_color(ui: &Ui) -> Color32 {
|
|||
}
|
||||
}
|
||||
|
||||
fn variation_block(ui: &mut Ui, n: &Notation, start: NodeId, out: &mut NotationResponse) {
|
||||
/// One piece of a variation's flowing text.
|
||||
enum Token {
|
||||
Move {
|
||||
id: NodeId,
|
||||
label: String,
|
||||
color: Option<Color32>,
|
||||
},
|
||||
Comment {
|
||||
slot: Slot,
|
||||
text: String,
|
||||
},
|
||||
Unparsed(String),
|
||||
Open,
|
||||
Close,
|
||||
}
|
||||
|
||||
fn variation_block(
|
||||
ui: &mut Ui,
|
||||
n: &Notation,
|
||||
start: NodeId,
|
||||
editor: &mut Option<&mut CommentEditor>,
|
||||
out: &mut NotationResponse,
|
||||
) {
|
||||
let mut tokens = Vec::new();
|
||||
line_tokens(n, start, &mut tokens);
|
||||
let editing = editor.as_ref().map(|e| e.slot);
|
||||
// The flow is cut where the editor stands: after the move for «after», before it
|
||||
// for «before»; a comment being edited is replaced by the editor.
|
||||
let cut = editing.and_then(|slot| {
|
||||
tokens
|
||||
.iter()
|
||||
.position(|t| matches!(t, Token::Comment { slot: s, .. } if *s == slot))
|
||||
.map(|i| (i, true))
|
||||
.or_else(|| match slot {
|
||||
Slot::After(id) => tokens
|
||||
.iter()
|
||||
.position(|t| matches!(t, Token::Move { id: m, .. } if *m == id))
|
||||
.map(|i| (i + 1, false)),
|
||||
Slot::Before(id) => tokens
|
||||
.iter()
|
||||
.position(|t| matches!(t, Token::Move { id: m, .. } if *m == id))
|
||||
.map(|i| (i, false)),
|
||||
})
|
||||
});
|
||||
block_frame(ui).show(ui, |ui| {
|
||||
ui.set_width(ui.available_width());
|
||||
ui.horizontal_wrapped(|ui| {
|
||||
ui.spacing_mut().item_spacing = vec2(3.0, 2.0);
|
||||
inline_line(ui, n, start, out);
|
||||
});
|
||||
match cut {
|
||||
None => flow(ui, n, &tokens, out),
|
||||
Some((at, replaces)) => {
|
||||
flow(ui, n, &tokens[..at], out);
|
||||
editor_block(ui, editor, out);
|
||||
let rest = if replaces { at + 1 } else { at };
|
||||
flow(ui, n, &tokens[rest..], out);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Renders a line starting at `first` (a variation move) as flowing text.
|
||||
fn inline_line(ui: &mut Ui, n: &Notation, first: NodeId, out: &mut NotationResponse) {
|
||||
/// Lays out tokens as flowing text.
|
||||
fn flow(ui: &mut Ui, n: &Notation, tokens: &[Token], out: &mut NotationResponse) {
|
||||
if tokens.is_empty() {
|
||||
return;
|
||||
}
|
||||
ui.horizontal_wrapped(|ui| {
|
||||
ui.spacing_mut().item_spacing = vec2(3.0, 2.0);
|
||||
for t in tokens {
|
||||
match t {
|
||||
Token::Move { id, label, color } => {
|
||||
let current = *id == n.cursor;
|
||||
let mut text = RichText::new(label).size(15.0);
|
||||
if let Some(c) = color
|
||||
&& !current
|
||||
{
|
||||
text = text.color(*c);
|
||||
}
|
||||
// Same strong highlight as the main-line table, so the current move is
|
||||
// visible inside text.
|
||||
let button = if current {
|
||||
let sel = ui.visuals().selection;
|
||||
egui::Button::new(text.color(sel.stroke.color).strong())
|
||||
.fill(sel.bg_fill)
|
||||
.stroke(egui::Stroke::new(1.0, sel.stroke.color))
|
||||
} else {
|
||||
egui::Button::new(text).frame_when_inactive(false)
|
||||
};
|
||||
let resp = ui.add(button);
|
||||
handle_move_response(ui, &resp, n, *id, out);
|
||||
}
|
||||
Token::Comment { slot, text } => {
|
||||
let (shown, mark) = translated(n, text);
|
||||
let resp = ui.add(
|
||||
egui::Label::new(RichText::new(shown).color(comment_color(ui)).size(14.5))
|
||||
.sense(Sense::click()),
|
||||
);
|
||||
comment_response(&resp, *slot, out);
|
||||
if let Some(m) = mark {
|
||||
inline_mark(ui, m, text);
|
||||
}
|
||||
}
|
||||
Token::Unparsed(u) => {
|
||||
ui.label(
|
||||
RichText::new(u)
|
||||
.monospace()
|
||||
.color(ui.visuals().warn_fg_color),
|
||||
);
|
||||
}
|
||||
Token::Open => {
|
||||
ui.label(RichText::new("(").weak());
|
||||
}
|
||||
Token::Close => {
|
||||
ui.label(RichText::new(")").weak());
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// The tokens of a line starting at `first` (a variation move), nested lines inside.
|
||||
fn line_tokens(n: &Notation, first: NodeId, tokens: &mut Vec<Token>) {
|
||||
let game = n.game;
|
||||
let mut node = first;
|
||||
let mut need_number = true;
|
||||
|
|
@ -517,11 +1018,10 @@ fn inline_line(ui: &mut Ui, n: &Notation, first: NodeId, out: &mut NotationRespo
|
|||
let Some(nd) = game.node(node) else { return };
|
||||
let before = comment_text(nd.comments_before());
|
||||
if !before.is_empty() {
|
||||
let (shown, mark) = translated(n, &before);
|
||||
ui.label(RichText::new(shown).color(comment_color(ui)).size(14.5));
|
||||
if let Some(m) = mark {
|
||||
inline_mark(ui, m, &before);
|
||||
}
|
||||
tokens.push(Token::Comment {
|
||||
slot: Slot::Before(node),
|
||||
text: before,
|
||||
});
|
||||
need_number = true;
|
||||
}
|
||||
let (num, color) = game.move_number(node).unwrap_or((0, Color::White));
|
||||
|
|
@ -534,40 +1034,21 @@ fn inline_line(ui: &mut Ui, n: &Notation, first: NodeId, out: &mut NotationRespo
|
|||
(Color::Black, false) => format!("{san}{sym}"),
|
||||
};
|
||||
need_number = false;
|
||||
let current = node == n.cursor;
|
||||
let mut text = RichText::new(label).size(15.0);
|
||||
if let Some(c) = sym_color
|
||||
&& !current
|
||||
{
|
||||
text = text.color(c);
|
||||
}
|
||||
// Same strong highlight as the main-line table, so the current move is visible inside text.
|
||||
let button = if current {
|
||||
let sel = ui.visuals().selection;
|
||||
egui::Button::new(text.color(sel.stroke.color).strong())
|
||||
.fill(sel.bg_fill)
|
||||
.stroke(egui::Stroke::new(1.0, sel.stroke.color))
|
||||
} else {
|
||||
egui::Button::new(text).frame_when_inactive(false)
|
||||
};
|
||||
let resp = ui.add(button);
|
||||
handle_move_response(ui, &resp, n, node, out);
|
||||
|
||||
tokens.push(Token::Move {
|
||||
id: node,
|
||||
label,
|
||||
color: sym_color,
|
||||
});
|
||||
let after = comment_text(nd.comments_after());
|
||||
if !after.is_empty() {
|
||||
let (shown, mark) = translated(n, &after);
|
||||
ui.label(RichText::new(shown).color(comment_color(ui)).size(14.5));
|
||||
if let Some(m) = mark {
|
||||
inline_mark(ui, m, &after);
|
||||
}
|
||||
tokens.push(Token::Comment {
|
||||
slot: Slot::After(node),
|
||||
text: after,
|
||||
});
|
||||
need_number = true;
|
||||
}
|
||||
for u in nd.unparsed() {
|
||||
ui.label(
|
||||
RichText::new(u)
|
||||
.monospace()
|
||||
.color(ui.visuals().warn_fg_color),
|
||||
);
|
||||
tokens.push(Token::Unparsed(u.clone()));
|
||||
}
|
||||
// Alternatives to this move are printed right after it (PGN order),
|
||||
// except for the line's own first move whose siblings belong to the outer level.
|
||||
|
|
@ -575,9 +1056,9 @@ fn inline_line(ui: &mut Ui, n: &Notation, first: NodeId, out: &mut NotationRespo
|
|||
&& let Some(parent) = game.parent(node)
|
||||
{
|
||||
for &v in game.variations(parent) {
|
||||
ui.label(RichText::new("(").weak());
|
||||
inline_line(ui, n, v, out);
|
||||
ui.label(RichText::new(")").weak());
|
||||
tokens.push(Token::Open);
|
||||
line_tokens(n, v, tokens);
|
||||
tokens.push(Token::Close);
|
||||
need_number = true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue