diff --git a/crates/ui-widgets/src/pickers/select.rs b/crates/ui-widgets/src/pickers/select.rs index 934fee1c8e..f78470f3db 100644 --- a/crates/ui-widgets/src/pickers/select.rs +++ b/crates/ui-widgets/src/pickers/select.rs @@ -71,6 +71,9 @@ pub enum SelectLook { /// and padding): at least `size.length.short`, at most a field length, where a longer text elides. It changes /// width with the pick (a workspace switcher at the end of an app bar). Quiet, + /// A field filled with `surface.control`, with no edge until it has focus, as wide as its widest entry: + /// VectorCraft 0.24's status bar selects. + Filled, } /// The icon of the button in the popup header that opens the header menu. @@ -260,12 +263,12 @@ fn fits(sizing: Option, in_bar: bool) -> bool { } } -/// The closed box of a quiet select: the panel surface at the control radius, the hover fill under the pointer, and -/// an edge only in the focus colour while it has focus. -fn quiet(cx: &StyleCx<'_>) -> SurfaceLook { +/// The closed box of a quiet or filled select: `fill` at the well radius, the hover fill under the pointer, and an edge +/// only in the focus colour while it has focus. +fn borderless(cx: &StyleCx<'_>, fill: craft_theme::Rgba) -> SurfaceLook { let colors = &cx.tokens.color; - let (panel, hover) = (cx.color(colors.surface.panel), cx.color(colors.state.hover)); - let fill = Stateful::new(panel).off(States::new(panel, hover, hover, panel)); + let (rest, hover) = (cx.color(fill), cx.color(colors.state.hover)); + let fill = Stateful::new(rest).off(States::new(rest, hover, hover, rest)); // The edge takes the fill's colour in every state, so it shows only with focus. let edge = fill.clone().when(Flags::FOCUSED, cx.color(colors.line.focus)); SurfaceLook::new(fill).radius(cx.surface(SurfaceKind::Well).radius).border(edge, cx.tokens.stroke.thin).border_replaces_edge() @@ -294,7 +297,8 @@ impl Component for Select<'_, T> { let (ink, muted) = (cx.ink(Ink::OnPanel), cx.color(cx.tokens.color.text.faint)); let (surface, least_width) = match key.look { SelectLook::Field => (cx.dropdown(), 0.0), - SelectLook::Quiet => (quiet(cx), cx.tokens.size.length.short), + SelectLook::Quiet => (borderless(cx, cx.tokens.color.surface.panel), cx.tokens.size.length.short), + SelectLook::Filled => (borderless(cx, cx.tokens.color.surface.control), 0.0), }; SelectStyle { surface, diff --git a/crates/ui-widgets/src/pickers/select/stories.rs b/crates/ui-widgets/src/pickers/select/stories.rs index f4a00109fe..6c8df32ed1 100644 --- a/crates/ui-widgets/src/pickers/select/stories.rs +++ b/crates/ui-widgets/src/pickers/select/stories.rs @@ -1,6 +1,6 @@ //! Stories of `Select`. -use super::Select; +use super::{Select, SelectLook}; use crate::contract::LabelScheme; use crate::contract::{Bind, Entry, Props, Role, Show as _, ShowContainer as _, Sizing, Validity}; use crate::form::{Field, FieldGroup}; @@ -88,6 +88,13 @@ fn select_first(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { Select::new("Blend mode", data.bind("first", 0u8), &entries).show(ui).into() } +/// A filled select (`SelectLook::Filled`, VectorCraft's status bar): `surface.control` with no edge until it has focus. +#[story(Select<'static, u8>, covers = [], drive = Drive::Never)] +fn select_filled(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + let entries = modes(); + Select::new("Blend mode", data.bind("filled", 0u8), &entries).look(SelectLook::Filled).show(ui).into() +} + #[story(Select<'static, u8>, covers = [Mixed], drive = Drive::Never)] fn select_mixed(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { let entries = modes(); diff --git a/crates/ui-widgets/tests/it/pickers/select_width.rs b/crates/ui-widgets/tests/it/pickers/select_width.rs index a42d8d9fbe..d41d8d8c7e 100644 --- a/crates/ui-widgets/tests/it/pickers/select_width.rs +++ b/crates/ui-widgets/tests/it/pickers/select_width.rs @@ -43,3 +43,30 @@ fn a_quiet_select_fits_the_entry_it_shows() { let (short, long) = (quiet_width(0), quiet_width(1)); assert!(short > 0.0 && short < long, "the quiet select is narrower showing the short entry: {short} against {long}"); } + +/// A filled select (`SelectLook::Filled`, VectorCraft's status bar) paints its closed box in `surface.control`, which +/// a field does not, and keeps a field's width whichever entry it shows. +#[trial] +fn a_filled_select_lies_on_the_control_surface_at_a_fields_width() { + let shown = |look: SelectLook, held: u8| { + let rect = Rc::new(Cell::new(egui::Rect::NOTHING)); + let seen = rect.clone(); + let mut fixture = Fixture::new(move |ui| { + let entries = [Entry::new(0u8, "Essentials"), Entry::new(1u8, "Print production and prepress")]; + seen.set(Select::new("Workspace", Bind::given(held), &entries).look(look).show(ui).response.rect); + }); + fixture.settle(); + let control = craft_ui::style::StyleCx::with_root(fixture.ctx(), |cx| cx.tokens.color.surface.control).0; + let control = egui::Color32::from_rgba_unmultiplied(control[0], control[1], control[2], control[3]); + let box_rect = rect.get(); + let filled = fixture.frame_shapes().iter().any(|shape| { + matches!(shape, egui::Shape::Rect(fill) if fill.fill == control && fill.rect.expand(0.5).contains_rect(box_rect) && box_rect.expand(0.5).contains_rect(fill.rect)) + }); + (box_rect.width(), filled) + }; + let ((short, filled), (long, _)) = (shown(SelectLook::Filled, 0), shown(SelectLook::Filled, 1)); + assert!(filled, "the filled select's box is surface.control"); + assert!((short - long).abs() <= 0.5, "the filled select keeps its width: {short} against {long}"); + let (_, field) = shown(SelectLook::Field, 0); + assert!(!field, "a field's box is not surface.control"); +}