diff --git a/crates/i18n/translations/en.json b/crates/i18n/translations/en.json index 6efbbebd..e4c8fc78 100644 --- a/crates/i18n/translations/en.json +++ b/crates/i18n/translations/en.json @@ -733,7 +733,9 @@ "merge": "Merge", "delete": "Delete", "hint": "Renaming onto an existing tag merges the two.", - "selectionHint": "Select the tags to merge, then type their common name.", + "selectionHint": "Tick a tag: its name comes into the field, to be corrected. Tick several to merge them under one name.", + "noName": "Type the new name.", + "sameName": "That is already its name.", "confirmRename": "Rename #{{tag}} to #{{into}} on {count, plural, one {# note} other {# notes}}?", "confirmMerge": "⚠️ Merge {{tags}} tags into #{{into}} across {count, plural, one {# note} other {# notes}}? A merge cannot be undone: once they are one, nothing knows which note carried which.", "confirmDelete": "Strip {tags, plural, one {# tag} other {# tags}} from {count, plural, one {# note} other {# notes}}? The notes themselves stay.", diff --git a/crates/i18n/translations/fr.json b/crates/i18n/translations/fr.json index a9f9d63e..0f4bc046 100644 --- a/crates/i18n/translations/fr.json +++ b/crates/i18n/translations/fr.json @@ -733,7 +733,9 @@ "merge": "Fusionner", "delete": "Supprimer", "hint": "Renommer vers un tag existant fusionne les deux.", - "selectionHint": "Sélectionnez les tags à fusionner, puis saisissez leur nom commun.", + "selectionHint": "Cochez un tag : son nom vient dans le champ, à corriger. Cochez-en plusieurs pour les fusionner sous un nom commun.", + "noName": "Saisissez le nouveau nom.", + "sameName": "C’est déjà son nom.", "confirmRename": "Renommer #{{tag}} en #{{into}} sur {count, plural, one {# note} other {# notes}} ?", "confirmMerge": "⚠️ Fusionner {{tags}} tags en #{{into}} sur {count, plural, one {# note} other {# notes}} ? Une fusion ne s’annule pas : une fois réunis, plus rien ne sait quel tag portait quelle note.", "confirmDelete": "Retirer {tags, plural, one {# tag} other {# tags}} de {count, plural, one {# note} other {# notes}} ? Les notes, elles, restent.", diff --git a/crates/ui/src/api/notes.rs b/crates/ui/src/api/notes.rs index a17acfd1..d1721e24 100644 --- a/crates/ui/src/api/notes.rs +++ b/crates/ui/src/api/notes.rs @@ -142,6 +142,11 @@ pub fn normalize_tags(tags: &[String]) -> Vec { engine::notes::model::normalize_tags(tags) } +/// What is left of a typed tag once brought to the rules, `None` when nothing readable is. +pub fn normalize_tag(tag: &str) -> Option<&str> { + engine::notes::model::normalize_tag(tag) +} + /// Into another kind; the undo writes back the body the core answers it had. pub fn convert( backend: &Backend, diff --git a/crates/ui/src/notes/overlays/tag_manager.rs b/crates/ui/src/notes/overlays/tag_manager.rs index e78a4b92..6107eec3 100644 --- a/crates/ui/src/notes/overlays/tag_manager.rs +++ b/crates/ui/src/notes/overlays/tag_manager.rs @@ -28,6 +28,28 @@ const LIST_SHARE: f32 = 0.45; #[derive(Default)] pub(in crate::notes) struct TagManagerView { target: String, + /// The name a tick put in the field, while nothing was typed over it: the next tick may + /// replace it, never what was typed. + filled: Option, + /// Focus the field and select what it holds, on the next frame it is drawn. + select: bool, +} + +impl TagManagerView { + fn clear(&mut self) { + self.target.clear(); + self.filled = None; + } + + /// Ticking `tag` leaves one tag ticked, `only`: its name comes into the field, to be edited. + fn fill(&mut self, only: &str) { + if !self.target.is_empty() && self.filled.as_deref() != Some(self.target.as_str()) { + return; + } + only.clone_into(&mut self.target); + self.filled = Some(only.to_owned()); + self.select = true; + } } fn ask(actions: &mut Actions, asked: Tags) { @@ -41,7 +63,7 @@ pub(in crate::notes) fn show( actions: &mut Actions, ) { if !state.tags.is_open() { - view.target.clear(); + view.clear(); return; } let _p = palette(ui.ctx()); @@ -104,7 +126,7 @@ fn panel(ui: &mut Ui, state: &AppState, view: &mut TagManagerView, actions: &mut ); return closed; } - list(ui, state, language, actions); + list(ui, state, view, actions); if let Some(change) = tags.pending() { confirmation(ui, change, language, view, actions); } else { @@ -120,8 +142,8 @@ fn say(ui: &mut Ui, words: &str, colour: Color32, author_id: &str) { } /// The tags as a cloud that scrolls, only its lines in view drawn. -fn list(ui: &mut Ui, state: &AppState, language: i18n::Language, actions: &mut Actions) { - let tags = &state.tags; +fn list(ui: &mut Ui, state: &AppState, view: &mut TagManagerView, actions: &mut Actions) { + let (tags, language) = (&state.tags, state.language); let widths: Vec = tags .tags() .iter() @@ -140,6 +162,9 @@ fn list(ui: &mut Ui, state: &AppState, language: i18n::Language, actions: &mut A ui.spacing_mut().item_spacing.x = gap::SNUG; for usage in &tags.tags()[line.clone()] { if item(ui, usage, tags.is_selected(&usage.tag), language) { + if let Some(only) = only_after_toggle(state, &usage.tag) { + view.fill(only); + } ask(actions, Tags::Toggle(usage.tag.clone())); } } @@ -149,6 +174,35 @@ fn list(ui: &mut Ui, state: &AppState, language: i18n::Language, actions: &mut A ui.spacing_mut().item_spacing.y = gap::INSET; } +/// The one tag ticked once `tag` is toggled, if one alone is. +fn only_after_toggle<'a>(state: &'a AppState, tag: &'a str) -> Option<&'a str> { + let tags = &state.tags; + match (tags.is_selected(tag), tags.selected()) { + (false, 0) => Some(tag), + (true, 2) => tags + .tags() + .iter() + .map(|usage| usage.tag.as_str()) + .find(|other| *other != tag && tags.is_selected(other)), + _ => None, + } +} + +/// Why « Rename » or « Merge » would change nothing, when it would not. +fn refusal(state: &AppState, target: &str) -> Option { + let Some(into) = crate::api::notes::normalize_tag(target) else { + return Some(keys::tag_manager::NO_NAME); + }; + let tags = &state.tags; + // Exactly: a change of case alone is written. + let same = tags.selected() == 1 + && tags + .tags() + .iter() + .any(|usage| tags.is_selected(&usage.tag) && usage.tag == into); + same.then_some(keys::tag_manager::SAME_NAME) +} + /// The chips each line holds, filled left to right. fn lines_of(widths: &[f32], room: f32) -> Vec> { let mut lines = Vec::new(); @@ -282,6 +336,7 @@ fn form(ui: &mut Ui, state: &AppState, view: &mut TagManagerView, actions: &mut .hline(ui.max_rect().x_range(), top, Stroke::new(1.0, p.line_soft)); ui.add_space(gap::TIGHT); let ticked = state.tags.selected() > 0; + let refused = refusal(state, &view.target).filter(|_| ticked); let apply = tr(if state.tags.selected() > 1 { keys::tag_manager::MERGE } else { @@ -304,12 +359,18 @@ fn form(ui: &mut Ui, state: &AppState, view: &mut TagManagerView, actions: &mut .show(ui) }) .inner; + if std::mem::take(&mut view.select) { + select_all(ui, &field, &view.target); + } + if field.changed() { + view.filled = None; + } let entered = field.lost_focus() && ui.input(|input| input.key_pressed(egui::Key::Enter)); let applied = Button::new(&apply, "tag-apply") - .enabled(ticked) + .enabled(ticked && refused.is_none()) .show(ui) .clicked(); - if ticked && (applied || entered) && !view.target.trim().is_empty() { + if ticked && refused.is_none() && (applied || entered) { ask(actions, Tags::Rename(view.target.clone())); } if Button::new(&delete, "tag-delete") @@ -321,6 +382,19 @@ fn form(ui: &mut Ui, state: &AppState, view: &mut TagManagerView, actions: &mut ask(actions, Tags::Delete); } }); + if let Some(refused) = refused { + say(ui, &tr(refused), p.text_2, "tag-target-reason"); + } +} + +/// The field focused, all it holds selected: what is typed replaces it. +fn select_all(ui: &Ui, field: &egui::Response, text: &str) { + use egui::text::{CCursor, CCursorRange}; + field.request_focus(); + let mut state = egui::text_edit::TextEditState::load(ui.ctx(), field.id).unwrap_or_default(); + let all = CCursorRange::two(CCursor::new(0), CCursor::new(text.chars().count())); + state.cursor.set_char_range(Some(all)); + state.store(ui.ctx(), field.id); } /// What it would do, to how many notes, said once more before it runs. @@ -374,7 +448,7 @@ fn confirmation( .show(ui) .clicked() { - view.target.clear(); + view.clear(); ask(actions, Tags::Confirm); } if Button::new(&tr(keys::tag_manager::CONFIRM_CANCEL), "tag-confirm-cancel") @@ -443,8 +517,11 @@ mod tests { settle(harness); } + /// Over what the field holds: a tick fills it. fn propose(harness: &mut Harness<'_, Bench>, into: &str) { click_id(harness, "tag-target"); + harness.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A); + harness.key_press(egui::Key::Backspace); harness .get_by(|node| node.author_id() == Some("tag-target")) .type_text(into); @@ -452,6 +529,22 @@ mod tests { click_id(harness, "tag-apply"); } + fn field(harness: &Harness<'_, Bench>) -> String { + let node = harness.get_by(|node| node.author_id() == Some("tag-target")); + node.accesskit_node().value().unwrap_or_default() + } + + fn applies(harness: &Harness<'_, Bench>) -> bool { + let apply = harness.get_by(|node| node.author_id() == Some("tag-apply")); + !apply.accesskit_node().is_disabled() + } + + fn reason(harness: &Harness<'_, Bench>) -> Option { + let node = harness.query_by(|node| node.author_id() == Some("tag-target-reason"))?; + let node = node.accesskit_node(); + node.label().or_else(|| node.value()) + } + fn said(harness: &Harness<'_, Bench>) -> String { let node = harness.get_by(|node| node.author_id() == Some("tag-confirm-text")); let node = node.accesskit_node(); @@ -518,6 +611,45 @@ mod tests { assert!(window.contains_rect(confirm), "{confirm:?}"); } + #[test] + fn a_ticked_tag_fills_the_field_selected_and_a_second_keeps_it() { + let mut harness = tagged(); + tick(&mut harness, "staging"); + assert_eq!(field(&harness), "staging"); + let focused = harness.get_by(|node| node.author_id() == Some("tag-target")); + assert!(focused.accesskit_node().is_focused(), "edited in place"); + assert!(!applies(&harness), "nothing would change yet"); + assert!(reason(&harness).is_some()); + + harness + .get_by(|node| node.author_id() == Some("tag-target")) + .type_text("-eu"); + settle(&mut harness); + assert_eq!(field(&harness), "-eu", "typed over the selection"); + assert!(applies(&harness)); + + tick(&mut harness, "urgent"); + assert_eq!(field(&harness), "-eu", "what was typed is kept"); + } + + #[test] + fn a_field_emptied_or_holding_no_name_says_why_nothing_applies() { + let mut harness = tagged(); + tick(&mut harness, "staging"); + for typed in ["", "#", "# staging "] { + propose(&mut harness, typed); + assert!(!applies(&harness), "{typed:?} changes nothing"); + assert!(reason(&harness).is_some(), "{typed:?} is said"); + assert!( + harness + .query_by(|node| node.author_id() == Some("tag-confirm-text")) + .is_none() + ); + } + propose(&mut harness, "#recette"); + assert!(said(&harness).contains("recette"), "{}", said(&harness)); + } + #[test] fn nothing_ticked_applies_nothing() { let harness = tagged(); diff --git a/e2e/tests/14_tag_manager.rs b/e2e/tests/14_tag_manager.rs index aeb5ed6a..6554397d 100644 --- a/e2e/tests/14_tag_manager.rs +++ b/e2e/tests/14_tag_manager.rs @@ -1,5 +1,5 @@ -//! The library's tags managed from the tag rail: renamed, merged onto an existing tag, corrected -//! in case alone, deleted — each said before it runs. The tags are the scenario's own, given +//! The library's tags managed from the tag rail: a ticked tag's name filled in the field, renamed, +//! merged onto an existing tag, corrected in case alone, deleted — each said before it runs. The tags are the scenario's own, given //! from the selection bar: the samples' are in the system's language. use e2e::{App, step}; @@ -35,9 +35,19 @@ fn tick(app: &mut App, tag: &str) { app.click_where("tag-item", |node| node.label() == Some(name.as_str())); } -/// The ticked tags under `into`, proposed then confirmed. +fn target(tree: &e2e::Tree) -> String { + let field = tree.find("tag-target"); + field + .and_then(|node| node.value()) + .unwrap_or_default() + .to_owned() +} + +/// The ticked tags under `into`, typed over what the field holds, proposed then confirmed. fn rename(app: &mut App, into: &str) { - app.fill("tag-target", into); + app.fill("tag-target", ""); + app.press(Key::A, Modifiers::COMMAND); + app.type_text(into); app.click("tag-apply"); app.click("tag-confirm-apply"); } @@ -74,7 +84,19 @@ fn tag_manager() { "a rename says what it touches, and cancelling writes nothing", || { tick(&mut app, "staging"); - app.fill("tag-target", "recette"); + app.eventually("its name fills the field, which has the focus", |tree| { + target(tree) == "staging" && tree.focused().starts_with("tag-target") + }); + assert!( + app.tree() + .find("tag-apply") + .is_some_and(egui::accesskit::Node::is_disabled), + "its own name changes nothing" + ); + assert!(app.tree().has("tag-target-reason")); + // Selected: what is typed replaces it. + app.type_text("recette"); + app.eventually("edited in place", |tree| target(tree) == "recette"); app.click("tag-apply"); app.eventually("two notes carry it, and the sentence says so", |tree| { tree.text("tag-confirm-text")