diff --git a/crates/ui-contract/src/primitives/text.rs b/crates/ui-contract/src/primitives/text.rs index b3f4b1aac..e804a9125 100644 --- a/crates/ui-contract/src/primitives/text.rs +++ b/crates/ui-contract/src/primitives/text.rs @@ -121,9 +121,9 @@ impl<'a> TextRun<'a> { } /// Sets the colour of the run's link spans. - pub fn link_color(self, color: Color32) -> Self { - let _ = color; - todo!("dp.R2") + pub fn link_color(mut self, color: Color32) -> Self { + self.link_color = Some(color); + self } /// Sets the colour. @@ -299,6 +299,8 @@ impl TextRun<'_> { to_color(text.muted) } else if span.shortcut { to_color(text.faint) + } else if span.link { + self.link_color.unwrap_or(base) } else { base }; @@ -496,8 +498,9 @@ impl TextLayout { runs } - /// Every row as its text, its rect from the galley's origin and its bidi runs, for the AccessKit run nodes. - pub(crate) fn row_lines(&self) -> Vec<(String, egui::Rect, Vec)> { + /// Every row as its text, its rect from the galley's origin and its bidi runs: the one per-row geometry, read by + /// the AccessKit run nodes and by a component that hits parts of a wrapped run, such as a link in a sentence. + pub fn row_lines(&self) -> Vec<(String, egui::Rect, Vec)> { (0..self.galley.rows.len()) .map(|at| { let placed = &self.galley.rows[at]; diff --git a/crates/ui-contract/src/style/span.rs b/crates/ui-contract/src/style/span.rs index 7099c200d..b2551e573 100644 --- a/crates/ui-contract/src/style/span.rs +++ b/crates/ui-contract/src/style/span.rs @@ -46,8 +46,9 @@ impl<'a> Span<'a> { } /// Makes the span a link: it takes the run's link colour and the pointer. - pub fn link(self) -> Self { - todo!("dp.R2") + pub fn link(mut self) -> Self { + self.link = true; + self } /// Italic. diff --git a/crates/ui-contract/tests/it/collection/rows.rs b/crates/ui-contract/tests/it/collection/rows.rs index 0049ef53d..388072fcb 100644 --- a/crates/ui-contract/tests/it/collection/rows.rs +++ b/crates/ui-contract/tests/it/collection/rows.rs @@ -3,9 +3,10 @@ use super::kit::{Count, Dismissed, List, ROW_H, RowOut, Words}; use craft_behavior::{ - CellAt, CellRange, CellSelection, ColumnKey, EditEvent, EditEvents, ItemSource, PartKey, ScrollPos, Selection, SelectionModel as _, Sense, - SheetLayout, SpanLayout, keyed, + CellAt, CellRange, CellSelection, ColumnKey, EditEvent, EditEvents, GestureId, ItemSource, Mixed, PartKey, ScrollPos, Selection, SelectionModel as _, + Sense, SheetLayout, SpanLayout, keyed, }; +use craft_keymap::Mods; use craft_ui::contract::{ Bind, BindMut, Collection, CollectionEvent, CollectionProps, CollectionResponse, Common, Component, ContainerProps, Cx, EditResponse, Focus as _, MeasureCx, PartsCx, Props as _, Role, SelectMode, ShowCollection as _, request_rename, @@ -122,6 +123,28 @@ impl Component for ProbeRow { } } +/// What the stub rename editor reports for a typed Enter: the key, the text typed, the modifiers held with it, and how +/// often the editor was shown. +struct Typing { + enter: Cell, + text: Cell>, + mods: Cell, + shown: Cell, +} + +impl Typing { + fn new() -> Self { + Typing { enter: Cell::new(false), text: Cell::new(None), mods: Cell::new(Mods::NONE), shown: Cell::new(0) } + } + + /// The next pass sees one Enter with `text` typed (none: the proposed draft), with Shift held when `shift`. + fn press(&self, text: Option<&str>, shift: bool) { + self.text.set(text.map(str::to_owned)); + self.mods.set(if shift { Mods::SHIFT } else { Mods::NONE }); + self.enter.set(true); + } +} + /// A list of `ProbeRow`s over `SpanLayout`, rows of the control height or of the heights they measure. #[derive(craft_ui::contract::Component)] #[component(collection)] @@ -136,6 +159,8 @@ struct Probe<'a> { auto: bool, /// The rename draft every row proposes, and whether the rename field reports Enter. rename: Option<(&'a str, &'a Cell)>, + /// The typed Enter the rename field reports, one per `Typing::press`. + typing: Option<&'a Typing>, } impl<'a> Probe<'a> { @@ -148,6 +173,7 @@ impl<'a> Probe<'a> { scroll: None, auto: false, rename: None, + typing: None, } } @@ -158,6 +184,13 @@ impl<'a> Probe<'a> { self } + /// Rows rename with `draft` proposed; the field reports the Enters `typing` is given, each once. + fn typing(mut self, draft: &'a str, typing: &'a Typing) -> Self { + self = self.renaming(draft, &typing.enter); + self.typing = Some(typing); + self + } + fn auto(mut self) -> Self { self.auto = true; self @@ -211,9 +244,22 @@ impl Collection for Probe<'_> { } fn rename_editor(&mut self, ui: &mut Ui, _: Bind<'_, String>, label: Rect, _: &str) -> Option> { - let (_, enter) = self.rename?; - let mut editor = EditResponse::lifted(ui.allocate_rect(label, egui::Sense::hover()), EditEvents::new(), None); - editor.submitted = enter.get(); + let (draft, enter) = self.rename?; + let mut events = EditEvents::new(); + let (submitted, mods) = match self.typing { + Some(typing) => { + typing.shown.set(typing.shown.get() + 1); + let pressed = enter.replace(false); + if pressed && let Some(text) = typing.text.take() { + events.push(EditEvent::commit(GestureId::new(0), Mixed::Same(draft.to_owned()), text)); + } + (pressed, typing.mods.get()) + } + None => (enter.get(), Mods::NONE), + }; + let mut editor = EditResponse::lifted(ui.allocate_rect(label, egui::Sense::hover()), events, None); + editor.submitted = submitted; + editor.submit_mods = mods; Some(editor) } } @@ -542,3 +588,61 @@ fn enter_on_an_untyped_rename_commits_only_a_proposed_draft() { assert_eq!(renamed("proposed"), ["proposed"], "Enter accepts the proposed draft"); assert!(renamed("b").is_empty(), "a draft equal to the row's text reports no commit"); } + +/// Shift+Enter in a rename field applies the name as a commit and the rename goes on, each Shift+Enter its own gesture; +/// plain Enter commits and ends it. The proposed draft does the same with Shift. +#[trial] +fn shift_enter_applies_a_rename_and_keeps_the_field_open() { + let (log, typing, words) = (Log::new(), Typing::new(), Words::flat(&["a", "b", "c"])); + let mut frames = Frames::new(); + let mut show = |ui: &mut Ui| { + in_region(ui, Rect::from_min_size(ORIGIN, VIEW), |ui| { + let list = Probe::new(SelectMode::Single).id_salt("list").typing("proposed", &typing); + log.take(list.show_items(ui, &words, &mut |key, _| ProbeRow::new(*key, log))); + }); + }; + let commits = || -> Vec<(GestureId, String)> { + log.events + .borrow() + .iter() + .filter_map(|event| match event { + CollectionEvent::Rename(EditEvent::Commit { id, after, .. }) => Some((*id, after.name.clone())), + _ => None, + }) + .collect() + }; + frames.idle(3, &mut show); + request_rename(&frames.ctx, log.id(), &1_usize); + frames.idle(3, &mut show); + assert!(typing.shown.get() > 0, "the rename shows its editor"); + + typing.press(Some("one"), true); + frames.frame(vec![], &mut show); + assert_eq!(commits().iter().map(|(_, name)| name.as_str()).collect::>(), ["one"], "Shift+Enter reports the typed name once"); + let shown = typing.shown.get(); + frames.idle(2, &mut show); + assert!(typing.shown.get() > shown, "the rename goes on: the editor shows in the next passes"); + assert_eq!(commits().len(), 1, "an idle pass reports nothing more"); + + typing.press(Some("two"), true); + frames.frame(vec![], &mut show); + let applied = commits(); + assert_eq!(applied.len(), 2, "a second Shift+Enter is a second commit"); + assert_ne!(applied[0].0, applied[1].0, "each is its own gesture, so its own undo step"); + + typing.press(Some("three"), false); + frames.frame(vec![], &mut show); + assert_eq!(commits().len(), 3, "plain Enter commits"); + let shown = typing.shown.get(); + frames.idle(2, &mut show); + assert_eq!(typing.shown.get(), shown, "and ends the rename"); + + request_rename(&frames.ctx, log.id(), &1_usize); + frames.idle(3, &mut show); + typing.press(None, true); + frames.frame(vec![], &mut show); + assert_eq!(commits().last().map(|(_, name)| name.as_str()), Some("proposed"), "Shift+Enter on a proposed draft commits it"); + let shown = typing.shown.get(); + frames.idle(2, &mut show); + assert!(typing.shown.get() > shown, "and keeps the rename going"); +} diff --git a/crates/ui-widgets/src/buttons/link.rs b/crates/ui-widgets/src/buttons/link.rs index 38bafe40a..f02656840 100644 --- a/crates/ui-widgets/src/buttons/link.rs +++ b/crates/ui-widgets/src/buttons/link.rs @@ -1,13 +1,18 @@ //! `Link`: text in the accent colour, always underlined, that reports a click. use super::ButtonOutput; -use crate::contract::{Access, Common, Component, Cx, MeasureCx, PaintUi, Pointer as _, Role, Styled as _}; +use crate::contract::{Access, Common, Component, Cx, MeasureCx, PaintUi, PartsCx, Pointer as _, Role, Styled as _}; use crate::primitives::{Span, TextLayout, TextRun, Wrap}; +use crate::runtime::env::Label; use crate::style::{ComponentStyle, FocusLook, Stateful, StyleCx, TextLook, TypeRole}; -use craft_behavior::{Flow, Metrics, PointerButton, PressPhase, Proposal, Sense}; +use craft_behavior::{CursorIcon, Flow, Metrics, PartKey, PointerButton, PressPhase, Proposal, Sense}; +use craft_theme::States; use craft_theme::variant::Tone; -use craft_theme::{Contrast, States}; -use egui::{Pos2, Vec2}; +use egui::{Pos2, Rect, Vec2}; +use std::ops::Range; + +/// The part a link in a sentence takes the pointer on, one per row the link's characters cover. +const LINK: PartKey = PartKey::new("link"); /// The style of a link. #[derive(Clone, Debug, PartialEq, ComponentStyle)] @@ -17,11 +22,25 @@ pub struct LinkStyle { metrics: Metrics, #[style(focus)] focus: FocusLook, - /// The text and underline colour: `accent.solid`, as PhotoCraft's links; `accent.text` at high contrast, which meets the text contrast `accent.solid` does not. + /// The link look (`StyleCx::link_look`), or the tone's colour in its place. #[style(text)] text: TextLook, } +/// What `parts` found of a link in a sentence, for `update` and `access`. +#[derive(Clone, Debug, Default)] +struct Laid { + /// The rows the link's characters cover: the parts `LINK.at(0)..LINK.at(rows)`. + rows: u64, + /// The paragraph's text and each character's start offset and advance in its row, rows in order. + text: String, + positions: Vec, + widths: Vec, + rtl: bool, + /// The link's characters in the paragraph. + chars: Range, +} + /// A link: drawn in the accent colour in a type role (body by default) and underlined, so colour is not its only cue. /// A click, Enter or Space opens the URL in the same tab; a middle click, or a click with a modifier held, opens it in a new tab. The caller can also act on `clicked()`. AccessKit reports the role `Link`, with the URL when /// it has one. @@ -43,49 +62,80 @@ pub struct Link { #[style_key] #[prop] tone: Option, + /// The text before and after the link when it sits in a sentence. + sentence: Option<(Label, Label)>, + laid: Laid, } impl Link { /// A link labelled `label`, in body type. - pub fn new(label: impl Into) -> Self { + pub fn new(label: impl Into