mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 01:22:49 +00:00
Span::strong and ListRow::trailing_strong: a trailing mark in the caption role's semibold face
This commit is contained in:
@@ -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<Cow<'a, str>>) -> 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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 => {
|
||||
|
||||
@@ -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<Label>) -> Self {
|
||||
self.trailing.push(Trailing { item: TrailingItem::Text { text: text.into(), name: None }, optional: false });
|
||||
self.trailing.push(Trailing { item: TrailingItem::Text { text: text.into(), name: None, strong: false }, optional: false });
|
||||
self
|
||||
}
|
||||
|
||||
@@ -415,14 +415,22 @@ impl<'a> ListRow<'a> {
|
||||
/// (a font class's sign for the class): `name` is its tooltip and part of the row's description.
|
||||
#[must_use]
|
||||
pub fn trailing_named(mut self, text: impl Into<Label>, name: impl Into<Label>) -> Self {
|
||||
self.trailing.push(Trailing { item: TrailingItem::Text { text: text.into(), name: Some(name.into()) }, optional: false });
|
||||
self.trailing.push(Trailing { item: TrailingItem::Text { text: text.into(), name: Some(name.into()), strong: false }, optional: false });
|
||||
self
|
||||
}
|
||||
|
||||
/// A short mark shown as [`trailing_named`](Self::trailing_named) shows one, in the caption role's semibold face
|
||||
/// (a layer's `fx`).
|
||||
#[must_use]
|
||||
pub fn trailing_strong(mut self, text: impl Into<Label>, name: impl Into<Label>) -> Self {
|
||||
self.trailing.push(Trailing { item: TrailingItem::Text { text: text.into(), name: Some(name.into()), strong: true }, optional: false });
|
||||
self
|
||||
}
|
||||
|
||||
/// A text at the trailing end that drops out when the label would get narrower than its least width with it.
|
||||
#[must_use]
|
||||
pub fn optional(mut self, text: impl Into<Label>) -> Self {
|
||||
self.trailing.push(Trailing { item: TrailingItem::Text { text: text.into(), name: None }, optional: true });
|
||||
self.trailing.push(Trailing { item: TrailingItem::Text { text: text.into(), name: None, strong: false }, optional: true });
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
@@ -25,8 +25,8 @@ impl ListRow<'_> {
|
||||
let Some(slot) = slot else { continue };
|
||||
let shape = Rect::from_center_size(slot.center(), side);
|
||||
match &mut trailing.item {
|
||||
TrailingItem::Text { text, .. } => {
|
||||
ui.draw().text(*slot, &trailing_run(text));
|
||||
TrailingItem::Text { text, strong, .. } => {
|
||||
ui.draw().text(*slot, &trailing_run(text, *strong));
|
||||
}
|
||||
TrailingItem::Icon { icon, .. } => {
|
||||
let mut icon = icon.clone();
|
||||
|
||||
@@ -248,7 +248,7 @@ impl ListRow<'_> {
|
||||
/// icons and parts, and a hosted component's measured width.
|
||||
pub(super) fn widths(&self, sizes: &Sizes, text_width: &dyn Fn(&TextRun<'_>) -> f32) -> Widths {
|
||||
let width = |item: &TrailingItem| match item {
|
||||
TrailingItem::Text { text, .. } => text_width(&trailing_run(text)),
|
||||
TrailingItem::Text { text, strong, .. } => text_width(&trailing_run(text, *strong)),
|
||||
TrailingItem::Icon { .. } | TrailingItem::Part { .. } => sizes.icon,
|
||||
TrailingItem::Hosted(hosted) => hosted.size.map_or(0.0, |size| size.ideal.x),
|
||||
};
|
||||
|
||||
@@ -90,8 +90,9 @@ pub(in crate::list) struct Hosted<'a> {
|
||||
|
||||
/// What a trailing item is.
|
||||
pub(in crate::list) enum TrailingItem<'a> {
|
||||
/// A caption-role text as wide as it measures; a short mark carries the `name` it stands for, its tooltip.
|
||||
Text { text: Label, name: Option<Label> },
|
||||
/// A caption-role text as wide as it measures, semibold when `strong`; a short mark carries the `name` it stands
|
||||
/// for, its tooltip.
|
||||
Text { text: Label, name: Option<Label>, strong: bool },
|
||||
/// An icon of the row's icon size, named for the screen reader and the tooltip.
|
||||
Icon { icon: IconSpec<'static>, name: Label },
|
||||
/// A pressable face of the row's icon size; a `revealed` one shows only on a hovered or selected row, while it has
|
||||
@@ -109,6 +110,7 @@ pub(in crate::list) struct Trailing<'a> {
|
||||
}
|
||||
|
||||
/// The run of a trailing text.
|
||||
pub(super) fn trailing_run(text: &Label) -> TextRun<'_> {
|
||||
TextRun::spans(vec![Span::new(text.as_str()).muted()]).role(TypeRole::Caption)
|
||||
pub(super) fn trailing_run(text: &Label, strong: bool) -> TextRun<'_> {
|
||||
let span = Span::new(text.as_str()).muted();
|
||||
TextRun::spans(vec![if strong { span.strong() } else { span }]).role(TypeRole::Caption)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user