diff --git a/crates/review/src/comments.rs b/crates/review/src/comments.rs index 1530355ed..ed5536318 100644 --- a/crates/review/src/comments.rs +++ b/crates/review/src/comments.rs @@ -139,6 +139,10 @@ pub struct CommentThread { pub messages: Vec, /// Each reviewer's latest status, oldest first. pub statuses: Vec, + /// The statuses reviewers set on the thread's replies rather than on the comment: the search finds their text, + /// and the card shows only [`statuses`](Self::statuses). + #[serde(default)] + pub reply_statuses: Vec, /// Acrobat's per-comment checkmark. pub marked: bool, /// A locked thread can't be edited. @@ -259,12 +263,12 @@ pub fn sort_threads(threads: &[CommentThread], sort: CommentSort, page_order: Pa } /// Whether `thread` matches the search `query`: in any message's text or author, the text of a -/// reviewer's status reply, or the reader's name of the kind (`kind_name`), ignoring case (and the +/// reviewer's status reply on the comment or on one of its replies, or the reader's name of the kind (`kind_name`), ignoring case (and the /// Turkish dotted and dotless i, as PdfCraft's `fold_case` does) through `folder`. // pdfcraft: crates/ui-egui/src/panels/comments_panel.rs:104 `matches`, `:107` `any_contains`. pub fn matches(thread: &CommentThread, query: &str, folder: &Folder, kind_name: impl Fn(AnnotationKind) -> String) -> bool { let has = |text: &str| folder.contains(text, query); thread.kind.is_some_and(|kind| has(&kind_name(kind))) || thread.messages.iter().any(|m| has(&m.text) || has(&m.author)) - || thread.statuses.iter().any(|reviewer| has(&reviewer.text)) + || thread.statuses.iter().chain(&thread.reply_statuses).any(|reviewer| has(&reviewer.text)) } diff --git a/crates/review/src/ui/annotate/tool.rs b/crates/review/src/ui/annotate/tool.rs index a70145e28..b7d763aa4 100644 --- a/crates/review/src/ui/annotate/tool.rs +++ b/crates/review/src/ui/annotate/tool.rs @@ -375,7 +375,7 @@ pub enum HandlesWhileDrawing { /// The press continues the drawing. #[default] Draw, - /// The press edits through the handle. + /// A press on a resize handle edits through it; one on an end or corner point continues the drawing. Edit, } @@ -624,8 +624,8 @@ impl AnnotateTool { } /// The selection rule of a press on an annotation's body and of a click on its part: a second click opens it (by - /// [`OpenOn`]), the toggle modifiers toggle it, and a plain press on an unselected one selects only it. Returns - /// whether it opened. + /// [`OpenOn`]), the toggle modifiers toggle it, a plain press on an unselected one selects only it and on a + /// selected one makes it the primary, keeping the others. Returns whether it opened. fn select_part(&mut self, target: AnnotationId, mods: Mods, clicks: u32, words: &AnnotateWords) -> bool { let selected = self.selected.contains(&target); if clicks >= 2 && self.opens(target) { @@ -639,13 +639,19 @@ impl AnnotateTool { self.select(SelectionChange::Toggle(target), words); } else if !selected { self.select(SelectionChange::Set(vec![target]), words); + } else if self.primary() != [target] { + // A set's last key is its lead: the primary, whose handles show. + let mut ids: Vec = self.selected.iter().copied().filter(|id| *id != target).collect(); + ids.push(target); + self.select(SelectionChange::Set(ids), words); } false } /// The handle of the primary selection under `at` that a press of the drawing tool of `kind` edits: only under - /// [`HandlesWhileDrawing::Edit`], for a shape or point kind, and never while a polygon is being clicked out, which - /// keeps its clicks. + /// [`HandlesWhileDrawing::Edit`], for a shape or point kind, never while a polygon is being clicked out, which + /// keeps its clicks, and only a resize handle: a drawing tool draws from where it is pressed, so a new line starts + /// at the end of the one just drawn rather than dragging it. fn handle_while_drawing(&self, kind: AnnotationKind, at: Point, tolerance: f64, icon_side: f64) -> Option<(AnnotationId, AnnotationHandle)> { use AnnotationKind as K; let edits = self.select_options.handles_while_drawing == HandlesWhileDrawing::Edit @@ -656,7 +662,7 @@ impl AnnotateTool { return None; } match hit(&self.annotations, self.primary(), at, tolerance, icon_side) { - Some(AnnotationHit::Handle(target, handle)) => Some((target, handle)), + Some(AnnotationHit::Handle(target, handle @ AnnotationHandle::Box(..))) => Some((target, handle)), _ => None, } } diff --git a/crates/review/src/ui/comments.rs b/crates/review/src/ui/comments.rs index b73ee5545..f4596dfc0 100644 --- a/crates/review/src/ui/comments.rs +++ b/crates/review/src/ui/comments.rs @@ -121,8 +121,10 @@ pub struct CommentsPanelState { pub search_open: bool, /// The thread the next `show` reveals, once. revealing: Option, - /// The message being edited inline: (thread, message index, text). + /// The message being edited inline: (thread, message index, text). [`edit`](Self::edit) opens it with the focus. pub editing: Option<(ThreadId, usize, String)>, + /// The inline editor takes the focus the next time its card is built, once. + focus_editor: bool, /// The selected card's reply draft. pub reply: String, /// The "Add a comment" box's draft. @@ -143,6 +145,14 @@ impl CommentsPanelState { set_selection(self, Selection::single(thread)); self.revealing = Some(thread); } + + /// Opens the inline editor on message `index` of `thread` (0: the comment) with `text`, and has the editor take + /// the focus the next time its card is built, once: for an editor the app opens (a new comment's note), as a + /// double click on a card or the message menu's Edit opens one. + pub fn edit(&mut self, thread: ThreadId, index: usize, text: impl Into) { + self.editing = Some((thread, index, text.into())); + self.focus_editor = true; + } } /// What the panel asks of the app. @@ -455,6 +465,7 @@ fn set_selection(state: &mut CommentsPanelState, next: Selection) -> b if next.lead != state.selected.lead { state.reply.clear(); state.editing = None; + state.focus_editor = false; } state.selected = next; changed @@ -644,6 +655,8 @@ impl CommentsPanel<'_, H> { let mut selection = card_selection(&state.selected); let reply: &str = &state.reply; let editing = state.editing.as_ref().map(|(thread, index, text)| (*thread, *index, text.as_str())); + let focus_editor = state.focus_editor; + let mut editor_built = false; let mut openers = Vec::new(); let out = CommentList::new(label.clone(), Some(Bind::bound(&mut selection)), grouped, select_mode) .revealing(revealing.map(CardKey::Thread)) @@ -656,6 +669,7 @@ impl CommentsPanel<'_, H> { .map_or_else(String::new, |first| words.card_name.fill(&[("author", &first.author), ("text", &first.text)])); let is_lead = lead == Some(thread.id); let editing = editing.filter(|(id, ..)| *id == thread.id).map(|(_, index, text)| (index, text)); + editor_built |= editing.is_some(); if is_lead { openers.extend((0..thread.messages.len()).map(|index| (index, item.part_opener(MORE.at(index as u64))))); } @@ -663,10 +677,15 @@ impl CommentsPanel<'_, H> { .state(chosen.contains(&thread.id), !read_only && !thread.locked, now) .lead(is_lead) .focus_reply(is_lead && item.row.activated_by_key) + .focus_editor(focus_editor && editing.is_some()) .submit_key(reply_submit) .drafts(editing, if is_lead { reply } else { "" }) } }); + // The editor's card took the focus request this pass. + if editor_built { + state.focus_editor = false; + } let list = out.response.clone(); let clicks = out.activation_clicks; let buttons: Vec<(usize, Response)> = @@ -698,7 +717,7 @@ impl CommentsPanel<'_, H> { CollectionEvent::Activate(CardKey::Thread(id)) if clicks == 2 && !read_only => { if let Some(thread) = threads.iter().find(|thread| thread.id == id && !thread.locked) { let text = thread.messages.first().map(|message| message.text.clone()).unwrap_or_default(); - state.editing = Some((id, 0, text)); + state.edit(id, 0, text); } } CollectionEvent::Open(change) => { @@ -777,7 +796,7 @@ fn apply_menu( match action { CardMenu::Edit => { let text = thread.messages.get(index).map(|message| message.text.clone()).unwrap_or_default(); - state.editing = Some((id, index, text)); + state.edit(id, index, text); } CardMenu::Status(status) => edits.push(CommentEdit::SetStatus { thread: id, status }), CardMenu::Copy => edits.push(CommentEdit::CopyText { thread: id, index }), diff --git a/crates/review/src/ui/comments/row.rs b/crates/review/src/ui/comments/row.rs index 76aa4a357..1dfd30c8f 100644 --- a/crates/review/src/ui/comments/row.rs +++ b/crates/review/src/ui/comments/row.rs @@ -18,7 +18,7 @@ use egui::{Color32, CornerRadius, Pos2, Rect, Vec2, vec2}; use super::list::{CardEvent, RowOut}; use super::{CommentsWords, EDIT_LINES, Line, REPLY_LINES, age_text, status_label}; use crate::annotate::AnnotationKind; -use crate::comments::{CommentThread, ReviewStatus, ReviewerStatus, ThreadId}; +use crate::comments::{CommentMessage, CommentThread, ReviewStatus, ReviewerStatus, ThreadId}; use crate::ui::avatar::paint_face; /// The style of a card or heading row. @@ -122,11 +122,16 @@ trait Sink { fn badge_size(&mut self, slot: Slot, label: &str) -> Vec2; /// A status badge showing `label` in the tone of `status`. fn badge(&mut self, slot: Slot, rect: Rect, label: &str, status: ReviewStatus); + /// The extent of reply `index` (a message after the first), from its author line to the bottom of its text. + fn reply(&mut self, index: usize, rect: Rect); } /// The "..." button of message `index` of a card: a part of the row that opens the message's menu. pub(super) const MORE: PartKey = PartKey::new("more"); +/// Reply `index` of a card: the access node that names it. +const REPLY: PartKey = PartKey::new("reply"); + /// A card or a heading of the card collection. #[derive(craft_ui::contract::Component, craft_ui::contract::Props)] pub(crate) struct CommentRow<'r> { @@ -144,6 +149,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 takes the focus this pass, when the card shows one. + #[prop] + focus_editor: bool, /// The key that submits the reply box. submit_key: SubmitKey, words: Option>, @@ -155,6 +163,8 @@ pub(crate) struct CommentRow<'r> { badges: Vec, /// The messages whose "..." button the parts step declared. mores: Vec, + /// Each reply's index and its rect on the screen, from the parts step. + replies: Vec<(usize, Rect)>, events: Vec, } @@ -171,12 +181,14 @@ impl<'r> CommentRow<'r> { editing: None, reply: "", focus_reply: false, + focus_editor: false, submit_key: SubmitKey::default(), words: None, heights: [0.0; 2], buttons: [Vec2::ZERO; 3], badges: Vec::new(), mores: Vec::new(), + replies: Vec::new(), events: Vec::new(), } .label(name) @@ -316,6 +328,7 @@ impl CommentRow<'_> { y = self.statuses(thread, st, Line { at: Pos2::new(pad, y), room }, &words, sink); for index in 1..thread.messages.len() { let Some(message) = thread.messages.get(index) else { break }; + let top = y; sink.avatar(square(Pos2::new(pad, y), h), &message.author); let x = pad + h + gap; let name_room = width - pad - h - gap - x; @@ -337,7 +350,9 @@ impl CommentRow<'_> { }; sink.button(Slot::More(index), square(Pos2::new(width - pad - h, y), h), "more", &words.more, false); y += h.max(head) + gap; - y = self.message(index, thread, st, Line { at: Pos2::new(x, y), room: width - pad - x }, &words, sink) + gap; + let bottom = self.message(index, thread, st, Line { at: Pos2::new(x, y), room: width - pad - x }, &words, sink); + sink.reply(index, Rect::from_min_max(Pos2::new(pad, top), Pos2::new(width - pad, bottom))); + y = bottom + gap; } if self.lead && self.editable { let field = sink.field(Slot::ReplyField, strip(Pos2::new(pad, y), room), &words.reply, self.reply, REPLY_LINES); @@ -407,6 +422,8 @@ struct Measure<'a, 'b, 'r> { more: Vec<(usize, Rect)>, /// The size of each status badge, in the order the row lays them. badges: Vec, + /// The message index and left-to-right rect of each reply. + replies: Vec<(usize, Rect)>, } impl Sink for Measure<'_, '_, '_> { @@ -454,6 +471,10 @@ impl Sink for Measure<'_, '_, '_> { } fn badge(&mut self, _: Slot, _: Rect, _: &str, _: ReviewStatus) {} + + fn reply(&mut self, index: usize, rect: Rect) { + self.replies.push((index, rect)); + } } /// Where the push button `slot`'s measured size is kept. @@ -477,8 +498,9 @@ struct Paint<'a, 'b> { buttons: [Vec2; 3], badges: &'a [Vec2], reply_submit: SubmitKey, - /// Set when the reply box takes the focus as it shows: the context the request goes to. - focus_reply: Option<&'a egui::Context>, + /// The field that takes the focus as it shows (the reply box or the inline editor), and the context the request + /// goes to. + focus: Option<(Slot, &'a egui::Context)>, events: &'a mut Vec, } @@ -578,8 +600,8 @@ impl Sink for Paint<'_, '_> { if out.escaped && slot == Slot::EditField { self.events.push(CardEvent::EditCancel); } - if slot == Slot::ReplyField - && let Some(ctx) = self.focus_reply + if let Some((focused, ctx)) = self.focus + && focused == slot { focus::request(ctx, FocusTarget::Id(out.response.id)); } @@ -614,6 +636,8 @@ impl Sink for Paint<'_, '_> { let badge = Badge::new(label.to_owned()).variant(Variant::look(Emphasis::Soft, badge_tone(status))); let _ = self.ui.child(slot, self.place(rect), badge); } + + fn reply(&mut self, _: usize, _: Rect) {} } impl Component for CommentRow<'_> { @@ -627,9 +651,22 @@ impl Component for CommentRow<'_> { access.node(MORE.at(index as u64), Role::BUTTON).name = craft_ui::contract::Name::Text(words.more.as_str().to_owned()); } } - let Some(first) = self.thread.and_then(|thread| thread.messages.first()) else { return }; - let age = age_text(&cx.env, first.modified.unwrap_or(first.created), self.now); - access.name(craft_ui::contract::Name::Joined(vec![cx.resolved.label.to_owned(), age])); + let Some(thread) = self.thread else { return }; + let Some(first) = thread.messages.first() else { return }; + let named = |name: String, message: &CommentMessage| { + let age = age_text(&cx.env, message.modified.unwrap_or(message.created), self.now); + craft_ui::contract::Name::Joined(if age.is_empty() { vec![name] } else { vec![name, age] }) + }; + access.name(named(cx.resolved.label.to_owned(), first)); + // Each reply is a node of its own inside the card, named as the card is by its comment. + let Some(words) = &self.words else { return }; + for &(index, rect) in &self.replies { + let Some(message) = thread.messages.get(index) else { continue }; + let name = words.card_name.fill(&[("author", &message.author), ("text", &message.text)]); + let node = access.node(REPLY.at(index as u64), Role::GROUP); + node.name = named(name, message); + node.bounds(rect); + } } fn style(cx: &StyleCx<'_>, _: CommentRowStyleKey) -> CommentRowStyle { @@ -661,7 +698,7 @@ impl Component for CommentRow<'_> { self.words = Some(cx.words::()); let now = self.now; let age = |then: i64| age_text(&cx.env, then, now); - let mut sink = Measure { cx, fields: [0.0; 2], buttons: [Vec2::ZERO; 3], more: Vec::new(), badges: Vec::new() }; + let mut sink = Measure { cx, fields: [0.0; 2], buttons: [Vec2::ZERO; 3], more: Vec::new(), badges: Vec::new(), replies: Vec::new() }; let height = self.lay(cx.style, width, &age, &mut sink); self.heights = sink.fields; self.buttons = sink.buttons; @@ -673,9 +710,10 @@ impl Component for CommentRow<'_> { self.words = Some(cx.words::()); let (now, rtl) = (self.now, cx.flow.is_rtl()); let age = |then: i64| age_text(&cx.env, then, now); - let mut sink = Measure { cx, fields: self.heights, buttons: self.buttons, more: Vec::new(), badges: Vec::new() }; + let mut sink = Measure { cx, fields: self.heights, buttons: self.buttons, more: Vec::new(), badges: Vec::new(), replies: Vec::new() }; self.lay(cx.style, rect.width(), &age, &mut sink); self.mores = sink.more.iter().map(|(index, _)| *index).collect(); + self.replies = sink.replies.iter().map(|&(index, local)| (index, placed(rect.min, rect.width(), rtl, local))).collect(); for (index, local) in sink.more { cx.part(MORE.at(index as u64), placed(rect.min, rect.width(), rtl, local)) .sense(Sense::CLICK | Sense::FOCUS | Sense::TAKE_FOCUS) @@ -711,7 +749,11 @@ impl Component for CommentRow<'_> { buttons: self.buttons, badges: &self.badges, reply_submit: self.submit_key, - focus_reply: self.focus_reply.then_some(&cx.ctx), + focus: if self.focus_editor { + Some((Slot::EditField, &cx.ctx)) + } else { + self.focus_reply.then_some((Slot::ReplyField, &cx.ctx)) + }, events: &mut events, }; self.lay(st, rect.width(), &age, &mut sink); diff --git a/crates/review/src/ui/comments/stories.rs b/crates/review/src/ui/comments/stories.rs index ef4ef4a3b..6eca81a50 100644 --- a/crates/review/src/ui/comments/stories.rs +++ b/crates/review/src/ui/comments/stories.rs @@ -31,6 +31,7 @@ fn thread(id: u64, page: usize, text: &str, replies: usize) -> CommentThread { color: Some([255, 200, 0]), messages, statuses: if id == 1 { vec![reviewer("Bo", ReviewStatus::Accepted)] } else { Vec::new() }, + reply_statuses: Vec::new(), marked: id == 3, locked: false, resolved: false, diff --git a/crates/review/src/ui/thread/note_popup.rs b/crates/review/src/ui/thread/note_popup.rs index 3afa9b077..4be4fc7fd 100644 --- a/crates/review/src/ui/thread/note_popup.rs +++ b/crates/review/src/ui/thread/note_popup.rs @@ -8,7 +8,7 @@ use craft_theme::variant::Variant; use craft_ui::buttons::{Button, IconButton}; use craft_ui::contract::overlay::{PlacementChange, PlacementKind, ResizeEdges}; use craft_ui::contract::{ - Bind, BindMut, BodyCx, Common, Component, Container, ContainerProps, Cx, DragBand, HeaderRegion, MeasureCx, Mount, NoValue, Outcome, Overlay, + Bind, BindMut, BodyCx, Common, Component, Container, ContainerProps, Cx, DragBand, HeaderRegion, MeasureCx, Mount, NoValue, Outcome, Overlay, Pad, PaintUi, Pattern, Props as _, Role, Show as _, ShowContainer as _, SizeLimits, clicked, }; use craft_ui::display::{Shade, Text}; @@ -95,8 +95,8 @@ impl NoteDrafts { } } -/// The pop-up's body: the messages, each indented by its depth, then the reply box. It reads and writes the host's -/// drafts and gathers what the pass committed. +/// The pop-up's body: the note's text and Reply, then each reply as a panel holding the panels of its answers. It reads +/// and writes the host's drafts and gathers what the pass committed. struct NoteBody<'a, 'm> { id: u64, messages: &'a [NotePopupMessage<'m>], @@ -110,21 +110,43 @@ struct NoteBody<'a, 'm> { impl NoteBody<'_, '_> { fn show(&mut self, ui: &mut Ui, words: &ThreadWords, view: &EnvView<'_>) { let messages = self.messages; + let (answers, replies) = nest(messages); Stack::new().fill_width().show(ui, |ui, _| { - for (index, message) in messages.iter().enumerate() { - let depth = u8::try_from(message.depth.min(MAX_NESTING)).unwrap_or(u8::MAX); - // The indent steps are the theme's, from the start edge. - Stack::new().fill_width().indent(depth).show(ui, |ui, _| { - if index > 0 { - byline(ui, message, view, self.now); - } - if index == 0 && self.allowed && message.editable { - self.note_text(ui, message, words); - } else { - Text::new(message.text.to_owned()).show(ui); - } - if self.allowed { - self.reply_box(ui, words, message.id); + if let Some(note) = messages.first() { + if self.allowed && note.editable { + self.note_text(ui, note, words); + } else { + Text::new(note.text.to_owned()).show(ui); + } + if self.allowed { + self.reply_box(ui, words, note.id); + } + } + for &at in &replies { + self.reply_panel(ui, words, view, &answers, at); + } + }); + } + + /// The panel of reply `at`: its author line, text and Reply, then the panels of its answers (`answers[at]`), one + /// indent step in. A reply's answers sit deeper than it and no deeper than [`MAX_NESTING`], so this recurses no + /// deeper than that. + fn reply_panel(&mut self, ui: &mut Ui, words: &ThreadWords, view: &EnvView<'_>, answers: &[Vec], at: usize) { + let messages = self.messages; + let Some(message) = messages.get(at) else { return }; + let name = words.pin.fill(&[("author", message.author)]); + Stack::new().surface(SurfaceKind::Frame).pad(Pad::Control).fill_width().label(name).show(ui, |ui, _| { + byline(ui, message, view, self.now); + Text::new(message.text.to_owned()).show(ui); + if self.allowed { + self.reply_box(ui, words, message.id); + } + let own = answers.get(at).map_or(&[][..], Vec::as_slice); + if !own.is_empty() { + // The indent step is the theme's, from the start edge. + Stack::new().fill_width().indent(1).show(ui, |ui, _| { + for &answer in own { + self.reply_panel(ui, words, view, answers, answer); } }); } @@ -178,6 +200,26 @@ impl NoteBody<'_, '_> { } } +/// The thread of `messages` (the note first, each reply after the message it answers, by depth): for each message the +/// replies whose panels sit in its panel, and the replies to the note. A reply deeper than [`MAX_NESTING`] counts as at +/// that depth, and one at depth 0 after the note as a reply to it. +fn nest(messages: &[NotePopupMessage<'_>]) -> (Vec>, Vec) { + let mut answers: Vec> = vec![Vec::new(); messages.len()]; + let mut replies = Vec::new(); + // The replies open at each depth above the one being placed. + let mut open: Vec = Vec::new(); + for (at, message) in messages.iter().enumerate().skip(1) { + let depth = message.depth.clamp(1, MAX_NESTING); + open.truncate(depth - 1); + match open.last().and_then(|&parent| answers.get_mut(parent)) { + Some(own) => own.push(at), + None => replies.push(at), + } + open.push(at); + } + (answers, replies) +} + /// A reply's author line: avatar, name and age. fn byline(ui: &mut Ui, message: &NotePopupMessage<'_>, view: &EnvView<'_>, now: i64) { Row::new().fill_width().show(ui, |ui, _| { @@ -218,8 +260,8 @@ pub struct NotePopupOutput { } /// A modeless note pop-up window over a note's messages: a title band in the note's colour with its kind, author and -/// age, and Close; the note's text, typed in place while the note may be edited; its replies, each indented by its -/// depth; and a Reply button under each message that opens a box to answer it (Post or Cmd/Ctrl+Enter posts, Cancel +/// age, and Close; the note's text, typed in place while the note may be edited; its replies, each a bordered panel +/// named for its author, an answer to a reply inside that reply's panel; and a Reply button under each message that opens a box to answer it (Post or Cmd/Ctrl+Enter posts, Cancel /// or Escape drops it). /// /// - Place: at the host's `rect`, its size kept within [`NOTE_POPUP_LIMITS`] on screen at every zoom, so it takes no diff --git a/crates/review/src/ui/thread/popover.rs b/crates/review/src/ui/thread/popover.rs index 84165f11f..f57f55cce 100644 --- a/crates/review/src/ui/thread/popover.rs +++ b/crates/review/src/ui/thread/popover.rs @@ -243,6 +243,7 @@ pub struct CommentComposer<'a> { anchor: Option, draft: &'a mut String, header: Option<(Label, &'a str)>, + hint: Option