diff --git a/crates/ui-widgets/src/display/mod.rs b/crates/ui-widgets/src/display/mod.rs index f693e9b72..860385fdd 100644 --- a/crates/ui-widgets/src/display/mod.rs +++ b/crates/ui-widgets/src/display/mod.rs @@ -1,4 +1,4 @@ -//! Display and status components: `Badge`, `Kbd`, `Progress`, `Spinner`, `Text`, `Icon`, `Picture` and `Divider`. +//! Display and status components: `Badge`, `Kbd`, `Progress`, `Spinner`, `TargetMark`, `Text`, `Icon`, `Picture` and `Divider`. //! //! None of them takes input or a Tab stop. A tooltip, or a label no text paints, gives the root a hover sense so the //! tooltip can show. @@ -11,6 +11,7 @@ mod kbd; mod picture; mod progress; mod spinner; +mod target_mark; mod text; mod words; @@ -22,6 +23,7 @@ pub use kbd::{ChordWrite, Kbd, KbdStyle}; pub use picture::{Picture, PictureSize, PictureStyle}; pub use progress::{Progress, ProgressStyle}; pub use spinner::{Spinner, SpinnerSize, SpinnerStyle}; +pub use target_mark::{TargetMark, TargetMarkStyle}; pub use text::{Elide, Shade, Text, TextStyle}; pub use words::DisplayWords; diff --git a/crates/ui-widgets/src/display/target_mark.rs b/crates/ui-widgets/src/display/target_mark.rs new file mode 100644 index 000000000..3f192b9cb --- /dev/null +++ b/crates/ui-widgets/src/display/target_mark.rs @@ -0,0 +1,133 @@ +//! `TargetMark`: a pulsing accent ring around a part of the window, on a passive layer above the content, so a guide +//! (a tour step) shows the part it explains. + +use crate::contract::overlay::OverlayFit; +use crate::contract::{ + Animate as _, Bind, BindMut, Common, Component, Container, ContainerProps, Cx, Mount, Overlay, Pad, PaintUi, Role, ShowContainer as _, +}; +use crate::primitives::{PaintShape, StrokeWidth}; +use crate::runtime::env::Env; +use crate::style::{ComponentStyle, StyleColor, StyleCx, UiTokens}; +use craft_behavior::{Metrics, Sense}; +use craft_theme::Mix as _; +use egui::{CornerRadius, Rect, Response, Ui}; +use std::f32::consts::TAU; + +#[cfg(feature = "testing")] +mod stories; + +/// The period of the pulse in seconds: a sine of four radians per second. +const PULSE_PERIOD: f64 = std::f64::consts::FRAC_PI_2; +/// The ring's opacity at the low point of its pulse; it reaches full opacity at the top. +const DIMMEST: f32 = 0.6; + +/// How the ring stands off its target: the gap left around the target, how far the ring grows at the top of its pulse, +/// and its line width. +#[derive(Clone, Copy, Debug, PartialEq)] +struct Reach { + gap: f32, + pulse: f32, + width: f32, +} + +impl Reach { + fn of(tokens: &UiTokens) -> Self { + Reach { gap: tokens.space.gap.tight, pulse: tokens.space.gap.tight, width: tokens.stroke.thick } + } + + /// The rect the ring covers at the top of its pulse: the layer's rect. + fn outer(self, target: Rect) -> Rect { + target.expand(self.gap + self.pulse + self.width) + } + + /// The line of the ring at `pulse` (0 to 1) of its growth, the stroke lying outside the gap. + fn line(self, target: Rect, pulse: f32) -> Rect { + target.expand(self.gap + self.pulse * pulse + self.width / 2.0) + } +} + +/// The look of a target mark. +#[derive(Clone, Debug, PartialEq, ComponentStyle)] +#[non_exhaustive] +pub struct TargetMarkStyle { + #[style(metrics)] + metrics: Metrics, + /// The ring at the top of its pulse. + ring: StyleColor, + /// The ring at the low point of its pulse. + ring_dim: StyleColor, + /// The ring's corner: a control's. + radius: CornerRadius, + reach: Reach, +} + +/// A ring around `target` that pulses in the accent colour, still where motion is off. It sits on a layer above the +/// content that takes no presses, no hover and no focus, so the part it marks stays usable. Show it with +/// [`show_mark`](TargetMark::show_mark) in every pass the mark shows. +#[derive(crate::contract::Component, crate::contract::Props)] +#[component(container)] +pub struct TargetMark { + #[common] + common: Common, + #[container] + props: ContainerProps, + target: Rect, + open: Bind<'static, bool>, +} + +impl TargetMark { + /// A mark around `target`, a rect in window coordinates. + pub fn new(target: Rect) -> Self { + TargetMark { common: Common::new(), props: ContainerProps::new().pad(Pad::None), target, open: Bind::given(true) } + } + + /// Shows the mark around its target. + pub fn show_mark(mut self, ui: &mut Ui) -> Response { + let outer = Reach::of(&Env::of(ui).tokens).outer(self.target); + let layer = Overlay::tooltip().fit(OverlayFit::Content).placed(outer).body_scroll(false); + self.props = self.props.mount(Mount::Layer(layer)); + self.show(ui, |_, _| ()).output + } +} + +impl Component for TargetMark { + const ROLE: Role = Role::None; + const PARTS: bool = false; + const STOPS: bool = false; + const OVERLAYS: bool = false; + type Style = TargetMarkStyle; + type Output = Response; + + fn style(cx: &StyleCx<'_>, _: TargetMarkStyleKey) -> TargetMarkStyle { + let accent = cx.tokens.color.accent.solid; + TargetMarkStyle { + metrics: Metrics::new(), + ring: cx.color(accent), + ring_dim: cx.color(accent.faded(DIMMEST)), + radius: UiTokens::corner(cx.tokens.shape.radius.control), + reach: Reach::of(cx.tokens), + } + } + + fn sense(&self) -> Sense { + Sense::empty() + } + + fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) { + let style = cx.style; + // Still, the ring stands at the top of its pulse. + let pulse = cx.phase(PULSE_PERIOD, None).map_or(1.0, |phase| 0.5 + 0.5 * (phase * TAU).sin()); + let line = style.reach.line(self.target, pulse); + ui.draw().stroke(&PaintShape::Rect(line, style.radius), StrokeWidth::Thick, style.ring_dim.mix(style.ring, pulse)); + } + + fn finish(self, cx: Cx<'_, Self>) -> Response { + cx.response.clone() + } +} + +impl Container for TargetMark { + fn open(&mut self) -> Option> { + Some(self.open.reborrow()) + } +} diff --git a/crates/ui-widgets/src/display/target_mark/stories.rs b/crates/ui-widgets/src/display/target_mark/stories.rs new file mode 100644 index 000000000..7f5d2cad9 --- /dev/null +++ b/crates/ui-widgets/src/display/target_mark/stories.rs @@ -0,0 +1,13 @@ +//! Stories of `TargetMark`. + +use super::TargetMark; +use crate::buttons::Button; +use crate::contract::Show as _; +use crate::testing::{Drive, StoryData, StoryOutput, story}; +use egui::Ui; + +#[story(TargetMark, covers = [], drive = Drive::Never)] +fn target_mark(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { + let target = Button::new("Layers").show(ui); + TargetMark::new(target.rect).show_mark(ui).into() +} diff --git a/docs/components-index.md b/docs/components-index.md index 623797283..c175a0f9c 100644 --- a/docs/components-index.md +++ b/docs/components-index.md @@ -96,7 +96,7 @@ Generated by `cargo run -p craft-ui-gallery --bin components-index`; edit the do | `Watermark<'_>` | Pattern | `craft_ui_patterns::patterns::form_dialogs::watermark` | The watermark rows of an export dialog: Style (Text, Graphic), the text field or the image path with Choose…, Orientation, Position as the 3 by 3 reference point, Rotation, Size, Opacity, Inset X and Y, and Shadow | | `Inspector<'_>` | Pattern | `craft_ui_patterns::patterns::panels::inspector` | A properties panel: a `Panel` whose header names the target (icon, name and, after it, the kind), and whose body the caller fills with the target's controls | | `CommandPalette<'_>` | Pattern | `craft_ui_patterns::patterns::panels::palette` | A search over the app's commands in a centred modal box: a search field over the matching commands | -| `Tour<'_>` | Pattern | `craft_ui_patterns::patterns::panels::tour` | A guided tour of the interface: a card with the step's heading and text, Skip, Back and Next (Done on the last step) and the step count, beside the part the step points at | +| `Tour<'_>` | Pattern | `craft_ui_patterns::patterns::panels::tour` | A guided tour of the interface: a card with the step count and a close button, the step's heading and text, Skip, Back and Next (Done on the last step), beside the part the step points at, which a pulsing ring marks | | `PreferencesWindow<'_>` | Pattern | `craft_ui_patterns::patterns::preferences::window` | The preferences window: a search field over a page list and the shown page, with OK, Cancel and Apply under them | | `PresetBrowser<'_, u32>` | Pattern | `craft_ui_patterns::patterns::presets::browser` | A library of named presets shown as thumbnails | | `PresetFooter<'_, u32>` | Pattern | `craft_ui_patterns::patterns::presets::footer` | The footer of a preset panel: the host's own controls, the thumbnail size slider and the count, then the action buttons Delete, New and New Group and the host's own actions | @@ -142,6 +142,7 @@ Generated by `cargo run -p craft-ui-gallery --bin components-index`; edit the do | `Picture<'_>` | Leaf | `craft_ui_widgets::display::picture` | A picture of an image the caller makes | | `Progress` | Leaf | `craft_ui_widgets::display::progress` | A progress bar | | `Spinner` | Leaf | `craft_ui_widgets::display::spinner` | The busy mark | +| `TargetMark` | Body | `craft_ui_widgets::display::target_mark` | A ring around `target` that pulses in the accent colour, still where motion is off | | `Text` | Leaf | `craft_ui_widgets::display::text` | Text in a type role (body by default) and a shade (normal by default), or in the colour of a tone | | `Field` | Body | `craft_ui_widgets::form::field` | One control with a side label: beside it, above it, as a caption or after it (`Field::layout`) | | `FieldGroup` | Body | `craft_ui_widgets::form::group` | A stack of `Field`s (and `LinkedFields`) named `label`, filling the width offered |