Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/i18n/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
4 changes: 3 additions & 1 deletion crates/i18n/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
5 changes: 5 additions & 0 deletions crates/ui/src/api/notes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ pub fn normalize_tags(tags: &[String]) -> Vec<String> {
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,
Expand Down
146 changes: 139 additions & 7 deletions crates/ui/src/notes/overlays/tag_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
/// 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) {
Expand All @@ -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());
Expand Down Expand Up @@ -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 {
Expand All @@ -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<f32> = tags
.tags()
.iter()
Expand All @@ -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()));
}
}
Expand All @@ -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<i18n::Key> {
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<std::ops::Range<usize>> {
let mut lines = Vec::new();
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
Expand All @@ -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.
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -443,15 +517,34 @@ 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);
settle(harness);
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<String> {
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();
Expand Down Expand Up @@ -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();
Expand Down
32 changes: 27 additions & 5 deletions e2e/tests/14_tag_manager.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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")
Expand Down
Loading