diff --git a/crates/table/src/ui/slicer.rs b/crates/table/src/ui/slicer.rs index 3cfc8b2df..a3cf1863b 100644 --- a/crates/table/src/ui/slicer.rs +++ b/crates/table/src/ui/slicer.rs @@ -11,10 +11,10 @@ use craft_ui::contract::{ HeaderRegion, NoValue, Outcome, Pad, PaintUi, Pattern, Props, Role, SelectMode, Show as _, ShowCollection as _, ShowContainer as _, Sizing, Styled as _, WordSetCx as _, clicked, }; -use craft_ui::display::Text; use craft_ui::primitives::{IconSpec, TextRun}; use craft_ui::runtime::env::{Env, Label}; -use craft_ui::style::{ComponentStyle, Flags, FocusLook, Stateful, StyleColor, StyleCx, SurfaceKind, SurfaceLook, TextLook, TypeRole}; +use craft_ui::sections::{HeaderStrength, TitleRow}; +use craft_ui::style::{ComponentStyle, Flags, FocusLook, Stateful, StyleColor, StyleCx, SurfaceKind, SurfaceLook, TextLook, TypeRole, UiTokens}; use egui::{Rect, Response, Ui, Vec2, pos2}; #[cfg(feature = "screen-testing")] @@ -57,10 +57,10 @@ impl Outcome for SlicerOutput { } } -/// Excel's slicer: a raised card whose header holds the caption and the funnel button (tinted while a filter is on), -/// over value tiles in `columns` columns (on: the accent fill with the on-accent text; off: the hover fill with muted -/// text). A click, Enter or Space on a tile reports [`SlicerAction::Toggle`] with its value, the funnel reports -/// [`SlicerAction::Clear`]; elsewhere a press is the app's (select, drag the object). +/// Excel's slicer: a raised card whose header band, in the accent's tint, holds the caption and the funnel button +/// (tinted while a filter is on), over value tiles in `columns` columns (on: the accent fill with the on-accent text; +/// off: the hover fill with muted text). A click, Enter or Space on a tile reports [`SlicerAction::Toggle`] with its +/// value, the funnel reports [`SlicerAction::Clear`]; elsewhere a press is the app's (select, drag the object). /// /// The tiles are one Tab stop whose arrows move between them in reading order and wrap over the rows; the funnel /// button is a Tab stop of its own. A card too small for all the tiles scrolls them. `zoom` is the sheet zoom the @@ -180,7 +180,8 @@ struct SlicerCardStyle { metrics: Metrics, } -/// The raised card: the caption in its header, the funnel button at the header's end and the tiles in its body. +/// The raised card: the caption in its accent header band, the funnel button at the band's end, the tiles in its +/// body. #[derive(Component, Props)] #[component(container)] struct SlicerCard { @@ -198,7 +199,7 @@ impl SlicerCard { .surface(SurfaceKind::Raised) .pad(Pad::Control) .sizing(Sizing::Fill) - .header(HeaderRegion::new().rule(true).trailing(true)) + .header(HeaderRegion::new().tint(Tone::Accent).inset(Pad::Control).trailing(true)) .collection_body(); SlicerCard { common: Common::new(), props, filtered, cleared: false }.label(caption) } @@ -226,7 +227,7 @@ impl Component for SlicerCard { impl Container for SlicerCard { fn header(&mut self, ui: &mut Ui, cx: &mut BodyCx<'_>) { - Text::new(cx.label()).role(TypeRole::BodyStrong).show(ui); + TitleRow::new(cx.label()).strength(HeaderStrength::Section).tone(Tone::Accent).show(ui); } fn controls(&mut self, ui: &mut Ui, _: &mut BodyCx<'_>) { @@ -354,7 +355,7 @@ impl Component for SlicerTile { let (hover, solid): (StyleColor, StyleColor) = (cx.color(colors.state.hover), cx.color(colors.accent.solid)); let (muted, on_accent): (StyleColor, StyleColor) = (cx.color(colors.text.muted), cx.color(colors.text.on_accent)); SlicerTileStyle { - surface: SurfaceLook::new(Stateful::new(hover).when(Flags::CHECKED, solid)).radius(cx.variant_look().radius), + surface: SurfaceLook::new(Stateful::new(hover).when(Flags::CHECKED, solid)).radius(UiTokens::corner(cx.tokens.shape.radius.control)), metrics: Metrics::new().min_size(Vec2::new(0.0, cx.control().height)), focus: cx.focus_look(), text: TextLook::new(TypeRole::Body, Stateful::new(muted).when(Flags::CHECKED, on_accent)), diff --git a/crates/theme/src/variant.rs b/crates/theme/src/variant.rs index c78afe84b..bb5a8b0a0 100644 --- a/crates/theme/src/variant.rs +++ b/crates/theme/src/variant.rs @@ -22,7 +22,7 @@ pub enum Emphasis { } /// What a variant says about its subject. The one tone type of the component layer: messages, notices, badges and -/// status ink all name it, and `StyleCx::tone` and `StyleCx::tone_fill` give its colours. +/// status ink all name it, and `StyleCx::tone`, [`Tone::soft_fill`] and [`Tone::on_soft`] give its colours. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)] #[non_exhaustive] pub enum Tone { @@ -41,6 +41,19 @@ pub enum Tone { Bad, } +impl Tone { + /// The tone's tinted fill: `accent.soft`, the status background, and the sunken surface for no status. + pub fn soft_fill(self, tokens: &Tokens) -> Rgba { + tokens.color.get(ToneTokens::of(self).soft_fill) + } + + /// The ink text and icons read in on the tone's [`soft_fill`](Self::soft_fill): `accent.on_soft`, the status + /// foreground, and the normal text for no status. + pub fn on_soft(self, tokens: &Tokens) -> Rgba { + tokens.color.get(ToneTokens::of(self).on_soft) + } +} + /// Which radius of the theme's shape a look uses. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)] #[non_exhaustive] @@ -75,6 +88,8 @@ pub struct VariantLook { #[derive(Clone, Copy, Debug)] struct ToneTokens { soft_fill: ColorToken, + /// Text and icons on `soft_fill`. + on_soft: ColorToken, fg: ColorToken, edge: ColorToken, solid: ColorToken, @@ -88,6 +103,7 @@ impl ToneTokens { use ColorToken as T; let status = |fg, bg, edge, solid, solid_hover, solid_pressed| ToneTokens { soft_fill: bg, + on_soft: fg, fg, edge, solid, @@ -98,6 +114,7 @@ impl ToneTokens { match tone { Tone::Neutral => ToneTokens { soft_fill: T::SurfaceSunken, + on_soft: T::TextNormal, fg: T::TextNormal, edge: T::TextMuted, solid: T::TextMuted, @@ -107,6 +124,7 @@ impl ToneTokens { }, Tone::Accent => ToneTokens { soft_fill: T::AccentSoft, + on_soft: T::AccentOnSoft, fg: T::AccentText, edge: T::AccentBorder, solid: T::AccentStrong, diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index 0c0145fa4..0559163f1 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -30,7 +30,7 @@ use crate::runtime::overlay::CloseReason; use crate::runtime::pass_state; use crate::runtime::{keys, layout_memory, pointer, wheel}; use crate::style::ComponentStyle as _; -use crate::style::{ScrollBarKind, StyleCx, SurfaceKind, scroll_bar_look, surface_style}; +use crate::style::{ScrollBarKind, StyleCx, SurfaceKind, band_color, scroll_bar_look, surface_style}; use craft_behavior::layout_memory::measure_tolerance; use craft_behavior::{CancelRule, CursorIcon, EditEvent, KeyRole, Mixed, Roving, ScrollPos, ScrollSpan, WheelRoom, WheelTaker}; use egui::{Context, CornerRadius, Id, Pos2, Rect, Shape, Ui, UiBuilder, Vec2}; @@ -1033,10 +1033,10 @@ impl ShapeHooks for BodyHooks<'_> { if let Some(kind) = props.surface { surface_slot.paint(&env.tokens, kind, rect, env.direction); } - if let Some(kind) = props.header.and_then(|header| header.fill) { + if let Some(fill) = props.header.and_then(|header| header.fill) { let style = surface_style(&env.tokens, props.surface.unwrap_or(SurfaceKind::Panel)); let radius = CornerRadius { sw: 0, se: 0, ..style.radius }; - ui.painter().set(band_slot, Shape::rect_filled(regions.header, radius, surface_style(&env.tokens, kind).fill)); + ui.painter().set(band_slot, Shape::rect_filled(regions.header, radius, band_color(&env.tokens, fill))); } paint_rules(ui, &props, env, inner, ®ions); paint_edge_rule(ui, &props, env, rect); diff --git a/crates/ui-contract/src/contract/container.rs b/crates/ui-contract/src/contract/container.rs index 0111ba367..c090f18d0 100644 --- a/crates/ui-contract/src/contract/container.rs +++ b/crates/ui-contract/src/contract/container.rs @@ -12,6 +12,7 @@ use crate::runtime::windows::WindowSpec; use crate::style::{GlyphRole, SurfaceKind}; use craft_behavior::{CanvasTransform, Roving}; use craft_theme::ControlSize; +use craft_theme::variant::Tone; use egui::Rect; pub use crate::runtime::env::ThemeScope; @@ -265,6 +266,16 @@ pub enum HeaderEdge { End, } +/// What the band behind a header paints. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[non_exhaustive] +pub enum BandFill { + /// A surface's fill. + Surface(SurfaceKind), + /// A tone's tinted fill (`Tone::soft_fill`). + Tone(Tone), +} + /// A header region, filled by `Container::header`. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] #[non_exhaustive] @@ -282,9 +293,9 @@ pub struct HeaderRegion { /// Whether the header has a trailing child region at its end edge, filled by `Container::controls`; the header /// itself fills the room it leaves. pub trailing: bool, - /// The surface a band behind the header paints. The band spans the container's width and starts at its top edge + /// What a band behind the header paints. The band spans the container's width and starts at its top edge /// whatever the body's padding, and has no rule. - pub fill: Option, + pub fill: Option, /// The padding on the header's two sides: the header's own tier, independent of the body's. pub inset: Pad, /// The space between a stacked header and the body, none by default. @@ -299,7 +310,14 @@ impl HeaderRegion { /// The surface of a band behind the header. pub const fn fill(mut self, kind: SurfaceKind) -> Self { - self.fill = Some(kind); + self.fill = Some(BandFill::Surface(kind)); + self + } + + /// A band behind the header in `tone`'s tinted fill (a slicer's caption band); the header's title row takes the + /// tone's ink for that fill (`TitleRow::tone`). + pub const fn tint(mut self, tone: Tone) -> Self { + self.fill = Some(BandFill::Tone(tone)); self } diff --git a/crates/ui-contract/src/contract/mod.rs b/crates/ui-contract/src/contract/mod.rs index e1cfeff3a..b43eb01be 100644 --- a/crates/ui-contract/src/contract/mod.rs +++ b/crates/ui-contract/src/contract/mod.rs @@ -59,8 +59,8 @@ pub use container::InBar; #[doc(hidden)] pub use container::InMenu; pub use container::{ - Collapse, ContainerProps, DragBand, EndRegion, FooterPlace, FooterRegion, Gap, HeaderEdge, HeaderParts, HeaderRegion, LeastWidth, Mount, Notch, - POPUP_PAD, Pad, PaneEdge, Scroll, ScrollAnchor, ScrollAxes, ShellPane, Sizing, ThemeScope, WrapWidth, + BandFill, Collapse, ContainerProps, DragBand, EndRegion, FooterPlace, FooterRegion, Gap, HeaderEdge, HeaderParts, HeaderRegion, LeastWidth, + Mount, Notch, POPUP_PAD, Pad, PaneEdge, Scroll, ScrollAnchor, ScrollAxes, ShellPane, Sizing, ThemeScope, WrapWidth, }; pub use craft_ui_macros::{CommandSet, Component, Pattern, Props, register_contract}; #[doc(hidden)] diff --git a/crates/ui-contract/src/style/cx.rs b/crates/ui-contract/src/style/cx.rs index 2396c6da7..c217b0798 100644 --- a/crates/ui-contract/src/style/cx.rs +++ b/crates/ui-contract/src/style/cx.rs @@ -230,15 +230,13 @@ impl<'a> StyleCx<'a> { /// The tinted background text and icons of `tone` sit on: the status or accent background, and the sunken surface /// for no status. pub fn tone_fill(&self, tone: Tone) -> StyleColor { - let c = &self.tokens.color; - self.color(match tone { - Tone::Accent => c.accent.soft, - Tone::Info => c.status.info.bg, - Tone::Good => c.status.good.bg, - Tone::Warn => c.status.warn.bg, - Tone::Bad => c.status.bad.bg, - _ => c.surface.sunken, - }) + self.color(tone.soft_fill(self.tokens)) + } + + /// The colour text and icons read in on [`tone_fill`](Self::tone_fill): `accent.on_soft`, the status foreground, + /// and the normal text for no status. + pub fn tone_on_fill(&self, tone: Tone) -> StyleColor { + self.color(tone.on_soft(self.tokens)) } /// A text or icon colour that is `base` when the component stands alone and the text colour of the host part diff --git a/crates/ui-contract/src/style/mod.rs b/crates/ui-contract/src/style/mod.rs index 99f92e0f0..f7dec3416 100644 --- a/crates/ui-contract/src/style/mod.rs +++ b/crates/ui-contract/src/style/mod.rs @@ -39,4 +39,4 @@ pub use paint::color as rgba_color; #[doc(hidden)] pub use paint::ink_color; pub(crate) use paint::{rule_look, scrim_color, state_layer_look}; -pub(crate) use surface::surface_style; +pub(crate) use surface::{band_color, surface_style}; diff --git a/crates/ui-contract/src/style/surface.rs b/crates/ui-contract/src/style/surface.rs index d04301840..0bc3df88e 100644 --- a/crates/ui-contract/src/style/surface.rs +++ b/crates/ui-contract/src/style/surface.rs @@ -2,6 +2,7 @@ use super::look::SurfaceKind; use super::tokens::UiTokens; +use crate::contract::BandFill; use craft_theme::{Edge, Elevation, Rgba, Stroke as StrokeStep}; use egui::{Color32, CornerRadius, Shadow, Stroke}; @@ -81,6 +82,14 @@ fn shadow(e: Elevation) -> Option { } /// The look of `kind` in `tokens`. +/// The colour a band behind a header paints: the surface's fill, or the tone's tinted fill. +pub(crate) fn band_color(tokens: &UiTokens, fill: BandFill) -> Color32 { + match fill { + BandFill::Surface(kind) => surface_style(tokens, kind).fill, + BandFill::Tone(tone) => color(tone.soft_fill(tokens)), + } +} + pub(crate) fn surface_style(tokens: &UiTokens, kind: SurfaceKind) -> SurfaceStyle { use SurfaceKind as K; let c = &tokens.color; diff --git a/crates/ui-panels/src/sections/title_row.rs b/crates/ui-panels/src/sections/title_row.rs index 8aed9da18..3c3ec415e 100644 --- a/crates/ui-panels/src/sections/title_row.rs +++ b/crates/ui-panels/src/sections/title_row.rs @@ -16,6 +16,7 @@ use crate::runtime::commands::CommandRef; use crate::runtime::env::Label; use crate::style::{ComponentStyle, IconLook, Ink, StyleCx, TextLook, TypeRole}; use craft_behavior::{CursorIcon, Flow, IconId, Metrics, Mixed, NavShape, PartKey, Roving, Sense}; +use craft_theme::variant::Tone; use craft_theme::{ControlSize, Stateful, States}; use egui::{Id, Rect, Response, Ui, Vec2}; @@ -168,6 +169,10 @@ pub struct TitleRow<'a> { #[style_key] #[prop] strength: HeaderStrength, + /// The tone of the band the row sits on (`HeaderRegion::tint`): the title takes the tone's ink for that fill. + #[style_key] + #[prop(into)] + tone: Option, /// The heading level AccessKit reports. #[prop] level: usize, @@ -220,6 +225,7 @@ impl<'a> TitleRow<'a> { TitleRow { common, strength: HeaderStrength::Normal, + tone: None, level: 2, note: None, subtitle: None, @@ -333,7 +339,8 @@ impl Component for TitleRow<'_> { HeaderStrength::Heading => (cx.tokens.size.panel_header, TypeRole::Heading, text.normal), HeaderStrength::Caption => (cx.control().height, TypeRole::Label, text.faint), }; - let (shade, ink, disabled) = (cx.color(shade), cx.ink(Ink::OnPanel), cx.color(text.disabled)); + let shade = key.tone.map_or_else(|| cx.color(shade), |tone| cx.tone_on_fill(tone)); + let (ink, disabled) = (cx.ink(Ink::OnPanel), cx.color(text.disabled)); TitleRowStyle { metrics: Metrics::new().min_size(Vec2::new(0.0, height)).gap(cx.control().gap), text: TextLook::new(role, Stateful::new(shade).off(States::new(shade, shade, shade, disabled))),