From 81d5fbf7f1ac709ea623bdde98c0f943aea9229d Mon Sep 17 00:00:00 2001 From: Valentin Millet Date: Fri, 27 Nov 2026 21:31:04 +0100 Subject: [PATCH] Deletion: a space or a folder can put its notes in the trash Both deletions take what becomes of the notes: kept, as before, or trashed in the same transaction, a folder taking its whole subtree. Nothing is erased; the panels count what each choice does and the status says what was done. --- crates/engine/src/folders.rs | 31 +++++-- crates/engine/src/folders/model.rs | 8 ++ crates/engine/src/folders/store.rs | 91 ++++++++++++++++---- crates/engine/src/notes/model.rs | 9 ++ crates/engine/src/spaces.rs | 18 +++- crates/engine/src/spaces/store.rs | 23 ++++- crates/engine/tests/commands/folders.rs | 97 ++++++++++++++++++++- crates/engine/tests/commands/index.rs | 11 ++- crates/engine/tests/commands/spaces.rs | 81 ++++++++++++++++- crates/engine/tests/folders.rs | 12 +-- crates/engine/tests/notes/batches.rs | 4 +- crates/engine/tests/spaces.rs | 10 +-- crates/i18n/translations/en.json | 14 ++- crates/i18n/translations/fr.json | 14 ++- crates/ui/src/api/folders.rs | 15 +++- crates/ui/src/api/spaces.rs | 8 +- crates/ui/src/app/captures.rs | 44 ++++++++++ crates/ui/src/notes/canvas/board/zone.rs | 11 +-- crates/ui/src/notes/header/switchers.rs | 6 +- crates/ui/src/notes/mod.rs | 11 ++- crates/ui/src/notes/panel.rs | 101 +++++++++++++++++++--- crates/ui/src/notes/sidebar/mod.rs | 3 +- crates/ui/src/notes/sidebar/tests.rs | 105 +++++++++++++++++++++++ crates/ui/src/state/apply_notes.rs | 12 ++- crates/ui/src/state/folders.rs | 64 ++++++++++++-- crates/ui/src/state/mod.rs | 10 +-- crates/ui/src/state/spaces.rs | 52 ++++++++--- docs/architecture.md | 20 +++-- e2e/tests/05_spaces.rs | 38 +++++++- e2e/tests/17_folders.rs | 35 +++++++- 30 files changed, 840 insertions(+), 118 deletions(-) diff --git a/crates/engine/src/folders.rs b/crates/engine/src/folders.rs index 079d1d24f..763d0c12d 100644 --- a/crates/engine/src/folders.rs +++ b/crates/engine/src/folders.rs @@ -9,13 +9,13 @@ use crate::count::saturating_u32 as count; use crate::db::lock; use crate::error::AppError; use crate::index::Changed; -use crate::notes::model::NotePlacement; +use crate::notes::model::{HeldNotes, NotePlacement}; use board::{ BoardArrangement, BoardQuery, BoardScope, BoardView, CardPlacement, Occupancy, PreviewLines, ZonePlacement, }; -use model::{Folder, FolderColour, FolderDraft, FolderMove, Lineage, NoteFiling}; +use model::{Folder, FolderColour, FolderContents, FolderDraft, FolderMove, Lineage, NoteFiling}; /// The second way to look at a space, one level at a time: the root's sub-folders as zones, /// each gathering what is under it, and what is filed in the root itself free beside them. It @@ -179,15 +179,32 @@ pub fn move_folder_back(engine: &Engine, was: &FolderMove) -> Result Result<(), AppError> { +/// What deleting the folder would keep or trash, counted under the lock: its live notes and its +/// sub-folders, all the way down. +pub fn count_folder_contents(engine: &Engine, id: &str) -> Result { + let db = &engine.db; + let mut connection = lock(db)?; + + Ok(store::contents(&mut connection, id)?) +} + +/// No refuge, unlike [`crate::spaces::delete_space`]: what it held goes to its parent, or loose +/// at the top of the space. With `HeldNotes::Trash` its sub-folders go with it and every note +/// under it goes to the trash, restored loose in the space. `lines` sizes the rows the parent's +/// zone grows by. Answers how many live notes it held. +pub fn delete_folder( + engine: &Engine, + id: &str, + lines: PreviewLines, + held: HeldNotes, +) -> Result { engine.write(|db| { let mut connection = lock(db)?; - store::delete(&mut connection, id, lines.card_height())?; + let trashed_at = (held == HeldNotes::Trash).then(Utc::now); + let held = store::delete(&mut connection, id, lines.card_height(), trashed_at)?; let changed = Changed::everything(); - Ok(((), changed)) + Ok((count(held), changed)) }) } diff --git a/crates/engine/src/folders/model.rs b/crates/engine/src/folders/model.rs index 7731ee850..7f28d7342 100644 --- a/crates/engine/src/folders/model.rs +++ b/crates/engine/src/folders/model.rs @@ -75,6 +75,14 @@ pub struct FolderMove { pub notes: Vec, } +/// What a folder's deletion would keep or trash: the live notes under it, sub-folders included, +/// and how many sub-folders it has. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct FolderContents { + pub notes: u32, + pub folders: u32, +} + /// Which folder each note left, the only thing that can put a filing back; `None` was loose. #[derive(Debug, Clone, PartialEq, Eq)] pub struct NoteFiling { diff --git a/crates/engine/src/folders/store.rs b/crates/engine/src/folders/store.rs index e8970af9e..d3781c8a3 100644 --- a/crates/engine/src/folders/store.rs +++ b/crates/engine/src/folders/store.rs @@ -10,6 +10,7 @@ use diesel::prelude::*; use uuid::Uuid; use super::model::{Folder, FolderColour, FolderMove, Lineage, NoteFiling, NoteFolder}; +use crate::count::saturating_u32; use crate::db::schema::{folders, notes}; use crate::db::{Library, iso8601}; use crate::error::StorageError; @@ -413,35 +414,91 @@ fn place( /// What it held goes to its parent — its notes, filed there, and its sub-folders, each to a /// free slot on that board — or loose at the top of the space. `updated_at` is not touched: /// the user aimed at the folder. -pub fn delete(connection: &mut Library, id: &str, card: i32) -> Result<(), StorageError> { +pub fn contents( + connection: &mut Library, + id: &str, +) -> Result { + let (connection, _vault) = connection.split(); + let under = lineage_of(connection, id)?.subtree(id); + Ok(super::model::FolderContents { + notes: saturating_u32(count_filed(connection, &under)?), + folders: saturating_u32(under.len().saturating_sub(1)), + }) +} + +/// Answers how many live notes it held; `trashed_at`, they and its sub-folders go with it. +pub fn delete( + connection: &mut Library, + id: &str, + card: i32, + trashed_at: Option>, +) -> Result { connection.transaction(|connection, _vault| { let lineage = lineage_of(connection, id)?; let parent = lineage.parent(id).map(str::to_string); - - for child in lineage.children(Some(id)) { - place(connection, child, parent.as_deref(), None)?; + let under = lineage.subtree(id); + let held = count_filed(connection, &under)?; + match trashed_at { + Some(at) => trash_under(connection, &under, at)?, + None => hand_up(connection, &lineage, id, parent.as_deref())?, } - let held: Vec = notes::table - .filter(notes::folder_id.eq(id)) - .select(notes::id) - .load::(connection)?; - // A place on the deleted folder's board means nothing on its parent's. - board::forget_positions(connection, &held)?; - diesel::update(notes::table.filter(notes::folder_id.eq(id))) - .set(notes::folder_id.eq(parent.as_deref())) - .execute(connection)?; - - diesel::delete(folders::table.find(id)).execute(connection)?; - if let Some(parent) = &parent { let after = lineage_of(connection, parent)?; grow_with_ancestors(connection, &after, parent, card)?; } - Ok(()) + Ok(held) }) } +/// Its sub-folders and its notes to its parent, or loose at the top; then the folder goes. +fn hand_up( + connection: &mut SqliteConnection, + lineage: &Lineage, + id: &str, + parent: Option<&str>, +) -> Result<(), StorageError> { + for child in lineage.children(Some(id)) { + place(connection, child, parent, None)?; + } + + let held: Vec = notes::table + .filter(notes::folder_id.eq(id)) + .select(notes::id) + .load::(connection)?; + // A place on the deleted folder's board means nothing on its parent's. + board::forget_positions(connection, &held)?; + diesel::update(notes::table.filter(notes::folder_id.eq(id))) + .set(notes::folder_id.eq(parent)) + .execute(connection)?; + + diesel::delete(folders::table.find(id)).execute(connection)?; + Ok(()) +} + +/// Every note under the folders `under` to the trash, then the folders: the notes' `folder_id` +/// is emptied by its key, so a restore brings them back loose in the space. +fn trash_under( + connection: &mut SqliteConnection, + under: &[String], + at: DateTime, +) -> Result<(), StorageError> { + let held: Vec = notes::table + .filter(notes::folder_id.eq_any(under)) + .select(notes::id) + .load::(connection)?; + board::forget_positions(connection, &held)?; + diesel::update( + notes::table + .filter(notes::folder_id.eq_any(under)) + .filter(notes::deleted_at.is_null()), + ) + .set(notes::deleted_at.eq(iso8601::format(at))) + .execute(connection)?; + diesel::delete(folders::table.filter(folders::id.eq_any(under))).execute(connection)?; + Ok(()) +} + /// Every zone that shows a folder's notes grows to fit them: its own, and its ancestors', /// which gather what is under them. fn grow_with_ancestors( diff --git a/crates/engine/src/notes/model.rs b/crates/engine/src/notes/model.rs index db3a92beb..9fffff13d 100644 --- a/crates/engine/src/notes/model.rs +++ b/crates/engine/src/notes/model.rs @@ -1205,3 +1205,12 @@ mod tests { assert_eq!(note.placeholders[0].name, "host"); } } + +/// What deleting a space or a folder does to the notes it holds: they live on elsewhere, or go to +/// the trash with it. Never erased: only emptying the trash, or its retention, is final. +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] +pub enum HeldNotes { + #[default] + Keep, + Trash, +} diff --git a/crates/engine/src/spaces.rs b/crates/engine/src/spaces.rs index 22437799e..0318b2aeb 100644 --- a/crates/engine/src/spaces.rs +++ b/crates/engine/src/spaces.rs @@ -1,10 +1,14 @@ pub mod model; pub mod store; +use chrono::Utc; + use crate::Engine; +use crate::count::saturating_u32 as count; use crate::db::lock; use crate::error::AppError; use crate::index::Changed; +use crate::notes::model::HeldNotes; use model::{Space, SpaceContents, SpaceDraft}; pub fn list_spaces(engine: &Engine) -> Result, AppError> { @@ -47,7 +51,14 @@ pub fn count_space_contents(engine: &Engine, id: &str) -> Result Result<(), AppError> { +/// Its notes go to `target_space_id`, out of any folder; with `HeldNotes::Trash` they go to the +/// trash as well, and a restore brings them back there. Answers how many live notes it moved. +pub fn delete_space( + engine: &Engine, + id: &str, + target_space_id: &str, + held: HeldNotes, +) -> Result { engine.write(|db| { // A space as its own refuge would see its notes swept away by the cascade right // after the transfer. @@ -55,8 +66,9 @@ pub fn delete_space(engine: &Engine, id: &str, target_space_id: &str) -> Result< let mut connection = lock(db)?; - store::delete(&mut connection, id, target_space_id)?; + let trashed_at = (held == HeldNotes::Trash).then(Utc::now); + let moved = store::delete(&mut connection, id, target_space_id, trashed_at)?; let changed = Changed::everything(); - Ok(((), changed)) + Ok((count(moved), changed)) }) } diff --git a/crates/engine/src/spaces/store.rs b/crates/engine/src/spaces/store.rs index 61088b3a3..9cc2a927b 100644 --- a/crates/engine/src/spaces/store.rs +++ b/crates/engine/src/spaces/store.rs @@ -9,11 +9,12 @@ use diesel::prelude::*; use super::model::{Space, SpaceContents}; use crate::count::saturating_u32 as count; -use crate::db::Library; use crate::db::schema::{folders, notes, spaces}; +use crate::db::{Library, iso8601}; use crate::error::StorageError; use crate::name; use crate::vault::key::Vault; +use chrono::{DateTime, Utc}; use uuid::Uuid; /// Pinned first, then by name — the same shape the canvas gives notes. Folded for the @@ -191,12 +192,28 @@ pub fn contents(connection: &mut Library, id: &str) -> Result Result<(), StorageError> { +/// Answers how many live notes it moved; `trashed_at`, they are trashed on the way. +pub fn delete( + connection: &mut Library, + id: &str, + target_id: &str, + trashed_at: Option>, +) -> Result { connection.transaction(|connection, _vault| { if !exists(connection, target_id)? { return Err(StorageError::SpaceNotFound(target_id.to_string())); } + let live = notes::table + .filter(notes::space_id.eq(id)) + .filter(notes::deleted_at.is_null()); + // Before the move: after it, the refuge's own notes would be among them. + let moved = match trashed_at { + Some(at) => diesel::update(live) + .set(notes::deleted_at.eq(iso8601::format(at))) + .execute(connection)?, + None => usize::try_from(live.count().get_result::(connection)?).unwrap_or(0), + }; diesel::update(notes::table.filter(notes::space_id.eq(id))) .set(notes::space_id.eq(target_id)) .execute(connection)?; @@ -204,6 +221,6 @@ pub fn delete(connection: &mut Library, id: &str, target_id: &str) -> Result<(), return Err(StorageError::SpaceNotFound(id.to_string())); } - Ok(()) + Ok(moved) }) } diff --git a/crates/engine/tests/commands/folders.rs b/crates/engine/tests/commands/folders.rs index 826f23e2a..78d9f5121 100644 --- a/crates/engine/tests/commands/folders.rs +++ b/crates/engine/tests/commands/folders.rs @@ -4,15 +4,18 @@ use engine::folders::board::{ BoardFrame, BoardQuery, BoardScope, MIN_ZONE_HEIGHT, MIN_ZONE_WIDTH, PreviewLines, ZonePlacement, }; +use engine::folders::count_folder_contents; use engine::folders::model::{FolderColour, FolderDraft}; use engine::folders::{ arrange_board, board_view, create_folder, delete_folder, file_notes, file_notes_back, list_folders, move_folder, move_folder_back, move_notes_into, recolour_folder, rename_folder, save_board_layout, }; +use engine::notes::model::HeldNotes; use engine::notes::view::{Grouping, NoteFilter, NoteOrder, NotesQuery}; use engine::notes::{ - create_note, delete_notes, get_note, move_notes, move_notes_back, query_notes, restore_notes, + create_note, delete_notes, get_note, list_trash, move_notes, move_notes_back, query_notes, + restore_notes, }; use super::common::snippet; @@ -56,7 +59,7 @@ fn a_folder_is_created_renamed_recoloured_and_deleted() { .unwrap(); let listed = session.call(|e| list_folders(e, Some(&space))).unwrap(); session - .call(|e| delete_folder(e, &id, PreviewLines::default())) + .call(|e| delete_folder(e, &id, PreviewLines::default(), HeldNotes::Keep)) .unwrap(); assert_eq!(renamed.name, "Shell"); @@ -480,3 +483,93 @@ fn the_board_reads_today_as_the_list_does() { let board_card = on_board.loose[0].note.calc.clone(); assert_eq!(board_card, list_card); } + +fn nested(session: &Session, space_id: &str, parent: &str, name: &str) -> String { + let draft = FolderDraft { + space_id: space_id.to_string(), + parent_id: Some(parent.to_string()), + name: name.to_string(), + }; + + session.call(|e| create_folder(e, &draft)).unwrap().id +} + +fn filed(session: &Session, space_id: &str, folder: &str) -> String { + let id = session + .call(|e| create_note(e, snippet(space_id))) + .unwrap() + .note + .id; + let ids = [id.clone()]; + let into = folder.to_string(); + session + .call(|e| file_notes(e, &ids, Some(&into), PreviewLines::default())) + .unwrap(); + id +} + +/// What the panel names before the second click: every live note under it, and its sub-folders. +#[test] +fn a_folder_counts_what_is_under_it() { + let session = Session::open(); + let space = session.space("Personal"); + let ops = folder(&session, &space, "Ops"); + let deep = nested(&session, &space, &ops, "Deep"); + filed(&session, &space, &ops); + filed(&session, &space, &deep); + + let contents = session.call(|e| count_folder_contents(e, &ops)).unwrap(); + + assert_eq!((contents.notes, contents.folders), (2, 1)); +} + +/// Its whole subtree goes, every note under it waits in the trash, and comes back loose. +#[test] +fn a_folder_deleted_with_its_notes_takes_its_sub_folders_and_trashes_them() { + let session = Session::open(); + let space = session.space("Personal"); + let ops = folder(&session, &space, "Ops"); + let deep = nested(&session, &space, &ops, "Deep"); + let kept = folder(&session, &space, "Kept"); + let mut held = vec![ + filed(&session, &space, &ops), + filed(&session, &space, &deep), + ]; + let other = filed(&session, &space, &kept); + let loose = session + .call(|e| create_note(e, snippet(&space))) + .unwrap() + .note + .id; + + let trashed = session + .call(|e| delete_folder(e, &ops, PreviewLines::default(), HeldNotes::Trash)) + .unwrap(); + + assert_eq!(trashed, 2); + let left: Vec = session + .call(|e| list_folders(e, Some(&space))) + .unwrap() + .into_iter() + .map(|folder| folder.id) + .collect(); + assert_eq!(left, std::slice::from_ref(&kept)); + let mut in_trash: Vec = session + .call(|e| list_trash(e, "")) + .unwrap() + .into_iter() + .map(|row| row.note.id) + .collect(); + in_trash.sort(); + held.sort(); + assert_eq!(in_trash, held); + assert!(session.call(|e| get_note(e, &other)).is_ok()); + assert!(session.call(|e| get_note(e, &loose)).is_ok()); + + session.call(|e| restore_notes(e, &held)).unwrap(); + for id in &held { + let back = session.call(|e| get_note(e, id)).unwrap(); + assert_eq!(back.note.space_id, space); + assert_eq!(back.note.folder_id, None, "loose in its space"); + } +} diff --git a/crates/engine/tests/commands/index.rs b/crates/engine/tests/commands/index.rs index d8ac4ace1..b6a762ac7 100644 --- a/crates/engine/tests/commands/index.rs +++ b/crates/engine/tests/commands/index.rs @@ -123,7 +123,12 @@ fn folders_attachments_and_global_values_are_read_again() { written(&session, |e| delete_attachment(e, &attachment.id)); written(&session, |e| set_global_placeholders(e, globals)); written(&session, |e| { - delete_folder(e, &prod.id, engine::folders::board::PreviewLines::default()) + delete_folder( + e, + &prod.id, + engine::folders::board::PreviewLines::default(), + engine::notes::model::HeldNotes::Keep, + ) }); } @@ -138,5 +143,7 @@ fn a_corpus_wide_write_leaves_an_index_the_next_read_builds_whole() { session.wait(engine::index::fill); let refuge = session.space("Refuge"); - written(&session, |e| delete_space(e, &space, &refuge)); + written(&session, |e| { + delete_space(e, &space, &refuge, engine::notes::model::HeldNotes::Keep) + }); } diff --git a/crates/engine/tests/commands/spaces.rs b/crates/engine/tests/commands/spaces.rs index 9e9601126..c3e53a55a 100644 --- a/crates/engine/tests/commands/spaces.rs +++ b/crates/engine/tests/commands/spaces.rs @@ -1,7 +1,9 @@ +use engine::attachments::{attach_file, list_attachments}; use engine::error::ErrorCode; use engine::folders::create_folder; use engine::folders::model::FolderDraft; -use engine::notes::{create_note, get_note}; +use engine::notes::model::HeldNotes; +use engine::notes::{create_note, get_note, list_trash, restore_notes}; use engine::spaces::model::SpaceContents; use engine::spaces::model::SpaceDraft; use engine::spaces::{ @@ -67,7 +69,9 @@ fn a_deleted_space_hands_its_notes_to_its_refuge() { .note .id; - session.call(|e| delete_space(e, &doomed, &refuge)).unwrap(); + session + .call(|e| delete_space(e, &doomed, &refuge, HeldNotes::Keep)) + .unwrap(); let moved = session.call(|e| get_note(e, ¬e)).unwrap(); assert_eq!(session.spaces(), ["Work"]); @@ -81,7 +85,7 @@ fn a_space_cannot_be_its_own_refuge() { let id = session.space("Personal"); assert!(matches!( - code(session.call(|e| delete_space(e, &id, &id))), + code(session.call(|e| delete_space(e, &id, &id, HeldNotes::Keep))), ErrorCode::InvalidInput )); assert_eq!(session.spaces(), ["Personal"]); @@ -118,3 +122,74 @@ fn a_space_counts_what_its_deletion_would_move_and_take() { ErrorCode::SpaceNotFound )); } + +/// Nothing is erased: the notes wait in the trash, and come back into the refuge chosen. +#[test] +fn a_space_deleted_with_its_notes_trashes_them_for_the_refuge_to_take_back() { + let scratch = tempfile::tempdir().unwrap(); + let session = Session::on_disk(scratch.path()); + let doomed = session.space("Personal"); + let refuge = session.space("Work"); + let shelf = session + .call(|e| { + let draft = FolderDraft { + space_id: doomed.clone(), + parent_id: None, + name: "Shelf".to_string(), + }; + create_folder(e, &draft) + }) + .unwrap() + .id; + let doomed_note = session + .call(|e| create_note(e, snippet(&doomed))) + .unwrap() + .note + .id; + session + .call(|e| { + let ids = [doomed_note.clone()]; + let lines = engine::folders::board::PreviewLines::default(); + engine::folders::file_notes(e, &ids, Some(&shelf), lines) + }) + .unwrap(); + let kept = session + .call(|e| create_note(e, snippet(&refuge))) + .unwrap() + .note + .id; + let source = scratch.path().join("capture.png"); + std::fs::write(&source, b"\x89PNG").unwrap(); + session + .wait(|e| attach_file(e, doomed_note.clone(), source.to_string_lossy().to_string())) + .unwrap(); + + let moved = session + .call(|e| delete_space(e, &doomed, &refuge, HeldNotes::Trash)) + .unwrap(); + + assert_eq!(moved, 1); + assert_eq!(session.spaces(), ["Work"]); + let trash = session.call(|e| list_trash(e, "")).unwrap(); + let trashed: Vec<&str> = trash.iter().map(|row| row.note.id.as_str()).collect(); + assert_eq!( + trashed, + [doomed_note.as_str()], + "the refuge's own notes stay" + ); + assert!(session.call(|e| get_note(e, &kept)).is_ok()); + + session + .call(|e| restore_notes(e, std::slice::from_ref(&doomed_note))) + .unwrap(); + let back = session.call(|e| get_note(e, &doomed_note)).unwrap(); + assert_eq!(back.note.space_id, refuge); + assert_eq!(back.note.folder_id, None); + assert_eq!( + session + .call(|e| list_attachments(e, &doomed_note)) + .unwrap() + .len(), + 1 + ); +} diff --git a/crates/engine/tests/folders.rs b/crates/engine/tests/folders.rs index 10b11301e..81527d96a 100644 --- a/crates/engine/tests/folders.rs +++ b/crates/engine/tests/folders.rs @@ -365,7 +365,7 @@ fn deleting_a_folder_leaves_its_notes_loose_rather_than_deleting_them() { ) .unwrap(); - delete(&mut connection, &perf.id, CARD).unwrap(); + delete(&mut connection, &perf.id, CARD, None).unwrap(); assert!(list(&mut connection, Some(&sql)).unwrap().is_empty()); assert_eq!(folder_of(&mut connection, ¬e.id), None); @@ -376,7 +376,7 @@ fn deleting_an_unknown_folder_says_so_rather_than_answering_ok() { let mut connection = open_in_memory().unwrap(); assert!(matches!( - delete(&mut connection, "nowhere", CARD), + delete(&mut connection, "nowhere", CARD, None), Err(StorageError::FolderNotFound(_)) )); } @@ -398,7 +398,7 @@ fn deleting_a_space_takes_its_folders_and_unfiles_the_notes_it_hands_over() { ) .unwrap(); - spaces::delete(&mut connection, &sql, &refuge).unwrap(); + spaces::delete(&mut connection, &sql, &refuge, None).unwrap(); assert!(list(&mut connection, Some(&sql)).unwrap().is_empty()); assert_eq!(folder_of(&mut connection, ¬e.id), None); @@ -1869,7 +1869,7 @@ mod nesting { let mut connection = open_in_memory().unwrap(); let tree = tree(&mut connection); let was = move_to(&mut connection, &tree.migrations.id, (&tree.space, None)).unwrap(); - delete(&mut connection, &tree.sql.id, CARD).unwrap(); + delete(&mut connection, &tree.sql.id, CARD, None).unwrap(); assert!(move_back(&mut connection, &was).unwrap().is_none()); assert_eq!(parent_of(&mut connection, &tree.migrations.id), None); @@ -1883,7 +1883,7 @@ mod nesting { let held = filed(&mut connection, &tree, "EXPLAIN", &tree.perf); let below = filed(&mut connection, &tree, "Index partiel", &tree.index); - delete(&mut connection, &tree.perf.id, CARD).unwrap(); + delete(&mut connection, &tree.perf.id, CARD, None).unwrap(); assert_eq!( folder_of(&mut connection, &held.id), @@ -1905,7 +1905,7 @@ mod nesting { let tree = tree(&mut connection); let held = filed(&mut connection, &tree, "Requête", &tree.sql); - delete(&mut connection, &tree.sql.id, CARD).unwrap(); + delete(&mut connection, &tree.sql.id, CARD, None).unwrap(); assert_eq!(folder_of(&mut connection, &held.id), None); assert_eq!(parent_of(&mut connection, &tree.perf.id), None); diff --git a/crates/engine/tests/notes/batches.rs b/crates/engine/tests/notes/batches.rs index ac42a3551..cfa4d68d5 100644 --- a/crates/engine/tests/notes/batches.rs +++ b/crates/engine/tests/notes/batches.rs @@ -226,7 +226,7 @@ fn a_space_gone_since_the_move_does_not_take_the_rest_of_the_undo_with_it() { &target, ) .unwrap(); - spaces::delete(&mut connection, &doomed, &target).unwrap(); + spaces::delete(&mut connection, &doomed, &target, None).unwrap(); let restored = restore_placements(&mut connection, &previous).unwrap(); @@ -289,7 +289,7 @@ fn a_folder_gone_since_the_move_leaves_the_note_loose() { ) .unwrap(); let previous = move_many(&mut connection, std::slice::from_ref(¬e.id), &target).unwrap(); - folders::delete(&mut connection, &folder.id, crate::CARD).unwrap(); + folders::delete(&mut connection, &folder.id, crate::CARD, None).unwrap(); assert_eq!(restore_placements(&mut connection, &previous).unwrap(), 1); diff --git a/crates/engine/tests/spaces.rs b/crates/engine/tests/spaces.rs index 4466248f8..83101d631 100644 --- a/crates/engine/tests/spaces.rs +++ b/crates/engine/tests/spaces.rs @@ -203,7 +203,7 @@ fn deleting_a_space_moves_its_notes_to_the_target() { let refuge = create(&mut connection, "Boulot").unwrap(); note_in(&mut connection, &doomed.id); - delete(&mut connection, &doomed.id, &refuge.id).unwrap(); + delete(&mut connection, &doomed.id, &refuge.id, None).unwrap(); let space_id = notes::table .find("n-1") @@ -221,7 +221,7 @@ fn moving_notes_out_of_a_deleted_space_does_not_touch_their_timestamps() { let refuge = create(&mut connection, "Boulot").unwrap(); note_in(&mut connection, &doomed.id); - delete(&mut connection, &doomed.id, &refuge.id).unwrap(); + delete(&mut connection, &doomed.id, &refuge.id, None).unwrap(); let updated_at = notes::table .find("n-1") @@ -237,7 +237,7 @@ fn deleting_an_empty_space_leaves_the_others_alone() { let doomed = create(&mut connection, "Personal").unwrap(); let refuge = create(&mut connection, "Boulot").unwrap(); - delete(&mut connection, &doomed.id, &refuge.id).unwrap(); + delete(&mut connection, &doomed.id, &refuge.id, None).unwrap(); assert_eq!(names(&mut connection), ["Boulot"]); } @@ -247,7 +247,7 @@ fn deleting_an_unknown_space_reports_an_error() { let mut connection = open_in_memory().unwrap(); let refuge = create(&mut connection, "Boulot").unwrap(); - let error = delete(&mut connection, "unknown", &refuge.id).unwrap_err(); + let error = delete(&mut connection, "unknown", &refuge.id, None).unwrap_err(); assert!(matches!(error, StorageError::SpaceNotFound(_))); } @@ -258,7 +258,7 @@ fn deleting_into_an_unknown_space_changes_nothing() { let doomed = create(&mut connection, "Personal").unwrap(); note_in(&mut connection, &doomed.id); - let error = delete(&mut connection, &doomed.id, "unknown").unwrap_err(); + let error = delete(&mut connection, &doomed.id, "unknown", None).unwrap_err(); assert!(matches!(error, StorageError::SpaceNotFound(_))); assert_eq!(list(&mut connection).unwrap().len(), 1); diff --git a/crates/i18n/translations/en.json b/crates/i18n/translations/en.json index 8e86a0900..7feecba71 100644 --- a/crates/i18n/translations/en.json +++ b/crates/i18n/translations/en.json @@ -21,6 +21,13 @@ "deleteSpaceConfirm": "Confirm?", "deleteSpaceNotes": "{count, plural, one {Its note goes} other {Its # notes go}} to “{{refuge}}”, out of any folder.", "deleteSpaceNoNotes": "It holds no note.", + "deleteSpaceTrashNotes": "{count, plural, one {Its note goes to the trash; restored, it comes back in “{{refuge}}”.} other {Its # notes go to the trash; restored, they come back in “{{refuge}}”.}}", + "heldNotes": "Its notes", + "keepNotes": "Keep them", + "trashNotes": "Put them in the trash", + "restoreNotesIn": "Restore them in", + "spaceDeleted": "{count, plural, =0 {Space deleted.} one {Space deleted: its note is in “{{refuge}}”.} other {Space deleted: its # notes are in “{{refuge}}”.}}", + "spaceDeletedTrashed": "{count, plural, =0 {Space deleted.} one {Space deleted: its note is in the trash.} other {Space deleted: its # notes are in the trash.}}", "deleteSpaceFolders": "{count, plural, one {Its folder goes with it.} other {Its # folders go with it.}}", "deleteSpaceNeedsAnother": "Create a second space before deleting this one: its notes need somewhere to go.", "noteOptionsLabel": "Options for note {{title}}", @@ -186,7 +193,12 @@ }, "delete": "Delete folder", "deleteConfirm": "Confirm?", - "deleteKeepsNotes": "Its notes and sub-folders stay, one level up.", + "deleteKeepNotes": "{count, plural, =0 {It holds no note.} one {Its note stays, one level up.} other {Its # notes stay, one level up.}}", + "deleteKeepFolders": "{count, plural, one {So does its sub-folder.} other {So do its # sub-folders.}}", + "deleteTrashNotes": "{count, plural, =0 {It holds no note.} one {Its note goes to the trash; restored, it comes back in the space, out of any folder.} other {Its # notes go to the trash; restored, they come back in the space, out of any folder.}}", + "deleteTrashFolders": "{count, plural, one {Its sub-folder goes with it.} other {Its # sub-folders go with it.}}", + "deleted": "{count, plural, =0 {Folder deleted.} one {Folder deleted: its note stays in the space.} other {Folder deleted: its # notes stay in the space.}}", + "deletedTrashed": "{count, plural, =0 {Folder deleted.} one {Folder deleted: its note is in the trash.} other {Folder deleted: its # notes are in the trash.}}", "breadcrumbLabel": "Folder breadcrumb", "selectNotes": "Select {count, plural, one {the note} other {all # notes}}" }, diff --git a/crates/i18n/translations/fr.json b/crates/i18n/translations/fr.json index e08cc2f87..cc7e518a7 100644 --- a/crates/i18n/translations/fr.json +++ b/crates/i18n/translations/fr.json @@ -21,6 +21,13 @@ "deleteSpaceConfirm": "Confirmer ?", "deleteSpaceNotes": "{count, plural, one {Sa note ira} other {Ses # notes iront}} dans « {{refuge}} », hors de tout dossier.", "deleteSpaceNoNotes": "Il ne contient aucune note.", + "deleteSpaceTrashNotes": "{count, plural, one {Sa note ira à la corbeille ; restaurée, elle reviendra dans « {{refuge}} ».} other {Ses # notes iront à la corbeille ; restaurées, elles reviendront dans « {{refuge}} ».}}", + "heldNotes": "Ses notes", + "keepNotes": "Les garder", + "trashNotes": "Les mettre à la corbeille", + "restoreNotesIn": "Les restaurer dans", + "spaceDeleted": "{count, plural, =0 {Espace supprimé.} one {Espace supprimé : sa note est dans « {{refuge}} ».} other {Espace supprimé : ses # notes sont dans « {{refuge}} ».}}", + "spaceDeletedTrashed": "{count, plural, =0 {Espace supprimé.} one {Espace supprimé : sa note est dans la corbeille.} other {Espace supprimé : ses # notes sont dans la corbeille.}}", "deleteSpaceFolders": "{count, plural, one {Son dossier part avec lui.} other {Ses # dossiers partent avec lui.}}", "deleteSpaceNeedsAnother": "Créez un second espace pour pouvoir supprimer celui-ci : ses notes doivent aller quelque part.", "noteOptionsLabel": "Options de la note {{title}}", @@ -186,7 +193,12 @@ }, "delete": "Supprimer le dossier", "deleteConfirm": "Confirmer ?", - "deleteKeepsNotes": "Ses notes et ses sous-dossiers restent, un niveau plus haut.", + "deleteKeepNotes": "{count, plural, =0 {Il ne contient aucune note.} one {Sa note reste, un niveau plus haut.} other {Ses # notes restent, un niveau plus haut.}}", + "deleteKeepFolders": "{count, plural, one {Son sous-dossier aussi.} other {Ses # sous-dossiers aussi.}}", + "deleteTrashNotes": "{count, plural, =0 {Il ne contient aucune note.} one {Sa note ira à la corbeille ; restaurée, elle reviendra dans l’espace, hors dossier.} other {Ses # notes iront à la corbeille ; restaurées, elles reviendront dans l’espace, hors dossier.}}", + "deleteTrashFolders": "{count, plural, one {Son sous-dossier part avec lui.} other {Ses # sous-dossiers partent avec lui.}}", + "deleted": "{count, plural, =0 {Dossier supprimé.} one {Dossier supprimé : sa note reste dans l’espace.} other {Dossier supprimé : ses # notes restent dans l’espace.}}", + "deletedTrashed": "{count, plural, =0 {Dossier supprimé.} one {Dossier supprimé : sa note est dans la corbeille.} other {Dossier supprimé : ses # notes sont dans la corbeille.}}", "breadcrumbLabel": "Fil d’Ariane du dossier", "selectNotes": "Sélectionner {count, plural, one {la note} other {les # notes}}" }, diff --git a/crates/ui/src/api/folders.rs b/crates/ui/src/api/folders.rs index c0c4281e9..c51d68a49 100644 --- a/crates/ui/src/api/folders.rs +++ b/crates/ui/src/api/folders.rs @@ -3,8 +3,9 @@ use engine::folders::{ self, board::PreviewLines, - model::{Folder, FolderColour, FolderDraft, FolderMove}, + model::{Folder, FolderColour, FolderContents, FolderDraft, FolderMove}, }; +use engine::notes::model::HeldNotes; use super::undo::Reversible; use crate::backend::{Backend, Pending}; @@ -36,9 +37,15 @@ pub fn recolour(backend: &Backend, id: String, colour: FolderColour) -> Pending< backend.db(move |engine| folders::recolour_folder(engine, &id, colour)) } -/// What it holds goes up a level. -pub fn delete(backend: &Backend, id: String, lines: PreviewLines) -> Pending<()> { - backend.db(move |engine| folders::delete_folder(engine, &id, lines)) +/// What deleting it would keep or trash, said before the second click. +pub fn contents(backend: &Backend, id: String) -> Pending { + backend.db(move |engine| folders::count_folder_contents(engine, &id)) +} + +/// What it holds goes up a level, or to the trash with its sub-folders: answers how many live +/// notes it held. +pub fn delete(backend: &Backend, id: String, lines: PreviewLines, held: HeldNotes) -> Pending { + backend.db(move |engine| folders::delete_folder(engine, &id, lines, held)) } /// Under `parent_id`, or at the top of its space, with what it holds: answers where it was. diff --git a/crates/ui/src/api/spaces.rs b/crates/ui/src/api/spaces.rs index 5b8e0f55e..8ee11a418 100644 --- a/crates/ui/src/api/spaces.rs +++ b/crates/ui/src/api/spaces.rs @@ -1,5 +1,6 @@ //! Spaces: the rail's top level, and where a note lives. +use engine::notes::model::HeldNotes; use engine::spaces::{self, model::Space, model::SpaceContents, model::SpaceDraft}; use crate::backend::{Backend, Pending}; @@ -26,7 +27,8 @@ pub fn contents(backend: &Backend, id: String) -> Pending { backend.db(move |engine| spaces::count_space_contents(engine, &id)) } -/// Its notes go to `refuge` first: the space leaves with whatever nobody takes in. -pub fn delete(backend: &Backend, id: String, refuge: String) -> Pending<()> { - backend.db(move |engine| spaces::delete_space(engine, &id, &refuge)) +/// Its notes go to `refuge` first, kept or in the trash: the space leaves with whatever nobody +/// takes in. Answers how many live notes it moved. +pub fn delete(backend: &Backend, id: String, refuge: String, held: HeldNotes) -> Pending { + backend.db(move |engine| spaces::delete_space(engine, &id, &refuge, held)) } diff --git a/crates/ui/src/app/captures.rs b/crates/ui/src/app/captures.rs index 04d3aa099..5b69546b1 100644 --- a/crates/ui/src/app/captures.rs +++ b/crates/ui/src/app/captures.rs @@ -2106,3 +2106,47 @@ fn an_update_found_at_the_gate() { settle(&mut harness); capture(&mut harness, "gate-update"); } + +/// The options of a space and of a folder, their deletion asked: what each does to its notes. +#[test] +fn deleting_a_space_and_a_folder() { + let profile = tempfile::tempdir().unwrap(); + let mut harness = launch(profile.path()); + type_into(&mut harness, "vault-passphrase", PASSPHRASE); + type_into(&mut harness, "vault-confirmation", PASSPHRASE); + click(&mut harness, "vault-submit"); + settle(&mut harness); + let backend = &harness.state().backend; + crate::notes::bench::space(backend, "Archives"); + harness.state_mut().state.revision.bump(); + settle(&mut harness); + let options = |harness: &mut Harness<'static, DevNotesApp>, author_id: &str, name: &str| { + harness + .get_by(|node| { + node.author_id() == Some(author_id) + && node.label().is_some_and(|label| label.ends_with(name)) + }) + .click(); + settle(harness); + }; + // The samples space, whatever the system language names it. + let samples = crate::notes::bench::spaces_of_backend(&harness.state().backend) + .into_iter() + .find(|space| space.name != "Archives") + .unwrap() + .name; + options(&mut harness, "space-edit", &samples); + click(&mut harness, "space-delete"); + capture(&mut harness, "space-delete"); + click(&mut harness, "space-delete-held"); + click(&mut harness, "space-delete-held-1"); + capture(&mut harness, "space-delete-trash"); + harness.key_press(egui::Key::Escape); + settle(&mut harness); + options(&mut harness, "folder-edit", "Snippets"); + click(&mut harness, "folder-delete"); + capture(&mut harness, "folder-delete"); + click(&mut harness, "folder-delete-held"); + click(&mut harness, "folder-delete-held-1"); + capture(&mut harness, "folder-delete-trash"); +} diff --git a/crates/ui/src/notes/canvas/board/zone.rs b/crates/ui/src/notes/canvas/board/zone.rs index 6d72b6079..01df03364 100644 --- a/crates/ui/src/notes/canvas/board/zone.rs +++ b/crates/ui/src/notes/canvas/board/zone.rs @@ -13,6 +13,7 @@ use engine::folders::board::{ BoardChild, BoardFrame, BoardNote, BoardZone, CARD_WIDTH, GAP, ZONE_BORDER, ZONE_HEADER, ZONE_PADDING, ZONE_SUBFOLDERS, columns_in, }; +use engine::folders::model::FolderContents; use i18n::keys; use super::gesture::{self, Kind}; @@ -82,7 +83,7 @@ pub(super) fn show( header( ui, head, - (zone, drawn), + (zone, drawn, context.folders.contents(&zone.folder.id)), (context.language, menu, hand), actions, ); @@ -118,7 +119,7 @@ pub(super) fn show( fn header( ui: &mut Ui, head: Rect, - (zone, drawn): (&BoardZone, &Drawn<'_>), + (zone, drawn, contents): (&BoardZone, &Drawn<'_>, Option<&FolderContents>), (language, menu_edit, hand): (i18n::Language, &mut Option, &mut Hand<'_>), actions: &mut Actions, ) { @@ -201,7 +202,7 @@ fn header( ui, menu_room, (zone, drawn.notes.len()), - language, + (contents, language), menu_edit, actions, ); @@ -356,7 +357,7 @@ fn menu( ui: &mut Ui, room: Rect, (zone, held): (&BoardZone, usize), - language: i18n::Language, + (contents, language): (Option<&FolderContents>, i18n::Language), menu_edit: &mut Option, actions: &mut Actions, ) { @@ -395,7 +396,7 @@ fn menu( Some(edit) if edit.id == folder.id => edit, other => other.insert(FolderEdit::open(folder)), }; - if panel::folder(ui, edit, folder, language, actions) { + if panel::folder(ui, edit, (folder, contents), language, actions) { ui.close(); } }); diff --git a/crates/ui/src/notes/header/switchers.rs b/crates/ui/src/notes/header/switchers.rs index ff44d239d..05da9c3f5 100644 --- a/crates/ui/src/notes/header/switchers.rs +++ b/crates/ui/src/notes/header/switchers.rs @@ -230,7 +230,8 @@ pub(super) fn folder_switcher( let Some(folder) = folders.iter().find(|folder| folder.id == edit.id) else { return; }; - if panel::folder(ui, edit, folder, language, actions) { + let contents = state.folders.contents(&folder.id); + if panel::folder(ui, edit, (folder, contents), language, actions) { ui.close(); } } @@ -390,7 +391,8 @@ pub(super) fn breadcrumb( .show(|ui| { ui.set_width(260.0); let editing = edit.get_or_insert_with(|| FolderEdit::open(folder)); - if panel::folder(ui, editing, folder, language, actions) { + let contents = state.folders.contents(&folder.id); + if panel::folder(ui, editing, (folder, contents), language, actions) { ui.close(); } }); diff --git a/crates/ui/src/notes/mod.rs b/crates/ui/src/notes/mod.rs index 00e7684a4..2069a2fbd 100644 --- a/crates/ui/src/notes/mod.rs +++ b/crates/ui/src/notes/mod.rs @@ -21,6 +21,7 @@ use i18n::{Key, keys}; use engine::folders::model::FolderColour; use engine::notes::checklist::ChecklistItem; +use engine::notes::model::HeldNotes; use engine::notes::view::NoteFilter; use crate::action::{Action as Asked, Actions, AppAction}; @@ -57,10 +58,11 @@ pub enum Action { id: String, pinned: bool, }, - /// Its notes go to `refuge` first. + /// Its notes go to `refuge` first, kept or in the trash. DeleteSpace { id: String, refuge: String, + held: HeldNotes, }, CreateFolder { space_id: String, @@ -75,7 +77,12 @@ pub enum Action { id: String, colour: FolderColour, }, - DeleteFolder(String), + /// What deleting it would keep or trash, for its panel. + CountFolder(String), + DeleteFolder { + id: String, + held: HeldNotes, + }, /// Under `parent`, or at the top of `space` for `None`, with everything it holds; refused /// and said when it cannot go there. MoveFolder { diff --git a/crates/ui/src/notes/panel.rs b/crates/ui/src/notes/panel.rs index 964fc06e6..c3b089a21 100644 --- a/crates/ui/src/notes/panel.rs +++ b/crates/ui/src/notes/panel.rs @@ -5,7 +5,8 @@ use crate::theme::tokens::{font, gap, radius}; use egui::{Color32, CornerRadius, FontFamily, Rect, Response, Role, Sense, Stroke, Ui, Vec2}; use egui::{StrokeKind, WidgetInfo}; -use engine::folders::model::{Folder, FolderColour}; +use engine::folders::model::{Folder, FolderColour, FolderContents}; +use engine::notes::model::HeldNotes; use engine::spaces::model::{Space, SpaceContents}; use i18n::{Key, keys}; @@ -129,6 +130,7 @@ pub(super) struct SpaceEdit { focus: bool, /// Among the other spaces, in their order. refuge: usize, + held: HeldNotes, /// Two steps: nothing corpus-wide runs before saying what it touches. confirming: bool, } @@ -140,6 +142,7 @@ impl SpaceEdit { name: space.name.clone(), focus: true, refuge: 0, + held: HeldNotes::Keep, confirming: false, } } @@ -269,7 +272,13 @@ fn deletion( ) -> bool { let tr = |key| i18n::translate(language, key, &[]); push(actions, Action::CountSpace(space.id.clone())); - let move_to = tr(keys::notes::MOVE_NOTES_TO); + if held_choice(ui, &mut edit.held, "space-delete-held", language) { + edit.confirming = false; + } + let move_to = tr(match edit.held { + HeldNotes::Keep => keys::notes::MOVE_NOTES_TO, + HeldNotes::Trash => keys::notes::RESTORE_NOTES_IN, + }); note(ui, &move_to, "space-move-label"); edit.refuge = edit.refuge.min(others.len() - 1); let options: Vec<(usize, &str)> = others @@ -296,6 +305,7 @@ fn deletion( Action::DeleteSpace { id: space.id.clone(), refuge: refuge.id.clone(), + held: edit.held, }, ); close = true; @@ -306,21 +316,51 @@ fn deletion( if let Some(contents) = contents { note( ui, - &consequences(*contents, &refuge.name, language), + &consequences(*contents, (&refuge.name, edit.held), language), "space-delete-consequence", ); } close } -/// The notes and where they go, then the folders that go with the space. -fn consequences(contents: SpaceContents, refuge: &str, language: i18n::Language) -> String { +/// What becomes of the notes: kept, as before, or put in the trash with what holds them. `true` +/// when the choice changed. +fn held_choice( + ui: &mut Ui, + held: &mut HeldNotes, + author_id: &str, + language: i18n::Language, +) -> bool { + let tr = |key| i18n::translate(language, key, &[]); + let label = tr(keys::notes::HELD_NOTES); + note(ui, &label, &format!("{author_id}-label")); + let (keep, trash) = (tr(keys::notes::KEEP_NOTES), tr(keys::notes::TRASH_NOTES)); + let options = [ + (HeldNotes::Keep, keep.as_str()), + (HeldNotes::Trash, trash.as_str()), + ]; + ChoiceMenu::new(&label, author_id, &options) + .show(ui, held) + .changed() +} + +/// The notes and where they go, or that they go to the trash and come back there; then the +/// folders that go with the space. +fn consequences( + contents: SpaceContents, + (refuge, held): (&str, HeldNotes), + language: i18n::Language, +) -> String { let notes = if contents.notes == 0 { i18n::translate(language, keys::notes::DELETE_SPACE_NO_NOTES, &[]) } else { + let key = match held { + HeldNotes::Keep => keys::notes::DELETE_SPACE_NOTES, + HeldNotes::Trash => keys::notes::DELETE_SPACE_TRASH_NOTES, + }; i18n::translate( language, - keys::notes::DELETE_SPACE_NOTES, + key, &[("count", contents.notes.into()), ("refuge", refuge.into())], ) }; @@ -341,6 +381,7 @@ pub(super) struct FolderEdit { focus: bool, /// The sub-folder's name, once asked for: the panel still opens on what most come for. pub(super) nesting: Option, + held: HeldNotes, confirming: bool, } @@ -351,6 +392,7 @@ impl FolderEdit { name: folder.name.clone(), focus: true, nesting: None, + held: HeldNotes::Keep, confirming: false, } } @@ -360,7 +402,7 @@ impl FolderEdit { pub(super) fn folder( ui: &mut Ui, edit: &mut FolderEdit, - folder: &Folder, + (folder, contents): (&Folder, Option<&FolderContents>), language: i18n::Language, actions: &mut Actions, ) -> bool { @@ -403,6 +445,10 @@ pub(super) fn folder( close = true; } close |= sub_folder(ui, edit, folder, language, actions); + push(actions, Action::CountFolder(folder.id.clone())); + if held_choice(ui, &mut edit.held, "folder-delete-held", language) { + edit.confirming = false; + } let label = tr(if edit.confirming { keys::folders::DELETE_CONFIRM } else { @@ -414,21 +460,50 @@ pub(super) fn folder( .show(ui); if delete.clicked() { if edit.confirming { - push(actions, Action::DeleteFolder(folder.id.clone())); + push( + actions, + Action::DeleteFolder { + id: folder.id.clone(), + held: edit.held, + }, + ); close = true; } else { edit.confirming = true; } } - note( - ui, - &tr(keys::folders::DELETE_KEEPS_NOTES), - "folder-delete-note", - ); + if let Some(contents) = contents { + let said = folder_consequences(*contents, edit.held, language); + note(ui, &said, "folder-delete-note"); + } }); close } +/// What the folder's deletion does to the notes and the sub-folders under it, counted by the core. +fn folder_consequences( + contents: FolderContents, + held: HeldNotes, + language: i18n::Language, +) -> String { + let (notes, folders) = match held { + HeldNotes::Keep => ( + keys::folders::DELETE_KEEP_NOTES, + keys::folders::DELETE_KEEP_FOLDERS, + ), + HeldNotes::Trash => ( + keys::folders::DELETE_TRASH_NOTES, + keys::folders::DELETE_TRASH_FOLDERS, + ), + }; + let notes = i18n::translate(language, notes, &[("count", contents.notes.into())]); + if contents.folders == 0 { + return notes; + } + let folders = i18n::translate(language, folders, &[("count", contents.folders.into())]); + format!("{notes} {folders}") +} + fn sub_folder( ui: &mut Ui, edit: &mut FolderEdit, diff --git a/crates/ui/src/notes/sidebar/mod.rs b/crates/ui/src/notes/sidebar/mod.rs index a93857ce0..35d5380ec 100644 --- a/crates/ui/src/notes/sidebar/mod.rs +++ b/crates/ui/src/notes/sidebar/mod.rs @@ -315,7 +315,8 @@ fn folder_row( && edit.id == folder.id { let made_inside = edit.nesting.is_some(); - let closed = panel::folder(ui, edit, folder, language, actions); + let contents = state.folders.contents(&folder.id); + let closed = panel::folder(ui, edit, (folder, contents), language, actions); if closed { if made_inside { view.reveal(&folder.id); diff --git a/crates/ui/src/notes/sidebar/tests.rs b/crates/ui/src/notes/sidebar/tests.rs index 357a0817a..ee1fefd50 100644 --- a/crates/ui/src/notes/sidebar/tests.rs +++ b/crates/ui/src/notes/sidebar/tests.rs @@ -2,6 +2,7 @@ use egui_kittest::Harness; use egui_kittest::kittest::{NodeT, Queryable}; +use engine::folders::board::PreviewLines; use engine::folders::model::FolderColour; use crate::notes::bench::{ @@ -480,3 +481,107 @@ fn a_name_cut_in_the_rail_is_read_whole_on_hover_and_its_menu_stays_in_reach() { assert!(said, "the full name in a tooltip"); harness.get_by(|node| node.author_id() == Some("folder-edit")); } + +fn said_by(harness: &Harness<'_, Bench>, author_id: &str) -> String { + harness + .get_by(|node| node.author_id() == Some(author_id)) + .accesskit_node() + .value() + .unwrap_or_default() +} + +fn trashed(harness: &Harness<'_, Bench>) -> Vec { + let backend = &harness.state().backend; + engine::db::run_here(backend.engine(), |engine| { + engine::notes::list_trash(engine, "") + }) + .unwrap() + .into_iter() + .map(|row| row.note.id) + .collect() +} + +fn latest_report(harness: &Harness<'_, Bench>) -> Option { + let status = &harness.state().state.status; + status.latest().map(|(_, message)| message.key) +} + +#[test] +fn a_space_deleted_with_its_notes_puts_them_in_the_trash_restored_in_its_refuge() { + let (backend, profile) = testing::unlocked(); + space(&backend, "Home"); + let reading = space(&backend, "Reading"); + let draft = testing::snippet(reading.id.clone(), "an article".into()); + let article = engine::db::run_here(backend.engine(), move |engine| { + engine::notes::create_note(engine, draft) + }) + .unwrap() + .note + .id; + let mut harness = area(backend, profile); + + click(&mut harness, "space-edit", "Options for space Reading"); + let kept = said_by(&harness, "space-delete-consequence"); + click_id(&mut harness, "space-delete-held"); + click_id(&mut harness, "space-delete-held-1"); + let trashing = said_by(&harness, "space-delete-consequence"); + assert_ne!(kept, trashing, "the choice switches what is said"); + assert!( + trashing.contains("trash") && trashing.contains("“Home”"), + "{trashing}" + ); + click_id(&mut harness, "space-delete"); + click_id(&mut harness, "space-delete"); + + assert_eq!(names(&harness, "space-option"), ["Home"]); + assert_eq!(trashed(&harness), [article]); + assert_eq!(cards(&harness), 0); + assert_eq!( + latest_report(&harness), + Some(i18n::keys::notes::SPACE_DELETED_TRASHED) + ); +} + +#[test] +fn a_folder_deleted_with_its_notes_takes_its_sub_folder_and_trashes_them() { + let (backend, profile) = testing::unlocked(); + let filed = testing::note(&backend, "filed"); + let deeper = testing::note(&backend, "deeper"); + testing::note(&backend, "loose"); + let spaces = engine::db::run_here(backend.engine(), engine::spaces::list_spaces).unwrap(); + let runbooks = folder(&backend, &spaces[0], None, "Runbooks"); + let api = folder(&backend, &spaces[0], Some(&runbooks), "api"); + for (note, into) in [(&filed, &runbooks), (&deeper, &api)] { + let (ids, into) = ([note.id.clone()], into.id.clone()); + engine::db::run_here(backend.engine(), move |engine| { + engine::folders::file_notes(engine, &ids, Some(&into), PreviewLines::default()) + }) + .unwrap(); + } + let mut harness = area(backend, profile); + + click(&mut harness, "folder-edit", "Options for folder Runbooks"); + let kept = said_by(&harness, "folder-delete-note"); + assert!( + kept.contains("2 notes stay") && kept.contains("sub-folder"), + "{kept}" + ); + click_id(&mut harness, "folder-delete-held"); + click_id(&mut harness, "folder-delete-held-1"); + let trashing = said_by(&harness, "folder-delete-note"); + assert!(trashing.contains("2 notes go to the trash"), "{trashing}"); + click_id(&mut harness, "folder-delete"); + click_id(&mut harness, "folder-delete"); + + assert!(names(&harness, "folder-option").is_empty()); + let mut gone = trashed(&harness); + gone.sort(); + let mut expected = vec![filed.id.clone(), deeper.id.clone()]; + expected.sort(); + assert_eq!(gone, expected); + assert_eq!(cards(&harness), 1, "the loose note stays"); + assert_eq!( + latest_report(&harness), + Some(i18n::keys::folders::DELETED_TRASHED) + ); +} diff --git a/crates/ui/src/state/apply_notes.rs b/crates/ui/src/state/apply_notes.rs index bf3c9d4ae..3c7afe8a7 100644 --- a/crates/ui/src/state/apply_notes.rs +++ b/crates/ui/src/state/apply_notes.rs @@ -298,8 +298,8 @@ impl AppState { Notes::PinSpace { id, pinned } => { self.spaces.pin_space(backend, id, pinned); } - Notes::DeleteSpace { id, refuge } => { - self.spaces.delete_space(backend, id, refuge); + Notes::DeleteSpace { id, refuge, held } => { + self.spaces.delete_space(backend, id, refuge, held); } Notes::CreateFolder { space_id, @@ -308,7 +308,10 @@ impl AppState { } => self.folders.create(backend, space_id, parent_id, &name), Notes::RenameFolder { id, name } => self.folders.rename(backend, id, &name), Notes::RecolourFolder { id, colour } => self.folders.recolour(backend, id, colour), - Notes::DeleteFolder(id) => self.folders.delete(backend, id, self.preview_lines), + Notes::CountFolder(id) => self.folders.count(id), + Notes::DeleteFolder { id, held } => { + self.folders.delete(backend, id, self.preview_lines, held); + } Notes::MoveFolder { id, parent, space } => { self.move_folder(backend, id, (parent, space)); } @@ -376,7 +379,8 @@ impl AppState { | Notes::CreateFolder { .. } | Notes::RenameFolder { .. } | Notes::RecolourFolder { .. } - | Notes::DeleteFolder(_) + | Notes::CountFolder(_) + | Notes::DeleteFolder { .. } | Notes::MoveFolder { .. } | Notes::NudgeFolder { .. }) => self.apply_place(place, backend), Notes::SetView(mode) => self.set_view(mode, backend), diff --git a/crates/ui/src/state/folders.rs b/crates/ui/src/state/folders.rs index 734ae4d05..9988f5ea6 100644 --- a/crates/ui/src/state/folders.rs +++ b/crates/ui/src/state/folders.rs @@ -4,7 +4,8 @@ //! again with it. use engine::folders::board::PreviewLines; -use engine::folders::model::{Folder, FolderColour, FolderMove}; +use engine::folders::model::{Folder, FolderColour, FolderContents, FolderMove}; +use engine::notes::model::HeldNotes; use i18n::{Message, keys}; use super::{QueryCell, Revision}; @@ -13,17 +14,20 @@ use crate::api::undo::Reversible; use crate::backend::{Backend, Pending}; use crate::services::errors::ErrorNotifier; use crate::services::preferences::Preferences; +use crate::services::status::StatusNotifier; /// The library's, like everything under `devnotes.notes.`: an id means nothing elsewhere. pub const ACTIVE_FOLDER_KEY: &str = "devnotes.notes.activeFolder"; enum Write { Folder(Pending), - /// What it held goes up a level, and so does the canvas if it was showing it: to `parent`. + /// What it held goes up a level or to the trash, and the canvas, if it was showing it, to + /// `parent`. Deleted { - pending: Pending<()>, + pending: Pending, folder: String, parent: Option, + held: HeldNotes, }, /// Offered back by the undo bar, named in its words. Moved { @@ -55,6 +59,9 @@ pub struct FoldersState { writes: Vec, /// The palette's « New folder » waits for its name. naming: bool, + /// The folder whose deletion is being considered, and what it would keep or trash. + counted: Option, + contents: QueryCell<(String, Revision), FolderContents>, } impl FoldersState { @@ -77,10 +84,11 @@ impl FoldersState { &mut self, backend: &Backend, revision: &mut Revision, - errors: &mut ErrorNotifier, + (status, errors): (&mut StatusNotifier, &mut ErrorNotifier), ) -> Answered { let mut answered = Answered::default(); let mut left = Vec::new(); + let mut said = Vec::new(); self.writes.retain_mut(|writing| { let answer = match &mut writing.write { Write::Folder(pending) => pending.poll().map(|answer| answer.map(|_| None)), @@ -88,9 +96,13 @@ impl FoldersState { pending, folder, parent, - } => pending - .poll() - .map(|answer| answer.map(|()| Some((folder.clone(), parent.clone())))), + held, + } => pending.poll().map(|answer| { + answer.map(|notes| { + said.push(deleted(*held, notes)); + Some((folder.clone(), parent.clone())) + }) + }), Write::Moved { pending, name, @@ -119,7 +131,13 @@ impl FoldersState { } } }); + for message in said { + status.report(message); + } for (folder, parent) in left { + if self.counted.as_deref() == Some(folder.as_str()) { + self.counted = None; + } if self.chosen.as_deref() == Some(folder.as_str()) { self.chosen = parent; answered.moved = true; @@ -131,6 +149,11 @@ impl FoldersState { { errors.report(keys::errors::FOLDERS_LOAD_FAILED, error); } + if let Some(id) = &self.counted { + self.contents.want((id.clone(), *revision)); + self.contents + .update(|(id, _)| api::folders::contents(backend, id)); + } answered } @@ -223,17 +246,31 @@ impl FoldersState { } /// What it held goes up a level, and so does the canvas if it was showing it. - pub fn delete(&mut self, backend: &Backend, id: String, lines: PreviewLines) { + pub fn delete(&mut self, backend: &Backend, id: String, lines: PreviewLines, held: HeldNotes) { let parent = self.find(&id).and_then(|folder| folder.parent_id.clone()); let failed = Message::new(keys::errors::FOLDER_DELETE_FAILED); let write = Write::Deleted { - pending: api::folders::delete(backend, id.clone(), lines), + pending: api::folders::delete(backend, id.clone(), lines, held), folder: id, parent, + held, }; self.push(write, failed); } + pub fn count(&mut self, id: String) { + if self.counted.as_deref() != Some(id.as_str()) { + self.contents = QueryCell::default(); + self.counted = Some(id); + } + } + + pub fn contents(&self, id: &str) -> Option<&FolderContents> { + (self.counted.as_deref() == Some(id)) + .then(|| self.contents.value()) + .flatten() + } + /// Under `parent_id` of `space_id`, or at its top for `None`, with everything it holds; /// `name` and `into` are what the undo bar says. The rules are `Lineage`'s, in the core; a /// move it refuses is said, and changes nothing. @@ -262,3 +299,12 @@ fn trimmed(name: &str) -> Option { let name = name.trim(); (!name.is_empty()).then(|| name.to_owned()) } + +/// How many notes stayed in the space, or went to the trash. +fn deleted(held: HeldNotes, notes: u32) -> Message { + let key = match held { + HeldNotes::Keep => keys::folders::DELETED, + HeldNotes::Trash => keys::folders::DELETED_TRASHED, + }; + Message::new(key).with("count", notes) +} diff --git a/crates/ui/src/state/mod.rs b/crates/ui/src/state/mod.rs index 0f079f51d..7d6d705c8 100644 --- a/crates/ui/src/state/mod.rs +++ b/crates/ui/src/state/mod.rs @@ -383,15 +383,13 @@ impl AppState { } self.caches.thumbnails.update(ctx, backend); self.caches.pictures.update(ctx, backend); - let mut moved = self - .spaces - .update(backend, &mut self.revision, &mut self.errors); + let notifiers = (&mut self.status, &mut self.errors); + let mut moved = self.spaces.update(backend, &mut self.revision, notifiers); if moved { self.folders.leave(); } - let answered = self - .folders - .update(backend, &mut self.revision, &mut self.errors); + let notifiers = (&mut self.status, &mut self.errors); + let answered = self.folders.update(backend, &mut self.revision, notifiers); for done in answered.done { self.undo.record(done, time); } diff --git a/crates/ui/src/state/spaces.rs b/crates/ui/src/state/spaces.rs index 25d356c92..d31781d15 100644 --- a/crates/ui/src/state/spaces.rs +++ b/crates/ui/src/state/spaces.rs @@ -2,6 +2,7 @@ //! and not a waiting state. Writes are not optimistic: each answer bumps the revision, and the //! list is asked again with it. The folders inside them are `state::folders`'. +use engine::notes::model::HeldNotes; use engine::spaces::model::{Space, SpaceContents}; use i18n::{Message, keys}; @@ -10,6 +11,7 @@ use crate::api; use crate::backend::{Backend, Pending}; use crate::services::errors::ErrorNotifier; use crate::services::preferences::Preferences; +use crate::services::status::StatusNotifier; /// The library's, like everything under `devnotes.notes.`: an id means nothing elsewhere. pub const ACTIVE_SPACE_KEY: &str = "devnotes.notes.activeSpace"; @@ -18,10 +20,12 @@ enum Write { /// Shown once made: its id arrives with the answer. Made(Pending), Changed(Pending), - /// Its refuge is shown once it is gone: the notes have just landed there. + /// Its refuge is shown once it is gone: the notes have just landed there. What it did is + /// said with the refuge's name. Deleted { - pending: Pending<()>, - refuge: String, + pending: Pending, + refuge: (String, String), + held: HeldNotes, }, } @@ -61,18 +65,26 @@ impl SpacesState { &mut self, backend: &Backend, revision: &mut Revision, - errors: &mut ErrorNotifier, + (status, errors): (&mut StatusNotifier, &mut ErrorNotifier), ) -> bool { let mut shown = Vec::new(); + let mut said = Vec::new(); self.writes.retain_mut(|writing| { let answer = match &mut writing.write { Write::Made(pending) => pending .poll() .map(|answer| answer.map(|space| Some(space.id))), Write::Changed(pending) => pending.poll().map(|answer| answer.map(|_| None)), - Write::Deleted { pending, refuge } => pending - .poll() - .map(|answer| answer.map(|()| Some(refuge.clone()))), + Write::Deleted { + pending, + refuge: (id, name), + held, + } => pending.poll().map(|answer| { + answer.map(|moved| { + said.push(deleted(*held, name, moved)); + Some(id.clone()) + }) + }), }; match answer { None => true, @@ -87,6 +99,9 @@ impl SpacesState { } } }); + for message in said { + status.report(message); + } let moved = !shown.is_empty(); if let Some(space) = shown.pop() { self.chosen = Some(space); @@ -174,14 +189,21 @@ impl SpacesState { } /// The refuge is then shown: the notes have just landed there. - pub fn delete_space(&mut self, backend: &Backend, id: String, refuge: String) { + pub fn delete_space(&mut self, backend: &Backend, id: String, refuge: String, held: HeldNotes) { if id == refuge { return; } let failed = Message::new(keys::errors::SPACE_DELETE_FAILED); + let name = self + .spaces() + .iter() + .find(|space| space.id == refuge) + .map(|space| space.name.clone()) + .unwrap_or_default(); let write = Write::Deleted { - pending: api::spaces::delete(backend, id, refuge.clone()), - refuge, + pending: api::spaces::delete(backend, id, refuge.clone(), held), + refuge: (refuge, name), + held, }; self.push(write, failed); } @@ -191,6 +213,16 @@ impl SpacesState { } } +/// How many notes went where: into the refuge, or into the trash. +fn deleted(held: HeldNotes, refuge: &str, moved: u32) -> Message { + match held { + HeldNotes::Keep => Message::new(keys::notes::SPACE_DELETED) + .with("count", moved) + .with("refuge", refuge), + HeldNotes::Trash => Message::new(keys::notes::SPACE_DELETED_TRASHED).with("count", moved), + } +} + fn trimmed(name: &str) -> Option { let name = name.trim(); (!name.is_empty()).then(|| name.to_owned()) diff --git a/docs/architecture.md b/docs/architecture.md index 663ff2ea4..03fce024f 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -2721,7 +2721,12 @@ space the panel explains why rather than offering a button that could only fail. goes before the first click**: `count_space_contents` answers the space's live notes and its folders when the panel opens, and the panel words them with the refuge chosen — « Ses 12 notes iront dans « Perso », hors de tout dossier. Ses 2 dossiers partent avec lui. » — since the -folders cascade with the space and nothing undoes it. Each space +folders cascade with the space and nothing undoes it. **Its notes are kept, or put in the trash** +(« Ses notes » : « Les garder » / « Les mettre à la corbeille », `notes::model::HeldNotes`): trashed, +they still go to the refuge, which a restore brings them back into; nothing is erased outside an +explicit emptying of the trash. A folder's panel offers the same choice, counted by +`count_folder_contents` (its live notes and sub-folders, all the way down). Either deletion is +then said through the status notifier, with how many notes went where. Each space row is a `role="none"` wrapper holding the select button and the `⋯` trigger, so the menu keeps its direct menuitem children. Arrow-key navigation stays on the select buttons only. @@ -2754,7 +2759,10 @@ holds is deliberately small: also why `delete_folder` takes no refuge argument where `delete_space(id, targetSpaceId)` must: what the folder held goes to **its parent** — its notes filed there, its sub-folders each to a free slot on that board — or loose at the top of the space. That is done in Rust, in one -transaction, before the row goes; `parent_id`'s own `SET NULL` is only a guard. +transaction, before the row goes; `parent_id`'s own `SET NULL` is only a guard. With +`HeldNotes::Trash` the folder takes its whole sub-tree instead: every note under it is trashed, +then the folders are deleted, and the notes' `SET NULL` leaves them to come back loose in the +space. **A folder holds folders, one tree per space.** Every rule about it is `folders::model::Lineage`, built from ids and parents alone, so no name is opened to answer one: the sub-tree, the path, the @@ -4586,7 +4594,8 @@ the library into rows; what nests in what is `folders::model::Lineage`'s, siblin `view::fold` of their name. A row's ⋯ opens its panel under it, one at a time: a space is pinned, renamed (written when the field is left, or on Enter), and deleted in two steps with a refuge chosen and what moves counted by the core first; a folder is recoloured, renamed, given a -sub-folder, and deleted, what it held going up a level. A folder's row is dragged onto another +sub-folder, and deleted, what it held going up a level. Either keeps its notes, or puts them in +the trash with it. A folder's row is dragged onto another row — into that folder, or to the top of that space — or moved by `Alt+←` (up a level) and `Alt+→` (into the sibling above it); there is no order of siblings, so no `Alt+↑/↓`. The rail records its rows each frame and reports the gesture; whether the folder may go there is @@ -4942,7 +4951,7 @@ The service functions, grouped by the feature that owns them (`crates/engine/src | ------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `notes` | `count_notes_tagged`, `create_note`, `delete_note`, `delete_notes`, `delete_tags`, `empty_trash`, `fill_placeholders`, `get_note`, `list_global_placeholders`, `list_revisions`, `list_tags`, `list_trash`, `move_notes`, `move_notes_back`, `purge_notes`, `query_notes`, `rename_tags`, `restore_notes`, `restore_revision`, `revision_diff`, `seed_samples`, `set_global_placeholders`, `set_placeholder_values`, `tag_notes`, `untag_notes`, `update_note` | | `spaces` | `count_space_contents`, `create_space`, `delete_space`, `list_spaces`, `pin_space`, `rename_space` | -| `folders` | `arrange_board`, `board_view`, `create_folder`, `delete_folder`, `file_notes`, `file_notes_back`, `list_folders`, `recolour_folder`, `rename_folder`, `save_board_layout` | +| `folders` | `arrange_board`, `board_view`, `count_folder_contents`, `create_folder`, `delete_folder`, `file_notes`, `file_notes_back`, `list_folders`, `recolour_folder`, `rename_folder`, `save_board_layout` | | `attachments` | `attach_clipboard_image`, `attach_file`, `delete_attachment`, `list_attachments`, `open_attachment`, `read_attachment`, `save_attachment` | | `transfer` | `count_export`, `export_is_protected`, `export_notes` (over an `ExportScope`: the library, a space or a selection), `import_notes`, `share_notes` | | `vault` | `change_passphrase`, `create_vault`, `unlock_vault`, `vault_state` | @@ -4963,7 +4972,8 @@ easily as the note it just opened. `delete_space` takes a **refuge** (`target_space_id`). The refuge is not optional: `notes.space_id` carries an `ON DELETE CASCADE`, so a bare delete would take the notes with it. `spaces::store::delete` moves them and drops the space in one -transaction, in that order, and deliberately leaves `updated_at` alone — the canvas orders on +transaction, in that order — with `HeldNotes::Trash`, the live notes are trashed before the move, +or the refuge's own would be among them — and deliberately leaves `updated_at` alone — the canvas orders on that column, and refreshing it would float the whole absorbed space to the top as if every note had just been edited. A space cannot be its own refuge (`spaces::model::validate_move_target`); the cascade would take the notes back out one statement after the move. diff --git a/e2e/tests/05_spaces.rs b/e2e/tests/05_spaces.rs index 63cabfbb0..e7338f4f7 100644 --- a/e2e/tests/05_spaces.rs +++ b/e2e/tests/05_spaces.rs @@ -1,5 +1,6 @@ //! Spaces from the library rail: made, renamed, pinned, chosen, remembered, and deleted with -//! their notes moved out first. Only a real database proves the cascade never fires. +//! their notes moved out first, or put in the trash. Only a real database proves the cascade +//! never fires. //! //! Nothing compares a seeded word: the samples are in the system's language. The space made //! here is named by the scenario, and found by that name. @@ -9,6 +10,7 @@ use egui::accesskit::{Node, Toggled}; use egui::{Key, Modifiers}; const CARDS: usize = 6; +const DOOMED: &str = "Brouillon à jeter"; fn named(name: &str) -> impl Fn(&Node) -> bool + '_ { move |node| node.label() == Some(name) @@ -25,6 +27,38 @@ fn is_shown(tree: &e2e::Tree, name: &str) -> bool { .any(|node| node.label() == Some(name) && node.toggled() == Some(Toggled::True)) } +fn deleted_with_its_notes(app: &mut App) { + step( + "a space deleted with its notes puts them in the trash", + || { + app.click("space-create-open"); + app.fill("space-create-input", "Brouillons"); + app.press(Key::Enter, Modifiers::NONE); + app.eventually("the new space is shown", |tree| { + is_shown(tree, "Brouillons") && tree.count("note-card") == 0 + }); + app.click("new-note"); + app.fill("editor-title", DOOMED); + app.fill("editor-body", "à jeter"); + app.click("editor-close"); + app.eventually("its note is a card", |tree| tree.count("note-card") == 1); + app.click_where("space-edit", options_of("Brouillons")); + app.click("space-delete-held"); + app.click("space-delete-held-1"); + app.click("space-delete"); + app.click("space-delete"); + app.eventually("the space is gone, its note not in the refuge", |tree| { + tree.count("space-option") == 1 && tree.count("note-card") == CARDS + }); + app.click("trash-open"); + app.eventually("the note waits in the trash", |tree| { + tree.texts("trash-row-title").contains(&DOOMED.to_owned()) + }); + app.press(Key::Escape, Modifiers::NONE); + }, + ); +} + #[test] fn spaces() { let profile = tempfile::tempdir().unwrap(); @@ -124,5 +158,7 @@ fn spaces() { }, ); + deleted_with_its_notes(&mut app); + app.quit(); } diff --git a/e2e/tests/17_folders.rs b/e2e/tests/17_folders.rs index 71c2e4b46..5f39df7bd 100644 --- a/e2e/tests/17_folders.rs +++ b/e2e/tests/17_folders.rs @@ -2,7 +2,8 @@ //! out, the chip a card wears, a note filed from its card, the notes left standing when the //! folder goes — then folders inside folders: dragged onto another in the rail and offered back, //! the list of an opened folder gathering its sub-tree, its own board one level down, a -//! sub-folder made from its panel, and the folder dragged onto another space with its notes. +//! sub-folder made from its panel, the folder dragged onto another space with its notes, and one +//! deleted with its notes, which wait in the trash. //! //! A space of its own, made here: the samples are in the system's language. @@ -14,6 +15,7 @@ const SPACE: &str = "Dossiers"; const FIRST: &str = "Requête lente"; const SECOND: &str = "Plan d'exécution"; const NESTED: &str = "Index partiel sur jobs"; +const DOOMED: &str = "Brouillon à jeter"; fn named(name: &str) -> impl Fn(&Node) -> bool + '_ { move |node| node.label() == Some(name) @@ -318,6 +320,36 @@ fn to_another_space(app: &mut App) { ); } +fn deleted_with_its_notes(app: &mut App) { + step( + "a folder deleted with its notes puts them in the trash", + || { + make_folder(app, "Jetable"); + app.eventually("the folder is listed", |tree| { + rows_named(tree, "Jetable") == 1 + }); + app.click_where("folder-option", named("Jetable")); + app.wait_for("folder-breadcrumb-name"); + write(app, DOOMED); + app.click_where("folder-edit", |node| { + node.label().is_some_and(|label| label.contains("Jetable")) + }); + app.click("folder-delete-held"); + app.click("folder-delete-held-1"); + app.click("folder-delete"); + app.click("folder-delete"); + app.eventually("the folder is gone, its note with it", |tree| { + rows_named(tree, "Jetable") == 0 && !titles(tree).contains(&DOOMED.to_owned()) + }); + app.click("trash-open"); + app.eventually("the note waits in the trash", |tree| { + tree.texts("trash-row-title").contains(&DOOMED.to_owned()) + }); + app.press(Key::Escape, Modifiers::NONE); + }, + ); +} + #[test] fn folders() { let profile = tempfile::tempdir().unwrap(); @@ -329,5 +361,6 @@ fn folders() { filing(&mut app); nested(&mut app); to_another_space(&mut app); + deleted_with_its_notes(&mut app); app.quit(); }