diff --git a/crates/color-ui/src/ui/paint_chip.rs b/crates/color-ui/src/ui/paint_chip.rs index 28de7cc7b..64213ce9a 100644 --- a/crates/color-ui/src/ui/paint_chip.rs +++ b/crates/color-ui/src/ui/paint_chip.rs @@ -15,7 +15,7 @@ use craft_cms::convert::CmykConverter; use craft_keymap::Mods; use craft_theme::Srgba; use craft_ui::buttons::{Button, PopupMark}; -use craft_ui::choice::{ChoiceWords, RING_SHARE, Segmented, Swatch, SwatchStyle, paint_ring}; +use craft_ui::choice::{ChoiceWords, Segmented, Swatch, SwatchStyle, paint_ring, ring_band}; use craft_ui::contract::{ Access, Activation, Bind, Common, Component, Cx, DisabledLook, Edit as _, EditResponse, Entry, Meta as _, Outcome, Overlay, OverlayUi, Overlays as _, Pages, PaintUi, PartsCx, Pointer as _, PopupKind, Props as _, Remember as _, Role, Show as _, ShowContainer as _, WordSetCx as _, @@ -372,7 +372,7 @@ pub(crate) fn paint_well(ui: &mut PaintUi<'_>, cx: &Cx<'_, impl craft_ui::contra if style.ring { let ground = ui.with_style(cx.resolved.variant, |scope| SurfaceStyle { edge: SurfaceEdge::None, shadow: None, ..scope.surface(cx.env.surface()) }); - ui.draw().surface_styled(well.shrink(well.width() * RING_SHARE), &ground); + ui.draw().surface_styled(well.shrink(ring_band(well)), &ground); } else { ui.draw().stroke(&shape, StrokeWidth::Thin, style.edge); } diff --git a/crates/color-ui/tests/it/paint_chip.rs b/crates/color-ui/tests/it/paint_chip.rs index ace5e8668..c0174ec85 100644 --- a/crates/color-ui/tests/it/paint_chip.rs +++ b/crates/color-ui/tests/it/paint_chip.rs @@ -1,13 +1,49 @@ -//! `PaintChip`'s menu mark, the arrow after the well that is part of the chip, and its swatch size. +//! `PaintChip`'s menu mark, the arrow after the well that is part of the chip, its swatch size and its ring. -use craft_color_ui::ui::{Paint, PaintChip, PaintChipSize}; +use craft_color_ui::ui::{GradientPreview, Paint, PaintChip, PaintChipSize}; use craft_theme::Srgba; use craft_ui::buttons::PopupMark; use craft_ui::contract::{Bind, Role, Show as _}; use craft_ui::testing::{Fixture, trial}; +use egui::{Rect, Shape}; use std::cell::Cell; use std::rc::Rc; +/// Every shape of the frame, groups opened. +fn flat(shapes: Vec) -> Vec { + shapes.into_iter().flat_map(|shape| if let Shape::Vec(inner) = shape { flat(inner) } else { vec![shape] }).collect() +} + +/// A ring chip wider than it is high keeps an open middle: the hole inside the band is at least a third of the chip's +/// height, and the band is as thick at the sides as at the top and bottom. Checked for a solid paint, drawn as a band +/// of the colour, and for a gradient, whose middle is covered by the surface the chip sits on. +#[trial] +fn a_wide_ring_chip_keeps_an_open_middle() { + let gradient = GradientPreview::new(vec![(0.0, Srgba::rgb(0.2, 0.4, 0.8)), (1.0, Srgba::WHITE)]); + for paint in [Paint::Solid(Srgba::rgb(0.2, 0.4, 0.8)), Paint::Gradient(gradient)] { + let mut fixture = Fixture::new(move |ui| { + let mut paint = paint.clone(); + let _ = PaintChip::new("Stroke", Bind::bound(&mut paint), Srgba::BLACK).ring(true).show(ui); + }); + fixture.settle(); + let well = fixture.by_role(Role::COLOR_WELL).first().copied().expect("the chip is a colour well").rect; + assert!(well.width() > well.height(), "the chip is wider than it is high"); + // The hole is the smallest rect drawn round the well's centre. + let hole = flat(fixture.frame_shapes()) + .into_iter() + .filter_map(|shape| match shape { + Shape::Rect(rect) => Some(rect.rect), + _ => None, + }) + .filter(|rect: &Rect| rect.center().distance(well.center()) < 0.5 && rect.width() < well.width() - 2.0) + .min_by(|a, b| a.area().total_cmp(&b.area())) + .expect("a hole inside the band"); + assert!(hole.height() >= well.height() / 3.0, "the hole is {:?} in a {:?} well", hole.size(), well.size()); + let (side, top) = (hole.left() - well.left(), hole.top() - well.top()); + assert!((side - top).abs() < 0.5, "the band is {side} at the sides and {top} at the top"); + } +} + /// A click on the menu mark, past the well, opens the chip's popover, as a click on the well does. #[trial] fn a_click_on_the_menu_mark_opens_the_popover() { diff --git a/crates/ui-widgets/src/choice/mod.rs b/crates/ui-widgets/src/choice/mod.rs index ab2265181..61285716e 100644 --- a/crates/ui-widgets/src/choice/mod.rs +++ b/crates/ui-widgets/src/choice/mod.rs @@ -18,7 +18,7 @@ pub use switch::{Switch, SwitchStyle}; pub use words::ChoiceWords; pub use swatch::{ - CellMarks, RING_SHARE, Swatch, SwatchFace, SwatchFill, SwatchImage, SwatchMetrics, SwatchOutput, SwatchShape, SwatchStyle, paint_ring, + CellMarks, RING_SHARE, Swatch, SwatchFace, SwatchFill, SwatchImage, SwatchMetrics, SwatchOutput, SwatchShape, SwatchStyle, paint_ring, ring_band, }; pub use swatch_table::{RowMarks, SwatchArrangement, SwatchRows, SwatchSpacing, SwatchTable, SwatchTableOutput, SwatchTableStyle}; diff --git a/crates/ui-widgets/src/choice/swatch.rs b/crates/ui-widgets/src/choice/swatch.rs index 7743cabaa..1ef77a713 100644 --- a/crates/ui-widgets/src/choice/swatch.rs +++ b/crates/ui-widgets/src/choice/swatch.rs @@ -390,13 +390,19 @@ impl CellMarks { } } -/// The share of a ring well's width its band of colour takes. +/// The share of a ring well's shorter side its band of colour takes. pub const RING_SHARE: f32 = 0.3; +/// The thickness of a ring well's band of colour: [`RING_SHARE`] of the well's shorter side, the same on every side. +#[must_use] +pub fn ring_band(well: Rect) -> f32 { + well.width().min(well.height()) * RING_SHARE +} + /// Paints `color` as a ring in `well`: a band of the colour round the edge, open in the middle, each side with the /// edge and the inner edge (`SwatchStyle::inner_edge`) that stand out from the colour, in `state`. pub fn paint_ring(draw: &Draw<'_>, well: Rect, color: Srgba, style: &SwatchStyle, state: &StateSet, radius: CornerRadius) { - let band = well.width() * RING_SHARE; + let band = ring_band(well); let line = draw.width(StrokeWidth::Thin); let contrast = style.inner_edge(Some(color)); let edge = state.resolve(&style.edge);