From 0f7062bd15d99ffc20ea519ed8393575cbdc07f5 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 01:11:21 +0000 Subject: [PATCH] Span::strong and ListRow::trailing_strong: a trailing mark in the caption role's semibold face --- crates/ui-contract/src/primitives/text.rs | 12 ++++++++++-- crates/ui-contract/src/style/text_measure.rs | 20 ++++++++------------ crates/ui-contract/src/style/tokens.rs | 9 ++++++++- crates/ui-widgets/src/list/row.rs | 14 +++++++++++--- crates/ui-widgets/src/list/row/paint.rs | 4 ++-- crates/ui-widgets/src/list/row/placement.rs | 2 +- crates/ui-widgets/src/list/row/trailing.rs | 10 ++++++---- 7 files changed, 46 insertions(+), 25 deletions(-) diff --git a/crates/ui-contract/src/primitives/text.rs b/crates/ui-contract/src/primitives/text.rs index ab2efec58..73e3690a1 100644 --- a/crates/ui-contract/src/primitives/text.rs +++ b/crates/ui-contract/src/primitives/text.rs @@ -73,12 +73,14 @@ pub struct Span<'a> { pub highlight: bool, /// Underlined in the span's colour, `stroke.thin` wide, along the bottom of the line box. pub underline: bool, + /// The role's semibold face, at the role's size. + pub strong: bool, } impl<'a> Span<'a> { /// A plain span. pub fn new(text: impl Into>) -> Self { - Span { text: text.into(), italic: false, tabular: false, mono: false, muted: false, shortcut: false, highlight: false, underline: false } + Span { text: text.into(), italic: false, tabular: false, mono: false, muted: false, shortcut: false, highlight: false, underline: false, strong: false } } /// Italic. @@ -122,6 +124,12 @@ impl<'a> Span<'a> { self.underline = true; self } + + /// The role's semibold face: a short mark that stands out among the row's other text, such as a layer's `fx`. + pub fn strong(mut self) -> Self { + self.strong = true; + self + } } /// The one description of UI text. @@ -318,7 +326,7 @@ impl TextRun<'_> { base }; let background = if span.highlight { to_color(env.tokens.color.state.r#match) } else { Color32::TRANSPARENT }; - let mut format = span_format(env.tokens, env.fonts, self.role, span.mono, color, span.italic, background); + let mut format = span_format(env.tokens, env.fonts, self.role, span, color, background); if span.underline { format.underline = egui::Stroke::new(env.tokens.stroke.thin, color); } diff --git a/crates/ui-contract/src/style/text_measure.rs b/crates/ui-contract/src/style/text_measure.rs index 8a5458aad..83a76614f 100644 --- a/crates/ui-contract/src/style/text_measure.rs +++ b/crates/ui-contract/src/style/text_measure.rs @@ -4,6 +4,7 @@ use super::tokens::UiTokens; use crate::fonts::StackFacts; +use crate::primitives::Span; use crate::runtime::egui::text_layout; use craft_i18n::Direction; use craft_theme::TypeRole; @@ -11,17 +12,12 @@ use egui::epaint::text::TextDirection; use egui::text::{LayoutJob, TextFormat, TextWrapping}; use egui::{Align, Color32, Context}; -/// The format of a span in `role`: its font at the role's size, with the optical size axis where the face has one. -pub(crate) fn span_format( - tokens: &UiTokens, - fonts: StackFacts, - role: TypeRole, - mono: bool, - color: Color32, - italics: bool, - background: Color32, -) -> TextFormat { - let mut format = TextFormat { font_id: tokens.font(fonts, role, mono), color, italics, background, ..TextFormat::default() }; +/// The format of `span` in `role`: its font at the role's size (mono, semibold or italic as the span is), with the +/// optical size axis where the face has one. The span's text and colour flags are not read. +pub(crate) fn span_format(tokens: &UiTokens, fonts: StackFacts, role: TypeRole, span: &Span<'_>, color: Color32, background: Color32) -> TextFormat { + let (mono, italics) = (span.mono, span.italic); + let font_id = tokens.font_weighted(fonts, role, mono, span.strong); + let mut format = TextFormat { font_id, color, italics, background, ..TextFormat::default() }; if let Some(size) = tokens.optical_size(fonts, role, mono) { format.coords.push(*b"opsz", size); } @@ -45,6 +41,6 @@ pub(crate) fn paragraph_job(direction: Direction, ltr: bool, wrap: TextWrapping) pub(crate) fn line_width(ctx: &Context, tokens: &UiTokens, fonts: StackFacts, direction: Direction, role: TypeRole, text: &str) -> f32 { let wrap = TextWrapping { max_width: f32::INFINITY, max_rows: usize::MAX, break_anywhere: false, ..TextWrapping::default() }; let mut job = paragraph_job(direction, false, wrap); - job.append(text, 0.0, span_format(tokens, fonts, role, false, Color32::PLACEHOLDER, false, Color32::TRANSPARENT)); + job.append(text, 0.0, span_format(tokens, fonts, role, &Span::new(""), Color32::PLACEHOLDER, Color32::TRANSPARENT)); text_layout::layout(ctx, job).size().x } diff --git a/crates/ui-contract/src/style/tokens.rs b/crates/ui-contract/src/style/tokens.rs index 54422b356..d17bd5507 100644 --- a/crates/ui-contract/src/style/tokens.rs +++ b/crates/ui-contract/src/style/tokens.rs @@ -57,8 +57,15 @@ impl UiTokens { /// stack of `facts` has one, and in the proportional family otherwise, so text never lands in a family with no /// faces. pub(crate) fn font(&self, facts: StackFacts, role: TypeRole, mono: bool) -> FontId { + self.font_weighted(facts, role, mono, false) + } + + /// The font of `role` as [`Self::font`] gives it, in the semibold slot when `strong` and the run is not mono: a + /// strong span keeps its role's size. + pub(crate) fn font_weighted(&self, facts: StackFacts, role: TypeRole, mono: bool, strong: bool) -> FontId { let style = if mono { self.typography.mono } else { self.typography.get(role) }; - let family = match style.font { + let slot = if strong && style.font == craft_theme::FontSlot::Ui { craft_theme::FontSlot::UiSemibold } else { style.font }; + let family = match slot { craft_theme::FontSlot::Mono => FontFamily::Monospace, craft_theme::FontSlot::Ui => FontFamily::Proportional, craft_theme::FontSlot::UiSemibold => { diff --git a/crates/ui-widgets/src/list/row.rs b/crates/ui-widgets/src/list/row.rs index 0d443a497..9d79d5063 100644 --- a/crates/ui-widgets/src/list/row.rs +++ b/crates/ui-widgets/src/list/row.rs @@ -407,7 +407,7 @@ impl<'a> ListRow<'a> { /// The first item added is the outermost. #[must_use] pub fn trailing(mut self, text: impl Into