From bac347860ede3a7dd6705860dce77b3fef2c67c3 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 18 Nov 2026 06:54:15 -0500 Subject: [PATCH] pdf-comments-w3: net change of 3 commits - pdf-comments-w3: finished by an agent - pdf-comments-w3: review fixes (review-1) - pdf-comments-w3: review fixes (review-1b) --- crates/review/src/ui/thread.rs | 2 +- crates/review/src/ui/thread/note_popup.rs | 576 ++++++++++++++++++++-- crates/review/src/ui/thread/popover.rs | 106 ++-- crates/review/src/ui/thread/stories.rs | 102 +++- crates/review/tests/it/main.rs | 1 + crates/review/tests/it/note_popup.rs | 178 +++++++ 6 files changed, 872 insertions(+), 93 deletions(-) create mode 100644 crates/review/tests/it/note_popup.rs diff --git a/crates/review/src/ui/thread.rs b/crates/review/src/ui/thread.rs index aa81084ff..ab7fd4848 100644 --- a/crates/review/src/ui/thread.rs +++ b/crates/review/src/ui/thread.rs @@ -38,6 +38,6 @@ craft_ui::words! { pin: template "thread.pin" = "Comment by {author}"; pin_text: template "thread.pinText" = "Comment by {author}: {words}"; cluster: template "thread.cluster" = "{n} comments"; - note_title: "thread.noteTitle" = "Note"; + note_title: template "thread.noteTitle" = "{kind} – {author}"; } } diff --git a/crates/review/src/ui/thread/note_popup.rs b/crates/review/src/ui/thread/note_popup.rs index 090c0a696..3afa9b077 100644 --- a/crates/review/src/ui/thread/note_popup.rs +++ b/crates/review/src/ui/thread/note_popup.rs @@ -2,10 +2,29 @@ use std::collections::HashMap; -use craft_ui::contract::SizeLimits; -use craft_ui::contract::overlay::PlacementKind; -use craft_ui::runtime::env::Label; -use egui::{Response, Ui, vec2}; +use craft_behavior::{GesturePhase, IconId, Metrics, Sense}; +use craft_theme::Opacity; +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, + PaintUi, Pattern, Props as _, Role, Show as _, ShowContainer as _, SizeLimits, clicked, +}; +use craft_ui::display::{Shade, Text}; +use craft_ui::layout_components::{Row, Stack}; +use craft_ui::primitives::{IconSpec, PaintShape, TextRun, Wrap}; +use craft_ui::runtime::env::{Env, EnvView, Label}; +use craft_ui::runtime::overlay::CloseReason; +use craft_ui::style::{ComponentStyle, DataColor, StyleCx, SurfaceKind, TypeRole}; +use craft_ui::text::TextField; +use egui::{Color32, CornerRadius, Pos2, Rect, Response, Ui, Vec2, pos2, vec2}; + +use crate::ui::annotate::DEFAULT_COLOR; +use crate::ui::avatar::Avatar; +use crate::ui::comments::age_text; + +use super::ThreadWords; /// One message of a note pop-up. #[derive(Clone, Debug, PartialEq)] @@ -26,16 +45,162 @@ pub struct NotePopupMessage<'a> { pub editable: bool, } -/// The pop-ups' reply drafts, one per note. -#[derive(Clone, Debug, Default)] +/// The pop-ups' drafts, by note: the typed text not yet committed, the open reply box and its text, and the window's +/// rect while a move or resize runs. The host keeps it, so a pop-up remembers nothing of its own across passes. +#[derive(Clone, Debug, Default, PartialEq)] pub struct NoteDrafts { - #[allow(dead_code)] // P3(pdf-comments-w3): CM-C17 - drafts: HashMap, + texts: HashMap, + replies: HashMap, + placing: HashMap, } -/// The smallest size of a note pop-up, in points; it has no largest size. +impl NoteDrafts { + /// These drafts with the reply box of the note `note` open under its message `message`, on `text`, for a story that + /// shows the box. + #[cfg(feature = "screen-testing")] + pub(super) fn with_reply(mut self, note: u64, message: u64, text: &str) -> Self { + self.replies.insert(note, (message, text.to_owned())); + self + } + + /// Forgets everything of the note `note`: the pop-up closed. + fn forget(&mut self, note: u64) { + self.texts.remove(¬e); + self.replies.remove(¬e); + self.placing.remove(¬e); + } + + /// Takes this pass's placement `changes` of the note `note`, whose window the host put at `window`: a running + /// gesture holds the window where it is, and a commit that changed the window by at least [`LEAST_CHANGE`] is the + /// report. Each pass applies only the changes its own input made, so a second pass in a frame changes nothing. + fn settle(&mut self, note: u64, window: Rect, changes: &[PlacementChange]) -> Option<(Rect, PlacementKind)> { + let mut placed = None; + for change in changes { + match change.phase { + GesturePhase::Begin | GesturePhase::Update => { + self.placing.insert(note, change.rect); + } + GesturePhase::Commit => { + self.placing.remove(¬e); + if changed(window, change.rect) { + placed = Some((change.rect, change.kind)); + } + } + _ => { + self.placing.remove(¬e); + } + } + } + placed + } +} + +/// 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. +struct NoteBody<'a, 'm> { + id: u64, + messages: &'a [NotePopupMessage<'m>], + drafts: &'a mut NoteDrafts, + allowed: bool, + now: i64, + body: Option, + reply: Option<(u64, String)>, +} + +impl NoteBody<'_, '_> { + fn show(&mut self, ui: &mut Ui, words: &ThreadWords, view: &EnvView<'_>) { + let messages = self.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); + } + }); + } + }); + } + + /// The note's text, typed in place: it commits on focus loss, Escape drops what was typed. + fn note_text(&mut self, ui: &mut Ui, note: &NotePopupMessage<'_>, words: &ThreadWords) { + let id = self.id; + let mut text = self.drafts.texts.get(&id).cloned().unwrap_or_else(|| note.text.to_owned()); + let field = TextField::new(words.add_comment.clone(), Bind::bound(&mut text)).lines(NOTE_ROWS).show(ui); + if let Some(committed) = field.events.committed().filter(|_| !field.escaped) { + self.body = edited(note.text, committed).or(self.body.take()); + self.drafts.texts.remove(&id); + } else if field.escaped || text == note.text { + self.drafts.texts.remove(&id); + } else { + self.drafts.texts.insert(id, text); + } + } + + /// The Reply button under the message `to`, or once it is pressed the box to answer that message in: Post or + /// Cmd/Ctrl+Enter posts the trimmed text, Cancel or Escape drops it. One box is open per note. + fn reply_box(&mut self, ui: &mut Ui, words: &ThreadWords, to: u64) { + let id = self.id; + let open = self.drafts.replies.get(&id).filter(|(target, _)| *target == to).map(|(_, text)| text.clone()); + let Some(mut typed) = open else { + if clicked(&Button::new(words.reply.clone()).show(ui)) { + self.drafts.replies.insert(id, (to, String::new())); + } + return; + }; + let field = TextField::new(words.reply.clone(), Bind::bound(&mut typed)).lines(NOTE_ROWS).placeholder(words.reply.clone()).submits().show(ui); + let text = typed.trim().to_owned(); + // The multi-line field submits on Cmd/Ctrl+Enter; plain Enter adds a line. + let mut post = field.submitted && field.submit_mods.cmd; + let mut cancel = field.escaped; + Row::new().show(ui, |ui, _| { + post |= clicked(&Button::new(words.post.clone()).variant(Variant::PRIMARY).enabled(!text.is_empty()).show(ui)); + cancel |= clicked(&Button::new(words.cancel.clone()).show(ui)); + }); + let posted = post && !text.is_empty(); + if posted { + self.reply = Some((to, text)); + } + if posted || cancel { + self.drafts.replies.remove(&id); + } else { + self.drafts.replies.insert(id, (to, typed)); + } + } +} + +/// 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, _| { + let _ = Avatar::new(message.author).show(ui); + Text::new(message.author.to_owned()).role(TypeRole::BodyStrong).show(ui); + let age = message.modified.map_or_else(String::new, |then| age_text(view, then, now)); + Text::new(age).role(TypeRole::Caption).shade(Shade::Muted).show(ui); + }); +} + +/// The size a note pop-up keeps to, in points: 200 to 800 wide and 120 to 800 high, whatever size the file gives it. pub const NOTE_POPUP_LIMITS: SizeLimits = SizeLimits::new(vec2(200.0, 120.0), vec2(800.0, 800.0)); +/// The rows the note's text and a reply show. +const NOTE_ROWS: usize = 2; +/// Past this depth a reply indents no further. +const MAX_NESTING: usize = 8; +/// The least a move or resize changes the window by to be reported, in points: a press that ends where it began +/// changes nothing. +const LEAST_CHANGE: f32 = 2.0; +/// How opaque the connector wedge is: production's 90 of 255. +const CONNECTOR_SHARE: f32 = 90.0 / 255.0; + /// What a note pop-up did this pass. #[derive(Debug)] #[non_exhaustive] @@ -46,84 +211,399 @@ pub struct NotePopupOutput { pub closed: bool, /// The note's edited text, once it is committed. pub body: Option, - /// A reply posted this pass. - pub reply: Option, + /// A reply posted this pass: the id of the message it answers and the text. + pub reply: Option<(u64, String)>, /// The window's new rectangle and the gesture that made it, reported when a move or resize commits. pub placed: Option<(egui::Rect, PlacementKind)>, } -/// A modeless note pop-up window over a note's messages. It keeps its size on screen at every zoom, so it takes no -/// zoom scale; a move or resize is reported as [`NotePopupOutput::placed`]. +/// 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 +/// 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 +/// zoom scale. The title band moves it and the lower end corner resizes it while commenting is allowed; a move or +/// resize is reported once as [`NotePopupOutput::placed`] when it commits, and one under 2 points is not. +/// - Text: the note's text commits when its field loses focus or the pop-up closes, with trailing whitespace trimmed. +/// - Connector: with [`anchor`](Self::anchor), a faint wedge joins the mark to the window while either is hovered or +/// the note is selected, and only while the two do not overlap. #[must_use = "show the pop-up with `show`"] -#[allow(dead_code)] // P3(pdf-comments-w3): CM-C18 +#[derive(Pattern)] pub struct NotePopup<'a> { id: u64, label: Label, rect: egui::Rect, messages: &'a [NotePopupMessage<'a>], drafts: &'a mut NoteDrafts, + color: Option<[u8; 3]>, + anchor: Option<(egui::Rect, bool)>, + selected: bool, + now: i64, + allowed: bool, } impl<'a> NotePopup<'a> { /// The pop-up `id` named `label` at `rect` over `messages`, keeping its reply drafts in `drafts`. pub fn new(id: u64, label: impl Into