ui-widgets: LinkLook, an env style value for how a link standing alone marks itself beside its colour: Underlined (default) or Plain, the link colour alone, as VectorCraft's start page lists its community links; a link in a sentence stays underlined. Story link_plain

This commit is contained in:
bflatastic
2026-11-25 15:20:32 +07:00
parent 9ff5832607
commit 69411864af
3 changed files with 43 additions and 11 deletions
+15 -9
View File
@@ -1,4 +1,4 @@
//! `Link`: text in the accent colour, always underlined, that reports a click.
//! `Link`: text in the accent colour, underlined unless the app's `LinkLook` says not, that reports a click.
use super::ButtonOutput;
use crate::contract::{Access, Common, Component, Cx, MeasureCx, Name, PaintUi, PartsCx, Pointer as _, Role, Styled as _};
@@ -29,6 +29,8 @@ pub struct LinkStyle {
/// The link look (`StyleCx::link_look`), or the tone's colour in its place.
#[style(text)]
text: TextLook,
/// A link standing alone is underlined (`LinkLook::Underlined`).
underline: bool,
}
/// What `parts` found of a link in a sentence, for `update` and `access`.
@@ -53,7 +55,8 @@ struct LaidLine {
link: Option<(Range<usize>, Rect)>,
}
/// A link: drawn in the accent colour in a type role (body by default) and underlined, so colour is not its only cue.
/// A link: drawn in the accent colour in a type role (body by default) and underlined, so colour is not its only cue
/// (`LinkLook::Plain` drops the underline of a link standing alone).
/// 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; in a sentence the node is the paragraph, and the link is a `Link` node below each run it covers.
#[derive(crate::contract::Component, crate::contract::Props)]
@@ -101,9 +104,12 @@ impl Link {
self.sentence.as_ref().map(|(before, after)| (resolve(before), resolve(after)))
}
/// The run: the link span alone, or the paragraph `before` + link + `after` (empty stretches left out).
fn run<'a>(&self, label: &'a str, (before, after): (&'a str, &'a str)) -> TextRun<'a> {
let spans = [Span::new(before), Span::new(label).link().underline(), Span::new(after)];
/// The run: the link span alone, underlined unless `style` says not, or the paragraph `before` + link + `after`
/// (empty stretches left out), the link always underlined.
fn run<'a>(&self, style: &LinkStyle, label: &'a str, (before, after): (&'a str, &'a str)) -> TextRun<'a> {
let link = Span::new(label).link();
let link = if style.underline || self.sentence.is_some() { link.underline() } else { link };
let spans = [Span::new(before), link, Span::new(after)];
TextRun::spans(spans.into_iter().filter(|span| span.link || !span.text.is_empty()).collect()).role(self.role).wrap(self.wrap)
}
@@ -143,7 +149,7 @@ impl Component for Link {
// PLAN: `link_look` takes the enabled state while the style is per key, so the off state is the look's own.
None => cx.link_look(true),
};
LinkStyle { metrics: Metrics::new(), focus: cx.focus_look(), text }
LinkStyle { metrics: Metrics::new(), focus: cx.focus_look(), text, underline: cx.env::<super::LinkLook>() == super::LinkLook::Underlined }
}
/// A middle click opens the link in a new tab. In a sentence the node only hovers: its parts take the click.
@@ -157,7 +163,7 @@ impl Component for Link {
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
let around = self.around(|label| cx.env.resolve(label));
let run = self.run(cx.resolved.label, stretches(&around));
let run = self.run(cx.style, cx.resolved.label, stretches(&around));
// At its least a link that elides is at its readable least.
if cx.proposal == Proposal::Min && self.cut_first() {
let least = cx.least(&run);
@@ -173,7 +179,7 @@ impl Component for Link {
self.laid = Laid::default();
let around = self.around(|label| cx.env.resolve(label));
let Some((before, label)) = around.as_ref().map(|(before, _)| (before.as_str(), cx.resolved.label)) else { return };
let run = self.run(label, stretches(&around));
let run = self.run(cx.style, label, stretches(&around));
let layout = cx.layout_at(&run, cx.content_rect.width());
let from = before.chars().count();
let chars = from..from + label.chars().count();
@@ -253,7 +259,7 @@ impl Component for Link {
let color = cx.resolve(&cx.style.text.color);
let draw = ui.draw();
let around = self.around(|label| cx.env.resolve(label));
let run = self.run(cx.resolved.label, stretches(&around)).link_color(color.egui());
let run = self.run(cx.style, cx.resolved.label, stretches(&around)).link_color(color.egui());
let layout = draw.layout(&run, cx.content_rect.width());
draw.galley(origin(cx.content_rect, &cx.flow, &layout), &layout);
}
+13
View File
@@ -37,6 +37,19 @@ pub enum GroupLevel {
Collapsed,
}
/// How a `Link` that stands alone marks itself beside its colour. An app sets it once at its root (`RootEnv::values`);
/// a link in a sentence stays underlined, since the text around it shares its line.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, EnvValue, MemoProps)]
#[env(style)]
#[non_exhaustive]
pub enum LinkLook {
/// Underlined, so colour is not its only cue.
#[default]
Underlined,
/// The link colour alone, as a list of links under its heading on a start page (VectorCraft's).
Plain,
}
/// Where controls are shown. A ribbon group sets `Ribbon` for its content (and for its collapsed button and the
/// popover that holds its content), and styles keyed on the env follow it:
///
+15 -2
View File
@@ -1,8 +1,8 @@
//! Stories of the buttons.
use super::{
Arrangement, Button, ButtonChord, CommandSearch, ControlPlace, GroupLevel, IconButton, Link, PopupMark, PressFocus, PushButtonLook, SplitButton,
SplitMenu, ToggleButton,
Arrangement, Button, ButtonChord, CommandSearch, ControlPlace, GroupLevel, IconButton, Link, LinkLook, PopupMark, PressFocus, PushButtonLook,
SplitButton, SplitMenu, ToggleButton,
};
use crate::contract::{Bind, BodyCx, Entry, LeastWidth, PopupKind, Props as _, Show as _, ShowContainer as _, Sizing, Trigger, Validity};
use crate::display::{Picture, Shade, Text};
@@ -531,6 +531,19 @@ fn link(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
Link::new("Show all your shortcuts").url("https://example.com/shortcuts").show(ui).into()
}
/// A link in the link colour alone (`LinkLook::Plain`), as a start page's list of links shows it.
#[story(Link, covers = [], title = "Plain")]
fn link_plain(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
with_env(
ui,
|overrides| {
overrides.values.set(LinkLook::Plain);
},
|ui| Link::new("Source code on the web").url("https://example.com/source").show(ui),
)
.into()
}
/// A command button showing its command's chord after the label.
#[story(Button<'static>, covers = [], drive = Drive::Never, group = "Variants", title = "Chord")]
fn button_chord(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {