Merge #14750 into the merge queue batch

Notes panel: empty state, "N / M" counter, author as stored, caret in a new note (part of #11793)
This commit is contained in:
David Mann committed 2026-11-20 02:04:37 +01:00
commit 983d4f85fb
2 files changed
+123 -9

No files matched your search

+38 -9
View File
@@ -84,6 +84,9 @@ pub struct AnalysisUi {
pub dialog: Option<(String, Map<String, Value>)>,
#[serde(skip)]
pub drag: Option<Drag>,
/// A note was just made with a click: the Notes panel focuses its text next frame (#11793).
#[serde(skip)]
pub focus_note_text: bool,
}
impl Default for AnalysisUi {
@@ -98,6 +101,7 @@ impl Default for AnalysisUi {
use_measurement_scale: false,
dialog: None,
drag: None,
focus_note_text: false,
}
}
}
@@ -332,6 +336,7 @@ pub(crate) fn create_note(app: &mut PhotocraftApp, x: f64, y: f64) -> Result<Val
let result = app.run("notes.add", params)?;
app.ui.analysis.note_selected = result["index"].as_u64().map(|v| v as usize);
app.ui.analysis.notes = true;
app.ui.analysis.focus_note_text = true;
app.ui.view.show.notes = true;
Ok(result)
}
@@ -1049,7 +1054,9 @@ fn show_note_options(app: &mut PhotocraftApp, i: usize) {
fn notes_panel(app: &mut PhotocraftApp, ctx: &egui::Context) {
let notes = app.session.active().map(|d| d.doc.notes.clone()).unwrap_or_default();
let n = notes.len();
let mut sel = app.ui.analysis.note_selected.filter(|i| *i < n).or((n > 0).then_some(0));
// No note selected (after Delete, Clear All, a new document) stays unselected (#11793).
let mut sel = app.ui.analysis.note_selected.filter(|i| *i < n);
let focus_text = std::mem::take(&mut app.ui.analysis.focus_note_text);
let mut close = false;
let mut edit: Option<Value> = None;
let mut delete = false;
@@ -1058,31 +1065,44 @@ fn notes_panel(app: &mut PhotocraftApp, ctx: &egui::Context) {
panel_window(app, ctx, "notes-panel", tl!("Notes"), vec2(-40.0, 300.0), 260.0, |ui| {
close = title_row(ui, tl!("Notes"));
match sel {
// No note selected: an empty, inactive text box with the reference's placeholder.
None => {
ui.label(RichText::new(tl!("Click with the Note tool to add a note.")).color(t.text_dim).size(11.0));
ui.label(RichText::new(" ").size(11.0));
let mut empty = String::new();
ui.add_enabled(
false,
egui::TextEdit::multiline(&mut empty).desired_rows(6).desired_width(f32::INFINITY).hint_text(tl!("Click a note to view or edit")),
)
.accessible_name(tl!("Notes"));
}
Some(i) => {
let note = &notes[i];
ui.label(RichText::new(if note.author.is_empty() { tl!("No author").to_string() } else { note.author.clone() }).color(t.text_dim).size(11.0));
// The author line as stored: empty for a note without author (#11793).
ui.label(RichText::new(if note.author.is_empty() { " ".to_string() } else { note.author.clone() }).color(t.text_dim).size(11.0));
let mut text = note.text.clone();
if ui.add(egui::TextEdit::multiline(&mut text).desired_rows(6).desired_width(f32::INFINITY)).accessible_name(tl!("Notes")).changed() {
let resp = ui.add(egui::TextEdit::multiline(&mut text).desired_rows(6).desired_width(f32::INFINITY)).accessible_name(tl!("Notes"));
// A note just made with a click: the caret goes into its text, ready to type.
if focus_text {
resp.request_focus();
}
if resp.changed() {
edit = Some(json!({"index": i, "text": text, "coalesce": format!("note-{i}")}));
}
}
}
crate::widgets::hairline(ui);
ui.horizontal(|ui| {
if ui.add_enabled_ui(n > 1, |ui| crate::icons::button(ui, "chevrons-left", 22.0, false, tl!("Previous note"))).inner.clicked() {
let step = n > 1 && sel.is_some();
if ui.add_enabled_ui(step, |ui| crate::icons::button(ui, "chevrons-left", 22.0, false, tl!("Previous note"))).inner.clicked() {
sel = sel.map(|i| (i + n - 1) % n);
navigated = true;
}
if ui.add_enabled_ui(n > 1, |ui| crate::icons::button(ui, "chevrons-right", 22.0, false, tl!("Next note"))).inner.clicked() {
if ui.add_enabled_ui(step, |ui| crate::icons::button(ui, "chevrons-right", 22.0, false, tl!("Next note"))).inner.clicked() {
sel = sel.map(|i| (i + 1) % n);
navigated = true;
}
let template = "{index} of {total}";
let position = crate::i18n::fmt(tl!(template), &[("index", &sel.map_or(0, |i| i + 1).to_string()), ("total", &n.to_string())]);
ui.label(RichText::new(position).color(t.text_dim).size(11.0));
// "N / M", blank with no note selected (#11793).
ui.label(RichText::new(note_counter(sel, n)).color(t.text_dim).size(11.0));
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if ui.add_enabled_ui(sel.is_some(), |ui| crate::icons::button(ui, "trash", 22.0, false, tl!("Delete note"))).inner.clicked() {
delete = true;
@@ -1105,6 +1125,11 @@ fn notes_panel(app: &mut PhotocraftApp, ctx: &egui::Context) {
}
}
/// The Notes panel's counter: "N / M" for note N of M, blank with no note selected (#11793).
pub(crate) fn note_counter(sel: Option<usize>, n: usize) -> String {
sel.filter(|i| *i < n).map_or_else(String::new, |i| format!("{} / {n}", i + 1))
}
fn num_field(ui: &mut egui::Ui, f: &mut Map<String, Value>, key: &str, label: &str, range: std::ops::RangeInclusive<f32>, suffix: &str) {
let t = Tokens::get(ui.ctx());
ui.horizontal(|ui| {
@@ -1272,6 +1297,10 @@ pub(crate) mod tests;
#[cfg(test)]
#[path = "count_group_name_tests.rs"]
mod count_group_name_tests;
#[cfg(test)]
#[path = "notes_panel_layout_tests.rs"]
mod notes_panel_layout_tests;
#[cfg(test)]
mod sampler_drag_tests;
@@ -0,0 +1,85 @@
//! #11793: the Notes panel with no note selected shows an empty, inactive text box with the
//! placeholder "Click a note to view or edit", a blank counter and greyed arrows and trash,
//! and selects no note by itself; the author line is empty for a note without author; the
//! counter reads "N / M"; a note made with a click puts the caret in its text.
use egui::vec2;
use egui_kittest::Harness;
use egui_kittest::kittest::{NodeT, Queryable};
use serde_json::json;
use super::*;
/// A 300 × 200 document with two notes (the second without author) and the Notes panel open,
/// nothing selected; the harness draws the analysis windows.
fn harness() -> Harness<'static, PhotocraftApp> {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({"width": 300, "height": 200})).unwrap();
app.run("notes.add", json!({"x": 20, "y": 20, "author": "Ann", "text": "first"})).unwrap();
app.run("notes.add", json!({"x": 120, "y": 80, "author": "", "text": "second"})).unwrap();
app.ui.analysis.notes = true;
app.ui.analysis.note_selected = None;
let mut h = Harness::builder().with_size(vec2(1400.0, 900.0)).build_ui_state(
|ui, app: &mut PhotocraftApp| {
if !ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {
return;
}
super::windows(app, ui.ctx());
},
app,
);
PhotocraftApp::setup_context(&h.ctx, Default::default());
h.run_steps(4);
h
}
fn disabled(h: &Harness<'static, PhotocraftApp>, label: &str) -> bool {
h.get_by_label(label).accesskit_node().is_disabled()
}
#[test]
fn the_counter_reads_n_of_m_with_a_slash() {
assert_eq!(note_counter(Some(0), 4), "1 / 4");
assert_eq!(note_counter(Some(3), 4), "4 / 4");
assert_eq!(note_counter(None, 4), "");
assert_eq!(note_counter(Some(5), 4), "", "a stale selection reads as none");
}
#[test]
fn no_selection_stays_empty_with_greyed_controls() {
let h = harness();
assert_eq!(h.state().ui.analysis.note_selected, None, "note 1 isn't selected by itself");
assert!(h.query_by_label("1 / 2").is_none() && h.query_by_label("2 / 2").is_none(), "blank counter");
for label in ["Previous note", "Next note", "Delete note"] {
assert!(disabled(&h, label), "{label} is greyed out");
}
assert!(h.query_by_label("first").is_none(), "no note's text is shown");
}
#[test]
fn a_selected_note_shows_its_counter_and_an_empty_author_line() {
let mut h = harness();
h.state_mut().ui.analysis.note_selected = Some(1);
h.run_steps(3);
assert!(h.query_by_label("2 / 2").is_some());
assert!(h.query_by_label("No author").is_none(), "an empty author stays empty");
assert!(!disabled(&h, "Next note") && !disabled(&h, "Delete note"));
h.state_mut().ui.analysis.note_selected = Some(0);
h.run_steps(3);
assert!(h.query_by_label("1 / 2").is_some());
assert!(h.query_by_label("Ann").is_some(), "the stored author");
}
#[test]
fn a_note_made_with_a_click_takes_the_keyboard() {
let mut h = harness();
h.state_mut().ui.tool = Tool::Note;
crate::canvas::tool_event(h.state_mut(), ToolEvent::Down { x: 200.0, y: 150.0, pressure: 1.0 }, egui::Modifiers::NONE);
crate::canvas::tool_event(h.state_mut(), ToolEvent::Up { x: 200.0, y: 150.0 }, egui::Modifiers::NONE);
h.run_steps(3);
assert_eq!(h.state().ui.analysis.note_selected, Some(2));
assert!(h.ctx.memory(|m| m.focused().is_some()), "the note's text box has the keyboard");
h.event(egui::Event::Text("hello".into()));
h.run_steps(3);
assert_eq!(h.state().session.active().unwrap().doc.notes[2].text, "hello", "typing goes into the new note");
}