Section caption: label text as written, no rule above

This commit is contained in:
aiongg
2026-11-12 14:32:46 +00:00
parent e6c3f8400c
commit bb25285ace
4 changed files with 51 additions and 15 deletions
+9 -2
View File
@@ -65,8 +65,6 @@ pub struct Section<'a> {
#[prop]
open: Option<Bind<'a, bool>>,
collapsible: bool,
/// The header's weight: `Section` by default, `Caption` for a quiet heading.
#[prop]
strength: HeaderStrength,
/// The rows of the menu the header's Menu action, a right click, the Menu key and Shift+F10 open.
#[prop(func = "FnMut(&mut Ui, &mut BodyCx<'_>)")]
@@ -160,6 +158,15 @@ impl<'a> Section<'a> {
self.props.header = Some(header);
}
/// The header's weight: `Section` by default, under a rule; `Caption` for a quiet heading over a group of
/// controls, with no rule.
#[must_use]
pub fn strength(mut self, strength: HeaderStrength) -> Self {
self.strength = strength;
self.update_header(|header| header.rule_above(strength != HeaderStrength::Caption));
self
}
/// The action parts at the end of the header, in reading order.
#[must_use]
pub fn actions(mut self, actions: impl IntoIterator<Item = HeaderAction>) -> Self {
+6 -12
View File
@@ -18,7 +18,6 @@ use crate::style::{ComponentStyle, IconLook, Ink, StyleCx, TextLook, TypeRole};
use craft_behavior::{CursorIcon, Flow, IconId, Metrics, Mixed, NavShape, PartKey, Roving, Sense};
use craft_theme::{ControlSize, Stateful, States};
use egui::{Id, Rect, Response, Ui, Vec2};
use std::borrow::Cow;
#[cfg(feature = "testing")]
mod stories;
@@ -35,7 +34,7 @@ pub enum HeaderStrength {
Section,
/// A large heading: the heading role.
Heading,
/// A quiet heading: the caption role, faint, in capitals.
/// A quiet heading over a group of controls: label text, faint, as written, with no rule.
Caption,
}
@@ -270,11 +269,6 @@ impl<'a> TitleRow<'a> {
if matches!(self.lead, Lead::Disclosure(_)) { icon } else { row }
}
/// The title as it shows: a caption reads in capitals.
fn title_text<'t>(&self, label: &'t str) -> Cow<'t, str> {
if self.strength == HeaderStrength::Caption { Cow::Owned(label.to_uppercase()) } else { Cow::Borrowed(label) }
}
/// Divides the room left between the lead and the actions: the icon, then the title, then the subtitle when
/// both texts fit whole.
fn place(&self, mut band: Flow, cx: &PartsCx<'_, Self>) -> Placed {
@@ -300,7 +294,7 @@ impl<'a> TitleRow<'a> {
}
let role = cx.style.text.role;
let subtitle = self.subtitle.as_ref().and_then(|subtitle| {
let title = cx.layout_at(&TextRun::new(self.title_text(cx.resolved.label)).role(role), f32::INFINITY).galley.size().x;
let title = cx.layout_at(&TextRun::new(cx.resolved.label).role(role), f32::INFINITY).galley.size().x;
let width = cx.layout_at(&subtitle_run(subtitle.as_str(), role), f32::INFINITY).galley.size().x;
(title + cx.gap() + width <= band.rect().width()).then_some(title)
});
@@ -327,7 +321,7 @@ impl Component for TitleRow<'_> {
HeaderStrength::Strong => (cx.tokens.size.panel_header, TypeRole::Title, text.normal),
HeaderStrength::Section => (cx.tokens.size.panel_header, TypeRole::BodyStrong, text.normal),
HeaderStrength::Heading => (cx.tokens.size.panel_header, TypeRole::Heading, text.normal),
HeaderStrength::Caption => (cx.control().height, TypeRole::Caption, text.faint),
HeaderStrength::Caption => (cx.control().height, TypeRole::Label, text.faint),
};
let (shade, ink, disabled) = (cx.color(shade), cx.ink(Ink::OnPanel), cx.color(text.disabled));
TitleRowStyle {
@@ -341,7 +335,7 @@ impl Component for TitleRow<'_> {
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
let role = cx.style.text.role;
let title = cx.layout(&TextRun::new(self.title_text(cx.resolved.label)).role(role)).galley.size();
let title = cx.layout(&TextRun::new(cx.resolved.label).role(role)).galley.size();
let subtitle = self.subtitle.as_ref().map(|subtitle| cx.layout(&subtitle_run(subtitle.as_str(), role)).galley.size().x);
let parts =
usize::from(self.has_lead()) + usize::from(self.icon.is_some()) + usize::from(self.handle) + self.commands.len() + self.actions.len();
@@ -393,7 +387,7 @@ impl Component for TitleRow<'_> {
cx.roving(Roving::new(NavShape::Flat));
let band = flow.rect();
let placed = self.place(flow, cx);
let title = TextRun::new(self.title_text(cx.resolved.label)).role(cx.style.text.role).elide();
let title = TextRun::new(cx.resolved.label).role(cx.style.text.role).elide();
let elided = cx.layout_at(&title, placed.title.width()).elided;
let tooltip = cx.resolved.label.to_owned();
let part = if matches!(self.lead, Lead::Disclosure(_)) {
@@ -474,7 +468,7 @@ impl Component for TitleRow<'_> {
}
ui.draw().icon(rect, icon);
}
let mut title = TextRun::new(self.title_text(cx.resolved.label)).role(role).elide();
let mut title = TextRun::new(cx.resolved.label).role(role).elide();
if self.strength == HeaderStrength::Caption {
title = title.color(cx.resolve(&cx.style.text.color));
}
@@ -0,0 +1,34 @@
//! A caption-strength section is a quiet label over a group of controls: its title reads as written, with no rule
//! above it, while a section-strength one sits under a rule.
use craft_ui::sections::{HeaderStrength, Section};
use craft_ui::testing::{Fixture, trial};
use egui::{Shape, Vec2};
const ROOM: Vec2 = Vec2::new(400.0, 300.0);
/// The section titled `title` at `strength`, and the horizontal lines across most of its width that a frame paints.
fn rules(title: &'static str, strength: HeaderStrength) -> (Fixture, usize) {
let mut fixture = Fixture::new(move |ui| {
Section::fixed(title).strength(strength).show(ui, |ui, _| {
ui.label("body");
});
});
fixture.room(ROOM).settle();
let lines = fixture
.frame_shapes()
.iter()
.filter(|shape| !matches!(shape, Shape::Text(_)))
.map(Shape::visual_bounding_rect)
.filter(|rect| rect.is_finite() && rect.height() <= 2.0 && rect.width() >= ROOM.x / 2.0)
.count();
(fixture, lines)
}
#[trial]
fn a_caption_section_shows_its_title_as_written_with_no_rule_while_a_section_has_one() {
let (caption, caption_lines) = rules("Blend mode", HeaderStrength::Caption);
assert!(caption.texts().iter().any(|run| run.text == "Blend mode"), "the caption reads as written, not in capitals");
let (_, section_lines) = rules("Blend mode", HeaderStrength::Section);
assert!(section_lines > caption_lines, "a section draws a rule above its title; a caption draws none");
}
+2 -1
View File
@@ -1,5 +1,6 @@
//! Reorderable sections.
//! Sections: captions, footers and reorderable sections.
mod caption;
mod footer_keys;
mod footer_row;
mod layout;