diff --git a/crates/ui-contract/src/contract/body/inherit.rs b/crates/ui-contract/src/contract/body/inherit.rs index 8669cc8f0..2e09d9699 100644 --- a/crates/ui-contract/src/contract/body/inherit.rs +++ b/crates/ui-contract/src/contract/body/inherit.rs @@ -70,7 +70,13 @@ impl BodyHooks<'_> { if core.resolved.ltr_axis { scoped.direction = Direction::Ltr; } - scoped.ink = core.ink; + // A container that paints a fill reads in that fill's ink, so what it holds (an app icon's accent parts, text with + // no colour of its own) does too: OnDark on a brand rail or scrim, OnPanel on a panel nested inside one. A frame + // paints no fill, and a container with no surface paints none either: both keep the ink they sit in. + scoped.ink = match props.surface { + None | Some(SurfaceKind::Frame) => core.ink, + Some(kind) => kind.ink(), + }; scoped.start_room(); // An inside label and a filling cell are their control's alone: a container shows neither to what it holds. scoped.inside_label = None; diff --git a/crates/ui-contract/src/primitives/draw.rs b/crates/ui-contract/src/primitives/draw.rs index b9645b533..972df0013 100644 --- a/crates/ui-contract/src/primitives/draw.rs +++ b/crates/ui-contract/src/primitives/draw.rs @@ -400,7 +400,15 @@ impl<'a> Draw<'a> { self.initial(rect, letter, StyleColor(color)); return; } - _ => super::icon::paint(self.painter, self.env.tokens, self.env.direction, rect, icon, self.data != DataMode::Full), + _ => super::icon::paint( + self.painter, + self.env.tokens, + self.env.direction, + rect, + icon, + self.data != DataMode::Full, + self.env.ink == Ink::OnDark, + ), } match icon.badge { Some(super::IconMark::Dot(badge)) => { diff --git a/crates/ui-contract/src/primitives/icon.rs b/crates/ui-contract/src/primitives/icon.rs index 57e414720..6a8bedfc8 100644 --- a/crates/ui-contract/src/primitives/icon.rs +++ b/crates/ui-contract/src/primitives/icon.rs @@ -268,8 +268,10 @@ pub fn icon_names() -> &'static [&'static str] { } /// Paints `spec`'s glyph centred in the square of `rect`'s shorter side; `Draw::icon` adds the badge. `single_ink` paints -/// every layer of an app icon in the one colour: the icon of a component in its disabled state. -pub(crate) fn paint(painter: &Painter, tokens: &UiTokens, direction: Direction, rect: Rect, spec: &IconSpec<'_>, single_ink: bool) { +/// every layer of an app icon in the one colour: the icon of a component in its disabled state. `on_dark` is whether +/// the icon stands on a strong fill or scrim (the surface's ink is `Ink::OnDark`): its accent parts then paint in the +/// icon's colour, since `accent.solid` would vanish on the brand fill. +pub(crate) fn paint(painter: &Painter, tokens: &UiTokens, direction: Direction, rect: Rect, spec: &IconSpec<'_>, single_ink: bool, on_dark: bool) { let color = spec.color_in(tokens); let rtl = direction == Direction::Rtl; let side = rect.width().min(rect.height()); @@ -283,7 +285,7 @@ pub(crate) fn paint(painter: &Painter, tokens: &UiTokens, direction: Direction, IconSource::Glyph(glyph) => glyph.paint(painter, tokens, square, color, rtl && glyph.directional() && spec.mirror == Mirror::Auto), IconSource::Svg(id) => { if let Some(icon) = SvgIcon::of(tokens, direction, side, id, spec.mirror) { - icon.paint_layers(painter, square, LayerInks::of(tokens, color, single_ink)); + icon.paint_layers(painter, square, LayerInks::of(tokens, color, single_ink, on_dark || spec.ink == Ink::OnDark)); } } IconSource::Mark(badge) => super::pointer_art::paint_badge(painter, tokens, direction, square, *badge, color), @@ -396,8 +398,13 @@ impl LayerInks { } /// The inks of an icon painted in `color`: the accent parts in `colors.accent.solid` and the fixed parts as drawn, - /// except that with `single_ink` (a disabled component) every layer paints in `color`. - pub(crate) fn of(tokens: &UiTokens, color: Color32, single_ink: bool) -> Self { + /// except that with `single_ink` (a disabled component) every layer paints in `color`, and with `on_dark` (a strong + /// fill or scrim, where the accent colour would vanish) the accent parts paint in `color` and the fixed parts stay + /// as drawn. + pub(crate) fn of(tokens: &UiTokens, color: Color32, single_ink: bool, on_dark: bool) -> Self { + if on_dark && !single_ink { + return LayerInks { ink: color, accent: color, fixed: None }; + } if single_ink { return LayerInks::single(color); } diff --git a/crates/ui-contract/src/primitives/textures/thumbnails.rs b/crates/ui-contract/src/primitives/textures/thumbnails.rs index 697befc80..431810161 100644 --- a/crates/ui-contract/src/primitives/textures/thumbnails.rs +++ b/crates/ui-contract/src/primitives/textures/thumbnails.rs @@ -486,7 +486,7 @@ fn paint_placeholder(cx: &ContentPainterCx<'_>, rect: Rect, paper: Option, rect: Rect) { let side = cx.tokens.size.icon.sm.min(rect.width().min(rect.height())); let icon = Rect::from_center_size(rect.center(), vec2(side, side)); - crate::primitives::icon::paint(&cx.painter, cx.tokens, cx.direction, icon, &IconSpec::svg(IconId::new("warning")), false); + crate::primitives::icon::paint(&cx.painter, cx.tokens, cx.direction, icon, &IconSpec::svg(IconId::new("warning")), false, false); } /// A [`ThumbSource`] for stories and tests in every crate: pictures made from the key and a revision the test sets, diff --git a/crates/ui-contract/src/testing/icon_trials.rs b/crates/ui-contract/src/testing/icon_trials.rs index a78daa48e..19c73c055 100644 --- a/crates/ui-contract/src/testing/icon_trials.rs +++ b/crates/ui-contract/src/testing/icon_trials.rs @@ -111,6 +111,36 @@ fn texel(image: &ColorImage, x: usize, y: usize) -> Color32 { image.pixels[y * 24 + x] } +/// On a strong fill or scrim (the surface's ink is `OnDark`: a brand rail) the accent parts paint in the icon's own +/// colour, since `accent.solid` would vanish on the brand fill; the fixed parts stay as drawn, and on a panel the +/// accent parts keep the accent ink. +#[trial] +fn an_accent_part_on_a_dark_surface_paints_in_the_icons_ink() { + register_app_icons(&LAYERED_SET); + let tokens = style::tokens(Mode::Light, Contrast::Standard); + let rect = Rect::from_min_max(pos2(10.0, 10.0), pos2(34.0, 34.0)); + let mut frames = Frames::new(); + let mut tints = |surface: Ink, spec_ink: Option| { + let on = ink_color(&tokens, Ink::OnDark); + let mut spec = IconSpec::svg(IconId::from(std::borrow::Cow::Borrowed(LAYERED_NAME))).color(StyleColor(on)); + if let Some(ink) = spec_ink { + spec = spec.ink(ink); + } + let record = RefCell::new(PaintRecord::default()); + let shapes = frames.frame_shapes(|ui| { + let env = DrawEnv { tokens: &tokens, direction: Direction::Ltr, ink: surface }; + Draw::new(ui.painter(), &record, env).icon(rect, &spec); + }); + textured(&shapes).into_iter().map(|(_, tint)| tint).collect::>() + }; + let on = ink_color(&tokens, Ink::OnDark); + // design: allow[raw-colour/const](a fixed layer keeps the colours it is drawn in: the trial expects the white tint that leaves them unchanged) + let fixed = Color32::WHITE; + assert_eq!(tints(Ink::OnDark, None), [on, on, fixed], "on a brand rail the accent parts read in the icon's ink"); + assert_eq!(tints(Ink::OnPanel, Some(Ink::OnDark)), [on, on, fixed], "an icon that asks for the on-dark ink does the same"); + assert_eq!(tints(Ink::OnPanel, None), [on, rgba_color(tokens.color.accent.solid), fixed], "on a panel the accent ink is kept"); +} + #[trial] fn an_accent_part_paints_in_the_accent_ink_a_fixed_part_as_drawn_and_both_take_the_disabled_ink_when_disabled() { register_app_icons(&LAYERED_SET); diff --git a/crates/ui-widgets/tests/it/buttons.rs b/crates/ui-widgets/tests/it/buttons.rs index e310965ab..5c371a4da 100644 --- a/crates/ui-widgets/tests/it/buttons.rs +++ b/crates/ui-widgets/tests/it/buttons.rs @@ -7,7 +7,7 @@ use craft_theme::ControlSize; use craft_theme::Variant; use craft_ui::buttons::{Arrangement, Button, ControlPlace, GroupLevel, SplitButton, SplitMenu, ToggleButton}; use craft_ui::contract::tooltip::DELAY; -use craft_ui::contract::{Bind, CommandSet, Entry, Hover, Props as _, Role, Show as _}; +use craft_ui::contract::{Bind, CommandSet, Entry, Hover, Props as _, Role, Show as _, ShowContainer as _}; use craft_ui::display::Picture; use craft_ui::list::ThumbAspect; use craft_ui::pickers::ComboBox; @@ -609,3 +609,70 @@ fn a_split_bodys_request_close_closes_the_menu_layer_and_it_opens_again_with_its open_menu(&mut fixture); assert!(fixture.by_label("Red").is_some(), "opened again, the menu shows its body"); } + +/// An app icon with an accent part, named for this test. +const ACCENT_ICON_NAME: &str = "zz_item4_accent_icon"; +static ACCENT_ICONS: craft_ui::primitives::AppIcons = craft_ui::primitives::AppIcons::new( + "buttons-test-accent", + None, + &[( + ACCENT_ICON_NAME, + br##""##, + )], +); + +/// The tints of the textured quads a button with the accent icon paints, in paint order, inside a stack of `surface`. +fn accent_icon_tints(surface: craft_ui::style::SurfaceKind) -> Vec { + accent_icon_tints_in(None, surface) +} + +/// As `accent_icon_tints`, with the stack of `surface` nested inside a stack of `outer`. +fn accent_icon_tints_in(outer: Option, surface: craft_ui::style::SurfaceKind) -> Vec { + craft_ui::primitives::register_app_icons(&ACCENT_ICONS); + let mut fixture = Fixture::new(move |ui| { + let inner = |ui: &mut egui::Ui| { + let _ = craft_ui::layout_components::Stack::new().surface(surface).show(ui, |ui, _| { + let _ = Button::new("Pen").icon(IconId::new(ACCENT_ICON_NAME)).variant(Variant::GHOST).show(ui); + }); + }; + match outer { + Some(outer) => { + let _ = craft_ui::layout_components::Stack::new().surface(outer).show(ui, |ui, _| inner(ui)); + } + None => inner(ui), + } + }); + fixture.settle(); + fixture + .frame_shapes() + .iter() + .filter_map(|shape| match shape { + egui::Shape::Rect(rect) if shape.texture_id() != egui::TextureId::default() => Some(rect.fill), + egui::Shape::Mesh(mesh) if mesh.texture_id != egui::TextureId::default() => mesh.vertices.first().map(|vertex| vertex.color), + _ => None, + }) + .collect() +} + +/// An app icon on a brand or scrim surface paints its accent parts in the ink of strong fills, as its plain parts: +/// the accent colour would vanish on the brand fill. On a panel the accent parts keep their own colour. +#[trial] +fn an_icons_accent_parts_on_a_brand_surface_paint_in_the_icons_ink() { + let brand = accent_icon_tints(craft_ui::style::SurfaceKind::Brand); + let panel = accent_icon_tints(craft_ui::style::SurfaceKind::Panel); + assert!(brand.len() >= 2 && panel.len() >= 2, "the icon paints an ink and an accent layer: {brand:?} {panel:?}"); + let (ink, accent) = (brand.len() - 2, brand.len() - 1); + assert_eq!(brand[ink], brand[accent], "on the brand fill the accent parts read in the icon's ink: {brand:?}"); + let (ink, accent) = (panel.len() - 2, panel.len() - 1); + assert_ne!(panel[ink], panel[accent], "on a panel the accent parts keep the accent colour: {panel:?}"); +} + +/// A panel nested inside a brand stack sets its own ink: the icon's accent parts keep the accent colour on it, not the +/// strong-fill ink of the rail around it. +#[trial] +fn an_icons_accent_parts_on_a_panel_inside_a_brand_surface_keep_the_accent_colour() { + let tints = accent_icon_tints_in(Some(craft_ui::style::SurfaceKind::Brand), craft_ui::style::SurfaceKind::Panel); + assert!(tints.len() >= 2, "the icon paints an ink and an accent layer: {tints:?}"); + let (ink, accent) = (tints.len() - 2, tints.len() - 1); + assert_ne!(tints[ink], tints[accent], "on a panel inside a brand stack the accent parts keep the accent colour: {tints:?}"); +} diff --git a/design/exceptions/craft-ui-contract/testing.toml b/design/exceptions/craft-ui-contract/testing.toml index 191f5f806..6f7dba9a5 100644 --- a/design/exceptions/craft-ui-contract/testing.toml +++ b/design/exceptions/craft-ui-contract/testing.toml @@ -6,7 +6,7 @@ kind = "marker" rule = "raw-colour/const" path = "crates/ui-contract/src/testing/icon_trials.rs" reason = "A fixed layer keeps the colours it is drawn in: the trial expects the white tint that leaves them unchanged." -count = 2 +count = 3 [[exception]] name = "face-runs-text-size"