diff --git a/crates/review/src/ui/balloons.rs b/crates/review/src/ui/balloons.rs index aebca5636..db469fa4a 100644 --- a/crates/review/src/ui/balloons.rs +++ b/crates/review/src/ui/balloons.rs @@ -88,7 +88,9 @@ pub enum BalloonPlace { /// card's top is its anchor's, or the bottom of the card before it plus a gap when that is lower. /// The stacking starts again at each page. /// - A comment card's message is cut to ten rows, so a long body never pushes the next card out of view. -/// - The selected card shows Reply, Resolve or Reopen, and Delete; a click on a card selects it. +/// - The selected card shows Reply, Resolve or Reopen, and Delete; a click on a card selects it. A card selected +/// again keeps its own edit draft (one the app could not write back), and an edit takes the keyboard in the pass +/// it opens, so one click on a balloon whose edit the app opens is enough to type. /// - A dashed leader runs on the page to the page's edge, a solid one on to the card. #[must_use = "show the balloons with `show`"] #[derive(Pattern)] diff --git a/crates/review/src/ui/comments.rs b/crates/review/src/ui/comments.rs index df812b59a..5dd95be01 100644 --- a/crates/review/src/ui/comments.rs +++ b/crates/review/src/ui/comments.rs @@ -262,7 +262,9 @@ pub(crate) fn age_text(env: &EnvView<'_>, then: i64, now: i64) -> String { /// cards. A card shows the kind's icon, author, age (relative under a week, then the date), the /// text, a status icon, the checkmark, a lock icon and the replies, each with its own "..." menu. /// - The selected card has the accent bar, the reply box and "..." (Edit, Set status, Delete). -/// Locked threads and `read_only` hide the editing items. +/// Locked threads and `read_only` hide the editing items. An inline editor takes the keyboard in +/// the pass it opens. Selecting another card drops the reply draft and the edit of another +/// thread; a card selected again keeps its own edit draft. /// - Keyboard: Up and Down move between cards (selecting them), Enter puts the focus in the selected /// card's reply box, Cmd+Enter (Ctrl+Enter off macOS) in the reply box or an inline editor posts, /// Escape in a control of a card returns to the card. @@ -356,13 +358,17 @@ fn toggle(list: &mut Vec, value: T) { } } -/// Makes `thread` the selected one; false when it already was. +/// Makes `thread` the selected one; false when it already was. The reply draft and an edit of another thread go; an +/// edit of `thread` itself stays, so a draft the app could not write back (a document that refused it) is there again +/// when its card is selected again. fn select_thread(state: &mut CommentsPanelState, thread: ThreadId) -> bool { let changed = state.selected != Some(thread); if changed { state.selected = Some(thread); state.reply.clear(); - state.editing = None; + if state.editing.as_ref().is_some_and(|(editing, ..)| *editing != thread) { + state.editing = None; + } } changed } diff --git a/crates/review/src/ui/comments/row.rs b/crates/review/src/ui/comments/row.rs index 7a3db5970..5ff80a465 100644 --- a/crates/review/src/ui/comments/row.rs +++ b/crates/review/src/ui/comments/row.rs @@ -5,7 +5,7 @@ use std::sync::Arc; use craft_behavior::{IconId, Metrics, PartKey, Proposal, Sense}; use craft_theme::variant::Variant; use craft_ui::buttons::{Button, IconButton}; -use craft_ui::contract::{Bind, Common, Component, Cx, MeasureCx, PaintUi, PartsCx, Props as _, Role, WordSetCx as _, clicked}; +use craft_ui::contract::{Bind, Common, Component, Cx, MeasureCx, PaintUi, PartsCx, Props as _, Remember as _, Role, WordSetCx as _, clicked}; use craft_ui::controls::disclosure::DisclosureGlyph; use craft_ui::primitives::{HAlign, IconSpec, PaintShape, StrokeWidth, TextRun, Wrap}; use craft_ui::runtime::env::Label; @@ -164,6 +164,10 @@ pub(crate) enum CardActions { Buttons, } +/// The message whose inline editor a row showed at its last pass, if any. +#[derive(Clone, Copy, Default)] +struct EditShown(Option); + /// A card or a heading of the card collection. #[derive(craft_ui::contract::Component, craft_ui::contract::Props)] pub(crate) struct CommentRow<'r> { @@ -181,6 +185,9 @@ pub(crate) struct CommentRow<'r> { /// The activation key reached the card this pass: its reply box takes the focus, when it shows one. #[prop] focus_reply: bool, + /// The inline editor opened this pass (it did not show the pass before): its field takes the keyboard, so the + /// first click that opens an edit is enough to type. + focus_edit: bool, /// What the card's buttons are. actions: CardActions, /// The most rows of a message's text the card shows, if limited. @@ -208,6 +215,7 @@ impl<'r> CommentRow<'r> { editing: None, reply: "", focus_reply: false, + focus_edit: false, actions: CardActions::default(), body_rows: None, words: None, @@ -509,6 +517,8 @@ struct Paint<'a, 'b> { ctx: &'a egui::Context, /// Set when the reply box takes the focus as it shows. focus_reply: bool, + /// Set when the inline editor's field takes the focus as it shows. + focus_edit: bool, /// The reply field's id once it is shown, for the Reply button to focus. reply_id: Option, events: &'a mut Vec, @@ -618,6 +628,8 @@ impl Sink for Paint<'_, '_> { if self.focus_reply { focus::request(self.ctx, FocusTarget::Id(out.response.id)); } + } else if self.focus_edit { + focus::request(self.ctx, FocusTarget::Id(out.response.id)); } height } @@ -725,6 +737,15 @@ impl Component for CommentRow<'_> { } } + /// Notes whether the inline editor opened this pass: the row remembers which message's editor it showed, and an + /// editor it did not show the pass before takes the keyboard. + fn update(&mut self, cx: &mut Cx<'_, Self>) { + let editing = self.editing.map(|(index, _)| index); + let shown = cx.memory::(); + self.focus_edit = editing.is_some() && shown.0 != editing; + shown.0 = editing; + } + fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) { self.words = Some(cx.words::()); let st = cx.style; @@ -760,6 +781,7 @@ impl Component for CommentRow<'_> { buttons: self.buttons, ctx: &cx.ctx, focus_reply: self.focus_reply, + focus_edit: self.focus_edit, reply_id: None, events: &mut events, }; diff --git a/crates/review/tests/it/balloons.rs b/crates/review/tests/it/balloons.rs index a93be4a5d..607016930 100644 --- a/crates/review/tests/it/balloons.rs +++ b/crates/review/tests/it/balloons.rs @@ -127,6 +127,48 @@ fn selecting_a_card_and_posting_a_reply_report_the_matching_edits() { assert!(shown.state.borrow().reply.is_empty(), "a posted reply clears the binding"); } +/// An edit draft the app could not write back stays when its card is selected again, as the old WordCraft kept a +/// refused draft for when its balloon was selected again; selecting another card drops it. +#[trial] +fn reselecting_a_card_keeps_its_edit_draft() { + let threads = vec![(thread(1, "First"), 100.0), (thread(2, "Second"), 400.0)]; + // The app left the balloon (no card selected) and kept the draft the document refused. + let left = CommentsPanelState { editing: Some((ThreadId(1), 0, "Edited".into())), ..CommentsPanelState::default() }; + let mut shown = Shown::new(threads, left, BalloonPlace::Column, room(), Direction::Ltr); + // A click on the card's heading, above the open edit's field. + let card = shown.card(1); + shown.fixture.click_at(card.left_top() + vec2(30.0, 8.0)).settle(); + assert_eq!(shown.state.borrow().selected, Some(ThreadId(1)), "the click selects the card"); + assert_eq!(shown.state.borrow().editing, Some((ThreadId(1), 0, "Edited".into())), "the card's own draft survives its selection"); + + let other = shown.card(2); + shown.fixture.click_at(other.center()).settle(); + assert_eq!(shown.state.borrow().selected, Some(ThreadId(2))); + assert_eq!(shown.state.borrow().editing, None, "selecting another card drops the draft, as before"); +} + +/// The inline editor takes the keyboard in the pass it opens, so typing needs no second click on its field, as the +/// old WordCraft's balloon took typing once selected. An editor that stays open does not take the keyboard back. +#[trial] +fn an_edit_that_opens_takes_the_keyboard() { + let threads = vec![(thread(1, "First"), 100.0)]; + let selected = CommentsPanelState { selected: Some(ThreadId(1)), ..CommentsPanelState::default() }; + let mut shown = Shown::new(threads, selected, BalloonPlace::Column, room(), Direction::Ltr); + let words = CommentsWords::default(); + // The app opens the edit of the selected card's comment, as on a click on its balloon. + shown.state.borrow_mut().editing = Some((ThreadId(1), 0, "First".into())); + shown.fixture.settle(); + shown.fixture.type_text("!").settle(); + let draft = shown.state.borrow().editing.as_ref().map(|(.., text)| text.clone()); + assert!(draft.as_deref().is_some_and(|text| text.contains('!') && text.len() == "First!".len()), "typing reaches the edit that opened: {draft:?}"); + + // The reply field takes the keyboard; the open edit does not take it back. + let reply = shown.fixture.by_label(words.reply.as_str()).expect("the selected card's reply field"); + shown.fixture.click(&reply).settle(); + shown.fixture.type_text("x").settle(); + assert_eq!(shown.state.borrow().reply, "x", "the reply field keeps the keyboard"); +} + /// Guards `CommentBalloons::read_only`: while comments cannot change, a selected card takes no typing, as the panel's /// rows do: it shows no reply field, and text typed after a click on it reaches no reply. #[trial]