From c7716b5ea7040175aaa308917da5afb29a03cacb Mon Sep 17 00:00:00 2001 From: aiongg Date: Wed, 11 Nov 2026 20:08:09 +0000 Subject: [PATCH] Surface kinds while on: a toggle button that is on sinks, the selected segment rises on_surface draws a raised button pressed in while on; the selected segment takes the raised kind with its edge in place of the flat one, so a bevelled theme shows the raised outline. Trials for the kinds, the field's inner line and the dropdown fill. --- crates/ui-contract/tests/it/style/mod.rs | 1 + .../tests/it/style/surface_look.rs | 55 +++++++++++++++++++ crates/ui-widgets/src/buttons/face.rs | 5 +- crates/ui-widgets/src/choice/segmented.rs | 7 ++- 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 crates/ui-contract/tests/it/style/surface_look.rs diff --git a/crates/ui-contract/tests/it/style/mod.rs b/crates/ui-contract/tests/it/style/mod.rs index a41506708..ebed6b688 100644 --- a/crates/ui-contract/tests/it/style/mod.rs +++ b/crates/ui-contract/tests/it/style/mod.rs @@ -1 +1,2 @@ mod cache; +mod surface_look; diff --git a/crates/ui-contract/tests/it/style/surface_look.rs b/crates/ui-contract/tests/it/style/surface_look.rs new file mode 100644 index 000000000..9b012d499 --- /dev/null +++ b/crates/ui-contract/tests/it/style/surface_look.rs @@ -0,0 +1,55 @@ +//! What a surface look shows in each state: its kind, its edges and its fill. + +use craft_theme::{Contrast, ControlSize, Mode, Variant}; +use craft_ui::style::{Flags, State, StateMix, StateSet, Stateful, StyleColor, SurfaceKind, SurfaceLook}; +use craft_ui::testing::style::{tokens, with_cx}; +use craft_ui::testing::trial; +use egui::Color32; + +#[trial] +fn a_toggle_look_sinks_while_on_or_pressed_and_a_mixed_one_stays_raised() { + let look = SurfaceLook::new(Stateful::new(StyleColor(Color32::GRAY))) + .kind(SurfaceKind::Raised) + .kind_down(SurfaceKind::Pressed) + .kind_on(SurfaceKind::Pressed); + let at = |state: State, flags: Flags| look.kind_in(&StateSet::new(state, flags, StateMix::REST)); + assert_eq!(at(State::Rest, Flags::empty()), Some(SurfaceKind::Raised)); + assert_eq!(at(State::Pressed, Flags::empty()), Some(SurfaceKind::Pressed), "a press sinks it"); + assert_eq!(at(State::Hover, Flags::CHECKED), Some(SurfaceKind::Pressed), "on sinks it"); + assert_eq!(at(State::Rest, Flags::CHECKED | Flags::MIXED), Some(SurfaceKind::Raised), "mixed shows the off look"); + let segment = SurfaceLook::new(Stateful::new(StyleColor(Color32::GRAY))).kind_on(SurfaceKind::Raised); + let at = |state: State, flags: Flags| segment.kind_in(&StateSet::new(state, flags, StateMix::REST)); + assert_eq!(at(State::Pressed, Flags::empty()), None, "a pressed off segment draws no surface"); + assert_eq!(at(State::Rest, Flags::SELECTED), Some(SurfaceKind::Raised), "the selected segment rises"); +} + +#[trial] +fn a_focused_invalid_field_keeps_its_focus_edge_with_the_failure_line_inside() { + let ctx = egui::Context::default(); + let tokens = tokens(Mode::Dark, Contrast::Standard); + with_cx(&ctx, &tokens, Variant::SECONDARY, ControlSize::Md, |cx| { + let look = cx.field(); + let (edge, inner) = (look.border.as_ref().expect("a field draws an edge"), look.inner.as_ref().expect("a field has an inner line")); + let at = |flags: Flags| StateSet::new(State::Rest, flags, StateMix::REST); + let (focused, invalid, both) = (at(Flags::FOCUSED), at(Flags::INVALID), at(Flags::FOCUSED | Flags::INVALID)); + assert_eq!(both.resolve(edge), focused.resolve(edge), "focus keeps its edge while the value is invalid"); + assert_eq!(both.resolve(inner), invalid.resolve(edge), "the failure colour shows inside the focus edge"); + assert_eq!(invalid.resolve(inner).egui().a(), 0, "an unfocused invalid field shows the failure colour on its edge alone"); + }); +} + +#[trial] +fn a_dropdown_box_fills_under_the_pointer_and_while_open() { + let ctx = egui::Context::default(); + let tokens = tokens(Mode::Dark, Contrast::Standard); + with_cx(&ctx, &tokens, Variant::SECONDARY, ControlSize::Md, |cx| { + let look = cx.dropdown(); + let at = |state: State, flags: Flags| StateSet::new(state, flags, StateMix::REST).resolve(&look.fill); + let rest = at(State::Rest, Flags::empty()); + let hover = StateSet::new(State::Hover, Flags::empty(), StateMix::new(1.0, 0.0, 0.0)).resolve(&look.fill); + // An open popup counts as pressed (`StateSet::state`). + let open = StateSet::new(State::Pressed, Flags::OPEN, StateMix::new(0.0, 1.0, 0.0)).resolve(&look.fill); + assert_ne!(hover, rest, "the box lights under the pointer"); + assert_eq!(open, hover, "an open box keeps the hover fill"); + }); +} diff --git a/crates/ui-widgets/src/buttons/face.rs b/crates/ui-widgets/src/buttons/face.rs index 2ac8b71a8..2d8fc692d 100644 --- a/crates/ui-widgets/src/buttons/face.rs +++ b/crates/ui-widgets/src/buttons/face.rs @@ -97,9 +97,12 @@ pub(super) fn surface(cx: &StyleCx<'_>, parts: &VariantParts) -> SurfaceLook { } /// The surface of a button that shows the on look while on: the variant's surface, with the on plate and edge in -/// place of its fill and edge while on. +/// place of its fill and edge while on, and a raised surface drawn pressed in while on. pub(super) fn on_surface(cx: &StyleCx<'_>, off: &VariantParts, on: &VariantParts) -> SurfaceLook { let mut look = surface(cx, off); + if look.kind.is_some() { + look = look.kind_on(SurfaceKind::Pressed); + } look.fill = off.fill.clone().on(on.fill.off); look.border = off.edge.clone().map(|edge| edge.on(on.edge.as_ref().map_or(on.fill.off, |edge| edge.off))); look diff --git a/crates/ui-widgets/src/choice/segmented.rs b/crates/ui-widgets/src/choice/segmented.rs index 3ef123415..b2070182a 100644 --- a/crates/ui-widgets/src/choice/segmented.rs +++ b/crates/ui-widgets/src/choice/segmented.rs @@ -219,7 +219,12 @@ impl Component for Segment { let ink = Stateful::new(muted).off(States::new(muted, muted, muted, off)).on(States::new(on_selected, on_selected, on_selected, off)); let control = cx.segment_control(); SegmentStyle { - surface: SurfaceLook::new(fill).radius(UiTokens::corner(cx.tokens.shape.radius.control)).border(edge, cx.tokens.stroke.thin), + // The selected segment rises: a bevelled theme draws the raised bevel, a flat one the edge alone. + surface: SurfaceLook::new(fill) + .kind_on(SurfaceKind::Raised) + .radius(UiTokens::corner(cx.tokens.shape.radius.control)) + .border(edge, cx.tokens.stroke.thin) + .border_replaces_edge(), metrics: control.metrics(), focus: face::focus(cx), text: TextLook::new(TypeRole::Label, ink.clone()),